Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #192]Optimize BossCommand code and add ServerCommand #194

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/io/openmessaging/storage/dledger/DLedger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@
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;

public class BossCommand {

public static void main(String args[]) {
Map<String, BaseCommand> 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));
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}