Search in sources :

Example 91 with Status

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

the class UtilsTest method testRunClosure.

@Test
public void testRunClosure() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    Utils.runClosureInThread(new Closure() {

        @Override
        public void run(Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) Closure(com.alipay.sofa.jraft.Closure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 92 with Status

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

the class LogManagerImpl method checkConsistency.

@Override
public Status checkConsistency() {
    this.readLock.lock();
    try {
        Requires.requireTrue(this.firstLogIndex > 0);
        Requires.requireTrue(this.lastLogIndex >= 0);
        if (this.lastSnapshotId.equals(new LogId(0, 0))) {
            if (this.firstLogIndex == 1) {
                return Status.OK();
            }
            return new Status(RaftError.EIO, "Missing logs in (0, %d)", this.firstLogIndex);
        } else {
            if (this.lastSnapshotId.getIndex() >= this.firstLogIndex - 1 && this.lastSnapshotId.getIndex() <= this.lastLogIndex) {
                return Status.OK();
            }
            return new Status(RaftError.EIO, "There's a gap between snapshot={%d, %d} and log=[%d, %d] ", this.lastSnapshotId.toString(), this.lastSnapshotId.getTerm(), this.firstLogIndex, this.lastLogIndex);
        }
    } finally {
        this.readLock.unlock();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) LogId(com.alipay.sofa.jraft.entity.LogId)

Example 93 with Status

use of com.alipay.sofa.jraft.Status 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 94 with Status

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

the class DefaultRaftClientService method onConnectionFail.

// fail-fast when no connection
private Future<Message> onConnectionFail(final Endpoint endpoint, final Message request, Closure done, final Executor executor) {
    final FutureImpl<Message> future = new FutureImpl<>();
    executor.execute(() -> {
        final String fmt = "Check connection[%s] fail and try to create new one";
        if (done != null) {
            try {
                done.run(new Status(RaftError.EINTERNAL, fmt, endpoint));
            } catch (final Throwable t) {
                LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
            }
        }
        if (!future.isDone()) {
            future.failure(new RemotingException(String.format(fmt, endpoint)));
        }
    });
    return future;
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) RemotingException(com.alipay.sofa.jraft.error.RemotingException)

Example 95 with Status

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

the class BaseCliRequestProcessor method processRequest.

@Override
public Message processRequest(T request, RpcRequestClosure done) {
    String groupId = getGroupId(request);
    String peerIdStr = getPeerId(request);
    PeerId peerId = null;
    if (!StringUtils.isBlank(peerIdStr)) {
        peerId = new PeerId();
        if (!peerId.parse(peerIdStr)) {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer: %s", peerIdStr);
        }
    }
    Status st = new Status();
    Node node = getNode(groupId, peerId, st);
    if (!st.isOk()) {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st.getCode(), st.getErrorMsg());
    } else {
        return processRequest0(new CliRequestContext(node, groupId, peerId), request, done);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11