Search in sources :

Example 16 with Response

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

the class RetrieveServiceTest method testListValidWithTransaction.

@Test
public void testListValidWithTransaction() {
    EntityDescriptor entity = server.repo.getEntity("dummy");
    AttributeDescriptor<?> attribute = entity.getAttribute("wildcard.*");
    String key = "my-fancy-entity-key";
    List<StreamElement> elements = Arrays.asList(StreamElement.upsert(entity, attribute, 1000L, key, "wildcard.1", System.currentTimeMillis(), new byte[] { 1, 2, 3 }), StreamElement.upsert(entity, attribute, 1001L, key, "wildcard.2", System.currentTimeMillis(), new byte[] { 1, 2, 3, 4 }));
    elements.forEach(el -> Optionals.get(server.direct.getWriter(el.getAttributeDescriptor())).write(el, (succ, exc) -> {
    }));
    String transactionId = UUID.randomUUID().toString();
    Rpc.ListRequest request = Rpc.ListRequest.newBuilder().setEntity(entity.getName()).setWildcardPrefix("wildcard").setKey(key).setTransactionId(transactionId).build();
    List<Rpc.ListResponse> responses = new ArrayList<>();
    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() {
        }
    };
    retrieve.listAttributes(request, responseObserver);
    assertEquals(1, responses.size());
    Rpc.ListResponse response = responses.get(0);
    assertEquals(200, response.getStatus());
    assertEquals(2, response.getValueCount());
    assertEquals("wildcard.1", response.getValue(0).getAttribute());
    assertArrayEquals(new byte[] { 1, 2, 3 }, response.getValue(0).getValue().toByteArray());
    assertEquals("wildcard.2", response.getValue(1).getAttribute());
    assertArrayEquals(new byte[] { 1, 2, 3, 4 }, response.getValue(1).getValue().toByteArray());
    assertEquals(1, transactionUpdates.size());
    assertTrue(transactionUpdates.containsKey(transactionId));
    assertEquals(KeyAttributes.ofWildcardQueryElements(entity, key, attribute, elements), transactionUpdates.get(transactionId));
}
Also used : Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StreamObserver(io.grpc.stub.StreamObserver) TransactionContext(cz.o2.proxima.server.transaction.TransactionContext) StreamElement(cz.o2.proxima.storage.StreamElement) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Map(java.util.Map) ConfigFactory(com.typesafe.config.ConfigFactory) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Assert.assertTrue(org.junit.Assert.assertTrue) Rpc(cz.o2.proxima.proto.service.Rpc) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) Test(org.junit.Test) UUID(java.util.UUID) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) List(java.util.List) ExtendedMessage(cz.o2.proxima.server.test.Test.ExtendedMessage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Rpc(cz.o2.proxima.proto.service.Rpc) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Test(org.junit.Test)

Example 17 with Response

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

the class RetrieveServiceTest method testListValidWithLimitWithTransaction.

@Test
public void testListValidWithLimitWithTransaction() {
    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).setTransactionId(UUID.randomUUID().toString()).setLimit(1).build();
    final List<Rpc.ListResponse> responses = new ArrayList<>();
    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() {
        }
    };
    retrieve.listAttributes(request, responseObserver);
    assertEquals(1, responses.size());
    Rpc.ListResponse response = responses.get(0);
    assertEquals(400, response.getStatus());
}
Also used : Rpc(cz.o2.proxima.proto.service.Rpc) ArrayList(java.util.ArrayList) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Test(org.junit.Test)

Example 18 with Response

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

the class RetrieveServiceTest method testListValid.

@Test
public void testListValid() {
    EntityDescriptor entity = server.repo.getEntity("dummy");
    AttributeDescriptor<?> attribute = entity.getAttribute("wildcard.*");
    String key = "my-fancy-entity-key";
    Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, UUID.randomUUID().toString(), key, "wildcard.1", System.currentTimeMillis(), new byte[] { 1, 2, 3 }), (s, err) -> {
    });
    Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, UUID.randomUUID().toString(), key, "wildcard.2", System.currentTimeMillis(), new byte[] { 1, 2, 3, 4 }), (s, err) -> {
    });
    Rpc.ListRequest request = Rpc.ListRequest.newBuilder().setEntity(entity.getName()).setWildcardPrefix("wildcard").setKey(key).build();
    List<Rpc.ListResponse> responses = new ArrayList<>();
    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());
    assertEquals(2, response.getValueCount());
    assertEquals("wildcard.1", response.getValue(0).getAttribute());
    assertArrayEquals(new byte[] { 1, 2, 3 }, response.getValue(0).getValue().toByteArray());
    assertEquals("wildcard.2", response.getValue(1).getAttribute());
    assertArrayEquals(new byte[] { 1, 2, 3, 4 }, response.getValue(1).getValue().toByteArray());
}
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 19 with Response

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

the class RetrieveServiceTest method testGetValidExtendedScheme.

@Test
public void testGetValidExtendedScheme() throws InvalidProtocolBufferException {
    EntityDescriptor entity = server.repo.getEntity("test");
    AttributeDescriptor<?> attribute = entity.getAttribute("data");
    String key = "my-fancy-entity-key";
    ExtendedMessage payload = ExtendedMessage.newBuilder().setFirst(1).setSecond(2).build();
    Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, UUID.randomUUID().toString(), key, attribute.getName(), System.currentTimeMillis(), payload.toByteArray()), (s, err) -> {
    });
    Rpc.GetRequest request = Rpc.GetRequest.newBuilder().setEntity(entity.getName()).setAttribute(attribute.getName()).setKey(key).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());
    assertEquals(payload, ExtendedMessage.parseFrom(response.getValue().toByteArray()));
}
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) ExtendedMessage(cz.o2.proxima.server.test.Test.ExtendedMessage) Test(org.junit.Test)

Example 20 with Response

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

the class RetrieveService method listAttributes.

@Override
public void listAttributes(Rpc.ListRequest request, StreamObserver<Rpc.ListResponse> responseObserver) {
    try {
        Metrics.LIST_REQUESTS.increment();
        if (request.getEntity().isEmpty() || request.getKey().isEmpty() || request.getWildcardPrefix().isEmpty()) {
            throw new Status(400, "Missing some required fields");
        }
        if (!request.getTransactionId().isEmpty() && (!request.getOffset().isEmpty() || request.getLimit() > 0)) {
            throw new Status(400, "Unsupported: transactions do not support limited list requests, currently");
        }
        EntityDescriptor entity = repo.findEntity(request.getEntity()).orElseThrow(() -> new Status(404, "Entity " + request.getEntity() + " not found"));
        AttributeDescriptor<Object> wildcard = entity.findAttribute(request.getWildcardPrefix() + ".*").orElseThrow(() -> new Status(404, "Entity " + request.getEntity() + " does not have wildcard attribute " + request.getWildcardPrefix()));
        RandomAccessReader reader = instantiateReader(wildcard);
        List<KeyValue<Object>> kvs = new ArrayList<>();
        Rpc.ListResponse.Builder response = Rpc.ListResponse.newBuilder().setStatus(200);
        synchronized (reader) {
            reader.scanWildcard(request.getKey(), wildcard, reader.fetchOffset(RandomAccessReader.Listing.ATTRIBUTE, request.getOffset()), request.getLimit() > 0 ? request.getLimit() : -1, kvs::add);
            kvs.forEach(kv -> response.addValue(Rpc.ListResponse.AttrValue.newBuilder().setAttribute(kv.getAttribute()).setValue(ByteString.copyFrom(kv.getValue()))));
        }
        noticeListResult(request, entity, wildcard, kvs);
        replyLogged(responseObserver, request, response.build());
    } catch (Status s) {
        replyStatusLogged(responseObserver, request, s.statusCode, s.message);
    } catch (Exception ex) {
        log.error("Failed to process request {}", request, ex);
        replyStatusLogged(responseObserver, request, 500, ex.getMessage());
    }
    responseObserver.onCompleted();
}
Also used : EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) RandomAccessReader(cz.o2.proxima.direct.randomaccess.RandomAccessReader) ArrayList(java.util.ArrayList) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException)

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