Search in sources :

Example 31 with Response

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

the class TransactionLogObserverTest method testPostCommitOutOfOrder.

@Test(timeout = 10000)
public void testPostCommitOutOfOrder() throws InterruptedException {
    createObserver();
    State committed = State.open(2001L, now, Collections.emptyList()).committed(Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2001L, "1")));
    observer.transactionPostCommit(committed);
    State committed2 = State.open(1001L, now - 1, Collections.emptyList()).committed(Collections.singletonList(KeyAttributes.ofStreamElement(userGateways.delete(1001L, "user", "1", now - 1))));
    observer.transactionPostCommit(committed2);
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    try (ClientTransactionManager clientManager = direct.getClientTransactionManager()) {
        String t = "t";
        clientManager.begin(t, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), KeyAttributes.ofWildcardQueryElements(user, "user", userGateways, Collections.emptyList()));
        Pair<String, Response> response = responseQueue.take();
        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) State(cz.o2.proxima.transaction.State) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 32 with Response

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

the class TransactionLogObserverTest method testCreateTransactionCommitWithConflictInInputs.

@Test(timeout = 10000)
public void testCreateTransactionCommitWithConflictInInputs() throws InterruptedException {
    createObserver();
    ClientTransactionManager clientManager = direct.getClientTransactionManager();
    String t1 = UUID.randomUUID().toString();
    String t2 = UUID.randomUUID().toString();
    List<KeyAttribute> inputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(this.user, "user", userGateways, 1L, "1"));
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), inputs);
    responseQueue.take();
    clientManager.commit(t1, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(this.user, "user", userGateways, 2L, "1")));
    Pair<String, Response> response = responseQueue.take();
    assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
    clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), inputs);
    response = responseQueue.take();
    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 33 with Response

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

the class TransactionLogObserverTest method testTransactionUpdate.

@Test(timeout = 10000)
public void testTransactionUpdate() 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());
    assertFalse(response.getSecond().hasStamp());
    do {
        State currentState = direct.getServerTransactionManager().getCurrentState(transactionId);
        assertNotNull(currentState);
        if (currentState.getInputAttributes().size() == 2) {
            assertEquals("user", Iterables.get(currentState.getInputAttributes(), 0).getKey());
            assertEquals("user2", Iterables.get(currentState.getInputAttributes(), 1).getKey());
            break;
        }
    } while (true);
}
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) State(cz.o2.proxima.transaction.State) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 34 with Response

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

the class RetrieveServiceTest method testGetValidWithTransaction.

@Test
public void testGetValidWithTransaction() {
    EntityDescriptor entity = server.repo.getEntity("dummy");
    AttributeDescriptor<?> attribute = entity.getAttribute("data");
    String key = "my-fancy-entity-key";
    Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, 1000L, key, attribute.getName(), System.currentTimeMillis(), new byte[] { 1, 2, 3 }), (s, err) -> {
    });
    String transactionId = UUID.randomUUID().toString();
    Rpc.GetRequest request = Rpc.GetRequest.newBuilder().setEntity(entity.getName()).setAttribute(attribute.getName()).setKey(key).setTransactionId(transactionId).build();
    final List<Rpc.GetResponse> responses = new ArrayList<>();
    final AtomicBoolean finished = new AtomicBoolean(false);
    final StreamObserver<Rpc.GetResponse> responseObserver;
    responseObserver = new StreamObserver<Rpc.GetResponse>() {

        @Override
        public void onNext(Rpc.GetResponse res) {
            responses.add(res);
        }

        @Override
        public void onError(Throwable thrwbl) {
            throw new RuntimeException(thrwbl);
        }

        @Override
        public void onCompleted() {
            finished.set(true);
        }
    };
    retrieve.get(request, responseObserver);
    assertTrue(finished.get());
    assertEquals(1, responses.size());
    Rpc.GetResponse response = responses.get(0);
    assertEquals("Error: " + response.getStatus() + ": " + response.getStatusMessage(), 200, response.getStatus());
    assertArrayEquals(new byte[] { 1, 2, 3 }, response.getValue().toByteArray());
    assertEquals(1, transactionUpdates.size());
    assertTrue(transactionUpdates.containsKey(transactionId));
    assertEquals(Collections.singletonList(KeyAttributes.ofAttributeDescriptor(entity, key, attribute, 1000L)), transactionUpdates.get(transactionId));
}
Also used : Rpc(cz.o2.proxima.proto.service.Rpc) ArrayList(java.util.ArrayList) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 35 with Response

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

the class RetrieveServiceTest method testListNotFound.

@Test
public void testListNotFound() {
    EntityDescriptor entity = server.repo.getEntity("dummy");
    AttributeDescriptor<?> attribute = entity.getAttribute("wildcard.*");
    String key = "my-fancy-entity-key";
    Rpc.ListRequest request = Rpc.ListRequest.newBuilder().setEntity(entity.getName()).setWildcardPrefix("wildcard").setKey(key).setLimit(1).build();
    final List<Rpc.ListResponse> responses = new ArrayList<>();
    final AtomicBoolean finished = new AtomicBoolean(false);
    final StreamObserver<Rpc.ListResponse> responseObserver;
    responseObserver = new StreamObserver<Rpc.ListResponse>() {

        @Override
        public void onNext(Rpc.ListResponse res) {
            responses.add(res);
        }

        @Override
        public void onError(Throwable thrwbl) {
            throw new RuntimeException(thrwbl);
        }

        @Override
        public void onCompleted() {
            finished.set(true);
        }
    };
    retrieve.listAttributes(request, responseObserver);
    assertTrue(finished.get());
    assertEquals(1, responses.size());
    Rpc.ListResponse response = responses.get(0);
    assertEquals(200, response.getStatus());
    assertTrue(response.getValueList().isEmpty());
}
Also used : Rpc(cz.o2.proxima.proto.service.Rpc) ArrayList(java.util.ArrayList) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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