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