Search in sources :

Example 16 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor 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 17 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor 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 18 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor 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 19 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor 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)

Example 20 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testProxyScan.

@Test
public void testProxyScan() {
    EntityDescriptor proxied = repo.getEntity("proxied");
    AttributeDescriptor<?> source = proxied.getAttribute("event.*");
    Set<DirectAttributeFamilyDescriptor> proxiedFamilies = direct.getFamiliesForAttribute(source);
    assertTrue(direct.getWriter(source).isPresent());
    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<>();
    proxiedFamilies.iterator().next().getRandomAccessReader().get().scanWildcard("key", source, kvs::add);
    assertEquals(2, kvs.size());
    assertEquals("test", new String((byte[]) kvs.get(0).getValue()));
    assertEquals(source, kvs.get(0).getAttributeDescriptor());
    assertEquals("event.abc", kvs.get(0).getAttribute());
    assertEquals("key", kvs.get(0).getKey());
    assertEquals("test2", new String((byte[]) kvs.get(1).getValue()));
    assertEquals(source, kvs.get(1).getAttributeDescriptor());
    assertEquals("event.def", kvs.get(1).getAttribute());
    assertEquals("key", kvs.get(1).getKey());
}
Also used : EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)71 Test (org.junit.Test)59 StreamElement (cz.o2.proxima.storage.StreamElement)51 ArrayList (java.util.ArrayList)46 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)33 CountDownLatch (java.util.concurrent.CountDownLatch)33 List (java.util.List)29 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)24 Slf4j (lombok.extern.slf4j.Slf4j)24 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)22 Map (java.util.Map)22 Collectors (java.util.stream.Collectors)22 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)21 RandomAccessReader (cz.o2.proxima.direct.randomaccess.RandomAccessReader)21 AttributeFamilyDescriptor (cz.o2.proxima.repository.AttributeFamilyDescriptor)20 Arrays (java.util.Arrays)20 Collections (java.util.Collections)20 Optional (java.util.Optional)20 Set (java.util.Set)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20