use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class IgniteCacheGroupsTest method cacheDataStreamer.
/**
* @param cache Cache.
* @throws Exception If failed.
*/
private void cacheDataStreamer(final IgniteCache cache) throws Exception {
final int keys = 400;
final int loaders = 4;
final Integer[] data = generateData(keys * loaders);
// Stream through a client node.
Ignite clientNode = ignite(4);
List<Callable<?>> cls = new ArrayList<>(loaders);
for (final int i : sequence(loaders)) {
final IgniteDataStreamer ldr = clientNode.dataStreamer(cache.getName());
ldr.autoFlushFrequency(0);
cls.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
List<IgniteFuture> futs = new ArrayList<>(keys);
for (int j = 0, size = keys * loaders; j < size; j++) {
if (j % loaders == i)
futs.add(ldr.addData(j, data[j]));
if (j % (100 * loaders) == 0)
ldr.flush();
}
ldr.flush();
for (IgniteFuture fut : futs) fut.get();
return null;
}
});
}
GridTestUtils.runMultiThreaded(cls, "loaders");
Set<Integer> keysSet = sequence(data.length);
for (Cache.Entry<Integer, Integer> entry : (IgniteCache<Integer, Integer>) cache) {
assertTrue(keysSet.remove(entry.getKey()));
assertEquals(data[entry.getKey()], entry.getValue());
}
assertTrue(keysSet.isEmpty());
tearDown(cache);
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class GridCacheQueueJoinedNodeSelfAbstractTest method testTakeFromJoined.
/**
* @throws Exception If failed.
*/
public void testTakeFromJoined() throws Exception {
String queueName = UUID.randomUUID().toString();
IgniteQueue<Integer> queue = grid(0).queue(queueName, 0, config(true));
assertNotNull(queue);
assertTrue(queue.isEmpty());
PutJob putJob = new PutJob(queueName);
IgniteCompute comp = compute(grid(0).cluster().forLocal());
IgniteFuture<?> fut = comp.runAsync(putJob);
Collection<IgniteFuture<?>> futs = new ArrayList<>(GRID_CNT - 1);
Collection<TakeJob> jobs = new ArrayList<>(GRID_CNT - 1);
int itemsLeft = ITEMS_CNT;
for (int i = 1; i < GRID_CNT; i++) {
int cnt = ITEMS_CNT / (GRID_CNT - 1);
TakeJob job = new TakeJob(queueName, cnt, 10);
jobs.add(job);
comp = compute(grid(i).cluster().forLocal());
futs.add(comp.callAsync(job));
itemsLeft -= cnt;
}
assertEquals("Not all items will be polled", 0, itemsLeft);
// Wait for half of items to be polled.
for (TakeJob job : jobs) job.awaitItems();
log.info("Start one more grid.");
Ignite joined = startGrid(GRID_CNT);
// We expect at least one item to be taken.
TakeJob joinedJob = new TakeJob(queueName, 1, 1);
jobs.add(joinedJob);
Integer polled = forLocal(joined).call(joinedJob);
assertNotNull("Joined node should poll item", polled);
info(">>> Joined node polled " + polled);
for (IgniteFuture<?> f : futs) f.cancel();
putJob.stop(true);
fut.get();
for (TakeJob job : jobs) job.awaitDone();
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class IgniteBinaryMetadataUpdateChangingTopologySelfTest method testNoDeadlockInvoke.
/**
* @throws Exception If failed.
*/
public void testNoDeadlockInvoke() throws Exception {
int key1 = primaryKey(ignite(1).cache("cache"));
int key2 = primaryKey(ignite(2).cache("cache"));
TestCommunicationSpi spi = (TestCommunicationSpi) ignite(1).configuration().getCommunicationSpi();
spi.blockMessages(GridNearTxPrepareResponse.class, ignite(0).cluster().localNode().id());
IgniteCache<Object, Object> cache = ignite(0).cache("cache");
IgniteFuture futInvokeAll = cache.invokeAllAsync(F.asSet(key1, key2), new TestEntryProcessor());
try {
Thread.sleep(500);
IgniteInternalFuture<Void> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
startGrid(4);
return null;
}
});
Thread.sleep(1000);
spi.stopBlock();
futInvokeAll.get();
fut.get();
} finally {
stopGrid(4);
}
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class IgniteBinaryMetadataUpdateChangingTopologySelfTest method testNoDeadlockOptimistic.
/**
* @throws Exception If failed.
*/
public void testNoDeadlockOptimistic() throws Exception {
int key1 = primaryKey(ignite(1).cache("cache"));
int key2 = primaryKey(ignite(2).cache("cache"));
TestCommunicationSpi spi = (TestCommunicationSpi) ignite(1).configuration().getCommunicationSpi();
spi.blockMessages(GridNearTxPrepareResponse.class, ignite(0).cluster().localNode().id());
IgniteCache<Object, Object> cache = ignite(0).cache("cache");
IgniteFuture futPutAll = cache.putAllAsync(F.asMap(key1, "val1", key2, new TestValue1()));
try {
Thread.sleep(500);
IgniteInternalFuture<Void> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
startGrid(4);
return null;
}
});
Thread.sleep(1000);
spi.stopBlock();
futPutAll.get();
fut.get();
} finally {
stopGrid(4);
}
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class GridCachePutAllFailoverSelfTest method checkPutAllFailover.
/**
* Tests putAll() method along with failover and cache backup.
*
* Checks that the resulting primary cache size is the same as
* expected.
*
* @param near Near enabled.
* @param workerCnt Worker count.
* @param shutdownCnt Shutdown count.
* @throws Exception If failed.
*/
public void checkPutAllFailover(boolean near, int workerCnt, int shutdownCnt) throws Exception {
nearEnabled = near;
backups = shutdownCnt;
Collection<Integer> testKeys = generateTestKeys();
final Ignite master = startGrid(MASTER);
List<Ignite> workers = new ArrayList<>(workerCnt);
for (int i = 1; i <= workerCnt; i++) workers.add(startGrid("worker" + i));
info("Master: " + master.cluster().localNode().id());
List<Ignite> runningWorkers = new ArrayList<>(workerCnt);
for (int i = 1; i <= workerCnt; i++) {
UUID id = workers.get(i - 1).cluster().localNode().id();
info(String.format("Worker%d - %s", i, id));
runningWorkers.add(workers.get(i - 1));
}
try {
// Dummy call to fetch affinity function from remote node
master.affinity(CACHE_NAME).mapKeyToNode("Dummy");
Random rnd = new Random();
Collection<Integer> dataChunk = new ArrayList<>(DATA_CHUNK_SIZE);
int entryCntr = 0;
int chunkCntr = 0;
final AtomicBoolean jobFailed = new AtomicBoolean(false);
int failoverPushGap = 0;
final CountDownLatch emptyLatch = new CountDownLatch(1);
final AtomicBoolean inputExhausted = new AtomicBoolean();
IgniteCompute comp = compute(master.cluster().forPredicate(workerNodesFilter));
for (Integer key : testKeys) {
dataChunk.add(key);
entryCntr++;
if (entryCntr == DATA_CHUNK_SIZE) {
// time to send data
chunkCntr++;
assert dataChunk.size() == DATA_CHUNK_SIZE;
log.info("Pushing data chunk [chunkNo=" + chunkCntr + "]");
ComputeTaskFuture<Void> fut = comp.executeAsync(new GridCachePutAllTask(runningWorkers.get(rnd.nextInt(runningWorkers.size())).cluster().localNode().id(), CACHE_NAME), dataChunk);
// Blocks if queue is full.
resQueue.put(fut);
fut.listen(new CI1<IgniteFuture<Void>>() {
@Override
public void apply(IgniteFuture<Void> f) {
ComputeTaskFuture<?> taskFut = (ComputeTaskFuture<?>) f;
try {
// if something went wrong - we'll get exception here
taskFut.get();
} catch (IgniteException e) {
log.error("Job failed", e);
jobFailed.set(true);
}
// Remove complete future from queue to allow other jobs to proceed.
resQueue.remove(taskFut);
if (inputExhausted.get() && resQueue.isEmpty())
emptyLatch.countDown();
}
});
entryCntr = 0;
dataChunk = new ArrayList<>(DATA_CHUNK_SIZE);
if (chunkCntr >= FAIL_ON_CHUNK_NO) {
if (workerCnt - runningWorkers.size() < shutdownCnt) {
if (failoverPushGap > 0)
failoverPushGap--;
else {
Ignite victim = runningWorkers.remove(0);
info("Shutting down node: " + victim.cluster().localNode().id());
stopGrid(victim.name());
// Fail next node after some jobs have been pushed.
failoverPushGap = FAILOVER_PUSH_GAP;
}
}
}
}
}
inputExhausted.set(true);
if (resQueue.isEmpty())
emptyLatch.countDown();
assert chunkCntr == TEST_MAP_SIZE / DATA_CHUNK_SIZE;
// Wait for queue to empty.
log.info("Waiting for empty queue...");
boolean failedWait = false;
if (!emptyLatch.await(AWAIT_TIMEOUT_SEC, TimeUnit.SECONDS)) {
info(">>> Failed to wait for queue to empty.");
failedWait = true;
}
if (!failedWait)
assertFalse("One or more jobs have failed.", jobFailed.get());
Collection<Integer> absentKeys = findAbsentKeys(runningWorkers.get(0), testKeys);
if (!failedWait && !absentKeys.isEmpty()) {
// Give some time to preloader.
U.sleep(20000);
absentKeys = findAbsentKeys(runningWorkers.get(0), testKeys);
}
info(">>> Absent keys: " + absentKeys);
if (!F.isEmpty(absentKeys)) {
for (Ignite g : runningWorkers) {
IgniteKernal k = (IgniteKernal) g;
info(">>>> Entries on node: " + k.getLocalNodeId());
GridCacheAdapter<Object, Object> cache = k.internalCache("partitioned");
for (Integer key : absentKeys) {
GridCacheEntryEx entry = cache.peekEx(key);
if (entry != null)
info(" >>> " + entry);
if (cache.context().isNear()) {
GridCacheEntryEx entry0 = cache.context().near().dht().peekEx(key);
if (entry0 != null)
info(" >>> " + entry);
}
}
info("");
}
}
assertTrue(absentKeys.isEmpty());
// Actual primary cache size.
int primaryCacheSize = 0;
for (Ignite g : runningWorkers) {
info("Cache size [node=" + g.name() + ", localSize=" + g.cache(CACHE_NAME).localSize() + ", localPrimarySize=" + g.cache(CACHE_NAME).localSize(PRIMARY) + ']');
primaryCacheSize += ((IgniteKernal) g).internalCache(CACHE_NAME).primarySize();
}
assertEquals(TEST_MAP_SIZE, primaryCacheSize);
for (Ignite g : runningWorkers) assertEquals(TEST_MAP_SIZE, g.cache(CACHE_NAME).size(PRIMARY));
} finally {
stopAllGrids();
}
}
Aggregations