Search in sources :

Example 1 with CachedTransaction

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);
    }
}
Also used : DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) CachedView(cz.o2.proxima.direct.view.CachedView) Commit(cz.o2.proxima.transaction.Commit) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) StreamElement(cz.o2.proxima.storage.StreamElement)

Example 2 with CachedTransaction

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());
    }
}
Also used : CachedTransaction(cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Test(org.junit.Test)

Example 3 with CachedTransaction

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());
    }
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) CachedTransaction(cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction) StreamElement(cz.o2.proxima.storage.StreamElement) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CachedTransaction (cz.o2.proxima.direct.transaction.TransactionResourceManager.CachedTransaction)2 StreamElement (cz.o2.proxima.storage.StreamElement)2 KeyAttribute (cz.o2.proxima.transaction.KeyAttribute)2 Test (org.junit.Test)2 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)1 DirectAttributeFamilyDescriptor (cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor)1 OnlineAttributeWriter (cz.o2.proxima.direct.core.OnlineAttributeWriter)1 CachedView (cz.o2.proxima.direct.view.CachedView)1 Commit (cz.o2.proxima.transaction.Commit)1 CountDownLatch (java.util.concurrent.CountDownLatch)1