use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.
the class CoordinatorServer method initNodeOptions.
private NodeOptions initNodeOptions(StateMachine stateMachine) {
final Configuration initialConf = new Configuration();
String svrList = svrOpts.getRaft().getInitCoordRaftSvrList();
if (!initialConf.parse(svrList)) {
throw new RuntimeException("configuration parse error, initCoordSrvList: " + svrList);
}
NodeOptions nodeOpts = new NodeOptions();
nodeOpts.setInitialConf(initialConf);
nodeOpts.setFsm(stateMachine);
final String dbPath = svrOpts.getOptions().getStoreDBOptions().getDataPath();
if (Strings.isBlank(nodeOpts.getLogUri())) {
final String logUri = Paths.get(dbPath, COORDINATOR, "log").toString();
try {
FileUtils.forceMkdir(new File(logUri));
log.info("data path created: {}", logUri);
} catch (final Throwable t) {
throw new RuntimeException("Fail to make dir for dbPath: " + logUri);
}
nodeOpts.setLogUri(logUri);
}
if (Strings.isBlank(nodeOpts.getRaftMetaUri())) {
final String meteUri = Paths.get(dbPath, COORDINATOR, "meta").toString();
nodeOpts.setRaftMetaUri(meteUri);
}
if (Strings.isBlank(nodeOpts.getSnapshotUri())) {
final String snapshotUri = Paths.get(dbPath, COORDINATOR, "snapshot").toString();
nodeOpts.setSnapshotUri(snapshotUri);
}
return nodeOpts;
}
use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.
the class RouteTable method refreshLeader.
/**
* Blocking the thread until query_leader finishes.
*
* @param groupId raft group id
* @param timeoutMs timeout millis
* @return operation status
*/
public Status refreshLeader(final CliClientService cliClientService, final String groupId, final int timeoutMs) throws InterruptedException, TimeoutException {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireTrue(timeoutMs > 0, "Invalid timeout: " + timeoutMs);
final Configuration conf = getConfiguration(groupId);
if (conf == null) {
return new Status(RaftError.ENOENT, "Group %s is not registered in RouteTable, forgot to call updateConfiguration?", groupId);
}
final Status st = Status.OK();
final CliRequests.GetLeaderRequest.Builder rb = CliRequests.GetLeaderRequest.newBuilder();
rb.setGroupId(groupId);
final CliRequests.GetLeaderRequest request = rb.build();
TimeoutException timeoutException = null;
for (final PeerId peer : conf) {
if (!cliClientService.connect(peer.getEndpoint())) {
if (st.isOk()) {
st.setError(-1, "Fail to init channel to %s", peer);
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, Fail to init channel to %s", savedMsg, peer);
}
continue;
}
final Future<Message> result = cliClientService.getLeader(peer.getEndpoint(), request, null);
try {
final Message msg = result.get(timeoutMs, TimeUnit.MILLISECONDS);
if (msg instanceof RpcRequests.ErrorResponse) {
if (st.isOk()) {
st.setError(-1, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
}
} else {
final CliRequests.GetLeaderResponse response = (CliRequests.GetLeaderResponse) msg;
updateLeader(groupId, response.getLeaderId());
return Status.OK();
}
} catch (final TimeoutException e) {
timeoutException = e;
} catch (final ExecutionException e) {
if (st.isOk()) {
st.setError(-1, e.getMessage());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, e.getMessage());
}
}
}
if (timeoutException != null) {
throw timeoutException;
}
return st;
}
use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.
the class RouteTable method updateConfiguration.
/**
* Update configuration of group in route table.
*
* @param groupId raft group id
* @param confStr configuration string
* @return true on success
*/
public boolean updateConfiguration(final String groupId, final String confStr) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireTrue(!StringUtils.isBlank(confStr), "Blank configuration");
final Configuration conf = new Configuration();
if (conf.parse(confStr)) {
return updateConfiguration(groupId, conf);
} else {
LOG.error("Fail to parse confStr: {}", confStr);
return false;
}
}
use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.
the class RouteTable method getConfiguration.
/**
* Get the configuration by groupId, returns null when not found.
*
* @param groupId raft group id
* @return configuration of the group id
*/
public Configuration getConfiguration(final String groupId) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
final GroupConf gc = this.groupConfTable.get(groupId);
if (gc == null) {
return null;
}
final StampedLock stampedLock = gc.stampedLock;
long stamp = stampedLock.tryOptimisticRead();
Configuration conf = gc.conf;
if (!stampedLock.validate(stamp)) {
stamp = stampedLock.readLock();
try {
conf = gc.conf;
} finally {
stampedLock.unlockRead(stamp);
}
}
return conf;
}
use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.
the class RouteTable method refreshConfiguration.
public Status refreshConfiguration(final CliClientService cliClientService, final String groupId, final int timeoutMs) throws InterruptedException, TimeoutException {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireTrue(timeoutMs > 0, "Invalid timeout: " + timeoutMs);
final Configuration conf = getConfiguration(groupId);
if (conf == null) {
return new Status(RaftError.ENOENT, "Group %s is not registered in RouteTable, forgot to call updateConfiguration?", groupId);
}
final Status st = Status.OK();
PeerId leaderId = selectLeader(groupId);
if (leaderId == null) {
refreshLeader(cliClientService, groupId, timeoutMs);
leaderId = selectLeader(groupId);
}
if (leaderId == null) {
st.setError(-1, "Fail to get leader of group %s", groupId);
return st;
}
if (!cliClientService.connect(leaderId.getEndpoint())) {
st.setError(-1, "Fail to init channel to %s", leaderId);
return st;
}
final CliRequests.GetPeersRequest.Builder rb = CliRequests.GetPeersRequest.newBuilder();
rb.setGroupId(groupId);
rb.setLeaderId(leaderId.toString());
try {
final Message result = cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), null).get(timeoutMs, TimeUnit.MILLISECONDS);
if (result instanceof CliRequests.GetPeersResponse) {
final CliRequests.GetPeersResponse resp = (CliRequests.GetPeersResponse) result;
final Configuration newConf = new Configuration();
for (final String peerIdStr : resp.getPeersList()) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addPeer(newPeer);
}
if (!conf.equals(newConf)) {
LOG.info("Configuration of replication group {} changed from {} to {}", groupId, conf, newConf);
}
updateConfiguration(groupId, newConf);
} else {
final RpcRequests.ErrorResponse resp = (RpcRequests.ErrorResponse) result;
st.setError(resp.getErrorCode(), resp.getErrorMsg());
}
} catch (final Exception e) {
st.setError(-1, e.getMessage());
}
return st;
}
Aggregations