use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class IgniteTxHandler method prepareNearTx.
/**
* @param nearNodeId Near node ID that initiated transaction.
* @param req Near prepare request.
* @param locReq Local request flag.
* @return Prepare future.
*/
public IgniteInternalFuture<GridNearTxPrepareResponse> prepareNearTx(final UUID nearNodeId, final GridNearTxPrepareRequest req, boolean locReq) {
// Make sure not to provide Near entries to DHT cache.
if (locReq)
req.cloneEntries();
ClusterNode nearNode = ctx.node(nearNodeId);
if (nearNode == null) {
if (txPrepareMsgLog.isDebugEnabled()) {
txPrepareMsgLog.debug("Received near prepare from node that left grid (will ignore) [" + "txId=" + req.version() + ", node=" + nearNodeId + ']');
}
return null;
}
IgniteTxEntry firstEntry;
try {
IgniteTxEntry firstWrite = unmarshal(req.writes());
IgniteTxEntry firstRead = unmarshal(req.reads());
firstEntry = firstWrite != null ? firstWrite : firstRead;
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
assert firstEntry != null : req;
GridDhtTxLocal tx = null;
GridCacheVersion mappedVer = ctx.tm().mappedVersion(req.version());
if (mappedVer != null) {
tx = ctx.tm().tx(mappedVer);
if (tx == null)
U.warn(log, "Missing local transaction for mapped near version [nearVer=" + req.version() + ", mappedVer=" + mappedVer + ']');
else {
if (req.concurrency() == PESSIMISTIC)
tx.nearFutureId(req.futureId());
}
} else {
GridDhtPartitionTopology top = null;
if (req.firstClientRequest()) {
assert req.concurrency() == OPTIMISTIC : req;
assert CU.clientNode(nearNode) : nearNode;
top = firstEntry.context().topology();
top.readLock();
}
try {
if (top != null && needRemap(req.topologyVersion(), top.topologyVersion(), req)) {
if (txPrepareMsgLog.isDebugEnabled()) {
txPrepareMsgLog.debug("Topology version mismatch for near prepare, need remap transaction [" + "txId=" + req.version() + ", node=" + nearNodeId + ", reqTopVer=" + req.topologyVersion() + ", locTopVer=" + top.topologyVersion() + ", req=" + req + ']');
}
GridNearTxPrepareResponse res = new GridNearTxPrepareResponse(req.partition(), req.version(), req.futureId(), req.miniId(), req.version(), req.version(), null, null, top.topologyVersion(), req.deployInfo() != null);
try {
ctx.io().send(nearNodeId, res, req.policy());
if (txPrepareMsgLog.isDebugEnabled()) {
txPrepareMsgLog.debug("Sent remap response for near prepare [txId=" + req.version() + ", node=" + nearNodeId + ']');
}
} catch (ClusterTopologyCheckedException ignored) {
if (txPrepareMsgLog.isDebugEnabled()) {
txPrepareMsgLog.debug("Failed to send remap response for near prepare, node failed [" + "txId=" + req.version() + ", node=" + nearNodeId + ']');
}
} catch (IgniteCheckedException e) {
U.error(txPrepareMsgLog, "Failed to send remap response for near prepare " + "[txId=" + req.version() + ", node=" + nearNodeId + ", req=" + req + ']', e);
}
return new GridFinishedFuture<>(res);
}
tx = new GridDhtTxLocal(ctx, req.topologyVersion(), nearNode.id(), req.version(), req.futureId(), req.miniId(), req.threadId(), req.implicitSingle(), req.implicitSingle(), req.system(), req.explicitLock(), req.policy(), req.concurrency(), req.isolation(), req.timeout(), req.isInvalidate(), true, req.onePhaseCommit(), req.txSize(), req.transactionNodes(), req.subjectId(), req.taskNameHash());
tx = ctx.tm().onCreated(null, tx);
if (tx != null)
tx.topologyVersion(req.topologyVersion());
else
U.warn(log, "Failed to create local transaction (was transaction rolled back?) [xid=" + req.version() + ", req=" + req + ']');
} finally {
if (tx != null)
req.txState(tx.txState());
if (top != null)
top.readUnlock();
}
}
if (tx != null) {
req.txState(tx.txState());
if (req.explicitLock())
tx.explicitLock(true);
tx.transactionNodes(req.transactionNodes());
if (req.near())
tx.nearOnOriginatingNode(true);
if (req.onePhaseCommit()) {
assert req.last() : req;
tx.onePhaseCommit(true);
}
if (req.needReturnValue())
tx.needReturnValue(true);
IgniteInternalFuture<GridNearTxPrepareResponse> fut = tx.prepareAsync(req.reads(), req.writes(), req.dhtVersions(), req.messageId(), req.miniId(), req.transactionNodes(), req.last());
if (tx.isRollbackOnly() && !tx.commitOnPrepare()) {
if (tx.state() != TransactionState.ROLLED_BACK && tx.state() != TransactionState.ROLLING_BACK)
tx.rollbackDhtLocalAsync();
}
final GridDhtTxLocal tx0 = tx;
fut.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> txFut) {
try {
txFut.get();
} catch (IgniteCheckedException e) {
// Just in case.
tx0.setRollbackOnly();
if (!X.hasCause(e, IgniteTxOptimisticCheckedException.class) && !X.hasCause(e, IgniteFutureCancelledException.class) && !ctx.kernalContext().isStopping())
U.error(log, "Failed to prepare DHT transaction: " + tx0, e);
}
}
});
return fut;
} else
return new GridFinishedFuture<>((GridNearTxPrepareResponse) null);
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridTopologyCommandHandler method handleAsync.
/** {@inheritDoc} */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
assert req instanceof GridRestTopologyRequest : "Invalid command for topology handler: " + req;
assert SUPPORTED_COMMANDS.contains(req.command());
if (log.isDebugEnabled())
log.debug("Handling topology REST request: " + req);
GridRestTopologyRequest req0 = (GridRestTopologyRequest) req;
GridRestResponse res = new GridRestResponse();
boolean mtr = req0.includeMetrics();
boolean attr = req0.includeAttributes();
switch(req.command()) {
case TOPOLOGY:
{
Collection<ClusterNode> allNodes = F.concat(false, ctx.discovery().allNodes(), ctx.discovery().daemonNodes());
Collection<GridClientNodeBean> top = new ArrayList<>(allNodes.size());
for (ClusterNode node : allNodes) top.add(createNodeBean(node, mtr, attr));
res.setResponse(top);
break;
}
case NODE:
{
UUID id = req0.nodeId();
final String ip = req0.nodeIp();
if (id == null && ip == null)
return new GridFinishedFuture<>(new IgniteCheckedException("Failed to handle request (either id or ip should be specified)."));
ClusterNode node;
if (id != null) {
// Always refresh topology so client see most up-to-date view.
ctx.discovery().alive(id);
node = ctx.grid().cluster().node(id);
if (ip != null && node != null && !containsIp(node.addresses(), ip))
node = null;
} else
node = F.find(ctx.discovery().allNodes(), null, new P1<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
return containsIp(n.addresses(), ip);
}
});
if (node != null)
res.setResponse(createNodeBean(node, mtr, attr));
else
res.setResponse(null);
break;
}
default:
assert false : "Invalid command for topology handler: " + req;
}
if (log.isDebugEnabled())
log.debug("Handled topology REST request [res=" + res + ", req=" + req + ']');
return new GridFinishedFuture<>(res);
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridLogCommandHandler method handleAsync.
/** {@inheritDoc} */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
assert req != null;
if (req.command() == LOG) {
if (log.isDebugEnabled())
log.debug("Handling log REST request: " + req);
GridRestLogRequest req0 = (GridRestLogRequest) req;
if (req0.from() < -1 || req0.to() < -1)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "One of the request parameters is invalid [from=" + req0.from() + ", to=" + req0.to() + ']'));
int from;
if (req0.from() != -1) {
if (req0.to() == -1)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'to' is not set."));
from = req0.from();
} else
from = DEFAULT_FROM;
int to;
if (req0.to() != -1) {
if (req0.from() == -1)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'from' is not set."));
to = req0.to();
} else
to = DEFAULT_TO;
if (from >= to)
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'from' must be less than 'to'."));
File logFile;
try {
if (req0.path() != null) {
if (log.fileName() != null) {
if (!req0.path().equals(log.fileName())) {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'path' must contain a path to valid log file."));
} else
logFile = new File(req0.path());
} else if (req0.path().startsWith(ctx.config().getIgniteHome()))
logFile = new File(req0.path());
else {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Request parameter 'path' must contain a path to valid log file."));
}
} else if (log.fileName() == null)
logFile = new File(ctx.config().getIgniteHome() + "/work/log/ignite.log");
else
logFile = new File(log.fileName());
} catch (InvalidPathException e) {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, "Incorrect path to a log file [msg=" + e.getMessage() + ']'));
}
try {
String content = readLog(from, to, logFile);
return new GridFinishedFuture<>(new GridRestResponse(content));
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage()));
}
}
return new GridFinishedFuture<>();
}
use of org.apache.ignite.internal.util.future.GridFinishedFuture in project ignite by apache.
the class GridContinuousProcessor method startRoutine.
/**
* @param hnd Handler.
* @param bufSize Buffer size.
* @param interval Time interval.
* @param autoUnsubscribe Automatic unsubscribe flag.
* @param locOnly Local only flag.
* @param prjPred Projection predicate.
* @return Future.
*/
@SuppressWarnings("TooBroadScope")
public IgniteInternalFuture<UUID> startRoutine(GridContinuousHandler hnd, boolean locOnly, int bufSize, long interval, boolean autoUnsubscribe, @Nullable IgnitePredicate<ClusterNode> prjPred) {
assert hnd != null;
assert bufSize > 0;
assert interval >= 0;
// Generate ID.
final UUID routineId = UUID.randomUUID();
// Register routine locally.
locInfos.put(routineId, new LocalRoutineInfo(prjPred, hnd, bufSize, interval, autoUnsubscribe));
if (locOnly) {
try {
registerHandler(ctx.localNodeId(), routineId, hnd, bufSize, interval, autoUnsubscribe, true);
return new GridFinishedFuture<>(routineId);
} catch (IgniteCheckedException e) {
unregisterHandler(routineId, hnd, true);
return new GridFinishedFuture<>(e);
}
}
// Whether local node is included in routine.
boolean locIncluded = prjPred == null || prjPred.apply(ctx.discovery().localNode());
StartRequestData reqData = new StartRequestData(prjPred, hnd.clone(), bufSize, interval, autoUnsubscribe);
try {
if (ctx.config().isPeerClassLoadingEnabled()) {
// Handle peer deployment for projection predicate.
if (prjPred != null && !U.isGrid(prjPred.getClass())) {
Class cls = U.detectClass(prjPred);
String clsName = cls.getName();
GridDeployment dep = ctx.deploy().deploy(cls, U.detectClassLoader(cls));
if (dep == null)
throw new IgniteDeploymentCheckedException("Failed to deploy projection predicate: " + prjPred);
reqData.className(clsName);
reqData.deploymentInfo(new GridDeploymentInfoBean(dep));
reqData.p2pMarshal(marsh);
}
// Handle peer deployment for other handler-specific objects.
reqData.handler().p2pMarshal(ctx);
}
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
// Register per-routine notifications listener if ordered messaging is used.
registerMessageListener(hnd);
StartFuture fut = new StartFuture(ctx, routineId);
startFuts.put(routineId, fut);
try {
if (locIncluded || hnd.isQuery())
registerHandler(ctx.localNodeId(), routineId, hnd, bufSize, interval, autoUnsubscribe, true);
ctx.discovery().sendCustomEvent(new StartRoutineDiscoveryMessage(routineId, reqData, reqData.handler().keepBinary()));
} catch (IgniteCheckedException e) {
startFuts.remove(routineId);
locInfos.remove(routineId);
unregisterHandler(routineId, hnd, true);
fut.onDone(e);
return fut;
}
// Handler is registered locally.
fut.onLocalRegistered();
return fut;
}
Aggregations