Search in sources :

Example 6 with ConfigurationEntry

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();
        }
    }
}
Also used : ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry)

Example 7 with ConfigurationEntry

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();
        }
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogId(com.alipay.sofa.jraft.entity.LogId)

Example 8 with ConfigurationEntry

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();
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 9 with ConfigurationEntry

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);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 10 with ConfigurationEntry

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);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)13 LogId (com.alipay.sofa.jraft.entity.LogId)6 Status (com.alipay.sofa.jraft.Status)5 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 Test (org.junit.Test)5 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)4 PeerId (com.alipay.sofa.jraft.entity.PeerId)3 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)3 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)1 NodeId (com.alipay.sofa.jraft.entity.NodeId)1 BallotBoxOptions (com.alipay.sofa.jraft.option.BallotBoxOptions)1 ReadOnlyServiceOptions (com.alipay.sofa.jraft.option.ReadOnlyServiceOptions)1 ReplicatorGroupOptions (com.alipay.sofa.jraft.option.ReplicatorGroupOptions)1 DefaultRaftClientService (com.alipay.sofa.jraft.rpc.impl.core.DefaultRaftClientService)1 LogManager (com.alipay.sofa.jraft.storage.LogManager)1 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)1 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)1 NamedThreadFactory (com.alipay.sofa.jraft.util.NamedThreadFactory)1 RepeatedTimer (com.alipay.sofa.jraft.util.RepeatedTimer)1 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1