Search in sources :

Example 1 with Commit

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

the class KafkaAccessorTest method testIsAcceptableFailsForRegexp.

@Test
public void testIsAcceptableFailsForRegexp() {
    EntityDescriptor entity = EntityDescriptor.newBuilder().setName("entity").build();
    URI storageUri = URI.create("kafka://broker/?topicPattern=pattern");
    kafkaAccessor = new KafkaAccessor(entity, storageUri, new HashMap<>());
    AttributeFamilyDescriptor descriptor = AttributeFamilyDescriptor.newBuilder().setName("test-state-commit-log").setAccess(AccessType.from(ConfigConstants.STATE_COMMIT_LOG)).setType(StorageType.PRIMARY).setStorageUri(storageUri).setEntity(entity).build();
    assertThrows(IllegalStateException.class, () -> kafkaAccessor.isAcceptable(descriptor));
}
Also used : EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) URI(java.net.URI) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) Test(org.junit.Test)

Example 2 with Commit

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

the class TransactionLogObserverTest method testCreateTransactionCommitAfterFailover.

@Test(timeout = 10000)
public void testCreateTransactionCommitAfterFailover() throws InterruptedException {
    String transactionId = UUID.randomUUID().toString();
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    try (ClientTransactionManager clientManager = new TransactionResourceManager(direct, Collections.emptyMap())) {
        createObserver();
        clientManager.begin(transactionId, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
        takeResponseFor(responseQueue, "open.1");
        observer.getRawManager().close();
        createObserver();
        clientManager.commit(transactionId, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
        Pair<String, Response> response = takeResponseFor(responseQueue, "commit");
        assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
    }
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 3 with Commit

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

the class TransactionLogObserverTest method testCreateTransactionRollbackAfterFailover.

@Test(timeout = 10000)
public void testCreateTransactionRollbackAfterFailover() throws InterruptedException {
    String t1 = "t1-" + UUID.randomUUID();
    String t2 = "t2-" + UUID.randomUUID();
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    try (ClientTransactionManager clientManager = new TransactionResourceManager(direct, Collections.emptyMap())) {
        createObserver();
        clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
        Pair<String, Response> t1openResponse = takeResponseFor(responseQueue, "open.1");
        clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 1L, "1")));
        Pair<String, Response> t2openResponse = takeResponseFor(responseQueue, "open.1");
        assertTrue(t2openResponse.getSecond().getSeqId() > t1openResponse.getSecond().getSeqId());
        clientManager.commit(t2, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
        Pair<String, Response> t2commitResponse = takeResponseFor(responseQueue, "commit");
        assertEquals("commit", t2commitResponse.getFirst());
        observer.getRawManager().close();
        createObserver();
        clientManager.commit(t1, Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, 2L, "1")));
        Pair<String, Response> response = takeResponseFor(responseQueue, "commit");
        assertTrue("Expected exactly one committed transaction, got " + t2commitResponse.getSecond() + " and " + response.getSecond(), t2commitResponse.getSecond().getFlags() == Response.Flags.COMMITTED ^ response.getSecond().getFlags() == Response.Flags.COMMITTED);
        tearDown();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 4 with Commit

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

the class TransactionLogObserverTest method testCreateTransactionCommitWithConflictInOutputs.

@Test(timeout = 10000)
public void testCreateTransactionCommitWithConflictInOutputs() throws InterruptedException {
    createObserver();
    ClientTransactionManager clientManager = direct.getClientTransactionManager();
    String t1 = UUID.randomUUID().toString();
    String t2 = UUID.randomUUID().toString();
    BlockingQueue<Pair<String, Response>> responseQueue = new ArrayBlockingQueue<>(1);
    clientManager.begin(t1, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "u1", userGateways, 1L, "1")));
    Pair<String, Response> t1OpenResponse = responseQueue.take();
    List<KeyAttribute> t1Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t1OpenResponse.getSecond().getSeqId(), "1"));
    clientManager.begin(t2, ExceptionUtils.uncheckedBiConsumer((k, v) -> responseQueue.put(Pair.of(k, v))), // not conflicting with the previous one
    Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "u2", userGateways, 1L, "1")));
    Pair<String, Response> t2OpenResponse = responseQueue.take();
    List<KeyAttribute> t2Outputs = Collections.singletonList(KeyAttributes.ofAttributeDescriptor(user, "user", userGateways, t2OpenResponse.getSecond().getSeqId(), "1"));
    clientManager.commit(t2, t2Outputs);
    Pair<String, Response> response = responseQueue.take();
    assertEquals("commit", response.getFirst());
    assertEquals(Response.Flags.COMMITTED, response.getSecond().getFlags());
    clientManager.commit(t1, t1Outputs);
    response = responseQueue.take();
    assertEquals("commit", response.getFirst());
    assertEquals(Response.Flags.ABORTED, response.getSecond().getFlags());
}
Also used : Iterables(com.google.common.collect.Iterables) ServerTransactionManager(cz.o2.proxima.direct.transaction.ServerTransactionManager) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConfigUtils(cz.o2.proxima.repository.config.ConfigUtils) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) StreamElement(cz.o2.proxima.storage.StreamElement) Pair(cz.o2.proxima.util.Pair) TransformationRunner(cz.o2.proxima.util.TransformationRunner) After(org.junit.After) TransactionResourceManager(cz.o2.proxima.direct.transaction.TransactionResourceManager) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Before(org.junit.Before) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Set(java.util.Set) KeyAttributes(cz.o2.proxima.transaction.KeyAttributes) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Response(cz.o2.proxima.transaction.Response) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ClientTransactionManager(cz.o2.proxima.direct.transaction.ClientTransactionManager) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) Pair(cz.o2.proxima.util.Pair) Test(org.junit.Test)

Example 5 with Commit

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

the class LocalKafkaCommitLogDescriptorTest method testOnlineObserveWithRebalanceResetsOffsetCommitter.

@Test(timeout = 100000)
public void testOnlineObserveWithRebalanceResetsOffsetCommitter() throws InterruptedException {
    int numWrites = 5;
    Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, cfg(Pair.of(LocalKafkaCommitLogDescriptor.CFG_NUM_PARTITIONS, 3), // poll single record to commit it in atomic way
    Pair.of(KafkaAccessor.MAX_POLL_RECORDS, 1))));
    final CountDownLatch latch = new CountDownLatch(numWrites);
    AtomicInteger consumed = new AtomicInteger();
    List<OnNextContext> unconfirmed = Collections.synchronizedList(new ArrayList<>());
    CommitLogObserver observer = new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            switch(consumed.getAndIncrement()) {
                case 0:
                    // we must confirm the first message to create a committed position
                    context.confirm();
                    break;
                case 2:
                    throw new RuntimeException("Failing first consumer!");
                default:
                    unconfirmed.add(context);
                    break;
            }
            if (consumed.get() == numWrites) {
                unconfirmed.forEach(OnNextContext::confirm);
            }
            latch.countDown();
            return true;
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public boolean onError(Throwable error) {
            return true;
        }
    };
    testOnlineObserveWithRebalanceResetsOffsetCommitterWithObserver(observer, accessor, numWrites);
    latch.await();
    assertEquals("Invalid committed offests: " + accessor.committedOffsets, 3, accessor.committedOffsets.values().stream().mapToInt(AtomicInteger::get).sum());
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Accessor(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)50 StreamElement (cz.o2.proxima.storage.StreamElement)40 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)25 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)24 CountDownLatch (java.util.concurrent.CountDownLatch)22 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)20 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)20 Repository (cz.o2.proxima.repository.Repository)20 List (java.util.List)20 ConfigFactory (com.typesafe.config.ConfigFactory)19 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)19 ArrayList (java.util.ArrayList)19 KeyAttribute (cz.o2.proxima.transaction.KeyAttribute)18 UUID (java.util.UUID)18 Before (org.junit.Before)18 TransformationRunner (cz.o2.proxima.util.TransformationRunner)16 Collections (java.util.Collections)16 OnlineAttributeWriter (cz.o2.proxima.direct.core.OnlineAttributeWriter)15 Response (cz.o2.proxima.transaction.Response)15 State (cz.o2.proxima.transaction.State)15