Search in sources :

Example 96 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class ReplicationController method consumeLog.

private void consumeLog(DirectAttributeFamilyDescriptor primaryFamily, DirectAttributeFamilyDescriptor replicaFamily) {
    final CommitLogReader commitLog = primaryFamily.getCommitLogReader().orElseThrow(() -> new IllegalStateException(String.format("Failed to find commit-log reader in family %s.", primaryFamily)));
    final AttributeWriterBase writer = replicaFamily.getWriter().orElseThrow(() -> new IllegalStateException(String.format("Unable to get writer for family %s.", replicaFamily.getDesc().getName())));
    final StorageFilter filter = replicaFamily.getDesc().getFilter();
    final Set<AttributeDescriptor<?>> allowedAttributes = new HashSet<>(replicaFamily.getAttributes());
    final String name = replicaFamily.getDesc().getReplicationConsumerNameFactory().apply();
    log.info("Using consumer name {} to replicate family {}", name, replicaFamily.getDesc().getName());
    registerWriterTo(name, commitLog, allowedAttributes, filter, writer);
    log.info("Started consumer {} consuming from log {} with URI {} into {} attributes {}", name, commitLog, commitLog.getUri(), writer.getUri(), allowedAttributes);
}
Also used : StorageFilter(cz.o2.proxima.storage.StorageFilter) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) HashSet(java.util.HashSet)

Example 97 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class RetrieveService method noticeGetResult.

private void noticeGetResult(GetRequest getRequest, EntityDescriptor entity, AttributeDescriptor<Object> attribute, Optional<KeyValue<Object>> maybeValue) throws TransactionRejectedException {
    if (!getRequest.getTransactionId().isEmpty()) {
        final KeyAttribute ka;
        if (maybeValue.isPresent()) {
            ka = KeyAttributes.ofStreamElement(maybeValue.get());
        } else if (attribute.isWildcard()) {
            ka = KeyAttributes.ofMissingAttribute(entity, getRequest.getKey(), attribute, getRequest.getAttribute().substring(attribute.toAttributePrefix().length()));
        } else {
            ka = KeyAttributes.ofMissingAttribute(entity, getRequest.getKey(), attribute);
        }
        updateTransaction(getRequest.getTransactionId(), Collections.singletonList(ka));
    }
}
Also used : KeyAttribute(cz.o2.proxima.transaction.KeyAttribute)

Example 98 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class RetrieveService method get.

@Override
public void get(Rpc.GetRequest request, StreamObserver<Rpc.GetResponse> responseObserver) {
    Metrics.GET_REQUESTS.increment();
    try {
        if (request.getEntity().isEmpty() || request.getKey().isEmpty() || request.getAttribute().isEmpty()) {
            throw new Status(400, "Missing some required fields");
        }
        EntityDescriptor entity = repo.findEntity(request.getEntity()).orElseThrow(() -> new Status(404, "Entity " + request.getEntity() + " not found"));
        AttributeDescriptor<Object> attribute = entity.findAttribute(request.getAttribute()).orElseThrow(() -> new Status(404, "Entity " + request.getEntity() + " does not have attribute " + request.getAttribute()));
        RandomAccessReader reader = instantiateReader(attribute);
        synchronized (reader) {
            Optional<KeyValue<Object>> maybeValue = reader.get(request.getKey(), request.getAttribute(), attribute);
            noticeGetResult(request, entity, attribute, maybeValue);
            KeyValue<Object> kv = maybeValue.orElseThrow(() -> new Status(404, "Key " + request.getKey() + " and/or attribute " + request.getAttribute() + " not found"));
            logStatus("get", request, 200, "OK");
            responseObserver.onNext(Rpc.GetResponse.newBuilder().setStatus(200).setValue(ByteString.copyFrom(kv.getValue())).build());
        }
    } catch (Status s) {
        logStatus("get", request, s.statusCode, s.message);
        responseObserver.onNext(Rpc.GetResponse.newBuilder().setStatus(s.statusCode).setStatusMessage(s.message).build());
    } catch (TransactionRejectedException ex) {
        logStatus("get", request, 412, ex.getMessage());
        responseObserver.onNext(Rpc.GetResponse.newBuilder().setStatus(412).setStatusMessage(ex.getMessage()).build());
    } catch (Exception ex) {
        log.error("Failed to process request {}", request, ex);
        logStatus("get", request, 500, ex.getMessage());
        responseObserver.onNext(Rpc.GetResponse.newBuilder().setStatus(500).setStatusMessage(ex.getMessage()).build());
    }
    responseObserver.onCompleted();
}
Also used : EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) RandomAccessReader(cz.o2.proxima.direct.randomaccess.RandomAccessReader) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException)

Example 99 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class ReplicationControllerTest method testEventReplicationWithReadOfInvalidAttributeBulk.

@Test
public void testEventReplicationWithReadOfInvalidAttributeBulk() {
    List<StreamElement> written = new ArrayList<>();
    EntityDescriptor gateway = repo.getEntity("gateway");
    AttributeDescriptor<byte[]> armed = gateway.getAttribute("armed");
    AttributeDescriptor<byte[]> status = gateway.getAttribute("status");
    CommitLogObserver observer = controller.createBulkObserver("consumer", direct.getCommitLogReader(status).orElseThrow(() -> new IllegalArgumentException("Missing commit log reader for data")), Sets.newHashSet(status), new PassthroughFilter(), fakeBulkWriter(written, stamp -> stamp == 100));
    AtomicInteger commits = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        long watermark = i * 20;
        observer.onNext(getUpdate(gateway, armed, now), new OnNextContext() {

            @Override
            public OffsetCommitter committer() {
                return (success, error) -> commits.incrementAndGet();
            }

            @Override
            public Partition getPartition() {
                return null;
            }

            @Override
            public long getWatermark() {
                return watermark;
            }

            @Override
            public Offset getOffset() {
                return null;
            }
        });
    }
    assertEquals(0, commits.get());
    assertEquals(0, written.size());
}
Also used : Partition(cz.o2.proxima.storage.Partition) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) CompletableFuture(java.util.concurrent.CompletableFuture) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) UnaryPredicate(cz.o2.proxima.functional.UnaryPredicate) URI(java.net.URI) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) PassthroughFilter(cz.o2.proxima.storage.PassthroughFilter) Metrics(cz.o2.proxima.server.metrics.Metrics) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) BulkAttributeWriter(cz.o2.proxima.direct.core.BulkAttributeWriter) InMemStorage(cz.o2.proxima.direct.storage.InMemStorage) Repository(cz.o2.proxima.repository.Repository) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Sets(com.google.common.collect.Sets) Offset(cz.o2.proxima.direct.commitlog.Offset) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TransformationObserver(cz.o2.proxima.direct.transform.TransformationObserver) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Partition(cz.o2.proxima.storage.Partition) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) Offset(cz.o2.proxima.direct.commitlog.Offset) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) PassthroughFilter(cz.o2.proxima.storage.PassthroughFilter) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) Test(org.junit.Test)

Example 100 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class RetrieveServiceTest method testListValidWithOffset.

@Test
public void testListValidWithOffset() {
    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).setOffset("wildcard.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());
    assertEquals(1, response.getValueCount());
    assertEquals("wildcard.2", response.getValue(0).getAttribute());
    assertArrayEquals(new byte[] { 1, 2, 3, 4 }, response.getValue(0).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)

Aggregations

EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)71 Test (org.junit.Test)59 StreamElement (cz.o2.proxima.storage.StreamElement)51 ArrayList (java.util.ArrayList)46 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)33 CountDownLatch (java.util.concurrent.CountDownLatch)33 List (java.util.List)29 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)24 Slf4j (lombok.extern.slf4j.Slf4j)24 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)22 Map (java.util.Map)22 Collectors (java.util.stream.Collectors)22 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)21 RandomAccessReader (cz.o2.proxima.direct.randomaccess.RandomAccessReader)21 AttributeFamilyDescriptor (cz.o2.proxima.repository.AttributeFamilyDescriptor)20 Arrays (java.util.Arrays)20 Collections (java.util.Collections)20 Optional (java.util.Optional)20 Set (java.util.Set)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20