From 82df79169b68ec9e51172dda843cbc2b9fea907a Mon Sep 17 00:00:00 2001 From: Rkr1992 <65733786+Rkr1992@users.noreply.github.com> Date: Thu, 28 May 2020 09:39:15 -0500 Subject: [PATCH] addTransaction-removeTransaction (#1338) * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169 * TANDS-19169-changing-CVSS-score * addTransaction-removeTransaction * addTransaction-removeTransaction * addTransaction-removeTransaction * addTransaction-removeTransaction * addTransaction-removeTransaction * addressing-comments * addressing-comments * addressing-comments * addressing-comments * addressing-comments * addressing-comments * addressing-comments * addressing-comments * addressing-comments * making DataStore an abstract class * making DataStore an abstract class * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * adding-transaction-id to transaction implementations * adding-transaction-id to transaction implementations * adding-transaction-id to transaction implementations * adding-transaction-id to transaction implementations * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * fixing bugs * addressing comments * addressing comments * addressing comments * fixing bugs Co-authored-by: Ramsha Rao --- .../src/main/java/com/yahoo/elide/Elide.java | 9 ++- .../elide/core/DataStoreTransaction.java | 10 +++- .../DataStoreTransactionImplementation.java | 22 ++++++++ .../yahoo/elide/core/TransactionRegistry.java | 55 ++++++++----------- .../inmemory/HashMapStoreTransaction.java | 3 +- .../inmemory/InMemoryStoreTransaction.java | 4 +- .../datastore/wrapped/TransactionWrapper.java | 3 +- .../elide/core/DataStoreTransactionTest.java | 2 +- .../AggregationDataStoreTransaction.java | 6 +- .../hibernate3/HibernateTransaction.java | 4 +- .../hibernate5/HibernateTransaction.java | 4 +- .../inmemory/HashMapStoreTransaction.java | 4 ++ .../transaction/AbstractJpaTransaction.java | 3 +- .../jpa/transaction/JtaTransaction.java | 1 - .../jpa/transaction/NonJtaTransaction.java | 1 - .../multiplex/MultiplexReadTransaction.java | 1 - .../multiplex/MultiplexTransaction.java | 3 +- .../datastores/multiplex/TestDataStore.java | 3 +- .../bridgeable/BridgeableRedisStore.java | 3 +- .../datastores/noop/NoopTransaction.java | 4 +- .../search/SearchDataTransaction.java | 1 - .../com/yahoo/elide/graphql/QueryRunner.java | 6 +- 22 files changed, 91 insertions(+), 61 deletions(-) create mode 100644 elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransactionImplementation.java diff --git a/elide-core/src/main/java/com/yahoo/elide/Elide.java b/elide-core/src/main/java/com/yahoo/elide/Elide.java index d04014e3cb..ac3daa339f 100644 --- a/elide-core/src/main/java/com/yahoo/elide/Elide.java +++ b/elide-core/src/main/java/com/yahoo/elide/Elide.java @@ -10,6 +10,7 @@ import com.yahoo.elide.core.DataStoreTransaction; import com.yahoo.elide.core.HttpStatus; import com.yahoo.elide.core.RequestScope; +import com.yahoo.elide.core.TransactionRegistry; import com.yahoo.elide.core.datastore.inmemory.InMemoryDataStore; import com.yahoo.elide.core.exceptions.ForbiddenAccessException; import com.yahoo.elide.core.exceptions.HttpStatusException; @@ -57,6 +58,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Set; +import java.util.UUID; import java.util.function.Supplier; import javax.validation.ConstraintViolationException; @@ -76,7 +78,7 @@ public class Elide { @Getter private final AuditLogger auditLogger; @Getter private final DataStore dataStore; @Getter private final JsonApiMapper mapper; - + @Getter private final TransactionRegistry transactionRegistry; /** * Instantiates a new Elide instance. * @@ -88,6 +90,7 @@ public Elide(ElideSettings elideSettings) { this.dataStore = new InMemoryDataStore(elideSettings.getDataStore()); this.dataStore.populateEntityDictionary(elideSettings.getDictionary()); this.mapper = elideSettings.getMapper(); + this.transactionRegistry = new TransactionRegistry(); elideSettings.getSerdes().forEach((targetType, serde) -> { CoerceUtil.register(targetType, serde); @@ -437,7 +440,10 @@ protected ElideResponse handleRequest(boolean isReadOnly, User user, Supplier transaction, Handler handler) { boolean isVerbose = false; + UUID requestId = null; try (DataStoreTransaction tx = transaction.get()) { + requestId = tx.getRequestId(); + transactionRegistry.addRunningTransaction(requestId, tx); HandlerResult result = handler.handle(tx, user); RequestScope requestScope = result.getRequestScope(); isVerbose = requestScope.getPermissionExecutor().isVerbose(); @@ -506,6 +512,7 @@ protected ElideResponse handleRequest(boolean isReadOnly, User user, throw e; } finally { + transactionRegistry.removeRunningTransaction(requestId); auditLogger.clear(); } } diff --git a/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransaction.java b/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransaction.java index a57d31f65e..3ccd5f4af3 100644 --- a/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransaction.java +++ b/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransaction.java @@ -17,7 +17,7 @@ import java.io.Serializable; import java.util.Iterator; import java.util.Set; - +import java.util.UUID; /** * Wraps the Database Transaction type. */ @@ -31,7 +31,6 @@ public enum FeatureSupport { PARTIAL, NONE } - /** * Save the updated object. * @@ -229,7 +228,7 @@ default Object getAttribute(Object entity, * @param entity - The object which owns the attribute. * @param attribute - the attribute to set. * @param scope - contains request level metadata. - */ + */ default void setAttribute(Object entity, Attribute attribute, RequestScope scope) { @@ -264,4 +263,9 @@ default boolean supportsSorting(Class entityClass, Sorting sorting) { default boolean supportsPagination(Class entityClass, FilterExpression expression) { return true; } + /** + * Transaction ID for each transaction + * @return UUID id + */ + UUID getRequestId(); } diff --git a/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransactionImplementation.java b/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransactionImplementation.java new file mode 100644 index 0000000000..3a59d45d9e --- /dev/null +++ b/elide-core/src/main/java/com/yahoo/elide/core/DataStoreTransactionImplementation.java @@ -0,0 +1,22 @@ +/* + * Copyright 2020, Yahoo Inc. + * Licensed under the Apache License, Version 2.0 + * See LICENSE file in project root for terms. + */ +package com.yahoo.elide.core; + +import lombok.Getter; + +import java.util.UUID; + +/** + * Data Store Transaction Implementation to return transaction id + */ +public abstract class DataStoreTransactionImplementation implements DataStoreTransaction { + @Getter private final UUID requestId = UUID.randomUUID(); + + @Override + public UUID getRequestId() { + return requestId; + } +} diff --git a/elide-core/src/main/java/com/yahoo/elide/core/TransactionRegistry.java b/elide-core/src/main/java/com/yahoo/elide/core/TransactionRegistry.java index 954dbeee71..86d943bb6a 100644 --- a/elide-core/src/main/java/com/yahoo/elide/core/TransactionRegistry.java +++ b/elide-core/src/main/java/com/yahoo/elide/core/TransactionRegistry.java @@ -5,44 +5,33 @@ */ package com.yahoo.elide.core; -import lombok.Data; +import lombok.Getter; +import java.util.HashMap; +import java.util.Map; import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; /** - * Transaction Registry interface to surface transaction details to other parts of Elide. - */ - -public interface TransactionRegistry { - /** - * @see RequestScope - * @see DataStoreTransaction - */ - @Data - public static class TransactionEntry { - public RequestScope request; - public DataStoreTransaction transaction; +* Transaction Registry class. +*/ +@Getter +public class TransactionRegistry { + private Map transactionMap = new HashMap<>(); + public Set getRunningTransactions() { + Set transactions = transactionMap.values().stream().collect(Collectors.toSet()); + return transactions; } - /** - * @return all running transactions - */ - Set getRunningTransactions(); - - /** - * @param requestId - * @return matching running transaction - */ - Set getRunningTransaction(String requestId); + public DataStoreTransaction getRunningTransaction(UUID requestId) { + return transactionMap.get(requestId); + } - /** - * Adds running transaction - * @param transactionEntry TransactionEntry transactionEntry - */ - void addRunningTransaction(TransactionEntry transactionEntry); + public void addRunningTransaction(UUID requestId, DataStoreTransaction tx) { + transactionMap.put(requestId, tx); + } - /** - * Removes running transaction when we call cancel on it - * @param transactionEntry TransactionEntry transactionEntry - */ - void removeRunningTransaction(TransactionEntry transactionEntry); + public void removeRunningTransaction(UUID requestId) { + transactionMap.remove(requestId); + } } diff --git a/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/HashMapStoreTransaction.java b/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/HashMapStoreTransaction.java index d4ce2ef85d..d1c919c06d 100644 --- a/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/HashMapStoreTransaction.java +++ b/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/HashMapStoreTransaction.java @@ -6,6 +6,7 @@ package com.yahoo.elide.core.datastore.inmemory; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.RequestScope; import com.yahoo.elide.core.exceptions.TransactionException; @@ -27,7 +28,7 @@ /** * HashMapDataStore transaction handler. */ -public class HashMapStoreTransaction implements DataStoreTransaction { +public class HashMapStoreTransaction extends DataStoreTransactionImplementation { private final Map, Map> dataStore; private final List operations; private final EntityDictionary dictionary; diff --git a/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/InMemoryStoreTransaction.java b/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/InMemoryStoreTransaction.java index aea6969483..e3a52f67a6 100644 --- a/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/InMemoryStoreTransaction.java +++ b/elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/InMemoryStoreTransaction.java @@ -7,6 +7,7 @@ package com.yahoo.elide.core.datastore.inmemory; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.Path; import com.yahoo.elide.core.PersistentResource; import com.yahoo.elide.core.RequestScope; @@ -40,10 +41,9 @@ * Data Store Transaction that wraps another transaction and provides in-memory filtering, soring, and pagination * when the underlying transaction cannot perform the equivalent function. */ -public class InMemoryStoreTransaction implements DataStoreTransaction { +public class InMemoryStoreTransaction extends DataStoreTransactionImplementation implements DataStoreTransaction { private final DataStoreTransaction tx; - private static final Comparator NULL_SAFE_COMPARE = (a, b) -> { if (a == null && b == null) { return 0; diff --git a/elide-core/src/main/java/com/yahoo/elide/core/datastore/wrapped/TransactionWrapper.java b/elide-core/src/main/java/com/yahoo/elide/core/datastore/wrapped/TransactionWrapper.java index 9ee3631d61..dc840aafe9 100644 --- a/elide-core/src/main/java/com/yahoo/elide/core/datastore/wrapped/TransactionWrapper.java +++ b/elide-core/src/main/java/com/yahoo/elide/core/datastore/wrapped/TransactionWrapper.java @@ -7,6 +7,7 @@ package com.yahoo.elide.core.datastore.wrapped; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.RequestScope; import com.yahoo.elide.core.filter.expression.FilterExpression; import com.yahoo.elide.request.Attribute; @@ -25,7 +26,7 @@ */ @Data @AllArgsConstructor -public abstract class TransactionWrapper implements DataStoreTransaction { +public abstract class TransactionWrapper extends DataStoreTransactionImplementation { protected DataStoreTransaction tx; @Override diff --git a/elide-core/src/test/java/com/yahoo/elide/core/DataStoreTransactionTest.java b/elide-core/src/test/java/com/yahoo/elide/core/DataStoreTransactionTest.java index 0988a56e2a..725ef746c9 100644 --- a/elide-core/src/test/java/com/yahoo/elide/core/DataStoreTransactionTest.java +++ b/elide-core/src/test/java/com/yahoo/elide/core/DataStoreTransactionTest.java @@ -25,7 +25,7 @@ import java.io.Serializable; import java.util.Arrays; -public class DataStoreTransactionTest implements DataStoreTransaction { +public class DataStoreTransactionTest extends DataStoreTransactionImplementation implements DataStoreTransaction { private static final String NAME = "name"; private static final Attribute NAME_ATTRIBUTE = Attribute.builder().name(NAME).type(String.class).build(); private static final String ENTITY = "entity"; diff --git a/elide-datastore/elide-datastore-aggregation/src/main/java/com/yahoo/elide/datastores/aggregation/AggregationDataStoreTransaction.java b/elide-datastore/elide-datastore-aggregation/src/main/java/com/yahoo/elide/datastores/aggregation/AggregationDataStoreTransaction.java index c994305a0f..544b491315 100644 --- a/elide-datastore/elide-datastore-aggregation/src/main/java/com/yahoo/elide/datastores/aggregation/AggregationDataStoreTransaction.java +++ b/elide-datastore/elide-datastore-aggregation/src/main/java/com/yahoo/elide/datastores/aggregation/AggregationDataStoreTransaction.java @@ -5,7 +5,7 @@ */ package com.yahoo.elide.datastores.aggregation; -import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.RequestScope; import com.yahoo.elide.datastores.aggregation.metadata.models.Table; import com.yahoo.elide.datastores.aggregation.query.Query; @@ -15,13 +15,11 @@ import com.google.common.annotations.VisibleForTesting; import java.io.IOException; - /** * Transaction handler for {@link AggregationDataStore}. */ -public class AggregationDataStoreTransaction implements DataStoreTransaction { +public class AggregationDataStoreTransaction extends DataStoreTransactionImplementation { private QueryEngine queryEngine; - public AggregationDataStoreTransaction(QueryEngine queryEngine) { this.queryEngine = queryEngine; } diff --git a/elide-datastore/elide-datastore-hibernate3/src/main/java/com/yahoo/elide/datastores/hibernate3/HibernateTransaction.java b/elide-datastore/elide-datastore-hibernate3/src/main/java/com/yahoo/elide/datastores/hibernate3/HibernateTransaction.java index 6e5ca3d789..d462b69ea3 100644 --- a/elide-datastore/elide-datastore-hibernate3/src/main/java/com/yahoo/elide/datastores/hibernate3/HibernateTransaction.java +++ b/elide-datastore/elide-datastore-hibernate3/src/main/java/com/yahoo/elide/datastores/hibernate3/HibernateTransaction.java @@ -6,6 +6,7 @@ package com.yahoo.elide.datastores.hibernate3; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.Path; import com.yahoo.elide.core.RequestScope; @@ -48,13 +49,12 @@ * Hibernate Transaction implementation. */ @Slf4j -public class HibernateTransaction implements DataStoreTransaction { +public class HibernateTransaction extends DataStoreTransactionImplementation { private final Session session; private final SessionWrapper sessionWrapper; private final LinkedHashSet deferredTasks = new LinkedHashSet<>(); private final boolean isScrollEnabled; - /** * Constructor. * diff --git a/elide-datastore/elide-datastore-hibernate5/src/main/java/com/yahoo/elide/datastores/hibernate5/HibernateTransaction.java b/elide-datastore/elide-datastore-hibernate5/src/main/java/com/yahoo/elide/datastores/hibernate5/HibernateTransaction.java index 7045d556db..dc1b7fd20c 100644 --- a/elide-datastore/elide-datastore-hibernate5/src/main/java/com/yahoo/elide/datastores/hibernate5/HibernateTransaction.java +++ b/elide-datastore/elide-datastore-hibernate5/src/main/java/com/yahoo/elide/datastores/hibernate5/HibernateTransaction.java @@ -6,6 +6,7 @@ package com.yahoo.elide.datastores.hibernate5; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.Path; import com.yahoo.elide.core.RequestScope; @@ -48,13 +49,12 @@ * Hibernate Transaction implementation. */ @Slf4j -public class HibernateTransaction implements DataStoreTransaction { +public class HibernateTransaction extends DataStoreTransactionImplementation { private final Session session; private final SessionWrapper sessionWrapper; private final LinkedHashSet deferredTasks = new LinkedHashSet<>(); private final boolean isScrollEnabled; - /** * Constructor. * diff --git a/elide-datastore/elide-datastore-inmemorydb/src/main/java/com/yahoo/elide/datastores/inmemory/HashMapStoreTransaction.java b/elide-datastore/elide-datastore-inmemorydb/src/main/java/com/yahoo/elide/datastores/inmemory/HashMapStoreTransaction.java index d86a0c497e..9c2763634a 100644 --- a/elide-datastore/elide-datastore-inmemorydb/src/main/java/com/yahoo/elide/datastores/inmemory/HashMapStoreTransaction.java +++ b/elide-datastore/elide-datastore-inmemorydb/src/main/java/com/yahoo/elide/datastores/inmemory/HashMapStoreTransaction.java @@ -7,7 +7,10 @@ import com.yahoo.elide.core.EntityDictionary; +import lombok.Getter; + import java.util.Map; +import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; /** @@ -16,6 +19,7 @@ */ @Deprecated public class HashMapStoreTransaction extends com.yahoo.elide.core.datastore.inmemory.HashMapStoreTransaction { + @Getter private final UUID requestId = UUID.randomUUID(); public HashMapStoreTransaction(Map, Map> dataStore, EntityDictionary dictionary, Map, AtomicLong> typeIds) { super(dataStore, dictionary, typeIds); diff --git a/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/AbstractJpaTransaction.java b/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/AbstractJpaTransaction.java index ded501eebe..89e91bb2d6 100644 --- a/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/AbstractJpaTransaction.java +++ b/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/AbstractJpaTransaction.java @@ -6,6 +6,7 @@ package com.yahoo.elide.datastores.jpa.transaction; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.Path; import com.yahoo.elide.core.RequestScope; @@ -48,7 +49,7 @@ * Base JPA transaction implementation class. */ @Slf4j -public abstract class AbstractJpaTransaction implements JpaTransaction { +public abstract class AbstractJpaTransaction extends DataStoreTransactionImplementation implements JpaTransaction { private static final Predicate> IS_PERSISTENT_COLLECTION = new PersistentCollectionChecker(); diff --git a/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/JtaTransaction.java b/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/JtaTransaction.java index 68ea8edb09..a96a95992b 100644 --- a/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/JtaTransaction.java +++ b/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/JtaTransaction.java @@ -22,7 +22,6 @@ @Slf4j public class JtaTransaction extends AbstractJpaTransaction { private final UserTransaction transaction; - public JtaTransaction(EntityManager entityManager) { this(entityManager, lookupUserTransaction()); } diff --git a/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/NonJtaTransaction.java b/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/NonJtaTransaction.java index 405a8444c1..a65b5cebeb 100644 --- a/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/NonJtaTransaction.java +++ b/elide-datastore/elide-datastore-jpa/src/main/java/com/yahoo/elide/datastores/jpa/transaction/NonJtaTransaction.java @@ -19,7 +19,6 @@ @Slf4j public class NonJtaTransaction extends AbstractJpaTransaction { private final EntityTransaction transaction; - public NonJtaTransaction(EntityManager entityManager) { super(entityManager); this.transaction = entityManager.getTransaction(); diff --git a/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexReadTransaction.java b/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexReadTransaction.java index b2fbcbeca3..632adc20a8 100644 --- a/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexReadTransaction.java +++ b/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexReadTransaction.java @@ -13,7 +13,6 @@ * Multiplex transaction handler. */ public class MultiplexReadTransaction extends MultiplexTransaction { - public MultiplexReadTransaction(MultiplexManager multiplexManager) { super(multiplexManager); } diff --git a/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexTransaction.java b/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexTransaction.java index 001746ffb4..7956b38eb4 100644 --- a/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexTransaction.java +++ b/elide-datastore/elide-datastore-multiplex/src/main/java/com/yahoo/elide/datastores/multiplex/MultiplexTransaction.java @@ -7,6 +7,7 @@ import com.yahoo.elide.core.DataStore; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.RelationshipType; import com.yahoo.elide.core.RequestScope; @@ -32,7 +33,7 @@ * Multiplex transaction handler. Process each sub-database transactions within a single transaction. * If any commit fails in process, reverse any commits already completed. */ -public abstract class MultiplexTransaction implements DataStoreTransaction { +public abstract class MultiplexTransaction extends DataStoreTransactionImplementation { protected final LinkedHashMap transactions; protected final MultiplexManager multiplexManager; protected final DataStoreTransaction lastDataStoreTransaction; diff --git a/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/TestDataStore.java b/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/TestDataStore.java index 1712965e8a..a219367314 100644 --- a/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/TestDataStore.java +++ b/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/TestDataStore.java @@ -7,6 +7,7 @@ import com.yahoo.elide.core.DataStore; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.RequestScope; import com.yahoo.elide.core.exceptions.TransactionException; @@ -18,7 +19,7 @@ import javax.persistence.Entity; -class TestDataStore implements DataStore, DataStoreTransaction { +class TestDataStore extends DataStoreTransactionImplementation implements DataStore { private final Package beanPackage; diff --git a/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/bridgeable/BridgeableRedisStore.java b/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/bridgeable/BridgeableRedisStore.java index df990037fa..b084b9bb95 100644 --- a/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/bridgeable/BridgeableRedisStore.java +++ b/elide-datastore/elide-datastore-multiplex/src/test/java/com/yahoo/elide/datastores/multiplex/bridgeable/BridgeableRedisStore.java @@ -7,6 +7,7 @@ import com.yahoo.elide.core.DataStore; import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.EntityDictionary; import com.yahoo.elide.core.Path; import com.yahoo.elide.core.RequestScope; @@ -58,7 +59,7 @@ public DataStoreTransaction beginReadTransaction() { return new ExampleRedisTransaction(); } - public class ExampleRedisTransaction implements DataStoreTransaction, BridgeableTransaction { + public class ExampleRedisTransaction extends DataStoreTransactionImplementation implements BridgeableTransaction { @Override public Object loadObject(EntityProjection projection, diff --git a/elide-datastore/elide-datastore-noop/src/main/java/com/yahoo/elide/datastores/noop/NoopTransaction.java b/elide-datastore/elide-datastore-noop/src/main/java/com/yahoo/elide/datastores/noop/NoopTransaction.java index 8e51831449..5a12533313 100644 --- a/elide-datastore/elide-datastore-noop/src/main/java/com/yahoo/elide/datastores/noop/NoopTransaction.java +++ b/elide-datastore/elide-datastore-noop/src/main/java/com/yahoo/elide/datastores/noop/NoopTransaction.java @@ -5,7 +5,7 @@ */ package com.yahoo.elide.datastores.noop; -import com.yahoo.elide.core.DataStoreTransaction; +import com.yahoo.elide.core.DataStoreTransactionImplementation; import com.yahoo.elide.core.PersistentResource; import com.yahoo.elide.core.RequestScope; @@ -20,7 +20,7 @@ * Noop transaction. Specifically, this transaction does not perform any actions (i.e. no operation). */ @Slf4j -public class NoopTransaction implements DataStoreTransaction { +public class NoopTransaction extends DataStoreTransactionImplementation { /** * No-op transaction, do nothing. * @param entity - the object to save. diff --git a/elide-datastore/elide-datastore-search/src/main/java/com/yahoo/elide/datastores/search/SearchDataTransaction.java b/elide-datastore/elide-datastore-search/src/main/java/com/yahoo/elide/datastores/search/SearchDataTransaction.java index 9e5b7d7956..c75208a378 100644 --- a/elide-datastore/elide-datastore-search/src/main/java/com/yahoo/elide/datastores/search/SearchDataTransaction.java +++ b/elide-datastore/elide-datastore-search/src/main/java/com/yahoo/elide/datastores/search/SearchDataTransaction.java @@ -55,7 +55,6 @@ public class SearchDataTransaction extends TransactionWrapper { private FullTextEntityManager em; private int minNgram; private int maxNgram; - public SearchDataTransaction(DataStoreTransaction tx, EntityDictionary dictionary, FullTextEntityManager em, diff --git a/elide-graphql/src/main/java/com/yahoo/elide/graphql/QueryRunner.java b/elide-graphql/src/main/java/com/yahoo/elide/graphql/QueryRunner.java index 73df69b4b1..49f1883b4b 100644 --- a/elide-graphql/src/main/java/com/yahoo/elide/graphql/QueryRunner.java +++ b/elide-graphql/src/main/java/com/yahoo/elide/graphql/QueryRunner.java @@ -41,6 +41,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.UUID; import java.util.function.Function; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -147,8 +148,10 @@ public ElideResponse run(String baseUrlEndPoint, String graphQLDocument, User us private ElideResponse executeGraphQLRequest(String baseUrlEndPoint, ObjectMapper mapper, User principal, String graphQLDocument, JsonNode jsonDocument) { boolean isVerbose = false; + UUID requestId = null; try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) { - + requestId = tx.getRequestId(); + elide.getTransactionRegistry().addRunningTransaction(requestId, tx); if (!jsonDocument.has(QUERY)) { return ElideResponse.builder() .responseCode(HttpStatus.SC_BAD_REQUEST) @@ -274,6 +277,7 @@ public String toString() { log.error("Unhandled error or exception.", e); throw e; } finally { + elide.getTransactionRegistry().removeRunningTransaction(requestId); elide.getAuditLogger().clear(); } }