Search in sources :

Example 1 with Bytes

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

the class ReadOnlyServiceTest method testOnApplied.

@Test
public void testOnApplied() throws Exception {
    final ArrayList<ReadIndexState> states = new ArrayList<>();
    final byte[] reqContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    final ReadIndexState state = new ReadIndexState(new Bytes(reqContext), new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertTrue(status.isOk());
            assertEquals(index, 1);
            assertArrayEquals(reqCtx, reqContext);
            latch.countDown();
        }
    }, Utils.monotonicMs());
    state.setIndex(1);
    states.add(state);
    final ReadIndexStatus readIndexStatus = new ReadIndexStatus(states, null, 1);
    this.readOnlyServiceImpl.getPendingNotifyStatus().put(1L, Arrays.asList(readIndexStatus));
    this.readOnlyServiceImpl.onApplied(2);
    latch.await();
    assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) Bytes(com.alipay.sofa.jraft.util.Bytes) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ReadIndexState(com.alipay.sofa.jraft.entity.ReadIndexState) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Bytes

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

the class ReadOnlyServiceImpl method addRequest.

@Override
public void addRequest(final byte[] reqCtx, final ReadIndexClosure closure) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(closure, new Status(RaftError.EHOSTDOWN, "Was stopped"));
        throw new IllegalStateException("Service already shutdown.");
    }
    try {
        EventTranslator<ReadIndexEvent> translator = (event, sequence) -> {
            event.done = closure;
            event.requestContext = new Bytes(reqCtx);
            event.startTime = Utils.monotonicMs();
        };
        switch(this.node.getOptions().getApplyTaskMode()) {
            case Blocking:
                this.readIndexQueue.publishEvent(translator);
                break;
            case NonBlocking:
            default:
                if (!this.readIndexQueue.tryPublishEvent(translator)) {
                    final String errorMsg = "Node is busy, has too many read-index requests, queue is full and bufferSize=" + this.readIndexQueue.getBufferSize();
                    Utils.runClosureInThread(closure, new Status(RaftError.EBUSY, errorMsg));
                    this.nodeMetrics.recordTimes("read-index-overload-times", 1);
                    LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
                    if (closure == null) {
                        throw new OverloadException(errorMsg);
                    }
                }
                break;
        }
    } catch (final Exception e) {
        Utils.runClosureInThread(closure, new Status(RaftError.EPERM, "Node is down."));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) OverloadException(com.alipay.sofa.jraft.error.OverloadException) ReadIndexState(com.alipay.sofa.jraft.entity.ReadIndexState) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) ReadIndexResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexResponse) LoggerFactory(org.slf4j.LoggerFactory) ReadIndexRequest(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest) LastAppliedLogIndexListener(com.alipay.sofa.jraft.FSMCaller.LastAppliedLogIndexListener) Utils(com.alipay.sofa.jraft.util.Utils) ArrayList(java.util.ArrayList) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RaftError(com.alipay.sofa.jraft.error.RaftError) EventHandler(com.lmax.disruptor.EventHandler) LogExceptionHandler(com.alipay.sofa.jraft.util.LogExceptionHandler) OnlyForTest(com.alipay.sofa.jraft.util.OnlyForTest) ZeroByteStringHelper(com.google.protobuf.ZeroByteStringHelper) Logger(org.slf4j.Logger) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) RingBuffer(com.lmax.disruptor.RingBuffer) DisruptorBuilder(com.alipay.sofa.jraft.util.DisruptorBuilder) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ReadOnlyServiceOptions(com.alipay.sofa.jraft.option.ReadOnlyServiceOptions) List(java.util.List) Lock(java.util.concurrent.locks.Lock) TreeMap(java.util.TreeMap) EventTranslator(com.lmax.disruptor.EventTranslator) FSMCaller(com.alipay.sofa.jraft.FSMCaller) Bytes(com.alipay.sofa.jraft.util.Bytes) ReadOnlyService(com.alipay.sofa.jraft.ReadOnlyService) EventFactory(com.lmax.disruptor.EventFactory) RaftException(com.alipay.sofa.jraft.error.RaftException) Disruptor(com.lmax.disruptor.dsl.Disruptor) RpcResponseClosureAdapter(com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter) Bytes(com.alipay.sofa.jraft.util.Bytes) OverloadException(com.alipay.sofa.jraft.error.OverloadException) OverloadException(com.alipay.sofa.jraft.error.OverloadException) RaftException(com.alipay.sofa.jraft.error.RaftException)

Aggregations

Status (com.alipay.sofa.jraft.Status)2 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)2 ReadIndexState (com.alipay.sofa.jraft.entity.ReadIndexState)2 ReadIndexStatus (com.alipay.sofa.jraft.entity.ReadIndexStatus)2 Bytes (com.alipay.sofa.jraft.util.Bytes)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 FSMCaller (com.alipay.sofa.jraft.FSMCaller)1 LastAppliedLogIndexListener (com.alipay.sofa.jraft.FSMCaller.LastAppliedLogIndexListener)1 ReadOnlyService (com.alipay.sofa.jraft.ReadOnlyService)1 OverloadException (com.alipay.sofa.jraft.error.OverloadException)1 RaftError (com.alipay.sofa.jraft.error.RaftError)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)1 ReadOnlyServiceOptions (com.alipay.sofa.jraft.option.ReadOnlyServiceOptions)1 ReadIndexRequest (com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest)1 ReadIndexResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexResponse)1 RpcResponseClosureAdapter (com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter)1 DisruptorBuilder (com.alipay.sofa.jraft.util.DisruptorBuilder)1 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)1