Search in sources :

Example 21 with Response

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

the class TransactionCommitTransformationTest method testTransformTransactionUpdate.

@Test
public void testTransformTransactionUpdate() throws InterruptedException {
    long now = System.currentTimeMillis();
    Commit commit = Commit.of(Arrays.asList(new TransactionUpdate("all-transaction-commit-log-state", StreamElement.upsert(transaction, stateDesc, UUID.randomUUID().toString(), "t", stateDesc.getName(), now, stateDesc.getValueSerializer().serialize(State.empty()))), new TransactionUpdate("all-transaction-commit-log-response", StreamElement.upsert(transaction, responseDesc, UUID.randomUUID().toString(), "t", responseDesc.toAttributePrefix() + "1", now, responseDesc.getValueSerializer().serialize(Response.forRequest(Request.builder().responsePartitionId(0).build()))))));
    List<StreamElement> requests = new ArrayList<>();
    List<StreamElement> states = new ArrayList<>();
    CommitLogObserver requestObserver = LogObserverUtils.toList(requests, ign -> {
    });
    CommitLogObserver stateObserver = LogObserverUtils.toList(states, ign -> {
    });
    Optionals.get(direct.getFamilyByName("all-transaction-commit-log-state").getCommitLogReader()).observe("first", stateObserver);
    Optionals.get(direct.getFamilyByName("all-transaction-commit-log-response").getCommitLogReader()).observe("second", requestObserver);
    transformation.transform(StreamElement.upsert(transaction, commitDesc, UUID.randomUUID().toString(), UUID.randomUUID().toString(), commitDesc.getName(), System.currentTimeMillis(), commitDesc.getValueSerializer().serialize(commit)), (succ, exc) -> assertTrue(succ));
    assertEquals(1, requests.size());
    assertEquals(1, states.size());
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Commit(cz.o2.proxima.transaction.Commit) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) TransactionUpdate(cz.o2.proxima.transaction.Commit.TransactionUpdate) Test(org.junit.Test)

Example 22 with Response

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

the class TransactionResourceManagerTest method testTransactionRequestRollback.

@Test(timeout = 10000)
public void testTransactionRequestRollback() throws InterruptedException {
    try (TransactionResourceManager manager = TransactionResourceManager.create(direct)) {
        String transactionId = UUID.randomUUID().toString();
        BlockingQueue<Pair<String, Response>> receivedResponses = new ArrayBlockingQueue<>(1);
        // create a simple ping-pong communication
        runObservations(manager, "requests", (ingest, context) -> {
            if (ingest.getAttributeDescriptor().equals(requestDesc)) {
                String key = ingest.getKey();
                String requestId = requestDesc.extractSuffix(ingest.getAttribute());
                Request request = Optionals.get(requestDesc.valueOf(ingest));
                CountDownLatch latch = new CountDownLatch(1);
                CommitCallback commit = (succ, exc) -> {
                    latch.countDown();
                    context.commit(succ, exc);
                };
                long stamp = System.currentTimeMillis();
                if (request.getFlags() == Request.Flags.ROLLBACK) {
                    manager.writeResponseAndUpdateState(key, State.empty(), requestId, Response.forRequest(request).aborted(), commit);
                } else if (request.getFlags() == Request.Flags.OPEN) {
                    manager.writeResponseAndUpdateState(key, State.open(1L, stamp, new HashSet<>(request.getInputAttributes())), requestId, Response.forRequest(request).open(1L, stamp), commit);
                }
                ExceptionUtils.ignoringInterrupted(latch::await);
            } else {
                context.confirm();
            }
            return true;
        });
        manager.begin(transactionId, (k, v) -> receivedResponses.add(Pair.of(k, v)), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(gateway, "gw1", status, 1L)));
        receivedResponses.take();
        manager.rollback(transactionId);
        Pair<String, Response> response = receivedResponses.take();
        assertEquals("rollback", response.getFirst());
        assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
        State currentState = manager.getCurrentState(transactionId);
        assertEquals(State.Flags.UNKNOWN, currentState.getFlags());
    }
}
Also used : Iterables(com.google.common.collect.Iterables) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ArrayList(java.util.ArrayList) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) HashSet(java.util.HashSet) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) Optionals(cz.o2.proxima.util.Optionals) DeclaredThreadSafe(cz.o2.proxima.annotations.DeclaredThreadSafe) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Request(cz.o2.proxima.transaction.Request) CachedTransaction(cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) 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) Request(cz.o2.proxima.transaction.Request) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) State(cz.o2.proxima.transaction.State) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 23 with Response

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

the class TransactionalOnlineAttributeWriterTest method setUp.

@Before
public void setUp() {
    toReturn.clear();
    manager = direct.getServerTransactionManager();
    runObservations(manager, "test", (ingest, context) -> {
        String transactionId = ingest.getKey();
        if (ingest.getAttributeDescriptor().equals(manager.getRequestDesc())) {
            String responseId = manager.getRequestDesc().extractSuffix(ingest.getAttribute());
            Request request = Optionals.get(manager.getRequestDesc().valueOf(ingest));
            Response response = Optional.ofNullable(toReturn.poll()).orElse(Response.empty());
            State state = manager.getCurrentState(transactionId);
            if (response.getFlags() == Response.Flags.OPEN) {
                state = State.open(response.getSeqId(), response.getStamp(), request.getInputAttributes());
            }
            manager.writeResponseAndUpdateState(transactionId, state, responseId, response, context::commit);
        } else {
            context.confirm();
        }
        return true;
    });
    TransformationRunner.runTransformations(repo, direct);
}
Also used : Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) Request(cz.o2.proxima.transaction.Request) Before(org.junit.Before)

Example 24 with Response

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

the class IngestClient method newStatusObserver.

private StreamObserver<Rpc.StatusBulk> newStatusObserver() {
    return new StreamObserver<Rpc.StatusBulk>() {

        @Override
        public void onNext(Rpc.StatusBulk bulk) {
            for (Rpc.Status status : bulk.getStatusList()) {
                final String uuid = status.getUuid();
                final Request request = inFlightRequests.remove(uuid);
                if (request == null) {
                    log.warn("Received response for unknown message {}", TextFormat.shortDebugString(status));
                } else {
                    synchronized (inFlightRequests) {
                        inFlightRequests.notifyAll();
                    }
                    request.setStatus(status);
                }
            }
        }

        @Override
        public void onError(Throwable thrwbl) {
            IngestClient.this.onError(thrwbl);
        }

        @Override
        public void onCompleted() {
            synchronized (inFlightRequests) {
                inFlightRequests.clear();
            }
            closedLatch.countDown();
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Rpc(cz.o2.proxima.proto.service.Rpc) ByteString(com.google.protobuf.ByteString)

Example 25 with Response

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

the class RemoteConsumer method observer.

StreamObserver<Item> observer() {
    if (channel == null) {
        channel = ManagedChannelBuilder.forAddress(hostname, port).usePlaintext().build();
        CountDownLatch connected = new CountDownLatch(1);
        channel.notifyWhenStateChanged(ConnectivityState.READY, connected::countDown);
        ExceptionUtils.ignoringInterrupted(() -> {
            if (!connected.await(1, TimeUnit.SECONDS)) {
                log.warn("Timeout waiting for channel to become connected. Skipping.");
            }
        });
    }
    if (stub == null) {
        stub = CollectServiceGrpc.newStub(channel);
    }
    if (observer == null) {
        terminateFuture = new CompletableFuture<>();
        CountDownLatch initLatch = new CountDownLatch(1);
        observer = stub.collect(new StreamObserver<Response>() {

            @Override
            public void onNext(Response response) {
                if (response.getStatus() == CONTINUE) {
                    initLatch.countDown();
                } else if (response.getStatus() == OK) {
                    terminateFuture.complete(null);
                }
            }

            @Override
            public void onError(Throwable throwable) {
                terminateFuture.completeExceptionally(throwable);
                observer = null;
            }

            @Override
            public void onCompleted() {
            // nop
            }
        });
        initLatch.countDown();
    }
    return observer;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Response(cz.o2.proxima.beam.tools.proto.service.Collect.Response) CountDownLatch(java.util.concurrent.CountDownLatch)

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