use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.
the class IgniteCacheQueryNodeRestartSelfTest2 method testRestarts.
/**
* @throws Exception If failed.
*/
@Ignore("https://issues.apache.org/jira/browse/IGNITE-10917")
@Test
public void testRestarts() throws Exception {
int duration = 90 * 1000;
int qryThreadNum = 4;
// 4 + 2 = 6 nodes
int restartThreadsNum = 2;
final int nodeLifeTime = 2 * 1000;
final int logFreq = 10;
startGridsMultiThreaded(GRID_CNT);
final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT);
fillCaches();
final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(PARTITIONED_QRY)).getAll();
Thread.sleep(3000);
assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(PARTITIONED_QRY)).getAll());
final List<List<?>> rRes = grid(0).cache("co").query(new SqlFieldsQuery(REPLICATED_QRY)).getAll();
assertFalse(pRes.isEmpty());
assertFalse(rRes.isEmpty());
final AtomicInteger qryCnt = new AtomicInteger();
final AtomicBoolean qrysDone = new AtomicBoolean();
IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {
@Override
public void applyx() throws IgniteCheckedException {
final GridRandom rnd = new GridRandom();
while (!qrysDone.get()) {
int g;
do {
g = rnd.nextInt(locks.length());
} while (!locks.compareAndSet(g, 0, 1));
try {
final IgniteEx grid = grid(g);
if (rnd.nextBoolean()) {
// Partitioned query.
final IgniteCache<?, ?> cache = grid.cache("pu");
final SqlFieldsQuery qry = new SqlFieldsQuery(PARTITIONED_QRY);
boolean smallPageSize = rnd.nextBoolean();
if (smallPageSize)
qry.setPageSize(3);
final IgniteCache<Integer, Company> co = grid.cache("co");
try {
runQuery(grid, new Runnable() {
@Override
public void run() {
if (rnd.nextBoolean())
// Get lock run test with open transaction.
co.get(rnd.nextInt(COMPANY_CNT));
assertEquals(pRes, cache.query(qry).getAll());
}
});
} catch (CacheException e) {
// Interruptions are expected here.
if (e.getCause() instanceof IgniteInterruptedCheckedException || e.getCause() instanceof InterruptedException || e.getCause() instanceof ClusterTopologyException || e.getCause() instanceof TransactionTimeoutException || e.getCause() instanceof TransactionException)
continue;
if (e.getCause() instanceof QueryCancelledException)
fail("Retry is expected");
if (!smallPageSize)
U.error(grid.log(), "On large page size must retry.", e);
assertTrue("On large page size must retry.", smallPageSize);
boolean failedOnRemoteFetch = false;
boolean failedOnInterruption = false;
for (Throwable th = e; th != null; th = th.getCause()) {
if (th instanceof InterruptedException) {
failedOnInterruption = true;
break;
}
if (!(th instanceof CacheException))
continue;
if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
failedOnRemoteFetch = true;
break;
}
}
// Interruptions are expected here.
if (failedOnInterruption)
continue;
if (!failedOnRemoteFetch) {
U.error(grid.log(), "Must fail inside of GridResultPage.fetchNextPage or subclass.", e);
fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
}
}
} else {
// Replicated query.
IgniteCache<?, ?> cache = grid.cache("co");
assertEquals(rRes, cache.query(new SqlFieldsQuery(REPLICATED_QRY)).getAll());
}
} finally {
// Clearing lock in final handler to avoid endless loop if exception is thrown.
locks.set(g, 0);
int c = qryCnt.incrementAndGet();
if (c % logFreq == 0)
info("Executed queries: " + c);
}
}
}
}, qryThreadNum, "query-thread");
final AtomicInteger restartCnt = new AtomicInteger();
final AtomicBoolean restartsDone = new AtomicBoolean();
IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {
@SuppressWarnings({ "BusyWait" })
@Override
public Object call() throws Exception {
GridRandom rnd = new GridRandom();
while (!restartsDone.get()) {
int g;
do {
g = rnd.nextInt(locks.length());
} while (!locks.compareAndSet(g, 0, -1));
try {
log.info("Stop node: " + g);
stopGrid(g);
Thread.sleep(rnd.nextInt(nodeLifeTime));
log.info("Start node: " + g);
startGrid(g);
Thread.sleep(rnd.nextInt(nodeLifeTime));
} finally {
locks.set(g, 0);
int c = restartCnt.incrementAndGet();
if (c % logFreq == 0)
info("Node restarts: " + c);
}
}
return true;
}
}, restartThreadsNum, "restart-thread");
Thread.sleep(duration);
info("Stopping..");
restartsDone.set(true);
try {
fut2.get(20_000);
} catch (IgniteFutureTimeoutCheckedException e) {
U.dumpThreads(log);
fail("Stopping restarts timeout.");
}
info("Restarts stopped.");
qrysDone.set(true);
// Query thread can stuck in next page waiting loop because all nodes are left.
try {
fut1.get(5_000);
} catch (IgniteFutureTimeoutCheckedException ignored) {
fut1.cancel();
}
info("Queries stopped.");
}
use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.
the class TxRollbackOnTimeoutTest method testRandomMixedTxConfigurations.
/**
* Test timeouts with random values and different tx configurations.
*/
@Test
public void testRandomMixedTxConfigurations() throws Exception {
final Ignite client = startClient();
final AtomicBoolean stop = new AtomicBoolean();
final long seed = System.currentTimeMillis();
final Random r = new Random(seed);
log.info("Using seed: " + seed);
final int threadsCnt = Runtime.getRuntime().availableProcessors() * 2;
for (int k = 0; k < threadsCnt; k++) grid(0).cache(CACHE_NAME).put(k, (long) 0);
final TransactionConcurrency[] TC_VALS = TransactionConcurrency.values();
final TransactionIsolation[] TI_VALS = TransactionIsolation.values();
final LongAdder cntr0 = new LongAdder();
final LongAdder cntr1 = new LongAdder();
final LongAdder cntr2 = new LongAdder();
final LongAdder cntr3 = new LongAdder();
final IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
while (!stop.get()) {
int nodeId = r.nextInt(GRID_CNT + 1);
Ignite node = nodeId == GRID_CNT || nearCacheEnabled() ? client : grid(nodeId);
TransactionConcurrency conc = TC_VALS[r.nextInt(TC_VALS.length)];
TransactionIsolation isolation = TI_VALS[r.nextInt(TI_VALS.length)];
int k = r.nextInt(threadsCnt);
long timeout = r.nextInt(200) + 50;
// Roughly 50% of transactions should time out.
try (Transaction tx = node.transactions().txStart(conc, isolation, timeout, 1)) {
cntr0.add(1);
final Long v = (Long) node.cache(CACHE_NAME).get(k);
assertNotNull("Expecting not null value: " + tx, v);
final int delay = r.nextInt(400);
if (delay > 0)
sleep(delay);
node.cache(CACHE_NAME).put(k, v + 1);
tx.commit();
cntr1.add(1);
} catch (TransactionTimeoutException e) {
cntr2.add(1);
} catch (CacheException e) {
assertEquals(TransactionTimeoutException.class, X.getCause(e).getClass());
cntr2.add(1);
} catch (Exception e) {
cntr3.add(1);
}
}
}
}, threadsCnt, "tx-async-thread");
sleep(DURATION);
stop.set(true);
try {
fut.get(30_000);
} catch (IgniteFutureTimeoutCheckedException e) {
error("Transactions hang", e);
for (Ignite node : G.allGrids()) ((IgniteKernal) node).dumpDebugInfo();
// Try to interrupt hanging threads.
fut.cancel();
throw e;
}
log.info("Tx test stats: started=" + cntr0.sum() + ", completed=" + cntr1.sum() + ", failed=" + cntr3.sum() + ", timedOut=" + cntr2.sum());
assertEquals("Expected finished count same as started count", cntr0.sum(), cntr1.sum() + cntr2.sum() + cntr3.sum());
}
use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project ignite by apache.
the class IgniteCacheOffheapEvictQueryTest method testEvictAndRemove.
/**
* @throws Exception If failed.
*/
@Test
public void testEvictAndRemove() throws Exception {
final int KEYS_CNT = 3000;
final int THREADS_CNT = 250;
final IgniteCache<Integer, Integer> c = startGrid().cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < KEYS_CNT; i++) {
c.put(i, i);
if ((i & 1) == 0)
c.localEvict(F.asList(i));
}
X.println("___ Cache loaded...");
final CyclicBarrier b = new CyclicBarrier(THREADS_CNT, new Runnable() {
@Override
public void run() {
X.println("___ go!");
}
});
final AtomicInteger keys = new AtomicInteger(KEYS_CNT);
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
Random rnd = new GridRandom();
try {
b.await();
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
} catch (BrokenBarrierException e) {
throw new IllegalStateException(e);
}
while (keys.get() > 0) {
int k = rnd.nextInt(KEYS_CNT);
try {
switch(rnd.nextInt(4)) {
case 0:
c.localEvict(F.asList(k));
break;
case 1:
c.get(k);
break;
case 2:
if (c.remove(k))
keys.decrementAndGet();
break;
case 3:
c.query(new SqlFieldsQuery("select _val from Integer where _key between ? and ?").setArgs(k, k + 20).setLocal(true)).getAll();
break;
}
} catch (CacheException e) {
String msgStart = "Failed to get value for key:";
for (Throwable th = e; th != null; th = th.getCause()) {
String msg = th.getMessage();
if (msg != null && msg.startsWith(msgStart)) {
int dot = msg.indexOf('.', msgStart.length());
assertTrue(dot != -1);
final Integer failedKey = Integer.parseInt(msg.substring(msgStart.length(), dot).trim());
X.println("___ failed key: " + failedKey);
break;
}
}
LT.warn(log, e.getMessage());
return;
}
}
}
}, THREADS_CNT);
try {
fut.get(60_000);
if (c.size(CachePeekMode.ALL) != 0)
fail("Not all keys removed.");
X.println("___ all keys removed");
} catch (IgniteFutureTimeoutCheckedException ignored) {
X.println("___ timeout");
X.println("___ keys: " + keys.get());
keys.set(0);
fut.get();
}
}
use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project gridgain by gridgain.
the class GridExecutorService method invokeAny.
/**
* {@inheritDoc}
* <p>
* Note, for compilation with JDK 1.6 necessary to change method signature
* (note the {@code <? extends T>} clause).
* <pre name="code" class="java">
* ...
* public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
* throws InterruptedException, ExecutionException, TimeoutException {
* }
* ...
* </pre>
*/
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
A.notNull(tasks, "tasks != null");
A.ensure(!tasks.isEmpty(), "!tasks.isEmpty()");
A.ensure(timeout >= 0, "timeout >= 0");
A.notNull(unit, "unit != null");
long startNanos = System.nanoTime();
timeout = TimeUnit.MILLISECONDS.convert(timeout, unit);
// Prevent overflow.
if (timeout <= 0)
timeout = Long.MAX_VALUE;
checkShutdown();
Collection<IgniteInternalFuture<T>> taskFuts = new ArrayList<>();
for (Callable<T> cmd : tasks) {
// Execute task with predefined timeout.
IgniteInternalFuture<T> fut;
ctx.gateway().readLock();
try {
fut = ctx.closure().callAsync(BALANCE, cmd, prj.nodes());
} finally {
ctx.gateway().readUnlock();
}
taskFuts.add(fut);
}
T res = null;
boolean isInterrupted = false;
boolean isResRcvd = false;
int errCnt = 0;
for (IgniteInternalFuture<T> fut : taskFuts) {
long passedMillis = U.millisSinceNanos(startNanos);
boolean cancel = false;
if (!isInterrupted && !isResRcvd && passedMillis < timeout) {
try {
res = fut.get(timeout - passedMillis);
isResRcvd = true;
// Cancel next tasks (avoid current task cancellation below in loop).
continue;
} catch (IgniteFutureTimeoutCheckedException ignored) {
if (log.isDebugEnabled())
log.debug("Timeout occurred during getting task result: " + fut);
cancel = true;
} catch (IgniteCheckedException e) {
// Note: that execution may be interrupted on remote node. Possible bug.
if (e.getCause() instanceof InterruptedException)
isInterrupted = true;
else
errCnt++;
}
}
// Cancel active task if any task interrupted, timeout elapsed or received task result before.
if ((isInterrupted || isResRcvd || cancel) && !fut.isDone())
cancelFuture(fut);
}
// Throw exception if any task wait was interrupted.
if (isInterrupted)
throw new InterruptedException("Got interrupted while waiting for tasks invocation.");
// per executor service contract.
if (!isResRcvd && taskFuts.size() == errCnt)
throw new ExecutionException("Failed to get any task completion.", null);
// throw timeout exception per executor service contract.
if (!isResRcvd)
throw new TimeoutException("Timeout occurred during tasks invocation.");
return res;
}
use of org.apache.ignite.internal.IgniteFutureTimeoutCheckedException in project gridgain by gridgain.
the class ConnectionClientPool method handleUnreachableNodeException.
/**
* Handles {@link NodeUnreachableException}. This means that the method will try to trigger client itself to open
* connection. The only possible way of doing this is to use {@link #connRequestor}'s trigger and wait.
* Specifics of triggers implementation technically should be considered unknown, but for now it's not true and we
* expect that {@link NodeUnreachableException} won't be thrown in {@link IgniteDiscoveryThread}.
*
* @param node Node to open connection to.
* @param connIdx Connection index.
* @param fut Current future for opening connection.
* @param e Curent exception.
* @return New future that will return the client or error. {@code null} client is possible if newly opened
* connection has been closed by idle worker, at least that's what documentation says.
* @throws IgniteCheckedException If trigerring failed or trigger is not configured.
*/
private GridFutureAdapter<GridCommunicationClient> handleUnreachableNodeException(ClusterNode node, int connIdx, GridFutureAdapter<GridCommunicationClient> fut, NodeUnreachableException e) throws IgniteCheckedException {
if (connRequestor != null) {
ConnectFuture fut0 = (ConnectFuture) fut;
final ConnectionKey key = new ConnectionKey(node.id(), connIdx, -1);
GridFutureAdapter<GridCommunicationClient> triggerFut = new GridFutureAdapter<>();
triggerFut.listen(f -> {
try {
fut0.onDone(f.get());
} catch (Throwable t) {
fut0.onDone(t);
}
});
clientFuts.put(key, triggerFut);
fut = triggerFut;
if (nodeGetter.apply(node.id()) != null) {
try {
connRequestor.request(node, connIdx);
long failTimeout = cfg.failureDetectionTimeoutEnabled() ? cfg.failureDetectionTimeout() : cfg.connectionTimeout();
fut.get(failTimeout);
} catch (Throwable triggerException) {
if (forcibleNodeKillEnabled && node.isClient() && triggerException instanceof IgniteFutureTimeoutCheckedException) {
CommunicationTcpUtils.failNode(node, tcpCommSpi.getSpiContext(), triggerException, log);
}
IgniteSpiException spiE = new IgniteSpiException(e);
spiE.addSuppressed(triggerException);
String msg = "Failed to wait for establishing inverse communication connection from node " + node;
log.warning(msg, spiE);
fut.onDone(spiE);
throw spiE;
}
} else {
ClusterTopologyCheckedException topE = new ClusterTopologyCheckedException("Failed to send message " + "(node left topology): " + node);
fut.onDone(topE);
throw topE;
}
} else {
fut.onDone(e);
throw new IgniteCheckedException(e);
}
return fut;
}
Aggregations