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);
}
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));
}
}
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();
}
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());
}
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());
}
Aggregations