diff --git a/src/main/java/io/openmessaging/storage/dledger/DLedger.java b/src/main/java/io/openmessaging/storage/dledger/DLedger.java index 7eb59392..3a4a3533 100644 --- a/src/main/java/io/openmessaging/storage/dledger/DLedger.java +++ b/src/main/java/io/openmessaging/storage/dledger/DLedger.java @@ -25,9 +25,14 @@ public class DLedger { private static Logger logger = LoggerFactory.getLogger(DLedger.class); + @Deprecated public static void main(String args[]) { DLedgerConfig dLedgerConfig = new DLedgerConfig(); JCommander.newBuilder().addObject(dLedgerConfig).build().parse(args); + startup(dLedgerConfig); + } + + public static void startup(DLedgerConfig dLedgerConfig) { DLedgerServer dLedgerServer = new DLedgerServer(dLedgerConfig); dLedgerServer.startup(); logger.info("[{}] group {} start ok with config {}", dLedgerConfig.getSelfId(), dLedgerConfig.getGroup(), JSON.toJSONString(dLedgerConfig)); diff --git a/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java b/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java index 5cae5d2b..b8742495 100644 --- a/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java +++ b/src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java @@ -47,6 +47,12 @@ public class DLedgerConfig { @Parameter(names = {"--peer-push-quotas"}, description = "The quotas of the pusher") private int peerPushQuota = 20 * 1024 * 1024; + @Parameter(names = {"--preferred-leader-id"}, description = "Preferred LeaderId") + private String preferredLeaderIds; + + @Parameter(names = {"--config-file", "-c"}, description = "Dledger config properties file") + private String configFile; //config properties file + private String storeType = FILE; //FILE, MEMORY private String dataStorePath; @@ -82,11 +88,9 @@ public class DLedgerConfig { private boolean enablePushToFollower = true; - @Parameter(names = {"--preferred-leader-id"}, description = "Preferred LeaderId") - private String preferredLeaderIds; private long maxLeadershipTransferWaitIndex = 1000; - private int minTakeLeadershipVoteIntervalMs = 30; - private int maxTakeLeadershipVoteIntervalMs = 100; + private int minTakeLeadershipVoteIntervalMs = 30; + private int maxTakeLeadershipVoteIntervalMs = 100; private boolean isEnableBatchPush = false; private int maxBatchPushSize = 4 * 1024; @@ -418,4 +422,12 @@ public String getReadOnlyDataStoreDirs() { public void setReadOnlyDataStoreDirs(String readOnlyDataStoreDirs) { this.readOnlyDataStoreDirs = readOnlyDataStoreDirs; } + + public String getConfigFile() { + return configFile; + } + + public void setConfigFile(String configFile) { + this.configFile = configFile; + } } diff --git a/src/main/java/io/openmessaging/storage/dledger/cmdline/AppendCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/AppendCommand.java index 5ef36d8a..ffa807aa 100644 --- a/src/main/java/io/openmessaging/storage/dledger/cmdline/AppendCommand.java +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/AppendCommand.java @@ -18,11 +18,13 @@ import com.alibaba.fastjson.JSON; import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; import io.openmessaging.storage.dledger.client.DLedgerClient; import io.openmessaging.storage.dledger.protocol.AppendEntryResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Parameters(commandDescription = "append data to dledger server") public class AppendCommand extends BaseCommand { private static Logger logger = LoggerFactory.getLogger(AppendCommand.class); diff --git a/src/main/java/io/openmessaging/storage/dledger/cmdline/BossCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/BossCommand.java index e3cfe37f..c2e572fa 100644 --- a/src/main/java/io/openmessaging/storage/dledger/cmdline/BossCommand.java +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/BossCommand.java @@ -17,8 +17,6 @@ package io.openmessaging.storage.dledger.cmdline; import com.beust.jcommander.JCommander; -import io.openmessaging.storage.dledger.DLedger; -import io.openmessaging.storage.dledger.DLedgerConfig; import java.util.HashMap; import java.util.Map; @@ -26,25 +24,20 @@ public class BossCommand { public static void main(String args[]) { Map commands = new HashMap<>(); + commands.put("server", new ServerCommand()); commands.put("append", new AppendCommand()); commands.put("get", new GetCommand()); commands.put("readFile", new ReadFileCommand()); commands.put("leadershipTransfer", new LeadershipTransferCommand()); JCommander.Builder builder = JCommander.newBuilder(); - builder.addCommand("server", new DLedgerConfig()); for (String cmd : commands.keySet()) { builder.addCommand(cmd, commands.get(cmd)); } JCommander jc = builder.build(); jc.parse(args); - if (jc.getParsedCommand() == null) { jc.usage(); - } else if (jc.getParsedCommand().equals("server")) { - String[] subArgs = new String[args.length - 1]; - System.arraycopy(args, 1, subArgs, 0, subArgs.length); - DLedger.main(subArgs); } else { BaseCommand command = commands.get(jc.getParsedCommand()); if (command != null) { diff --git a/src/main/java/io/openmessaging/storage/dledger/cmdline/GetCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/GetCommand.java index 51204c0e..5a3aae7f 100644 --- a/src/main/java/io/openmessaging/storage/dledger/cmdline/GetCommand.java +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/GetCommand.java @@ -18,12 +18,14 @@ import com.alibaba.fastjson.JSON; import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; import io.openmessaging.storage.dledger.client.DLedgerClient; import io.openmessaging.storage.dledger.entry.DLedgerEntry; import io.openmessaging.storage.dledger.protocol.GetEntriesResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Parameters(commandDescription = "get data from dledger server") public class GetCommand extends BaseCommand { private static Logger logger = LoggerFactory.getLogger(GetCommand.class); diff --git a/src/main/java/io/openmessaging/storage/dledger/cmdline/LeadershipTransferCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/LeadershipTransferCommand.java index d0c81a6a..897c033f 100644 --- a/src/main/java/io/openmessaging/storage/dledger/cmdline/LeadershipTransferCommand.java +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/LeadershipTransferCommand.java @@ -18,12 +18,14 @@ import com.alibaba.fastjson.JSON; import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; import io.openmessaging.storage.dledger.client.DLedgerClient; import io.openmessaging.storage.dledger.protocol.DLedgerResponseCode; import io.openmessaging.storage.dledger.protocol.LeadershipTransferResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Parameters(commandDescription = "leadership transfer") public class LeadershipTransferCommand extends BaseCommand { private static Logger logger = LoggerFactory.getLogger(LeadershipTransferCommand.class); diff --git a/src/main/java/io/openmessaging/storage/dledger/cmdline/ReadFileCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/ReadFileCommand.java index 4ee8cc7d..9029ef1d 100644 --- a/src/main/java/io/openmessaging/storage/dledger/cmdline/ReadFileCommand.java +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/ReadFileCommand.java @@ -18,6 +18,7 @@ import com.alibaba.fastjson.JSON; import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; import io.openmessaging.storage.dledger.entry.DLedgerEntry; import io.openmessaging.storage.dledger.entry.DLedgerEntryCoder; import io.openmessaging.storage.dledger.store.file.DLedgerMmapFileStore; @@ -28,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Parameters(commandDescription = "read dledger data file") public class ReadFileCommand extends BaseCommand { private static Logger logger = LoggerFactory.getLogger(ReadFileCommand.class); diff --git a/src/main/java/io/openmessaging/storage/dledger/cmdline/ServerCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/ServerCommand.java new file mode 100644 index 00000000..a12711ef --- /dev/null +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/ServerCommand.java @@ -0,0 +1,111 @@ +/* + * Copyright 2017-2022 The DLedger Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.openmessaging.storage.dledger.cmdline; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import io.openmessaging.storage.dledger.DLedger; +import io.openmessaging.storage.dledger.DLedgerConfig; +import io.openmessaging.storage.dledger.utils.IOUtils; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Properties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters(commandDescription = "start dledger server") +public class ServerCommand extends BaseCommand { + + private static Logger logger = LoggerFactory.getLogger(ServerCommand.class); + + @Parameter(names = {"--group", "-g"}, description = "Group of this server") + private String group = "default"; + + @Parameter(names = {"--id", "-i"}, description = "Self id of this server") + private String selfId = "n0"; + + @Parameter(names = {"--peers", "-p"}, description = "Peer info of this server") + private String peers = "n0-localhost:20911"; + + @Parameter(names = {"--store-base-dir", "-s"}, description = "The base store dir of this server") + private String storeBaseDir = File.separator + "tmp" + File.separator + "dledgerstore"; + + @Parameter(names = {"--read-only-data-store-dirs"}, description = "The dirs of this server to be read only") + private String readOnlyDataStoreDirs; + + @Parameter(names = {"--peer-push-throttle-point"}, description = "When the follower is behind the leader more than this value, it will trigger the throttle") + private int peerPushThrottlePoint = 300 * 1024 * 1024; + + @Parameter(names = {"--peer-push-quotas"}, description = "The quotas of the pusher") + private int peerPushQuota = 20 * 1024 * 1024; + + @Parameter(names = {"--preferred-leader-id"}, description = "Preferred LeaderId") + private String preferredLeaderIds; + + @Parameter(names = {"--config-file", "-c"}, description = "Dledger config properties file") + private String configFile; + + @Override + public void doCommand() { + + DLedgerConfig dLedgerConfig = new DLedgerConfig(); + mergeCommandAttributeIntoConfig(dLedgerConfig); + mergePropertiesIntoConfig(dLedgerConfig); + DLedger.startup(dLedgerConfig); + logger.info("[{}] Dledger Server started", selfId); + } + + private void mergePropertiesIntoConfig(DLedgerConfig dLedgerConfig) { + + if (null == configFile || configFile.trim().isEmpty()) { + return; + } + Properties properties = new Properties(); + try { + properties.load(new FileInputStream(configFile)); + } catch (IOException e) { + throw new IllegalArgumentException(e.getMessage()); + } + IOUtils.properties2Object(properties, dLedgerConfig); + } + + private void mergeCommandAttributeIntoConfig(DLedgerConfig dLedgerConfig) { + Field[] fields = ServerCommand.this.getClass().getDeclaredFields(); + if (fields == null || fields.length == 0) { + return; + } + Properties properties = new Properties(); + for (Field field : fields) { + if (field.getType() != String.class) { + continue; + } + field.setAccessible(true); + try { + Object value = field.get(ServerCommand.this); + if (value == null) { + continue; + } + properties.put(field.getName(), value); + } catch (IllegalAccessException e) { + logger.warn("get field value error", e); + } + } + IOUtils.properties2Object(properties, dLedgerConfig); + } +} diff --git a/src/test/java/io/openmessaging/storage/dledger/DLedgerConfigTest.java b/src/test/java/io/openmessaging/storage/dledger/DLedgerConfigTest.java index e4f557bf..8939ef9f 100644 --- a/src/test/java/io/openmessaging/storage/dledger/DLedgerConfigTest.java +++ b/src/test/java/io/openmessaging/storage/dledger/DLedgerConfigTest.java @@ -27,10 +27,11 @@ public void testParseArguments() { DLedgerConfig dLedgerConfig = new DLedgerConfig(); JCommander.Builder builder = JCommander.newBuilder().addObject(dLedgerConfig); JCommander jc = builder.build(); - jc.parse("-i", "n1", "-g", "test", "-p", "n1-localhost:21911", "-s", "/tmp"); + jc.parse("-i", "n1", "-g", "test", "-p", "n1-localhost:21911", "-s", "/tmp", "-c", "/tmp/dledger/dledger.properties"); Assertions.assertEquals("n1", dLedgerConfig.getSelfId()); Assertions.assertEquals("test", dLedgerConfig.getGroup()); Assertions.assertEquals("n1-localhost:21911", dLedgerConfig.getPeers()); Assertions.assertEquals("/tmp", dLedgerConfig.getStoreBaseDir()); + Assertions.assertEquals("/tmp/dledger/dledger.properties", dLedgerConfig.getConfigFile()); } }