Search in sources :

Example 1 with DirectAttributeFamilyDescriptor

use of cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor 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 2 with DirectAttributeFamilyDescriptor

use of cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method setUp.

@Before
public void setUp() {
    DirectAttributeFamilyDescriptor family = repo.getAllFamilies().filter(af -> af.getName().equals("event-storage-stream")).findAny().map(repo.getOrCreateOperator(DirectDataOperator.class)::resolveRequired).get();
    reader = family.getCommitLogReader().get();
    writer = family.getWriter().get();
}
Also used : Iterables(com.google.common.collect.Iterables) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) Partition(cz.o2.proxima.storage.Partition) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Mockito.spy(org.mockito.Mockito.spy) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnaryFunction(cz.o2.proxima.functional.UnaryFunction) Duration(java.time.Duration) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) LongStream(java.util.stream.LongStream) Repository(cz.o2.proxima.repository.Repository) BiFunction(cz.o2.proxima.functional.BiFunction) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) Before(org.junit.Before)

Example 3 with DirectAttributeFamilyDescriptor

use of cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperator method getCachedView.

/**
 * Retrieve {@link CachedView} for given {@link AttributeDescriptor}s.
 *
 * @param attrs the attributes to find cached view for
 * @return optional cached view
 */
public Optional<CachedView> getCachedView(Collection<AttributeDescriptor<?>> attrs) {
    boolean hasTransactions = attrs.stream().anyMatch(a -> a.getTransactionMode() != TransactionMode.NONE);
    Optional<CachedView> view = getFamilyForAttributes(attrs, DirectAttributeFamilyDescriptor::hasCachedView).flatMap(DirectAttributeFamilyDescriptor::getCachedView);
    if (hasTransactions) {
        return view.map(v -> new TransactionalCachedView(this, v));
    }
    return view;
}
Also used : TransactionalCachedView(cz.o2.proxima.direct.transaction.TransactionalCachedView) CachedView(cz.o2.proxima.direct.view.CachedView) TransactionalCachedView(cz.o2.proxima.direct.transaction.TransactionalCachedView)

Example 4 with DirectAttributeFamilyDescriptor

use of cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testProxyScanWithOffset.

@Test
public void testProxyScanWithOffset() {
    EntityDescriptor proxied = repo.getEntity("proxied");
    AttributeDescriptor<?> source = proxied.getAttribute("event.*");
    Set<DirectAttributeFamilyDescriptor> proxiedFamilies = direct.getFamiliesForAttribute(source);
    direct.getWriter(source).get().write(StreamElement.upsert(proxied, source, UUID.randomUUID().toString(), "key", "event.abc", System.currentTimeMillis(), "test".getBytes(StandardCharsets.UTF_8)), (s, exc) -> {
        assertTrue(s);
    });
    direct.getWriter(source).get().write(StreamElement.upsert(proxied, source, UUID.randomUUID().toString(), "key", "event.def", System.currentTimeMillis(), "test2".getBytes(StandardCharsets.UTF_8)), (s, exc) -> {
        assertTrue(s);
    });
    List<KeyValue<?>> kvs = new ArrayList<>();
    RandomAccessReader reader = proxiedFamilies.iterator().next().getRandomAccessReader().get();
    reader.scanWildcard("key", source, reader.fetchOffset(RandomAccessReader.Listing.ATTRIBUTE, "event.abc"), 1, kvs::add);
    assertEquals(1, kvs.size());
    assertEquals("test2", new String((byte[]) kvs.get(0).getValue()));
    assertEquals(source, kvs.get(0).getAttributeDescriptor());
    assertEquals("event.def", kvs.get(0).getAttribute());
    assertEquals("key", kvs.get(0).getKey());
}
Also used : EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) RandomAccessReader(cz.o2.proxima.direct.randomaccess.RandomAccessReader) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with DirectAttributeFamilyDescriptor

use of cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testApplicationOfProxyTransformOnReplicatedDataWithTransform.

@Test(timeout = 10000)
public void testApplicationOfProxyTransformOnReplicatedDataWithTransform() throws InterruptedException {
    repo.reloadConfig(true, ConfigFactory.load().withFallback(ConfigFactory.load("test-replication-proxy.conf")).resolve());
    final EntityDescriptor dummy = repo.getEntity("dummy2");
    final EntityDescriptor event = repo.getEntity("event");
    final AttributeDescriptor<Object> data = event.getAttribute("data");
    final AttributeDescriptor<Object> raw = dummy.getAttribute("_e.*", true);
    TransformationRunner.runTransformations(repo, direct);
    CountDownLatch latch = new CountDownLatch(2);
    runAttributeReplicas(direct, tmp -> latch.countDown());
    assertTrue(direct.getWriter(data).isPresent());
    OnlineAttributeWriter writer = direct.getWriter(data).get();
    long now = System.currentTimeMillis();
    writer.write(StreamElement.upsert(dummy, data, "uuid", "gw", data.getName(), now, new byte[] { 1, 2 }), (succ, exc) -> {
        assertTrue(succ);
        latch.countDown();
    });
    latch.await();
    Optional<RandomAccessReader> reader = direct.getFamiliesForAttribute(raw).stream().filter(af -> af.getDesc().getAccess().canRandomRead()).findAny().flatMap(DirectAttributeFamilyDescriptor::getRandomAccessReader);
    assertTrue(reader.isPresent());
    assertTrue(reader.get().get("gw", raw.toAttributePrefix() + (now + 1), raw).isPresent());
    assertFalse(reader.get().get("gw", raw.toAttributePrefix() + now, raw).isPresent());
}
Also used : EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) RandomAccessReader(cz.o2.proxima.direct.randomaccess.RandomAccessReader) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)27 Test (org.junit.Test)23 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)22 StreamElement (cz.o2.proxima.storage.StreamElement)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)19 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)19 ArrayList (java.util.ArrayList)19 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)17 List (java.util.List)17 Slf4j (lombok.extern.slf4j.Slf4j)17 CachedView (cz.o2.proxima.direct.view.CachedView)16 Collections (java.util.Collections)16 Set (java.util.Set)16 TimeUnit (java.util.concurrent.TimeUnit)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 Collectors (java.util.stream.Collectors)16 RandomAccessReader (cz.o2.proxima.direct.randomaccess.RandomAccessReader)15 StorageType (cz.o2.proxima.storage.StorageType)15 Map (java.util.Map)15