use of org.apache.ignite.internal.util.GridLeanSet in project ignite by apache.
the class GridCacheIoManager method safeSend.
/**
* Sends message and automatically accounts for lefts nodes.
*
* @param nodes Nodes to send to.
* @param msg Message to send.
* @param plc IO policy.
* @param fallback Callback for failed nodes.
* @throws IgniteCheckedException If send failed.
*/
@SuppressWarnings({ "BusyWait", "unchecked" })
public void safeSend(Collection<? extends ClusterNode> nodes, GridCacheMessage msg, byte plc, @Nullable IgnitePredicate<ClusterNode> fallback) throws IgniteCheckedException {
assert nodes != null;
assert msg != null;
if (nodes.isEmpty()) {
if (log.isDebugEnabled())
log.debug("Message will not be sent as collection of nodes is empty: " + msg);
return;
}
if (!onSend(msg, null))
return;
if (log.isDebugEnabled())
log.debug("Sending cache message [msg=" + msg + ", nodes=" + U.toShortString(nodes) + ']');
final Collection<UUID> leftIds = new GridLeanSet<>();
int cnt = 0;
while (cnt < retryCnt) {
try {
Collection<? extends ClusterNode> nodesView = F.view(nodes, new P1<ClusterNode>() {
@Override
public boolean apply(ClusterNode e) {
return !leftIds.contains(e.id());
}
});
cctx.gridIO().sendToGridTopic(nodesView, TOPIC_CACHE, msg, plc);
boolean added = false;
// ignored the message during stopping.
for (ClusterNode n : nodes) {
if (!leftIds.contains(n.id()) && !cctx.discovery().alive(n.id())) {
leftIds.add(n.id());
if (fallback != null && !fallback.apply(n))
// If fallback signalled to stop.
return;
added = true;
}
}
if (added) {
if (!F.exist(F.nodeIds(nodes), F0.not(F.contains(leftIds)))) {
if (log.isDebugEnabled())
log.debug("Message will not be sent because all nodes left topology [msg=" + msg + ", nodes=" + U.toShortString(nodes) + ']');
return;
}
}
break;
} catch (IgniteCheckedException e) {
boolean added = false;
for (ClusterNode n : nodes) {
if (!leftIds.contains(n.id()) && (!cctx.discovery().alive(n.id()) || !cctx.discovery().pingNode(n.id()))) {
leftIds.add(n.id());
if (fallback != null && !fallback.apply(n))
// If fallback signalled to stop.
return;
added = true;
}
}
if (!added) {
cnt++;
if (cnt == retryCnt)
throw e;
U.sleep(retryDelay);
}
if (!F.exist(F.nodeIds(nodes), F0.not(F.contains(leftIds)))) {
if (log.isDebugEnabled())
log.debug("Message will not be sent because all nodes left topology [msg=" + msg + ", nodes=" + U.toShortString(nodes) + ']');
return;
}
if (log.isDebugEnabled())
log.debug("Message send will be retried [msg=" + msg + ", nodes=" + U.toShortString(nodes) + ", leftIds=" + leftIds + ']');
}
}
if (log.isDebugEnabled())
log.debug("Sent cache message [msg=" + msg + ", nodes=" + U.toShortString(nodes) + ']');
}
use of org.apache.ignite.internal.util.GridLeanSet in project ignite by apache.
the class GridDhtTxLocalAdapter method lockAllAsync.
/**
* @param cacheCtx Cache context.
* @param entries Entries to lock.
* @param msgId Message ID.
* @param read Read flag.
* @param createTtl TTL for create operation.
* @param accessTtl TTL for read operation.
* @param needRetVal Return value flag.
* @param skipStore Skip store flag.
* @return Lock future.
*/
@SuppressWarnings("ForLoopReplaceableByForEach")
IgniteInternalFuture<GridCacheReturn> lockAllAsync(GridCacheContext cacheCtx, List<GridCacheEntryEx> entries, long msgId, final boolean read, final boolean needRetVal, long createTtl, long accessTtl, boolean skipStore, boolean keepBinary) {
try {
checkValid();
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
final GridCacheReturn ret = new GridCacheReturn(localResult(), false);
if (F.isEmpty(entries))
return new GridFinishedFuture<>(ret);
init();
onePhaseCommit(onePhaseCommit);
try {
Set<KeyCacheObject> skipped = null;
AffinityTopologyVersion topVer = topologyVersion();
GridDhtCacheAdapter dhtCache = cacheCtx.isNear() ? cacheCtx.near().dht() : cacheCtx.dht();
// Enlist locks into transaction.
for (int i = 0; i < entries.size(); i++) {
GridCacheEntryEx entry = entries.get(i);
KeyCacheObject key = entry.key();
IgniteTxEntry txEntry = entry(entry.txKey());
// First time access.
if (txEntry == null) {
GridDhtCacheEntry cached;
while (true) {
try {
cached = dhtCache.entryExx(key, topVer);
cached.unswap(read);
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Get removed entry: " + key);
}
}
addActiveCache(dhtCache.context(), false);
txEntry = addEntry(NOOP, null, null, null, cached, null, CU.empty0(), false, -1L, -1L, null, skipStore, keepBinary);
if (read)
txEntry.ttl(accessTtl);
txEntry.cached(cached);
addReader(msgId, cached, txEntry, topVer);
} else {
if (skipped == null)
skipped = new GridLeanSet<>();
skipped.add(key);
}
}
assert pessimistic();
Collection<KeyCacheObject> keys = F.viewReadOnly(entries, CU.entry2Key());
// Acquire locks only after having added operation to the write set.
// Otherwise, during rollback we will not know whether locks need
// to be rolled back.
// Loose all skipped and previously locked (we cannot reenter locks here).
final Collection<KeyCacheObject> passedKeys = skipped != null ? F.view(keys, F0.notIn(skipped)) : keys;
if (log.isDebugEnabled())
log.debug("Lock keys: " + passedKeys);
return obtainLockAsync(cacheCtx, ret, passedKeys, read, needRetVal, createTtl, accessTtl, skipStore, keepBinary);
} catch (IgniteCheckedException e) {
setRollbackOnly();
return new GridFinishedFuture<>(e);
}
}
use of org.apache.ignite.internal.util.GridLeanSet in project ignite by apache.
the class GridDeployment method annotatedValue.
/**
* @param target Object to find a value in.
* @param annCls Annotation class.
* @param visited Set of visited objects to avoid cycling.
* @param annFound Flag indicating if value has already been found.
* @return Value of annotated field or method.
* @throws IgniteCheckedException If failed to find.
*/
private IgniteBiTuple<Object, Boolean> annotatedValue(Object target, Class<? extends Annotation> annCls, @Nullable Set<Object> visited, boolean annFound) throws IgniteCheckedException {
assert target != null;
// To avoid infinite recursion.
if (visited != null && visited.contains(target))
return F.t(null, annFound);
Object val = null;
for (Class<?> cls = target.getClass(); !cls.equals(Object.class); cls = cls.getSuperclass()) {
// Fields.
for (Field f : fieldsWithAnnotation(cls, annCls)) {
f.setAccessible(true);
Object fieldVal;
try {
fieldVal = f.get(target);
} catch (IllegalAccessException e) {
throw new IgniteCheckedException("Failed to get annotated field value [cls=" + cls.getName() + ", ann=" + annCls.getSimpleName(), e);
}
if (needsRecursion(f)) {
if (fieldVal != null) {
if (visited == null)
visited = new GridLeanSet<>();
visited.add(target);
// Recursion.
IgniteBiTuple<Object, Boolean> tup = annotatedValue(fieldVal, annCls, visited, annFound);
if (!annFound && tup.get2())
// Update value only if annotation was found in recursive call.
val = tup.get1();
annFound = tup.get2();
}
} else {
if (annFound)
throw new IgniteCheckedException("Multiple annotations have been found [cls=" + cls.getName() + ", ann=" + annCls.getSimpleName() + "]");
val = fieldVal;
annFound = true;
}
}
// Methods.
for (Method m : methodsWithAnnotation(cls, annCls)) {
if (annFound)
throw new IgniteCheckedException("Multiple annotations have been found [cls=" + cls.getName() + ", ann=" + annCls.getSimpleName() + "]");
m.setAccessible(true);
try {
val = m.invoke(target);
} catch (Exception e) {
throw new IgniteCheckedException("Failed to get annotated method value [cls=" + cls.getName() + ", ann=" + annCls.getSimpleName(), e);
}
annFound = true;
}
}
return F.t(val, annFound);
}
use of org.apache.ignite.internal.util.GridLeanSet in project ignite by apache.
the class GridDhtTxMapping method addMapping.
/**
* Adds information about next mapping.
*
* @param nodes Nodes.
*/
@SuppressWarnings("ConstantConditions")
public void addMapping(List<ClusterNode> nodes) {
assert !F.isEmpty(nodes) : nodes;
ClusterNode primary = nodes.get(0);
int size = nodes.size();
if (size > 1) {
Collection<UUID> backups = txNodes.get(primary.id());
if (backups == null) {
backups = U.newHashSet(size - 1);
txNodes.put(primary.id(), backups);
}
for (int i = 1; i < size; i++) backups.add(nodes.get(i).id());
} else
txNodes.put(primary.id(), new GridLeanSet<UUID>());
}
Aggregations