accumulo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joshelser <...@git.apache.org>
Subject [GitHub] accumulo pull request #224: ACCUMULO-4500 ACCUMULO-96 Added summarization
Date Thu, 09 Mar 2017 19:43:08 GMT
Github user joshelser commented on a diff in the pull request:

    https://github.com/apache/accumulo/pull/224#discussion_r105245989
  
    --- Diff: test/src/main/java/org/apache/accumulo/test/functional/SummaryIT.java ---
    @@ -0,0 +1,759 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.accumulo.test.functional;
    +
    +import static java.util.function.Function.identity;
    +import static java.util.stream.Collectors.counting;
    +import static java.util.stream.Collectors.groupingBy;
    +import static org.apache.accumulo.core.client.summary.CountingSummarizer.DELETES_IGNORED_STAT;
    +import static org.apache.accumulo.core.client.summary.CountingSummarizer.EMITTED_STAT;
    +import static org.apache.accumulo.core.client.summary.CountingSummarizer.SEEN_STAT;
    +import static org.apache.accumulo.core.client.summary.CountingSummarizer.TOO_LONG_STAT;
    +import static org.apache.accumulo.core.client.summary.CountingSummarizer.TOO_MANY_STAT;
    +
    +import java.io.IOException;
    +import java.util.Arrays;
    +import java.util.Collection;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.LongSummaryStatistics;
    +import java.util.Map;
    +import java.util.Map.Entry;
    +import java.util.Set;
    +import java.util.TreeSet;
    +import java.util.regex.PatternSyntaxException;
    +import java.util.stream.Stream;
    +import java.util.stream.StreamSupport;
    +
    +import org.apache.accumulo.core.client.AccumuloException;
    +import org.apache.accumulo.core.client.AccumuloSecurityException;
    +import org.apache.accumulo.core.client.BatchWriter;
    +import org.apache.accumulo.core.client.BatchWriterConfig;
    +import org.apache.accumulo.core.client.Connector;
    +import org.apache.accumulo.core.client.IteratorSetting;
    +import org.apache.accumulo.core.client.MutationsRejectedException;
    +import org.apache.accumulo.core.client.Scanner;
    +import org.apache.accumulo.core.client.TableNotFoundException;
    +import org.apache.accumulo.core.client.TableOfflineException;
    +import org.apache.accumulo.core.client.ZooKeeperInstance;
    +import org.apache.accumulo.core.client.admin.CompactionConfig;
    +import org.apache.accumulo.core.client.admin.CompactionStrategyConfig;
    +import org.apache.accumulo.core.client.admin.NewTableConfiguration;
    +import org.apache.accumulo.core.client.impl.AccumuloServerException;
    +import org.apache.accumulo.core.client.security.SecurityErrorCode;
    +import org.apache.accumulo.core.client.security.tokens.PasswordToken;
    +import org.apache.accumulo.core.client.summary.Summarizer;
    +import org.apache.accumulo.core.client.summary.SummarizerConfiguration;
    +import org.apache.accumulo.core.client.summary.Summary;
    +import org.apache.accumulo.core.client.summary.summarizers.FamilySummarizer;
    +import org.apache.accumulo.core.client.summary.summarizers.VisibilitySummarizer;
    +import org.apache.accumulo.core.data.Key;
    +import org.apache.accumulo.core.data.Mutation;
    +import org.apache.accumulo.core.data.Range;
    +import org.apache.accumulo.core.data.Value;
    +import org.apache.accumulo.core.iterators.Filter;
    +import org.apache.accumulo.core.security.Authorizations;
    +import org.apache.accumulo.core.security.TablePermission;
    +import org.apache.accumulo.fate.util.UtilWaitThread;
    +import org.apache.accumulo.harness.AccumuloClusterHarness;
    +import org.apache.accumulo.tserver.compaction.CompactionPlan;
    +import org.apache.accumulo.tserver.compaction.CompactionStrategy;
    +import org.apache.accumulo.tserver.compaction.MajorCompactionRequest;
    +import org.apache.hadoop.io.Text;
    +import org.junit.Assert;
    +import org.junit.Test;
    +
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.ImmutableMap.Builder;
    +import com.google.common.collect.ImmutableSet;
    +import com.google.common.collect.Iterables;
    +import com.google.common.collect.Lists;
    +
    +public class SummaryIT extends AccumuloClusterHarness {
    +
    +  private LongSummaryStatistics getTimestampStats(final String table, Connector c) throws
TableNotFoundException {
    +    try (Scanner scanner = c.createScanner(table, Authorizations.EMPTY)) {
    +      Stream<Entry<Key,Value>> stream = StreamSupport.stream(scanner.spliterator(),
false);
    +      LongSummaryStatistics stats = stream.mapToLong(e -> e.getKey().getTimestamp()).summaryStatistics();
    +      return stats;
    +    }
    +  }
    +
    +  private LongSummaryStatistics getTimestampStats(final String table, Connector c, String
startRow, String endRow) throws TableNotFoundException {
    +    try (Scanner scanner = c.createScanner(table, Authorizations.EMPTY)) {
    +      scanner.setRange(new Range(startRow, false, endRow, true));
    +      Stream<Entry<Key,Value>> stream = StreamSupport.stream(scanner.spliterator(),
false);
    +      LongSummaryStatistics stats = stream.mapToLong(e -> e.getKey().getTimestamp()).summaryStatistics();
    +      return stats;
    +    }
    +  }
    +
    +  private void checkSummaries(Collection<Summary> summaries, SummarizerConfiguration
sc, int total, int missing, int extra, Object... kvs) {
    +    Summary summary = Iterables.getOnlyElement(summaries);
    +    Assert.assertEquals("total wrong", total, summary.getFileStatistics().getTotal());
    +    Assert.assertEquals("missing wrong", missing, summary.getFileStatistics().getMissing());
    +    Assert.assertEquals("extra wrong", extra, summary.getFileStatistics().getExtra());
    +    Assert.assertEquals(sc, summary.getSummarizerConfiguration());
    +    Map<String,Long> expected = new HashMap<>();
    +    for (int i = 0; i < kvs.length; i += 2) {
    +      expected.put((String) kvs[i], (Long) kvs[i + 1]);
    +    }
    +    Assert.assertEquals(expected, summary.getStatistics());
    +  }
    +
    +  private void addSplits(final String table, Connector c, String... splits) throws TableNotFoundException,
AccumuloException, AccumuloSecurityException {
    +    c.tableOperations().addSplits(table, new TreeSet<Text>(Lists.transform(Arrays.asList(splits),
Text::new)));
    +  }
    +
    +  @Test
    +  public void basicSummaryTest() throws Exception {
    +    final String table = getUniqueNames(1)[0];
    +    Connector c = getConnector();
    +    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().getSummaries(table).flush(false).retrieve();
    +    Assert.assertEquals(0, summaries.size());
    +
    +    LongSummaryStatistics stats = getTimestampStats(table, c);
    +
    +    summaries = c.tableOperations().getSummaries(table).flush(true).retrieve();
    +    checkSummaries(summaries, sc1, 1, 0, 0, "total", 100_000l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 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().getSummaries(table).retrieve();
    +
    +    checkSummaries(summaries, sc1, 2, 0, 0, "total", 100_002l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 1l);
    +
    +    bw.close();
    +
    +    c.tableOperations().compact(table, new CompactionConfig().setWait(true));
    +
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +    checkSummaries(summaries, sc1, 1, 0, 0, "total", 100_000l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 0l);
    +
    +    // split tablet into two
    +    String sp1 = String.format("r%09x", 50_000);
    +    addSplits(table, c, sp1);
    +
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +
    +    checkSummaries(summaries, sc1, 1, 0, 0, "total", 100_000l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 0l);
    +
    +    // compact 2nd tablet
    +    c.tableOperations().compact(table, new CompactionConfig().setStartRow(new Text(sp1)).setWait(true));
    +
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +    checkSummaries(summaries, sc1, 2, 0, 1, "total", 113_999l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 0l);
    +
    +    // get summaries for first tablet
    +    stats = getTimestampStats(table, c, sp1, null);
    +    summaries = c.tableOperations().getSummaries(table).startRow(sp1).retrieve();
    +    checkSummaries(summaries, sc1, 1, 0, 0, "total", 49_999l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 0l);
    +
    +    // compact all tablets and regenerate all summaries
    +    c.tableOperations().compact(table, new CompactionConfig());
    +
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +    stats = getTimestampStats(table, c);
    +    checkSummaries(summaries, sc1, 2, 0, 0, "total", 100_000l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 0l);
    +
    +    summaries = c.tableOperations().getSummaries(table).startRow(String.format("r%09x",
75_000)).endRow(String.format("r%09x", 80_000)).retrieve();
    +    Summary summary = Iterables.getOnlyElement(summaries);
    +    Assert.assertEquals(1, summary.getFileStatistics().getTotal());
    +    Assert.assertEquals(1, summary.getFileStatistics().getExtra());
    +    long total = summary.getStatistics().get("total");
    +    Assert.assertTrue(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);
    +    Assert.assertEquals(1, summarizers.size());
    +    Assert.assertTrue(summarizers.contains(sc1));
    +
    +    c.tableOperations().removeSummarizers(table, sc -> sc.getClassName().equals(BasicSummarizer.class.getName()));
    +    summarizers = c.tableOperations().listSummarizers(table);
    +    Assert.assertEquals(0, summarizers.size());
    +
    +    c.tableOperations().compact(table, new CompactionConfig().setWait(true));
    +
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +    Assert.assertEquals(0, summaries.size());
    +
    +    c.tableOperations().addSummarizers(table, sc1);
    +    c.tableOperations().compact(table, new CompactionConfig().setWait(true));
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +    checkSummaries(summaries, sc1, 2, 0, 0, "total", 100_000l, "minStamp", stats.getMin(),
"maxStamp", stats.getMax(), "deletes", 0l);
    +  }
    +
    +  private BatchWriter writeData(final String table, Connector c) throws TableNotFoundException,
MutationsRejectedException {
    +    BatchWriter bw = c.createBatchWriter(table, new BatchWriterConfig());
    +    for (int i = 0; i < 100_000; i++) {
    +      Mutation m = new Mutation(String.format("r%09x", i));
    +      m.put("f1", "q1", "" + i);
    +      bw.addMutation(m);
    +    }
    +    bw.flush();
    +    return bw;
    +  }
    +
    +  public static class KeySizeSummarizer implements Summarizer {
    +
    +    @Override
    +    public Collector collector(SummarizerConfiguration sc) {
    +      return new Collector() {
    +        private int maxLen = Integer.parseInt(sc.getOptions().getOrDefault("maxLen",
"1024"));
    +        private long[] lengths = new long[maxLen];
    +        private long overMax = 0;
    +
    +        @Override
    +        public void accept(Key k, Value v) {
    +          int size = k.getSize();
    +          if (size >= maxLen) {
    +            overMax++;
    +          } else {
    +            lengths[size]++;
    +          }
    +        }
    +
    +        @Override
    +        public void summarize(StatisticConsumer sc) {
    +          if (overMax > 0) {
    +            sc.accept("len>=" + maxLen, overMax);
    +          }
    +          for (int i = 0; i < lengths.length; i++) {
    +            if (lengths[i] > 0) {
    +              sc.accept("len=" + i, lengths[i]);
    +            }
    +          }
    +        }
    +
    +      };
    +    }
    +
    +    @Override
    +    public Combiner combiner(SummarizerConfiguration sc) {
    +      return (m1, m2) -> m2.forEach((k, v) -> m1.merge(k, v, Long::sum));
    +    }
    +  }
    +
    +  private static void checkSummary(Collection<Summary> summaries, SummarizerConfiguration
sc, Object... stats) {
    +    Map<String,Long> expected = new HashMap<>();
    +    for (int i = 0; i < stats.length; i += 2) {
    +      expected.put((String) stats[i], (Long) stats[i + 1]);
    +    }
    +
    +    for (Summary summary : summaries) {
    +      if (summary.getSummarizerConfiguration().equals(sc)) {
    +        Assert.assertEquals(expected, summary.getStatistics());
    +        return;
    +      }
    +    }
    +
    +    Assert.fail("Did not find summary with config : " + sc);
    +  }
    +
    +  @Test
    +  public void selectionTest() throws Exception {
    +    final String table = getUniqueNames(1)[0];
    +    Connector c = getConnector();
    +    NewTableConfiguration ntc = new NewTableConfiguration();
    +    SummarizerConfiguration sc1 = SummarizerConfiguration.builder(BasicSummarizer.class).build();
    +    SummarizerConfiguration sc2 = SummarizerConfiguration.builder(KeySizeSummarizer.class).addOption("maxLen",
"512").build();
    +    ntc.enableSummarization(sc1, sc2);
    +    c.tableOperations().create(table, ntc);
    +
    +    BatchWriter bw = writeData(table, c);
    +    bw.close();
    +
    +    c.tableOperations().flush(table, null, null, true);
    +
    +    LongSummaryStatistics stats = getTimestampStats(table, c);
    +
    +    Collection<Summary> summaries = c.tableOperations().getSummaries(table).withConfiguration(sc2).retrieve();
    +    Assert.assertEquals(1, summaries.size());
    +    checkSummary(summaries, sc2, "len=14", 100_000l);
    +
    +    summaries = c.tableOperations().getSummaries(table).withConfiguration(sc1).retrieve();
    +    Assert.assertEquals(1, summaries.size());
    +    checkSummary(summaries, sc1, "total", 100_000l, "minStamp", stats.getMin(), "maxStamp",
stats.getMax(), "deletes", 0l);
    +
    +    // retrieve a non-existant summary
    +    SummarizerConfiguration sc3 = SummarizerConfiguration.builder(KeySizeSummarizer.class.getName()).addOption("maxLen",
"256").build();
    +    summaries = c.tableOperations().getSummaries(table).withConfiguration(sc3).retrieve();
    +    Assert.assertEquals(0, summaries.size());
    +
    +    summaries = c.tableOperations().getSummaries(table).withConfiguration(sc1, sc2).retrieve();
    +    Assert.assertEquals(2, summaries.size());
    +    checkSummary(summaries, sc1, "total", 100_000l, "minStamp", stats.getMin(), "maxStamp",
stats.getMax(), "deletes", 0l);
    +    checkSummary(summaries, sc2, "len=14", 100_000l);
    +
    +    summaries = c.tableOperations().getSummaries(table).retrieve();
    +    Assert.assertEquals(2, summaries.size());
    +    checkSummary(summaries, sc1, "total", 100_000l, "minStamp", stats.getMin(), "maxStamp",
stats.getMax(), "deletes", 0l);
    +    checkSummary(summaries, sc2, "len=14", 100_000l);
    +
    +    summaries = c.tableOperations().getSummaries(table).withMatchingConfiguration(".*BasicSummarizer
\\{\\}.*").retrieve();
    +    Assert.assertEquals(1, summaries.size());
    +    checkSummary(summaries, sc1, "total", 100_000l, "minStamp", stats.getMin(), "maxStamp",
stats.getMax(), "deletes", 0l);
    +
    +    summaries = c.tableOperations().getSummaries(table).withMatchingConfiguration(".*KeySizeSummarizer
\\{maxLen=512\\}.*").retrieve();
    +    Assert.assertEquals(1, summaries.size());
    +    checkSummary(summaries, sc2, "len=14", 100_000l);
    +
    +    summaries = c.tableOperations().getSummaries(table).withMatchingConfiguration(".*KeySizeSummarizer
\\{maxLen=256\\}.*").retrieve();
    +    Assert.assertEquals(0, summaries.size());
    +
    +    summaries = c.tableOperations().getSummaries(table).withMatchingConfiguration(".*BasicSummarizer
\\{\\}.*").withConfiguration(sc2).retrieve();
    +    Assert.assertEquals(2, summaries.size());
    +    checkSummary(summaries, sc1, "total", 100_000l, "minStamp", stats.getMin(), "maxStamp",
stats.getMax(), "deletes", 0l);
    +    checkSummary(summaries, sc2, "len=14", 100_000l);
    +
    +    // Ensure a bad regex fails fast.
    +    try {
    +      summaries = c.tableOperations().getSummaries(table).withMatchingConfiguration(".*KeySizeSummarizer
{maxLen=256}.*").retrieve();
    +      Assert.fail("Bad regex should have caused exception");
    +    } catch (PatternSyntaxException e) {}
    +  }
    +
    +  public static class FooCounter implements Summarizer {
    +
    +    @Override
    +    public Collector collector(SummarizerConfiguration sc) {
    +      return new Collector() {
    +
    +        long foos = 0;
    +        long bars = 0;
    +
    +        @Override
    +        public void accept(Key k, Value v) {
    +          String row = k.getRowData().toString();
    +
    +          if (row.contains("foo")) {
    +            foos++;
    +          } else if (row.contains("bar")) {
    +            bars++;
    +          }
    +        }
    +
    +        @Override
    +        public void summarize(StatisticConsumer sc) {
    +          sc.accept("foos", foos);
    +          sc.accept("bars", bars);
    +        }
    +
    +      };
    +    }
    +
    +    @Override
    +    public Combiner combiner(SummarizerConfiguration sc) {
    +      return (m1, m2) -> m2.forEach((k, v) -> m1.merge(k, v, Long::sum));
    +    }
    +  }
    +
    +  public static class FooFilter extends Filter {
    +    @Override
    +    public boolean accept(Key k, Value v) {
    +      return !k.getRowData().toString().contains("foo");
    +    }
    +  }
    +
    +  public static class FooCS extends CompactionStrategy {
    --- End diff --
    
    Can you provide a better name and/or javadoc?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message