Repository: atlas Updated Branches: refs/heads/branch-0.8 c5c4742b7 -> f2e714550
ATLAS-2856: added utility methods to RequestContext, to find number of active requests and earliest active request-time Project: http://git-wip-us.apache.org/repos/asf/atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/f2e71455 Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/f2e71455 Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/f2e71455 Branch: refs/heads/branch-0.8 Commit: f2e714550a1bb30f7a7b4fbea5688225b628a4bc Parents: c5c4742 Author: Madhan Neethiraj <mad...@apache.org> Authored: Fri Aug 31 16:04:14 2018 -0700 Committer: Madhan Neethiraj <mad...@apache.org> Committed: Fri Aug 31 21:34:40 2018 -0700 ---------------------------------------------------------------------- .../store/graph/v1/AtlasEntityStoreV1Test.java | 4 + .../java/org/apache/atlas/RequestContext.java | 94 ++++++++++++-------- .../java/org/apache/atlas/RequestContextV1.java | 31 ++++++- 3 files changed, 91 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/atlas/blob/f2e71455/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1Test.java index 35d1a74..089ff08 100644 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1Test.java +++ b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityStoreV1Test.java @@ -20,6 +20,7 @@ package org.apache.atlas.repository.store.graph.v1; import com.google.common.collect.ImmutableSet; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasException; +import org.apache.atlas.RequestContext; import org.apache.atlas.TestModules; import org.apache.atlas.RequestContextV1; import org.apache.atlas.TestUtils; @@ -147,6 +148,9 @@ public class AtlasEntityStoreV1Test { entityStore = new AtlasEntityStoreV1(deleteHandler, typeRegistry, mockChangeNotifier, graphMapper); RequestContextV1.clear(); RequestContextV1.get().setUser(TestUtilsV2.TEST_USER); + + LOG.debug("RequestContextV1: activeCount={}, earliestActiveRequestTime={}", RequestContextV1.getActiveRequestsCount(), RequestContextV1.earliestActiveRequestTime()); + LOG.debug("RequestContext: activeCount={}, earliestActiveRequestTime={}", RequestContext.getActiveRequestsCount(), RequestContext.earliestActiveRequestTime()); } @Test http://git-wip-us.apache.org/repos/asf/atlas/blob/f2e71455/server-api/src/main/java/org/apache/atlas/RequestContext.java ---------------------------------------------------------------------- diff --git a/server-api/src/main/java/org/apache/atlas/RequestContext.java b/server-api/src/main/java/org/apache/atlas/RequestContext.java index 3b2b575..eb3995a 100644 --- a/server-api/src/main/java/org/apache/atlas/RequestContext.java +++ b/server-api/src/main/java/org/apache/atlas/RequestContext.java @@ -21,6 +21,7 @@ package org.apache.atlas; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -29,9 +30,6 @@ import java.util.Set; import org.apache.atlas.metrics.Metrics; import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.TypeSystem; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,19 +38,18 @@ public class RequestContext { private static final Logger LOG = LoggerFactory.getLogger(RequestContext.class); private static final ThreadLocal<RequestContext> CURRENT_CONTEXT = new ThreadLocal<>(); + private static final Set<RequestContext> ACTIVE_REQUESTS = new HashSet<>(); - private Set<String> createdEntityIds = new LinkedHashSet<>(); - private Set<String> updatedEntityIds = new LinkedHashSet<>(); - private Set<String> deletedEntityIds = new LinkedHashSet<>(); - private List<ITypedReferenceableInstance> deletedEntities = new ArrayList<>(); - private Map<String,ITypedReferenceableInstance> entityCacheV1 = new HashMap<>(); - private Map<String,AtlasEntityWithExtInfo> entityCacheV2 = new HashMap<>(); + private final Set<String> createdEntityIds = new LinkedHashSet<>(); + private final Set<String> updatedEntityIds = new LinkedHashSet<>(); + private final Set<String> deletedEntityIds = new LinkedHashSet<>(); + private final List<ITypedReferenceableInstance> deletedEntities = new ArrayList<>(); + private final Map<String,ITypedReferenceableInstance> entityCacheV1 = new HashMap<>(); + private final Map<String,AtlasEntityWithExtInfo> entityCacheV2 = new HashMap<>(); + private final Metrics metrics = new Metrics(); + private final long requestTime = System.currentTimeMillis(); private String user; - private long requestTime; - - private TypeSystem typeSystem = TypeSystem.getInstance(); - private Metrics metrics = new Metrics(); private RequestContext() { } @@ -61,10 +58,12 @@ public class RequestContext { //createContext called for every request in the filter public static RequestContext get() { if (CURRENT_CONTEXT.get() == null) { - synchronized (RequestContext.class) { - if (CURRENT_CONTEXT.get() == null) { - createContext(); - } + RequestContext context = new RequestContext(); + + CURRENT_CONTEXT.set(context); + + synchronized (ACTIVE_REQUESTS) { + ACTIVE_REQUESTS.add(context); } } @@ -74,11 +73,48 @@ public class RequestContext { return CURRENT_CONTEXT.get(); } + public static void clear() { + RequestContext instance = CURRENT_CONTEXT.get(); + + if (instance != null) { + if (instance.entityCacheV1 != null) { + instance.entityCacheV1.clear(); + } + + if (instance.entityCacheV2 != null) { + instance.entityCacheV2.clear(); + } + + synchronized (ACTIVE_REQUESTS) { + ACTIVE_REQUESTS.remove(instance); + } + } + + CURRENT_CONTEXT.remove(); + } + public static RequestContext createContext() { - RequestContext context = new RequestContext(); - context.requestTime = System.currentTimeMillis(); - CURRENT_CONTEXT.set(context); - return context; + clear(); + + return get(); + } + + public static int getActiveRequestsCount() { + return ACTIVE_REQUESTS.size(); + } + + public static long earliestActiveRequestTime() { + long ret = System.currentTimeMillis(); + + synchronized (ACTIVE_REQUESTS) { + for (RequestContext context : ACTIVE_REQUESTS) { + if (ret > context.getRequestTime()) { + ret = context.getRequestTime(); + } + } + } + + return ret; } /** @@ -121,22 +157,6 @@ public class RequestContext { return entityCacheV2.get(guid); } - public static void clear() { - RequestContext instance = CURRENT_CONTEXT.get(); - - if (instance != null) { - if (instance.entityCacheV1 != null) { - instance.entityCacheV1.clear(); - } - - if (instance.entityCacheV2 != null) { - instance.entityCacheV2.clear(); - } - } - - CURRENT_CONTEXT.remove(); - } - public String getUser() { return user; } http://git-wip-us.apache.org/repos/asf/atlas/blob/f2e71455/server-api/src/main/java/org/apache/atlas/RequestContextV1.java ---------------------------------------------------------------------- diff --git a/server-api/src/main/java/org/apache/atlas/RequestContextV1.java b/server-api/src/main/java/org/apache/atlas/RequestContextV1.java index 8506d18..a6d5820 100644 --- a/server-api/src/main/java/org/apache/atlas/RequestContextV1.java +++ b/server-api/src/main/java/org/apache/atlas/RequestContextV1.java @@ -26,19 +26,21 @@ import org.slf4j.LoggerFactory; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; public class RequestContextV1 { private static final Logger LOG = LoggerFactory.getLogger(RequestContextV1.class); private static final ThreadLocal<RequestContextV1> CURRENT_CONTEXT = new ThreadLocal<>(); + private static final Set<RequestContextV1> ACTIVE_REQUESTS = new HashSet<>(); private final Map<String, AtlasObjectId> updatedEntities = new HashMap<>(); private final Map<String, AtlasObjectId> deletedEntities = new HashMap<>(); private final Map<String, AtlasEntity> entityCacheV2 = new HashMap<>(); private final Metrics metrics = new Metrics(); private final long requestTime = System.currentTimeMillis(); - private boolean shouldUpdateModificationTimestamp = true; private String user; @@ -53,10 +55,15 @@ public class RequestContextV1 { if (ret == null) { ret = new RequestContextV1(); CURRENT_CONTEXT.set(ret); + + synchronized (ACTIVE_REQUESTS) { + ACTIVE_REQUESTS.add(ret); + } } return ret; } + public static void clear() { RequestContextV1 instance = CURRENT_CONTEXT.get(); @@ -64,11 +71,33 @@ public class RequestContextV1 { instance.updatedEntities.clear(); instance.deletedEntities.clear(); instance.entityCacheV2.clear(); + + synchronized (ACTIVE_REQUESTS) { + ACTIVE_REQUESTS.remove(instance); + } } CURRENT_CONTEXT.remove(); } + public static int getActiveRequestsCount() { + return ACTIVE_REQUESTS.size(); + } + + public static long earliestActiveRequestTime() { + long ret = System.currentTimeMillis(); + + synchronized (ACTIVE_REQUESTS) { + for (RequestContextV1 context : ACTIVE_REQUESTS) { + if (ret > context.getRequestTime()) { + ret = context.getRequestTime(); + } + } + } + + return ret; + } + public String getUser() { return user; }