Search in sources :

Example 1 with TransactionalOnlineAttributeWriter

use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class TransactionIT method removeSingleDevice.

private void removeSingleDevice(int numUsers) throws InterruptedException {
    do {
        TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(device)).transactional();
        try (Transaction t = writer.begin()) {
            String userId = "user" + random.nextInt(numUsers);
            List<StreamElement> devices = new ArrayList<>();
            view.scanWildcard(userId, device, devices::add);
            List<KeyAttribute> keyAttributes = KeyAttributes.ofWildcardQueryElements(user, userId, device, devices);
            if (devices.isEmpty()) {
                continue;
            }
            long stamp = System.currentTimeMillis();
            t.update(keyAttributes);
            String name = device.extractSuffix(devices.get(random.nextInt(devices.size())).getAttribute());
            StreamElement deviceUpdate = device.delete(userId, name, stamp);
            StreamElement numDevicesUpdate = numDevices.upsert(userId, "all", stamp, devices.size() - 1);
            CountDownLatch latch = new CountDownLatch(1);
            t.commitWrite(Arrays.asList(deviceUpdate, numDevicesUpdate), (succ, exc) -> latch.countDown());
            latch.await();
            break;
        } catch (TransactionRejectedException e) {
        // repeat
        }
    } while (true);
}
Also used : Transaction(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction) TransactionalOnlineAttributeWriter(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with TransactionalOnlineAttributeWriter

use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class TransactionIT_Large method writeSingleDevice.

private void writeSingleDevice(int numUsers, boolean intoAll) throws InterruptedException {
    String name = UUID.randomUUID().toString();
    String userId = "user" + random.nextInt(numUsers);
    do {
        TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(device)).transactional();
        long stamp = System.currentTimeMillis();
        try (Transaction t = writer.begin()) {
            List<StreamElement> devices = new ArrayList<>();
            view.scanWildcard(userId, device, devices::add);
            List<KeyAttribute> keyAttributes = KeyAttributes.ofWildcardQueryElements(user, userId, device, devices);
            t.update(keyAttributes);
            StreamElement deviceUpdate = device.upsert(userId, name, stamp, new byte[] {});
            final StreamElement numDevicesUpdate;
            int count = devices.size() + 1;
            if (intoAll) {
                numDevicesUpdate = numDevices.upsert(userId, "all", stamp, count);
            } else {
                numDevicesUpdate = numDevices.upsert(userId, String.valueOf(count), stamp, count);
            }
            CountDownLatch latch = new CountDownLatch(1);
            t.commitWrite(Arrays.asList(deviceUpdate, numDevicesUpdate), (succ, exc) -> latch.countDown());
            latch.await();
            break;
        } catch (TransactionRejectedException e) {
        // repeat
        }
    } while (true);
}
Also used : TransactionalOnlineAttributeWriter(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) CountDownLatch(java.util.concurrent.CountDownLatch) Transaction(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException)

Example 3 with TransactionalOnlineAttributeWriter

use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class TransactionIT_Large method transferAmountRandomly.

private void transferAmountRandomly(int numUsers) throws InterruptedException {
    int first = random.nextInt(numUsers);
    int second = (first + 1 + random.nextInt(numUsers - 1)) % numUsers;
    String userFirst = "user" + first;
    String userSecond = "user" + second;
    double swap = random.nextDouble() * 1000;
    TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(amount)).transactional();
    do {
        Optional<KeyValue<Double>> firstAmount = view.get(userFirst, amount);
        Optional<KeyValue<Double>> secondAmount = view.get(userSecond, amount);
        try (Transaction t = writer.begin()) {
            t.update(Arrays.asList(KeyAttributes.ofAttributeDescriptor(user, userFirst, amount, firstAmount.map(KeyValue::getSequentialId).orElse(1L)), KeyAttributes.ofAttributeDescriptor(user, userSecond, amount, secondAmount.map(KeyValue::getSequentialId).orElse(1L))));
            double firstWillHave = firstAmount.map(KeyValue::getParsedRequired).orElse(0.0) - swap;
            double secondWillHave = secondAmount.map(KeyValue::getParsedRequired).orElse(0.0) + swap;
            List<StreamElement> outputs = Arrays.asList(amount.upsert(userFirst, System.currentTimeMillis(), firstWillHave), amount.upsert(userSecond, System.currentTimeMillis(), secondWillHave));
            CountDownLatch latch = new CountDownLatch(1);
            t.commitWrite(outputs, (succ, exc) -> latch.countDown());
            latch.await();
            break;
        } catch (TransactionRejectedException e) {
        // repeat
        }
    } while (true);
}
Also used : KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) Transaction(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction) TransactionalOnlineAttributeWriter(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with TransactionalOnlineAttributeWriter

use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class TransactionIT method transferAmountRandomly.

private void transferAmountRandomly(int numUsers) throws InterruptedException {
    int first = random.nextInt(numUsers);
    int second = (first + 1 + random.nextInt(numUsers - 1)) % numUsers;
    String userFirst = "user" + first;
    String userSecond = "user" + second;
    double swap = random.nextDouble() * 1000;
    TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(amount)).transactional();
    do {
        Optional<KeyValue<Double>> firstAmount = view.get(userFirst, amount);
        Optional<KeyValue<Double>> secondAmount = view.get(userSecond, amount);
        try (Transaction t = writer.begin()) {
            t.update(Arrays.asList(KeyAttributes.ofAttributeDescriptor(user, userFirst, amount, firstAmount.map(KeyValue::getSequentialId).orElse(1L)), KeyAttributes.ofAttributeDescriptor(user, userSecond, amount, secondAmount.map(KeyValue::getSequentialId).orElse(1L))));
            double firstWillHave = firstAmount.map(KeyValue::getParsedRequired).orElse(0.0) - swap;
            double secondWillHave = secondAmount.map(KeyValue::getParsedRequired).orElse(0.0) + swap;
            List<StreamElement> outputs = Arrays.asList(amount.upsert(userFirst, System.currentTimeMillis(), firstWillHave), amount.upsert(userSecond, System.currentTimeMillis(), secondWillHave));
            CountDownLatch latch = new CountDownLatch(1);
            t.commitWrite(outputs, (succ, exc) -> latch.countDown());
            latch.await();
            break;
        } catch (TransactionRejectedException e) {
        // repeat
        }
    } while (true);
}
Also used : KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) Transaction(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction) TransactionalOnlineAttributeWriter(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with TransactionalOnlineAttributeWriter

use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class TransactionIT_Large method removeSingleDevice.

private void removeSingleDevice(int numUsers) throws InterruptedException {
    do {
        TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(device)).transactional();
        try (Transaction t = writer.begin()) {
            String userId = "user" + random.nextInt(numUsers);
            List<StreamElement> devices = new ArrayList<>();
            view.scanWildcard(userId, device, devices::add);
            List<KeyAttribute> keyAttributes = KeyAttributes.ofWildcardQueryElements(user, userId, device, devices);
            if (devices.isEmpty()) {
                continue;
            }
            long stamp = System.currentTimeMillis();
            t.update(keyAttributes);
            String name = device.extractSuffix(devices.get(random.nextInt(devices.size())).getAttribute());
            StreamElement deviceUpdate = device.delete(userId, name, stamp);
            StreamElement numDevicesUpdate = numDevices.upsert(userId, "all", stamp, devices.size() - 1);
            CountDownLatch latch = new CountDownLatch(1);
            t.commitWrite(Arrays.asList(deviceUpdate, numDevicesUpdate), (succ, exc) -> latch.countDown());
            latch.await();
            break;
        } catch (TransactionRejectedException e) {
        // repeat
        }
    } while (true);
}
Also used : Transaction(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction) TransactionalOnlineAttributeWriter(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Transaction (cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 TransactionalOnlineAttributeWriter (cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter)6 TransactionRejectedException (cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException)6 StreamElement (cz.o2.proxima.storage.StreamElement)6 KeyAttribute (cz.o2.proxima.transaction.KeyAttribute)4 ArrayList (java.util.ArrayList)4 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)3 CachedView (cz.o2.proxima.direct.view.CachedView)1 Test (org.junit.Test)1