Github user mjwall commented on a diff in the pull request:

    https://github.com/apache/accumulo/pull/82#discussion_r57000165
  
    --- Diff: test/src/test/java/org/apache/accumulo/test/InMemoryMapIT.java ---
    @@ -0,0 +1,320 @@
    +/*
    + * 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;
    +
    +import com.google.common.collect.ImmutableSet;
    +import java.io.File;
    +import java.io.FileNotFoundException;
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.logging.Level;
    +import org.apache.accumulo.core.data.ArrayByteSequence;
    +import org.apache.accumulo.core.data.ByteSequence;
    +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.SortedKeyValueIterator;
    +import org.apache.accumulo.test.functional.NativeMapIT;
    +import org.apache.accumulo.tserver.InMemoryMap;
    +import org.apache.accumulo.tserver.MemKey;
    +import org.apache.accumulo.tserver.NativeMap;
    +import org.apache.hadoop.io.Text;
    +import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.fail;
    +import org.junit.BeforeClass;
    +import org.junit.Rule;
    +import org.junit.Test;
    +import org.junit.rules.TemporaryFolder;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * Integration Test for https://issues.apache.org/jira/browse/ACCUMULO-4148
    + * <p>
    + * User had problem writing one Mutation with multiple KV pairs that had 
the same key. Doing so should write out all pairs in all mutations with a 
unique id. In
    + * typical operation, you would only see the last one when scanning. User 
had a combiner on the table, and they noticed that when using InMemoryMap with
    + * NativeMapWrapper, only the last KV pair was ever written. When 
InMemoryMap used DefaultMap, all KV pairs were added and the behavior worked as 
expected.
    + *
    + * This IT inserts a variety of Mutations with and without the same KV 
pairs and then inspects result of InMemoryMap mutate, looking for unique id 
stored with
    + * each key. This unique id, shown as mc= in the MemKey toString, was 
originally used for scan Isolation. Writing the same key multiple times in the 
same
    + * mutation is a secondary use case, discussed in 
https://issues.apache.org/jira/browse/ACCUMULO-227. In addition to 
NativeMapWrapper and DefaultMap,
    + * LocalityGroupMap was add in 
https://issues.apache.org/jira/browse/ACCUMULO-112.
    + *
    + * This test has to be an IT in accumulo-test, because libaccumulo is 
built in 'integration-test' phase of accumulo-native, which currently runs 
right before
    + * accumulo-test. The tests for DefaultMap could move to a unit test in 
tserver, but they are here for convenience of viewing both at the same time.
    + */
    +public class InMemoryMapIT {
    +
    +  private static final Logger log = 
LoggerFactory.getLogger(InMemoryMapIT.class);
    +
    +  @Rule
    +  public TemporaryFolder tempFolder = new TemporaryFolder(new 
File(System.getProperty("user.dir") + "/target"));
    +
    +  @BeforeClass
    +  public static void ensureNativeLibrary() throws FileNotFoundException {
    +    File nativeMapLocation = NativeMapIT.nativeMapLocation();
    +    log.debug("Native map location " + nativeMapLocation);
    +    NativeMap.loadNativeLib(Collections.singletonList(nativeMapLocation));
    +    if (!NativeMap.isLoaded()) {
    +      fail("Missing the native library from " + 
nativeMapLocation.getAbsolutePath() + "\nYou need to build the libaccumulo 
binary first. "
    +          + "\nTry running 'mvn clean install -Dit.test=InMemoryMapIT 
-Dtest=foo -DfailIfNoTests=false -Dfindbugs.skip -Dcheckstyle.skip'");
    +      // afterwards, you can run the following
    +      // mvn clean verify -Dit.test=InMemoryMapIT -Dtest=foo 
-DfailIfNoTests=false -Dfindbugs.skip -Dcheckstyle.skip -pl :accumulo-test
    +    }
    +    log.debug("Native map loaded");
    +
    +  }
    +
    +  @Test
    +  public void testOneMutationOneKey() {
    +    Mutation m = new Mutation("a");
    +    m.put(new Text("1cf"), new Text("1cq"), new Value("vala".getBytes()));
    +
    +    assertEquivalentMutate(m);
    +  }
    +
    +  @Test
    +  public void testOneMutationManyKeys() throws IOException {
    +    Mutation m = new Mutation("a");
    +    for (int i = 1; i < 6; i++) {
    +      m.put(new Text("2cf" + i), new Text("2cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +
    +    assertEquivalentMutate(m);
    +  }
    +
    +  @Test
    +  public void testOneMutationManySameKeys() {
    +    Mutation m = new Mutation("a");
    +    for (int i = 1; i <= 5; i++) {
    +      // same keys
    +      m.put(new Text("3cf"), new Text("3cq"), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +
    +    assertEquivalentMutate(m);
    +  }
    +
    +  @Test
    +  public void testMultipleMutationsOneKey() {
    +    Mutation m1 = new Mutation("a");
    +    m1.put(new Text("4cf"), new Text("4cq"), new Value("vala".getBytes()));
    +    Mutation m2 = new Mutation("b");
    +    m2.put(new Text("4cf"), new Text("4cq"), new Value("vala".getBytes()));
    +
    +    assertEquivalentMutate(Arrays.asList(m1, m2));
    +  }
    +
    +  @Test
    +  public void testMultipleMutationsSameOneKey() {
    +    Mutation m1 = new Mutation("a");
    +    m1.put(new Text("5cf"), new Text("5cq"), new Value("vala".getBytes()));
    +    Mutation m2 = new Mutation("a");
    +    m2.put(new Text("5cf"), new Text("5cq"), new Value("vala".getBytes()));
    +
    +    assertEquivalentMutate(Arrays.asList(m1, m2));
    +  }
    +
    +  @Test
    +  public void testMutlipleMutationsMultipleKeys() {
    +    Mutation m1 = new Mutation("a");
    +    for (int i = 1; i < 6; i++) {
    +      m1.put(new Text("6cf" + i), new Text("6cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    Mutation m2 = new Mutation("b");
    +    for (int i = 1; i < 3; i++) {
    +      m2.put(new Text("6cf" + i), new Text("6cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +
    +    assertEquivalentMutate(Arrays.asList(m1, m2));
    +  }
    +
    +  @Test
    +  public void testMultipleMutationsMultipleSameKeys() {
    +    Mutation m1 = new Mutation("a");
    +    for (int i = 1; i < 3; i++) {
    +      m1.put(new Text("7cf"), new Text("7cq"), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    Mutation m2 = new Mutation("a");
    +    for (int i = 1; i < 4; i++) {
    +      m2.put(new Text("7cf"), new Text("7cq"), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +
    +    assertEquivalentMutate(Arrays.asList(m1, m2));
    +  }
    +
    +  @Test
    +  public void testMultipleMutationsMultipleKeysSomeSame() {
    +    Mutation m1 = new Mutation("a");
    +    for (int i = 1; i < 2; i++) {
    +      m1.put(new Text("8cf"), new Text("8cq"), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    for (int i = 1; i < 3; i++) {
    +      m1.put(new Text("8cf" + i), new Text("8cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    for (int i = 1; i < 2; i++) {
    +      m1.put(new Text("8cf" + i), new Text("8cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    Mutation m2 = new Mutation("a");
    +    for (int i = 1; i < 3; i++) {
    +      m2.put(new Text("8cf"), new Text("8cq"), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    for (int i = 1; i < 4; i++) {
    +      m2.put(new Text("8cf" + i), new Text("8cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +    Mutation m3 = new Mutation("b");
    +    for (int i = 1; i < 3; i++) {
    +      m3.put(new Text("8cf" + i), new Text("8cq" + i), new 
Value(Integer.toString(i).getBytes()));
    +    }
    +
    +    assertEquivalentMutate(Arrays.asList(m1, m2, m3));
    +  }
    +
    +  private void assertEquivalentMutate(Mutation m) {
    +    assertEquivalentMutate(Collections.singletonList(m));
    +  }
    +
    +  private void assertEquivalentMutate(List<Mutation> mutations) {
    +    InMemoryMap defaultMap = null;
    +    InMemoryMap nativeMapWrapper = null;
    +    InMemoryMap localityGroupMap = null;
    +    InMemoryMap localityGroupMapWithNative = null;
    +
    +    try {
    +      defaultMap = new InMemoryMap(false, 
tempFolder.newFolder().getAbsolutePath());
    +      nativeMapWrapper = new InMemoryMap(true, 
tempFolder.newFolder().getAbsolutePath());
    +      localityGroupMap = new InMemoryMap(getLocalityGroups(), false, 
tempFolder.newFolder().getAbsolutePath());
    +      localityGroupMapWithNative = new InMemoryMap(getLocalityGroups(), 
false, tempFolder.newFolder().getAbsolutePath());
    +    } catch (IOException e) {
    +      log.error("Error getting new InMemoryMap ", e);
    +      fail(e.getMessage());
    +    }
    +
    +    defaultMap.mutate(mutations);
    +    nativeMapWrapper.mutate(mutations);
    +    localityGroupMap.mutate(mutations);
    +    localityGroupMapWithNative.mutate(mutations);
    +
    +    // let's use the transitive property to assert all four are equivalent
    +    assertMutatesEquivalent(mutations, defaultMap, nativeMapWrapper);
    +    assertMutatesEquivalent(mutations, defaultMap, localityGroupMap);
    +    assertMutatesEquivalent(mutations, defaultMap, 
localityGroupMapWithNative);
    +  }
    +
    +  /**
    +   * Assert that a set of mutations mutate to equivalent map in both of 
the InMemoryMaps.
    +   * <p>
    +   * In this case, equivalent means 2 things.
    +   * <ul>
    +   * <li>The size of both maps generated is equal to the number of key 
value pairs in all mutations passed</li>
    +   * <li>The size of the map generated from the first InMemoryMap equals 
the size of the map generated from the second</li>
    +   * <li>Each key value pair in each mutated map has a unique id 
(kvCount)</li>
    +   * </ul>
    +   *
    +   * @param mutations
    +   *          List of mutations
    +   * @param imm1
    +   *          InMemoryMap to compare
    +   * @param imm2
    +   *          InMemoryMap to compare
    +   */
    +  private void assertMutatesEquivalent(List<Mutation> mutations, 
InMemoryMap imm1, InMemoryMap imm2) {
    +    int mutationKVPairs = countKVPairs(mutations);
    +
    +    List<MemKey> memKeys1 = getArrayOfMemKeys(imm1);
    +    List<MemKey> memKeys2 = getArrayOfMemKeys(imm2);
    +
    +    assertEquals("Not all key value pairs included: " + 
dumpInMemoryMap(imm1, memKeys1), mutationKVPairs, memKeys1.size());
    +    assertEquals("Not all key value pairs included: " + 
dumpInMemoryMap(imm2, memKeys2), mutationKVPairs, memKeys2.size());
    +    assertEquals("InMemoryMaps differ in size: " + dumpInMemoryMap(imm1, 
memKeys1) + "\n" + dumpInMemoryMap(imm2, memKeys2), memKeys1.size(), 
memKeys2.size());
    --- End diff --
    
    I think it will always be true.  I initially did it this way to give a very 
specific message if something was wrong.  But you are correct, if the 
InMemoryMaps differ in size, one of the two prior asserts will fail first.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to