use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class IgnitePdsCacheRebalancingAbstractTest method testPartitionCounterConsistencyOnUnstableTopology.
/**
* @throws Exception If failed
*/
public void testPartitionCounterConsistencyOnUnstableTopology() throws Exception {
System.setProperty(IgniteSystemProperties.IGNITE_PDS_MAX_CHECKPOINT_MEMORY_HISTORY_SIZE, "1");
final Ignite ig = startGrids(4);
ig.cluster().active(true);
int k = 0;
try (IgniteDataStreamer ds = ig.dataStreamer(cacheName)) {
ds.allowOverwrite(true);
for (int k0 = k; k < k0 + 50_000; k++) ds.addData(k, k);
}
for (int t = 0; t < 5; t++) {
int t0 = t;
IgniteInternalFuture fut = GridTestUtils.runAsync(() -> {
try {
stopGrid(3);
// Clear checkpoint history to avoid rebalance from WAL.
forceCheckpoint();
forceCheckpoint();
// Wait for data load.
U.sleep(500);
IgniteEx ig0 = startGrid(3);
// Wait for node join.
U.sleep(2000);
if (t0 % 2 == 1) {
stopGrid(2);
awaitPartitionMapExchange();
// Clear checkpoint history to avoid rebalance from WAL.
forceCheckpoint();
forceCheckpoint();
startGrid(2);
awaitPartitionMapExchange();
}
ig0.cache(cacheName).rebalance().get();
} catch (Exception e) {
error("Unable to start/stop grid", e);
throw new RuntimeException(e);
}
});
try (IgniteDataStreamer ds = ig.dataStreamer(cacheName)) {
ds.allowOverwrite(true);
while (!fut.isDone()) {
int k0 = k;
for (; k < k0 + 3; k++) ds.addData(k, k);
U.sleep(1);
}
} catch (Exception e) {
log.error("Unable to write data", e);
}
fut.get();
log.info("Checking data...");
Map<Integer, Long> cntrs = new HashMap<>();
for (int g = 0; g < 4; g++) {
IgniteEx ig0 = grid(g);
for (GridDhtLocalPartition part : ig0.cachex(cacheName).context().topology().currentLocalPartitions()) {
if (cntrs.containsKey(part.id()))
assertEquals(String.valueOf(part.id()), (long) cntrs.get(part.id()), part.updateCounter());
else
cntrs.put(part.id(), part.updateCounter());
}
for (int k0 = 0; k0 < k; k0++) assertEquals(String.valueOf(k0) + " " + g, k0, ig0.cache(cacheName).get(k0));
}
assertEquals(ig.affinity(cacheName).partitions(), cntrs.size());
}
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class IgnitePdsCacheRebalancingAbstractTest method testTopologyChangesWithConstantLoad.
/**
* @throws Exception If failed.
*/
public void testTopologyChangesWithConstantLoad() throws Exception {
final long timeOut = U.currentTimeMillis() + 10 * 60 * 1000;
final int entriesCnt = 10_000;
int maxNodesCount = 4;
int topChanges = 20;
final String cacheName = "indexed";
final AtomicBoolean stop = new AtomicBoolean();
final ConcurrentMap<Integer, TestValue> map = new ConcurrentHashMap<>();
Ignite ignite = startGrid(0);
ignite.cluster().active(true);
IgniteCache<Integer, TestValue> cache = ignite.cache(cacheName);
for (int i = 0; i < entriesCnt; i++) {
cache.put(i, new TestValue(i, i));
map.put(i, new TestValue(i, i));
}
final AtomicInteger nodesCnt = new AtomicInteger();
IgniteInternalFuture fut = runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
while (true) {
if (stop.get())
return null;
int k = ThreadLocalRandom.current().nextInt(entriesCnt);
int v1 = ThreadLocalRandom.current().nextInt();
int v2 = ThreadLocalRandom.current().nextInt();
int n = nodesCnt.get();
if (n <= 0)
continue;
Ignite ignite;
try {
ignite = grid(ThreadLocalRandom.current().nextInt(n));
} catch (Exception ignored) {
continue;
}
if (ignite == null)
continue;
Transaction tx = null;
boolean success = true;
if (explicitTx)
tx = ignite.transactions().txStart();
try {
ignite.cache(cacheName).put(k, new TestValue(v1, v2));
} catch (Exception ignored) {
success = false;
} finally {
if (tx != null) {
try {
tx.commit();
} catch (Exception ignored) {
success = false;
}
}
}
if (success)
map.put(k, new TestValue(v1, v2));
}
}
}, 1, "load-runner");
try {
for (int i = 0; i < topChanges; i++) {
if (U.currentTimeMillis() > timeOut)
break;
U.sleep(3_000);
boolean add;
if (nodesCnt.get() <= maxNodesCount / 2)
add = true;
else if (nodesCnt.get() > maxNodesCount)
add = false;
else
// More chance that node will be added
add = ThreadLocalRandom.current().nextInt(3) <= 1;
if (add)
startGrid(nodesCnt.incrementAndGet());
else
stopGrid(nodesCnt.getAndDecrement());
awaitPartitionMapExchange();
cache.rebalance().get();
}
} finally {
stop.set(true);
}
fut.get();
awaitPartitionMapExchange();
for (Map.Entry<Integer, TestValue> entry : map.entrySet()) assertEquals(Integer.toString(entry.getKey()), entry.getValue(), cache.get(entry.getKey()));
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class JdbcThinStatementSelfTest method testCancel.
/**
* @throws Exception If failed.
*/
public void testCancel() throws Exception {
fail("https://issues.apache.org/jira/browse/IGNITE-5439");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
stmt.execute("select sleep_func(3)");
return null;
}
}, SQLException.class, "The query is canceled");
IgniteInternalFuture f = GridTestUtils.runAsync(new Runnable() {
@Override
public void run() {
try {
stmt.cancel();
} catch (SQLException e) {
log.error("Unexpected exception", e);
fail("Unexpected exception.");
}
}
});
f.get();
stmt.close();
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
stmt.cancel();
return null;
}
}, SQLException.class, "Statement is closed");
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class GridDhtPreloader method request0.
/**
* @param cctx Cache context.
* @param keys Keys to request.
* @param topVer Topology version.
* @return Future for request.
*/
@SuppressWarnings({ "unchecked", "RedundantCast" })
private GridDhtFuture<Object> request0(GridCacheContext cctx, Collection<KeyCacheObject> keys, AffinityTopologyVersion topVer) {
if (cctx.isNear())
cctx = cctx.near().dht().context();
final GridDhtForceKeysFuture<?, ?> fut = new GridDhtForceKeysFuture<>(cctx, topVer, keys);
IgniteInternalFuture<?> topReadyFut = cctx.affinity().affinityReadyFuturex(topVer);
if (startFut.isDone() && topReadyFut == null)
fut.init();
else {
if (topReadyFut == null)
startFut.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> syncFut) {
ctx.kernalContext().closure().runLocalSafe(new GridPlainRunnable() {
@Override
public void run() {
fut.init();
}
});
}
});
else {
GridCompoundFuture<Object, Object> compound = new GridCompoundFuture<>();
compound.add((IgniteInternalFuture<Object>) startFut);
compound.add((IgniteInternalFuture<Object>) topReadyFut);
compound.markInitialized();
compound.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> syncFut) {
fut.init();
}
});
}
}
return (GridDhtFuture) fut;
}
use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.
the class GridTaskCommandHandler method handleAsyncUnsafe.
/**
* @param req Request.
* @return Future.
* @throws IgniteCheckedException On any handling exception.
*/
private IgniteInternalFuture<GridRestResponse> handleAsyncUnsafe(final GridRestRequest req) throws IgniteCheckedException {
assert req instanceof GridRestTaskRequest : "Invalid command for topology handler: " + req;
assert SUPPORTED_COMMANDS.contains(req.command());
if (log.isDebugEnabled())
log.debug("Handling task REST request: " + req);
GridRestTaskRequest req0 = (GridRestTaskRequest) req;
final GridFutureAdapter<GridRestResponse> fut = new GridFutureAdapter<>();
final GridRestResponse res = new GridRestResponse();
final GridClientTaskResultBean taskRestRes = new GridClientTaskResultBean();
// Set ID placeholder for the case it wouldn't be available due to remote execution.
taskRestRes.setId('~' + ctx.localNodeId().toString());
final boolean locExec = req0.destinationId() == null || req0.destinationId().equals(ctx.localNodeId()) || ctx.discovery().node(req0.destinationId()) == null;
switch(req.command()) {
case EXE:
{
final boolean async = req0.async();
final String name = req0.taskName();
if (F.isEmpty(name))
throw new IgniteCheckedException(missingParameter("name"));
final List<Object> params = req0.params();
long timeout = req0.timeout();
final UUID clientId = req.clientId();
final IgniteInternalFuture<Object> taskFut;
if (locExec) {
ctx.task().setThreadContextIfNotNull(TC_SUBJ_ID, clientId);
ctx.task().setThreadContext(TC_TIMEOUT, timeout);
Object arg = !F.isEmpty(params) ? params.size() == 1 ? params.get(0) : params.toArray() : null;
taskFut = ctx.task().execute(name, arg);
} else {
// Using predicate instead of node intentionally
// in order to provide user well-structured EmptyProjectionException.
ClusterGroup prj = ctx.grid().cluster().forPredicate(F.nodeForNodeId(req.destinationId()));
ctx.task().setThreadContext(TC_NO_FAILOVER, true);
taskFut = ctx.closure().callAsync(BALANCE, new ExeCallable(name, params, timeout, clientId), prj.nodes());
}
if (async) {
if (locExec) {
IgniteUuid tid = ((ComputeTaskInternalFuture) taskFut).getTaskSession().getId();
taskDescs.put(tid, new TaskDescriptor(false, null, null));
taskRestRes.setId(tid.toString() + '~' + ctx.localNodeId().toString());
res.setResponse(taskRestRes);
} else
res.setError("Asynchronous task execution is not supported for routing request.");
fut.onDone(res);
}
taskFut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {
@Override
public void apply(IgniteInternalFuture<Object> taskFut) {
try {
TaskDescriptor desc;
try {
desc = new TaskDescriptor(true, taskFut.get(), null);
} catch (IgniteCheckedException e) {
if (e.hasCause(ClusterTopologyCheckedException.class, ClusterGroupEmptyCheckedException.class))
U.warn(log, "Failed to execute task due to topology issues (are all mapped " + "nodes alive?) [name=" + name + ", clientId=" + req.clientId() + ", err=" + e + ']');
else {
if (!X.hasCause(e, VisorClusterGroupEmptyException.class))
U.error(log, "Failed to execute task [name=" + name + ", clientId=" + req.clientId() + ']', e);
}
desc = new TaskDescriptor(true, null, e);
}
if (async && locExec) {
assert taskFut instanceof ComputeTaskInternalFuture;
IgniteUuid tid = ((ComputeTaskInternalFuture) taskFut).getTaskSession().getId();
taskDescs.put(tid, desc);
}
if (!async) {
if (desc.error() == null) {
try {
taskRestRes.setFinished(true);
taskRestRes.setResult(desc.result());
res.setResponse(taskRestRes);
fut.onDone(res);
} catch (IgniteException e) {
fut.onDone(new IgniteCheckedException("Failed to marshal task result: " + desc.result(), e));
}
} else
fut.onDone(desc.error());
}
} finally {
if (!async && !fut.isDone())
fut.onDone(new IgniteCheckedException("Failed to execute task (see server logs for details)."));
}
}
});
break;
}
case RESULT:
{
String id = req0.taskId();
if (F.isEmpty(id))
throw new IgniteCheckedException(missingParameter("id"));
StringTokenizer st = new StringTokenizer(id, "~");
if (st.countTokens() != 2)
throw new IgniteCheckedException("Failed to parse id parameter: " + id);
String tidParam = st.nextToken();
String resHolderIdParam = st.nextToken();
taskRestRes.setId(id);
try {
IgniteUuid tid = !F.isEmpty(tidParam) ? IgniteUuid.fromString(tidParam) : null;
UUID resHolderId = !F.isEmpty(resHolderIdParam) ? UUID.fromString(resHolderIdParam) : null;
if (tid == null || resHolderId == null)
throw new IgniteCheckedException("Failed to parse id parameter: " + id);
if (ctx.localNodeId().equals(resHolderId)) {
TaskDescriptor desc = taskDescs.get(tid);
if (desc == null)
throw new IgniteCheckedException("Task with provided id has never been started on provided node" + " [taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ']');
taskRestRes.setFinished(desc.finished());
if (desc.error() != null)
throw new IgniteCheckedException(desc.error().getMessage());
taskRestRes.setResult(desc.result());
res.setResponse(taskRestRes);
} else {
IgniteBiTuple<String, GridTaskResultResponse> t = requestTaskResult(resHolderId, tid);
if (t.get1() != null)
throw new IgniteCheckedException(t.get1());
GridTaskResultResponse taskRes = t.get2();
assert taskRes != null;
if (!taskRes.found())
throw new IgniteCheckedException("Task with provided id has never been started on provided node " + "[taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ']');
taskRestRes.setFinished(taskRes.finished());
if (taskRes.error() != null)
throw new IgniteCheckedException(taskRes.error());
taskRestRes.setResult(taskRes.result());
res.setResponse(taskRestRes);
}
} catch (IllegalArgumentException e) {
String msg = "Failed to parse parameters [taskId=" + tidParam + ", taskResHolderId=" + resHolderIdParam + ", err=" + e.getMessage() + ']';
if (log.isDebugEnabled())
log.debug(msg);
throw new IgniteCheckedException(msg, e);
}
fut.onDone(res);
break;
}
case NOOP:
{
fut.onDone(new GridRestResponse());
break;
}
default:
assert false : "Invalid command for task handler: " + req;
}
if (log.isDebugEnabled())
log.debug("Handled task REST request [res=" + res + ", req=" + req + ']');
return fut;
}
Aggregations