use of com.alipay.sofa.jraft.conf.ConfigurationEntry in project sofa-jraft by sofastack.
the class NodeImpl method checkAndSetConfiguration.
/**
* Check and set configuration for node.At the same time, if configuration is changed,
* then compute and update the target priority value.
*
* @param inLock whether the writeLock has already been locked in other place.
*/
private void checkAndSetConfiguration(final boolean inLock) {
if (!inLock) {
this.writeLock.lock();
}
try {
final ConfigurationEntry prevConf = this.conf;
this.conf = this.logManager.checkAndSetConfiguration(prevConf);
if (this.conf != prevConf) {
// Update target priority value
final int prevTargetPriority = this.targetPriority;
this.targetPriority = getMaxPriorityOfNodes(this.conf.getConf().getPeers());
if (prevTargetPriority != this.targetPriority) {
LOG.info("Node {} target priority value has changed from: {}, to: {}.", getNodeId(), prevTargetPriority, this.targetPriority);
}
this.electionTimeoutCounter = 0;
}
} finally {
if (!inLock) {
this.writeLock.unlock();
}
}
}
use of com.alipay.sofa.jraft.conf.ConfigurationEntry 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.ConfigurationEntry 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.ConfigurationEntry in project sofa-jraft by sofastack.
the class ReplicatorGroupTest method testFindTheNextCandidateWithPriority2.
@Test
public void testFindTheNextCandidateWithPriority2() {
final PeerId p1 = new PeerId("localhost", 18881, 0, 0);
final PeerId p2 = new PeerId("localhost", 18882, 0, 0);
final PeerId p3 = new PeerId("localhost", 18883, 0, -1);
Mockito.when(this.rpcService.connect(p1.getEndpoint())).thenReturn(true);
Mockito.when(this.rpcService.connect(p2.getEndpoint())).thenReturn(true);
Mockito.when(this.rpcService.connect(p3.getEndpoint())).thenReturn(true);
this.replicatorGroup.resetTerm(1);
this.replicatorGroup.addReplicator(p1);
this.replicatorGroup.addReplicator(p2);
this.replicatorGroup.addReplicator(p3);
final ConfigurationEntry conf = new ConfigurationEntry();
conf.setConf(new Configuration(Arrays.asList(p1, p2, p3)));
final PeerId p = this.replicatorGroup.findTheNextCandidate(conf);
assertEquals(p3, p);
}
use of com.alipay.sofa.jraft.conf.ConfigurationEntry in project sofa-jraft by sofastack.
the class ReplicatorGroupTest method testFindTheNextCandidateWithPriority1.
@Test
public void testFindTheNextCandidateWithPriority1() {
final PeerId p1 = new PeerId("localhost", 18881, 0, 60);
final PeerId p2 = new PeerId("localhost", 18882, 0, 80);
final PeerId p3 = new PeerId("localhost", 18883, 0, 100);
Mockito.when(this.rpcService.connect(p1.getEndpoint())).thenReturn(true);
Mockito.when(this.rpcService.connect(p2.getEndpoint())).thenReturn(true);
Mockito.when(this.rpcService.connect(p3.getEndpoint())).thenReturn(true);
this.replicatorGroup.resetTerm(1);
this.replicatorGroup.addReplicator(p1);
this.replicatorGroup.addReplicator(p2);
this.replicatorGroup.addReplicator(p3);
final ConfigurationEntry conf = new ConfigurationEntry();
conf.setConf(new Configuration(Arrays.asList(p1, p2, p3)));
final PeerId p = this.replicatorGroup.findTheNextCandidate(conf);
assertEquals(p3, p);
}
Aggregations