use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class LogManagerImpl method oldConfFromMeta.
private Configuration oldConfFromMeta(final SnapshotMeta meta) {
final Configuration oldConf = new Configuration();
for (int i = 0; i < meta.getOldPeersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getOldPeers(i));
oldConf.addPeer(peer);
}
for (int i = 0; i < meta.getOldLearnersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getOldLearners(i));
oldConf.addLearner(peer);
}
return oldConf;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class LogManagerImpl method confFromMeta.
private Configuration confFromMeta(final SnapshotMeta meta) {
final Configuration conf = new Configuration();
for (int i = 0; i < meta.getPeersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getPeers(i));
conf.addPeer(peer);
}
for (int i = 0; i < meta.getLearnersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getLearners(i));
conf.addLearner(peer);
}
return conf;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class LogManagerImpl method setSnapshot.
@Override
public void setSnapshot(final SnapshotMeta meta) {
LOG.debug("set snapshot: {}.", meta);
boolean doUnlock = true;
this.writeLock.lock();
try {
if (meta.getLastIncludedIndex() <= this.lastSnapshotId.getIndex()) {
return;
}
final Configuration conf = confFromMeta(meta);
final Configuration oldConf = oldConfFromMeta(meta);
final ConfigurationEntry entry = new ConfigurationEntry(new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm()), conf, oldConf);
this.configManager.setSnapshot(entry);
final long term = unsafeGetTerm(meta.getLastIncludedIndex());
final long savedLastSnapshotIndex = this.lastSnapshotId.getIndex();
this.lastSnapshotId.setIndex(meta.getLastIncludedIndex());
this.lastSnapshotId.setTerm(meta.getLastIncludedTerm());
if (this.lastSnapshotId.compareTo(this.appliedId) > 0) {
this.appliedId = this.lastSnapshotId.copy();
}
if (term == 0) {
// last_included_index is larger than last_index
// FIXME: what if last_included_index is less than first_index?
doUnlock = false;
// unlock in truncatePrefix
truncatePrefix(meta.getLastIncludedIndex() + 1, this.writeLock);
} else if (term == meta.getLastIncludedTerm()) {
// TODO if there are still be need?
if (savedLastSnapshotIndex > 0) {
doUnlock = false;
// unlock in truncatePrefix
truncatePrefix(savedLastSnapshotIndex + 1, this.writeLock);
}
} else {
if (!reset(meta.getLastIncludedIndex() + 1)) {
LOG.warn("Reset log manager failed, nextLogIndex={}.", meta.getLastIncludedIndex() + 1);
}
}
} finally {
if (doUnlock) {
this.writeLock.unlock();
}
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class LogManagerImpl method appendEntries.
@Override
public void appendEntries(final List<LogEntry> entries, final StableClosure done) {
assert (done != null);
Requires.requireNonNull(done, "done");
if (this.hasError) {
entries.clear();
Utils.runClosureInThread(done, new Status(RaftError.EIO, "Corrupted LogStorage"));
return;
}
boolean doUnlock = true;
this.writeLock.lock();
try {
if (!entries.isEmpty() && !checkAndResolveConflict(entries, done, this.writeLock)) {
// If checkAndResolveConflict returns false, the done will be called in it.
entries.clear();
return;
}
for (int i = 0; i < entries.size(); i++) {
final LogEntry entry = entries.get(i);
// Set checksum after checkAndResolveConflict
if (this.raftOptions.isEnableLogEntryChecksum()) {
entry.setChecksum(entry.checksum());
}
if (entry.getType() == EntryType.ENTRY_TYPE_CONFIGURATION) {
Configuration oldConf = new Configuration();
if (entry.getOldPeers() != null) {
oldConf = new Configuration(entry.getOldPeers(), entry.getOldLearners());
}
final ConfigurationEntry conf = new ConfigurationEntry(entry.getId(), new Configuration(entry.getPeers(), entry.getLearners()), oldConf);
this.configManager.add(conf);
}
}
if (!entries.isEmpty()) {
done.setFirstLogIndex(entries.get(0).getId().getIndex());
this.logsInMemory.addAll(entries);
}
done.setEntries(entries);
doUnlock = false;
if (!wakeupAllWaiter(this.writeLock)) {
notifyLastLogIndexListeners();
}
// publish event out of lock
this.diskQueue.publishEvent((event, sequence) -> {
event.reset();
event.type = EventType.OTHER;
event.done = done;
});
} finally {
if (doUnlock) {
this.writeLock.unlock();
}
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class ChangePeersRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final ChangePeersRequest request, final RpcRequestClosure done) {
final List<PeerId> oldConf = ctx.node.listPeers();
final Configuration conf = new Configuration();
for (final String peerIdStr : request.getNewPeersList()) {
final PeerId peer = new PeerId();
if (peer.parse(peerIdStr)) {
conf.addPeer(peer);
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
}
}
LOG.info("Receive ChangePeersRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), conf);
ctx.node.changePeers(conf, status -> {
if (!status.isOk()) {
done.run(status);
} else {
ChangePeersResponse.Builder rb = ChangePeersResponse.newBuilder();
for (final PeerId peer : oldConf) {
rb.addOldPeers(peer.toString());
}
for (final PeerId peer : conf) {
rb.addNewPeers(peer.toString());
}
done.sendResponse(rb.build());
}
});
return null;
}
Aggregations