ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agoncha...@apache.org
Subject [07/25] ignite git commit: IGNITE-6013 Optimized processing response from cluster.
Date Wed, 16 Aug 2017 15:41:30 GMT
IGNITE-6013 Optimized processing response from cluster.


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

Branch: refs/heads/ignite-5901
Commit: b02c4812d9c145cf127eeaf54ab64709d230a3c7
Parents: 3a390c8
Author: Alexey Kuznetsov <akuznetsov@apache.org>
Authored: Fri Aug 11 11:18:42 2017 +0700
Committer: Alexey Kuznetsov <akuznetsov@apache.org>
Committed: Fri Aug 11 11:18:42 2017 +0700

----------------------------------------------------------------------
 .../console/agent/handlers/ClusterListener.java |  13 +-
 .../ignite/console/agent/rest/RestExecutor.java | 201 +++++++++++++++++--
 2 files changed, 194 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/b02c4812/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/handlers/ClusterListener.java
----------------------------------------------------------------------
diff --git a/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/handlers/ClusterListener.java
b/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/handlers/ClusterListener.java
index b811a2d..435ce74 100644
--- a/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/handlers/ClusterListener.java
+++ b/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/handlers/ClusterListener.java
@@ -46,7 +46,7 @@ import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_BUILD_VER;
 import static org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_SUCCESS;
 
 /**
- * API to retranslate topology from Ignite cluster available by node-uri.
+ * API to transfer topology from Ignite cluster available by node-uri.
  */
 public class ClusterListener {
     /** */
@@ -65,7 +65,7 @@ public class ClusterListener {
     private static final long DFLT_TIMEOUT = 3000L;
 
     /** JSON object mapper. */
-    private static final ObjectMapper mapper = new GridJettyObjectMapper();
+    private static final ObjectMapper MAPPER = new GridJettyObjectMapper();
 
     /** Latest topology snapshot. */
     private TopologySnapshot top;
@@ -234,10 +234,7 @@ public class ClusterListener {
 
         /**  */
         boolean differentCluster(TopologySnapshot old) {
-            if (old == null || F.isEmpty(old.nids))
-                return true;
-
-            return Collections.disjoint(nids, old.nids);
+            return old == null || F.isEmpty(old.nids) || Collections.disjoint(nids, old.nids);
         }
     }
 
@@ -250,7 +247,7 @@ public class ClusterListener {
 
                 switch (res.getStatus()) {
                     case STATUS_SUCCESS:
-                        List<GridClientNodeBean> nodes = mapper.readValue(res.getData(),
+                        List<GridClientNodeBean> nodes = MAPPER.readValue(res.getData(),
                             new TypeReference<List<GridClientNodeBean>>() {});
 
                         TopologySnapshot newTop = new TopologySnapshot(nodes);
@@ -290,7 +287,7 @@ public class ClusterListener {
 
                 switch (res.getStatus()) {
                     case STATUS_SUCCESS:
-                        List<GridClientNodeBean> nodes = mapper.readValue(res.getData(),
+                        List<GridClientNodeBean> nodes = MAPPER.readValue(res.getData(),
                             new TypeReference<List<GridClientNodeBean>>() {});
 
                         TopologySnapshot newTop = new TopologySnapshot(nodes);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b02c4812/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/rest/RestExecutor.java
----------------------------------------------------------------------
diff --git a/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/rest/RestExecutor.java
b/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/rest/RestExecutor.java
index 03eca4e..13989b4 100644
--- a/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/rest/RestExecutor.java
+++ b/modules/web-console/web-agent/src/main/java/org/apache/ignite/console/agent/rest/RestExecutor.java
@@ -17,9 +17,16 @@
 
 package org.apache.ignite.console.agent.rest;
 
-import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import java.io.IOException;
+import java.io.StringWriter;
 import java.net.ConnectException;
 import java.util.HashMap;
 import java.util.Map;
@@ -40,6 +47,9 @@ import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.logger.slf4j.Slf4jLogger;
 import org.slf4j.LoggerFactory;
 
+import static com.fasterxml.jackson.core.JsonToken.END_ARRAY;
+import static com.fasterxml.jackson.core.JsonToken.END_OBJECT;
+import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;
 import static org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_AUTH_FAILED;
 import static org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_FAILED;
 import static org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_SUCCESS;
@@ -52,7 +62,7 @@ public class RestExecutor {
     private static final IgniteLogger log = new Slf4jLogger(LoggerFactory.getLogger(RestExecutor.class));
 
     /** JSON object mapper. */
-    private static final ObjectMapper mapper = new GridJettyObjectMapper();
+    private static final ObjectMapper MAPPER = new GridJettyObjectMapper();
 
     /** */
     private final OkHttpClient httpClient;
@@ -141,34 +151,35 @@ public class RestExecutor {
         reqBuilder.url(urlBuilder.build());
 
         try (Response resp = httpClient.newCall(reqBuilder.build()).execute()) {
-            String content = resp.body().string();
-
             if (resp.isSuccessful()) {
-                JsonNode node = mapper.readTree(content);
+                RestResponseHolder res = MAPPER.readValue(resp.body().byteStream(), RestResponseHolder.class);
 
-                int status = node.get("successStatus").asInt();
+                int status = res.getSuccessStatus();
 
                 switch (status) {
                     case STATUS_SUCCESS:
-                        return RestResult.success(node.get("response").toString());
+                        return RestResult.success(res.getResponse());
 
                     default:
-                        return RestResult.fail(status, node.get("error").asText());
+                        return RestResult.fail(status, res.getError());
                 }
             }
 
             if (resp.code() == 401)
-                return RestResult.fail(STATUS_AUTH_FAILED, "Failed to authenticate in grid.
" +
+                return RestResult.fail(STATUS_AUTH_FAILED, "Failed to authenticate in cluster.
" +
                     "Please check agent\'s login and password or node port.");
 
-            return RestResult.fail(STATUS_FAILED, "Failed connect to node and execute REST
command.");
+            if (resp.code() == 404)
+                return RestResult.fail(STATUS_FAILED, "Failed connect to cluster.");
+
+            return RestResult.fail(STATUS_FAILED, "Failed to execute REST command: " + resp.message());
         }
         catch (ConnectException ignored) {
-            LT.warn(log, "Failed connect to node and execute REST command. " +
+            LT.warn(log, "Failed connect to cluster. " +
                 "Please ensure that nodes have [ignite-rest-http] module in classpath " +
                 "(was copied from libs/optional to libs folder).");
 
-            throw new ConnectException("Failed connect to node and execute REST command [url="
+ urlBuilder + ", parameters=" + params + "]");
+            throw new ConnectException("Failed connect to cluster [url=" + urlBuilder + ",
parameters=" + params + "]");
         }
     }
 
@@ -208,4 +219,170 @@ public class RestExecutor {
 
         return sendRequest(demo, "ignite", params, null, null);
     }
+
+    /**
+     * REST response holder Java bean.
+     */
+    private static class RestResponseHolder {
+        /** Success flag */
+        private int successStatus;
+
+        /** Error. */
+        private String err;
+
+        /** Response. */
+        private String res;
+
+        /** Session token string representation. */
+        private String sesTokStr;
+
+        /**
+         * @return {@code True} if this request was successful.
+         */
+        public int getSuccessStatus() {
+            return successStatus;
+        }
+
+        /**
+         * @param successStatus Whether request was successful.
+         */
+        public void setSuccessStatus(int successStatus) {
+            this.successStatus = successStatus;
+        }
+
+        /**
+         * @return Error.
+         */
+        public String getError() {
+            return err;
+        }
+
+        /**
+         * @param err Error.
+         */
+        public void setError(String err) {
+            this.err = err;
+        }
+
+        /**
+         * @return Response object.
+         */
+        public String getResponse() {
+            return res;
+        }
+
+        /**
+         * @param res Response object.
+         */
+        @JsonDeserialize(using = RawContentDeserializer.class)
+        public void setResponse(String res) {
+            this.res = res;
+        }
+
+        /**
+         * @return String representation of session token.
+         */
+        public String getSessionToken() {
+            return sesTokStr;
+        }
+
+        /**
+         * @param sesTokStr String representation of session token.
+         */
+        public void setSessionToken(String sesTokStr) {
+            this.sesTokStr = sesTokStr;
+        }
+    }
+
+    /**
+     * Raw content deserializer that will deserialize any data as string.
+     */
+    private static class RawContentDeserializer extends JsonDeserializer<String> {
+        /** */
+        private final JsonFactory factory = new JsonFactory();
+
+        /**
+         * @param tok Token to process.
+         * @param p Parser.
+         * @param gen Generator.
+         */
+        private void writeToken(JsonToken tok, JsonParser p, JsonGenerator gen) throws IOException
{
+            switch (tok) {
+                case FIELD_NAME:
+                    gen.writeFieldName(p.getText());
+                    break;
+
+                case START_ARRAY:
+                    gen.writeStartArray();
+                    break;
+
+                case END_ARRAY:
+                    gen.writeEndArray();
+                    break;
+
+                case START_OBJECT:
+                    gen.writeStartObject();
+                    break;
+
+                case END_OBJECT:
+                    gen.writeEndObject();
+                    break;
+
+                case VALUE_NUMBER_INT:
+                    gen.writeNumber(p.getLongValue());
+                    break;
+
+                case VALUE_NUMBER_FLOAT:
+                    gen.writeNumber(p.getDoubleValue());
+                    break;
+
+                case VALUE_TRUE:
+                    gen.writeBoolean(true);
+                    break;
+
+                case VALUE_FALSE:
+                    gen.writeBoolean(false);
+                    break;
+
+                case VALUE_NULL:
+                    gen.writeNull();
+                    break;
+
+                default:
+                    gen.writeString(p.getText());
+            }
+        }
+
+        /** {@inheritDoc} */
+        @Override public String deserialize(JsonParser p, DeserializationContext ctxt) throws
IOException {
+            JsonToken startTok = p.getCurrentToken();
+
+            if (startTok.isStructStart()) {
+                StringWriter wrt = new StringWriter(4096);
+
+                JsonGenerator gen = factory.createGenerator(wrt);
+
+                JsonToken tok = startTok, endTok = startTok == START_ARRAY ? END_ARRAY :
END_OBJECT;
+
+                int cnt = 1;
+
+                while (cnt > 0) {
+                    writeToken(tok, p, gen);
+
+                    tok = p.nextToken();
+
+                    if (tok == startTok)
+                        cnt++;
+                    else if (tok == endTok)
+                        cnt--;
+                }
+
+                gen.close();
+
+                return wrt.toString();
+            }
+
+            return p.getValueAsString();
+        }
+    }
 }


Mime
View raw message