diff --git a/src/main/java/io/openmessaging/storage/dledger/DLedger.java b/src/main/java/io/openmessaging/storage/dledger/DLedger.java index 7eb59392..6a7fb2da 100644 --- a/src/main/java/io/openmessaging/storage/dledger/DLedger.java +++ b/src/main/java/io/openmessaging/storage/dledger/DLedger.java @@ -24,10 +24,21 @@ 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); + bootstrapDLedger(dLedgerConfig); + } + + public static void bootstrapDLedger(DLedgerConfig dLedgerConfig) { + + if (null == dLedgerConfig) { + logger.error("Bootstrap DLedger server error", new IllegalArgumentException("DLedgerConfig is null")); + System.exit(-1); + } + 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/cmdline/BossCommand.java b/src/main/java/io/openmessaging/storage/dledger/cmdline/BossCommand.java index e3cfe37f..3eea49fd 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,13 +24,13 @@ 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)); } @@ -41,13 +39,9 @@ public static void main(String 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) { + if (null != command) { command.doCommand(); } else { jc.usage(); 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..4a8a18d1 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..047d6507 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..98b9129d 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 data from DLedger server 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..598b1ae4 --- /dev/null +++ b/src/main/java/io/openmessaging/storage/dledger/cmdline/ServerCommand.java @@ -0,0 +1,75 @@ +/* + * 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 java.io.File; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Parameters(commandDescription = "Bootstrap 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 = null; + + @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 = null; + + @Override + public void doCommand() { + DLedgerConfig dLedgerConfig = buildDLedgerConfig(); + DLedger.bootstrapDLedger(dLedgerConfig); + logger.info("Bootstrap DLedger Server success", selfId); + } + + private DLedgerConfig buildDLedgerConfig() { + DLedgerConfig dLedgerConfig = new DLedgerConfig(); + dLedgerConfig.setGroup(this.group); + dLedgerConfig.setSelfId(this.selfId); + dLedgerConfig.setPeers(this.peers); + dLedgerConfig.setStoreBaseDir(this.storeBaseDir); + dLedgerConfig.setReadOnlyDataStoreDirs(this.readOnlyDataStoreDirs); + dLedgerConfig.setPeerPushThrottlePoint(this.peerPushThrottlePoint); + dLedgerConfig.setPeerPushQuota(this.peerPushQuota); + dLedgerConfig.setPreferredLeaderIds(this.preferredLeaderIds); + return dLedgerConfig; + } +}