IGNITE-9318: SQL: added "BASELINE_NODES" system view. This closes #4575.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e2fc48e2
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e2fc48e2
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e2fc48e2

Branch: refs/heads/ignite-9273
Commit: e2fc48e200189e5f36dcf72de8791f23e9d51c2f
Parents: 394d5ea
Author: Aleksey Plekhanov <plehanov.a...@gmail.com>
Authored: Thu Aug 23 11:18:05 2018 +0300
Committer: devozerov <voze...@gridgain.com>
Committed: Thu Aug 23 11:18:05 2018 +0300

----------------------------------------------------------------------
 .../processors/query/h2/IgniteH2Indexing.java   |  2 +
 .../h2/sys/view/SqlAbstractLocalSystemView.java | 10 +++
 .../h2/sys/view/SqlSystemViewBaselineNodes.java | 87 ++++++++++++++++++++
 .../query/SqlSystemViewsSelfTest.java           | 71 ++++++++++++++++
 4 files changed, 170 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e2fc48e2/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
index 44cda44..5859ecb 100644
--- 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
+++ 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
@@ -114,6 +114,7 @@ import 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlQueryParser;
 import org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuerySplitter;
 import org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement;
 import org.apache.ignite.internal.processors.query.h2.sys.SqlSystemTableEngine;
+import 
org.apache.ignite.internal.processors.query.h2.sys.view.SqlSystemViewBaselineNodes;
 import 
org.apache.ignite.internal.processors.query.h2.sys.view.SqlSystemViewNodeAttributes;
 import 
org.apache.ignite.internal.processors.query.h2.sys.view.SqlSystemViewNodes;
 import 
org.apache.ignite.internal.processors.query.h2.twostep.GridMapQueryExecutor;
@@ -2690,6 +2691,7 @@ public class IgniteH2Indexing implements 
GridQueryIndexing {
 
         views.add(new SqlSystemViewNodes(ctx));
         views.add(new SqlSystemViewNodeAttributes(ctx));
+        views.add(new SqlSystemViewBaselineNodes(ctx));
 
         return views;
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/e2fc48e2/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlAbstractLocalSystemView.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlAbstractLocalSystemView.java
 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlAbstractLocalSystemView.java
index e8d52d2..ac90b63 100644
--- 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlAbstractLocalSystemView.java
+++ 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlAbstractLocalSystemView.java
@@ -48,6 +48,16 @@ public abstract class SqlAbstractLocalSystemView extends 
SqlAbstractSystemView {
     }
 
     /**
+     * @param tblName Table name.
+     * @param desc Description.
+     * @param ctx Context.
+     * @param cols Columns.
+     */
+    public SqlAbstractLocalSystemView(String tblName, String desc, 
GridKernalContext ctx, Column ... cols) {
+        this(tblName, desc, ctx, new String[] {}, cols);
+    }
+
+    /**
      * @param ses Session.
      * @param key Key.
      * @param data Data for each column.

http://git-wip-us.apache.org/repos/asf/ignite/blob/e2fc48e2/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlSystemViewBaselineNodes.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlSystemViewBaselineNodes.java
 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlSystemViewBaselineNodes.java
new file mode 100644
index 0000000..81a9a77
--- /dev/null
+++ 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sys/view/SqlSystemViewBaselineNodes.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.h2.sys.view;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.processors.cluster.BaselineTopology;
+import org.apache.ignite.internal.util.typedef.F;
+import org.h2.engine.Session;
+import org.h2.result.Row;
+import org.h2.result.SearchRow;
+import org.h2.value.Value;
+
+/**
+ * System view: baseline nodes.
+ */
+public class SqlSystemViewBaselineNodes extends SqlAbstractLocalSystemView {
+    /**
+     * @param ctx Grid context.
+     */
+    public SqlSystemViewBaselineNodes(GridKernalContext ctx) {
+        super("BASELINE_NODES", "Baseline topology nodes", ctx,
+            newColumn("CONSISTENT_ID"),
+            newColumn("ONLINE", Value.BOOLEAN)
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override public Iterator<Row> getRows(Session ses, SearchRow first, 
SearchRow last) {
+        List<Row> rows = new ArrayList<>();
+
+        BaselineTopology blt = ctx.state().clusterState().baselineTopology();
+
+        if (blt == null)
+            return rows.iterator();
+
+        Set<Object> consistentIds = blt.consistentIds();
+
+        Collection<ClusterNode> srvNodes = ctx.discovery().aliveServerNodes();
+
+        Set<Object> aliveNodeIds = new 
HashSet<>(F.nodeConsistentIds(srvNodes));
+
+        for (Object consistentId : consistentIds) {
+            rows.add(
+                createRow(ses, rows.size(),
+                    consistentId,
+                    aliveNodeIds.contains(consistentId)
+                )
+            );
+        }
+
+        return rows.iterator();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean canGetRowCount() {
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getRowCount() {
+        BaselineTopology blt = ctx.state().clusterState().baselineTopology();
+
+        return blt == null ? 0 : blt.consistentIds().size();
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/e2fc48e2/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java
 
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java
index acd2a1c..bc5afa1 100644
--- 
a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java
+++ 
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSystemViewsSelfTest.java
@@ -26,6 +26,9 @@ import org.apache.ignite.IgniteCache;
 import org.apache.ignite.cache.QueryEntity;
 import org.apache.ignite.cache.query.SqlFieldsQuery;
 import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.internal.IgniteNodeAttributes;
 import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode;
 import org.apache.ignite.internal.util.typedef.X;
@@ -37,8 +40,17 @@ import 
org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
  */
 public class SqlSystemViewsSelfTest extends GridCommonAbstractTest {
     /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        cleanPersistenceDir();
+    }
+
+    /** {@inheritDoc} */
     @Override protected void afterTest() throws Exception {
         stopAllGrids();
+
+        cleanPersistenceDir();
     }
 
     /**
@@ -272,4 +284,63 @@ public class SqlSystemViewsSelfTest extends 
GridCommonAbstractTest {
             execSql("SELECT NODE_ID FROM IGNITE.NODE_ATTRIBUTES WHERE NODE_ID 
= ? AND NAME = '-'",
                 cliNodeId).size());
     }
+
+    /**
+     * Test baseline topology system view.
+     */
+    public void testBaselineViews() throws Exception {
+        cleanPersistenceDir();
+
+        Ignite ignite = startGrid(getTestIgniteInstanceName(), 
getPdsConfiguration("node0"));
+        startGrid(getTestIgniteInstanceName(1), getPdsConfiguration("node1"));
+
+        ignite.cluster().active(true);
+
+        List<List<?>> res = execSql("SELECT CONSISTENT_ID, ONLINE FROM 
IGNITE.BASELINE_NODES ORDER BY CONSISTENT_ID");
+
+        assertColumnTypes(res.get(0), String.class, Boolean.class);
+
+        assertEquals(2, res.size());
+
+        assertEquals("node0", res.get(0).get(0));
+        assertEquals("node1", res.get(1).get(0));
+
+        assertEquals(true, res.get(0).get(1));
+        assertEquals(true, res.get(1).get(1));
+
+        stopGrid(getTestIgniteInstanceName(1));
+
+        res = execSql("SELECT CONSISTENT_ID FROM IGNITE.BASELINE_NODES WHERE 
ONLINE = false");
+
+        assertEquals(1, res.size());
+
+        assertEquals("node1", res.get(0).get(0));
+
+        Ignite ignite2 = startGrid(getTestIgniteInstanceName(2), 
getPdsConfiguration("node2"));
+
+        assertEquals(2, execSql(ignite2, "SELECT CONSISTENT_ID FROM 
IGNITE.BASELINE_NODES").size());
+
+        res = execSql("SELECT CONSISTENT_ID FROM IGNITE.NODES N WHERE NOT 
EXISTS (SELECT 1 FROM " +
+            "IGNITE.BASELINE_NODES B WHERE B.CONSISTENT_ID = 
N.CONSISTENT_ID)");
+
+        assertEquals(1, res.size());
+
+        assertEquals("node2", res.get(0).get(0));
+    }
+
+    /**
+     * Gets ignite configuration with persistance enabled.
+     */
+    private IgniteConfiguration getPdsConfiguration(String consistentId) 
throws Exception {
+        IgniteConfiguration cfg = getConfiguration();
+
+        cfg.setDataStorageConfiguration(
+            new 
DataStorageConfiguration().setDefaultDataRegionConfiguration(new 
DataRegionConfiguration()
+                .setMaxSize(100L * 1024L * 1024L).setPersistenceEnabled(true))
+        );
+
+        cfg.setConsistentId(consistentId);
+
+        return cfg;
+    }
 }

Reply via email to