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 #171] Dledger Support properties configuration file #182

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions src/main/java/io/openmessaging/storage/dledger/DLedger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/io/openmessaging/storage/dledger/DLedgerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,27 @@
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));
}
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) {
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 dledger 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,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}