Search in sources :

Example 31 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class IgniteTestUtils method setFieldValue.

/**
 * Set object field value via reflection.
 *
 * @param obj       Object to set field value to.
 * @param cls       Class to get field from.
 * @param fieldName Field name to set value for.
 * @param val       New field value.
 * @throws IgniteInternalException In case of error.
 */
public static void setFieldValue(Object obj, Class cls, String fieldName, Object val) throws IgniteInternalException {
    assert fieldName != null;
    try {
        Field field = cls.getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        boolean isFinal = (field.getModifiers() & Modifier.FINAL) != 0;
        boolean isStatic = (field.getModifiers() & Modifier.STATIC) != 0;
        /*
             * http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5.3
             * If a final field is initialized to a compile-time constant in the field declaration,
             *   changes to the final field may not be observed.
             */
        if (isFinal && isStatic) {
            throw new IgniteInternalException("Modification of static final field through reflection.");
        }
        if (isFinal) {
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        }
        field.set(obj, val);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new IgniteInternalException("Failed to set object field [obj=" + obj + ", field=" + fieldName + ']', e);
    }
}
Also used : Field(java.lang.reflect.Field) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException)

Example 32 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class ItJraftCounterServerTest method testCreateSnapshotAbnormalFailure.

@Test
public void testCreateSnapshotAbnormalFailure() throws Exception {
    listenerFactory = () -> new CounterListener() {

        @Override
        public void onSnapshotSave(Path path, Consumer<Throwable> doneClo) {
            doneClo.accept(new IgniteInternalException("Very bad"));
        }
    };
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    long val = applyIncrements(client1, 1, 10);
    assertEquals(sum(10), val);
    Peer peer = servers.get(0).localPeer(COUNTER_GROUP_0);
    try {
        client1.snapshot(peer).get();
        fail();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof RaftException);
    }
}
Also used : Path(java.nio.file.Path) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) TimeoutException(java.util.concurrent.TimeoutException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IOException(java.io.IOException) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) Test(org.junit.jupiter.api.Test)

Example 33 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class ItJraftCounterServerTest method testCreateSnapshotGracefulFailure.

@Test
public void testCreateSnapshotGracefulFailure() throws Exception {
    listenerFactory = () -> new CounterListener() {

        @Override
        public void onSnapshotSave(Path path, Consumer<Throwable> doneClo) {
            doneClo.accept(new IgniteInternalException("Very bad"));
        }
    };
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    RaftServer server = servers.get(0);
    Peer peer = server.localPeer(COUNTER_GROUP_0);
    long val = applyIncrements(client1, 1, 10);
    assertEquals(sum(10), val);
    try {
        client1.snapshot(peer).get();
        fail();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof RaftException);
    }
}
Also used : Path(java.nio.file.Path) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) TimeoutException(java.util.concurrent.TimeoutException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IOException(java.io.IOException) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) Test(org.junit.jupiter.api.Test)

Example 34 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class ItJraftCounterServerTest method testApplyWithFailure.

/**
 * Tests if a raft group become unavailable in case of a critical error.
 */
@Test
public void testApplyWithFailure() throws Exception {
    listenerFactory = () -> new CounterListener() {

        @Override
        public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) {
            Iterator<CommandClosure<WriteCommand>> wrapper = new Iterator<>() {

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public CommandClosure<WriteCommand> next() {
                    CommandClosure<WriteCommand> cmd = iterator.next();
                    IncrementAndGetCommand command = (IncrementAndGetCommand) cmd.command();
                    if (command.delta() == 10) {
                        throw new IgniteInternalException("Very bad");
                    }
                    return cmd;
                }
            };
            super.onWrite(wrapper);
        }
    };
    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);
    long val1 = applyIncrements(client1, 1, 5);
    long val2 = applyIncrements(client2, 1, 7);
    assertEquals(sum(5), val1);
    assertEquals(sum(7), val2);
    long val3 = applyIncrements(client1, 6, 9);
    assertEquals(sum(9), val3);
    try {
        client1.<Long>run(new IncrementAndGetCommand(10)).get();
        fail();
    } catch (Exception e) {
        // Expected.
        Throwable cause = e.getCause();
        assertTrue(cause instanceof RaftException);
    }
    NodeImpl finalLeader = leader;
    waitForCondition(() -> finalLeader.getState() == STATE_ERROR, 5_000);
    // Client can't switch to new leader, because only one peer in the list.
    try {
        client1.<Long>run(new IncrementAndGetCommand(11)).get();
    } catch (Exception e) {
        boolean isValid = e.getCause() instanceof TimeoutException;
        if (!isValid) {
            LOG.error("Got unexpected exception", e);
        }
        assertTrue(isValid, "Expecting the timeout");
    }
}
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) WriteCommand(org.apache.ignite.raft.client.WriteCommand) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) NodeImpl(org.apache.ignite.raft.jraft.core.NodeImpl) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) 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) Iterator(java.util.Iterator) CommandClosure(org.apache.ignite.raft.client.service.CommandClosure) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 35 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class JraftServerImpl method startRaftGroup.

/**
 * {@inheritDoc}
 */
@Override
public synchronized boolean startRaftGroup(String groupId, RaftGroupListener lsnr, @Nullable List<Peer> initialConf) {
    if (groups.containsKey(groupId)) {
        return false;
    }
    // Thread pools are shared by all raft groups.
    NodeOptions nodeOptions = opts.copy();
    Path serverDataPath = getServerDataPath(groupId);
    try {
        Files.createDirectories(serverDataPath);
    } catch (IOException e) {
        throw new IgniteInternalException(e);
    }
    nodeOptions.setLogUri(serverDataPath.resolve("logs").toString());
    nodeOptions.setRaftMetaUri(serverDataPath.resolve("meta").toString());
    nodeOptions.setSnapshotUri(serverDataPath.resolve("snapshot").toString());
    nodeOptions.setFsm(new DelegatingStateMachine(lsnr));
    if (initialConf != null) {
        List<PeerId> mapped = initialConf.stream().map(PeerId::fromPeer).collect(Collectors.toList());
        nodeOptions.setInitialConf(new Configuration(mapped, null));
    }
    IgniteRpcClient client = new IgniteRpcClient(service);
    nodeOptions.setRpcClient(client);
    NetworkAddress addr = service.topologyService().localMember().address();
    var peerId = new PeerId(addr.host(), addr.port(), 0, ElectionPriority.DISABLED);
    var server = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager);
    server.start();
    groups.put(groupId, server);
    return true;
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) NetworkAddress(org.apache.ignite.network.NetworkAddress) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) IOException(java.io.IOException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)58 RocksDBException (org.rocksdb.RocksDBException)19 IOException (java.io.IOException)11 Path (java.nio.file.Path)10 ArrayList (java.util.ArrayList)10 NotNull (org.jetbrains.annotations.NotNull)10 WriteBatch (org.rocksdb.WriteBatch)9 List (java.util.List)7 Entry (org.apache.ignite.internal.metastorage.server.Entry)7 NoSuchElementException (java.util.NoSuchElementException)6 NetworkAddress (org.apache.ignite.network.NetworkAddress)6 Test (org.junit.jupiter.api.Test)6 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)5 Nullable (org.jetbrains.annotations.Nullable)5 UUID (java.util.UUID)4 TimeoutException (java.util.concurrent.TimeoutException)4 NodeStoppingException (org.apache.ignite.lang.NodeStoppingException)4 ReadOptions (org.rocksdb.ReadOptions)4 RocksIterator (org.rocksdb.RocksIterator)4 CompletableFuture (java.util.concurrent.CompletableFuture)3