Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic graphs #1

Open
wants to merge 4 commits into
base: 3_2_3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions giraph-gremlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>giraph-gremlin</artifactId>
<name>Apache TinkerPop :: Giraph Gremlin</name>
Expand Down Expand Up @@ -243,4 +243,4 @@ limitations under the License.
</plugin>
</plugins>
</build>
</project>
</project>
4 changes: 2 additions & 2 deletions gremlin-archetype/gremlin-archetype-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-archetype</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>

<artifactId>gremlin-archetype-server</artifactId>
Expand Down Expand Up @@ -56,4 +56,4 @@ limitations under the License.
</resource>
</resources>
</build>
</project>
</project>
4 changes: 2 additions & 2 deletions gremlin-archetype/gremlin-archetype-tinkergraph/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-archetype</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>

<artifactId>gremlin-archetype-tinkergraph</artifactId>
Expand Down Expand Up @@ -56,4 +56,4 @@ limitations under the License.
</resource>
</resources>
</build>
</project>
</project>
4 changes: 2 additions & 2 deletions gremlin-archetype/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<artifactId>tinkerpop</artifactId>
<groupId>org.apache.tinkerpop</groupId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>

<artifactId>gremlin-archetype</artifactId>
Expand Down Expand Up @@ -67,4 +67,4 @@ limitations under the License.
</plugins>
</pluginManagement>
</build>
</project>
</project>
2 changes: 1 addition & 1 deletion gremlin-benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<artifactId>tinkerpop</artifactId>
<groupId>org.apache.tinkerpop</groupId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>

<artifactId>gremlin-benchmark</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<artifactId>tinkerpop</artifactId>
<groupId>org.apache.tinkerpop</groupId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-console</artifactId>
<name>Apache TinkerPop :: Gremlin Console</name>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-core</artifactId>
<name>Apache TinkerPop :: Gremlin Core</name>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-driver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-driver</artifactId>
<name>Apache TinkerPop :: Gremlin Driver</name>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-groovy-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-groovy-test</artifactId>
<name>Apache TinkerPop :: Gremlin Groovy Test</name>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-groovy</artifactId>
<name>Apache TinkerPop :: Gremlin Groovy</name>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-python/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-python</artifactId>
<name>Apache TinkerPop :: Gremlin Python</name>
Expand Down
2 changes: 1 addition & 1 deletion gremlin-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
<parent>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkerpop</artifactId>
<version>3.2.3</version>
<version>3.2.3_JANUSGRAPH_IBM</version>
</parent>
<artifactId>gremlin-server</artifactId>
<name>Apache TinkerPop :: Gremlin Server</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.tinkerpop.gremlin.server.handler.IteratorHandler;
import org.apache.tinkerpop.gremlin.server.handler.OpExecutorHandler;
import org.apache.tinkerpop.gremlin.server.handler.OpSelectorHandler;
import org.apache.tinkerpop.gremlin.structure.Graph;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
Expand All @@ -53,6 +54,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Stream;
import java.util.Iterator;

/**
* A base implementation for the {@code Channelizer} which does a basic configuration of the pipeline, one that
Expand Down Expand Up @@ -179,8 +181,12 @@ private void configureSerializers() {
}

final MessageSerializer serializer = (MessageSerializer) clazz.newInstance();
final Map<String, Graph> graphsDefinedAtStartup = new HashMap<String, Graph>();
for (String graphName : settings.graphs.keySet()) {
graphsDefinedAtStartup.put(graphName, graphManager.getGraph(graphName));
}
if (config.config != null)
serializer.configure(config.config, graphManager.getGraphs());
serializer.configure(config.config, graphsDefinedAtStartup);

return Optional.ofNullable(serializer);
} catch (ClassNotFoundException cnfe) {
Expand Down Expand Up @@ -252,4 +258,4 @@ private SslContext createSSLContext(final Settings settings) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,139 +21,98 @@
import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.Bindings;
import javax.script.SimpleBindings;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
* Holder for {@link Graph} and {@link TraversalSource} instances configured for the server to be passed to script
* engine bindings. The {@link Graph} instances are read from the {@link Settings} for Gremlin Server as defined in
* the configuration file. The {@link TraversalSource} instances are rebound to the {@code GraphManager} once
* initialization scripts construct them.
*/
public final class GraphManager {
private static final Logger logger = LoggerFactory.getLogger(GremlinServer.class);

private final Map<String, Graph> graphs = new ConcurrentHashMap<>();
private final Map<String, TraversalSource> traversalSources = new ConcurrentHashMap<>();
import java.util.function.Supplier;

public interface GraphManager {
/**
* Create a new instance using the {@link Settings} from Gremlin Server.
* Get a list of the {@link Graph} instances and their binding names
*
* @return a {@link Map} where the key is the name of the {@link Graph} and the value is the {@link Graph} itself
*/
public GraphManager(final Settings settings) {
settings.graphs.entrySet().forEach(e -> {
try {
final Graph newGraph = GraphFactory.open(e.getValue());
graphs.put(e.getKey(), newGraph);
logger.info("Graph [{}] was successfully configured via [{}].", e.getKey(), e.getValue());
} catch (RuntimeException re) {
logger.warn(String.format("Graph [%s] configured at [%s] could not be instantiated and will not be available in Gremlin Server. GraphFactory message: %s",
e.getKey(), e.getValue(), re.getMessage()), re);
if (re.getCause() != null) logger.debug("GraphFactory exception", re.getCause());
}
});
}
public Map<String, Graph> getGraphs();

/**
* Get a list of the {@link Graph} instances and their binding names as defined in the Gremlin Server
* configuration file.
* Get {@link Graph} instance whose name matches {@link gName}
*
* @return a {@link Map} where the key is the name of the {@link Graph} and the value is the {@link Graph} itself
* @return {@link Graph} if exists, else null
*/
public Map<String, Graph> getGraphs() {
return graphs;
}
public Graph getGraph(final String gName);

/**
* Get a list of the {@link TraversalSource} instances and their binding names as defined by Gremlin Server
* initialization scripts.
* Add {@link Graph} g with name {@link String} gName to
* {@link Map<String, Graph>} returned by call to getGraphs()
*/
public void addGraph(final String gName, final Graph g);

/**
* Get a list of the {@link TraversalSource} instances and their binding names
*
* @return a {@link Map} where the key is the name of the {@link TraversalSource} and the value is the
* {@link TraversalSource} itself
*/
public Map<String, TraversalSource> getTraversalSources() {
return traversalSources;
}
public Map<String, TraversalSource> getTraversalSources();

/**
* Get {@link TraversalSource} instance whose name matches {@link tsName}
*
* @return {@link TraversalSource} if exists, else null
*/

public TraversalSource getTraversalSource(final String tsName);
/**
* Get the {@link Graph} and {@link TraversalSource} list as a set of bindings.
*/
public Bindings getAsBindings() {
final Bindings bindings = new SimpleBindings();
graphs.forEach(bindings::put);
traversalSources.forEach(bindings::put);
return bindings;
}

/**
* Add {@link TraversalSource} ts with name {@link String} tsName to
* {@link Map<String, TraversalSource>} returned by call to getTraversalSources()
*/
public void addTraversalSource(final String tsName, final TraversalSource ts);

public Bindings getAsBindings();

/**
* Rollback transactions across all {@link Graph} objects.
*/
public void rollbackAll() {
graphs.entrySet().forEach(e -> {
final Graph graph = e.getValue();
if (graph.features().graph().supportsTransactions() && graph.tx().isOpen())
graph.tx().rollback();
});
}
public void rollbackAll();

/**
* Selectively rollback transactions on the specified graphs or the graphs of traversal sources.
*/
public void rollback(final Set<String> graphSourceNamesToCloseTxOn) {
closeTx(graphSourceNamesToCloseTxOn, Transaction.Status.ROLLBACK);
}
public void rollback(final Set<String> graphSourceNamesToCloseTxOn);

/**
* Commit transactions across all {@link Graph} objects.
*/
public void commitAll() {
graphs.entrySet().forEach(e -> {
final Graph graph = e.getValue();
if (graph.features().graph().supportsTransactions() && graph.tx().isOpen())
graph.tx().commit();
});
}
public void commitAll();

/**
* Selectively commit transactions on the specified graphs or the graphs of traversal sources.
*/
public void commit(final Set<String> graphSourceNamesToCloseTxOn) {
closeTx(graphSourceNamesToCloseTxOn, Transaction.Status.COMMIT);
}
public void commit(final Set<String> graphSourceNamesToCloseTxOn);

/**
* Implementation that allows for custom graph-opening implementations; if the {@link Map}
* tracking graph references has a {@link Graph} object corresponding to the {@link String} graphName,
* then we return that {@link Graph}-- otherwise, we use the custom {@link Supplier} to instantiate a
* a new {@link Graph}, add it to the {@link Map} tracking graph references, and return said {@link Graph}.
*/
public Graph openGraph(final String graphName, final Supplier<Graph> supplier);

/**
* Implementation that allows for custom graph-closing implementations; it is up to the implementor
* to decide if this method should also remove the {@link Graph} graph from the {@link Map}
* tracking {@link Graph} references (and how it would do so).
*/
public void closeGraph(final Graph graph) throws Exception;

/**
* Selectively close transactions on the specified graphs or the graphs of traversal sources.
* Remove {@link Graph} corresponding to {@link String} graphName from
* {@link Map} tracking graph references.
*/
private void closeTx(final Set<String> graphSourceNamesToCloseTxOn, final Transaction.Status tx) {
final Set<Graph> graphsToCloseTxOn = new HashSet<>();

// by the time this method has been called, it should be validated that the source/graph is present.
// might be possible that it could have been removed dynamically, but that i'm not sure how one would do
// that as of right now unless they were embedded in which case they'd need to know what they were doing
// anyway
graphSourceNamesToCloseTxOn.forEach(r -> {
if (graphs.containsKey(r))
graphsToCloseTxOn.add(graphs.get(r));
else
graphsToCloseTxOn.add(traversalSources.get(r).getGraph());
});

graphsToCloseTxOn.forEach(graph -> {
if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) {
if (tx == Transaction.Status.COMMIT)
graph.tx().commit();
else
graph.tx().rollback();
}
});
}
}
public void removeGraph(final String graphName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.tinkerpop.gremlin.server.auth.AllowAllAuthenticator;
import org.apache.tinkerpop.gremlin.server.auth.Authenticator;
import org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer;
import org.apache.tinkerpop.gremlin.server.util.DefaultGraphManager;
import info.ganglia.gmetric4j.gmetric.GMetric;
import org.apache.tinkerpop.gremlin.server.util.LifeCycleHook;
import org.apache.tinkerpop.gremlin.structure.Graph;
Expand Down Expand Up @@ -172,6 +173,11 @@ public Settings() {
*/
public String channelizer = WebSocketChannelizer.class.getName();

/**
* The full class name of the {@link GraphManager} to use in Gremlin Server.
*/
public String graphManager = DefaultGraphManager.class.getName();

/**
* Configured metrics for Gremlin Server.
*/
Expand Down
Loading