use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class IgniteCacheLockPartitionOnAffinityRunTest method testReleasePartitionJobImplementMasterLeave.
/**
* @throws Exception If failed.
*/
public void testReleasePartitionJobImplementMasterLeave() throws Exception {
final int orgId = primaryKey(grid(0).cache(Organization.class.getSimpleName()));
try {
grid(1).compute().affinityRunAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new RunnableWithMasterLeave() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public void onMasterNodeLeft(ComputeTaskSession ses) throws IgniteException {
// No-op.
}
@Override
public void run() {
try {
checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
// No-op.
}
}
});
stopGrid(1, true);
Thread.sleep(3000);
awaitPartitionMapExchange();
checkPartitionsReservations(grid(0), orgId, 0);
} finally {
startGrid(1);
awaitPartitionMapExchange();
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class IgniteCacheLockPartitionOnAffinityRunTest method testReleasePartitionJobMasterLeave.
/**
* @throws Exception If failed.
*/
public void testReleasePartitionJobMasterLeave() throws Exception {
final int orgId = primaryKey(grid(0).cache(Organization.class.getSimpleName()));
try {
grid(1).compute().affinityRunAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new IgniteRunnable() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public void run() {
try {
checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
// No-op.
}
}
});
stopGrid(1, true);
Thread.sleep(3000);
awaitPartitionMapExchange();
checkPartitionsReservations(grid(0), orgId, 0);
} finally {
startGrid(1);
awaitPartitionMapExchange();
}
try {
grid(1).compute().affinityCallAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new IgniteCallable<Object>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public Object call() {
try {
checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
// No-op.
}
return null;
}
});
stopGrid(1, true);
Thread.sleep(3000);
awaitPartitionMapExchange();
checkPartitionsReservations(grid(0), orgId, 0);
} finally {
startGrid(1);
awaitPartitionMapExchange();
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class ClosureServiceClientsNodesTest method testClientClosure.
/**
* @throws Exception If failed.
*/
public void testClientClosure() throws Exception {
for (int i = 0; i < NODES_CNT; i++) {
log.info("Iteration: " + i);
Ignite ignite = grid(i);
Collection<String> res = ignite.compute(ignite.cluster().forClients()).broadcast(new IgniteCallable<String>() {
@IgniteInstanceResource
Ignite ignite;
@Override
public String call() throws Exception {
assertTrue(ignite.configuration().isClientMode());
return ignite.name();
}
});
assertEquals(1, res.size());
assertEquals(getTestIgniteInstanceName(0), F.first(res));
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCachePutAllTask method map.
/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable final Collection<Integer> data) {
assert !subgrid.isEmpty();
// Give preference to wanted node. Otherwise, take the first one.
ClusterNode targetNode = F.find(subgrid, subgrid.get(0), new IgnitePredicate<ClusterNode>() {
/** {@inheritDoc} */
@Override
public boolean apply(ClusterNode e) {
return preferredNode.equals(e.id());
}
});
return Collections.singletonMap(new ComputeJobAdapter() {
@LoggerResource
private IgniteLogger log;
@IgniteInstanceResource
private Ignite ignite;
@Override
public Object execute() {
if (DEBUG_DATA)
log.info("Going to put data: " + data);
else
log.info("Going to put data [size=" + data.size() + ']');
IgniteCache<Object, Object> cache = ignite.cache(cacheName);
assert cache != null;
HashMap<Integer, Integer> putMap = U.newLinkedHashMap(TX_BOUND);
Iterator<Integer> it = data.iterator();
int cnt = 0;
final int RETRIES = 5;
while (it.hasNext()) {
Integer val = it.next();
putMap.put(val, val);
if (++cnt == TX_BOUND) {
if (DEBUG_DATA)
log.info("Putting keys to cache: " + putMap.keySet());
else
log.info("Putting keys to cache [size=" + putMap.size() + ']');
for (int i = 0; i < RETRIES; i++) {
try {
cache.putAll(putMap);
break;
} catch (CacheException e) {
if (i < RETRIES - 1)
log.info("Put error, will retry: " + e);
else
throw new IgniteException(e);
}
}
cnt = 0;
putMap = U.newLinkedHashMap(TX_BOUND);
}
}
assert cnt < TX_BOUND;
assert putMap.size() == (data.size() % TX_BOUND) : "putMap.size() = " + putMap.size();
if (DEBUG_DATA)
log.info("Putting keys to cache: " + putMap.keySet());
else
log.info("Putting keys to cache [size=" + putMap.size() + ']');
for (int i = 0; i < RETRIES; i++) {
try {
cache.putAll(putMap);
break;
} catch (CacheException e) {
if (i < RETRIES - 1)
log.info("Put error, will retry: " + e);
else
throw new IgniteException(e);
}
}
if (DEBUG_DATA)
log.info("Finished putting data: " + data);
else
log.info("Finished putting data [size=" + data.size() + ']');
return data;
}
}, targetNode);
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCacheQueueApiSelfAbstractTest method testAffinityRun.
/**
* @throws Exception If failed.
*/
public void testAffinityRun() throws Exception {
final CollectionConfiguration colCfg = collectionConfiguration();
colCfg.setCollocated(false);
colCfg.setCacheMode(CacheMode.PARTITIONED);
try (final IgniteQueue<Integer> queue1 = grid(0).queue("Queue1", 0, colCfg)) {
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
queue1.affinityRun(new IgniteRunnable() {
@Override
public void run() {
// No-op.
}
});
return null;
}
}, IgniteException.class, "Failed to execute affinityRun() for non-collocated queue: " + queue1.name() + ". This operation is supported only for collocated queues.");
}
colCfg.setCollocated(true);
try (final IgniteQueue<Integer> queue2 = grid(0).queue("Queue2", 0, colCfg)) {
queue2.add(100);
queue2.affinityRun(new IgniteRunnable() {
@IgniteInstanceResource
private IgniteEx ignite;
@Override
public void run() {
assertTrue(ignite.cachex("datastructures_0").affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Queue2"));
assertEquals(100, queue2.take().intValue());
}
});
}
}
Aggregations