Search in sources :

Example 21 with CommitLogReader

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

the class TransactionResourceManager method runObservations.

/**
 * Observe all transactional families with given observer.
 *
 * @param name name of the observer (will be appended with name of the family)
 * @param requestObserver the observer (need not be synchronized)
 */
@Override
public void runObservations(String name, BiConsumer<StreamElement, Pair<Long, Object>> updateConsumer, CommitLogObserver requestObserver) {
    final CommitLogObserver effectiveObserver;
    if (isNotThreadSafe(requestObserver)) {
        effectiveObserver = requestObserver;
    } else {
        effectiveObserver = new ThreadPooledObserver(direct.getContext().getExecutorService(), requestObserver, getDeclaredParallelism(requestObserver).orElse(Runtime.getRuntime().availableProcessors()));
    }
    List<Set<String>> families = direct.getRepository().getAllEntities().filter(EntityDescriptor::isTransactional).flatMap(e -> e.getAllAttributes().stream()).filter(a -> a.getTransactionMode() != TransactionMode.NONE).map(AttributeDescriptor::getTransactionalManagerFamilies).map(Sets::newHashSet).distinct().collect(Collectors.toList());
    CountDownLatch initializedLatch = new CountDownLatch(families.size());
    families.stream().map(this::toRequestStatePair).forEach(p -> {
        DirectAttributeFamilyDescriptor requestFamily = p.getFirst();
        DirectAttributeFamilyDescriptor stateFamily = p.getSecond();
        String consumerName = name + "-" + requestFamily.getDesc().getName();
        log.info("Starting to observe family {} with URI {} and associated state family {} as {}", requestFamily, requestFamily.getDesc().getStorageUri(), stateFamily, consumerName);
        CommitLogReader reader = Optionals.get(requestFamily.getCommitLogReader());
        CachedView view = stateViews.get(stateFamily);
        if (view == null) {
            view = Optionals.get(stateFamily.getCachedView());
            Duration ttl = Duration.ofMillis(cleanupIntervalMs);
            stateViews.put(stateFamily, view);
            view.assign(view.getPartitions(), updateConsumer, ttl);
        }
        initializedLatch.countDown();
        serverObservedFamilies.put(requestFamily, reader.observe(consumerName, repartitionHookForBeingActive(stateFamily, reader.getPartitions().size(), effectiveObserver)));
    });
    ExceptionUtils.unchecked(initializedLatch::await);
}
Also used : Arrays(java.util.Arrays) Partition(cz.o2.proxima.storage.Partition) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Flags(cz.o2.proxima.transaction.Request.Flags) Wildcard(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Wildcard) InetAddress(java.net.InetAddress) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) CachedView(cz.o2.proxima.direct.view.CachedView) StreamElement(cz.o2.proxima.storage.StreamElement) ForwardingObserver(cz.o2.proxima.direct.commitlog.CommitLogObservers.ForwardingObserver) Pair(cz.o2.proxima.util.Pair) Duration(java.time.Duration) Map(java.util.Map) Optionals(cz.o2.proxima.util.Optionals) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) TransactionMode(cz.o2.proxima.repository.TransactionMode) ThreadSafe(javax.annotation.concurrent.ThreadSafe) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) BiConsumer(cz.o2.proxima.functional.BiConsumer) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Internal(cz.o2.proxima.annotations.Internal) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) Response(cz.o2.proxima.transaction.Response) State(cz.o2.proxima.transaction.State) Optional(java.util.Optional) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Getter(lombok.Getter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) AccessLevel(lombok.AccessLevel) Regular(cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Regular) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ObserveHandleUtils(cz.o2.proxima.direct.commitlog.ObserveHandleUtils) Commit(cz.o2.proxima.transaction.Commit) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) Nullable(javax.annotation.Nullable) DeclaredThreadSafe(cz.o2.proxima.annotations.DeclaredThreadSafe) Classpath(cz.o2.proxima.util.Classpath) Request(cz.o2.proxima.transaction.Request) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) KeyAttribute(cz.o2.proxima.transaction.KeyAttribute) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) CachedView(cz.o2.proxima.direct.view.CachedView) Set(java.util.Set) HashSet(java.util.HashSet) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) Duration(java.time.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Sets(com.google.common.collect.Sets)

Example 22 with CommitLogReader

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

the class CommitLogReaderTest method testCommitLogReaderFetchOffsetDefaults.

@Test
public void testCommitLogReaderFetchOffsetDefaults() {
    CommitLogReader commitLogReader = spy(CommitLogReader.class);
    when(commitLogReader.hasExternalizableOffsets()).thenReturn(false);
    assertThrows(UnsupportedOperationException.class, () -> commitLogReader.fetchOffsets(Position.NEWEST, Collections.emptyList()));
    when(commitLogReader.hasExternalizableOffsets()).thenReturn(true);
    assertThrows(IllegalStateException.class, () -> commitLogReader.fetchOffsets(Position.NEWEST, Collections.emptyList()));
}
Also used : LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) Test(org.junit.Test)

Example 23 with CommitLogReader

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

the class DirectAttributeFamilyProxyDescriptor method getPartitionedCachedView.

private static Optional<CachedView> getPartitionedCachedView(AttrLookup lookup, Context context, AttributeFamilyProxyDescriptor desc) {
    if (desc.getTargetFamilyRead().getAccess().canReadCommitLog() && !desc.getTargetFamilyWrite().getAccess().isReadonly() && desc.getTargetFamilyRead().getAccess().canCreateCachedView()) {
        Optional<CommitLogReader> maybeReader = getCommitLogReader(lookup, context, desc);
        Optional<OnlineAttributeWriter> maybeWriter = getWriter(lookup, context, desc).map(AttributeWriterBase::online);
        if (maybeReader.isPresent() && maybeWriter.isPresent()) {
            return Optional.of(new LocalCachedPartitionedView(desc.getTargetFamilyRead().getEntity(), maybeReader.get(), maybeWriter.get()));
        }
    }
    return Optional.empty();
}
Also used : LocalCachedPartitionedView(cz.o2.proxima.direct.view.LocalCachedPartitionedView) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader)

Example 24 with CommitLogReader

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

the class CommitLogReaderTest method testThroughputLimitedCommitLogReaderAsFactory.

@Test
public void testThroughputLimitedCommitLogReaderAsFactory() {
    ThroughputLimiter limiter = ThroughputLimiter.NoOpThroughputLimiter.INSTANCE;
    CommitLogReader limitedReader = CommitLogReaders.withThroughputLimit(reader, limiter);
    CommitLogReader cloned = limitedReader.asFactory().apply(repo);
    assertTrue(limitedReader.getClass().isAssignableFrom(cloned.getClass()));
}
Also used : ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) Test(org.junit.Test)

Example 25 with CommitLogReader

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

the class CommitLogReaderTest method testOffsetExternalizer.

@Test
public void testOffsetExternalizer() {
    CommitLogReader commitLogReader = spy(CommitLogReader.class);
    when(commitLogReader.hasExternalizableOffsets()).thenReturn(false);
    assertThrows(UnsupportedOperationException.class, commitLogReader::getOffsetExternalizer);
    when(commitLogReader.hasExternalizableOffsets()).thenReturn(true);
    assertThrows(IllegalStateException.class, commitLogReader::getOffsetExternalizer);
}
Also used : LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) Test(org.junit.Test)

Aggregations

CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)80 Test (org.junit.Test)70 StreamElement (cz.o2.proxima.storage.StreamElement)67 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)63 CountDownLatch (java.util.concurrent.CountDownLatch)58 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)45 ArrayList (java.util.ArrayList)41 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)34 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)33 List (java.util.List)31 UUID (java.util.UUID)29 ConfigFactory (com.typesafe.config.ConfigFactory)28 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)28 Offset (cz.o2.proxima.direct.commitlog.Offset)27 Collections (java.util.Collections)27 Collectors (java.util.stream.Collectors)27 OnNextContext (cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 Repository (cz.o2.proxima.repository.Repository)25 Accessor (cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor)24