keith-turner commented on a change in pull request #953: fixes #917 Added read 
locks
URL: https://github.com/apache/fluo/pull/953#discussion_r147250321
 
 

 ##########
 File path: 
modules/integration/src/test/java/org/apache/fluo/integration/impl/ReadLockIT.java
 ##########
 @@ -0,0 +1,548 @@
+/*
+ * 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.fluo.integration.impl;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Random;
+import java.util.Set;
+import java.util.function.Consumer;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import org.apache.accumulo.core.client.Scanner;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.fluo.accumulo.format.FluoFormatter;
+import org.apache.fluo.api.client.FluoClient;
+import org.apache.fluo.api.client.FluoFactory;
+import org.apache.fluo.api.client.Loader;
+import org.apache.fluo.api.client.LoaderExecutor;
+import org.apache.fluo.api.client.Snapshot;
+import org.apache.fluo.api.client.SnapshotBase;
+import org.apache.fluo.api.client.Transaction;
+import org.apache.fluo.api.client.TransactionBase;
+import org.apache.fluo.api.client.scanner.CellScanner;
+import org.apache.fluo.api.config.FluoConfiguration;
+import org.apache.fluo.api.data.Column;
+import org.apache.fluo.api.data.RowColumn;
+import org.apache.fluo.api.data.RowColumnValue;
+import org.apache.fluo.api.data.Span;
+import org.apache.fluo.api.exceptions.AlreadySetException;
+import org.apache.fluo.api.exceptions.CommitException;
+import org.apache.fluo.integration.ITBaseImpl;
+import org.apache.fluo.integration.TestTransaction;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static java.util.Arrays.asList;
+
+public class ReadLockIT extends ITBaseImpl {
+
+  private static final Column ALIAS_COL = new Column("node", "alias");
+
+  private void addEdge(String node1, String node2) {
+    try (Transaction tx = client.newTransaction()) {
+      addEdge(tx, node1, node2);
+      tx.commit();
+    }
+  }
+
+  static void addEdge(TransactionBase tx, String node1, String node2) {
+    Map<String, Map<Column, String>> aliases =
+        tx.withReadLock().gets(asList("r:" + node1, "r:" + node2), ALIAS_COL);
+    String alias1 = aliases.get("r:" + node1).get(ALIAS_COL);
+    String alias2 = aliases.get("r:" + node2).get(ALIAS_COL);
+
+    addEdge(tx, node1, node2, alias1, alias2);
+  }
+
+  static void addEdge(TransactionBase tx, String node1, String node2, String 
alias1,
+      String alias2) {
+    tx.set("d:" + alias1 + ":" + alias2, new Column("edge", node1 + ":" + 
node2), "");
+    tx.set("d:" + alias2 + ":" + alias1, new Column("edge", node2 + ":" + 
node1), "");
+
+    tx.set("r:" + node1 + ":" + node2, new Column("edge", "aliases"), alias1 + 
":" + alias2);
+    tx.set("r:" + node2 + ":" + node1, new Column("edge", "aliases"), alias2 + 
":" + alias1);
+  }
+
+  static void setAlias(TransactionBase tx, String node, String alias) {
+    tx.set("r:" + node, new Column("node", "alias"), alias);
+
+    CellScanner scanner = tx.scanner().over(Span.prefix("r:" + node + 
":")).build();
+
+    for (RowColumnValue rcv : scanner) {
+      String otherNode = rcv.getsRow().split(":")[2];
+      String[] aliases = rcv.getsValue().split(":");
+
+      if (aliases.length != 2) {
+        throw new RuntimeException("bad alias " + rcv);
+      }
+
+      if (!alias.equals(aliases[0])) {
+        tx.delete("d:" + aliases[0] + ":" + aliases[1], new Column("edge", 
node + ":" + otherNode));
+        tx.delete("d:" + aliases[1] + ":" + aliases[0], new Column("edge", 
otherNode + ":" + node));
+
+        addEdge(tx, node, otherNode, alias, aliases[1]);
+      }
+    }
+  }
+
+  @Test
+  public void testConcurrentReadlocks() throws Exception {
+
+    try (Transaction tx = client.newTransaction()) {
+      setAlias(tx, "node1", "bob");
+      setAlias(tx, "node2", "joe");
+      setAlias(tx, "node3", "alice");
+      tx.commit();
+    }
+
+
+    TestTransaction tx1 = new TestTransaction(env);
+    setAlias(tx1, "node2", "jojo");
+
+    TestTransaction tx2 = new TestTransaction(env);
+    TestTransaction tx3 = new TestTransaction(env);
+
+    addEdge(tx2, "node1", "node2");
+    addEdge(tx3, "node1", "node3");
+
+    tx2.commit();
+    tx2.close();
+
+    tx3.commit();
+    tx3.close();
+
+    Assert.assertEquals(ImmutableSet.of("bob:joe", "joe:bob", "bob:alice", 
"alice:bob"),
+        getDerivedEdges());
+
+    try {
+      tx1.commit();
+      Assert.fail("Expected exception");
+    } catch (CommitException ce) {
+    }
+
+    Assert.assertEquals(ImmutableSet.of("bob:joe", "joe:bob", "bob:alice", 
"alice:bob"),
+        getDerivedEdges());
+  }
+
+  @Test
+  public void testWriteCausesReadLockToFail() throws Exception {
+    try (Transaction tx = client.newTransaction()) {
+      setAlias(tx, "node1", "bob");
+      setAlias(tx, "node2", "joe");
+      tx.commit();
+    }
+
+    TestTransaction tx1 = new TestTransaction(env);
+    setAlias(tx1, "node2", "jojo");
+
+    TestTransaction tx2 = new TestTransaction(env);
+
+    addEdge(tx2, "node1", "node2");
+
+    tx1.commit();
+    tx1.close();
+
+    Assert.assertEquals(0, getDerivedEdges().size());
+
+    try {
+      tx2.commit();
+      Assert.fail("Expected exception");
+    } catch (CommitException ce) {
+    }
+
+    // ensure the failed read lock on node1 is cleaned up
+    try (Transaction tx = client.newTransaction()) {
+      setAlias(tx, "node1", "fred");
+      tx.commit();
+    }
+
+    try (Transaction tx = client.newTransaction()) {
+      addEdge(tx, "node1", "node2");
+      tx.commit();
+    }
+
+    Assert.assertEquals(ImmutableSet.of("fred:jojo", "jojo:fred"), 
getDerivedEdges());
+  }
+
+  private void dumpRow(String row, Consumer<String> out) throws 
TableNotFoundException {
+    Scanner scanner = conn.createScanner(getCurTableName(), 
Authorizations.EMPTY);
+    scanner.setRange(Range.exact(row));
+    for (Entry<Key, Value> entry : scanner) {
+      out.accept(FluoFormatter.toString(entry));
+    }
+  }
+
+  private void dumpTable(Consumer<String> out) throws TableNotFoundException {
+    Scanner scanner = conn.createScanner(getCurTableName(), 
Authorizations.EMPTY);
+    for (Entry<Key, Value> entry : scanner) {
+      out.accept(FluoFormatter.toString(entry));
+    }
+  }
+
+  @Test
+  public void testWriteAfterReadLock() throws Exception {
+    try (Transaction tx = client.newTransaction()) {
+      setAlias(tx, "node1", "bob");
+      setAlias(tx, "node2", "joe");
+      setAlias(tx, "node3", "alice");
+
+      tx.commit();
+    }
+
+    addEdge("node1", "node2");
+    Assert.assertEquals(ImmutableSet.of("bob:joe", "joe:bob"), 
getDerivedEdges());
+
+    try (Transaction tx = client.newTransaction()) {
+      setAlias(tx, "node1", "bobby");
+      tx.commit();
+    }
+
+    addEdge("node1", "node3");
+    Assert.assertEquals(ImmutableSet.of("bobby:joe", "joe:bobby", 
"bobby:alice", "alice:bobby"),
+        getDerivedEdges());
+  }
+
+  @Test
+  public void testRandom() throws Exception {
+    int numNodes = 100;
+    int numEdges = 1000;
+    int numAliasChanges = 25;
+
+    Random rand = new Random();
+
+    Map<String, String> nodes = new HashMap<>();
+    while (nodes.size() < numNodes) {
+      nodes.put(String.format("n-%09d", rand.nextInt(1000000000)),
+          String.format("a-%09d", rand.nextInt(1000000000)));
+    }
+
+    List<String> nodesList = new ArrayList<>(nodes.keySet());
+    Set<String> edges = new HashSet<>();
+    while (edges.size() < numEdges) {
+      String n1 = nodesList.get(rand.nextInt(nodesList.size()));
+      String n2 = nodesList.get(rand.nextInt(nodesList.size()));
+      if (n1.equals(n2) || edges.contains(n2 + ":" + n1)) {
+        continue;
+      }
+
+      edges.add(n1 + ":" + n2);
+    }
+
+    try (LoaderExecutor le = client.newLoaderExecutor()) {
+      for (Entry<String, String> entry : nodes.entrySet()) {
+        le.execute((tx, ctx) -> setAlias(tx, entry.getKey(), 
entry.getValue()));
+      }
+    }
+
+    List<Loader> loadOps = new ArrayList<>();
+    for (String edge : edges) {
+      String[] enodes = edge.split(":");
+      loadOps.add((tx, ctx) -> {
+        try {
+          addEdge(tx, enodes[0], enodes[1]);
+        } catch (NullPointerException e) {
 
 Review comment:
   yeah I need to remove that.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to