usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [33/50] [abbrv] incubator-usergrid git commit: add get by name timers
Date Fri, 24 Jul 2015 15:22:01 GMT
add get by name timers


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

Branch: refs/heads/USERGRID-869
Commit: 75d59e7c06636ea8ee5d66c92484e0b87ddebf2b
Parents: 23ce4c5
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Tue Jul 21 14:03:31 2015 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Tue Jul 21 14:03:31 2015 -0600

----------------------------------------------------------------------
 .../usergrid/services/AbstractService.java      | 292 +++++++++++--------
 1 file changed, 173 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/75d59e7c/stack/services/src/main/java/org/apache/usergrid/services/AbstractService.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/AbstractService.java
b/stack/services/src/main/java/org/apache/usergrid/services/AbstractService.java
index edf8ab2..660545e 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/AbstractService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/AbstractService.java
@@ -26,6 +26,9 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
+import com.codahale.metrics.Timer;
+import org.apache.usergrid.persistence.core.metrics.MetricsFactory;
+import org.apache.usergrid.persistence.core.metrics.ObservableTimer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
@@ -98,8 +101,15 @@ public abstract class AbstractService implements Service {
 
     private Scheduler rxScheduler;
     private RxSchedulerFig rxSchedulerFig;
-
-
+    private MetricsFactory metricsFactory;
+    private Timer entityGetTimer;
+    private Timer entitiesGetTimer;
+    private Timer entitiesParallelGetTimer;
+    private Timer deleteByNameTimer;
+    private Timer postByNameTimer;
+    private Timer putByNameTimer;
+    private Timer getByNameTimer;
+    private Timer headByNameTimer;
 
 
     public AbstractService() {
@@ -112,7 +122,16 @@ public abstract class AbstractService implements Service {
         em = sm.getEntityManager();
         final Injector injector = sm.getApplicationContext().getBean( Injector.class );
         rxScheduler = injector.getInstance( RxTaskScheduler.class ).getAsyncIOScheduler();
-        rxSchedulerFig = injector.getInstance( RxSchedulerFig.class );
+        rxSchedulerFig = injector.getInstance(RxSchedulerFig.class);
+        metricsFactory = injector.getInstance(MetricsFactory.class);
+        this.entityGetTimer = metricsFactory.getTimer(this.getClass(), "importEntity.get");
+        this.entitiesGetTimer = metricsFactory.getTimer(this.getClass(), "importEntities.get");
+        this.entitiesParallelGetTimer = metricsFactory.getTimer( this.getClass(),"importEntitiesP.get"
);
+        this.getByNameTimer = metricsFactory.getTimer( this.getClass(),"byname.get" );
+        this.putByNameTimer = metricsFactory.getTimer( this.getClass(),"byname.put" );
+        this.postByNameTimer = metricsFactory.getTimer( this.getClass(),"byname.post" );
+        this.deleteByNameTimer = metricsFactory.getTimer(this.getClass(), "byname.delete");
+        this.headByNameTimer = metricsFactory.getTimer(this.getClass(), "byname.head");
     }
 
 
@@ -170,9 +189,9 @@ public abstract class AbstractService implements Service {
             return false;
         }
         if ( "application".equals( context.getOwner().getType() ) ) {
-            return Schema.getDefaultSchema().isCollectionReversed( "application", pluralize(
info.getItemType() ) );
+            return Schema.getDefaultSchema().isCollectionReversed("application", pluralize(info.getItemType()));
         }
-        return Schema.getDefaultSchema().isCollectionReversed( info.getContainerType(), info.getCollectionName()
);
+        return Schema.getDefaultSchema().isCollectionReversed(info.getContainerType(), info.getCollectionName());
     }
 
 
@@ -181,9 +200,9 @@ public abstract class AbstractService implements Service {
             return null;
         }
         if ( "application".equals( context.getOwner().getType() ) ) {
-            return Schema.getDefaultSchema().getCollectionSort( "application", pluralize(
info.getItemType() ) );
+            return Schema.getDefaultSchema().getCollectionSort("application", pluralize(info.getItemType()));
         }
-        return Schema.getDefaultSchema().getCollectionSort( info.getContainerType(), info.getCollectionName()
);
+        return Schema.getDefaultSchema().getCollectionSort(info.getContainerType(), info.getCollectionName());
     }
 
 
@@ -191,7 +210,7 @@ public abstract class AbstractService implements Service {
         if ( privateConnections == null ) {
             privateConnections = new LinkedHashSet<String>();
         }
-        privateConnections.add( connection );
+        privateConnections.add(connection);
     }
 
 
@@ -199,7 +218,7 @@ public abstract class AbstractService implements Service {
         if ( privateConnections == null ) {
             privateConnections = new LinkedHashSet<String>();
         }
-        privateConnections.addAll( connections );
+        privateConnections.addAll(connections);
     }
 
 
@@ -207,7 +226,7 @@ public abstract class AbstractService implements Service {
         if ( declaredConnections == null ) {
             declaredConnections = new LinkedHashSet<String>();
         }
-        declaredConnections.add( connection );
+        declaredConnections.add(connection);
     }
 
 
@@ -215,7 +234,7 @@ public abstract class AbstractService implements Service {
         if ( declaredConnections == null ) {
             declaredConnections = new LinkedHashSet<String>();
         }
-        declaredConnections.addAll( connections );
+        declaredConnections.addAll(connections);
     }
 
 
@@ -223,7 +242,7 @@ public abstract class AbstractService implements Service {
         if ( privateCollections == null ) {
             privateCollections = new LinkedHashSet<String>();
         }
-        privateCollections.add( collection );
+        privateCollections.add(collection);
     }
 
 
@@ -231,7 +250,7 @@ public abstract class AbstractService implements Service {
         if ( privateCollections == null ) {
             privateCollections = new LinkedHashSet<String>();
         }
-        privateCollections.addAll( collections );
+        privateCollections.addAll(collections);
     }
 
 
@@ -239,7 +258,7 @@ public abstract class AbstractService implements Service {
         if ( declaredCollections == null ) {
             declaredCollections = new LinkedHashSet<String>();
         }
-        declaredCollections.add( collection );
+        declaredCollections.add(collection);
     }
 
 
@@ -247,7 +266,7 @@ public abstract class AbstractService implements Service {
         if ( declaredCollections == null ) {
             declaredCollections = new LinkedHashSet<String>();
         }
-        declaredCollections.addAll( collections );
+        declaredCollections.addAll(collections);
     }
 
 
@@ -255,7 +274,7 @@ public abstract class AbstractService implements Service {
         if ( replaceParameters == null ) {
             replaceParameters = new LinkedHashMap<List<String>, List<String>>();
         }
-        replaceParameters.put( find, replace );
+        replaceParameters.put(find, replace);
     }
 
 
@@ -263,7 +282,7 @@ public abstract class AbstractService implements Service {
         if ( serviceCommands == null ) {
             serviceCommands = new LinkedHashSet<String>();
         }
-        serviceCommands.add( command );
+        serviceCommands.add(command);
     }
 
 
@@ -271,7 +290,7 @@ public abstract class AbstractService implements Service {
         if ( serviceCommands == null ) {
             serviceCommands = new LinkedHashSet<String>();
         }
-        serviceCommands.addAll( commands );
+        serviceCommands.addAll(commands);
     }
 
 
@@ -279,7 +298,7 @@ public abstract class AbstractService implements Service {
         if ( entityDictionaries == null ) {
             entityDictionaries = new LinkedHashSet<EntityDictionaryEntry>();
         }
-        entityDictionaries.add( dictionary );
+        entityDictionaries.add(dictionary);
     }
 
 
@@ -305,7 +324,7 @@ public abstract class AbstractService implements Service {
         if ( metadataTypes == null ) {
             metadataTypes = new LinkedHashSet<String>();
         }
-        metadataTypes.add( type );
+        metadataTypes.add(type);
     }
 
 
@@ -313,7 +332,7 @@ public abstract class AbstractService implements Service {
         if ( metadataTypes == null ) {
             metadataTypes = new LinkedHashSet<String>();
         }
-        metadataTypes.addAll( typeList );
+        metadataTypes.addAll(typeList);
     }
 
 
@@ -321,7 +340,7 @@ public abstract class AbstractService implements Service {
         if ( entityCommands == null ) {
             entityCommands = new LinkedHashSet<String>();
         }
-        entityCommands.add( command );
+        entityCommands.add(command);
     }
 
 
@@ -329,7 +348,7 @@ public abstract class AbstractService implements Service {
         if ( entityCommands == null ) {
             entityCommands = new LinkedHashSet<String>();
         }
-        entityCommands.addAll( commands );
+        entityCommands.addAll(commands);
     }
 
 
@@ -354,69 +373,79 @@ public abstract class AbstractService implements Service {
 
     @Override
     public Entity importEntity( ServiceRequest request, Entity entity ) throws Exception
{
-        if ( entity == null ) {
-            return null;
-        }
+        Timer.Context getEntityTimer = entityGetTimer.time();
+        try {
+            if (entity == null) {
+                return null;
+            }
 
-        if ( !isRootService() ) {
-            return sm.importEntity( request, entity );
-        }
+            if (!isRootService()) {
+                return sm.importEntity(request, entity);
+            }
 
-        String path = request.getPath() + "/" + entity.getUuid();
-        Map<String, Object> metadata = new LinkedHashMap<String, Object>();
-        metadata.put( "path", path );
 
-        if ( defaultEntityMetadata != null ) {
-            metadata.putAll( defaultEntityMetadata );
-        }
+            String path = request.getPath() + "/" + entity.getUuid();
+            Map<String, Object> metadata = new LinkedHashMap<String, Object>();
+            metadata.put("path", path);
 
-        Set<Object> connections = getConnectedTypesSet( entity );
-        if ( connections != null ) {
-            Map<String, Object> m = new LinkedHashMap<String, Object>();
-            for ( Object n : connections ) {
-                m.put( n.toString(), path + "/" + n );
+            if (defaultEntityMetadata != null) {
+                metadata.putAll(defaultEntityMetadata);
             }
-            metadata.put( "connections", m );
-        }
 
-        Set<Object> connecting = getConnectingTypesSet( entity );
-        if ( connecting != null ) {
-            Map<String, Object> m = new LinkedHashMap<String, Object>();
-            for ( Object n : connecting ) {
-                m.put( n.toString(), path + "/connecting/" + n );
+            Set<Object> connections = getConnectedTypesSet(entity);
+            if (connections != null) {
+                Map<String, Object> m = new LinkedHashMap<String, Object>();
+                for (Object n : connections) {
+                    m.put(n.toString(), path + "/" + n);
+                }
+                metadata.put("connections", m);
             }
-            metadata.put( "connecting", m );
-        }
 
-        Set<String> collections = getCollectionSet( entity );
-        if ( collections != null ) {
-            Map<String, Object> m = new LinkedHashMap<String, Object>();
-            for ( Object n : collections ) {
-                m.put( n.toString(), path + "/" + n );
+            Set<Object> connecting = getConnectingTypesSet(entity);
+            if (connecting != null) {
+                Map<String, Object> m = new LinkedHashMap<String, Object>();
+                for (Object n : connecting) {
+                    m.put(n.toString(), path + "/connecting/" + n);
+                }
+                metadata.put("connecting", m);
             }
-            metadata.put( "collections", m );
-        }
 
-        if ( entityDictionaries != null ) {
-            Map<String, Object> m = new LinkedHashMap<String, Object>();
-            for ( EntityDictionaryEntry dict : entityDictionaries ) {
-                m.put( dict.getName(), path + "/" + dict.getPath() );
+            Set<String> collections = getCollectionSet(entity);
+            if (collections != null) {
+                Map<String, Object> m = new LinkedHashMap<String, Object>();
+                for (Object n : collections) {
+                    m.put(n.toString(), path + "/" + n);
+                }
+                metadata.put("collections", m);
+            }
+
+            if (entityDictionaries != null) {
+                Map<String, Object> m = new LinkedHashMap<String, Object>();
+                for (EntityDictionaryEntry dict : entityDictionaries) {
+                    m.put(dict.getName(), path + "/" + dict.getPath());
+                }
+                metadata.put("sets", m);
             }
-            metadata.put( "sets", m );
-        }
 
-        if ( metadata.size() > 0 ) {
-            entity.mergeMetadata( metadata );
+            if (metadata.size() > 0) {
+                entity.mergeMetadata(metadata);
+            }
+            return entity;
+        }finally {
+            getEntityTimer.stop();
         }
-        return entity;
     }
 
 
     public void importEntities( ServiceRequest request, Results results ) throws Exception
{
-
-        List<Entity> entities = results.getEntities();
-        if ( entities != null ) {
-            importEntitiesParallel(request, results);
+        Timer.Context timer = entitiesGetTimer.time();
+        try {
+            List<Entity> entities = results.getEntities();
+            if (entities != null) {
+                importEntitiesParallel(request, results);
+            }
+        }finally {
+            timer.stop();
         }
     }
 
@@ -427,42 +456,45 @@ public abstract class AbstractService implements Service {
      * @param results
      */
     private void importEntitiesParallel(final ServiceRequest request, final Results results
) {
+        Timer.Context timer = entitiesParallelGetTimer.time();
+        try {
+            //create our tuples
+            final Observable<EntityTuple> tuples = Observable.create(new Observable.OnSubscribe<EntityTuple>()
{
+                @Override
+                public void call(final Subscriber<? super EntityTuple> subscriber)
{
+                    subscriber.onStart();
+
+                    final List<Entity> entities = results.getEntities();
+                    final int size = entities.size();
+                    for (int i = 0; i < size && !subscriber.isUnsubscribed();
i++) {
+                        subscriber.onNext(new EntityTuple(i, entities.get(i)));
+                    }
 
-        //create our tuples
-        final Observable<EntityTuple> tuples = Observable.create( new Observable.OnSubscribe<EntityTuple>()
{
-            @Override
-            public void call( final Subscriber<? super EntityTuple> subscriber ) {
-                subscriber.onStart();
-
-                final List<Entity> entities = results.getEntities();
-                final int size = entities.size();
-                for ( int i = 0; i < size && !subscriber.isUnsubscribed(); i++
) {
-                    subscriber.onNext( new EntityTuple( i, entities.get( i ) ) );
+                    subscriber.onCompleted();
                 }
+            });
 
-                subscriber.onCompleted();
-            }
-        } );
-
-        //now process them in parallel up to 10 threads
+            //now process them in parallel up to 10 threads
 
-        tuples.flatMap( tuple -> {
-            //map the entity into the tuple
-            return Observable.just( tuple ).doOnNext( parallelTuple -> {
-                //import the entity and set it at index
-                try {
+            tuples.flatMap(tuple -> {
+                //map the entity into the tuple
+                return Observable.just(tuple).doOnNext(parallelTuple -> {
+                    //import the entity and set it at index
+                    try {
 
-                    final Entity imported = importEntity( request, parallelTuple.entity );
+                        final Entity imported = importEntity(request, parallelTuple.entity);
 
-                    if(imported != null) {
-                        results.setEntity( parallelTuple.index, imported );
+                        if (imported != null) {
+                            results.setEntity(parallelTuple.index, imported);
+                        }
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
                     }
-                }
-                catch ( Exception e ) {
-                    throw new RuntimeException(e);
-                }
-            } ).subscribeOn( rxScheduler );
-        }, rxSchedulerFig.getImportThreads() ).toBlocking().lastOrDefault( null );
+                }).subscribeOn(rxScheduler);
+            }, rxSchedulerFig.getImportThreads()).toBlocking().lastOrDefault(null);
+        } finally {
+            timer.stop();
+        }
     }
 
 
@@ -528,7 +560,7 @@ public abstract class AbstractService implements Service {
             entity.addProperties( payload.getProperties() );
             return entity;
         }
-        logger.error( "Attempted update of entity reference rather than full entity, currently
unsupport - MUSTFIX" );
+        logger.error("Attempted update of entity reference rather than full entity, currently
unsupport - MUSTFIX");
         throw new NotImplementedException();
     }
 
@@ -555,7 +587,7 @@ public abstract class AbstractService implements Service {
 
 
     public Set<Object> getConnectedTypesSet( EntityRef ref ) throws Exception {
-        final Set<String> connections = em.getConnectionsAsSource( ref );
+        final Set<String> connections = em.getConnectionsAsSource(ref);
 
         if ( connections == null ) {
             return null;
@@ -574,7 +606,7 @@ public abstract class AbstractService implements Service {
 
 
     public Set<Object> getConnectingTypesSet( EntityRef ref ) throws Exception {
-        final Set<String> connections = em.getConnectionsAsTarget( ref );
+        final Set<String> connections = em.getConnectionsAsTarget(ref);
 
         if ( connections == null ) {
             return null;
@@ -593,7 +625,7 @@ public abstract class AbstractService implements Service {
 
 
     public Set<String> getCollectionSet( EntityRef ref ) {
-        Set<String> set = Schema.getDefaultSchema().getCollectionNames( ref.getType()
);
+        Set<String> set = Schema.getDefaultSchema().getCollectionNames(ref.getType());
         set = new LinkedHashSet<String>( set );
         if ( declaredCollections != null ) {
             set.addAll( declaredCollections );
@@ -612,7 +644,7 @@ public abstract class AbstractService implements Service {
     public ServiceResults invoke( ServiceAction action, ServiceRequest request, ServiceResults
previousResults,
                                   ServicePayload payload ) throws Exception {
 
-        ServiceContext context = getContext( action, request, previousResults, payload );
+        ServiceContext context = getContext(action, request, previousResults, payload);
 
         return invoke( context );
     }
@@ -629,7 +661,7 @@ public abstract class AbstractService implements Service {
         EntityRef owner = request.getOwner();
         String collectionName =
                 "application".equals( owner.getType() ) ? pluralize( info.getItemType() )
: info.getCollectionName();
-        List<ServiceParameter> parameters = filter( request.getParameters(), replaceParameters
);
+        List<ServiceParameter> parameters = filter(request.getParameters(), replaceParameters);
 
         ServiceParameter first_parameter = null;
         if ( !isEmpty( parameters ) ) {
@@ -652,7 +684,7 @@ public abstract class AbstractService implements Service {
         if ( first_parameter instanceof QueryParameter ) {
             query = first_parameter.getQuery();
         }
-        parameters = mergeQueries( query, parameters );
+        parameters = mergeQueries(query, parameters);
 
         if ( first_parameter instanceof IdParameter ) {
             UUID id = first_parameter.getId();
@@ -718,19 +750,19 @@ public abstract class AbstractService implements Service {
 
         switch ( context.getAction() ) {
             case GET:
-                return getItemById( context, id );
+                return getItemById(context, id);
 
             case POST:
-                return postItemById( context, id );
+                return postItemById(context, id);
 
             case PUT:
-                return putItemById( context, id );
+                return putItemById(context, id);
 
             case DELETE:
-                return deleteItemById( context, id );
+                return deleteItemById(context, id);
 
             case HEAD:
-                return headItemById( context, id );
+                return headItemById(context, id);
         }
 
         throw new ServiceInvocationException( context, "Request action unhandled " + context.getAction()
);
@@ -739,21 +771,43 @@ public abstract class AbstractService implements Service {
 
     public ServiceResults invokeItemWithName( ServiceContext context, String name ) throws
Exception {
 
+        Timer.Context time;
         switch ( context.getAction() ) {
             case GET:
-                return getItemByName( context, name );
-
+                time = getByNameTimer.time();
+                try {
+                    return getItemByName(context, name);
+                } finally {
+                    time.stop();
+                }
             case POST:
-                return postItemByName( context, name );
-
+                time = postByNameTimer.time();
+                try {
+                    return postItemByName(context, name);
+                } finally {
+                    time.stop();
+                }
             case PUT:
-                return putItemByName( context, name );
-
+                time = putByNameTimer.time();
+                try {
+                    return putItemByName(context, name);
+                } finally {
+                    time.stop();
+                }
             case DELETE:
-                return deleteItemByName( context, name );
-
+                time = deleteByNameTimer.time();
+                try {
+                    return deleteItemByName(context, name);
+                } finally {
+                    time.stop();
+                }
             case HEAD:
-                return headItemByName( context, name );
+                time = headByNameTimer.time();
+                try {
+                    return headItemByName(context, name);
+                } finally {
+                    time.stop();
+                }
         }
 
         throw new ServiceInvocationException( context, "Request action unhandled " + context.getAction()
);


Mime
View raw message