Search in sources :

Example 6 with Response

use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.

the class TransactionLogObserverTest method testCreateTransactionCommitRollback.

@Test(timeout = 10000)
public void testCreateTransactionCommitRollback() throws InterruptedException {
    createObserver();
    ClientTransactionManager clientManager = direct.getClientTransactionManager();
    String transactionId = UUID.randomUUID().toString();
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
    responseQueue.take();
    clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
    responseQueue.take();
    clientManager.rollback(transactionId);
    Pair<String, Response> response = responseQueue.take();
    assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
    // now, when we start new transaction, it must be let through
    transactionId = UUID.randomUUID().toString();
    responseQueue.clear();
    clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), KeyAttributes.ofWildcardQueryElements(user, "user", userGateways, Collections.emptyList()));
    responseQueue.take();
    clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
    response = responseQueue.take();
    assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 7 with Response

use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.

the class TransactionLogObserverTest method testCreateTransactionCommitWithConflictInOutputs.

@Test(timeout = 10000)
public void testCreateTransactionCommitWithConflictInOutputs() throws InterruptedException {
    createObserver();
    ClientTransactionManager clientManager = direct.getClientTransactionManager();
    String t1 = UUID.randomUUID().toString();
    String t2 = UUID.randomUUID().toString();
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "u1", userGateways, 1L, "1")));
    Pair<String, Response> t1OpenResponse = responseQueue.take();
    List<KeyAttribute> t1Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t1OpenResponse.getSecond().getSeqId(), "1"));
    clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), // not conflicting with the previous one
    Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "u2", userGateways, 1L, "1")));
    Pair<String, Response> t2OpenResponse = responseQueue.take();
    List<KeyAttribute> t2Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t2OpenResponse.getSecond().getSeqId(), "1"));
    clientManager.commit(t2, t2Outputs);
    Pair<String, Response> response = responseQueue.take();
    assertEquals("commit", response.getFirst());
    assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
    clientManager.commit(t1, t1Outputs);
    response = responseQueue.take();
    assertEquals("commit", response.getFirst());
    assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 8 with Response

use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.

the class TransactionLogObserverTest method testCreateTransaction.

@Test(timeout = 10000)
public void testCreateTransaction() throws InterruptedException {
    createObserver();
    ClientTransactionManager clientManager = direct.getClientTransactionManager();
    String transactionId = UUID.randomUUID().toString();
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
    Pair<String, Response> response = responseQueue.take();
    assertTrue(response.getFirst().startsWith("open."));
    assertEquals(Response.Flags.OPEN, response.getSecond().getFlags());
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 9 with Response

use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.

the class TransactionLogObserver method processTransactionRequest.

private void processTransactionRequest(String transactionId, String requestId, Request request, OnNextContext context) {
    log.debug("Processing request to {} with {} for transaction {}", requestId, request, transactionId);
    State currentState = manager.getCurrentState(transactionId);
    @Nullable State newState = transitionState(transactionId, currentState, request);
    if (newState != null) {
        // we have successfully computed new state, produce response
        Response response = getResponseForNewState(request, currentState, newState);
        manager.ensureTransactionOpen(transactionId, newState);
        manager.writeResponseAndUpdateState(transactionId, newState, requestId, response, context::commit);
    } else if (request.getFlags() == Request.Flags.OPEN && (currentState.getFlags() == State.Flags.OPEN || currentState.getFlags() == State.Flags.COMMITTED)) {
        manager.writeResponseAndUpdateState(transactionId, currentState, requestId, Response.forRequest(request).duplicate(currentState.getSequentialId()), context::commit);
    } else {
        log.warn("Unexpected {} request for transaction {} seqId {} when the state is {}. " + "Refusing to respond, because the correct response is unknown.", request.getFlags(), transactionId, currentState.getSequentialId(), currentState.getFlags());
        context.confirm();
    }
}
Also used : Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) Nullable(javax.annotation.Nullable)

Example 10 with Response

use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.

the class TransactionLogObserverTest method testTwoOpenedCommitWildcard.

@Test(timeout = 10000)
public void testTwoOpenedCommitWildcard() throws InterruptedException {
    createObserver();
    try (ClientTransactionManager clientManager = direct.getClientTransactionManager()) {
        String t1 = UUID.randomUUID().toString();
        String t2 = UUID.randomUUID().toString();
        List<KeyAttribute> inputs = KeyAttributes.ofWildcardQueryElements(user, "user", userGateways, Collections.singletonList(userGateways.upsert(100L, "user", "1", now, new byte[] {})));
        BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
        clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), inputs);
        Pair<String, Response> t1OpenResponse = responseQueue.take();
        clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), inputs);
        Pair<String, Response> t2OpenResponse = responseQueue.take();
        assertEquals(Response.Flags.OPEN, t1OpenResponse.getSecond().getFlags());
        assertEquals(Response.Flags.OPEN, t2OpenResponse.getSecond().getFlags());
        List<KeyAttribute> t1Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t1OpenResponse.getSecond().getSeqId(), "1"));
        List<KeyAttribute> t2Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t2OpenResponse.getSecond().getSeqId(), "1"));
        clientManager.commit(t1, t1Outputs);
        Pair<String, Response> response = responseQueue.take();
        assertEquals("commit", response.getFirst());
        assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
        clientManager.commit(t2, t2Outputs);
        response = responseQueue.take();
        assertEquals("commit", response.getFirst());
        assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
    }
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)40 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)33 StreamElement (cz.o2.proxima.storage.StreamElement)25 Response (cz.o2.proxima.transaction.Response)24 KeyAttribute (cz.o2.proxima.transaction.KeyAttribute)22 State (cz.o2.proxima.transaction.State)22 ArrayList (java.util.ArrayList)22 Before (org.junit.Before)22 ConfigFactory (com.typesafe.config.ConfigFactory)21 Repository (cz.o2.proxima.repository.Repository)21 KeyAttributes (cz.o2.proxima.transaction.KeyAttributes)21 Request (cz.o2.proxima.transaction.Request)21 Pair (cz.o2.proxima.util.Pair)21 Collections (java.util.Collections)21 List (java.util.List)21 UUID (java.util.UUID)21 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)21 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)20 Wildcard (cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard)20 ExceptionUtils (cz.o2.proxima.util.ExceptionUtils)20