Search in sources :

Example 1 with ZookeeperDiscoverySpi

use of org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi in project ignite by apache.

the class ZookeeperDiscovery method ZookeeperDiscoveryConfigurationExample.

void ZookeeperDiscoveryConfigurationExample() {
    // tag::cfg[]
    ZookeeperDiscoverySpi zkDiscoverySpi = new ZookeeperDiscoverySpi();
    zkDiscoverySpi.setZkConnectionString("127.0.0.1:34076,127.0.0.1:43310,127.0.0.1:36745");
    zkDiscoverySpi.setSessionTimeout(30_000);
    zkDiscoverySpi.setZkRootPath("/ignite");
    zkDiscoverySpi.setJoinTimeout(10_000);
    IgniteConfiguration cfg = new IgniteConfiguration();
    // Override default discovery SPI.
    cfg.setDiscoverySpi(zkDiscoverySpi);
    // Start the node.
    Ignite ignite = Ignition.start(cfg);
    // end::cfg[]
    ignite.close();
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ZookeeperDiscoverySpi(org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi) Ignite(org.apache.ignite.Ignite)

Example 2 with ZookeeperDiscoverySpi

use of org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi 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 3 with ZookeeperDiscoverySpi

use of org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi in project ignite by apache.

the class ZookeeperDiscoverySpiSaslAuthAbstractTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String instanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(instanceName);
    ZookeeperDiscoverySpi zkSpi = new ZookeeperDiscoverySpi();
    if (joinTimeout != 0)
        zkSpi.setJoinTimeout(joinTimeout);
    zkSpi.setSessionTimeout(sesTimeout > 0 ? sesTimeout : 10_000);
    zkSpi.setZkConnectionString(hostPort);
    cfg.setDiscoverySpi(zkSpi);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ZookeeperDiscoverySpi(org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi)

Example 4 with ZookeeperDiscoverySpi

use of org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi in project ignite by apache.

the class ZookeeperDiscoveryCommunicationFailureTest method communicationFailureResolve_KillNodes.

/**
 * @param startNodes Number of nodes to start.
 * @param killNodes Nodes to kill by resolve process.
 * @throws Exception If failed.
 */
private void communicationFailureResolve_KillNodes(int startNodes, Collection<Long> killNodes) throws Exception {
    testCommSpi = true;
    commFailureRslvr = TestNodeKillCommunicationFailureResolver.factory(killNodes);
    startGrids(startNodes);
    ZkTestCommunicationSpi commSpi = ZkTestCommunicationSpi.testSpi(ignite(0));
    commSpi.checkRes = new BitSet(startNodes);
    ZookeeperDiscoverySpi spi = null;
    UUID killNodeId = null;
    for (Ignite node : G.allGrids()) {
        ZookeeperDiscoverySpi spi0 = spi(node);
        if (!killNodes.contains(node.cluster().localNode().order()))
            spi = spi0;
        else
            killNodeId = node.cluster().localNode().id();
    }
    assertNotNull(spi);
    assertNotNull(killNodeId);
    try {
        spi.resolveCommunicationFailure(spi.getNode(killNodeId), new Exception("test"));
        fail("Exception is not thrown");
    } catch (IgniteSpiException e) {
        assertTrue("Unexpected exception: " + e, e.getCause() instanceof ClusterTopologyCheckedException);
    }
    int expNodes = startNodes - killNodes.size();
    waitForTopology(expNodes);
    for (Ignite node : G.allGrids()) assertFalse(killNodes.contains(node.cluster().localNode().order()));
    startGrid(startNodes);
    waitForTopology(expNodes + 1);
}
Also used : BitSet(java.util.BitSet) ZookeeperDiscoverySpi(org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi) Ignite(org.apache.ignite.Ignite) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) UUID(java.util.UUID) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 5 with ZookeeperDiscoverySpi

use of org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi in project ignite by apache.

the class ZookeeperDiscoveryCommunicationFailureTest method testNoOpCommunicationErrorResolve_3.

/**
 * Tests case when one node fails before sending communication status.
 *
 * @throws Exception If failed.
 */
@Test
public void testNoOpCommunicationErrorResolve_3() throws Exception {
    sesTimeout = 2000;
    commFailureRslvr = NoOpCommunicationFailureResolver.FACTORY;
    startGridsMultiThreaded(3);
    sesTimeout = 10_000;
    testSockNio = true;
    sesTimeout = 5000;
    startGrid(3);
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() {
            ZookeeperDiscoverySpi spi = spi(ignite(0));
            spi.resolveCommunicationFailure(ignite(1).cluster().localNode(), new Exception("test"));
            return null;
        }
    });
    U.sleep(1000);
    ZkTestClientCnxnSocketNIO nio = ZkTestClientCnxnSocketNIO.forNode(ignite(3));
    nio.closeSocket(true);
    try {
        stopGrid(3);
        fut.get();
    } finally {
        nio.allowConnect();
    }
    waitForTopology(3);
}
Also used : ZkTestClientCnxnSocketNIO(org.apache.zookeeper.ZkTestClientCnxnSocketNIO) ZookeeperDiscoverySpi(org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) Test(org.junit.Test)

Aggregations

ZookeeperDiscoverySpi (org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi)21 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)13 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)12 Test (org.junit.Test)10 Ignite (org.apache.ignite.Ignite)9 UUID (java.util.UUID)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 ArrayList (java.util.ArrayList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ZkTestClientCnxnSocketNIO (org.apache.zookeeper.ZkTestClientCnxnSocketNIO)3 HashMap (java.util.HashMap)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 Event (org.apache.ignite.events.Event)2 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2 BitSet (java.util.BitSet)1