usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [2/4] git commit: Cleaned up cassandra write timestamps
Date Thu, 13 Feb 2014 04:20:53 GMT
Cleaned up cassandra write timestamps


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

Branch: refs/heads/two-dot-o
Commit: d312b31657cf78045021f2918d9bc22cd99c3b50
Parents: 85ffe7b
Author: Todd Nine <tnine@apigee.com>
Authored: Tue Feb 11 18:09:46 2014 -0700
Committer: Todd Nine <tnine@apigee.com>
Committed: Tue Feb 11 18:09:46 2014 -0700

----------------------------------------------------------------------
 .../graph/serialization/impl/CassUtils.java     |  11 +-
 .../impl/EdgeMetadataSerializationImpl.java     |   6 +-
 .../EdgeMetadataSerializationTest.java          | 129 +++++++++++++++++--
 3 files changed, 122 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d312b316/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/CassUtils.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/CassUtils.java
b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/CassUtils.java
index 7d6c70e..f49d832 100644
--- a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/CassUtils.java
+++ b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/CassUtils.java
@@ -34,18 +34,9 @@ public class CassUtils {
      * @param version
      * @return
      */
-    public static long getDeleteTimestamp(UUID version){
-//     return version.timestamp()+1;
+    public static long getTimestamp( UUID version ){
         return version.timestamp();
     }
 
 
-    /**
-     * Get the write timestamp from a version
-     * @param version
-     * @return
-     */
-    public static long getWriteTimestamp(UUID version){
-        return version.timestamp();
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d312b316/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeMetadataSerializationImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeMetadataSerializationImpl.java
b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeMetadataSerializationImpl.java
index c2aadb2..ed3cd0b 100644
--- a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeMetadataSerializationImpl.java
+++ b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeMetadataSerializationImpl.java
@@ -134,7 +134,7 @@ public class EdgeMetadataSerializationImpl implements EdgeMetadataSerialization,
         final Id source = edge.getSourceNode();
         final Id target = edge.getTargetNode();
         final String edgeType = edge.getType();
-        final long timestamp = CassUtils.getWriteTimestamp( edge.getVersion() );
+        final long timestamp = CassUtils.getTimestamp( edge.getVersion() );
 
 
         //add source->target edge type to meta data
@@ -213,7 +213,7 @@ public class EdgeMetadataSerializationImpl implements EdgeMetadataSerialization,
                                           final UUID version,
                                           final MultiTennantColumnFamily<OrganizationScope,
Id, String> cf ) {
 
-        final long timestamp = CassUtils.getDeleteTimestamp( version );
+        final long timestamp = CassUtils.getTimestamp( version );
 
 
         //write target<--source edge type meta data
@@ -248,7 +248,7 @@ public class EdgeMetadataSerializationImpl implements EdgeMetadataSerialization,
         MutationBatch batch = keyspace.prepareMutationBatch();
 
 
-        final long timestamp = CassUtils.getDeleteTimestamp( version );
+        final long timestamp = CassUtils.getTimestamp( version );
 
 
         //write target<--source edge type and id type to meta data

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/d312b316/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
index 0600119..425fb01 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
@@ -21,6 +21,7 @@ package org.apache.usergrid.persistence.graph.serialization;
 
 
 import java.util.Iterator;
+import java.util.UUID;
 
 import org.jukito.JukitoRunner;
 import org.jukito.UseModules;
@@ -38,8 +39,15 @@ import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 
+import com.fasterxml.uuid.UUIDComparator;
 import com.google.inject.Inject;
+import com.netflix.astyanax.Keyspace;
+import com.netflix.astyanax.MutationBatch;
 import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
+import com.netflix.astyanax.connectionpool.exceptions.NotFoundException;
+import com.netflix.astyanax.model.Column;
+import com.netflix.astyanax.model.ColumnFamily;
+import com.netflix.astyanax.serializers.StringSerializer;
 
 import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createEdge;
 import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createId;
@@ -47,6 +55,7 @@ import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.crea
 import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createSearchIdType;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -55,8 +64,8 @@ import static org.mockito.Mockito.when;
  *
  *
  */
-@RunWith( JukitoRunner.class )
-@UseModules( { TestGraphModule.class } )
+@RunWith(JukitoRunner.class)
+@UseModules({ TestGraphModule.class })
 public class EdgeMetadataSerializationTest {
 
 
@@ -72,6 +81,10 @@ public class EdgeMetadataSerializationTest {
     @Inject
     protected EdgeMetadataSerialization serialization;
 
+    @Inject
+    protected Keyspace keyspace;
+
+
     protected OrganizationScope scope;
 
 
@@ -179,8 +192,8 @@ public class EdgeMetadataSerializationTest {
         serialization.writeEdge( scope, edge4 ).execute();
 
         //now check we get both types back
-        Iterator<String> types = serialization.getIdTypesFromSource( scope,
-                createSearchIdType( sourceId, "edge", null ) );
+        Iterator<String> types =
+                serialization.getIdTypesFromSource( scope, createSearchIdType( sourceId,
"edge", null ) );
 
         assertEquals( "target", types.next() );
         assertEquals( "target2", types.next() );
@@ -225,8 +238,8 @@ public class EdgeMetadataSerializationTest {
         serialization.writeEdge( scope, edge4 ).execute();
 
         //now check we get both types back
-        Iterator<String> types = serialization.getIdTypesToTarget( scope, createSearchIdType(
targetId, "edge",
-                null ) );
+        Iterator<String> types =
+                serialization.getIdTypesToTarget( scope, createSearchIdType( targetId, "edge",
null ) );
 
         assertEquals( "source", types.next() );
         assertEquals( "source2", types.next() );
@@ -354,7 +367,6 @@ public class EdgeMetadataSerializationTest {
     }
 
 
-
     /**
      * Test write and read edge types from source -> target
      */
@@ -374,8 +386,8 @@ public class EdgeMetadataSerializationTest {
         serialization.writeEdge( scope, edge3 ).execute();
 
         //now check we get both types back
-        Iterator<String> edges = serialization.getIdTypesFromSource( scope,
-                createSearchIdType( sourceId, "edge", null ) );
+        Iterator<String> edges =
+                serialization.getIdTypesFromSource( scope, createSearchIdType( sourceId,
"edge", null ) );
 
         assertEquals( "target", edges.next() );
         assertEquals( "target2", edges.next() );
@@ -429,8 +441,8 @@ public class EdgeMetadataSerializationTest {
 
 
         //now check we get both types back
-        Iterator<String> edges = serialization.getIdTypesToTarget( scope, createSearchIdType(
targetId, "edge",
-                null ) );
+        Iterator<String> edges =
+                serialization.getIdTypesToTarget( scope, createSearchIdType( targetId, "edge",
null ) );
 
         assertEquals( "source", edges.next() );
         assertEquals( "source2", edges.next() );
@@ -461,4 +473,99 @@ public class EdgeMetadataSerializationTest {
 
         assertFalse( edges.hasNext() );
     }
+
+
+    @Test
+    public void validateDeleteCollision() throws ConnectionException {
+
+
+        final String CF_NAME = "test";
+        final StringSerializer STR_SER = StringSerializer.get();
+
+        ColumnFamily<String, String> testCf = new ColumnFamily<String, String>(
CF_NAME, STR_SER, STR_SER );
+        keyspace.createColumnFamily( testCf, null );
+
+
+        final String key = "key";
+        final String colname = "name";
+        final String colvalue = "value";
+
+        UUID firstUUID = UUIDGenerator.newTimeUUID();
+
+        UUID secondUUID = UUIDGenerator.newTimeUUID();
+
+        UUID thirdUUID = UUIDGenerator.newTimeUUID();
+
+        assertTrue( "First before second", UUIDComparator.staticCompare( firstUUID, secondUUID
) < 0 );
+
+        assertTrue( "Second before third", UUIDComparator.staticCompare( secondUUID, thirdUUID
) < 0 );
+
+        MutationBatch batch = keyspace.prepareMutationBatch();
+
+        batch.withRow( testCf, key ).setTimestamp( firstUUID.timestamp() ).putColumn( colname,
colvalue );
+
+        batch.execute();
+
+        //now read it back to validate
+
+        Column<String> col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn(
colname ).execute().getResult();
+
+        assertEquals( colname, col.getName() );
+        assertEquals( colvalue, col.getStringValue() );
+
+        //now issue a write and a delete with the same timestamp, write will win
+
+        batch = keyspace.prepareMutationBatch();
+        batch.withRow( testCf, key ).setTimestamp( firstUUID.timestamp() ).putColumn( colname,
colvalue );
+        batch.withRow( testCf, key ).setTimestamp( firstUUID.timestamp() ).deleteColumn(
colname );
+        batch.execute();
+
+        boolean deleted = false;
+
+        try {
+            keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult();
+        }
+        catch ( NotFoundException nfe ) {
+            deleted = true;
+        }
+
+        assertTrue( deleted );
+
+        //ensure that if we have a latent write, it won't overwrite a newer value
+        batch.withRow( testCf, key ).setTimestamp( secondUUID.timestamp() ).putColumn( colname,
colvalue );
+        batch.execute();
+
+        col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult();
+
+        assertEquals( colname, col.getName() );
+        assertEquals( colvalue, col.getStringValue() );
+
+        //now issue a delete with the first timestamp, column should still be present
+        batch = keyspace.prepareMutationBatch();
+        batch.withRow( testCf, key ).setTimestamp( firstUUID.timestamp() ).deleteColumn(
colname );
+        batch.execute();
+
+
+        col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult();
+
+        assertEquals( colname, col.getName() );
+        assertEquals( colvalue, col.getStringValue() );
+
+        //now delete it with the 3rd timestamp, it should disappear
+
+        batch = keyspace.prepareMutationBatch();
+        batch.withRow( testCf, key ).setTimestamp( thirdUUID.timestamp() ).deleteColumn(
colname );
+        batch.execute();
+
+        deleted = false;
+
+        try {
+            keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult();
+        }
+        catch ( NotFoundException nfe ) {
+            deleted = true;
+        }
+
+        assertTrue( deleted );
+    }
 }


Mime
View raw message