use of cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction in project proxima-platform by O2-Czech-Republic.
the class TransactionResourceManager method writeResponseAndUpdateState.
@Override
public void writeResponseAndUpdateState(String transactionId, State updateState, String responseId, Response response, CommitCallback callback) {
CachedTransaction cachedTransaction = openTransactionMap.get(transactionId);
if (cachedTransaction != null) {
DirectAttributeFamilyDescriptor responseFamily = cachedTransaction.getResponseFamily();
DirectAttributeFamilyDescriptor stateFamily = cachedTransaction.getStateFamily();
final OnlineAttributeWriter writer = cachedTransaction.getCommitWriter();
final CachedView stateView = cachedTransaction.getStateView();
long now = System.currentTimeMillis();
StreamElement stateUpsert = getStateDesc().upsert(transactionId, now, updateState);
Commit commit = Commit.of(Arrays.asList(new Commit.TransactionUpdate(stateFamily.getDesc().getName(), stateUpsert), new Commit.TransactionUpdate(responseFamily.getDesc().getName(), getResponseDesc().upsert(transactionId, responseId, now, response))));
synchronized (stateView) {
ensureTransactionOpen(transactionId, updateState);
stateView.cache(stateUpsert);
}
synchronized (writer) {
writer.write(getCommitDesc().upsert(transactionId, System.currentTimeMillis(), commit), callback);
}
} else {
log.warn("Transaction {} is not open, don't have a writer to return response {}", transactionId, response);
callback.commit(true, null);
}
}
use of cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction in project proxima-platform by O2-Czech-Republic.
the class TransactionResourceManagerTest method testCreateCachedTransactionWhenMissing.
@Test(timeout = 10000)
public void testCreateCachedTransactionWhenMissing() {
KeyAttribute ka = KeyAttributes.ofAttributeDescriptor(gateway, "g", status, 1L);
long stamp = System.currentTimeMillis();
try (TransactionResourceManager manager = TransactionResourceManager.create(direct)) {
CachedTransaction transaction = manager.createCachedTransaction("transaction", State.open(1L, stamp, Collections.singletonList(ka)));
assertEquals("transaction", transaction.getTransactionId());
}
try (TransactionResourceManager manager = TransactionResourceManager.create(direct)) {
CachedTransaction transaction = manager.createCachedTransaction("transaction", State.open(2L, stamp + 1, Collections.emptyList()).committed(Collections.singletonList(ka)));
assertEquals("transaction", transaction.getTransactionId());
}
}
use of cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction in project proxima-platform by O2-Czech-Republic.
the class TransactionResourceManagerTest method testTransactionWriteToCorrectFamily.
@Test(timeout = 10000)
public void testTransactionWriteToCorrectFamily() throws InterruptedException {
KeyAttribute ka = KeyAttributes.ofAttributeDescriptor(user, "u", allGateways, 1L, "gw");
long stamp = System.currentTimeMillis();
try (TransactionResourceManager manager = TransactionResourceManager.create(direct)) {
CountDownLatch repartitionLatch = new CountDownLatch(1);
runObservations(manager, "name", new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
return true;
}
@Override
public void onRepartition(OnRepartitionContext context) {
repartitionLatch.countDown();
}
});
CachedTransaction transaction = manager.createCachedTransaction("transaction", State.open(2L, stamp + 1, Collections.emptyList()).committed(Collections.singletonList(ka)), (a, b) -> {
});
transaction.open(Collections.singletonList(ka));
repartitionLatch.await();
assertEquals(Optionals.get(direct.getFamilyByName("all-transaction-commit-log-request").getWriter()), transaction.getRequestWriter().getSecond());
assertEquals(Optionals.get(direct.getFamilyByName("transactions-commit").getWriter()), transaction.getCommitWriter());
assertEquals(direct.getFamilyByName("all-transaction-commit-log-state").getCachedView().get(), transaction.getStateView());
}
}
Aggregations