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");
}
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;
}
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)));
}
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);
}
}
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();
}
Aggregations