Search in sources :

Example 1 with KeyValue

use of cz.o2.proxima.direct.randomaccess.KeyValue in project proxima-platform by O2-Czech-Republic.

the class HBaseWriterTest method testWriteGetObserve.

@Test
public void testWriteGetObserve() throws InterruptedException {
    URI uri = URI.create("hbase://localhost:2181/users?family=u");
    HBaseWriter writer = new HBaseWriter(uri, cluster.getConfiguration(), Collections.emptyMap());
    HBaseLogReader reader = new HBaseLogReader(uri, cluster.getConfiguration(), entity, Executors::newCachedThreadPool);
    RandomHBaseReader randomReader = new RandomHBaseReader(uri, cluster.getConfiguration(), Collections.emptyMap(), entity);
    StreamElement write = StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key", wildcard.toAttributePrefix() + "1", System.currentTimeMillis(), new byte[] { 1, 2, 3 });
    CountDownLatch writeLatch = new CountDownLatch(1);
    writer.write(write, (succ, exc) -> writeLatch.countDown());
    writeLatch.await();
    List<StreamElement> el = new ArrayList<>();
    CountDownLatch observeLatch = new CountDownLatch(1);
    reader.observe(reader.getPartitions(), Collections.singletonList(wildcard), new BatchLogObserver() {

        @Override
        public boolean onNext(StreamElement element) {
            el.add(element);
            return true;
        }

        @Override
        public void onCompleted() {
            observeLatch.countDown();
        }
    });
    observeLatch.await();
    assertEquals(1, el.size());
    assertEquals(write.getKey(), el.get(0).getKey());
    assertEquals(write.getAttribute(), el.get(0).getAttribute());
    assertArrayEquals(write.getValue(), el.get(0).getValue());
    Optional<? extends KeyValue<?>> kv = randomReader.get("key", wildcard.toAttributePrefix() + "1", wildcard, Long.MAX_VALUE);
    assertTrue(kv.isPresent());
    assertEquals(write.getKey(), el.get(0).getKey());
    assertEquals(write.getAttribute(), el.get(0).getAttribute());
    assertArrayEquals(write.getValue(), el.get(0).getValue());
}
Also used : ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) BatchLogObserver(cz.o2.proxima.direct.batch.BatchLogObserver) URI(java.net.URI) Test(org.junit.Test)

Example 2 with KeyValue

use of cz.o2.proxima.direct.randomaccess.KeyValue in project proxima-platform by O2-Czech-Republic.

the class TransactionIT method verifyNumDevicesMatch.

private void verifyNumDevicesMatch(int numWrites, int numUsers, boolean inCountAll) {
    int sum = 0;
    for (int i = 0; i < numUsers; i++) {
        String userId = "user" + i;
        AtomicInteger numDeviceAttrs = new AtomicInteger();
        AtomicInteger deviceAttrs = new AtomicInteger();
        view.scanWildcard(userId, device, d -> deviceAttrs.incrementAndGet());
        if (inCountAll) {
            Optional<KeyValue<Integer>> numAllDevices = view.get(userId, numDevices.toAttributePrefix() + "all", numDevices);
            numDeviceAttrs.set(numAllDevices.map(KeyValue::getParsedRequired).orElse(0));
        } else {
            view.scanWildcard(userId, numDevices, d -> numDeviceAttrs.incrementAndGet());
        }
        assertEquals(numDeviceAttrs.get(), deviceAttrs.get());
        sum += numDeviceAttrs.get();
    }
    assertEquals(numWrites, sum);
}
Also used : KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with KeyValue

use of cz.o2.proxima.direct.randomaccess.KeyValue in project proxima-platform by O2-Czech-Republic.

the class TransactionIT method swapValueBetween.

private boolean swapValueBetween(String key, String attrA, String attrB, boolean canFailWrite) throws InterruptedException {
    long retrySleep = 1;
    do {
        String transactionId = UUID.randomUUID().toString();
        BlockingQueue<Response> responses = new ArrayBlockingQueue<>(1);
        Optional<KeyValue<byte[]>> valA = view.get(key, attrA, device);
        Optional<KeyValue<byte[]>> valB = view.get(key, attrB, device);
        final List<KeyAttribute> fetched = Arrays.asList(valA.isPresent() ? KeyAttributes.ofStreamElement(valA.get()) : KeyAttributes.ofMissingAttribute(user, key, device, device.extractSuffix(attrA)), valB.isPresent() ? KeyAttributes.ofStreamElement(valB.get()) : KeyAttributes.ofMissingAttribute(user, key, device, device.extractSuffix(attrB)));
        client.begin(transactionId, (id, resp) -> ExceptionUtils.unchecked(() -> responses.put(resp)), fetched);
        Response response = responses.take();
        if (response.getFlags() != Flags.OPEN) {
            ;
            TimeUnit.MILLISECONDS.sleep(Math.min(8, retrySleep *= 2));
            continue;
        }
        long sequentialId = response.getSeqId();
        final List<StreamElement> updates;
        if (valA.isPresent()) {
            int currentVal = ByteBuffer.wrap(valA.get().getParsedRequired()).getInt();
            updates = updateAttributeAndRemove(sequentialId, key, attrB, attrA, currentVal);
        } else {
            int currentVal = ByteBuffer.wrap(valB.get().getParsedRequired()).getInt();
            updates = updateAttributeAndRemove(sequentialId, key, attrA, attrB, currentVal);
        }
        client.commit(transactionId, updates.stream().map(KeyAttributes::ofStreamElement).collect(Collectors.toList()));
        response = responses.take();
        if (response.getFlags() != Flags.COMMITTED) {
            TimeUnit.MILLISECONDS.sleep(Math.min(8, retrySleep *= 2));
            continue;
        }
        CountDownLatch latch = new CountDownLatch(1);
        AtomicBoolean succeeded = new AtomicBoolean();
        CommitCallback callback = (succ, exc) -> {
            if (!succ) {
                client.rollback(transactionId);
            }
            succeeded.set(succ);
            latch.countDown();
        };
        if (canFailWrite && random.nextBoolean()) {
            callback.commit(false, new RuntimeException("Failed!"));
        } else {
            CommitCallback multiCallback = CommitCallback.afterNumCommits(updates.size(), callback);
            updates.forEach(u -> view.write(u, multiCallback));
        }
        latch.await();
        return succeeded.get();
    } while (true);
}
Also used : Arrays(java.util.Arrays) Transaction(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction) ConfigConstants(cz.o2.proxima.repository.ConfigConstants) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) CachedView(cz.o2.proxima.direct.view.CachedView) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransformationRunner(cz.o2.proxima.util.TransformationRunner) Map(java.util.Map) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) Optionals(cz.o2.proxima.util.Optionals) Flags(cz.o2.proxima.transaction.Response.Flags) BlockingQueue(java.util.concurrent.BlockingQueue) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Collectors(java.util.stream.Collectors) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) Response(cz.o2.proxima.transaction.Response) Optional(java.util.Optional) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) Regular(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Regular) ConfigFactory(com.typesafe.config.ConfigFactory) ExecutorService(java.util.concurrent.ExecutorService) DeclaredThreadSafe(cz.o2.proxima.annotations.DeclaredThreadSafe) TransactionalOnlineAttributeWriter(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Assert.assertTrue(org.junit.Assert.assertTrue) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) TransactionRejectedException(cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) StreamElement(cz.o2.proxima.storage.StreamElement) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Response(cz.o2.proxima.transaction.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 4 with KeyValue

use of cz.o2.proxima.direct.randomaccess.KeyValue in project proxima-platform by O2-Czech-Republic.

the class TransactionIT_Large method verifyNumDevicesMatch.

private void verifyNumDevicesMatch(int numWrites, int numUsers, boolean inCountAll) {
    int sum = 0;
    for (int i = 0; i < numUsers; i++) {
        String userId = "user" + i;
        AtomicInteger numDeviceAttrs = new AtomicInteger();
        AtomicInteger deviceAttrs = new AtomicInteger();
        view.scanWildcard(userId, device, d -> deviceAttrs.incrementAndGet());
        if (inCountAll) {
            Optional<KeyValue<Integer>> numAllDevices = view.get(userId, numDevices.toAttributePrefix() + "all", numDevices);
            numDeviceAttrs.set(numAllDevices.map(KeyValue::getParsedRequired).orElse(0));
        } else {
            view.scanWildcard(userId, numDevices, d -> numDeviceAttrs.incrementAndGet());
        }
        assertEquals(numDeviceAttrs.get(), deviceAttrs.get());
        sum += numDeviceAttrs.get();
    }
    assertEquals(numWrites, sum);
}
Also used : KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with KeyValue

use of cz.o2.proxima.direct.randomaccess.KeyValue 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)

Aggregations

Test (org.junit.Test)29 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)25 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 ArrayList (java.util.ArrayList)16 StreamElement (cz.o2.proxima.storage.StreamElement)14 CachedView (cz.o2.proxima.direct.view.CachedView)11 OnlineAttributeWriter (cz.o2.proxima.direct.core.OnlineAttributeWriter)10 Transaction (cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 RandomAccessReader (cz.o2.proxima.direct.randomaccess.RandomAccessReader)6 DirectAttributeFamilyDescriptor (cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor)5 TransactionRejectedException (cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException)5 AttributeFamilyDescriptor (cz.o2.proxima.repository.AttributeFamilyDescriptor)5 Repository (cz.o2.proxima.repository.Repository)5 KeyAttribute (cz.o2.proxima.transaction.KeyAttribute)5 Map (java.util.Map)5 ConfigFactory (com.typesafe.config.ConfigFactory)4 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)4