Search in sources :

Example 1 with IgniteDiscoverySpiInternalListener

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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteDiscoverySpiInternalListener(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) DiscoveryLocalJoinData(org.apache.ignite.internal.managers.discovery.DiscoveryLocalJoinData) T2(org.apache.ignite.internal.util.typedef.T2) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) ZookeeperDiscoverySpi(org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecurityCredentials(org.apache.ignite.plugin.security.SecurityCredentials) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) ZookeeperDiscoverySpi(org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteLogger(org.apache.ignite.IgniteLogger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with IgniteDiscoverySpiInternalListener

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);
}
Also used : IgniteDiscoverySpiInternalListener(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener)

Example 3 with IgniteDiscoverySpiInternalListener

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);
}
Also used : IgniteDiscoverySpiInternalListener(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener)

Example 4 with IgniteDiscoverySpiInternalListener

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();
    }
}
Also used : IgniteDiscoverySpiInternalListener(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener) DiscoveryDataBag(org.apache.ignite.spi.discovery.DiscoveryDataBag) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) DataFormatException(java.util.zip.DataFormatException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

IgniteDiscoverySpiInternalListener (org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpiInternalListener)4 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DataFormatException (java.util.zip.DataFormatException)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)1 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 Event (org.apache.ignite.events.Event)1