tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject [03/50] incubator-tinkerpop git commit: Allowed ServerGraph and Cluster to be configured with Configuration object.
Date Thu, 17 Mar 2016 20:23:20 GMT
Allowed ServerGraph and Cluster to be configured with Configuration object.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/7da621c8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/7da621c8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/7da621c8

Branch: refs/heads/TINKERPOP-575
Commit: 7da621c8e365aaf819bdd5c40a002140b7a47989
Parents: fce344e
Author: Stephen Mallette <spmva@genoprime.com>
Authored: Fri Mar 11 09:13:39 2016 -0500
Committer: Stephen Mallette <spmva@genoprime.com>
Committed: Thu Mar 17 14:05:19 2016 -0400

----------------------------------------------------------------------
 .../gremlin/process/server/ServerGraph.java     |  33 +++++-
 .../tinkerpop/gremlin/driver/Cluster.java       |  12 +++
 .../tinkerpop/gremlin/driver/Settings.java      | 107 +++++++++++++++++++
 .../driver/strategy/DriverServerConnection.java |  45 ++++++++
 .../tinkerpop/gremlin/driver/SettingsTest.java  |  88 +++++++++++++++
 5 files changed, 283 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7da621c8/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/server/ServerGraph.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/server/ServerGraph.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/server/ServerGraph.java
index 1873e53..f24d172 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/server/ServerGraph.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/server/ServerGraph.java
@@ -29,6 +29,7 @@ import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;
 import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
 
+import java.lang.reflect.Constructor;
 import java.util.Collections;
 import java.util.Iterator;
 
@@ -45,6 +46,9 @@ public class ServerGraph implements Graph {
     private final ServerConnection connection;
     private final Class<? extends Graph> graphClass;
 
+    public static final String GREMLIN_SERVERGRAPH_SERVER_CONNECTION_CLASS = "gremlin.servergraph.serverConnectionClass";
+    public static final String GREMLIN_SERVERGRAPH_GRAPH_CLASS = "gremlin.servergraph.graphClass";
+
     private ServerGraph(final ServerConnection connection, final Class<? extends Graph>
graphClass) {
         this.connection = connection;
         this.graphClass = graphClass;TraversalStrategies.GlobalCache.registerStrategies(
@@ -53,10 +57,35 @@ public class ServerGraph implements Graph {
 
     /**
      * Creates a new {@link ServerGraph} instance using the specified configuration, which
allows {@link ServerGraph}
-     * to be compliant with {@link GraphFactory}.
+     * to be compliant with {@link GraphFactory}. Expects keys for the {@link #GREMLIN_SERVERGRAPH_GRAPH_CLASS}
and
+     * {@link #GREMLIN_SERVERGRAPH_SERVER_CONNECTION_CLASS} as well as any configuration
required by the underlying
+     * {@link ServerConnection} which will be instantiated. Note that the {@code Configuration}
object is passed down
+     * without change to the creation of the {@link ServerConnection} instance.
      */
     public static ServerGraph open(final Configuration conf) {
-        return null;
+        if (!conf.containsKey(GREMLIN_SERVERGRAPH_GRAPH_CLASS))
+            throw new IllegalArgumentException("Configuration must contain the '" + GREMLIN_SERVERGRAPH_GRAPH_CLASS
+"' key");
+
+        if (!conf.containsKey(GREMLIN_SERVERGRAPH_SERVER_CONNECTION_CLASS))
+            throw new IllegalArgumentException("Configuration must contain the '" + GREMLIN_SERVERGRAPH_SERVER_CONNECTION_CLASS
+"' key");
+
+        final ServerConnection serverConnection;
+        try {
+            final Class<? extends ServerConnection> clazz = Class.forName(conf.getString(GREMLIN_SERVERGRAPH_SERVER_CONNECTION_CLASS)).asSubclass(ServerConnection.class);
+            final Constructor<? extends ServerConnection> ctor = clazz.getConstructor(Configuration.class);
+            serverConnection = ctor.newInstance(conf);
+        } catch (Exception ex) {
+            throw new IllegalStateException(ex);
+        }
+
+        final Class<? extends Graph> graphClazz;
+        try {
+            graphClazz = Class.forName(conf.getString(GREMLIN_SERVERGRAPH_GRAPH_CLASS)).asSubclass(Graph.class);
+        } catch (Exception ex) {
+            throw new IllegalStateException(ex);
+        }
+
+        return new ServerGraph(serverConnection, graphClazz);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7da621c8/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
index c76e03c..72769c9 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java
@@ -20,6 +20,7 @@ package org.apache.tinkerpop.gremlin.driver;
 
 import io.netty.buffer.PooledByteBufAllocator;
 import io.netty.channel.ChannelOption;
+import org.apache.commons.configuration.Configuration;
 import org.apache.tinkerpop.gremlin.driver.ser.Serializers;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.EventLoopGroup;
@@ -146,6 +147,10 @@ public final class Cluster {
 
     public static Builder build(final File configurationFile) throws FileNotFoundException
{
         final Settings settings = Settings.read(new FileInputStream(configurationFile));
+        return getBuilderFromSettings(settings);
+    }
+
+    private static Builder getBuilderFromSettings(final Settings settings) {
         final List<String> addresses = settings.hosts;
         if (addresses.size() == 0)
             throw new IllegalStateException("At least one value must be specified to the
hosts setting");
@@ -199,6 +204,13 @@ public final class Cluster {
     }
 
     /**
+     * Create a {@code Cluster} from Apache Configurations.
+     */
+    public static Cluster open(final Configuration conf) {
+        return getBuilderFromSettings(Settings.from(conf)).create();
+    }
+
+    /**
      * Create a {@code Cluster} using a YAML-based configuration file.
      */
     public static Cluster open(final String configurationFile) throws Exception {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7da621c8/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java
index 4daa223..24848ad 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Settings.java
@@ -19,16 +19,22 @@
 package org.apache.tinkerpop.gremlin.driver;
 
 import org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0;
+import org.apache.commons.configuration.Configuration;
+import org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
 import org.yaml.snakeyaml.TypeDescription;
 import org.yaml.snakeyaml.Yaml;
 import org.yaml.snakeyaml.constructor.Constructor;
 
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.Properties;
+import java.util.stream.Collectors;
 
 /**
  * Settings for the {@link Cluster} and its related components.
@@ -107,6 +113,107 @@ final class Settings {
         return yaml.loadAs(stream, Settings.class);
     }
 
+    /**
+     * Read configuration from a file into a new {@link Settings} object.
+     */
+    public static Settings from(final Configuration conf) {
+        final Settings settings = new Settings();
+
+        if (conf.containsKey("port"))
+            settings.port = conf.getInt("port");
+
+        if (conf.containsKey("nioPoolSize"))
+            settings.nioPoolSize = conf.getInt("nioPoolSize");
+
+        if (conf.containsKey("workerPoolSize"))
+            settings.workerPoolSize = conf.getInt("workerPoolSize");
+
+        if (conf.containsKey("username"))
+            settings.username = conf.getString("username");
+
+        if (conf.containsKey("password"))
+            settings.password = conf.getString("password");
+
+        if (conf.containsKey("jaasEntry"))
+            settings.jaasEntry = conf.getString("jaasEntry");
+
+        if (conf.containsKey("protocol"))
+            settings.protocol = conf.getString("protocol");
+
+        if (conf.containsKey("hosts"))
+            settings.hosts = conf.getList("hosts").stream().map(Object::toString).collect(Collectors.toList());
+
+        if (conf.containsKey("serializer.className")) {
+            final SerializerSettings serializerSettings = new SerializerSettings();
+            final Configuration serializerConf = conf.subset("serializer");
+
+            if (serializerConf.containsKey("className"))
+                serializerSettings.className = serializerConf.getString("className");
+
+            final Configuration serializerConfigConf = conf.subset("serializer.config");
+            if (IteratorUtils.count(serializerConfigConf.getKeys()) > 0) {
+                final Map<String,Object> m = new HashMap<>();
+                serializerConfigConf.getKeys().forEachRemaining(name -> {
+                    m.put(name, serializerConfigConf.getProperty(name));
+                });
+                serializerSettings.config = m;
+            }
+            settings.serializer = serializerSettings;
+        }
+
+        final Configuration connectionPoolConf = conf.subset("connectionPool");
+        if (IteratorUtils.count(connectionPoolConf.getKeys()) > 0) {
+            final ConnectionPoolSettings cpSettings = new ConnectionPoolSettings();
+
+            if (connectionPoolConf.containsKey("channelizer"))
+                cpSettings.channelizer = connectionPoolConf.getString("channelizer");
+
+            if (connectionPoolConf.containsKey("enableSsl"))
+                cpSettings.enableSsl = connectionPoolConf.getBoolean("enableSsl");
+
+            if (connectionPoolConf.containsKey("trustCertChainFile"))
+                cpSettings.trustCertChainFile = connectionPoolConf.getString("trustCertChainFile");
+
+            if (connectionPoolConf.containsKey("minSize"))
+                cpSettings.minSize = connectionPoolConf.getInt("minSize");
+
+            if (connectionPoolConf.containsKey("maxSize"))
+                cpSettings.maxSize = connectionPoolConf.getInt("maxSize");
+
+            if (connectionPoolConf.containsKey("minSimultaneousUsagePerConnection"))
+                cpSettings.minSimultaneousUsagePerConnection = connectionPoolConf.getInt("minSimultaneousUsagePerConnection");
+
+            if (connectionPoolConf.containsKey("maxSimultaneousUsagePerConnection"))
+                cpSettings.maxSimultaneousUsagePerConnection = connectionPoolConf.getInt("maxSimultaneousUsagePerConnection");
+
+            if (connectionPoolConf.containsKey("maxInProcessPerConnection"))
+                cpSettings.maxInProcessPerConnection = connectionPoolConf.getInt("maxInProcessPerConnection");
+
+            if (connectionPoolConf.containsKey("minInProcessPerConnection"))
+                cpSettings.minInProcessPerConnection = connectionPoolConf.getInt("minInProcessPerConnection");
+
+            if (connectionPoolConf.containsKey("maxWaitForConnection"))
+                cpSettings.maxWaitForConnection = connectionPoolConf.getInt("maxWaitForConnection");
+
+            if (connectionPoolConf.containsKey("maxContentLength"))
+                cpSettings.maxContentLength = connectionPoolConf.getInt("maxContentLength");
+
+            if (connectionPoolConf.containsKey("reconnectInterval"))
+                cpSettings.reconnectInterval = connectionPoolConf.getInt("reconnectInterval");
+
+            if (connectionPoolConf.containsKey("reconnectInitialDelay"))
+                cpSettings.reconnectInitialDelay = connectionPoolConf.getInt("reconnectInitialDelay");
+
+            if (connectionPoolConf.containsKey("resultIterationBatchSize"))
+                cpSettings.resultIterationBatchSize = connectionPoolConf.getInt("resultIterationBatchSize");
+
+
+            settings.connectionPool = cpSettings;
+        }
+
+        return settings;
+    }
+
     static class ConnectionPoolSettings {
         /**
          * Determines if SSL should be enabled or not. If enabled on the server then it must
be enabled on the client.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7da621c8/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/strategy/DriverServerConnection.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/strategy/DriverServerConnection.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/strategy/DriverServerConnection.java
index e7cab0d..60ee6bb 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/strategy/DriverServerConnection.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/strategy/DriverServerConnection.java
@@ -43,6 +43,26 @@ public class DriverServerConnection implements ServerConnection {
     private final boolean tryCloseCluster;
     private final String connectionGraphName;
 
+    public DriverServerConnection(final Configuration conf) {
+        if (conf.containsKey("clusterConfigurationFile") && conf.containsKey("clusterConfiguration"))
+            throw new IllegalStateException("A configuration should not contain both 'clusterConfigurationFile'
and 'clusterConfiguration'");
+
+        if (!conf.containsKey("clusterConfigurationFile") && !conf.containsKey("clusterConfiguration"))
+            throw new IllegalStateException("A configuration must contain either 'clusterConfigurationFile'
and 'clusterConfiguration'");
+
+        connectionGraphName = conf.getString("connectionGraphName", "graph");
+
+        try {
+            final Cluster cluster = conf.containsKey("clusterConfigurationFile") ?
+                Cluster.open(conf.getString("clusterConfigurationFile")) : Cluster.open(conf.subset("clusterConfiguration"));
+            client = cluster.connect(Client.Settings.build().unrollTraversers(false).create()).alias(connectionGraphName);
+        } catch (Exception ex) {
+            throw new IllegalStateException(ex);
+        }
+
+        tryCloseCluster = true;
+    }
+
     private DriverServerConnection(final Cluster cluster, final boolean tryCloseCluster,
final String connectionGraphName){
         client = cluster.connect(Client.Settings.build().unrollTraversers(false).create()).alias(connectionGraphName);
         this.connectionGraphName = connectionGraphName;
@@ -87,6 +107,31 @@ public class DriverServerConnection implements ServerConnection {
         }
     }
 
+    /**
+     * Creates a {@link DriverServerConnection} using an Apache {@code Configuration} object.
This method of creation
+     * is typically used by {@link ServerGraph} when being constructed via {@link GraphFactory}.
The
+     * {@code Configuration} object should contain one of two required keys, either: {@code
clusterConfigurationFile}
+     * or {@code clusterConfiguration}. The {@code clusterConfigurationFile} key is a pointer
to a file location
+     * containing a configuration for a {@link Cluster}. The {@code clusterConfiguration}
should contain the actual
+     * contents of a configuration that would be used by a {@link Cluster}.  This {@code
configuration} may also
+     * contain the optional, but likely necessary, {@code connectionGraphName} which tells
the
+     * {@code DriverServerConnection} which graph on the server to bind to.
+     */
+    public static DriverServerConnection using(final Configuration conf) {
+        if (conf.containsKey("clusterConfigurationFile") && conf.containsKey("clusterConfiguration"))
+            throw new IllegalStateException("A configuration should not contain both 'clusterConfigurationFile'
and 'clusterConfiguration'");
+
+        if (!conf.containsKey("clusterConfigurationFile") && !conf.containsKey("clusterConfiguration"))
+            throw new IllegalStateException("A configuration must contain either 'clusterConfigurationFile'
and 'clusterConfiguration'");
+
+        final String connectionGraphName = conf.getString("connectionGraphName", "graph");
+        if (conf.containsKey("clusterConfigurationFile"))
+            return using(conf.getString("clusterConfigurationFile"), connectionGraphName);
+        else {
+            return using(Cluster.open(conf.subset("clusterConfiguration")), connectionGraphName);
+        }
+    }
+
     @Override
     public Iterator<Traverser> submit(final Traversal t) throws ServerConnectionException
 {
         try {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7da621c8/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/SettingsTest.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/SettingsTest.java
b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/SettingsTest.java
new file mode 100644
index 0000000..a6a2298
--- /dev/null
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/SettingsTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.tinkerpop.gremlin.driver;
+
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class SettingsTest {
+
+    @Test
+    public void shouldCreateFromConfiguration() {
+        final Configuration conf = new BaseConfiguration();
+        conf.setProperty("port", 8000);
+        conf.setProperty("nioPoolSize", 16);
+        conf.setProperty("workerPoolSize", 32);
+        conf.setProperty("username", "user1");
+        conf.setProperty("password", "password1");
+        conf.setProperty("jaasEntry", "JaasIt");
+        conf.setProperty("protocol", "protocol0");
+        conf.setProperty("hosts", Arrays.asList("255.0.0.1", "255.0.0.2", "255.0.0.3"));
+        conf.setProperty("serializer.className", "my.serializers.MySerializer");
+        conf.setProperty("serializer.config.any", "thing");
+        conf.setProperty("connectionPool.enableSsl", true);
+        conf.setProperty("connectionPool.trustCertChainFile", "pem");
+        conf.setProperty("connectionPool.minSize", 100);
+        conf.setProperty("connectionPool.maxSize", 200);
+        conf.setProperty("connectionPool.minSimultaneousUsagePerConnection", 300);
+        conf.setProperty("connectionPool.maxSimultaneousUsagePerConnection", 400);
+        conf.setProperty("connectionPool.maxInProcessPerConnection", 500);
+        conf.setProperty("connectionPool.minInProcessPerConnection", 600);
+        conf.setProperty("connectionPool.maxWaitForConnection", 700);
+        conf.setProperty("connectionPool.maxContentLength", 800);
+        conf.setProperty("connectionPool.reconnectInterval", 900);
+        conf.setProperty("connectionPool.reconnectInitialDelay", 1000);
+        conf.setProperty("connectionPool.resultIterationBatchSize", 1100);
+        conf.setProperty("connectionPool.channelizer", "channelizer0");
+
+        final Settings settings = Settings.from(conf);
+
+        assertEquals(8000, settings.port);
+        assertEquals(16, settings.nioPoolSize);
+        assertEquals(32, settings.workerPoolSize);
+        assertEquals("user1", settings.username);
+        assertEquals("password1", settings.password);
+        assertEquals("JaasIt", settings.jaasEntry);
+        assertEquals("protocol0", settings.protocol);
+        assertEquals("my.serializers.MySerializer", settings.serializer.className);
+        assertEquals("thing", settings.serializer.config.get("any"));
+        assertEquals(true, settings.connectionPool.enableSsl);
+        assertEquals("pem", settings.connectionPool.trustCertChainFile);
+        assertEquals(100, settings.connectionPool.minSize);
+        assertEquals(200, settings.connectionPool.maxSize);
+        assertEquals(300, settings.connectionPool.minSimultaneousUsagePerConnection);
+        assertEquals(400, settings.connectionPool.maxSimultaneousUsagePerConnection);
+        assertEquals(500, settings.connectionPool.maxInProcessPerConnection);
+        assertEquals(600, settings.connectionPool.minInProcessPerConnection);
+        assertEquals(700, settings.connectionPool.maxWaitForConnection);
+        assertEquals(800, settings.connectionPool.maxContentLength);
+        assertEquals(900, settings.connectionPool.reconnectInterval);
+        assertEquals(1000, settings.connectionPool.reconnectInitialDelay);
+        assertEquals(1100, settings.connectionPool.resultIterationBatchSize);
+        assertEquals("channelizer0", settings.connectionPool.channelizer);
+    }
+}


Mime
View raw message