Search in sources :

Example 1 with ReadCommand

use of org.apache.ignite.raft.client.ReadCommand in project ignite-3 by apache.

the class ItJraftCounterServerTest method testClientCatchExceptionFromSm.

/**
 * Tests that users related exceptions from SM are propagated to the client.
 */
@Test
public void testClientCatchExceptionFromSm() throws Exception {
    listenerFactory = () -> new CounterListener() {

        @Override
        public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) {
            while (iterator.hasNext()) {
                CommandClosure<WriteCommand> clo = iterator.next();
                IncrementAndGetCommand cmd0 = (IncrementAndGetCommand) clo.command();
                clo.result(new RuntimeException("Expected message"));
            }
        }

        @Override
        public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) {
            while (iterator.hasNext()) {
                CommandClosure<ReadCommand> clo = iterator.next();
                assert clo.command() instanceof GetValueCommand;
                clo.result(new RuntimeException("Another expected message"));
            }
        }
    };
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    NodeImpl leader = servers.stream().map(s -> ((NodeImpl) s.raftGroupService(COUNTER_GROUP_0).getRaftNode())).filter(n -> n.getState() == STATE_LEADER).findFirst().orElse(null);
    assertNotNull(leader);
    try {
        client1.<Long>run(new IncrementAndGetCommand(3)).get();
        fail();
    } catch (Exception e) {
        // Expected.
        Throwable cause = e.getCause();
        assertTrue(cause instanceof RuntimeException);
        assertEquals(cause.getMessage(), "Expected message");
    }
    try {
        client1.<Long>run(new GetValueCommand()).get();
        fail();
    } catch (Exception e) {
        // Expected.
        Throwable cause = e.getCause();
        assertTrue(cause instanceof RuntimeException);
        assertEquals(cause.getMessage(), "Another expected message");
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TimeoutException(java.util.concurrent.TimeoutException) IgniteLogger(org.apache.ignite.lang.IgniteLogger) Future(java.util.concurrent.Future) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) TestUtils.getLocalAddress(org.apache.ignite.raft.jraft.test.TestUtils.getLocalAddress) Path(java.nio.file.Path) CommandClosure(org.apache.ignite.raft.client.service.CommandClosure) Collectors.toSet(java.util.stream.Collectors.toSet) STATE_ERROR(org.apache.ignite.raft.jraft.core.State.STATE_ERROR) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Collectors(java.util.stream.Collectors) TestUtils(org.apache.ignite.raft.jraft.test.TestUtils) ExecutorServiceHelper(org.apache.ignite.raft.jraft.util.ExecutorServiceHelper) Executors(java.util.concurrent.Executors) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) ReadCommand(org.apache.ignite.raft.client.ReadCommand) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) NotNull(org.jetbrains.annotations.NotNull) RaftGroupServiceImpl(org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl) Assertions.fail(org.junit.jupiter.api.Assertions.fail) IntStream(java.util.stream.IntStream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Loza(org.apache.ignite.internal.raft.Loza) Supplier(java.util.function.Supplier) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ArrayList(java.util.ArrayList) TestUtils.waitForTopology(org.apache.ignite.raft.jraft.test.TestUtils.waitForTopology) WriteCommand(org.apache.ignite.raft.client.WriteCommand) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Comparator.comparing(java.util.Comparator.comparing) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) TestUtils.waitForCondition(org.apache.ignite.raft.jraft.test.TestUtils.waitForCondition) StateMachineAdapter(org.apache.ignite.raft.jraft.core.StateMachineAdapter) Iterator(java.util.Iterator) Files(java.nio.file.Files) IOException(java.io.IOException) NodeImpl(org.apache.ignite.raft.jraft.core.NodeImpl) WorkDirectory(org.apache.ignite.internal.testframework.WorkDirectory) NetworkAddress(org.apache.ignite.network.NetworkAddress) STATE_LEADER(org.apache.ignite.raft.jraft.core.State.STATE_LEADER) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ClusterService(org.apache.ignite.network.ClusterService) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) NodeImpl(org.apache.ignite.raft.jraft.core.NodeImpl) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) TimeoutException(java.util.concurrent.TimeoutException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IOException(java.io.IOException) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) CommandClosure(org.apache.ignite.raft.client.service.CommandClosure) Test(org.junit.jupiter.api.Test)

Example 2 with ReadCommand

use of org.apache.ignite.raft.client.ReadCommand in project ignite-3 by apache.

the class MetaStorageListener method onRead.

/**
 * {@inheritDoc}
 */
@Override
public void onRead(Iterator<CommandClosure<ReadCommand>> iter) {
    while (iter.hasNext()) {
        CommandClosure<ReadCommand> clo = iter.next();
        ReadCommand command = clo.command();
        if (command instanceof GetCommand) {
            GetCommand getCmd = (GetCommand) command;
            Entry e;
            if (getCmd.revision() != 0) {
                e = storage.get(getCmd.key(), getCmd.revision());
            } else {
                e = storage.get(getCmd.key());
            }
            SingleEntryResponse resp = new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter());
            clo.result(resp);
        } else if (command instanceof GetAllCommand) {
            GetAllCommand getAllCmd = (GetAllCommand) command;
            Collection<Entry> entries;
            if (getAllCmd.revision() != 0) {
                entries = storage.getAll(getAllCmd.keys(), getAllCmd.revision());
            } else {
                entries = storage.getAll(getAllCmd.keys());
            }
            List<SingleEntryResponse> res = new ArrayList<>(entries.size());
            for (Entry e : entries) {
                res.add(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
            }
            clo.result(new MultipleEntryResponse(res));
        } else if (command instanceof CursorHasNextCommand) {
            CursorHasNextCommand cursorHasNextCmd = (CursorHasNextCommand) command;
            CursorMeta cursorDesc = cursors.get(cursorHasNextCmd.cursorId());
            clo.result(!(cursorDesc == null) && cursorDesc.cursor().hasNext());
        } else {
            assert false : "Command was not found [cmd=" + command + ']';
        }
    }
}
Also used : GetCommand(org.apache.ignite.internal.metastorage.common.command.GetCommand) Entry(org.apache.ignite.internal.metastorage.server.Entry) ReadCommand(org.apache.ignite.raft.client.ReadCommand) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) MultipleEntryResponse(org.apache.ignite.internal.metastorage.common.command.MultipleEntryResponse) SingleEntryResponse(org.apache.ignite.internal.metastorage.common.command.SingleEntryResponse) CursorHasNextCommand(org.apache.ignite.internal.metastorage.common.command.cursor.CursorHasNextCommand) GetAllCommand(org.apache.ignite.internal.metastorage.common.command.GetAllCommand)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 ReadCommand (org.apache.ignite.raft.client.ReadCommand)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 Comparator.comparing (java.util.Comparator.comparing)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1