Search in sources :

Example 26 with Response

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

the class TransactionLogObserverTest method testCreateTransactionCommitWithSameTimestampInOutputs.

@Test(timeout = 10000)
public void testCreateTransactionCommitWithSameTimestampInOutputs() throws InterruptedException {
    long stamp = System.currentTimeMillis();
    WithFixedTime factory = new WithFixedTime(stamp);
    createObserver(factory);
    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();
    assertEquals(stamp, t1OpenResponse.getSecond().getStamp());
    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();
    assertEquals(stamp, t2OpenResponse.getSecond().getStamp());
    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)

Example 27 with Response

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

the class TransactionLogObserverTest method testHousekeepingOfWildcardAttributes.

@Test(timeout = 10000)
public void testHousekeepingOfWildcardAttributes() throws InterruptedException {
    now = System.currentTimeMillis();
    AtomicLong stamp = new AtomicLong(now);
    createObserver(new WithTransactionTimeout(100, stamp));
    ServerTransactionManager serverManager = observer.getRawManager();
    try (ClientTransactionManager clientManager = new TransactionResourceManager(direct, Collections.emptyMap())) {
        serverManager.houseKeeping();
        String transactionId = "t1";
        BlockingQueue<Pair<String, Response>> queue = new ArrayBlockingQueue<>(10);
        clientManager.begin(transactionId, (s, r) -> ExceptionUtils.unchecked(() -> queue.put(Pair.of(s, r))), KeyAttributes.ofWildcardQueryElements(this.user, "user", userGateways, Collections.emptyList()));
        Pair<String, Response> response = queue.take();
        assertEquals(Response.Flags.OPEN, response.getSecond().getFlags());
        assertTrue("Expected empty queue, got " + queue, queue.isEmpty());
        StreamElement wildcardUpsert = userGateways.upsert(response.getSecond().getSeqId(), "user", "1", now, new byte[] {});
        clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofStreamElement(wildcardUpsert)));
        assertEquals(Response.Flags.COMMITTED, queue.take().getSecond().getFlags());
        assertTrue(queue.isEmpty());
        // wait till housekeeping time expires
        stamp.addAndGet(200L);
        transactionId = "t2";
        clientManager.begin(transactionId, (s, r) -> ExceptionUtils.unchecked(() -> queue.put(Pair.of(s, r))), KeyAttributes.ofWildcardQueryElements(user, "user", userGateways, Collections.singletonList(wildcardUpsert)));
        response = queue.take();
        assertEquals(Response.Flags.OPEN, response.getSecond().getFlags());
        assertTrue(queue.isEmpty());
        wildcardUpsert = userGateways.upsert(response.getSecond().getSeqId(), "user", "1", now, new byte[] {});
        clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofStreamElement(wildcardUpsert)));
        assertEquals(Response.Flags.COMMITTED, queue.take().getSecond().getFlags());
        assertTrue(queue.isEmpty());
        TimeUnit.MILLISECONDS.sleep(150);
        transactionId = "t3";
        clientManager.begin(transactionId, (s, r) -> ExceptionUtils.unchecked(() -> queue.put(Pair.of(s, r))), KeyAttributes.ofWildcardQueryElements(this.user, "user", userGateways, Collections.emptyList()));
        assertEquals(Response.Flags.ABORTED, queue.take().getSecond().getFlags());
        assertTrue(queue.isEmpty());
    }
}
Also used : Response(cz.o2.proxima.transaction.Response) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 28 with Response

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

the class TransactionLogObserverTest method testCreateTransactionDuplicate.

@Test(timeout = 10000)
public void testCreateTransactionDuplicate() 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> firstResponse = responseQueue.take();
    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.DUPLICATE, response.getSecond().getFlags());
    assertEquals(firstResponse.getSecond().getSeqId(), response.getSecond().getSeqId());
}
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 29 with Response

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

the class TransactionLogObserverTest method testCreateTransactionCommit.

@Test(timeout = 10000)
public void testCreateTransactionCommit() 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")));
    Pair<String, Response> response = responseQueue.take();
    assertEquals("commit", response.getFirst());
    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 30 with Response

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

the class TransactionLogObserverTest method testTransactionRollback.

@Test(timeout = 10000)
public void testTransactionRollback() 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")));
    // discard this
    responseQueue.take();
    clientManager.updateTransaction(transactionId, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user2", userGateways, 2L, "1")));
    Pair<String, Response> response = responseQueue.take();
    assertTrue(response.getFirst().startsWith("update."));
    assertEquals(Response.Flags.UPDATED, response.getSecond().getFlags());
    clientManager.rollback(transactionId);
    response = responseQueue.take();
    assertEquals("rollback", 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) 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