Search in sources :

Example 1 with Bytes

use of org.apache.ignite.raft.jraft.util.Bytes in project ignite-3 by apache.

the class ReadOnlyServiceImpl method addRequest.

@Override
public void addRequest(final byte[] reqCtx, final ReadIndexClosure closure) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), closure, new Status(RaftError.EHOSTDOWN, "Was stopped"));
        throw new IllegalStateException("Service already shutdown.");
    }
    try {
        EventTranslator<ReadIndexEvent> translator = (event, sequence) -> {
            event.groupId = this.groupId;
            event.done = closure;
            event.requestContext = new Bytes(reqCtx);
            event.startTime = Utils.monotonicMs();
        };
        int retryTimes = 0;
        while (true) {
            if (this.readIndexQueue.tryPublishEvent(translator)) {
                break;
            } else {
                retryTimes++;
                if (retryTimes > MAX_ADD_REQUEST_RETRY_TIMES) {
                    Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), closure, new Status(RaftError.EBUSY, "Node is busy, has too many read-only requests."));
                    this.nodeMetrics.recordTimes("read-index-overload-times", 1);
                    LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
                    return;
                }
                ThreadHelper.onSpinWait();
            }
        }
    } catch (final Exception e) {
        Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), closure, new Status(RaftError.EPERM, "Node is down."));
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) RaftException(org.apache.ignite.raft.jraft.error.RaftException) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ArrayList(java.util.ArrayList) LastAppliedLogIndexListener(org.apache.ignite.raft.jraft.FSMCaller.LastAppliedLogIndexListener) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) Map(java.util.Map) RpcResponseClosureAdapter(org.apache.ignite.raft.jraft.rpc.RpcResponseClosureAdapter) EventHandler(com.lmax.disruptor.EventHandler) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ReadIndexResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexResponse) ReadOnlyService(org.apache.ignite.raft.jraft.ReadOnlyService) ReadIndexRequestBuilder(org.apache.ignite.raft.jraft.rpc.ReadIndexRequestBuilder) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RingBuffer(com.lmax.disruptor.RingBuffer) Status(org.apache.ignite.raft.jraft.Status) ThreadHelper(org.apache.ignite.raft.jraft.util.ThreadHelper) GroupAware(org.apache.ignite.raft.jraft.disruptor.GroupAware) OnlyForTest(org.apache.ignite.raft.jraft.util.OnlyForTest) TimeUnit(java.util.concurrent.TimeUnit) Utils(org.apache.ignite.raft.jraft.util.Utils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(java.util.concurrent.locks.Lock) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) EventTranslator(com.lmax.disruptor.EventTranslator) TreeMap(java.util.TreeMap) FSMCaller(org.apache.ignite.raft.jraft.FSMCaller) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) ByteString(org.apache.ignite.raft.jraft.util.ByteString) Bytes(org.apache.ignite.raft.jraft.util.Bytes) EventFactory(com.lmax.disruptor.EventFactory) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RaftError(org.apache.ignite.raft.jraft.error.RaftError) Bytes(org.apache.ignite.raft.jraft.util.Bytes) RaftException(org.apache.ignite.raft.jraft.error.RaftException)

Example 2 with Bytes

use of org.apache.ignite.raft.jraft.util.Bytes in project ignite-3 by apache.

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(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) Bytes(org.apache.ignite.raft.jraft.util.Bytes) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Status (org.apache.ignite.raft.jraft.Status)2 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)2 ReadIndexState (org.apache.ignite.raft.jraft.entity.ReadIndexState)2 ReadIndexStatus (org.apache.ignite.raft.jraft.entity.ReadIndexStatus)2 Bytes (org.apache.ignite.raft.jraft.util.Bytes)2 EventFactory (com.lmax.disruptor.EventFactory)1 EventHandler (com.lmax.disruptor.EventHandler)1 EventTranslator (com.lmax.disruptor.EventTranslator)1 RingBuffer (com.lmax.disruptor.RingBuffer)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 IgniteLogger (org.apache.ignite.lang.IgniteLogger)1 FSMCaller (org.apache.ignite.raft.jraft.FSMCaller)1