Search in sources :

Example 1 with LogObserverUtils.toList

use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.

the class InMemStorageTest method testReadWriteSequentialIds.

@Test(timeout = 10000)
public void testReadWriteSequentialIds() throws InterruptedException {
    InMemStorage storage = new InMemStorage();
    DataAccessor accessor = storage.createAccessor(direct, createFamilyDescriptor(URI.create("inmem:///inmemstoragetest")));
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(direct.getContext()));
    AttributeWriterBase writer = Optionals.get(accessor.getWriter(direct.getContext()));
    List<StreamElement> result = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    CommitLogObserver observer = LogObserverUtils.toList(result, Assert::assertTrue, elem -> {
        latch.countDown();
        return false;
    });
    reader.observe("test", observer);
    writer.online().write(StreamElement.upsert(entity, data, 1L, "key", data.getName(), System.currentTimeMillis(), new byte[] { 1, 2, 3 }), (succ, exc) -> {
    });
    latch.await();
    assertEquals(1, result.size());
    assertTrue(result.get(0).hasSequentialId());
    assertEquals(1L, result.get(0).getSequentialId());
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Assert(org.junit.Assert) DataAccessor(cz.o2.proxima.direct.core.DataAccessor) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with LogObserverUtils.toList

use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList 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());
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Commit(cz.o2.proxima.transaction.Commit) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) TransactionUpdate(cz.o2.proxima.transaction.Commit.TransactionUpdate) Test(org.junit.Test)

Example 3 with LogObserverUtils.toList

use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.

the class InMemStorageTest method testFetchOffsets.

private void testFetchOffsets(int numPartitions) throws InterruptedException {
    InMemStorage storage = new InMemStorage();
    DataAccessor accessor = storage.createAccessor(direct, createFamilyDescriptor(URI.create("inmem:///test"), numPartitions));
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(direct.getContext()));
    AttributeWriterBase writer = Optionals.get(accessor.getWriter(direct.getContext()));
    long now = System.currentTimeMillis();
    List<StreamElement> updates = new ArrayList<>();
    for (int i = 0; i < 2 * numPartitions; i++) {
        updates.add(StreamElement.upsert(entity, data, UUID.randomUUID().toString(), "key" + (i + 1), data.getName(), now + i, new byte[] { 1, 2, 3 }));
    }
    updates.forEach(el -> writer.online().write(el, (succ, exc) -> {
    }));
    Map<Partition, Offset> startingOffsets = reader.fetchOffsets(Position.OLDEST, reader.getPartitions());
    assertEquals(numPartitions, startingOffsets.size());
    List<StreamElement> elements = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    CountDownLatch startLatch = new CountDownLatch(1);
    CommitLogObserver observer = LogObserverUtils.toList(elements, ign -> latch.countDown(), el -> {
        ExceptionUtils.ignoringInterrupted(startLatch::await);
        return true;
    });
    ObserveHandle handle = reader.observeBulkOffsets(startingOffsets.values(), true, observer);
    assertFalse(ObserveHandleUtils.isAtHead(handle, reader));
    startLatch.countDown();
    latch.await();
    assertEquals(2 * numPartitions, elements.size());
    List<Offset> committed = handle.getCommittedOffsets();
    Map<Partition, Offset> endOffsets2 = reader.fetchOffsets(Position.NEWEST, committed.stream().map(Offset::getPartition).collect(Collectors.toList()));
    assertTrue(ObserveHandleUtils.isAtHead(handle, reader));
    assertEquals(IntStream.range(0, 2 * numPartitions).mapToObj(i -> "key" + (i + 1)).collect(Collectors.toList()), elements.stream().map(StreamElement::getKey).collect(Collectors.toList()));
    elements.clear();
    Map<Partition, Offset> endOffsets = reader.fetchOffsets(Position.NEWEST, reader.getPartitions());
    assertEquals(numPartitions, endOffsets.size());
    CountDownLatch latch2 = new CountDownLatch(1);
    observer = LogObserverUtils.toList(elements, ign -> latch2.countDown());
    reader.observeBulkOffsets(endOffsets.values(), true, observer);
    latch2.await();
    assertEquals(numPartitions, elements.size());
    assertEquals(IntStream.range(numPartitions, 2 * numPartitions).mapToObj(i -> "key" + (i + 1)).collect(Collectors.toList()), elements.stream().map(StreamElement::getKey).collect(Collectors.toList()));
}
Also used : Partitioners(cz.o2.proxima.storage.commitlog.Partitioners) Arrays(java.util.Arrays) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) BatchLogReader(cz.o2.proxima.direct.batch.BatchLogReader) Partition(cz.o2.proxima.storage.Partition) KeyAttributePartitioner(cz.o2.proxima.storage.commitlog.KeyAttributePartitioner) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) StorageType(cz.o2.proxima.storage.StorageType) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) SerializationException(cz.o2.proxima.scheme.SerializationException) AccessType(cz.o2.proxima.storage.AccessType) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Optionals(cz.o2.proxima.util.Optionals) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) ConsumedOffset(cz.o2.proxima.direct.storage.InMemStorage.ConsumedOffset) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) DataAccessor(cz.o2.proxima.direct.core.DataAccessor) ConfigFactory(com.typesafe.config.ConfigFactory) Watermarks(cz.o2.proxima.time.Watermarks) ObserveHandleUtils(cz.o2.proxima.direct.commitlog.ObserveHandleUtils) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) Repository(cz.o2.proxima.repository.Repository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BatchLogObserver(cz.o2.proxima.direct.batch.BatchLogObserver) Test(org.junit.Test) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) LogObserverUtils(cz.o2.proxima.direct.commitlog.LogObserverUtils) Offset(cz.o2.proxima.direct.commitlog.Offset) TimeUnit(java.util.concurrent.TimeUnit) Partitioner(cz.o2.proxima.storage.commitlog.Partitioner) Preconditions(com.google.common.base.Preconditions) Assert(org.junit.Assert) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) Partition(cz.o2.proxima.storage.Partition) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) DataAccessor(cz.o2.proxima.direct.core.DataAccessor) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumedOffset(cz.o2.proxima.direct.storage.InMemStorage.ConsumedOffset) Offset(cz.o2.proxima.direct.commitlog.Offset) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver)

Example 4 with LogObserverUtils.toList

use of cz.o2.proxima.direct.commitlog.LogObserverUtils.toList in project proxima-platform by O2-Czech-Republic.

the class TransactionCommitTransformationTest method testTransformUpdates.

@Test
public void testTransformUpdates() throws InterruptedException {
    StreamElement upsert = StreamElement.upsert(gateway, status, UUID.randomUUID().toString(), "key", status.getName(), 0L, new byte[] { 1, 2, 3 });
    StreamElement delete = StreamElement.delete(gateway, status, UUID.randomUUID().toString(), "key", status.getName(), 1L);
    Commit commit = Commit.of(1L, 1234567890000L, Arrays.asList(upsert, delete));
    List<StreamElement> outputs = new ArrayList<>();
    CommitLogObserver observer = LogObserverUtils.toList(outputs, ign -> {
    });
    Optionals.get(direct.getCommitLogReader(status)).observe("name", observer).waitUntilReady();
    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(2, outputs.size());
    assertEquals(commit.getUpdates().get(0), outputs.get(0));
    assertEquals(commit.getUpdates().get(1), outputs.get(1));
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Commit(cz.o2.proxima.transaction.Commit) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Aggregations

CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)4 StreamElement (cz.o2.proxima.storage.StreamElement)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)2 AttributeWriterBase (cz.o2.proxima.direct.core.AttributeWriterBase)2 DataAccessor (cz.o2.proxima.direct.core.DataAccessor)2 Commit (cz.o2.proxima.transaction.Commit)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions (com.google.common.base.Preconditions)1 Iterables (com.google.common.collect.Iterables)1 ConfigFactory (com.typesafe.config.ConfigFactory)1 BatchLogObserver (cz.o2.proxima.direct.batch.BatchLogObserver)1 BatchLogReader (cz.o2.proxima.direct.batch.BatchLogReader)1 LogObserverUtils (cz.o2.proxima.direct.commitlog.LogObserverUtils)1 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)1 ObserveHandleUtils (cz.o2.proxima.direct.commitlog.ObserveHandleUtils)1 Offset (cz.o2.proxima.direct.commitlog.Offset)1