use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class IgniteRoundRobinErrorAfterClientReconnectTest method testClientReconnect.
/**
* @throws Exception If failed.
*/
@Test
public void testClientReconnect() throws Exception {
final Ignite cli = grid(CLI_IDX);
final GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
cli.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event event) {
try {
cli.compute().apply(new IgniteClosure<String, Void>() {
@Override
public Void apply(String arg) {
return null;
}
}, "Hello!");
fut.onDone(true);
return true;
} catch (Exception e) {
fut.onDone(e);
return false;
}
}
}, EventType.EVT_CLIENT_NODE_RECONNECTED);
stopGrid(SRV_IDX);
startGrid(SRV_IDX);
assert fut.get();
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class TxRollbackAsyncTest method testSynchronousRollback0.
/**
* @param holdLockNode Node holding the write lock.
* @param tryLockNode Node trying to acquire lock.
* @param useTimeout {@code True} if need to start tx with timeout.
* @throws Exception If failed.
*/
private void testSynchronousRollback0(Ignite holdLockNode, final Ignite tryLockNode, final boolean useTimeout) throws Exception {
final GridFutureAdapter<Void> keyLocked = new GridFutureAdapter<>();
CountDownLatch waitCommit = new CountDownLatch(1);
// Used for passing tx instance to rollback thread.
IgniteInternalFuture<?> lockFut = lockInTx(holdLockNode, keyLocked, waitCommit, 0);
keyLocked.get();
final int txCnt = SF.applyLB(250, 25);
final IgniteKernal k = (IgniteKernal) tryLockNode;
final GridCacheSharedContext<Object, Object> ctx = k.context().cache().context();
final GridCacheContext<Object, Object> cctx = ctx.cacheContext(CU.cacheId(CACHE_NAME));
GridFutureAdapter<Transaction> txReadyFut = new GridFutureAdapter<>();
long seed = System.currentTimeMillis();
Random r = new Random(seed);
log.info("Running: node0=" + holdLockNode.cluster().localNode().consistentId() + ", node1=" + tryLockNode.cluster().localNode().consistentId() + ", useTimeout=" + useTimeout + ", seed=" + seed);
IgniteInternalFuture<?> txFut = multithreadedAsync(new Runnable() {
@Override
public void run() {
for (int i = 0; i < txCnt; i++) {
GridNearTxLocal tx0 = ctx.tm().threadLocalTx(cctx);
assertTrue(tx0 == null || tx0.state() == ROLLED_BACK);
try (Transaction tx = tryLockNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, useTimeout ? 50 : 0, 1)) {
txReadyFut.onDone(tx);
// Will block on lock request until rolled back asynchronously.
Object o = tryLockNode.cache(CACHE_NAME).getAndPut(0, 0);
// If rolled back by close, previous get will return null.
assertNull(o);
} catch (Exception ignore) {
// If rolled back by rollback, previous get will throw an exception.
}
}
txReadyFut.onDone((Transaction) null);
}
}, 1, "tx-get-thread");
IgniteInternalFuture<?> rollbackFut = multithreadedAsync(new Runnable() {
@Override
public void run() {
Set<IgniteUuid> rolledBackVers = new HashSet<>();
int proc = 1;
while (true) {
try {
Transaction tx = txReadyFut.get();
txReadyFut.reset();
if (tx == null)
break;
// Wait a bit to reduce chance of rolling back empty transactions.
doSleep(r.nextInt(15));
if (rolledBackVers.contains(tx.xid()))
fail("Rollback version is expected");
try {
if (proc % 2 == 0)
tx.rollback();
else
tx.close();
} catch (IgniteException e) {
log.warning("Got exception while rolling back a transaction", e);
}
rolledBackVers.add(tx.xid());
if (proc % 100 == 0)
log.info("Rolled back: " + proc);
proc++;
} catch (IgniteCheckedException e) {
fail(e.getMessage());
}
}
}
}, 1, "tx-rollback-thread");
rollbackFut.get();
txFut.get();
log.info("All transactions are rolled back: holdLockNode=" + holdLockNode + ", tryLockNode=" + tryLockNode);
waitCommit.countDown();
lockFut.get();
assertEquals(0, holdLockNode.cache(CACHE_NAME).get(0));
checkFutures();
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class TxPartitionCounterStateOnePrimaryTwoBackupsTest method testMissingUpdateBetweenMultipleCheckpoints.
/**
* Test scenario:
* <p>
* 1. Assign counters in order tx0, tx1
* <p>
* 2. Commit tx1.
* <p>
* 3. Delay tx0 commit on backup1.
* <p>
* 4. Put more keys in partition, trigger checkpoint, put more keys.
* <p>
* 5. Commit delayed tx closing gap.
* <p>
* 5. Restart backup1 without triggering checkpoint on stop.
* <p>
*
* Pass condition: backup1 after restart has sequential update counter. No rebalance is expected.
* The test states necessity of storing gaps between checkpoints.
*
* @throws Exception If failed.
*/
@Test
public void testMissingUpdateBetweenMultipleCheckpoints() throws Exception {
int[] sizes = new int[] { 3, 7 };
int[] assignOrder = new int[] { 0, 1 };
int delayBackupIdx = 0;
GridFutureAdapter<T2<Ignite, GridFutureAdapter>> fut = new GridFutureAdapter<>();
GridTestUtils.runAsync(new Runnable() {
@Override
public void run() {
try {
T2<Ignite, GridFutureAdapter> pair = fut.get(30, TimeUnit.SECONDS);
IgniteEx client = grid(CLIENT_GRID_NAME);
// Allow txs to work as usual.
for (Ignite node : G.allGrids()) TestRecordingCommunicationSpi.spi(node).stopBlock(false, null, true, false);
List<Integer> keys = partitionKeys(client.cache(DEFAULT_CACHE_NAME), PARTITION_ID, 10, sizes[0] + sizes[1] + PRELOAD_KEYS_CNT);
for (Integer key : keys) client.cache(DEFAULT_CACHE_NAME).put(key, key);
Ignite backup1 = pair.get1();
forceCheckpoint(backup1);
// Commit delayed tx.
pair.get2().onDone();
} catch (IgniteCheckedException e) {
fail(X.getFullStackTrace(e));
}
}
});
Map<Integer, T2<Ignite, List<Ignite>>> txTops = runOnPartition(PARTITION_ID, null, BACKUPS, SERVERS_CNT, map -> {
Ignite primary = map.get(PARTITION_ID).get1();
Ignite backup1 = map.get(PARTITION_ID).get2().get(delayBackupIdx);
return new TwoPhaseCommitTxCallbackAdapter(U.map((IgniteEx) primary, assignOrder), new HashMap<>(), new HashMap<>(), sizes.length) {
@Override
public boolean beforeBackupFinish(IgniteEx primary, IgniteEx backup, @Nullable IgniteInternalTx primaryTx, IgniteInternalTx backupTx, IgniteUuid nearXidVer, GridFutureAdapter<?> proceedFut) {
if (order(nearXidVer) == assignOrder[0] && backup == backup1) {
fut.onDone(new T2<>(backup1, proceedFut));
// Delay commit on backup.
return true;
}
return super.beforeBackupFinish(primary, backup, primaryTx, backupTx, nearXidVer, proceedFut);
}
};
}, sizes);
// At this point all txs are committed and no gaps are expected.
Ignite backup1 = txTops.get(PARTITION_ID).get2().get(delayBackupIdx);
PartitionUpdateCounter cntr;
assertNotNull(cntr = counter(PARTITION_ID, backup1.name()));
assertTrue(cntr.sequential());
stopGrid(true, backup1.name());
startGrid(backup1.name());
awaitPartitionMapExchange();
assertNotNull(cntr = counter(PARTITION_ID, backup1.name()));
assertTrue(cntr.sequential());
assertPartitionsSame(idleVerify(grid(CLIENT_GRID_NAME), DEFAULT_CACHE_NAME));
assertCountersSame(PARTITION_ID, true, DEFAULT_CACHE_NAME);
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class TxPartitionCounterStateOnePrimaryTwoBackupsTest method doTestPartialCommit_3tx_2.
/**
* Test scenario:
* <p>
* 1. Assign counters in specified order.
* <p>
* 2. Prepare all three txs on backups.
* <p>
* 3. Fail primary to trigger recovery. Some tx will be committed, some will be rolled back.
* <p>
* Pass condition: after primary joined partitions are consistent, all transactions are committed.
*
* @param skipCheckpointOnStop Skip checkpoint on node stop.
* @throws Exception If failed.
*/
private void doTestPartialCommit_3tx_2(boolean skipCheckpointOnStop) throws Exception {
Map<Integer, T2<Ignite, List<Ignite>>> txTops = runOnPartition(PARTITION_ID, null, BACKUPS, SERVERS_CNT, map -> {
Ignite primary = map.get(PARTITION_ID).get1();
final Ignite backup1 = map.get(PARTITION_ID).get2().get(0);
final Ignite backup2 = map.get(PARTITION_ID).get2().get(1);
return new TwoPhaseCommitTxCallbackAdapter(U.map((IgniteEx) primary, new int[] { 0, 1, 2 }), U.map((IgniteEx) primary, new int[] { 0, 1, 2 }, (IgniteEx) backup1, new int[] { 0, 1, 2 }, (IgniteEx) backup2, new int[] { 0, 1, 2 }), new HashMap<>(), SIZES.length) {
@Override
public boolean beforePrimaryFinish(IgniteEx primary, IgniteInternalTx tx, GridFutureAdapter<?> proceedFut) {
runAsync(() -> {
stopGrid(skipCheckpointOnStop, primary.name());
TestRecordingCommunicationSpi.stopBlockAll();
});
// Stop primary before any tx committed.
return true;
}
};
}, SIZES);
waitForTopology(SERVERS_CNT);
awaitPartitionMapExchange();
IgniteEx client = grid(CLIENT_GRID_NAME);
assertPartitionsSame(idleVerify(client, DEFAULT_CACHE_NAME));
assertCountersSame(PARTITION_ID, true, DEFAULT_CACHE_NAME);
startGrid(txTops.get(PARTITION_ID).get1().name());
awaitPartitionMapExchange();
// TODO assert with expected lwm value.
assertCountersSame(PARTITION_ID, true, DEFAULT_CACHE_NAME);
assertPartitionsSame(idleVerify(client, DEFAULT_CACHE_NAME));
}
use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.
the class GridFutureListenPerformanceTest method main.
/**
* @param args Args.
* @throws InterruptedException If failed.
*/
public static void main(String[] args) throws InterruptedException {
final LongAdder cnt = new LongAdder();
final ConcurrentLinkedDeque<GridFutureAdapter<Object>> futs = new ConcurrentLinkedDeque<>();
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Thread statThread = new Thread() {
@SuppressWarnings("BusyWait")
@Override
public void run() {
while (!done) {
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {
return;
}
System.out.println(new Date() + " Notifications per sec: " + (cnt.sumThenReset() / 5));
}
}
};
statThread.setDaemon(true);
statThread.start();
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
pool.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Random rnd = new Random();
while (!done) {
for (int j = 0; j < rnd.nextInt(10); j++) {
GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
futs.add(fut);
for (int k = 1; k < rnd.nextInt(3); k++) {
fut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {
@Override
public void apply(IgniteInternalFuture<Object> t) {
try {
t.get();
} catch (IgniteCheckedException e) {
e.printStackTrace();
}
cnt.increment();
}
});
}
}
GridFutureAdapter<Object> fut;
while ((fut = futs.poll()) != null) fut.onDone();
}
return null;
}
});
}
Thread.sleep(5 * 60 * 1000);
done = true;
pool.shutdownNow();
pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
Aggregations