use of org.apache.ignite.events.Event in project ignite by apache.
the class GridManagerAdapter method onKernalStart.
/**
* {@inheritDoc}
*/
@Override
public final void onKernalStart(boolean active) throws IgniteCheckedException {
for (final IgniteSpi spi : spis) {
try {
spi.onContextInitialized(new IgniteSpiContext() {
@Override
public boolean isStopping() {
return ctx.isStopping();
}
@Override
public Collection<ClusterNode> remoteNodes() {
return ctx.discovery().remoteNodes();
}
@Override
public Collection<ClusterNode> nodes() {
return ctx.discovery().allNodes();
}
@Override
public ClusterNode localNode() {
return ctx.discovery().localNode();
}
@Override
public Collection<ClusterNode> remoteDaemonNodes() {
final Collection<ClusterNode> all = ctx.discovery().daemonNodes();
return !localNode().isDaemon() ? all : F.view(all, new IgnitePredicate<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
return n.isDaemon();
}
});
}
@Nullable
@Override
public ClusterNode node(UUID nodeId) {
A.notNull(nodeId, "nodeId");
return ctx.discovery().node(nodeId);
}
@Override
public boolean pingNode(UUID nodeId) {
A.notNull(nodeId, "nodeId");
try {
return ctx.discovery().pingNode(nodeId);
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public void send(ClusterNode node, Serializable msg, String topic) throws IgniteSpiException {
A.notNull(node, "node");
A.notNull(msg, "msg");
A.notNull(topic, "topic");
try {
if (msg instanceof Message)
ctx.io().sendToCustomTopic(node, topic, (Message) msg, SYSTEM_POOL);
else
ctx.io().sendUserMessage(Collections.singletonList(node), msg, topic, false, 0, false);
} catch (IgniteCheckedException e) {
throw unwrapException(e);
}
}
@Override
public void addLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
A.notNull(topic, "topic");
A.notNull(p, "p");
ctx.io().addUserMessageListener(topic, p);
}
@Override
public void removeLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
A.notNull(topic, "topic");
A.notNull(topic, "p");
ctx.io().removeUserMessageListener(topic, p);
}
@Override
public void addMessageListener(GridMessageListener lsnr, String topic) {
A.notNull(lsnr, "lsnr");
A.notNull(topic, "topic");
ctx.io().addMessageListener(topic, lsnr);
}
@Override
public boolean removeMessageListener(GridMessageListener lsnr, String topic) {
A.notNull(lsnr, "lsnr");
A.notNull(topic, "topic");
return ctx.io().removeMessageListener(topic, lsnr);
}
@Override
public void addLocalEventListener(GridLocalEventListener lsnr, int... types) {
A.notNull(lsnr, "lsnr");
ctx.event().addLocalEventListener(lsnr, types);
}
@Override
public boolean removeLocalEventListener(GridLocalEventListener lsnr) {
A.notNull(lsnr, "lsnr");
return ctx.event().removeLocalEventListener(lsnr);
}
@Override
public boolean isEventRecordable(int... types) {
for (int t : types) if (!ctx.event().isRecordable(t))
return false;
return true;
}
@Override
public void recordEvent(Event evt) {
A.notNull(evt, "evt");
if (ctx.event().isRecordable(evt.type()))
ctx.event().record(evt);
}
@Override
public void registerPort(int port, IgnitePortProtocol proto) {
ctx.ports().registerPort(port, proto, spi.getClass());
}
@Override
public void deregisterPort(int port, IgnitePortProtocol proto) {
ctx.ports().deregisterPort(port, proto, spi.getClass());
}
@Override
public void deregisterPorts() {
ctx.ports().deregisterPorts(spi.getClass());
}
@Nullable
@Override
public <K, V> V get(String cacheName, K key) {
return ctx.cache().<K, V>jcache(cacheName).get(key);
}
@Nullable
@Override
public <K, V> V put(String cacheName, K key, V val, long ttl) {
try {
if (ttl > 0) {
ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
return cache.getAndPut(key, val);
} else
return ctx.cache().<K, V>jcache(cacheName).getAndPut(key, val);
} catch (IgniteCheckedException e) {
throw CU.convertToCacheException(e);
}
}
@Nullable
@Override
public <K, V> V putIfAbsent(String cacheName, K key, V val, long ttl) {
try {
if (ttl > 0) {
ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
return cache.getAndPutIfAbsent(key, val);
} else
return ctx.cache().<K, V>jcache(cacheName).getAndPutIfAbsent(key, val);
} catch (IgniteCheckedException e) {
throw CU.convertToCacheException(e);
}
}
@Nullable
@Override
public <K, V> V remove(String cacheName, K key) {
return ctx.cache().<K, V>jcache(cacheName).getAndRemove(key);
}
@Override
public <K> boolean containsKey(String cacheName, K key) {
return ctx.cache().cache(cacheName).containsKey(key);
}
@Override
public int partition(String cacheName, Object key) {
return ctx.cache().cache(cacheName).affinity().partition(key);
}
@Override
public IgniteNodeValidationResult validateNode(ClusterNode node) {
for (GridComponent comp : ctx) {
IgniteNodeValidationResult err = comp.validateNode(node);
if (err != null)
return err;
}
return null;
}
@Nullable
@Override
public IgniteNodeValidationResult validateNode(ClusterNode node, DiscoveryDataBag discoData) {
for (GridComponent comp : ctx) {
if (comp.discoveryDataType() == null)
continue;
IgniteNodeValidationResult err = comp.validateNode(node, discoData.newJoinerDiscoveryData(comp.discoveryDataType().ordinal()));
if (err != null)
return err;
}
return null;
}
@Override
public Collection<SecuritySubject> authenticatedSubjects() {
try {
return ctx.security().authenticatedSubjects();
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public SecuritySubject authenticatedSubject(UUID subjId) {
try {
return ctx.security().authenticatedSubject(subjId);
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public MessageFormatter messageFormatter() {
return ctx.io().formatter();
}
@Override
public MessageFactory messageFactory() {
return ctx.io().messageFactory();
}
@Override
public boolean tryFailNode(UUID nodeId, @Nullable String warning) {
return ctx.discovery().tryFailNode(nodeId, warning);
}
@Override
public void failNode(UUID nodeId, @Nullable String warning) {
ctx.discovery().failNode(nodeId, warning);
}
@Override
public void addTimeoutObject(IgniteSpiTimeoutObject obj) {
ctx.timeout().addTimeoutObject(new GridSpiTimeoutObject(obj));
}
@Override
public void removeTimeoutObject(IgniteSpiTimeoutObject obj) {
ctx.timeout().removeTimeoutObject(new GridSpiTimeoutObject(obj));
}
@Override
public Map<String, Object> nodeAttributes() {
return ctx.nodeAttributes();
}
@Override
public boolean communicationFailureResolveSupported() {
return ctx.discovery().communicationErrorResolveSupported();
}
@Override
public void resolveCommunicationFailure(ClusterNode node, Exception err) {
ctx.discovery().resolveCommunicationError(node, err);
}
@Override
public ReadOnlyMetricRegistry getOrCreateMetricRegistry(String name) {
return ctx.metric().registry(name);
}
@Override
public void removeMetricRegistry(String name) {
ctx.metric().remove(name);
}
@Override
public Iterable<ReadOnlyMetricRegistry> metricRegistries() {
return ctx.metric();
}
@Override
public void addMetricRegistryCreationListener(Consumer<ReadOnlyMetricRegistry> lsnr) {
ctx.metric().addMetricRegistryCreationListener(lsnr);
}
/**
* @param e Exception to handle.
* @return GridSpiException Converted exception.
*/
private IgniteSpiException unwrapException(IgniteCheckedException e) {
// Avoid double-wrapping.
if (e.getCause() instanceof IgniteSpiException)
return (IgniteSpiException) e.getCause();
return new IgniteSpiException("Failed to execute SPI context method.", e);
}
});
} catch (IgniteSpiException e) {
throw new IgniteCheckedException("Failed to initialize SPI context.", e);
}
}
onKernalStart0();
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class ExchangeDiscoveryEvents method warnNoAffinityNodes.
/**
* @param cctx Context.
*/
public void warnNoAffinityNodes(GridCacheSharedContext<?, ?> cctx) {
List<String> cachesWithoutNodes = null;
for (DynamicCacheDescriptor cacheDesc : cctx.cache().cacheDescriptors().values()) {
if (discoCache.cacheGroupAffinityNodes(cacheDesc.groupId()).isEmpty()) {
if (cachesWithoutNodes == null)
cachesWithoutNodes = new ArrayList<>();
cachesWithoutNodes.add(cacheDesc.cacheName());
// Fire event even if there is no client cache started.
if (cctx.gridEvents().isRecordable(EventType.EVT_CACHE_NODES_LEFT)) {
Event evt = new CacheEvent(cacheDesc.cacheName(), cctx.localNode(), cctx.localNode(), "All server nodes have left the cluster.", EventType.EVT_CACHE_NODES_LEFT, 0, false, null, null, null, null, null, false, null, false, null, null, null);
cctx.gridEvents().record(evt);
}
}
}
if (cachesWithoutNodes != null) {
StringBuilder sb = new StringBuilder("All server nodes for the following caches have left the cluster: ");
for (int i = 0; i < cachesWithoutNodes.size(); i++) {
String cache = cachesWithoutNodes.get(i);
sb.append('\'').append(cache).append('\'');
if (i != cachesWithoutNodes.size() - 1)
sb.append(", ");
}
IgniteLogger log = cctx.logger(getClass());
U.quietAndWarn(log, sb.toString());
U.quietAndWarn(log, "Must have server nodes for caches to operate.");
}
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridCacheQueryManager method start0.
/**
* {@inheritDoc}
*/
@Override
public void start0() throws IgniteCheckedException {
CacheConfiguration ccfg = cctx.config();
qryProcEnabled = QueryUtils.isEnabled(ccfg);
qryProc = cctx.kernalContext().query();
cacheName = cctx.name();
enabled = qryProcEnabled || (isIndexingSpiEnabled() && !CU.isSystemCache(cacheName));
maxIterCnt = ccfg.getMaxQueryIteratorsCount();
detailMetricsSz = ccfg.getQueryDetailMetricsSize();
if (detailMetricsSz > 0)
detailMetrics = new ConcurrentHashMap<>(detailMetricsSz);
lsnr = new GridLocalEventListener() {
@Override
public void onEvent(Event evt) {
UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
Map<Long, GridFutureAdapter<QueryResult<K, V>>> futs = qryIters.remove(nodeId);
if (futs != null) {
for (Map.Entry<Long, GridFutureAdapter<QueryResult<K, V>>> entry : futs.entrySet()) {
final Object rcpt = recipient(nodeId, entry.getKey());
entry.getValue().listen(new CIX1<IgniteInternalFuture<QueryResult<K, V>>>() {
@Override
public void applyx(IgniteInternalFuture<QueryResult<K, V>> f) throws IgniteCheckedException {
f.get().closeIfNotShared(rcpt);
}
});
}
}
Map<Long, GridFutureAdapter<FieldsResult>> fieldsFuts = fieldsQryRes.remove(nodeId);
if (fieldsFuts != null) {
for (Map.Entry<Long, GridFutureAdapter<FieldsResult>> entry : fieldsFuts.entrySet()) {
final Object rcpt = recipient(nodeId, entry.getKey());
entry.getValue().listen(new CIX1<IgniteInternalFuture<FieldsResult>>() {
@Override
public void applyx(IgniteInternalFuture<FieldsResult> f) throws IgniteCheckedException {
f.get().closeIfNotShared(rcpt);
}
});
}
}
}
};
metrics = new GridCacheQueryMetricsAdapter(cctx.kernalContext().metric(), cctx.name(), cctx.isNear());
cctx.events().addListener(lsnr, EVT_NODE_LEFT, EVT_NODE_FAILED);
qryTopVer = cctx.startTopologyVersion();
assert qryTopVer != null : cctx.name();
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridCacheDistributedQueryManager method start0.
/**
* {@inheritDoc}
*/
@Override
public void start0() throws IgniteCheckedException {
super.start0();
assert cctx.config().getCacheMode() != LOCAL;
cctx.io().addCacheHandler(cctx.cacheId(), GridCacheQueryRequest.class, new CI2<UUID, GridCacheQueryRequest>() {
@Override
public void apply(UUID nodeId, GridCacheQueryRequest req) {
processQueryRequest(nodeId, req);
}
});
lsnr = new GridLocalEventListener() {
@Override
public void onEvent(Event evt) {
DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
for (GridCacheDistributedQueryFuture fut : futs.values()) fut.onNodeLeft(discoEvt.eventNode().id());
}
};
cctx.events().addListener(lsnr, EVT_NODE_LEFT, EVT_NODE_FAILED);
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class ClusterProcessor method initDiagnosticListeners.
/**
* @throws IgniteCheckedException If failed.
*/
public void initDiagnosticListeners() throws IgniteCheckedException {
ctx.event().addLocalEventListener(new GridLocalEventListener() {
@Override
public void onEvent(Event evt) {
assert evt instanceof DiscoveryEvent;
assert evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT;
DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
UUID nodeId = discoEvt.eventNode().id();
ConcurrentHashMap<Long, InternalDiagnosticFuture> futs = diagnosticFutMap.get();
if (futs != null) {
for (InternalDiagnosticFuture fut : futs.values()) {
if (fut.nodeId.equals(nodeId))
fut.onDone(new IgniteDiagnosticInfo("Target node failed: " + nodeId));
}
}
allNodesMetrics.remove(nodeId);
}
}, EVT_NODE_FAILED, EVT_NODE_LEFT);
ctx.io().addMessageListener(TOPIC_INTERNAL_DIAGNOSTIC, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
if (msg instanceof IgniteDiagnosticMessage) {
IgniteDiagnosticMessage msg0 = (IgniteDiagnosticMessage) msg;
if (msg0.request()) {
ClusterNode node = ctx.discovery().node(nodeId);
if (node == null) {
if (diagnosticLog.isDebugEnabled()) {
diagnosticLog.debug("Skip diagnostic request, sender node left " + "[node=" + nodeId + ", msg=" + msg + ']');
}
return;
}
byte[] diagRes;
IgniteClosure<GridKernalContext, IgniteDiagnosticInfo> c;
try {
c = msg0.unmarshal(marsh);
diagRes = marsh.marshal(c.apply(ctx));
} catch (Exception e) {
U.error(diagnosticLog, "Failed to run diagnostic closure: " + e, e);
try {
IgniteDiagnosticInfo errInfo = new IgniteDiagnosticInfo("Failed to run diagnostic closure: " + e);
diagRes = marsh.marshal(errInfo);
} catch (Exception e0) {
U.error(diagnosticLog, "Failed to marshal diagnostic closure result: " + e, e);
diagRes = null;
}
}
IgniteDiagnosticMessage res = IgniteDiagnosticMessage.createResponse(diagRes, msg0.futureId());
try {
ctx.io().sendToGridTopic(node, TOPIC_INTERNAL_DIAGNOSTIC, res, GridIoPolicy.SYSTEM_POOL);
} catch (ClusterTopologyCheckedException e) {
if (diagnosticLog.isDebugEnabled()) {
diagnosticLog.debug("Failed to send diagnostic response, node left " + "[node=" + nodeId + ", msg=" + msg + ']');
}
} catch (IgniteCheckedException e) {
U.error(diagnosticLog, "Failed to send diagnostic response [msg=" + msg0 + "]", e);
}
} else {
InternalDiagnosticFuture fut = diagnosticFuturesMap().get(msg0.futureId());
if (fut != null) {
IgniteDiagnosticInfo res;
try {
res = msg0.unmarshal(marsh);
if (res == null)
res = new IgniteDiagnosticInfo("Remote node failed to marshal response.");
} catch (Exception e) {
U.error(diagnosticLog, "Failed to unmarshal diagnostic response: " + e, e);
res = new IgniteDiagnosticInfo("Failed to unmarshal diagnostic response: " + e);
}
fut.onResponse(res);
} else
U.warn(diagnosticLog, "Failed to find diagnostic message future [msg=" + msg0 + ']');
}
} else
U.warn(diagnosticLog, "Received unexpected message: " + msg);
}
});
if (sndMetrics) {
ctx.io().addMessageListener(TOPIC_METRICS, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
if (msg instanceof ClusterMetricsUpdateMessage)
processMetricsUpdateMessage(nodeId, (ClusterMetricsUpdateMessage) msg);
else
U.warn(log, "Received unexpected message for TOPIC_METRICS: " + msg);
}
});
}
}
Aggregations