ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anovi...@apache.org
Subject [7/9] ignite git commit: IGNITE-1168 REST return sql metadata for all caches in one request. - Fixes #181.
Date Mon, 09 Nov 2015 04:07:47 GMT
IGNITE-1168 REST return sql metadata for all caches in one request. - Fixes #181.

Signed-off-by: Andrey <anovikov@gridgain.com>


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

Branch: refs/heads/master
Commit: 354358cc4ec9a8f15e75d132c7a57d2361b5722b
Parents: 621ecac
Author: Andrey <anovikov@gridgain.com>
Authored: Mon Nov 9 10:41:04 2015 +0700
Committer: Andrey <anovikov@gridgain.com>
Committed: Mon Nov 9 10:41:04 2015 +0700

----------------------------------------------------------------------
 .../JettyRestProcessorAbstractSelfTest.java     | 105 ++++++++-------
 .../discovery/GridDiscoveryManager.java         |   5 +-
 .../handlers/cache/GridCacheCommandHandler.java | 128 +++++++++++++++----
 .../top/GridTopologyCommandHandler.java         |   4 -
 4 files changed, 166 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/354358cc/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
index c9c4ced..4c73f78 100644
--- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
@@ -40,15 +40,14 @@ import org.apache.ignite.cache.query.SqlQuery;
 import org.apache.ignite.cache.query.annotations.QuerySqlField;
 import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.configuration.CacheConfiguration;
-import org.apache.ignite.internal.processors.cache.GridCacheProcessor;
 import org.apache.ignite.internal.processors.cache.IgniteCacheProxy;
-import org.apache.ignite.internal.processors.cache.IgniteInternalCache;
 import org.apache.ignite.internal.processors.cache.query.GridCacheSqlIndexMetadata;
 import org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata;
 import org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.CU;
 import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.util.typedef.P1;
 import org.apache.ignite.lang.IgniteBiPredicate;
 import org.apache.ignite.lang.IgnitePredicate;
 import org.apache.ignite.testframework.GridTestUtils;
@@ -919,14 +918,16 @@ public abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestPro
     }
 
     /**
-     * @param meta Metadata for Ignite cache.
+     * @param metas Metadata for Ignite caches.
      * @throws Exception If failed.
      */
-    private void testMetadata(GridCacheSqlMetadata meta) throws Exception {
+    private void testMetadata(Collection<GridCacheSqlMetadata> metas) throws Exception
{
         Map<String, String> params = F.asMap("cmd", GridRestCommand.CACHE_METADATA.key());
 
-        if (meta.cacheName() != null)
-            params.put("cacheName", meta.cacheName());
+        String cacheNameArg = F.first(metas).cacheName();
+
+        if (cacheNameArg != null)
+            params.put("cacheName", cacheNameArg);
 
         String ret = content(params);
 
@@ -935,53 +936,68 @@ public abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestPro
 
         info("Cache metadata result: " + ret);
 
-        jsonEquals(ret, pattern("\\{.+\\}", true));
+        jsonEquals(ret, pattern("\\[.+\\]", true));
+
+        Collection<Map> results = (Collection)JSONObject.fromObject(ret).get("response");
+
+        assertEquals(metas.size(), results.size());
+        assertEquals(cacheNameArg, F.first(results).get("cacheName"));
+
+        for (Map res : results) {
+            final Object cacheName = res.get("cacheName");
 
-        Map res = (Map)JSONObject.fromObject(ret).get("response");
+            GridCacheSqlMetadata meta = F.find(metas, null, new P1<GridCacheSqlMetadata>()
{
+                @Override public boolean apply(GridCacheSqlMetadata meta) {
+                    return F.eq(meta.cacheName(), cacheName);
+                }
+            });
 
-        Collection types = (Collection)res.get("types");
+            assertNotNull("REST return metadata for unexpected cache: " + cacheName, meta);
 
-        assertNotNull(types);
-        assertEqualsCollections(meta.types(), types);
+            Collection types = (Collection)res.get("types");
 
-        Map keyClasses = (Map)res.get("keyClasses");
+            assertNotNull(types);
+            assertEqualsCollections(meta.types(), types);
 
-        assertNotNull(keyClasses);
-        assertTrue(meta.keyClasses().equals(keyClasses));
+            Map keyClasses = (Map)res.get("keyClasses");
 
-        Map valClasses = (Map)res.get("valClasses");
+            assertNotNull(keyClasses);
+            assertTrue(meta.keyClasses().equals(keyClasses));
 
-        assertNotNull(valClasses);
-        assertTrue(meta.valClasses().equals(valClasses));
+            Map valClasses = (Map)res.get("valClasses");
 
-        Map fields = (Map)res.get("fields");
+            assertNotNull(valClasses);
+            assertTrue(meta.valClasses().equals(valClasses));
 
-        assertNotNull(fields);
-        assertTrue(meta.fields().equals(fields));
+            Map fields = (Map)res.get("fields");
 
-        Map indexesByType = (Map)res.get("indexes");
+            assertNotNull(fields);
+            assertTrue(meta.fields().equals(fields));
 
-        assertNotNull(indexesByType);
-        assertEquals(meta.indexes().size(), indexesByType.size());
+            Map indexesByType = (Map)res.get("indexes");
 
-        for (Map.Entry<String, Collection<GridCacheSqlIndexMetadata>> metaIndexes
: meta.indexes().entrySet()) {
-            Collection<Map> indexes = (Collection<Map>)indexesByType.get(metaIndexes.getKey());
+            assertNotNull(indexesByType);
+            assertEquals(meta.indexes().size(), indexesByType.size());
 
-            assertNotNull(indexes);
-            assertEquals(metaIndexes.getValue().size(), indexes.size());
+            for (Map.Entry<String, Collection<GridCacheSqlIndexMetadata>> metaIndexes
: meta.indexes().entrySet()) {
+                Collection<Map> indexes = (Collection<Map>)indexesByType.get(metaIndexes.getKey());
 
-            for (final GridCacheSqlIndexMetadata metaIdx : metaIndexes.getValue()) {
-                Map idx = F.find(indexes, null, new IgnitePredicate<Map>() {
-                    @Override public boolean apply(Map map) {
-                        return metaIdx.name().equals(map.get("name"));
-                    }
-                });
+                assertNotNull(indexes);
+                assertEquals(metaIndexes.getValue().size(), indexes.size());
 
-                assertNotNull(idx);
+                for (final GridCacheSqlIndexMetadata metaIdx : metaIndexes.getValue()) {
+                    Map idx = F.find(indexes, null, new P1<Map>() {
+                        @Override public boolean apply(Map map) {
+                            return metaIdx.name().equals(map.get("name"));
+                        }
+                    });
 
-                assertEqualsCollections(metaIdx.fields(), (Collection)idx.get("fields"));
-                assertEqualsCollections(metaIdx.descendings(), (Collection)idx.get("descendings"));
-                assertEquals(metaIdx.unique(), idx.get("unique"));
+                    assertNotNull(idx);
+
+                    assertEqualsCollections(metaIdx.fields(), (Collection)idx.get("fields"));
+                    assertEqualsCollections(metaIdx.descendings(), (Collection)idx.get("descendings"));
+                    assertEquals(metaIdx.unique(), idx.get("unique"));
+                }
             }
         }
     }
@@ -990,16 +1006,13 @@ public abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestPro
      * @throws Exception If failed.
      */
     public void testMetadataLocal() throws Exception {
-        GridCacheProcessor cacheProc = grid(0).context().cache();
+        IgniteCacheProxy<?, ?> cache = F.first(grid(0).context().cache().publicCaches());
 
-        for (IgniteInternalCache<?, ?> cache : cacheProc.caches()) {
-            if (CU.isSystemCache(cache.name()))
-                continue;
+        assertNotNull("Should have configured public cache!", cache);
 
-            GridCacheSqlMetadata meta = F.first(cache.context().queries().sqlMetadata());
+        Collection<GridCacheSqlMetadata> meta = cache.context().queries().sqlMetadata();
 
-            testMetadata(meta);
-        }
+        testMetadata(meta);
     }
 
     /**
@@ -1013,9 +1026,9 @@ public abstract class JettyRestProcessorAbstractSelfTest extends AbstractRestPro
 
         IgniteCacheProxy<Integer, String> c = (IgniteCacheProxy<Integer, String>)grid(1).createCache(partialCacheCfg);
 
-        GridCacheSqlMetadata meta = F.first(c.context().queries().sqlMetadata());
+        Collection<GridCacheSqlMetadata> metas = c.context().queries().sqlMetadata();
 
-        testMetadata(meta);
+        testMetadata(metas);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/354358cc/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
index 6aba211..cd2f49c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/managers/discovery/GridDiscoveryManager.java
@@ -1594,7 +1594,7 @@ public class GridDiscoveryManager extends GridManagerAdapter<DiscoverySpi>
{
 
     /**
      * @param node Node to check.
-     * @return Cache names accessible on the given node.
+     * @return Public cache names accessible on the given node.
      */
     public Map<String, CacheMode> nodeCaches(ClusterNode node) {
         Map<String, CacheMode> caches = U.newHashMap(registeredCaches.size());
@@ -1604,7 +1604,8 @@ public class GridDiscoveryManager extends GridManagerAdapter<DiscoverySpi>
{
 
             CachePredicate pred = entry.getValue();
 
-            if (pred != null && pred.cacheNode(node))
+            if (!CU.isSystemCache(cacheName) && !CU.isIgfsCache(ctx.config(), cacheName)
&&
+                pred != null && pred.cacheNode(node))
                 caches.put(cacheName, pred.cacheMode);
         }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/354358cc/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
index 1bbc754..65dca08 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
@@ -18,10 +18,13 @@
 package org.apache.ignite.internal.processors.rest.handlers.cache;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
@@ -35,10 +38,18 @@ import org.apache.ignite.cache.CacheMetrics;
 import org.apache.ignite.cache.CacheMode;
 import org.apache.ignite.cache.CachePeekMode;
 import org.apache.ignite.cluster.ClusterGroup;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.compute.ComputeJob;
+import org.apache.ignite.compute.ComputeJobAdapter;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.compute.ComputeTaskAdapter;
 import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.IgniteEx;
 import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
 import org.apache.ignite.internal.processors.cache.GridCacheAdapter;
+import org.apache.ignite.internal.processors.cache.IgniteCacheProxy;
 import org.apache.ignite.internal.processors.cache.IgniteInternalCache;
 import org.apache.ignite.internal.processors.cache.query.GridCacheSqlMetadata;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx;
@@ -370,20 +381,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
                 }
 
                 case CACHE_METADATA: {
-                    IgniteInternalCache<?, ?> cache = ctx.cache().cache(cacheName);
-
-                    if (cache != null) {
-                        GridCacheSqlMetadata res = F.first(cache.context().queries().sqlMetadata());
-
-                        fut = new GridFinishedFuture<>(new GridRestResponse(res));
-                    }
-                    else {
-                        ClusterGroup prj = ctx.grid().cluster().forDataNodes(cacheName);
-
-                        ctx.task().setThreadContext(TC_NO_FAILOVER, true);
-
-                        fut = ctx.closure().callAsync(BALANCE, new MetadataCommand(cacheName),
prj.nodes());
-                    }
+                    fut = ctx.task().execute(MetadataTask.class, cacheName);
 
                     break;
                 }
@@ -903,27 +901,109 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter
{
     }
 
     /** */
-    private static class MetadataCommand implements Callable<GridRestResponse>, Serializable
{
+    @GridInternal
+    private static class MetadataTask extends ComputeTaskAdapter<String, GridRestResponse>
{
         /** */
         private static final long serialVersionUID = 0L;
 
         /** */
-        private final String cacheName;
+        @IgniteInstanceResource
+        private IgniteEx ignite;
 
         /** */
-        @IgniteInstanceResource
-        private Ignite g;
+        private String cacheName;
 
-        /**
-         * @param cacheName Cache name.
-         */
-        private MetadataCommand(String cacheName) {
+        /** {@inheritDoc} */
+        @Nullable @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode>
subgrid,
+            @Nullable String cacheName) throws IgniteException {
             this.cacheName = cacheName;
+
+            GridDiscoveryManager discovery = ignite.context().discovery();
+
+            boolean sameCaches = true;
+
+            int hash = discovery.nodeCaches(F.first(subgrid)).hashCode();
+
+            for (int i = 1; i < subgrid.size(); i++) {
+                if (hash != discovery.nodeCaches(subgrid.get(i)).hashCode()) {
+                    sameCaches = false;
+
+                    break;
+                }
+            }
+
+            Map<ComputeJob, ClusterNode> map = U.newHashMap(sameCaches ? 1 : subgrid.size());
+
+            if (sameCaches)
+                map.put(new MetadataJob(), ignite.localNode());
+            else {
+                for (ClusterNode node : subgrid)
+                    map.put(new MetadataJob(), node);
+            }
+
+            return map;
         }
 
         /** {@inheritDoc} */
-        @Override public GridRestResponse call() throws Exception {
-            return  new GridRestResponse(F.first(cache(g, cacheName).context().queries().sqlMetadata()));
+        @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
+        @Nullable @Override public GridRestResponse reduce(List<ComputeJobResult> results)
throws IgniteException {
+            Map<String, GridCacheSqlMetadata> map = new HashMap<>();
+
+            for (ComputeJobResult r : results) {
+                if (!r.isCancelled() && r.getException() == null) {
+                    for (GridCacheSqlMetadata m : r.<Collection<GridCacheSqlMetadata>>getData())
{
+                        if (!map.containsKey(m.cacheName()))
+                            map.put(m.cacheName(), m);
+                    }
+                }
+            }
+
+            Collection<GridCacheSqlMetadata> metas = new ArrayList<>(map.size());
+
+            // Metadata for current cache must be first in list.
+            GridCacheSqlMetadata cacheMeta = map.remove(cacheName);
+
+            if (cacheMeta != null)
+                metas.add(cacheMeta);
+
+            metas.addAll(map.values());
+
+            return new GridRestResponse(metas);
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return S.toString(MetadataTask.class, this);
+        }
+    }
+
+    /** */
+    private static class MetadataJob extends ComputeJobAdapter {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** Auto-injected grid instance. */
+        @IgniteInstanceResource
+        private transient IgniteEx ignite;
+
+        /** {@inheritDoc} */
+        @Override public Collection<GridCacheSqlMetadata> execute() {
+            IgniteCacheProxy<?, ?> cache = F.first(ignite.context().cache().publicCaches());
+
+            if (cache == null)
+                return Collections.emptyList();
+
+            try {
+                return cache.context().queries().sqlMetadata();
+            }
+            catch (IgniteCheckedException e) {
+                throw U.convertException(e);
+            }
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return S.toString(MetadataJob.class, this);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/354358cc/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/top/GridTopologyCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/top/GridTopologyCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/top/GridTopologyCommandHandler.java
index eea29d2..f950ac2 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/top/GridTopologyCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/top/GridTopologyCommandHandler.java
@@ -44,7 +44,6 @@ import org.apache.ignite.internal.processors.rest.request.GridRestTopologyReques
 import org.apache.ignite.internal.util.future.GridFinishedFuture;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.P1;
-import org.apache.ignite.internal.util.typedef.internal.CU;
 import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.spi.IgnitePortProtocol;
@@ -202,9 +201,6 @@ public class GridTopologyCommandHandler extends GridRestCommandHandlerAdapter
{
         for (Map.Entry<String, CacheMode> cache : nodeCaches.entrySet()) {
             String cacheName = cache.getKey();
 
-            if (CU.isSystemCache(cacheName) || CU.isIgfsCache(ctx.config(), cacheName))
-                continue;
-
             String mode = cache.getValue().toString();
 
             if (cacheName != null)


Mime
View raw message