Skip to content

Commit

Permalink
addTransaction-removeTransaction (#1338)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and aklish committed Dec 11, 2020
1 parent 95b3031 commit 82df791
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 61 deletions.
9 changes: 8 additions & 1 deletion elide-core/src/main/java/com/yahoo/elide/Elide.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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);
Expand Down Expand Up @@ -437,7 +440,10 @@ protected ElideResponse handleRequest(boolean isReadOnly, User user,
Supplier<DataStoreTransaction> transaction,
Handler<DataStoreTransaction, User, HandlerResult> 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();
Expand Down Expand Up @@ -506,6 +512,7 @@ protected ElideResponse handleRequest(boolean isReadOnly, User user,
throw e;

} finally {
transactionRegistry.removeRunningTransaction(requestId);
auditLogger.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -31,7 +31,6 @@ public enum FeatureSupport {
PARTIAL,
NONE
}

/**
* Save the updated object.
*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, DataStoreTransaction> transactionMap = new HashMap<>();
public Set<DataStoreTransaction> getRunningTransactions() {
Set<DataStoreTransaction> transactions = transactionMap.values().stream().collect(Collectors.toSet());
return transactions;
}

/**
* @return all running transactions
*/
Set<TransactionEntry> getRunningTransactions();

/**
* @param requestId
* @return matching running transaction
*/
Set<TransactionEntry> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +28,7 @@
/**
* HashMapDataStore transaction handler.
*/
public class HashMapStoreTransaction implements DataStoreTransaction {
public class HashMapStoreTransaction extends DataStoreTransactionImplementation {
private final Map<Class<?>, Map<String, Object>> dataStore;
private final List<Operation> operations;
private final EntityDictionary dictionary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Object> NULL_SAFE_COMPARE = (a, b) -> {
if (a == null && b == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@
*/
@Data
@AllArgsConstructor
public abstract class TransactionWrapper implements DataStoreTransaction {
public abstract class TransactionWrapper extends DataStoreTransactionImplementation {
protected DataStoreTransaction tx;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Runnable> deferredTasks = new LinkedHashSet<>();
private final boolean isScrollEnabled;

/**
* Constructor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Runnable> deferredTasks = new LinkedHashSet<>();
private final boolean isScrollEnabled;

/**
* Constructor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<Class<?>, Map<String, Object>> dataStore,
EntityDictionary dictionary, Map<Class<?>, AtomicLong> typeIds) {
super(dataStore, dictionary, typeIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Collection<?>> IS_PERSISTENT_COLLECTION =
new PersistentCollectionChecker();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@Slf4j
public class JtaTransaction extends AbstractJpaTransaction {
private final UserTransaction transaction;

public JtaTransaction(EntityManager entityManager) {
this(entityManager, lookupUserTransaction());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
@Slf4j
public class NonJtaTransaction extends AbstractJpaTransaction {
private final EntityTransaction transaction;

public NonJtaTransaction(EntityManager entityManager) {
super(entityManager);
this.transaction = entityManager.getTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* Multiplex transaction handler.
*/
public class MultiplexReadTransaction extends MultiplexTransaction {

public MultiplexReadTransaction(MultiplexManager multiplexManager) {
super(multiplexManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DataStore, DataStoreTransaction> transactions;
protected final MultiplexManager multiplexManager;
protected final DataStoreTransaction lastDataStoreTransaction;
Expand Down
Loading

0 comments on commit 82df791

Please sign in to comment.