Skip to content

Commit 66e797b

Browse files
Merge pull request #194 from mxsm/dledger-192
[ISSUE #192]Optimize BossCommand code and add ServerCommand
2 parents 44ebb34 + 4a3b387 commit 66e797b

File tree

6 files changed

+95
-9
lines changed

6 files changed

+95
-9
lines changed

src/main/java/io/openmessaging/storage/dledger/DLedger.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@
2424
public class DLedger {
2525

2626
private static Logger logger = LoggerFactory.getLogger(DLedger.class);
27-
27+
28+
@Deprecated
2829
public static void main(String args[]) {
2930
DLedgerConfig dLedgerConfig = new DLedgerConfig();
3031
JCommander.newBuilder().addObject(dLedgerConfig).build().parse(args);
32+
bootstrapDLedger(dLedgerConfig);
33+
}
34+
35+
public static void bootstrapDLedger(DLedgerConfig dLedgerConfig) {
36+
37+
if (null == dLedgerConfig) {
38+
logger.error("Bootstrap DLedger server error", new IllegalArgumentException("DLedgerConfig is null"));
39+
System.exit(-1);
40+
}
41+
3142
DLedgerServer dLedgerServer = new DLedgerServer(dLedgerConfig);
3243
dLedgerServer.startup();
3344
logger.info("[{}] group {} start ok with config {}", dLedgerConfig.getSelfId(), dLedgerConfig.getGroup(), JSON.toJSONString(dLedgerConfig));

src/main/java/io/openmessaging/storage/dledger/cmdline/BossCommand.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@
1717
package io.openmessaging.storage.dledger.cmdline;
1818

1919
import com.beust.jcommander.JCommander;
20-
import io.openmessaging.storage.dledger.DLedger;
21-
import io.openmessaging.storage.dledger.DLedgerConfig;
2220
import java.util.HashMap;
2321
import java.util.Map;
2422

2523
public class BossCommand {
2624

2725
public static void main(String args[]) {
2826
Map<String, BaseCommand> commands = new HashMap<>();
27+
commands.put("server", new ServerCommand());
2928
commands.put("append", new AppendCommand());
3029
commands.put("get", new GetCommand());
3130
commands.put("readFile", new ReadFileCommand());
3231
commands.put("leadershipTransfer", new LeadershipTransferCommand());
3332

3433
JCommander.Builder builder = JCommander.newBuilder();
35-
builder.addCommand("server", new DLedgerConfig());
3634
for (String cmd : commands.keySet()) {
3735
builder.addCommand(cmd, commands.get(cmd));
3836
}
@@ -41,13 +39,9 @@ public static void main(String args[]) {
4139

4240
if (jc.getParsedCommand() == null) {
4341
jc.usage();
44-
} else if (jc.getParsedCommand().equals("server")) {
45-
String[] subArgs = new String[args.length - 1];
46-
System.arraycopy(args, 1, subArgs, 0, subArgs.length);
47-
DLedger.main(subArgs);
4842
} else {
4943
BaseCommand command = commands.get(jc.getParsedCommand());
50-
if (command != null) {
44+
if (null != command) {
5145
command.doCommand();
5246
} else {
5347
jc.usage();

src/main/java/io/openmessaging/storage/dledger/cmdline/GetCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import com.alibaba.fastjson.JSON;
2020
import com.beust.jcommander.Parameter;
21+
import com.beust.jcommander.Parameters;
2122
import io.openmessaging.storage.dledger.client.DLedgerClient;
2223
import io.openmessaging.storage.dledger.entry.DLedgerEntry;
2324
import io.openmessaging.storage.dledger.protocol.GetEntriesResponse;
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
2627

28+
@Parameters(commandDescription = "Get data from DLedger server")
2729
public class GetCommand extends BaseCommand {
2830

2931
private static Logger logger = LoggerFactory.getLogger(GetCommand.class);

src/main/java/io/openmessaging/storage/dledger/cmdline/LeadershipTransferCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import com.alibaba.fastjson.JSON;
2020
import com.beust.jcommander.Parameter;
21+
import com.beust.jcommander.Parameters;
2122
import io.openmessaging.storage.dledger.client.DLedgerClient;
2223
import io.openmessaging.storage.dledger.protocol.DLedgerResponseCode;
2324
import io.openmessaging.storage.dledger.protocol.LeadershipTransferResponse;
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
2627

28+
@Parameters(commandDescription = "Leadership transfer")
2729
public class LeadershipTransferCommand extends BaseCommand {
2830

2931
private static Logger logger = LoggerFactory.getLogger(LeadershipTransferCommand.class);

src/main/java/io/openmessaging/storage/dledger/cmdline/ReadFileCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.alibaba.fastjson.JSON;
2020
import com.beust.jcommander.Parameter;
21+
import com.beust.jcommander.Parameters;
2122
import io.openmessaging.storage.dledger.entry.DLedgerEntry;
2223
import io.openmessaging.storage.dledger.entry.DLedgerEntryCoder;
2324
import io.openmessaging.storage.dledger.store.file.DLedgerMmapFileStore;
@@ -28,6 +29,7 @@
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

32+
@Parameters(commandDescription = "Read data from DLedger server data file")
3133
public class ReadFileCommand extends BaseCommand {
3234

3335
private static Logger logger = LoggerFactory.getLogger(ReadFileCommand.class);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017-2022 The DLedger Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.openmessaging.storage.dledger.cmdline;
18+
19+
import com.beust.jcommander.Parameter;
20+
import com.beust.jcommander.Parameters;
21+
import io.openmessaging.storage.dledger.DLedger;
22+
import io.openmessaging.storage.dledger.DLedgerConfig;
23+
import java.io.File;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
@Parameters(commandDescription = "Bootstrap DLedger Server")
28+
public class ServerCommand extends BaseCommand {
29+
30+
private static Logger logger = LoggerFactory.getLogger(ServerCommand.class);
31+
32+
@Parameter(names = {"--group", "-g"}, description = "Group of this server")
33+
private String group = "default";
34+
35+
@Parameter(names = {"--id", "-i"}, description = "Self id of this server")
36+
private String selfId = "n0";
37+
38+
@Parameter(names = {"--peers", "-p"}, description = "Peer info of this server")
39+
private String peers = "n0-localhost:20911";
40+
41+
@Parameter(names = {"--store-base-dir", "-s"}, description = "The base store dir of this server")
42+
private String storeBaseDir = File.separator + "tmp" + File.separator + "dledgerstore";
43+
44+
@Parameter(names = {"--read-only-data-store-dirs"}, description = "The dirs of this server to be read only")
45+
private String readOnlyDataStoreDirs = null;
46+
47+
@Parameter(names = {"--peer-push-throttle-point"}, description = "When the follower is behind the leader more than this value, it will trigger the throttle")
48+
private int peerPushThrottlePoint = 300 * 1024 * 1024;
49+
50+
@Parameter(names = {"--peer-push-quotas"}, description = "The quotas of the pusher")
51+
private int peerPushQuota = 20 * 1024 * 1024;
52+
53+
@Parameter(names = {"--preferred-leader-id"}, description = "Preferred LeaderId")
54+
private String preferredLeaderIds = null;
55+
56+
@Override
57+
public void doCommand() {
58+
DLedgerConfig dLedgerConfig = buildDLedgerConfig();
59+
DLedger.bootstrapDLedger(dLedgerConfig);
60+
logger.info("Bootstrap DLedger Server success", selfId);
61+
}
62+
63+
private DLedgerConfig buildDLedgerConfig() {
64+
DLedgerConfig dLedgerConfig = new DLedgerConfig();
65+
dLedgerConfig.setGroup(this.group);
66+
dLedgerConfig.setSelfId(this.selfId);
67+
dLedgerConfig.setPeers(this.peers);
68+
dLedgerConfig.setStoreBaseDir(this.storeBaseDir);
69+
dLedgerConfig.setReadOnlyDataStoreDirs(this.readOnlyDataStoreDirs);
70+
dLedgerConfig.setPeerPushThrottlePoint(this.peerPushThrottlePoint);
71+
dLedgerConfig.setPeerPushQuota(this.peerPushQuota);
72+
dLedgerConfig.setPreferredLeaderIds(this.preferredLeaderIds);
73+
return dLedgerConfig;
74+
}
75+
}

0 commit comments

Comments
 (0)