You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/04/17 16:05:14 UTC

incubator-tinkerpop git commit: Add test to validate that a GryoReader.Builder can be used from different threads.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 4fd887029 -> 8122b15ae


Add test to validate that a GryoReader.Builder can be used from different threads.

The tests seems to show that there isn't a problem doing this, so it's unclear what issues there were when doing this from hadoop.  Perhaps I'm not replicating the case exactly enough.


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

Branch: refs/heads/master
Commit: 8122b15ae1f12bd6fc22ace3aad8a1ff838fa012
Parents: 4fd8870
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Apr 17 10:03:31 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Apr 17 10:03:31 2015 -0400

----------------------------------------------------------------------
 .../gremlin/structure/io/gryo/GryoReader.java   |   2 +-
 .../gremlin/structure/io/gryo/GryoWriter.java   |   2 +
 .../structure/io/gryo/GryoReaderWriterTest.java | 102 +++++++++++++++++++
 3 files changed, 105 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8122b15a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReader.java
index af64872..5299d2f 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReader.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReader.java
@@ -51,7 +51,7 @@ import java.util.function.Function;
  * non-lossy in terms of Gremlin Structure to Gremlin Structure migrations (assuming both structure implementations
  * support the same graph features).
  * <br/>
- * This implementation is not thread-safe.
+ * This implementation is not thread-safe.  Have one {@code GryoReader} instance per thread.
  *
  * @author Stephen Mallette (http://stephen.genoprime.com)
  * @author Marko A. Rodriguez (http://markorodriguez.com)

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8122b15a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoWriter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoWriter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoWriter.java
index 042911b..cf82579 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoWriter.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoWriter.java
@@ -37,6 +37,8 @@ import java.util.Iterator;
  * The {@link GraphWriter} for the Gremlin Structure serialization format based on Kryo.  The format is meant to be
  * non-lossy in terms of Gremlin Structure to Gremlin Structure migrations (assuming both structure implementations
  * support the same graph features).
+ * <br/>
+ * This implementation is not thread-safe.  Have one {@code GraphWriter} instance per thread.
  *
  * @author Stephen Mallette (http://stephen.genoprime.com)
  */

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8122b15a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReaderWriterTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReaderWriterTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReaderWriterTest.java
new file mode 100644
index 0000000..2e39040
--- /dev/null
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoReaderWriterTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.structure.io.gryo;
+
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.io.GraphWriter;
+import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GryoReaderWriterTest {
+    @Test
+    public void shouldBeAbleToReUseBuilderInDifferentThreads() throws Exception {
+        final ByteArrayOutputStream os = new ByteArrayOutputStream();
+        final GraphWriter writer = GryoWriter.build().create();
+        writer.writeVertex(os, new DetachedVertex(1, "test", new HashMap<>()));
+        os.close();
+
+        final byte[] bytes = os.toByteArray();
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        final AtomicBoolean assertProcess1 = new AtomicBoolean(false);
+        final AtomicBoolean assertProcess2 = new AtomicBoolean(false);
+        final AtomicBoolean assertProcess3 = new AtomicBoolean(false);
+
+        // just one builder to re-use among threads
+        final GryoReader.Builder builder = GryoReader.build();
+
+        final Thread process1 = new Thread(() -> {
+            try {
+                latch.await();
+                final GryoReader reader = builder.create();
+                final Vertex v = reader.readVertex(new ByteArrayInputStream(bytes), dv -> dv);
+                assertProcess1.set(v.id().equals(1));
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        });
+
+        final Thread process2 = new Thread(() -> {
+            try {
+                latch.await();
+                final GryoReader reader = builder.create();
+                final Vertex v = reader.readVertex(new ByteArrayInputStream(bytes), dv -> dv);
+                assertProcess2.set(v.id().equals(1));
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        });
+
+        final Thread process3 = new Thread(() -> {
+            try {
+                latch.await();
+                final GryoReader reader = builder.create();
+                final Vertex v = reader.readVertex(new ByteArrayInputStream(bytes), dv -> dv);
+                assertProcess3.set(v.id().equals(1));
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        });
+
+        process1.start();
+        process2.start();
+        process3.start();
+
+        latch.countDown();
+
+        process3.join();
+        process2.join();
+        process1.join();
+
+        assertTrue(assertProcess1.get());
+        assertTrue(assertProcess2.get());
+        assertTrue(assertProcess3.get());
+    }
+}