Search in sources :

Example 11 with ConfigurationEntry

use of com.alipay.sofa.jraft.conf.ConfigurationEntry in project sofa-jraft by sofastack.

the class RocksDBLogStorage method load.

private void load(final ConfigurationManager confManager) {
    checkState();
    try (final RocksIterator it = this.db.newIterator(this.confHandle, this.totalOrderReadOptions)) {
        it.seekToFirst();
        while (it.isValid()) {
            final byte[] ks = it.key();
            final byte[] bs = it.value();
            // LogEntry index
            if (ks.length == 8) {
                final LogEntry entry = this.logEntryDecoder.decode(bs);
                if (entry != null) {
                    if (entry.getType() == EntryType.ENTRY_TYPE_CONFIGURATION) {
                        final ConfigurationEntry confEntry = new ConfigurationEntry();
                        confEntry.setId(new LogId(entry.getId().getIndex(), entry.getId().getTerm()));
                        confEntry.setConf(new Configuration(entry.getPeers(), entry.getLearners()));
                        if (entry.getOldPeers() != null) {
                            confEntry.setOldConf(new Configuration(entry.getOldPeers(), entry.getOldLearners()));
                        }
                        if (confManager != null) {
                            confManager.add(confEntry);
                        }
                    }
                } else {
                    LOG.warn("Fail to decode conf entry at index {}, the log data is: {}.", Bits.getLong(ks, 0), BytesUtil.toHex(bs));
                }
            } else {
                if (Arrays.equals(FIRST_LOG_IDX_KEY, ks)) {
                    setFirstLogIndex(Bits.getLong(bs, 0));
                    truncatePrefixInBackground(0L, this.firstLogIndex);
                } else {
                    LOG.warn("Unknown entry in configuration storage key={}, value={}.", BytesUtil.toHex(ks), BytesUtil.toHex(bs));
                }
            }
            it.next();
        }
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RocksIterator(org.rocksdb.RocksIterator) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 12 with ConfigurationEntry

use of com.alipay.sofa.jraft.conf.ConfigurationEntry in project sofa-jraft by sofastack.

the class NodeImpl method unsafeRegisterConfChange.

private void unsafeRegisterConfChange(final Configuration oldConf, final Configuration newConf, final Closure done) {
    Requires.requireTrue(newConf.isValid(), "Invalid new conf: %s", newConf);
    // The new conf entry(will be stored in log manager) should be valid
    Requires.requireTrue(new ConfigurationEntry(null, newConf, oldConf).isValid(), "Invalid conf entry: %s", newConf);
    if (this.state != State.STATE_LEADER) {
        LOG.warn("Node {} refused configuration changing as the state={}.", getNodeId(), this.state);
        if (done != null) {
            final Status status = new Status();
            if (this.state == State.STATE_TRANSFERRING) {
                status.setError(RaftError.EBUSY, "Is transferring leadership.");
            } else {
                status.setError(RaftError.EPERM, "Not leader");
            }
            Utils.runClosureInThread(done, status);
        }
        return;
    }
    // check concurrent conf change
    if (this.confCtx.isBusy()) {
        LOG.warn("Node {} refused configuration concurrent changing.", getNodeId());
        if (done != null) {
            Utils.runClosureInThread(done, new Status(RaftError.EBUSY, "Doing another configuration change."));
        }
        return;
    }
    // Return immediately when the new peers equals to current configuration
    if (this.conf.getConf().equals(newConf)) {
        Utils.runClosureInThread(done);
        return;
    }
    this.confCtx.start(oldConf, newConf, done);
}
Also used : Status(com.alipay.sofa.jraft.Status) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry)

Example 13 with ConfigurationEntry

use of com.alipay.sofa.jraft.conf.ConfigurationEntry in project sofa-jraft by sofastack.

the class FSMCallerImpl method doSnapshotSave.

private void doSnapshotSave(final SaveSnapshotClosure done) {
    Requires.requireNonNull(done, "SaveSnapshotClosure is null");
    final long lastAppliedIndex = this.lastAppliedIndex.get();
    final RaftOutter.SnapshotMeta.Builder metaBuilder = // 
    RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(// 
    lastAppliedIndex).setLastIncludedTerm(this.lastAppliedTerm);
    final ConfigurationEntry confEntry = this.logManager.getConfiguration(lastAppliedIndex);
    if (confEntry == null || confEntry.isEmpty()) {
        LOG.error("Empty conf entry for lastAppliedIndex={}", lastAppliedIndex);
        Utils.runClosureInThread(done, new Status(RaftError.EINVAL, "Empty conf entry for lastAppliedIndex=%s", lastAppliedIndex));
        return;
    }
    for (final PeerId peer : confEntry.getConf()) {
        metaBuilder.addPeers(peer.toString());
    }
    for (final PeerId peer : confEntry.getConf().getLearners()) {
        metaBuilder.addLearners(peer.toString());
    }
    if (confEntry.getOldConf() != null) {
        for (final PeerId peer : confEntry.getOldConf()) {
            metaBuilder.addOldPeers(peer.toString());
        }
        for (final PeerId peer : confEntry.getOldConf().getLearners()) {
            metaBuilder.addOldLearners(peer.toString());
        }
    }
    final SnapshotWriter writer = done.start(metaBuilder.build());
    if (writer == null) {
        done.run(new Status(RaftError.EINVAL, "snapshot_storage create SnapshotWriter failed"));
        return;
    }
    this.fsm.onSnapshotSave(writer, done);
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId)

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