Search in sources :

Example 1 with IgniteCluster

use of org.apache.ignite.IgniteCluster in project ignite by apache.

the class ClusterGroupExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
            return;
        System.out.println();
        System.out.println("Compute example started.");
        IgniteCluster cluster = ignite.cluster();
        // Say hello to all nodes in the cluster, including local node.
        sayHello(ignite, cluster);
        // Say hello to all remote nodes.
        sayHello(ignite, cluster.forRemotes());
        // Pick random node out of remote nodes.
        ClusterGroup randomNode = cluster.forRemotes().forRandom();
        // Say hello to a random node.
        sayHello(ignite, randomNode);
        // Say hello to all nodes residing on the same host with random node.
        sayHello(ignite, cluster.forHost(randomNode.node()));
        // Say hello to all nodes that have current CPU load less than 50%.
        sayHello(ignite, cluster.forPredicate(new IgnitePredicate<ClusterNode>() {

            @Override
            public boolean apply(ClusterNode n) {
                return n.metrics().getCurrentCpuLoad() < 0.5;
            }
        }));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCluster(org.apache.ignite.IgniteCluster) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 2 with IgniteCluster

use of org.apache.ignite.IgniteCluster in project ignite by apache.

the class ClusterGroupExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
            return;
        System.out.println();
        System.out.println("Compute example started.");
        IgniteCluster cluster = ignite.cluster();
        // Say hello to all nodes in the cluster, including local node.
        sayHello(ignite, cluster);
        // Say hello to all remote nodes.
        sayHello(ignite, cluster.forRemotes());
        // Pick random node out of remote nodes.
        ClusterGroup randomNode = cluster.forRemotes().forRandom();
        // Say hello to a random node.
        sayHello(ignite, randomNode);
        // Say hello to all nodes residing on the same host with random node.
        sayHello(ignite, cluster.forHost(randomNode.node()));
        // Say hello to all nodes that have current CPU load less than 50%.
        sayHello(ignite, cluster.forPredicate(n -> n.metrics().getCurrentCpuLoad() < 0.5));
    }
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignition(org.apache.ignite.Ignition) IgniteCluster(org.apache.ignite.IgniteCluster) ExamplesUtils(org.apache.ignite.examples.ExamplesUtils) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) IgniteCluster(org.apache.ignite.IgniteCluster) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 3 with IgniteCluster

use of org.apache.ignite.IgniteCluster in project ignite by apache.

the class IgniteClientReconnectDiscoveryStateTest method testReconnect.

/**
 * @throws Exception If failed.
 */
public void testReconnect() throws Exception {
    final Ignite client = ignite(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    long topVer = 4;
    IgniteCluster cluster = client.cluster();
    cluster.nodeLocalMap().put("locMapKey", 10);
    Map<Integer, Integer> nodeCnt = new HashMap<>();
    nodeCnt.put(1, 1);
    nodeCnt.put(2, 2);
    nodeCnt.put(3, 3);
    nodeCnt.put(4, 4);
    for (Map.Entry<Integer, Integer> e : nodeCnt.entrySet()) {
        Collection<ClusterNode> nodes = cluster.topology(e.getKey());
        assertNotNull("No nodes for topology: " + e.getKey(), nodes);
        assertEquals((int) e.getValue(), nodes.size());
    }
    ClusterNode locNode = cluster.localNode();
    assertEquals(topVer, locNode.order());
    TestTcpDiscoverySpi srvSpi = spi(clientRouter(client));
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                info("Disconnected: " + evt);
                IgniteFuture<?> fut = client.cluster().clientReconnectFuture();
                assertNotNull(fut);
                assertFalse(fut.isDone());
            } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(reconnectLatch);
    // Client failed and rejoined.
    topVer += 2;
    locNode = cluster.localNode();
    assertEquals(topVer, locNode.order());
    assertEquals(topVer, cluster.topologyVersion());
    nodeCnt.put(5, 3);
    nodeCnt.put(6, 4);
    for (Map.Entry<Integer, Integer> e : nodeCnt.entrySet()) {
        Collection<ClusterNode> nodes = cluster.topology(e.getKey());
        assertNotNull("No nodes for topology: " + e.getKey(), nodes);
        assertEquals((int) e.getValue(), nodes.size());
    }
    assertEquals(10, cluster.nodeLocalMap().get("locMapKey"));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCluster(org.apache.ignite.IgniteCluster) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with IgniteCluster

use of org.apache.ignite.IgniteCluster in project ignite by apache.

the class ClusterGroupExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        if (!ExamplesUtils.checkMinTopologySize(ignite.cluster(), 2))
            return;
        System.out.println();
        System.out.println("Compute example started.");
        IgniteCluster cluster = ignite.cluster();
        // Say hello to all nodes in the cluster, including local node.
        sayHello(ignite, cluster);
        // Say hello to all remote nodes.
        sayHello(ignite, cluster.forRemotes());
        // Pick random node out of remote nodes.
        ClusterGroup randomNode = cluster.forRemotes().forRandom();
        // Say hello to a random node.
        sayHello(ignite, randomNode);
        // Say hello to all nodes residing on the same host with random node.
        sayHello(ignite, cluster.forHost(randomNode.node()));
        // Say hello to all nodes that have current CPU load less than 50%.
        sayHello(ignite, cluster.forPredicate(n -> n.metrics().getCurrentCpuLoad() < 0.5));
    }
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignition(org.apache.ignite.Ignition) IgniteCluster(org.apache.ignite.IgniteCluster) ExamplesUtils(org.apache.ignite.examples.ExamplesUtils) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) IgniteCluster(org.apache.ignite.IgniteCluster) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 5 with IgniteCluster

use of org.apache.ignite.IgniteCluster in project ignite by apache.

the class DdlStatementsProcessor method runDdlStatement.

/**
 * Run DDL statement.
 *
 * @param sql Original SQL.
 * @param cmd Command.
 * @return Result.
 * @throws IgniteCheckedException On error.
 */
@SuppressWarnings("unchecked")
public FieldsQueryCursor<List<?>> runDdlStatement(String sql, SqlCommand cmd) throws IgniteCheckedException {
    IgniteInternalFuture fut = null;
    try {
        if (cmd instanceof SqlCreateIndexCommand) {
            SqlCreateIndexCommand cmd0 = (SqlCreateIndexCommand) cmd;
            GridH2Table tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null)
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            assert tbl.rowDescriptor() != null;
            isDdlSupported(tbl);
            QueryIndex newIdx = new QueryIndex();
            newIdx.setName(cmd0.indexName());
            newIdx.setIndexType(cmd0.spatial() ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
            LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
            // Let's replace H2's table and property names by those operated by GridQueryProcessor.
            GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
            for (SqlIndexColumn col : cmd0.columns()) {
                GridQueryProperty prop = typeDesc.property(col.name());
                if (prop == null)
                    throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, col.name());
                flds.put(prop.name(), !col.descending());
            }
            newIdx.setFields(flds);
            newIdx.setInlineSize(cmd0.inlineSize());
            fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd0.ifNotExists(), cmd0.parallel());
        } else if (cmd instanceof SqlDropIndexCommand) {
            SqlDropIndexCommand cmd0 = (SqlDropIndexCommand) cmd;
            GridH2Table tbl = idx.dataTableForIndex(cmd0.schemaName(), cmd0.indexName());
            if (tbl != null) {
                isDdlSupported(tbl);
                fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd0.schemaName(), cmd0.indexName(), cmd0.ifExists());
            } else {
                if (cmd0.ifExists())
                    fut = new GridFinishedFuture();
                else
                    throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd0.indexName());
            }
        } else if (cmd instanceof SqlAlterTableCommand) {
            SqlAlterTableCommand cmd0 = (SqlAlterTableCommand) cmd;
            GridH2Table tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null) {
                ctx.cache().createMissingQueryCaches();
                tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
            }
            if (tbl == null) {
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            }
            Boolean logging = cmd0.logging();
            assert logging != null : "Only LOGGING/NOLOGGING are supported at the moment.";
            IgniteCluster cluster = ctx.grid().cluster();
            if (logging) {
                boolean res = cluster.enableWal(tbl.cacheName());
                if (!res)
                    throw new IgniteSQLException("Logging already enabled for table: " + cmd0.tableName());
            } else {
                boolean res = cluster.disableWal(tbl.cacheName());
                if (!res)
                    throw new IgniteSQLException("Logging already disabled for table: " + cmd0.tableName());
            }
            fut = new GridFinishedFuture();
        } else if (cmd instanceof SqlCreateUserCommand) {
            SqlCreateUserCommand addCmd = (SqlCreateUserCommand) cmd;
            ctx.authentication().addUser(addCmd.userName(), addCmd.password());
        } else if (cmd instanceof SqlAlterUserCommand) {
            SqlAlterUserCommand altCmd = (SqlAlterUserCommand) cmd;
            ctx.authentication().updateUser(altCmd.userName(), altCmd.password());
        } else if (cmd instanceof SqlDropUserCommand) {
            SqlDropUserCommand dropCmd = (SqlDropUserCommand) cmd;
            ctx.authentication().removeUser(dropCmd.userName());
        } else
            throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        if (fut != null)
            fut.get();
        return H2Utils.zeroCursor();
    } catch (SchemaOperationException e) {
        throw convert(e);
    } catch (IgniteSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new IgniteSQLException(e.getMessage(), e);
    }
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) LinkedHashMap(java.util.LinkedHashMap) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteCluster(org.apache.ignite.IgniteCluster) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryIndex(org.apache.ignite.cache.QueryIndex) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand)

Aggregations

IgniteCluster (org.apache.ignite.IgniteCluster)5 Ignite (org.apache.ignite.Ignite)4 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)3 IgniteException (org.apache.ignite.IgniteException)2 Ignition (org.apache.ignite.Ignition)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 ExampleNodeStartup (org.apache.ignite.examples.ExampleNodeStartup)2 ExamplesUtils (org.apache.ignite.examples.ExamplesUtils)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 QueryIndex (org.apache.ignite.cache.QueryIndex)1 Event (org.apache.ignite.events.Event)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)1 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)1 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)1 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)1