You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sf...@apache.org on 2015/02/17 01:28:06 UTC

[3/7] incubator-usergrid git commit: WriteCommit optimizations

WriteCommit optimizations


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

Branch: refs/heads/USERGRID-365
Commit: ab08b8d08a6f14546d43740aed7572d24d61934e
Parents: 302612c
Author: Shawn Feldman <sf...@apache.org>
Authored: Mon Feb 16 14:41:26 2015 -0800
Committer: Shawn Feldman <sf...@apache.org>
Committed: Mon Feb 16 15:06:18 2015 -0800

----------------------------------------------------------------------
 .../EntityCollectionManagerFactoryImpl.java     | 36 ++++++-------
 .../collection/mvcc/stage/write/WriteStart.java | 31 +++++------
 .../mvcc/stage/TestEntityGenerator.java         | 54 +++++++++++---------
 .../mvcc/stage/write/WriteStartTest.java        | 51 ++++++++++++++++--
 .../persistence/model/entity/Entity.java        |  5 ++
 5 files changed, 113 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ab08b8d0/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/impl/EntityCollectionManagerFactoryImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/impl/EntityCollectionManagerFactoryImpl.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/impl/EntityCollectionManagerFactoryImpl.java
index 0b9c643..816a585 100644
--- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/impl/EntityCollectionManagerFactoryImpl.java
+++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/impl/EntityCollectionManagerFactoryImpl.java
@@ -19,6 +19,22 @@
 
 package org.apache.usergrid.persistence.collection.impl;
 
+import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.netflix.astyanax.Keyspace;
+import org.apache.usergrid.persistence.collection.guice.CollectionTaskExecutor;
+import org.apache.usergrid.persistence.collection.guice.Write;
+import org.apache.usergrid.persistence.collection.guice.WriteUpdate;
+import org.apache.usergrid.persistence.collection.mvcc.MvccLogEntrySerializationStrategy;
+import org.apache.usergrid.persistence.collection.mvcc.stage.delete.MarkCommit;
+import org.apache.usergrid.persistence.collection.mvcc.stage.delete.MarkStart;
+import org.apache.usergrid.persistence.collection.serialization.UniqueValueSerializationStrategy;
+import org.apache.usergrid.persistence.core.guice.ProxyImpl;
+import org.apache.usergrid.persistence.core.task.TaskExecutor;
 
 import java.util.concurrent.ExecutionException;
 
@@ -31,30 +47,13 @@ import org.apache.usergrid.persistence.collection.EntityVersionCleanupFactory;
 import org.apache.usergrid.persistence.collection.EntityVersionCreatedFactory;
 import org.apache.usergrid.persistence.collection.cache.CachedEntityCollectionManager;
 import org.apache.usergrid.persistence.collection.cache.EntityCacheFig;
-import org.apache.usergrid.persistence.collection.guice.CollectionTaskExecutor;
-import org.apache.usergrid.persistence.collection.guice.Write;
-import org.apache.usergrid.persistence.collection.guice.WriteUpdate;
 import org.apache.usergrid.persistence.collection.mvcc.MvccEntitySerializationStrategy;
-import org.apache.usergrid.persistence.collection.mvcc.MvccLogEntrySerializationStrategy;
-import org.apache.usergrid.persistence.collection.mvcc.stage.delete.MarkCommit;
-import org.apache.usergrid.persistence.collection.mvcc.stage.delete.MarkStart;
 import org.apache.usergrid.persistence.collection.mvcc.stage.write.RollbackAction;
 import org.apache.usergrid.persistence.collection.mvcc.stage.write.WriteCommit;
 import org.apache.usergrid.persistence.collection.mvcc.stage.write.WriteOptimisticVerify;
 import org.apache.usergrid.persistence.collection.mvcc.stage.write.WriteStart;
 import org.apache.usergrid.persistence.collection.mvcc.stage.write.WriteUniqueVerify;
-import org.apache.usergrid.persistence.collection.serialization.SerializationFig;
-import org.apache.usergrid.persistence.collection.serialization.UniqueValueSerializationStrategy;
-import org.apache.usergrid.persistence.core.guice.ProxyImpl;
-import org.apache.usergrid.persistence.core.task.TaskExecutor;
 
-import com.google.common.base.Preconditions;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-import com.netflix.astyanax.Keyspace;
 
 
 /**
@@ -111,8 +110,7 @@ public class EntityCollectionManagerFactoryImpl implements EntityCollectionManag
                                                final MarkStart markStart, final MarkCommit markCommit, @ProxyImpl
                                                final MvccEntitySerializationStrategy entitySerializationStrategy,
                                                final UniqueValueSerializationStrategy uniqueValueSerializationStrategy,
-                                               final MvccLogEntrySerializationStrategy
-                                                   mvccLogEntrySerializationStrategy,
+                                               final MvccLogEntrySerializationStrategy mvccLogEntrySerializationStrategy,
                                                final Keyspace keyspace,
                                                final EntityVersionCleanupFactory entityVersionCleanupFactory,
                                                final EntityVersionCreatedFactory entityVersionCreatedFactory,

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ab08b8d0/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStart.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStart.java b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStart.java
index 700b5dd..8cd21e1 100644
--- a/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStart.java
+++ b/stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStart.java
@@ -28,7 +28,7 @@ import rx.functions.Func1;
 
 
 /**
- * This is the first stage and should be invoked immediately when a write is started.  It should 
+ * This is the first stage and should be invoked immediately when a write is started.  It should
  * persist the start of a new write in the data store for a checkpoint and recovery
  */
 @Singleton
@@ -70,25 +70,22 @@ public class WriteStart implements Func1<CollectionIoEvent<Entity>, CollectionIo
             MutationBatch write = logStrategy.write( collectionScope, startEntry );
 
             final MvccEntityImpl nextStage = new MvccEntityImpl( entityId, newVersion, status, entity );
-
-
-            try {
-                write.execute();
-            }
-            catch ( ConnectionException e ) {
-                LOG.error( "Failed to execute write ", e );
-                throw new WriteStartException( nextStage, collectionScope,
-                        "Failed to execute write ", e );
-            }
-            catch ( NullPointerException e) {
-                LOG.error( "Failed to execute write ", e );
-                throw new WriteStartException( nextStage, collectionScope,
+            if(ioEvent.getEvent().hasVersion()) {
+                try {
+                    write.execute();
+                } catch (ConnectionException e) {
+                    LOG.error("Failed to execute write ", e);
+                    throw new WriteStartException(nextStage, collectionScope,
+                        "Failed to execute write ", e);
+                } catch (NullPointerException e) {
+                    LOG.error("Failed to execute write ", e);
+                    throw new WriteStartException(nextStage, collectionScope,
                         "Failed to execute write", e);
+                }
             }
 
-
-          //create the mvcc entity for the next stage
-            //todo, we need to create a complete or partial update here (or sooner)
+            //create the mvcc entity for the next stage
+           //TODO: we need to create a complete or partial update here (or sooner)
 
             return new CollectionIoEvent<MvccEntity>( collectionScope, nextStage );
         }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ab08b8d0/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/TestEntityGenerator.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/TestEntityGenerator.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/TestEntityGenerator.java
index 576c1c6..4713f5a 100644
--- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/TestEntityGenerator.java
+++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/TestEntityGenerator.java
@@ -40,31 +40,33 @@ public class TestEntityGenerator {
 
     /**
      * Return an MvccEntityMock with valid inputs from the supplied entity
+     *
      * @param entity
      * @return
      */
-    public static MvccEntity fromEntity(Entity entity){
+    public static MvccEntity fromEntity(Entity entity) {
 
         final MvccEntity mvccEntity = mock(MvccEntity.class);
-        when(mvccEntity.getId()).thenReturn( entity.getId());
-        when(mvccEntity.getVersion()).thenReturn( entity.getVersion() );
-        when( mvccEntity.getEntity() ).thenReturn( Optional.of( entity ) );
+        when(mvccEntity.getId()).thenReturn(entity.getId());
+        when(mvccEntity.getVersion()).thenReturn(entity.getVersion());
+        when(mvccEntity.getEntity()).thenReturn(Optional.of(entity));
 
         return mvccEntity;
     }
 
     /**
      * Return an MvccEntityMock with valid inputs from the supplied entity
+     *
      * @param entity
      * @return
      */
-    public static MvccEntity fromEntityStatus(Entity entity,MvccEntity.Status status){
+    public static MvccEntity fromEntityStatus(Entity entity, MvccEntity.Status status) {
 
         final MvccEntity mvccEntity = mock(MvccEntity.class);
-        when(mvccEntity.getId()).thenReturn( entity.getId());
-        when(mvccEntity.getVersion()).thenReturn( entity.getVersion() );
-        when( mvccEntity.getEntity() ).thenReturn( Optional.of( entity ) );
-        when( mvccEntity.getStatus() ).thenReturn( status );
+        when(mvccEntity.getId()).thenReturn(entity.getId());
+        when(mvccEntity.getVersion()).thenReturn(entity.getVersion());
+        when(mvccEntity.getEntity()).thenReturn(Optional.of(entity));
+        when(mvccEntity.getStatus()).thenReturn(status);
 
         return mvccEntity;
     }
@@ -72,36 +74,38 @@ public class TestEntityGenerator {
 
     /**
      * Generate a valid entity
+     *
      * @return
      */
-    public static Entity generateEntity()  {
-            final Entity entity = new Entity( generateId());
-            final UUID version = UUIDGenerator.newTimeUUID();
+    public static Entity generateEntity() {
+        final Entity entity = new Entity(generateId());
+        final UUID version = UUIDGenerator.newTimeUUID();
 
-            EntityUtils.setVersion( entity, version );
+        EntityUtils.setVersion(entity, version);
 
-            return entity;
-        }
+        return entity;
+    }
 
     /**
-         * Generate a valid entity
-         * @return
-         */
-        public static Entity generateEntity(final Id id, final UUID version)  {
-                final Entity entity = new Entity(id);
-
-                EntityUtils.setVersion( entity, version );
+     * Generate a valid entity
+     *
+     * @return
+     */
+    public static Entity generateEntity(final Id id, final UUID version) {
+        final Entity entity = new Entity(id);
 
-                return entity;
-            }
+        EntityUtils.setVersion(entity, version);
 
+        return entity;
+    }
 
 
     /**
      * Generate an id with type "test" and a new time uuid
+     *
      * @return
      */
-    public static Id generateId(){
+    public static Id generateId() {
         return new SimpleId(UUIDGenerator.newTimeUUID(), "test");
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ab08b8d0/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStartTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStartTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStartTest.java
index e32c17c..a683d23 100644
--- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStartTest.java
+++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/mvcc/stage/write/WriteStartTest.java
@@ -18,6 +18,7 @@
 package org.apache.usergrid.persistence.collection.mvcc.stage.write;
 
 
+import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
@@ -34,11 +35,11 @@ import org.apache.usergrid.persistence.model.entity.Entity;
 
 import com.netflix.astyanax.MutationBatch;
 
+import java.util.UUID;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.same;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 
 /** @author tnine */
@@ -90,6 +91,50 @@ public class WriteStartTest extends AbstractEntityStageTest {
         assertSame( "Entity correct", entity, created.getEntity().get() );
     }
 
+    /** If no version then execute not called */
+    @Test
+    public void testNoVersion() throws Exception {
+
+        final CollectionScope context = mock( CollectionScope.class );
+
+        //mock returning a mock mutation when we do a log entry write
+        final MvccLogEntrySerializationStrategy logStrategy = mock( MvccLogEntrySerializationStrategy.class );
+
+
+        final ArgumentCaptor<MvccLogEntry> logEntry = ArgumentCaptor.forClass( MvccLogEntry.class );
+
+        final MutationBatch mutation = mock( MutationBatch.class );
+
+        final UUIDService uuidService = mock ( UUIDService.class );
+
+        when( logStrategy.write( same( context ), logEntry.capture() ) ).thenReturn( mutation );
+        when(mutation.execute()).thenThrow(new RuntimeException("Fail fail fail"));
+
+        //set up the mock to return the entity from the start phase
+        final Entity entity = TestEntityGenerator.generateEntity(new SimpleId(UUID.randomUUID(),"test"),null);
+        //run the stage
+        WriteStart newStage = new WriteStart( logStrategy, MvccEntity.Status.COMPLETE);
+
+        //verify the observable is correct
+        CollectionIoEvent<MvccEntity> result = newStage.call( new CollectionIoEvent<Entity>( context, entity ) );
+
+        verify(mutation,times(0)).execute();
+
+        //verify the log entry is correct
+        MvccLogEntry entry = logEntry.getValue();
+
+        assertEquals( "id correct", entity.getId(), entry.getEntityId() );
+        assertEquals( "EventStage is correct", Stage.ACTIVE, entry.getStage() );
+
+
+        MvccEntity created = result.getEvent();
+
+        //verify uuid and version in both the MvccEntity and the entity itself
+        //assertSame is used on purpose.  We want to make sure the same instance is used, not a copy.
+        //this way the caller's runtime type is retained.
+        assertSame( "id correct", entity.getId(), created.getId() );
+        assertSame( "Entity correct", entity, created.getEntity().get() );
+    }
 
     @Override
     protected void validateStage( final CollectionIoEvent<Entity> event ) {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ab08b8d0/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
index c5089dc..3addddb 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/Entity.java
@@ -131,4 +131,9 @@ public class Entity extends EntityObject {
                 ", version=" + version +
                 '}';
     }
+
+    public boolean hasVersion(){
+        return getVersion() != null;
+    }
+
 }