Search in sources :

Example 11 with IgniteException

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

the class TxAbstractTest method doTestSingleKeyMultithreaded.

/**
 * Performs a test.
 *
 * @param duration The duration.
 * @param verbose Verbose mode.
 * @throws InterruptedException If interrupted while waiting.
 */
private void doTestSingleKeyMultithreaded(long duration, boolean verbose) throws InterruptedException {
    int threadsCnt = Runtime.getRuntime().availableProcessors() * 2;
    Thread[] threads = new Thread[threadsCnt];
    final double initial = 1000;
    final double total = threads.length * initial;
    for (int i = 0; i < threads.length; i++) {
        accounts.recordView().upsert(null, makeValue(i, 1000));
    }
    double total0 = 0;
    for (long i = 0; i < threads.length; i++) {
        double balance = accounts.recordView().get(null, makeKey(i)).doubleValue("balance");
        total0 += balance;
    }
    assertEquals(total, total0, "Total amount invariant is not preserved");
    CyclicBarrier startBar = new CyclicBarrier(threads.length, () -> log.info("Before test"));
    LongAdder ops = new LongAdder();
    LongAdder fails = new LongAdder();
    AtomicBoolean stop = new AtomicBoolean();
    Random r = new Random();
    AtomicReference<Throwable> firstErr = new AtomicReference<>();
    for (int i = 0; i < threads.length; i++) {
        long finalI = i;
        threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    startBar.await();
                } catch (Exception e) {
                    fail();
                }
                while (!stop.get() && firstErr.get() == null) {
                    InternalTransaction tx = txManager(accounts).begin();
                    var table = accounts.recordView();
                    try {
                        long acc1 = finalI;
                        double amount = 100 + r.nextInt(500);
                        if (verbose) {
                            log.info("op=tryGet ts={} id={}", tx.timestamp(), acc1);
                        }
                        double val0 = table.get(tx, makeKey(acc1)).doubleValue("balance");
                        long acc2 = acc1;
                        while (acc1 == acc2) {
                            acc2 = r.nextInt(threads.length);
                        }
                        if (verbose) {
                            log.info("op=tryGet ts={} id={}", tx.timestamp(), acc2);
                        }
                        double val1 = table.get(tx, makeKey(acc2)).doubleValue("balance");
                        if (verbose) {
                            log.info("op=tryPut ts={} id={}", tx.timestamp(), acc1);
                        }
                        table.upsert(tx, makeValue(acc1, val0 - amount));
                        if (verbose) {
                            log.info("op=tryPut ts={} id={}", tx.timestamp(), acc2);
                        }
                        table.upsert(tx, makeValue(acc2, val1 + amount));
                        tx.commit();
                        assertTrue(txManager(accounts).state(tx.timestamp()) == COMMITED);
                        ops.increment();
                    } catch (Exception e) {
                        assertTrue(e.getMessage().contains("Failed to acquire a lock"), e.getMessage());
                        fails.increment();
                    }
                }
            }
        });
        threads[i].setName("Worker-" + i);
        threads[i].setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                firstErr.compareAndExchange(null, e);
            }
        });
        threads[i].start();
    }
    Thread.sleep(duration);
    stop.set(true);
    for (Thread thread : threads) {
        thread.join(3_000);
    }
    if (firstErr.get() != null) {
        throw new IgniteException(firstErr.get());
    }
    log.info("After test ops={} fails={}", ops.sum(), fails.sum());
    total0 = 0;
    for (long i = 0; i < threads.length; i++) {
        double balance = accounts.recordView().get(null, makeKey(i)).doubleValue("balance");
        total0 += balance;
    }
    assertEquals(total, total0, "Total amount invariant is not preserved");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) TransactionException(org.apache.ignite.tx.TransactionException) IgniteException(org.apache.ignite.lang.IgniteException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) Random(java.util.Random) IgniteException(org.apache.ignite.lang.IgniteException)

Example 12 with IgniteException

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

the class TableManager method dropTableAsyncInternal.

/**
 * Internal method that drops a table with the name specified. If appropriate table does not be found, a future will be
 * completed with {@link TableNotFoundException}.
 *
 * @param name Table name.
 * @return Future representing pending completion of the operation.
 * @throws IgniteException If an unspecified platform exception has happened internally. Is thrown when:
 *                         <ul>
 *                             <li>the node is stopping.</li>
 *                         </ul>
 * @see TableNotFoundException
 */
@NotNull
private CompletableFuture<Void> dropTableAsyncInternal(String name) {
    CompletableFuture<Void> dropTblFut = new CompletableFuture<>();
    tableAsyncInternal(name).thenAccept(tbl -> {
        // distributed table and the local config has lagged behind.
        if (tbl == null) {
            dropTblFut.completeExceptionally(new TableNotFoundException(name));
        } else {
            tablesCfg.tables().change(change -> {
                if (change.get(name) == null) {
                    throw new TableNotFoundException(name);
                }
                change.delete(name);
            }).whenComplete((res, t) -> {
                if (t != null) {
                    Throwable ex = getRootCause(t);
                    if (ex instanceof TableNotFoundException) {
                        dropTblFut.completeExceptionally(ex);
                    } else {
                        LOG.error(IgniteStringFormatter.format("Table wasn't dropped [name={}]", name), ex);
                        dropTblFut.completeExceptionally(ex);
                    }
                } else {
                    dropTblFut.complete(res);
                }
            });
        }
    });
    return dropTblFut;
}
Also used : IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) ConfigurationUtil.directProxy(org.apache.ignite.internal.configuration.util.ConfigurationUtil.directProxy) ConfigurationUtil.getByInternalId(org.apache.ignite.internal.configuration.util.ConfigurationUtil.getByInternalId) ExtendedTableChange(org.apache.ignite.internal.configuration.schema.ExtendedTableChange) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) Path(java.nio.file.Path) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) ExtendedTableView(org.apache.ignite.internal.configuration.schema.ExtendedTableView) IgniteComponent(org.apache.ignite.internal.manager.IgniteComponent) SchemaConfiguration(org.apache.ignite.internal.configuration.schema.SchemaConfiguration) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DEFAULT_DATA_REGION_NAME(org.apache.ignite.configuration.schemas.store.DataStorageConfigurationSchema.DEFAULT_DATA_REGION_NAME) CompletionException(java.util.concurrent.CompletionException) Producer(org.apache.ignite.internal.manager.Producer) StorageEngine(org.apache.ignite.internal.storage.engine.StorageEngine) UUID(java.util.UUID) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) Collectors(java.util.stream.Collectors) TxManager(org.apache.ignite.internal.tx.TxManager) TableView(org.apache.ignite.configuration.schemas.table.TableView) ClusterNode(org.apache.ignite.network.ClusterNode) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) InternalTable(org.apache.ignite.internal.table.InternalTable) IgniteException(org.apache.ignite.lang.IgniteException) Loza(org.apache.ignite.internal.raft.Loza) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) ByteUtils(org.apache.ignite.internal.util.ByteUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaSerializerImpl(org.apache.ignite.internal.schema.marshaller.schema.SchemaSerializerImpl) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SchemaView(org.apache.ignite.internal.configuration.schema.SchemaView) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteSpinBusyLock(org.apache.ignite.internal.util.IgniteSpinBusyLock) IgniteTables(org.apache.ignite.table.manager.IgniteTables) NoSuchElementException(java.util.NoSuchElementException) VersionedValue(org.apache.ignite.internal.causality.VersionedValue) TopologyService(org.apache.ignite.network.TopologyService) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) Files(java.nio.file.Files) NamedListView(org.apache.ignite.configuration.NamedListView) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IgniteObjectName(org.apache.ignite.internal.util.IgniteObjectName) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) BaselineManager(org.apache.ignite.internal.baseline.BaselineManager) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) ConfigurationNamedListListener(org.apache.ignite.configuration.notifications.ConfigurationNamedListListener) NetworkAddress(org.apache.ignite.network.NetworkAddress) Consumer(java.util.function.Consumer) ConfigurationNotificationEvent(org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent) TableEvent(org.apache.ignite.internal.table.event.TableEvent) AffinityUtils(org.apache.ignite.internal.affinity.AffinityUtils) EventListener(org.apache.ignite.internal.manager.EventListener) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) Table(org.apache.ignite.table.Table) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) RocksDbStorageEngine(org.apache.ignite.internal.storage.rocksdb.RocksDbStorageEngine) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) CompletableFuture(java.util.concurrent.CompletableFuture) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with IgniteException

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

the class TxAbstractTest method testPutAll.

@Test
public void testPutAll() throws TransactionException {
    igniteTransactions.runInTransaction(tx -> {
        accounts.recordView().upsertAll(tx, List.of(makeValue(1, 100.), makeValue(2, 200.)));
    });
    validateBalance(accounts.recordView().getAll(null, List.of(makeKey(2), makeKey(1))), 200., 100.);
    assertThrows(IgniteException.class, () -> igniteTransactions.runInTransaction(tx -> {
        accounts.recordView().upsertAll(tx, List.of(makeValue(3, 300.), makeValue(4, 400.)));
        if (true) {
            throw new IgniteException();
        }
    }));
    assertNull(accounts.recordView().get(null, makeKey(3)));
    assertNull(accounts.recordView().get(null, makeKey(4)));
}
Also used : RecordView(org.apache.ignite.table.RecordView) BeforeEach(org.junit.jupiter.api.BeforeEach) Pair(org.apache.ignite.internal.util.Pair) TransactionException(org.apache.ignite.tx.TransactionException) Random(java.util.Random) Disabled(org.junit.jupiter.api.Disabled) Row(org.apache.ignite.internal.schema.row.Row) TxState(org.apache.ignite.internal.tx.TxState) KeyValueView(org.apache.ignite.table.KeyValueView) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Flow(java.util.concurrent.Flow) Map(java.util.Map) CyclicBarrier(java.util.concurrent.CyclicBarrier) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Collection(java.util.Collection) UUID(java.util.UUID) TxManager(org.apache.ignite.internal.tx.TxManager) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) COMMITED(org.apache.ignite.internal.tx.TxState.COMMITED) IgniteTransactions(org.apache.ignite.tx.IgniteTransactions) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Strictness(org.mockito.quality.Strictness) ABORTED(org.apache.ignite.internal.tx.TxState.ABORTED) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) LongAdder(java.util.concurrent.atomic.LongAdder) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) IgniteException(org.apache.ignite.lang.IgniteException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) Transaction(org.apache.ignite.tx.Transaction) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) IgniteTestUtils(org.apache.ignite.internal.testframework.IgniteTestUtils) NativeTypes(org.apache.ignite.internal.schema.NativeTypes) LockManager(org.apache.ignite.internal.tx.LockManager) Column(org.apache.ignite.internal.schema.Column) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) Table(org.apache.ignite.table.Table) Tuple(org.apache.ignite.table.Tuple) IgniteException(org.apache.ignite.lang.IgniteException) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 14 with IgniteException

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

the class Timestamp method getLocalNodeId.

/**
 * Generates a local node id.
 *
 * @return Local node id as a long value.
 */
private static long getLocalNodeId() {
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        NetworkInterface iface = NetworkInterface.getByInetAddress(localHost);
        byte[] bytes = null;
        if (iface != null) {
            bytes = iface.getHardwareAddress();
        }
        if (bytes == null) {
            ThreadLocalRandom.current().nextBytes(bytes = new byte[Byte.SIZE]);
        }
        ByteBuffer buffer = ByteBuffer.allocate(Byte.SIZE);
        buffer.put(bytes);
        for (int i = bytes.length; i < Byte.SIZE; i++) {
            buffer.put((byte) 0);
        }
        buffer.flip();
        return buffer.getLong();
    } catch (Exception e) {
        throw new IgniteException("Failed to get local node id", e);
    }
}
Also used : IgniteException(org.apache.ignite.lang.IgniteException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) ByteBuffer(java.nio.ByteBuffer) IgniteException(org.apache.ignite.lang.IgniteException)

Example 15 with IgniteException

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

the class ClientHandlerModule method startEndpoint.

/**
 * Starts the endpoint.
 *
 * @return Channel future.
 * @throws InterruptedException If thread has been interrupted during the start.
 * @throws IgniteException      When startup has failed.
 */
private ChannelFuture startEndpoint() throws InterruptedException {
    var configuration = registry.getConfiguration(ClientConnectorConfiguration.KEY).value();
    int desiredPort = configuration.port();
    int portRange = configuration.portRange();
    int port = 0;
    Channel ch = null;
    ServerBootstrap bootstrap = bootstrapFactory.createServerBootstrap();
    bootstrap.childHandler(new ChannelInitializer<>() {

        @Override
        protected void initChannel(Channel ch) {
            ch.pipeline().addLast(new ClientMessageDecoder(), new ClientInboundMessageHandler(igniteTables, igniteTransactions, queryProcessor));
        }
    }).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.connectTimeout());
    for (int portCandidate = desiredPort; portCandidate <= desiredPort + portRange; portCandidate++) {
        ChannelFuture bindRes = bootstrap.bind(portCandidate).await();
        if (bindRes.isSuccess()) {
            ch = bindRes.channel();
            port = portCandidate;
            break;
        } else if (!(bindRes.cause() instanceof BindException)) {
            throw new IgniteException(bindRes.cause());
        }
    }
    if (ch == null) {
        String msg = "Cannot start thin client connector endpoint. " + "All ports in range [" + desiredPort + ", " + (desiredPort + portRange) + "] are in use.";
        LOG.error(msg);
        throw new IgniteException(msg);
    }
    LOG.info("Thin client protocol started successfully on port " + port);
    return ch.closeFuture();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ClientMessageDecoder(org.apache.ignite.internal.client.proto.ClientMessageDecoder) IgniteException(org.apache.ignite.lang.IgniteException) Channel(io.netty.channel.Channel) BindException(java.net.BindException) ChannelInitializer(io.netty.channel.ChannelInitializer) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

IgniteException (org.apache.ignite.lang.IgniteException)39 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)10 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)10 NotNull (org.jetbrains.annotations.NotNull)10 Map (java.util.Map)9 MarshallerException (org.apache.ignite.internal.schema.marshaller.MarshallerException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 HashMap (java.util.HashMap)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 NodeStoppingException (org.apache.ignite.lang.NodeStoppingException)7 HashSet (java.util.HashSet)6 CompletionException (java.util.concurrent.CompletionException)6 Row (org.apache.ignite.internal.schema.row.Row)6 NoSuchElementException (java.util.NoSuchElementException)5 Set (java.util.Set)5 UUID (java.util.UUID)5 Consumer (java.util.function.Consumer)5 Collectors (java.util.stream.Collectors)5