use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener in project ignite by apache.
the class ZookeeperDiscoverySpiTestBase method getConfiguration.
/**
* {@inheritDoc}
*/
@Override
protected IgniteConfiguration getConfiguration(final String igniteInstanceName) throws Exception {
if (testSockNio)
System.setProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET, ZkTestClientCnxnSocketNIO.class.getName());
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
if (nodeId != null)
cfg.setNodeId(nodeId);
if (!dfltConsistenId)
cfg.setConsistentId(igniteInstanceName);
ZookeeperDiscoverySpi zkSpi = new ZookeeperDiscoverySpi();
if (joinTimeout != 0)
zkSpi.setJoinTimeout(joinTimeout);
zkSpi.setSessionTimeout(sesTimeout > 0 ? sesTimeout : 10_000);
zkSpi.setClientReconnectDisabled(clientReconnectDisabled);
// Set authenticator for basic sanity tests.
if (auth != null) {
zkSpi.setAuthenticator(auth.apply());
zkSpi.setInternalListener(new IgniteDiscoverySpiInternalListener() {
@Override
public void beforeJoin(ClusterNode locNode, IgniteLogger log) {
ZookeeperClusterNode locNode0 = (ZookeeperClusterNode) locNode;
Map<String, Object> attrs = new HashMap<>(locNode0.getAttributes());
attrs.put(ATTR_SECURITY_CREDENTIALS, new SecurityCredentials(null, null, igniteInstanceName));
locNode0.setAttributes(attrs);
}
@Override
public boolean beforeSendCustomEvent(DiscoverySpi spi, IgniteLogger log, DiscoverySpiCustomMessage msg) {
return false;
}
});
}
spis.put(igniteInstanceName, zkSpi);
if (USE_TEST_CLUSTER) {
assert zkCluster != null;
zkSpi.setZkConnectionString(getTestClusterZkConnectionString());
if (zkRootPath != null)
zkSpi.setZkRootPath(zkRootPath);
} else
zkSpi.setZkConnectionString(getRealClusterZkConnectionString());
cfg.setDiscoverySpi(zkSpi);
cfg.setCacheConfiguration(getCacheConfiguration());
if (userAttrs != null)
cfg.setUserAttributes(userAttrs);
Map<IgnitePredicate<? extends Event>, int[]> lsnrs = new HashMap<>();
if (cfg.isClientMode()) {
UUID currNodeId = cfg.getNodeId();
lsnrs.put(new IgnitePredicate<Event>() {
/**
* Last remembered uuid before node reconnected.
*/
private UUID nodeId = currNodeId;
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
evts.remove(nodeId);
nodeId = evt.node().id();
}
return true;
}
}, new int[] { EVT_CLIENT_NODE_RECONNECTED });
}
lsnrs.put(new IgnitePredicate<Event>() {
/**
*/
@IgniteInstanceResource
private Ignite ignite;
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
@Override
public boolean apply(Event evt) {
try {
DiscoveryEvent discoveryEvt = (DiscoveryEvent) evt;
UUID locId = ((IgniteKernal) ignite).context().localNodeId();
Map<T2<Integer, Long>, DiscoveryEvent> nodeEvts = evts.get(locId);
if (nodeEvts == null) {
Object old = evts.put(locId, nodeEvts = new LinkedHashMap<>());
assertNull(old);
// If the current node has failed, the local join will never happened.
if (evt.type() != EVT_NODE_FAILED || discoveryEvt.eventNode().consistentId().equals(ignite.configuration().getConsistentId())) {
synchronized (nodeEvts) {
DiscoveryLocalJoinData locJoin = ((IgniteEx) ignite).context().discovery().localJoin();
if (locJoin.event().node().order() == 1)
clusterNum.incrementAndGet();
nodeEvts.put(new T2<>(clusterNum.get(), locJoin.event().topologyVersion()), locJoin.event());
}
}
}
synchronized (nodeEvts) {
DiscoveryEvent old = nodeEvts.put(new T2<>(clusterNum.get(), discoveryEvt.topologyVersion()), discoveryEvt);
assertNull(old);
}
} catch (Throwable e) {
error("Unexpected error [evt=" + evt + ", err=" + e + ']', e);
err = true;
}
return true;
}
}, new int[] { EVT_NODE_JOINED, EVT_NODE_FAILED, EVT_NODE_LEFT });
if (!isMultiJvm())
cfg.setLocalEventListeners(lsnrs);
if (persistence) {
DataStorageConfiguration memCfg = new DataStorageConfiguration().setDefaultDataRegionConfiguration(new DataRegionConfiguration().setMaxSize(100 * 1024 * 1024).setPersistenceEnabled(true)).setPageSize(1024).setWalMode(WALMode.LOG_ONLY);
cfg.setDataStorageConfiguration(memCfg);
}
if (testCommSpi)
cfg.setCommunicationSpi(new ZkTestCommunicationSpi());
if (failCommSpi)
cfg.setCommunicationSpi(new PeerToPeerCommunicationFailureSpi());
if (blockCommSpi) {
cfg.setCommunicationSpi(new TcpBlockCommunicationSpi(igniteInstanceName.contains("block")).setUsePairedConnections(true));
cfg.setNetworkTimeout(500);
}
if (commFailureRslvr != null)
cfg.setCommunicationFailureResolver(commFailureRslvr.apply());
cfg.setIncludeEventTypes(EventType.EVTS_ALL);
return cfg;
}
use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener in project ignite by apache.
the class TcpDiscoverySpi method sendCustomEvent.
/**
* {@inheritDoc}
*/
@Override
public void sendCustomEvent(DiscoverySpiCustomMessage msg) throws IgniteException {
IgniteDiscoverySpiInternalListener internalLsnr = this.internalLsnr;
if (internalLsnr != null) {
if (!internalLsnr.beforeSendCustomEvent(this, log, msg))
return;
}
impl.sendCustomEvent(msg);
}
use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener in project ignite by apache.
the class ZookeeperDiscoverySpi method sendCustomEvent.
/**
* {@inheritDoc}
*/
@Override
public void sendCustomEvent(DiscoverySpiCustomMessage msg) {
IgniteDiscoverySpiInternalListener internalLsnr = impl.internalLsnr;
if (internalLsnr != null) {
if (!internalLsnr.beforeSendCustomEvent(this, log, msg))
return;
}
impl.sendCustomMessage(msg);
}
use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener in project ignite by apache.
the class ZookeeperDiscoveryImpl method joinTopology.
/**
* @param prevState Previous state in case of connect retry.
* @throws InterruptedException If interrupted.
*/
private void joinTopology(@Nullable ZkRuntimeState prevState) throws InterruptedException {
if (!busyLock.enterBusy())
return;
try {
// Need fire EVT_CLIENT_NODE_RECONNECTED event if reconnect after already joined.
boolean reconnect = locNode.isClient() && prevState != null && (prevState.joined || prevState.reconnect);
IgniteDiscoverySpiInternalListener internalLsnr = this.internalLsnr;
if (internalLsnr != null)
internalLsnr.beforeJoin(locNode, log);
if (reconnect)
locNode.setAttributes(spi.getLocNodeAttrs());
marshalCredentialsOnJoin(locNode);
synchronized (stateMux) {
if (connState == ConnectionState.STOPPED)
return;
connState = ConnectionState.STARTED;
}
ZkRuntimeState rtState = this.rtState = new ZkRuntimeState(reconnect);
DiscoveryDataBag discoDataBag = new DiscoveryDataBag(locNode.id(), locNode.isClient());
exchange.collect(discoDataBag);
ZkJoiningNodeData joinData = new ZkJoiningNodeData(locNode, discoDataBag.joiningNodeData());
byte[] joinDataBytes;
try {
joinDataBytes = marshalZip(joinData);
} catch (Exception e) {
throw new IgniteSpiException("Failed to marshal joining node data", e);
}
try {
rtState.zkClient = new ZookeeperClient(igniteInstanceName, log, connectString, sesTimeout, new ConnectionLossListener());
} catch (Exception e) {
throw new IgniteSpiException("Failed to create Zookeeper client", e);
}
startJoin(rtState, prevState, joinDataBytes);
} finally {
busyLock.leaveBusy();
}
}
Aggregations