Search in sources :

Example 41 with Configuration

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

Example 42 with Configuration

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

Example 43 with Configuration

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

Example 44 with Configuration

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();
        }
    }
}
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 45 with Configuration

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;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ChangePeersResponse(com.alipay.sofa.jraft.rpc.CliRequests.ChangePeersResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3