Search in sources :

Example 21 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project pravega by pravega.

the class PersistentStreamBase method compareStreamCuts.

@Override
public CompletableFuture<StreamCutComparison> compareStreamCuts(Map<Long, Long> streamcut1, Map<Long, Long> streamcut2, OperationContext context) {
    Preconditions.checkNotNull(context, "operation context cannot be null");
    LongSummaryStatistics stats1 = streamcut1.keySet().stream().collect(Collectors.summarizingLong(Long::longValue));
    LongSummaryStatistics stats2 = streamcut1.keySet().stream().collect(Collectors.summarizingLong(Long::longValue));
    // streamcuts at all and can simply return the response.
    if (stats1.getMax() < stats2.getMin()) {
        // stats1 less than stats2
        return CompletableFuture.completedFuture(StreamCutComparison.Before);
    } else if (stats2.getMax() < stats1.getMin()) {
        // stats2 less than min
        return CompletableFuture.completedFuture(StreamCutComparison.EqualOrAfter);
    }
    CompletableFuture<ImmutableMap<StreamSegmentRecord, Integer>> span1Future = computeStreamCutSpan(streamcut1, context);
    CompletableFuture<ImmutableMap<StreamSegmentRecord, Integer>> span2Future = computeStreamCutSpan(streamcut2, context);
    return CompletableFuture.allOf(span1Future, span2Future).thenApply(v -> {
        ImmutableMap<StreamSegmentRecord, Integer> span1 = span1Future.join();
        ImmutableMap<StreamSegmentRecord, Integer> span2 = span2Future.join();
        // loop over all segments in streamcut1 and compare them with segments in streamcut2.
        // if we find all segments in streamcut1 greater than or equal to all segments in streamcut2
        boolean foundGt = false;
        boolean foundLt = false;
        for (Map.Entry<StreamSegmentRecord, Integer> e1 : span1.entrySet()) {
            for (Map.Entry<StreamSegmentRecord, Integer> e2 : span2.entrySet()) {
                int comparison;
                if (e2.getKey().segmentId() == e1.getKey().segmentId()) {
                    // same segment. compare offsets
                    comparison = Long.compare(streamcut1.get(e1.getKey().segmentId()), streamcut2.get(e2.getKey().segmentId()));
                } else if (e2.getKey().overlaps(e1.getKey())) {
                    // overlapping segment. compare segment id.
                    comparison = Long.compare(e1.getKey().segmentId(), e2.getKey().segmentId());
                } else {
                    continue;
                }
                foundGt = !foundGt ? comparison > 0 : foundGt;
                foundLt = !foundLt ? comparison < 0 : foundLt;
            }
        }
        if (foundGt) {
            if (foundLt) {
                // some segments are greater and some less. return overlapping
                return StreamCutComparison.Overlaps;
            } else {
                // segments are only greater or equal.
                return StreamCutComparison.EqualOrAfter;
            }
        } else {
            if (foundLt) {
                // no segment greater than but some segment less than.
                return StreamCutComparison.Before;
            } else {
                // no segment greater than no segment less than. this means all segments are equal.
                return StreamCutComparison.EqualOrAfter;
            }
        }
    });
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 22 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project metron by apache.

the class ProfileBuilderBolt method log.

/**
 * Logs information about the {@link TupleWindow}.
 *
 * @param window The tuple window.
 */
private void log(TupleWindow window) {
    // summarize the newly received tuples
    LongSummaryStatistics received = window.get().stream().map(tuple -> getField(TIMESTAMP_TUPLE_FIELD, tuple, Long.class)).collect(Collectors.summarizingLong(Long::longValue));
    LOG.debug("Tuple(s) received; count={}, min={}, max={}, range={} ms", received.getCount(), received.getMin(), received.getMax(), received.getMax() - received.getMin());
    if (window.getExpired().size() > 0) {
        // summarize the expired tuples
        LongSummaryStatistics expired = window.getExpired().stream().map(tuple -> getField(TIMESTAMP_TUPLE_FIELD, tuple, Long.class)).collect(Collectors.summarizingLong(Long::longValue));
        LOG.debug("Tuple(s) expired; count={}, min={}, max={}, range={} ms, lag={} ms", expired.getCount(), expired.getMin(), expired.getMax(), expired.getMax() - expired.getMin(), received.getMin() - expired.getMin());
    }
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics) OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ProfilerConfigurations(org.apache.metron.common.configuration.profiler.ProfilerConfigurations) BaseWindowedBolt(org.apache.storm.topology.base.BaseWindowedBolt) TopologyContext(org.apache.storm.task.TopologyContext) LoggerFactory(org.slf4j.LoggerFactory) TIMESTAMP_TUPLE_FIELD(org.apache.metron.profiler.storm.ProfileSplitterBolt.TIMESTAMP_TUPLE_FIELD) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Reloadable(org.apache.metron.common.zookeeper.configurations.Reloadable) PROFILE_TUPLE_FIELD(org.apache.metron.profiler.storm.ProfileSplitterBolt.PROFILE_TUPLE_FIELD) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) Tuple(org.apache.storm.tuple.Tuple) TupleWindow(org.apache.storm.windowing.TupleWindow) OutputCollector(org.apache.storm.task.OutputCollector) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Map(java.util.Map) ConfigurationType(org.apache.metron.common.configuration.ConfigurationType) ConfigurationsUtils(org.apache.metron.common.configuration.ConfigurationsUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DefaultMessageDistributor(org.apache.metron.profiler.DefaultMessageDistributor) LongSummaryStatistics(java.util.LongSummaryStatistics) Context(org.apache.metron.stellar.dsl.Context) Logger(org.slf4j.Logger) ConfigurationsUpdater(org.apache.metron.common.zookeeper.configurations.ConfigurationsUpdater) ZKCache(org.apache.metron.zookeeper.ZKCache) JSONParser(org.json.simple.parser.JSONParser) MethodHandles(java.lang.invoke.MethodHandles) ProfilerUpdater(org.apache.metron.common.zookeeper.configurations.ProfilerUpdater) Constants(org.apache.metron.common.Constants) MessageRoute(org.apache.metron.profiler.MessageRoute) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) SimpleEventListener(org.apache.metron.zookeeper.SimpleEventListener) TimeUnit(java.util.concurrent.TimeUnit) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) JSONObject(org.json.simple.JSONObject) ENTITY_TUPLE_FIELD(org.apache.metron.profiler.storm.ProfileSplitterBolt.ENTITY_TUPLE_FIELD) MessageDistributor(org.apache.metron.profiler.MessageDistributor) RetryPolicy(org.apache.curator.RetryPolicy) ConversionUtils(org.apache.metron.stellar.common.utils.ConversionUtils) ProfileConfig(org.apache.metron.common.configuration.profiler.ProfileConfig) MESSAGE_TUPLE_FIELD(org.apache.metron.profiler.storm.ProfileSplitterBolt.MESSAGE_TUPLE_FIELD)

Example 23 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project accumulo by apache.

the class SummaryIT method basicSummaryTest.

@Test
public void basicSummaryTest() throws Exception {
    final String table = getUniqueNames(1)[0];
    try (AccumuloClient c = Accumulo.newClient().from(getClientProps()).build()) {
        NewTableConfiguration ntc = new NewTableConfiguration();
        SummarizerConfiguration sc1 = SummarizerConfiguration.builder(BasicSummarizer.class.getName()).build();
        ntc.enableSummarization(sc1);
        c.tableOperations().create(table, ntc);
        BatchWriter bw = writeData(table, c);
        Collection<Summary> summaries = c.tableOperations().summaries(table).flush(false).retrieve();
        assertEquals(0, summaries.size());
        LongSummaryStatistics stats = getTimestampStats(table, c);
        summaries = c.tableOperations().summaries(table).flush(true).retrieve();
        checkSummaries(summaries, sc1, 1, 0, 0, TOTAL_STAT, 100_000L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
        Mutation m = new Mutation(String.format("r%09x", 999));
        m.put("f1", "q1", "999-0");
        m.putDelete("f1", "q2");
        bw.addMutation(m);
        bw.flush();
        c.tableOperations().flush(table, null, null, true);
        stats = getTimestampStats(table, c);
        summaries = c.tableOperations().summaries(table).retrieve();
        checkSummaries(summaries, sc1, 2, 0, 0, TOTAL_STAT, 100_002L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 1L);
        bw.close();
        c.tableOperations().compact(table, new CompactionConfig().setWait(true));
        summaries = c.tableOperations().summaries(table).retrieve();
        checkSummaries(summaries, sc1, 1, 0, 0, TOTAL_STAT, 100_000L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
        // split tablet into two
        String sp1 = String.format("r%09x", 50_000);
        addSplits(table, c, sp1);
        summaries = c.tableOperations().summaries(table).retrieve();
        checkSummaries(summaries, sc1, 1, 0, 0, TOTAL_STAT, 100_000L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
        // compact 2nd tablet
        c.tableOperations().compact(table, new CompactionConfig().setStartRow(new Text(sp1)).setWait(true));
        summaries = c.tableOperations().summaries(table).retrieve();
        checkSummaries(summaries, sc1, 2, 0, 1, TOTAL_STAT, 113_999L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
        // get summaries for first tablet
        stats = getTimestampStats(table, c, sp1, null);
        summaries = c.tableOperations().summaries(table).startRow(sp1).retrieve();
        checkSummaries(summaries, sc1, 1, 0, 0, TOTAL_STAT, 49_999L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
        // compact all tablets and regenerate all summaries
        c.tableOperations().compact(table, new CompactionConfig());
        summaries = c.tableOperations().summaries(table).retrieve();
        stats = getTimestampStats(table, c);
        checkSummaries(summaries, sc1, 2, 0, 0, TOTAL_STAT, 100_000L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
        summaries = c.tableOperations().summaries(table).startRow(String.format("r%09x", 75_000)).endRow(String.format("r%09x", 80_000)).retrieve();
        Summary summary = Iterables.getOnlyElement(summaries);
        assertEquals(1, summary.getFileStatistics().getTotal());
        assertEquals(1, summary.getFileStatistics().getExtra());
        long total = summary.getStatistics().get(TOTAL_STAT);
        assertTrue("Total " + total + " out of expected range", total > 0 && total <= 10_000);
        // test adding and removing
        c.tableOperations().removeSummarizers(table, sc -> sc.getClassName().contains("foo"));
        List<SummarizerConfiguration> summarizers = c.tableOperations().listSummarizers(table);
        assertEquals(1, summarizers.size());
        assertTrue(summarizers.contains(sc1));
        c.tableOperations().removeSummarizers(table, sc -> sc.getClassName().equals(BasicSummarizer.class.getName()));
        summarizers = c.tableOperations().listSummarizers(table);
        assertEquals(0, summarizers.size());
        c.tableOperations().compact(table, new CompactionConfig().setWait(true));
        summaries = c.tableOperations().summaries(table).retrieve();
        assertEquals(0, summaries.size());
        c.tableOperations().addSummarizers(table, sc1);
        c.tableOperations().compact(table, new CompactionConfig().setWait(true));
        summaries = c.tableOperations().summaries(table).retrieve();
        checkSummaries(summaries, sc1, 2, 0, 0, TOTAL_STAT, 100_000L, MIN_TIMESTAMP_STAT, stats.getMin(), MAX_TIMESTAMP_STAT, stats.getMax(), DELETES_STAT, 0L);
    }
}
Also used : AccumuloClient(org.apache.accumulo.core.client.AccumuloClient) Text(org.apache.hadoop.io.Text) LongSummaryStatistics(java.util.LongSummaryStatistics) NewTableConfiguration(org.apache.accumulo.core.client.admin.NewTableConfiguration) CompactionConfig(org.apache.accumulo.core.client.admin.CompactionConfig) CounterSummary(org.apache.accumulo.core.client.summary.CounterSummary) Summary(org.apache.accumulo.core.client.summary.Summary) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) SummarizerConfiguration(org.apache.accumulo.core.client.summary.SummarizerConfiguration) Test(org.junit.Test)

Example 24 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project intellij-community by JetBrains.

the class Main method test.

public static LongSummaryStatistics test(List<List<String>> list) {
    LongSummaryStatistics stat = new LongSummaryStatistics();
    for (List<String> a : list) {
        if (a != null) {
            for (String s : a) {
                long length = s.length();
                stat.accept(length);
            }
        }
    }
    return stat;
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics)

Example 25 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project jdk8u_jdk by JetBrains.

the class SummaryStatisticsTest method testLongStatistics.

public void testLongStatistics() {
    List<LongSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i)));
    instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics());
    for (LongSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000L);
        assertEquals(stats.getMin(), 1L);
    }
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics) List(java.util.List) LambdaTestHelpers.countTo(java.util.stream.LambdaTestHelpers.countTo) OpTestCase(java.util.stream.OpTestCase) IntSummaryStatistics(java.util.IntSummaryStatistics) Test(org.testng.annotations.Test) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Collectors(java.util.stream.Collectors) LongSummaryStatistics(java.util.LongSummaryStatistics) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList)

Aggregations

LongSummaryStatistics (java.util.LongSummaryStatistics)47 Test (org.junit.Test)18 List (java.util.List)15 Map (java.util.Map)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ArrayList (java.util.ArrayList)10 Collectors (java.util.stream.Collectors)10 Set (java.util.Set)9 TimeUnit (java.util.concurrent.TimeUnit)9 LoggerFactory (org.slf4j.LoggerFactory)9 HashMap (java.util.HashMap)8 Logger (org.slf4j.Logger)8 BaseTest (io.scalecube.testlib.BaseTest)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Arrays (java.util.Arrays)5 Stream (java.util.stream.Stream)5 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 Entry (java.util.Map.Entry)4