accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ujustgotbi...@apache.org
Subject [3/5] Merge remote-tracking branch 'apache-committers/1.4.5-SNAPSHOT' into 1.5.1-SNAPSHOT
Date Mon, 18 Nov 2013 19:38:38 GMT
http://git-wip-us.apache.org/repos/asf/accumulo/blob/04f81b50/core/src/test/java/org/apache/accumulo/core/client/impl/TabletLocatorImplTest.java
----------------------------------------------------------------------
diff --cc core/src/test/java/org/apache/accumulo/core/client/impl/TabletLocatorImplTest.java
index 8c63b1f,0000000..a42c280
mode 100644,000000..100644
--- a/core/src/test/java/org/apache/accumulo/core/client/impl/TabletLocatorImplTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/client/impl/TabletLocatorImplTest.java
@@@ -1,1280 -1,0 +1,1285 @@@
 +/*
 + * 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.core.client.impl;
 +
 +import java.nio.ByteBuffer;
 +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.Map.Entry;
 +import java.util.Set;
 +import java.util.SortedMap;
 +import java.util.TreeMap;
 +
 +import junit.framework.TestCase;
 +
 +import org.apache.accumulo.core.Constants;
 +import org.apache.accumulo.core.client.AccumuloException;
 +import org.apache.accumulo.core.client.AccumuloSecurityException;
 +import org.apache.accumulo.core.client.Connector;
 +import org.apache.accumulo.core.client.Instance;
 +import org.apache.accumulo.core.client.impl.TabletLocator.TabletLocation;
 +import org.apache.accumulo.core.client.impl.TabletLocator.TabletLocations;
 +import org.apache.accumulo.core.client.impl.TabletLocator.TabletServerMutations;
 +import org.apache.accumulo.core.client.impl.TabletLocatorImpl.TabletLocationObtainer;
 +import org.apache.accumulo.core.client.security.tokens.AuthenticationToken;
 +import org.apache.accumulo.core.conf.AccumuloConfiguration;
 +import org.apache.accumulo.core.data.Key;
 +import org.apache.accumulo.core.data.KeyExtent;
 +import org.apache.accumulo.core.data.Mutation;
 +import org.apache.accumulo.core.data.PartialKey;
 +import org.apache.accumulo.core.data.Range;
 +import org.apache.accumulo.core.data.Value;
 +import org.apache.accumulo.core.security.thrift.TCredentials;
 +import org.apache.accumulo.core.util.MetadataTable;
 +import org.apache.accumulo.core.util.Pair;
 +import org.apache.hadoop.io.Text;
 +
 +public class TabletLocatorImplTest extends TestCase {
 +  
 +  private static final KeyExtent RTE = Constants.ROOT_TABLET_EXTENT;
 +  private static final KeyExtent MTE = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), null, RTE.getEndRow());
 +  private static TCredentials credential = null;
 +  
 +  static KeyExtent nke(String t, String er, String per) {
 +    return new KeyExtent(new Text(t), er == null ? null : new Text(er), per == null ? null : new Text(per));
 +  }
 +  
 +  static Range nr(String k1, boolean si, String k2, boolean ei) {
 +    return new Range(k1 == null ? null : new Text(k1), si, k2 == null ? null : new Text(k2), ei);
 +  }
 +  
 +  static Range nr(String k1, String k2) {
 +    return new Range(k1 == null ? null : new Text(k1), k2 == null ? null : new Text(k2));
 +  }
 +  
 +  static List<Range> nrl(Range... ranges) {
 +    return Arrays.asList(ranges);
 +  }
 +  
 +  static Object[] nol(Object... objs) {
 +    return objs;
 +  }
 +  
 +  @SuppressWarnings("unchecked")
 +  static Map<String,Map<KeyExtent,List<Range>>> createExpectedBinnings(Object... data) {
 +    
 +    Map<String,Map<KeyExtent,List<Range>>> expBinnedRanges = new HashMap<String,Map<KeyExtent,List<Range>>>();
 +    
 +    for (int i = 0; i < data.length; i += 2) {
 +      String loc = (String) data[i];
 +      Object binData[] = (Object[]) data[i + 1];
 +      
 +      HashMap<KeyExtent,List<Range>> binnedKE = new HashMap<KeyExtent,List<Range>>();
 +      
 +      expBinnedRanges.put(loc, binnedKE);
 +      
 +      for (int j = 0; j < binData.length; j += 2) {
 +        KeyExtent ke = (KeyExtent) binData[j];
 +        List<Range> ranges = (List<Range>) binData[j + 1];
 +        
 +        binnedKE.put(ke, ranges);
 +      }
 +    }
 +    
 +    return expBinnedRanges;
 +  }
 +  
 +  static TreeMap<KeyExtent,TabletLocation> createMetaCacheKE(Object... data) {
 +    TreeMap<KeyExtent,TabletLocation> mcke = new TreeMap<KeyExtent,TabletLocation>();
 +    
 +    for (int i = 0; i < data.length; i += 2) {
 +      KeyExtent ke = (KeyExtent) data[i];
 +      String loc = (String) data[i + 1];
 +      mcke.put(ke, new TabletLocation(ke, loc));
 +    }
 +    
 +    return mcke;
 +  }
 +  
 +  static TreeMap<Text,TabletLocation> createMetaCache(Object... data) {
 +    TreeMap<KeyExtent,TabletLocation> mcke = createMetaCacheKE(data);
 +    
 +    TreeMap<Text,TabletLocation> mc = new TreeMap<Text,TabletLocation>(TabletLocatorImpl.endRowComparator);
 +    
 +    for (Entry<KeyExtent,TabletLocation> entry : mcke.entrySet()) {
 +      if (entry.getKey().getEndRow() == null)
 +        mc.put(TabletLocatorImpl.MAX_TEXT, entry.getValue());
 +      else
 +        mc.put(entry.getKey().getEndRow(), entry.getValue());
 +    }
 +    
 +    return mc;
 +  }
 +  
 +  static TabletLocatorImpl createLocators(TServers tservers, String rootTabLoc, String metaTabLoc, String table, Object... data) {
 +    
 +    TreeMap<KeyExtent,TabletLocation> mcke = createMetaCacheKE(data);
 +    
 +    TestTabletLocationObtainer ttlo = new TestTabletLocationObtainer(tservers);
 +    TestInstance testInstance = new TestInstance("instance1", "tserver1");
 +    
 +    RootTabletLocator rtl = new RootTabletLocator(testInstance);
 +    TabletLocatorImpl rootTabletCache = new TabletLocatorImpl(new Text(Constants.METADATA_TABLE_ID), rtl, ttlo);
 +    TabletLocatorImpl tab1TabletCache = new TabletLocatorImpl(new Text(table), rootTabletCache, ttlo);
 +    
 +    setLocation(tservers, rootTabLoc, RTE, MTE, metaTabLoc);
 +    
 +    for (Entry<KeyExtent,TabletLocation> entry : mcke.entrySet()) {
 +      setLocation(tservers, metaTabLoc, MTE, entry.getKey(), entry.getValue().tablet_location);
 +    }
 +    
 +    return tab1TabletCache;
 +    
 +  }
 +  
 +  static TabletLocatorImpl createLocators(String table, Object... data) {
 +    TServers tservers = new TServers();
 +    return createLocators(tservers, "tserver1", "tserver2", table, data);
 +  }
 +  
 +  private void runTest(Text tableName, List<Range> ranges, TabletLocatorImpl tab1TabletCache, Map<String,Map<KeyExtent,List<Range>>> expected) throws Exception {
 +    List<Range> failures = Collections.emptyList();
 +    runTest(tableName, ranges, tab1TabletCache, expected, failures);
 +  }
 +  
 +  private void runTest(Text tableName, List<Range> ranges, TabletLocatorImpl tab1TabletCache, Map<String,Map<KeyExtent,List<Range>>> expected,
 +      List<Range> efailures) throws Exception {
 +    
 +    Map<String,Map<KeyExtent,List<Range>>> binnedRanges = new HashMap<String,Map<KeyExtent,List<Range>>>();
 +    List<Range> f = tab1TabletCache.binRanges(ranges, binnedRanges, credential);
 +    assertEquals(expected, binnedRanges);
 +    
 +    HashSet<Range> f1 = new HashSet<Range>(f);
 +    HashSet<Range> f2 = new HashSet<Range>(efailures);
 +    
 +    assertEquals(f2, f1);
 +  }
 +  
 +  static Set<KeyExtent> nkes(KeyExtent... extents) {
 +    HashSet<KeyExtent> kes = new HashSet<KeyExtent>();
 +    
 +    for (KeyExtent keyExtent : extents) {
 +      kes.add(keyExtent);
 +    }
 +    
 +    return kes;
 +  }
 +  
 +  static void runTest(TreeMap<Text,TabletLocation> mc, KeyExtent remove, Set<KeyExtent> expected) {
 +    // copy so same metaCache can be used for multiple test
 +    
 +    mc = new TreeMap<Text,TabletLocation>(mc);
 +    
 +    TabletLocatorImpl.removeOverlapping(mc, remove);
 +    
 +    HashSet<KeyExtent> eic = new HashSet<KeyExtent>();
 +    for (TabletLocation tl : mc.values()) {
 +      eic.add(tl.tablet_extent);
 +    }
 +    
 +    assertEquals(expected, eic);
 +  }
 +  
 +  static Mutation nm(String row, String... data) {
 +    Mutation mut = new Mutation(new Text(row));
 +    
 +    for (int i = 0; i < data.length; i++) {
 +      String[] cvp = data[i].split("=");
 +      String[] cols = cvp[0].split(":");
 +      
 +      mut.put(new Text(cols[0]), new Text(cols[1]), new Value(cvp[1].getBytes()));
 +    }
 +    
 +    return mut;
 +  }
 +  
 +  static List<Mutation> nml(Mutation... ma) {
 +    return Arrays.asList(ma);
 +  }
 +  
 +  private void runTest(TabletLocatorImpl metaCache, List<Mutation> ml, Map<String,Map<KeyExtent,List<String>>> emb, String... efailures) throws Exception {
 +    Map<String,TabletServerMutations> binnedMutations = new HashMap<String,TabletServerMutations>();
 +    List<Mutation> afailures = new ArrayList<Mutation>();
 +    metaCache.binMutations(ml, binnedMutations, afailures, credential);
 +    
 +    verify(emb, binnedMutations);
 +    
 +    ArrayList<String> afs = new ArrayList<String>();
 +    ArrayList<String> efs = new ArrayList<String>(Arrays.asList(efailures));
 +    
 +    for (Mutation mutation : afailures) {
 +      afs.add(new String(mutation.getRow()));
 +    }
 +    
 +    Collections.sort(afs);
 +    Collections.sort(efs);
 +    
 +    assertEquals(efs, afs);
 +    
 +  }
 +  
 +  private void verify(Map<String,Map<KeyExtent,List<String>>> expected, Map<String,TabletServerMutations> actual) {
 +    assertEquals(expected.keySet(), actual.keySet());
 +    
 +    for (String server : actual.keySet()) {
 +      TabletServerMutations atb = actual.get(server);
 +      Map<KeyExtent,List<String>> etb = expected.get(server);
 +      
 +      assertEquals(etb.keySet(), atb.getMutations().keySet());
 +      
 +      for (KeyExtent ke : etb.keySet()) {
 +        ArrayList<String> eRows = new ArrayList<String>(etb.get(ke));
 +        ArrayList<String> aRows = new ArrayList<String>();
 +        
 +        for (Mutation m : atb.getMutations().get(ke)) {
 +          aRows.add(new String(m.getRow()));
 +        }
 +        
 +        Collections.sort(eRows);
 +        Collections.sort(aRows);
 +        
 +        assertEquals(eRows, aRows);
 +      }
 +    }
 +    
 +  }
 +  
 +  static Map<String,Map<KeyExtent,List<String>>> cemb(Object[]... ols) {
 +    
 +    Map<String,Map<KeyExtent,List<String>>> emb = new HashMap<String,Map<KeyExtent,List<String>>>();
 +    
 +    for (Object[] ol : ols) {
 +      String row = (String) ol[0];
 +      String server = (String) ol[1];
 +      KeyExtent ke = (KeyExtent) ol[2];
 +      
 +      Map<KeyExtent,List<String>> tb = emb.get(server);
 +      if (tb == null) {
 +        tb = new HashMap<KeyExtent,List<String>>();
 +        emb.put(server, tb);
 +      }
 +      
 +      List<String> rl = tb.get(ke);
 +      if (rl == null) {
 +        rl = new ArrayList<String>();
 +        tb.put(ke, rl);
 +      }
 +      
 +      rl.add(row);
 +    }
 +    
 +    return emb;
 +  }
 +  
 +  public void testRemoveOverlapping1() {
 +    TreeMap<Text,TabletLocation> mc = createMetaCache(nke("0", null, null), "l1");
 +    
 +    runTest(mc, nke("0", "a", null), nkes());
 +    runTest(mc, nke("0", null, null), nkes());
 +    runTest(mc, nke("0", null, "a"), nkes());
 +    
 +    mc = createMetaCache(nke("0", "g", null), "l1", nke("0", "r", "g"), "l1", nke("0", null, "r"), "l1");
 +    runTest(mc, nke("0", null, null), nkes());
 +    
 +    runTest(mc, nke("0", "a", null), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "g", null), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "h", null), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "r", null), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", null), nkes());
 +    
 +    runTest(mc, nke("0", "b", "a"), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "g", "a"), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "h", "a"), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "r", "a"), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", "a"), nkes());
 +    
 +    runTest(mc, nke("0", "h", "g"), nkes(nke("0", "g", null), nke("0", null, "r")));
 +    runTest(mc, nke("0", "r", "g"), nkes(nke("0", "g", null), nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", "g"), nkes(nke("0", "g", null)));
 +    
 +    runTest(mc, nke("0", "i", "h"), nkes(nke("0", "g", null), nke("0", null, "r")));
 +    runTest(mc, nke("0", "r", "h"), nkes(nke("0", "g", null), nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", "h"), nkes(nke("0", "g", null)));
 +    
 +    runTest(mc, nke("0", "z", "f"), nkes());
 +    runTest(mc, nke("0", "z", "g"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", "z", "q"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", "z", "r"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    runTest(mc, nke("0", "z", "s"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    
 +    runTest(mc, nke("0", null, "f"), nkes());
 +    runTest(mc, nke("0", null, "g"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", null, "q"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", null, "r"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    runTest(mc, nke("0", null, "s"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    
 +  }
 +  
 +  public void testRemoveOverlapping2() {
 +    
 +    // test removes when cache does not contain all tablets in a table
 +    TreeMap<Text,TabletLocation> mc = createMetaCache(nke("0", "r", "g"), "l1", nke("0", null, "r"), "l1");
 +    
 +    runTest(mc, nke("0", "a", null), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "g", null), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "h", null), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "r", null), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", null), nkes());
 +    
 +    runTest(mc, nke("0", "b", "a"), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "g", "a"), nkes(nke("0", "r", "g"), nke("0", null, "r")));
 +    runTest(mc, nke("0", "h", "a"), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "r", "a"), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", "a"), nkes());
 +    
 +    runTest(mc, nke("0", "h", "g"), nkes(nke("0", null, "r")));
 +    
 +    mc = createMetaCache(nke("0", "g", null), "l1", nke("0", null, "r"), "l1");
 +    
 +    runTest(mc, nke("0", "h", "g"), nkes(nke("0", "g", null), nke("0", null, "r")));
 +    runTest(mc, nke("0", "h", "a"), nkes(nke("0", null, "r")));
 +    runTest(mc, nke("0", "s", "g"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", "s", "a"), nkes());
 +    
 +    mc = createMetaCache(nke("0", "g", null), "l1", nke("0", "r", "g"), "l1");
 +    
 +    runTest(mc, nke("0", "z", "f"), nkes());
 +    runTest(mc, nke("0", "z", "g"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", "z", "q"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", "z", "r"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    runTest(mc, nke("0", "z", "s"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    
 +    runTest(mc, nke("0", null, "f"), nkes());
 +    runTest(mc, nke("0", null, "g"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", null, "q"), nkes(nke("0", "g", null)));
 +    runTest(mc, nke("0", null, "r"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +    runTest(mc, nke("0", null, "s"), nkes(nke("0", "g", null), nke("0", "r", "g")));
 +  }
 +  
 +  static class TestInstance implements Instance {
 +    
 +    private final String iid;
 +    private String rtl;
 +    
 +    public TestInstance(String iid, String rtl) {
 +      this.iid = iid;
 +      this.rtl = rtl;
 +    }
 +    
 +    @Override
 +    public String getInstanceID() {
 +      return iid;
 +    }
 +    
 +    @Override
 +    public String getInstanceName() {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    @Override
 +    public List<String> getMasterLocations() {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    @Override
 +    public String getRootTabletLocation() {
 +      return rtl;
 +    }
 +    
 +    @Override
 +    public String getZooKeepers() {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    @Override
 +    public int getZooKeepersSessionTimeOut() {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    public void setRootTabletLocation(String rtl) {
 +      this.rtl = rtl;
 +    }
 +    
 +    @Override
 +    @Deprecated
 +    public Connector getConnector(String user, byte[] pass) throws AccumuloException, AccumuloSecurityException {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    @Override
 +    @Deprecated
 +    public Connector getConnector(String user, ByteBuffer pass) throws AccumuloException, AccumuloSecurityException {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    AccumuloConfiguration conf = AccumuloConfiguration.getDefaultConfiguration();
 +    
 +    @Override
 +    public AccumuloConfiguration getConfiguration() {
 +      return conf;
 +    }
 +    
 +    @Override
 +    public void setConfiguration(AccumuloConfiguration conf) {
 +      this.conf = conf;
 +    }
 +    
 +    @Override
 +    @Deprecated
 +    public Connector getConnector(String user, CharSequence pass) throws AccumuloException, AccumuloSecurityException {
 +      throw new UnsupportedOperationException();
 +    }
 +    
 +    @Deprecated
 +    @Override
 +    public Connector getConnector(org.apache.accumulo.core.security.thrift.AuthInfo auth) throws AccumuloException, AccumuloSecurityException {
 +      return getConnector(auth.user, auth.getPassword());
 +    }
 +
 +    @Override
 +    public Connector getConnector(String principal, AuthenticationToken token) throws AccumuloException, AccumuloSecurityException {
 +      throw new UnsupportedOperationException();
 +    }
++    
++    @Override
++    public void close() throws AccumuloException {
++      // NOOP
++    }
 +  }
 +  
 +  static class TServers {
 +    private final Map<String,Map<KeyExtent,SortedMap<Key,Value>>> tservers = new HashMap<String,Map<KeyExtent,SortedMap<Key,Value>>>();
 +  }
 +  
 +  static class TestTabletLocationObtainer implements TabletLocationObtainer {
 +    
 +    private final Map<String,Map<KeyExtent,SortedMap<Key,Value>>> tservers;
 +    
 +    TestTabletLocationObtainer(TServers tservers) {
 +      this.tservers = tservers.tservers;
 +    }
 +    
 +    @Override
 +    public TabletLocations lookupTablet(TabletLocation src, Text row, Text stopRow, TabletLocator parent, TCredentials credentials) throws AccumuloSecurityException {
 +      
 +      // System.out.println("lookupTablet("+src+","+row+","+stopRow+","+ parent+")");
 +      // System.out.println(tservers);
 +      
 +      ArrayList<TabletLocation> list = new ArrayList<TabletLocation>();
 +      
 +      Map<KeyExtent,SortedMap<Key,Value>> tablets = tservers.get(src.tablet_location);
 +      
 +      if (tablets == null) {
 +        parent.invalidateCache(src.tablet_location);
 +        return null;
 +      }
 +      
 +      SortedMap<Key,Value> tabletData = tablets.get(src.tablet_extent);
 +      
 +      if (tabletData == null) {
 +        parent.invalidateCache(src.tablet_extent);
 +        return null;
 +      }
 +      
 +      // the following clip is done on a tablet, do it here to see if it throws exceptions
 +      src.tablet_extent.toDataRange().clip(new Range(row, true, stopRow, true));
 +      
 +      Key startKey = new Key(row);
 +      Key stopKey = new Key(stopRow).followingKey(PartialKey.ROW);
 +      
 +      SortedMap<Key,Value> results = tabletData.tailMap(startKey).headMap(stopKey);
 +      
 +      Pair<SortedMap<KeyExtent,Text>,List<KeyExtent>> metadata = MetadataTable.getMetadataLocationEntries(results);
 +      
 +      for (Entry<KeyExtent,Text> entry : metadata.getFirst().entrySet()) {
 +        list.add(new TabletLocation(entry.getKey(), entry.getValue().toString()));
 +      }
 +      
 +      return new TabletLocations(list, metadata.getSecond());
 +    }
 +    
 +    @Override
 +    public List<TabletLocation> lookupTablets(String tserver, Map<KeyExtent,List<Range>> map, TabletLocator parent, TCredentials credentials)
 +        throws AccumuloSecurityException {
 +      
 +      ArrayList<TabletLocation> list = new ArrayList<TabletLocation>();
 +      
 +      Map<KeyExtent,SortedMap<Key,Value>> tablets = tservers.get(tserver);
 +      
 +      if (tablets == null) {
 +        parent.invalidateCache(tserver);
 +        return list;
 +      }
 +      
 +      TreeMap<Key,Value> results = new TreeMap<Key,Value>();
 +      
 +      Set<Entry<KeyExtent,List<Range>>> es = map.entrySet();
 +      List<KeyExtent> failures = new ArrayList<KeyExtent>();
 +      for (Entry<KeyExtent,List<Range>> entry : es) {
 +        SortedMap<Key,Value> tabletData = tablets.get(entry.getKey());
 +        
 +        if (tabletData == null) {
 +          failures.add(entry.getKey());
 +          continue;
 +        }
 +        List<Range> ranges = entry.getValue();
 +        for (Range range : ranges) {
 +          SortedMap<Key,Value> tm;
 +          if (range.getStartKey() == null)
 +            tm = tabletData;
 +          else
 +            tm = tabletData.tailMap(range.getStartKey());
 +          
 +          for (Entry<Key,Value> de : tm.entrySet()) {
 +            if (range.afterEndKey(de.getKey())) {
 +              break;
 +            }
 +            
 +            if (range.contains(de.getKey())) {
 +              results.put(de.getKey(), de.getValue());
 +            }
 +          }
 +        }
 +      }
 +      
 +      if (failures.size() > 0)
 +        parent.invalidateCache(failures);
 +      
 +      SortedMap<KeyExtent,Text> metadata = MetadataTable.getMetadataLocationEntries(results).getFirst();
 +      
 +      for (Entry<KeyExtent,Text> entry : metadata.entrySet()) {
 +        list.add(new TabletLocation(entry.getKey(), entry.getValue().toString()));
 +      }
 +      
 +      return list;
 +      
 +    }
 +    
 +  }
 +  
 +  static void createEmptyTablet(TServers tservers, String server, KeyExtent tablet) {
 +    Map<KeyExtent,SortedMap<Key,Value>> tablets = tservers.tservers.get(server);
 +    if (tablets == null) {
 +      tablets = new HashMap<KeyExtent,SortedMap<Key,Value>>();
 +      tservers.tservers.put(server, tablets);
 +    }
 +    
 +    SortedMap<Key,Value> tabletData = tablets.get(tablet);
 +    if (tabletData == null) {
 +      tabletData = new TreeMap<Key,Value>();
 +      tablets.put(tablet, tabletData);
 +    } else if (tabletData.size() > 0) {
 +      throw new RuntimeException("Asked for empty tablet, but non empty tablet exists");
 +    }
 +  }
 +  
 +  static void setLocation(TServers tservers, String server, KeyExtent tablet, KeyExtent ke, String location, String instance) {
 +    Map<KeyExtent,SortedMap<Key,Value>> tablets = tservers.tservers.get(server);
 +    if (tablets == null) {
 +      tablets = new HashMap<KeyExtent,SortedMap<Key,Value>>();
 +      tservers.tservers.put(server, tablets);
 +    }
 +    
 +    SortedMap<Key,Value> tabletData = tablets.get(tablet);
 +    if (tabletData == null) {
 +      tabletData = new TreeMap<Key,Value>();
 +      tablets.put(tablet, tabletData);
 +    }
 +    
 +    Text mr = ke.getMetadataEntry();
 +    Value per = KeyExtent.encodePrevEndRow(ke.getPrevEndRow());
 +    
 +    if (location != null) {
 +      if (instance == null)
 +        instance = "";
 +      Key lk = new Key(mr, Constants.METADATA_CURRENT_LOCATION_COLUMN_FAMILY, new Text(instance));
 +      tabletData.put(lk, new Value(location.getBytes()));
 +    }
 +    
 +    Key pk = new Key(mr, Constants.METADATA_PREV_ROW_COLUMN.getColumnFamily(), Constants.METADATA_PREV_ROW_COLUMN.getColumnQualifier());
 +    tabletData.put(pk, per);
 +  }
 +  
 +  static void setLocation(TServers tservers, String server, KeyExtent tablet, KeyExtent ke, String location) {
 +    setLocation(tservers, server, tablet, ke, location, "");
 +  }
 +
 +  static void deleteServer(TServers tservers, String server) {
 +    tservers.tservers.remove(server);
 +    
 +  }
 +  
 +  private void locateTabletTest(TabletLocatorImpl cache, String row, boolean skipRow, KeyExtent expected, String server, TCredentials credentials)
 +      throws Exception {
 +    TabletLocation tl = cache.locateTablet(new Text(row), skipRow, false, credentials);
 +    
 +    if (expected == null) {
 +      if (tl != null)
 +        System.out.println("tl = " + tl);
 +      assertNull(tl);
 +    } else {
 +      assertNotNull(tl);
 +      assertEquals(server, tl.tablet_location);
 +      assertEquals(expected, tl.tablet_extent);
 +    }
 +  }
 +  
 +  private void locateTabletTest(TabletLocatorImpl cache, String row, KeyExtent expected, String server, TCredentials credentials) throws Exception {
 +    locateTabletTest(cache, row, false, expected, server, credentials);
 +  }
 +  
 +  public void test1() throws Exception {
 +    TServers tservers = new TServers();
 +    TestTabletLocationObtainer ttlo = new TestTabletLocationObtainer(tservers);
 +    TestInstance testInstance = new TestInstance("instance1", "tserver1");
 +    
 +    RootTabletLocator rtl = new RootTabletLocator(testInstance);
 +    TabletLocatorImpl rootTabletCache = new TabletLocatorImpl(new Text(Constants.METADATA_TABLE_ID), rtl, ttlo);
 +    TabletLocatorImpl tab1TabletCache = new TabletLocatorImpl(new Text("tab1"), rootTabletCache, ttlo);
 +    
 +    locateTabletTest(tab1TabletCache, "r1", null, null, credential);
 +    
 +    KeyExtent tab1e = nke("tab1", null, null);
 +    
 +    setLocation(tservers, "tserver1", RTE, MTE, "tserver2");
 +    setLocation(tservers, "tserver2", MTE, tab1e, "tserver3");
 +    
 +    locateTabletTest(tab1TabletCache, "r1", tab1e, "tserver3", credential);
 +    locateTabletTest(tab1TabletCache, "r2", tab1e, "tserver3", credential);
 +    
 +    // simulate a split
 +    KeyExtent tab1e1 = nke("tab1", "g", null);
 +    KeyExtent tab1e2 = nke("tab1", null, "g");
 +    
 +    setLocation(tservers, "tserver2", MTE, tab1e1, "tserver4");
 +    setLocation(tservers, "tserver2", MTE, tab1e2, "tserver5");
 +    
 +    locateTabletTest(tab1TabletCache, "r1", tab1e, "tserver3", credential);
 +    tab1TabletCache.invalidateCache(tab1e);
 +    locateTabletTest(tab1TabletCache, "r1", tab1e2, "tserver5", credential);
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver4", credential);
 +    locateTabletTest(tab1TabletCache, "a", true, tab1e1, "tserver4", credential);
 +    locateTabletTest(tab1TabletCache, "g", tab1e1, "tserver4", credential);
 +    locateTabletTest(tab1TabletCache, "g", true, tab1e2, "tserver5", credential);
 +    
 +    // simulate a partial split
 +    KeyExtent tab1e22 = nke("tab1", null, "m");
 +    setLocation(tservers, "tserver2", MTE, tab1e22, "tserver6");
 +    locateTabletTest(tab1TabletCache, "r1", tab1e2, "tserver5", credential);
 +    tab1TabletCache.invalidateCache(tab1e2);
 +    locateTabletTest(tab1TabletCache, "r1", tab1e22, "tserver6", credential);
 +    locateTabletTest(tab1TabletCache, "h", null, null, credential);
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver4", credential);
 +    KeyExtent tab1e21 = nke("tab1", "m", "g");
 +    setLocation(tservers, "tserver2", MTE, tab1e21, "tserver7");
 +    locateTabletTest(tab1TabletCache, "r1", tab1e22, "tserver6", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver7", credential);
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver4", credential);
 +    
 +    // simulate a migration
 +    setLocation(tservers, "tserver2", MTE, tab1e21, "tserver8");
 +    tab1TabletCache.invalidateCache(tab1e21);
 +    locateTabletTest(tab1TabletCache, "r1", tab1e22, "tserver6", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver8", credential);
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver4", credential);
 +    
 +    // simulate a server failure
 +    setLocation(tservers, "tserver2", MTE, tab1e21, "tserver9");
 +    tab1TabletCache.invalidateCache("tserver8");
 +    locateTabletTest(tab1TabletCache, "r1", tab1e22, "tserver6", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver9", credential);
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver4", credential);
 +    
 +    // simulate all servers failing
 +    deleteServer(tservers, "tserver1");
 +    deleteServer(tservers, "tserver2");
 +    tab1TabletCache.invalidateCache("tserver4");
 +    tab1TabletCache.invalidateCache("tserver6");
 +    tab1TabletCache.invalidateCache("tserver9");
 +    
 +    locateTabletTest(tab1TabletCache, "r1", null, null, credential);
 +    locateTabletTest(tab1TabletCache, "h", null, null, credential);
 +    locateTabletTest(tab1TabletCache, "a", null, null, credential);
 +    
 +    testInstance.setRootTabletLocation("tserver4");
 +    setLocation(tservers, "tserver4", RTE, MTE, "tserver5");
 +    setLocation(tservers, "tserver5", MTE, tab1e1, "tserver1");
 +    setLocation(tservers, "tserver5", MTE, tab1e21, "tserver2");
 +    setLocation(tservers, "tserver5", MTE, tab1e22, "tserver3");
 +    
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver1", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver2", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver3", credential);
 +    
 +    // simulate the !METADATA table splitting
 +    KeyExtent mte1 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), tab1e21.getMetadataEntry(), RTE.getEndRow());
 +    KeyExtent mte2 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), null, tab1e21.getMetadataEntry());
 +    
 +    setLocation(tservers, "tserver4", RTE, mte1, "tserver5");
 +    setLocation(tservers, "tserver4", RTE, mte2, "tserver6");
 +    deleteServer(tservers, "tserver5");
 +    setLocation(tservers, "tserver5", mte1, tab1e1, "tserver7");
 +    setLocation(tservers, "tserver5", mte1, tab1e21, "tserver8");
 +    setLocation(tservers, "tserver6", mte2, tab1e22, "tserver9");
 +    
 +    tab1TabletCache.invalidateCache(tab1e1);
 +    tab1TabletCache.invalidateCache(tab1e21);
 +    tab1TabletCache.invalidateCache(tab1e22);
 +    
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver7", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver8", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver9", credential);
 +    
 +    // simulate metadata and regular server down and the reassigned
 +    deleteServer(tservers, "tserver5");
 +    tab1TabletCache.invalidateCache("tserver7");
 +    locateTabletTest(tab1TabletCache, "a", null, null, credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver8", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver9", credential);
 +    
 +    setLocation(tservers, "tserver4", RTE, mte1, "tserver10");
 +    setLocation(tservers, "tserver10", mte1, tab1e1, "tserver7");
 +    setLocation(tservers, "tserver10", mte1, tab1e21, "tserver8");
 +    
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver7", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver8", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver9", credential);
 +    tab1TabletCache.invalidateCache("tserver7");
 +    setLocation(tservers, "tserver10", mte1, tab1e1, "tserver2");
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver2", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver8", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver9", credential);
 +    
 +    // simulate a hole in the !METADATA table, caused by a partial split
 +    KeyExtent mte11 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), tab1e1.getMetadataEntry(), RTE.getEndRow());
 +    KeyExtent mte12 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), tab1e21.getMetadataEntry(), tab1e1.getMetadataEntry());
 +    deleteServer(tservers, "tserver10");
 +    setLocation(tservers, "tserver4", RTE, mte12, "tserver10");
 +    setLocation(tservers, "tserver10", mte12, tab1e21, "tserver12");
 +    
 +    // at this point should be no info in !METADATA about tab1e1
 +    tab1TabletCache.invalidateCache(tab1e1);
 +    tab1TabletCache.invalidateCache(tab1e21);
 +    locateTabletTest(tab1TabletCache, "a", null, null, credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver12", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver9", credential);
 +    
 +    setLocation(tservers, "tserver4", RTE, mte11, "tserver5");
 +    setLocation(tservers, "tserver5", mte11, tab1e1, "tserver13");
 +    
 +    locateTabletTest(tab1TabletCache, "a", tab1e1, "tserver13", credential);
 +    locateTabletTest(tab1TabletCache, "h", tab1e21, "tserver12", credential);
 +    locateTabletTest(tab1TabletCache, "r", tab1e22, "tserver9", credential);
 +  }
 +  
 +  public void test2() throws Exception {
 +    TServers tservers = new TServers();
 +    TabletLocatorImpl metaCache = createLocators(tservers, "tserver1", "tserver2", "foo");
 +    
 +    KeyExtent ke1 = nke("foo", "m", null);
 +    KeyExtent ke2 = nke("foo", null, "m");
 +    
 +    setLocation(tservers, "tserver2", MTE, ke1, null);
 +    setLocation(tservers, "tserver2", MTE, ke2, "L1");
 +    
 +    locateTabletTest(metaCache, "a", null, null, credential);
 +    locateTabletTest(metaCache, "r", ke2, "L1", credential);
 +    
 +    setLocation(tservers, "tserver2", MTE, ke1, "L2");
 +    
 +    locateTabletTest(metaCache, "a", ke1, "L2", credential);
 +    locateTabletTest(metaCache, "r", ke2, "L1", credential);
 +  }
 +  
 +  public void testBinRanges1() throws Exception {
 +    Text tableName = new Text("foo");
 +    
 +    TabletLocatorImpl metaCache = createLocators("foo", nke("foo", null, null), "l1");
 +    
 +    List<Range> ranges = nrl(nr(null, null));
 +    Map<String,Map<KeyExtent,List<Range>>> expected = createExpectedBinnings("l1", nol(nke("foo", null, null), nrl(nr(null, null)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    ranges = nrl(nr("a", null));
 +    expected = createExpectedBinnings("l1", nol(nke("foo", null, null), nrl(nr("a", null)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    ranges = nrl(nr(null, "b"));
 +    expected = createExpectedBinnings("l1", nol(nke("foo", null, null), nrl(nr(null, "b")))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +  }
 +  
 +  public void testBinRanges2() throws Exception {
 +    
 +    Text tableName = new Text("foo");
 +    
 +    List<Range> ranges = nrl(nr(null, null));
 +    TabletLocatorImpl metaCache = createLocators("foo", nke("foo", "g", null), "l1", nke("foo", null, "g"), "l2");
 +    
 +    Map<String,Map<KeyExtent,List<Range>>> expected = createExpectedBinnings("l1", nol(nke("foo", "g", null), nrl(nr(null, null))), "l2",
 +        nol(nke("foo", null, "g"), nrl(nr(null, null)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +  }
 +  
 +  public void testBinRanges3() throws Exception {
 +    
 +    Text tableName = new Text("foo");
 +    
 +    // test with three tablets and a range that covers the whole table
 +    List<Range> ranges = nrl(nr(null, null));
 +    TabletLocatorImpl metaCache = createLocators("foo", nke("foo", "g", null), "l1", nke("foo", "m", "g"), "l2", nke("foo", null, "m"), "l2");
 +    
 +    Map<String,Map<KeyExtent,List<Range>>> expected = createExpectedBinnings("l1", nol(nke("foo", "g", null), nrl(nr(null, null))), "l2",
 +        nol(nke("foo", "m", "g"), nrl(nr(null, null)), nke("foo", null, "m"), nrl(nr(null, null)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    // test with three tablets where one range falls within the first tablet and last two ranges fall within the last tablet
 +    ranges = nrl(nr(null, "c"), nr("s", "y"), nr("z", null));
 +    expected = createExpectedBinnings("l1", nol(nke("foo", "g", null), nrl(nr(null, "c"))), "l2", nol(nke("foo", null, "m"), nrl(nr("s", "y"), nr("z", null)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    // test is same as above, but has an additional range that spans the first two tablets
 +    ranges = nrl(nr(null, "c"), nr("f", "i"), nr("s", "y"), nr("z", null));
 +    expected = createExpectedBinnings("l1", nol(nke("foo", "g", null), nrl(nr(null, "c"), nr("f", "i"))), "l2",
 +        nol(nke("foo", "m", "g"), nrl(nr("f", "i")), nke("foo", null, "m"), nrl(nr("s", "y"), nr("z", null)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    // test where start of range is not inclusive and same as tablet endrow
 +    ranges = nrl(nr("g", false, "m", true));
 +    expected = createExpectedBinnings("l2", nol(nke("foo", "m", "g"), nrl(nr("g", false, "m", true)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    // test where start of range is inclusive and same as tablet endrow
 +    ranges = nrl(nr("g", true, "m", true));
 +    expected = createExpectedBinnings("l1", nol(nke("foo", "g", null), nrl(nr("g", true, "m", true))), "l2",
 +        nol(nke("foo", "m", "g"), nrl(nr("g", true, "m", true)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    ranges = nrl(nr("g", true, "m", false));
 +    expected = createExpectedBinnings("l1", nol(nke("foo", "g", null), nrl(nr("g", true, "m", false))), "l2",
 +        nol(nke("foo", "m", "g"), nrl(nr("g", true, "m", false)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    ranges = nrl(nr("g", false, "m", false));
 +    expected = createExpectedBinnings("l2", nol(nke("foo", "m", "g"), nrl(nr("g", false, "m", false)))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +  }
 +  
 +  public void testBinRanges4() throws Exception {
 +    Text tableName = new Text("foo");
 +    
 +    List<Range> ranges = nrl(new Range(new Text("1")));
 +    TabletLocatorImpl metaCache = createLocators("foo", nke("foo", "0", null), "l1", nke("foo", "1", "0"), "l2", nke("foo", "2", "1"), "l3",
 +        nke("foo", "3", "2"), "l4", nke("foo", null, "3"), "l5");
 +    
 +    Map<String,Map<KeyExtent,List<Range>>> expected = createExpectedBinnings("l2", nol(nke("foo", "1", "0"), nrl(new Range(new Text("1"))))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected);
 +    
 +    Key rowColKey = new Key(new Text("3"), new Text("cf1"), new Text("cq1"));
 +    Range range = new Range(rowColKey, true, new Key(new Text("3")).followingKey(PartialKey.ROW), false);
 +    
 +    ranges = nrl(range);
 +    Map<String,Map<KeyExtent,List<Range>>> expected4 = createExpectedBinnings("l4", nol(nke("foo", "3", "2"), nrl(range))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected4, nrl());
 +    
 +    range = new Range(rowColKey, true, new Key(new Text("3")).followingKey(PartialKey.ROW), true);
 +    
 +    ranges = nrl(range);
 +    Map<String,Map<KeyExtent,List<Range>>> expected5 = createExpectedBinnings("l4", nol(nke("foo", "3", "2"), nrl(range)), "l5",
 +        nol(nke("foo", null, "3"), nrl(range))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected5, nrl());
 +    
 +    range = new Range(new Text("2"), false, new Text("3"), false);
 +    ranges = nrl(range);
 +    Map<String,Map<KeyExtent,List<Range>>> expected6 = createExpectedBinnings("l4", nol(nke("foo", "3", "2"), nrl(range))
 +    
 +    );
 +    runTest(tableName, ranges, metaCache, expected6, nrl());
 +    
 +    range = new Range(new Text("2"), true, new Text("3"), false);
 +    ranges = nrl(range);
 +    Map<String,Map<KeyExtent,List<Range>>> expected7 = createExpectedBinnings("l3", nol(nke("foo", "2", "1"), nrl(range)), "l4",
 +        nol(nke("foo", "3", "2"), nrl(range))
 +    
 +    );
 +    runTest(tableName, ranges, metaCache, expected7, nrl());
 +    
 +    range = new Range(new Text("2"), false, new Text("3"), true);
 +    ranges = nrl(range);
 +    Map<String,Map<KeyExtent,List<Range>>> expected8 = createExpectedBinnings("l4", nol(nke("foo", "3", "2"), nrl(range))
 +    
 +    );
 +    runTest(tableName, ranges, metaCache, expected8, nrl());
 +    
 +    range = new Range(new Text("2"), true, new Text("3"), true);
 +    ranges = nrl(range);
 +    Map<String,Map<KeyExtent,List<Range>>> expected9 = createExpectedBinnings("l3", nol(nke("foo", "2", "1"), nrl(range)), "l4",
 +        nol(nke("foo", "3", "2"), nrl(range))
 +    
 +    );
 +    runTest(tableName, ranges, metaCache, expected9, nrl());
 +    
 +  }
 +  
 +  public void testBinRanges5() throws Exception {
 +    // Test binning when there is a hole in the !METADATA information
 +    Text tableName = new Text("foo");
 +    
 +    List<Range> ranges = nrl(new Range(new Text("1")));
 +    TabletLocatorImpl metaCache = createLocators("foo", nke("foo", "0", null), "l1", nke("foo", "1", "0"), "l2", nke("foo", "3", "2"), "l4",
 +        nke("foo", null, "3"), "l5");
 +    
 +    Map<String,Map<KeyExtent,List<Range>>> expected1 = createExpectedBinnings("l2", nol(nke("foo", "1", "0"), nrl(new Range(new Text("1"))))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected1);
 +    
 +    ranges = nrl(new Range(new Text("2")), new Range(new Text("11")));
 +    Map<String,Map<KeyExtent,List<Range>>> expected2 = createExpectedBinnings();
 +    
 +    runTest(tableName, ranges, metaCache, expected2, ranges);
 +    
 +    ranges = nrl(new Range(new Text("1")), new Range(new Text("2")));
 +    
 +    runTest(tableName, ranges, metaCache, expected1, nrl(new Range(new Text("2"))));
 +    
 +    ranges = nrl(nr("0", "2"), nr("3", "4"));
 +    Map<String,Map<KeyExtent,List<Range>>> expected3 = createExpectedBinnings("l4", nol(nke("foo", "3", "2"), nrl(nr("3", "4"))), "l5",
 +        nol(nke("foo", null, "3"), nrl(nr("3", "4")))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected3, nrl(nr("0", "2")));
 +    
 +    ranges = nrl(nr("0", "1"), nr("0", "11"), nr("1", "2"), nr("0", "4"), nr("2", "4"), nr("21", "4"));
 +    Map<String,Map<KeyExtent,List<Range>>> expected4 = createExpectedBinnings("l1", nol(nke("foo", "0", null), nrl(nr("0", "1"))), "l2",
 +        nol(nke("foo", "1", "0"), nrl(nr("0", "1"))), "l4", nol(nke("foo", "3", "2"), nrl(nr("21", "4"))), "l5", nol(nke("foo", null, "3"), nrl(nr("21", "4")))
 +    
 +    );
 +    
 +    runTest(tableName, ranges, metaCache, expected4, nrl(nr("0", "11"), nr("1", "2"), nr("0", "4"), nr("2", "4")));
 +  }
 +  
 +  public void testBinMutations1() throws Exception {
 +    // one tablet table
 +    KeyExtent ke1 = nke("foo", null, null);
 +    TabletLocatorImpl metaCache = createLocators("foo", ke1, "l1");
 +    
 +    List<Mutation> ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("c", "cf1:cq1=v3", "cf1:cq2=v4"));
 +    Map<String,Map<KeyExtent,List<String>>> emb = cemb(nol("a", "l1", ke1), nol("c", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"));
 +    emb = cemb(nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("a", "cf1:cq3=v3"));
 +    emb = cemb(nol("a", "l1", ke1), nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +  }
 +  
 +  public void testBinMutations2() throws Exception {
 +    // no tablets for table
 +    TabletLocatorImpl metaCache = createLocators("foo");
 +    
 +    List<Mutation> ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("c", "cf1:cq1=v3", "cf1:cq2=v4"));
 +    Map<String,Map<KeyExtent,List<String>>> emb = cemb();
 +    runTest(metaCache, ml, emb, "a", "c");
 +  }
 +  
 +  public void testBinMutations3() throws Exception {
 +    // three tablet table
 +    KeyExtent ke1 = nke("foo", "h", null);
 +    KeyExtent ke2 = nke("foo", "t", "h");
 +    KeyExtent ke3 = nke("foo", null, "t");
 +    
 +    TabletLocatorImpl metaCache = createLocators("foo", ke1, "l1", ke2, "l2", ke3, "l3");
 +    
 +    List<Mutation> ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("i", "cf1:cq1=v3", "cf1:cq2=v4"));
 +    Map<String,Map<KeyExtent,List<String>>> emb = cemb(nol("a", "l1", ke1), nol("i", "l2", ke2));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"));
 +    emb = cemb(nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("a", "cf1:cq3=v3"));
 +    emb = cemb(nol("a", "l1", ke1), nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("w", "cf1:cq3=v3"));
 +    emb = cemb(nol("a", "l1", ke1), nol("w", "l3", ke3));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("w", "cf1:cq3=v3"), nm("z", "cf1:cq4=v4"));
 +    emb = cemb(nol("a", "l1", ke1), nol("w", "l3", ke3), nol("z", "l3", ke3));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("h", "cf1:cq1=v1", "cf1:cq2=v2"), nm("t", "cf1:cq1=v1", "cf1:cq2=v2"));
 +    emb = cemb(nol("h", "l1", ke1), nol("t", "l2", ke2));
 +    runTest(metaCache, ml, emb);
 +  }
 +  
 +  public void testBinMutations4() throws Exception {
 +    // three table with hole
 +    KeyExtent ke1 = nke("foo", "h", null);
 +    
 +    KeyExtent ke3 = nke("foo", null, "t");
 +    
 +    TabletLocatorImpl metaCache = createLocators("foo", ke1, "l1", ke3, "l3");
 +    
 +    List<Mutation> ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("i", "cf1:cq1=v3", "cf1:cq2=v4"));
 +    Map<String,Map<KeyExtent,List<String>>> emb = cemb(nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb, "i");
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"));
 +    emb = cemb(nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("a", "cf1:cq3=v3"));
 +    emb = cemb(nol("a", "l1", ke1), nol("a", "l1", ke1));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("w", "cf1:cq3=v3"));
 +    emb = cemb(nol("a", "l1", ke1), nol("w", "l3", ke3));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("w", "cf1:cq3=v3"), nm("z", "cf1:cq4=v4"));
 +    emb = cemb(nol("a", "l1", ke1), nol("w", "l3", ke3), nol("z", "l3", ke3));
 +    runTest(metaCache, ml, emb);
 +    
 +    ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("w", "cf1:cq3=v3"), nm("z", "cf1:cq4=v4"), nm("t", "cf1:cq5=v5"));
 +    emb = cemb(nol("a", "l1", ke1), nol("w", "l3", ke3), nol("z", "l3", ke3));
 +    runTest(metaCache, ml, emb, "t");
 +  }
 +  
 +  public void testBinSplit() throws Exception {
 +    // try binning mutations and ranges when a tablet splits
 +    
 +    for (int i = 0; i < 3; i++) {
 +      // when i == 0 only test binning mutations
 +      // when i == 1 only test binning ranges
 +      // when i == 2 test both
 +      
 +      KeyExtent ke1 = nke("foo", null, null);
 +      TServers tservers = new TServers();
 +      TabletLocatorImpl metaCache = createLocators(tservers, "tserver1", "tserver2", "foo", ke1, "l1");
 +      
 +      List<Mutation> ml = nml(nm("a", "cf1:cq1=v1", "cf1:cq2=v2"), nm("m", "cf1:cq1=v3", "cf1:cq2=v4"), nm("z", "cf1:cq1=v5"));
 +      Map<String,Map<KeyExtent,List<String>>> emb = cemb(nol("a", "l1", ke1), nol("m", "l1", ke1), nol("z", "l1", ke1));
 +      if (i == 0 || i == 2)
 +        runTest(metaCache, ml, emb);
 +      
 +      List<Range> ranges = nrl(new Range(new Text("a")), new Range(new Text("m")), new Range(new Text("z")));
 +      
 +      Map<String,Map<KeyExtent,List<Range>>> expected1 = createExpectedBinnings("l1", nol(nke("foo", null, null), ranges)
 +      
 +      );
 +      
 +      if (i == 1 || i == 2)
 +        runTest(new Text("foo"), ranges, metaCache, expected1);
 +      
 +      KeyExtent ke11 = nke("foo", "n", null);
 +      KeyExtent ke12 = nke("foo", null, "n");
 +      
 +      setLocation(tservers, "tserver2", MTE, ke12, "l2");
 +      
 +      metaCache.invalidateCache(ke1);
 +      
 +      emb = cemb(nol("z", "l2", ke12));
 +      if (i == 0 || i == 2)
 +        runTest(metaCache, ml, emb, "a", "m");
 +      
 +      Map<String,Map<KeyExtent,List<Range>>> expected2 = createExpectedBinnings("l2", nol(nke("foo", null, "n"), nrl(new Range(new Text("z"))))
 +      
 +      );
 +      
 +      if (i == 1 || i == 2)
 +        runTest(new Text("foo"), ranges, metaCache, expected2, nrl(new Range(new Text("a")), new Range(new Text("m"))));
 +      
 +      setLocation(tservers, "tserver2", MTE, ke11, "l3");
 +      emb = cemb(nol("a", "l3", ke11), nol("m", "l3", ke11), nol("z", "l2", ke12));
 +      if (i == 0 || i == 2)
 +        runTest(metaCache, ml, emb);
 +      
 +      Map<String,Map<KeyExtent,List<Range>>> expected3 = createExpectedBinnings("l2", nol(nke("foo", null, "n"), nrl(new Range(new Text("z")))), "l3",
 +          nol(nke("foo", "n", null), nrl(new Range(new Text("a")), new Range(new Text("m"))))
 +      
 +      );
 +      
 +      if (i == 1 || i == 2)
 +        runTest(new Text("foo"), ranges, metaCache, expected3);
 +    }
 +  }
 +  
 +  public void testBug1() throws Exception {
 +    // a bug that occurred while running continuous ingest
 +    KeyExtent mte1 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), new Text("0;0bc"), RTE.getEndRow());
 +    KeyExtent mte2 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), null, new Text("0;0bc"));
 +    
 +    TServers tservers = new TServers();
 +    TestTabletLocationObtainer ttlo = new TestTabletLocationObtainer(tservers);
 +    TestInstance testInstance = new TestInstance("instance1", "tserver1");
 +    
 +    RootTabletLocator rtl = new RootTabletLocator(testInstance);
 +    TabletLocatorImpl rootTabletCache = new TabletLocatorImpl(new Text(Constants.METADATA_TABLE_ID), rtl, ttlo);
 +    TabletLocatorImpl tab0TabletCache = new TabletLocatorImpl(new Text("0"), rootTabletCache, ttlo);
 +    
 +    setLocation(tservers, "tserver1", RTE, mte1, "tserver2");
 +    setLocation(tservers, "tserver1", RTE, mte2, "tserver3");
 +    
 +    // create two tablets that straddle a !METADATA split point
 +    KeyExtent ke1 = new KeyExtent(new Text("0"), new Text("0bbf20e"), null);
 +    KeyExtent ke2 = new KeyExtent(new Text("0"), new Text("0bc0756"), new Text("0bbf20e"));
 +    
 +    setLocation(tservers, "tserver2", mte1, ke1, "tserver4");
 +    setLocation(tservers, "tserver3", mte2, ke2, "tserver5");
 +    
 +    // look up something that comes after the last entry in mte1
 +    locateTabletTest(tab0TabletCache, "0bbff", ke2, "tserver5", credential);
 +  }
 +  
 +  public void testBug2() throws Exception {
 +    // a bug that occurred while running a functional test
 +    KeyExtent mte1 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), new Text("~"), RTE.getEndRow());
 +    KeyExtent mte2 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), null, new Text("~"));
 +    
 +    TServers tservers = new TServers();
 +    TestTabletLocationObtainer ttlo = new TestTabletLocationObtainer(tservers);
 +    TestInstance testInstance = new TestInstance("instance1", "tserver1");
 +    
 +    RootTabletLocator rtl = new RootTabletLocator(testInstance);
 +    TabletLocatorImpl rootTabletCache = new TabletLocatorImpl(new Text(Constants.METADATA_TABLE_ID), rtl, ttlo);
 +    TabletLocatorImpl tab0TabletCache = new TabletLocatorImpl(new Text("0"), rootTabletCache, ttlo);
 +    
 +    setLocation(tservers, "tserver1", RTE, mte1, "tserver2");
 +    setLocation(tservers, "tserver1", RTE, mte2, "tserver3");
 +    
 +    // create the ~ tablet so it exists
 +    Map<KeyExtent,SortedMap<Key,Value>> ts3 = new HashMap<KeyExtent,SortedMap<Key,Value>>();
 +    ts3.put(mte2, new TreeMap<Key,Value>());
 +    tservers.tservers.put("tserver3", ts3);
 +    
 +    assertNull(tab0TabletCache.locateTablet(new Text("row_0000000000"), false, false, credential));
 +    
 +  }
 +  
 +  // this test reproduces a problem where empty metadata tablets, that were created by user tablets being merged away, caused locating tablets to fail
 +  public void testBug3() throws Exception {
 +    KeyExtent mte1 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), new Text("1;c"), RTE.getEndRow());
 +    KeyExtent mte2 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), new Text("1;f"), new Text("1;c"));
 +    KeyExtent mte3 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), new Text("1;j"), new Text("1;f"));
 +    KeyExtent mte4 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), new Text("1;r"), new Text("1;j"));
 +    KeyExtent mte5 = new KeyExtent(new Text(Constants.METADATA_TABLE_ID), null, new Text("1;r"));
 +    
 +    KeyExtent ke1 = new KeyExtent(new Text("1"), null, null);
 +    
 +    TServers tservers = new TServers();
 +    TestTabletLocationObtainer ttlo = new TestTabletLocationObtainer(tservers);
 +    TestInstance testInstance = new TestInstance("instance1", "tserver1");
 +    
 +    RootTabletLocator rtl = new RootTabletLocator(testInstance);
 +    
 +    TabletLocatorImpl rootTabletCache = new TabletLocatorImpl(new Text(Constants.METADATA_TABLE_ID), rtl, ttlo);
 +    TabletLocatorImpl tab0TabletCache = new TabletLocatorImpl(new Text("1"), rootTabletCache, ttlo);
 +    
 +    setLocation(tservers, "tserver1", RTE, mte1, "tserver2");
 +    setLocation(tservers, "tserver1", RTE, mte2, "tserver3");
 +    setLocation(tservers, "tserver1", RTE, mte3, "tserver4");
 +    setLocation(tservers, "tserver1", RTE, mte4, "tserver5");
 +    setLocation(tservers, "tserver1", RTE, mte5, "tserver6");
 +    
 +    createEmptyTablet(tservers, "tserver2", mte1);
 +    createEmptyTablet(tservers, "tserver3", mte2);
 +    createEmptyTablet(tservers, "tserver4", mte3);
 +    createEmptyTablet(tservers, "tserver5", mte4);
 +    setLocation(tservers, "tserver6", mte5, ke1, "tserver7");
 +    
 +    locateTabletTest(tab0TabletCache, "a", ke1, "tserver7", credential);
 +    
 +  }
 +  
 +  public void testAccumulo1248() throws Exception {
 +    TServers tservers = new TServers();
 +    TabletLocatorImpl metaCache = createLocators(tservers, "tserver1", "tserver2", "foo");
 +    
 +    KeyExtent ke1 = nke("foo", null, null);
 +    
 +    // set two locations for a tablet, this is not supposed to happen. The metadata cache should throw an exception if it sees this rather than caching one of
 +    // the locations.
 +    setLocation(tservers, "tserver2", MTE, ke1, "L1", "I1");
 +    setLocation(tservers, "tserver2", MTE, ke1, "L2", "I2");
 +    
 +    try {
 +      metaCache.locateTablet(new Text("a"), false, false, credential);
 +      assertTrue(false);
 +    } catch (Exception e) {
 +      
 +    }
 +
 +
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/accumulo/blob/04f81b50/examples/simple/src/main/java/org/apache/accumulo/examples/simple/client/RandomBatchWriter.java
----------------------------------------------------------------------
diff --cc examples/simple/src/main/java/org/apache/accumulo/examples/simple/client/RandomBatchWriter.java
index 886c4ba,0000000..d78ae0e
mode 100644,000000..100644
--- a/examples/simple/src/main/java/org/apache/accumulo/examples/simple/client/RandomBatchWriter.java
+++ b/examples/simple/src/main/java/org/apache/accumulo/examples/simple/client/RandomBatchWriter.java
@@@ -1,169 -1,0 +1,171 @@@
 +/*
 + * 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.examples.simple.client;
 +
 +import java.util.HashMap;
 +import java.util.HashSet;
 +import java.util.Map.Entry;
 +import java.util.Random;
 +import java.util.Set;
 +
 +import org.apache.accumulo.core.cli.BatchWriterOpts;
 +import org.apache.accumulo.core.cli.ClientOnRequiredTable;
 +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.Connector;
 +import org.apache.accumulo.core.client.MutationsRejectedException;
 +import org.apache.accumulo.core.client.TableNotFoundException;
 +import org.apache.accumulo.core.client.security.SecurityErrorCode;
 +import org.apache.accumulo.core.data.KeyExtent;
 +import org.apache.accumulo.core.data.Mutation;
 +import org.apache.accumulo.core.data.Value;
 +import org.apache.accumulo.core.security.ColumnVisibility;
 +import org.apache.hadoop.io.Text;
 +
 +import com.beust.jcommander.Parameter;
 +
 +/**
 + * Simple example for writing random data to Accumulo. See docs/examples/README.batch for instructions.
 + * 
 + * The rows of the entries will be randomly generated numbers between a specified min and max (prefixed by "row_"). The column families will be "foo" and column
 + * qualifiers will be "1". The values will be random byte arrays of a specified size.
 + */
 +public class RandomBatchWriter {
 +  
 +  /**
 +   * Creates a random byte array of specified size using the specified seed.
 +   * 
 +   * @param rowid
 +   *          the seed to use for the random number generator
 +   * @param dataSize
 +   *          the size of the array
 +   * @return a random byte array
 +   */
 +  public static byte[] createValue(long rowid, int dataSize) {
 +    Random r = new Random(rowid);
 +    byte value[] = new byte[dataSize];
 +    
 +    r.nextBytes(value);
 +    
 +    // transform to printable chars
 +    for (int j = 0; j < value.length; j++) {
 +      value[j] = (byte) (((0xff & value[j]) % 92) + ' ');
 +    }
 +    
 +    return value;
 +  }
 +  
 +  /**
 +   * Creates a mutation on a specified row with column family "foo", column qualifier "1", specified visibility, and a random value of specified size.
 +   * 
 +   * @param rowid
 +   *          the row of the mutation
 +   * @param dataSize
 +   *          the size of the random value
 +   * @param visibility
 +   *          the visibility of the entry to insert
 +   * @return a mutation
 +   */
 +  public static Mutation createMutation(long rowid, int dataSize, ColumnVisibility visibility) {
 +    Text row = new Text(String.format("row_%010d", rowid));
 +    
 +    Mutation m = new Mutation(row);
 +    
 +    // create a random value that is a function of the
 +    // row id for verification purposes
 +    byte value[] = createValue(rowid, dataSize);
 +    
 +    m.put(new Text("foo"), new Text("1"), visibility, new Value(value));
 +    
 +    return m;
 +  }
 +  
 +  static class Opts extends ClientOnRequiredTable {
 +    @Parameter(names="--num", required=true)
 +    int num = 0;
 +    @Parameter(names="--min")
 +    long min = 0;
 +    @Parameter(names="--max")
 +    long max = Long.MAX_VALUE;
 +    @Parameter(names="--size", required=true, description="size of the value to write")
 +    int size = 0;
 +    @Parameter(names="--vis", converter=VisibilityConverter.class)
 +    ColumnVisibility visiblity = new ColumnVisibility("");
 +    @Parameter(names="--seed", description="seed for pseudo-random number generator")
 +    Long seed = null;
 +  }
 + 
 +  /**
 +   * Writes a specified number of entries to Accumulo using a {@link BatchWriter}.
 +   * 
 +   * @throws AccumuloException
 +   * @throws AccumuloSecurityException
 +   * @throws TableNotFoundException
 +   */
 +  public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
 +    Opts opts = new Opts();
 +    BatchWriterOpts bwOpts = new BatchWriterOpts();
 +    opts.parseArgs(RandomBatchWriter.class.getName(), args, bwOpts);
++
++    if ((opts.max - opts.min) < opts.num) {
++      System.err.println(String.format("You must specify a min and a max that allow for at least num possible values. For example, you requested %d rows, but a min of %d and a max of %d only allows for %d rows.", opts.num, opts.min, opts.max, (opts.max - opts.min)));
++      System.exit(1);
++    }
 +    
 +    Random r;
 +    if (opts.seed == null)
 +      r = new Random();
 +    else {
 +      r = new Random(opts.seed);
 +    }
 +    
 +    Connector connector = opts.getConnector();
 +    BatchWriter bw = connector.createBatchWriter(opts.tableName, bwOpts.getBatchWriterConfig());
 +    
 +    // reuse the ColumnVisibility object to improve performance
 +    ColumnVisibility cv = opts.visiblity;
 +    
-     for (int i = 0; i < opts.num; i++) {
-       
++    for (int i = 0; i < opts.num; i++) {  
 +      long rowid = (Math.abs(r.nextLong()) % (opts.max - opts.min)) + opts.min;
-       
 +      Mutation m = createMutation(rowid, opts.size, cv);
-       
 +      bw.addMutation(m);
-       
 +    }
 +    
 +    try {
 +      bw.close();
 +    } catch (MutationsRejectedException e) {
 +      if (e.getAuthorizationFailuresMap().size() > 0) {
 +        HashMap<String,Set<SecurityErrorCode>> tables = new HashMap<String,Set<SecurityErrorCode>>();
 +        for (Entry<KeyExtent,Set<SecurityErrorCode>> ke : e.getAuthorizationFailuresMap().entrySet()) {
 +          Set<SecurityErrorCode> secCodes = tables.get(ke.getKey().getTableId().toString());
 +          if (secCodes == null) {
 +            secCodes = new HashSet<SecurityErrorCode>();
 +            tables.put(ke.getKey().getTableId().toString(), secCodes);
 +          }
 +          secCodes.addAll(ke.getValue());
 +        }
 +        System.err.println("ERROR : Not authorized to write to tables : " + tables);
 +      }
 +      
 +      if (e.getConstraintViolationSummaries().size() > 0) {
 +        System.err.println("ERROR : Constraint violations occurred : " + e.getConstraintViolationSummaries());
 +      }
++      System.exit(1);
 +    }
 +  }
 +}

http://git-wip-us.apache.org/repos/asf/accumulo/blob/04f81b50/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooCache.java
----------------------------------------------------------------------
diff --cc fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooCache.java
index aa24552,0000000..420533a
mode 100644,000000..100644
--- a/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooCache.java
+++ b/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooCache.java
@@@ -1,310 -1,0 +1,317 @@@
 +/*
 + * 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.fate.zookeeper;
 +
 +import java.io.ByteArrayInputStream;
 +import java.io.ByteArrayOutputStream;
 +import java.io.DataInputStream;
 +import java.io.DataOutputStream;
 +import java.io.IOException;
 +import java.util.Collections;
 +import java.util.ConcurrentModificationException;
 +import java.util.HashMap;
 +import java.util.Iterator;
 +import java.util.List;
 +import java.util.Map;
 +
 +import org.apache.log4j.Logger;
 +import org.apache.zookeeper.KeeperException;
 +import org.apache.zookeeper.KeeperException.Code;
 +import org.apache.zookeeper.WatchedEvent;
 +import org.apache.zookeeper.Watcher;
 +import org.apache.zookeeper.ZooKeeper;
 +import org.apache.zookeeper.data.Stat;
 +
 +/**
 + * Caches values stored in zookeeper and keeps them up to date as they change in zookeeper.
 + * 
 + */
 +public class ZooCache {
 +  private static final Logger log = Logger.getLogger(ZooCache.class);
 +  
 +  private ZCacheWatcher watcher = new ZCacheWatcher();
 +  private Watcher externalWatcher = null;
 +  
 +  private HashMap<String,byte[]> cache;
 +  private HashMap<String,Stat> statCache;
 +  private HashMap<String,List<String>> childrenCache;
 +  
 +  private ZooReader zReader;
 +  
 +  private ZooKeeper getZooKeeper() {
 +    return zReader.getZooKeeper();
 +  }
 +  
 +  private class ZCacheWatcher implements Watcher {
 +    @Override
 +    public void process(WatchedEvent event) {
 +      
 +      if (log.isTraceEnabled())
 +        log.trace(event);
 +      
 +      switch (event.getType()) {
 +        case NodeDataChanged:
 +        case NodeChildrenChanged:
 +        case NodeCreated:
 +        case NodeDeleted:
 +          remove(event.getPath());
 +          break;
 +        case None:
 +          switch (event.getState()) {
 +            case Disconnected:
 +              if (log.isTraceEnabled())
 +                log.trace("Zoo keeper connection disconnected, clearing cache");
 +              clear();
 +              break;
 +            case SyncConnected:
 +              break;
 +            case Expired:
 +              if (log.isTraceEnabled())
 +                log.trace("Zoo keeper connection expired, clearing cache");
 +              clear();
 +              break;
 +            default:
 +              log.warn("Unhandled: " + event);
 +          }
 +          break;
 +        default:
 +          log.warn("Unhandled: " + event);
 +      }
 +      
 +      if (externalWatcher != null) {
 +        externalWatcher.process(event);
 +      }
 +    }
 +  }
 +  
 +  public ZooCache(String zooKeepers, int sessionTimeout) {
 +    this(zooKeepers, sessionTimeout, null);
 +  }
 +  
 +  public ZooCache(String zooKeepers, int sessionTimeout, Watcher watcher) {
 +    this(new ZooReader(zooKeepers, sessionTimeout), watcher);
 +  }
 +  
 +  public ZooCache(ZooReader reader, Watcher watcher) {
 +    this.zReader = reader;
 +    this.cache = new HashMap<String,byte[]>();
 +    this.statCache = new HashMap<String,Stat>();
 +    this.childrenCache = new HashMap<String,List<String>>();
 +    this.externalWatcher = watcher;
 +  }
 +  
 +  private static interface ZooRunnable {
 +    void run(ZooKeeper zooKeeper) throws KeeperException, InterruptedException;
 +  }
 +  
 +  private synchronized void retry(ZooRunnable op) {
 +    
 +    int sleepTime = 100;
 +    
 +    while (true) {
 +      
 +      ZooKeeper zooKeeper = getZooKeeper();
 +      
 +      try {
 +        op.run(zooKeeper);
 +        return;
 +        
 +      } catch (KeeperException e) {
 +        if (e.code() == Code.NONODE) {
 +          log.error("Looked up non existant node in cache " + e.getPath(), e);
 +        }
 +        log.warn("Zookeeper error, will retry", e);
 +      } catch (InterruptedException e) {
 +        log.info("Zookeeper error, will retry", e);
 +      } catch (ConcurrentModificationException e) {
 +        log.debug("Zookeeper was modified, will retry");
 +      }
 +      
 +      try {
 +        // do not hold lock while sleeping
 +        wait(sleepTime);
 +      } catch (InterruptedException e) {
 +        e.printStackTrace();
 +      }
 +      if (sleepTime < 10000)
 +        sleepTime = (int) (sleepTime + sleepTime * Math.random());
 +      
 +    }
 +  }
 +  
 +  public synchronized List<String> getChildren(final String zPath) {
 +    
 +    ZooRunnable zr = new ZooRunnable() {
 +      
 +      @Override
 +      public void run(ZooKeeper zooKeeper) throws KeeperException, InterruptedException {
 +        
 +        if (childrenCache.containsKey(zPath))
 +          return;
 +        
 +        try {
 +          List<String> children = zooKeeper.getChildren(zPath, watcher);
 +          childrenCache.put(zPath, children);
 +        } catch (KeeperException ke) {
 +          if (ke.code() != Code.NONODE) {
 +            throw ke;
 +          }
 +        }
 +      }
 +      
 +    };
 +    
 +    retry(zr);
 +    
 +    List<String> children = childrenCache.get(zPath);
 +    if (children == null) {
 +      return null;
 +    }
 +    return Collections.unmodifiableList(children);
 +  }
 +  
 +  public synchronized byte[] get(final String zPath) {
 +    return get(zPath, null);
 +  }
 +  
 +  public synchronized byte[] get(final String zPath, Stat stat) {
 +    ZooRunnable zr = new ZooRunnable() {
 +      
 +      @Override
 +      public void run(ZooKeeper zooKeeper) throws KeeperException, InterruptedException {
 +        
 +        if (cache.containsKey(zPath))
 +          return;
 +        
 +        /*
 +         * The following call to exists() is important, since we are caching that a node does not exist. Once the node comes into existance, it will be added to
 +         * the cache. But this notification of a node coming into existance will only be given if exists() was previously called.
 +         * 
 +         * If the call to exists() is bypassed and only getData() is called with a special case that looks for Code.NONODE in the KeeperException, then
 +         * non-existance can not be cached.
 +         */
 +        
 +        Stat stat = zooKeeper.exists(zPath, watcher);
 +        
 +        byte[] data = null;
 +        
 +        if (stat == null) {
 +          if (log.isTraceEnabled())
 +            log.trace("zookeeper did not contain " + zPath);
 +        } else {
 +          try {
 +            data = zooKeeper.getData(zPath, watcher, stat);
 +          } catch (KeeperException.BadVersionException e1) {
 +            throw new ConcurrentModificationException();
 +          } catch (KeeperException.NoNodeException e2) {
 +            throw new ConcurrentModificationException();
 +          }
 +          if (log.isTraceEnabled())
 +            log.trace("zookeeper contained " + zPath + " " + (data == null ? null : new String(data)));
 +        }
 +        if (log.isTraceEnabled())
 +          log.trace("putting " + zPath + " " + (data == null ? null : new String(data)) + " in cache");
 +        put(zPath, data, stat);
 +      }
 +      
 +    };
 +    
 +    retry(zr);
 +    
 +    if (stat != null) {
 +      Stat cstat = statCache.get(zPath);
 +      if (cstat != null) {
 +        try {
 +          ByteArrayOutputStream baos = new ByteArrayOutputStream();
 +          DataOutputStream dos = new DataOutputStream(baos);
 +          cstat.write(dos);
 +          dos.close();
 +          
 +          ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 +          DataInputStream dis = new DataInputStream(bais);
 +          stat.readFields(dis);
 +          
 +          dis.close();
 +        } catch (IOException e) {
 +          throw new RuntimeException(e);
 +        }
 +      }
 +    }
 +    
 +    return cache.get(zPath);
 +  }
 +  
 +  private synchronized void put(String zPath, byte[] data, Stat stat) {
 +    cache.put(zPath, data);
 +    statCache.put(zPath, stat);
 +  }
 +  
 +  private synchronized void remove(String zPath) {
 +    if (log.isTraceEnabled())
 +      log.trace("removing " + zPath + " from cache");
 +    cache.remove(zPath);
 +    childrenCache.remove(zPath);
 +    statCache.remove(zPath);
 +  }
 +  
 +  public synchronized void clear() {
 +    cache.clear();
 +    childrenCache.clear();
 +    statCache.clear();
 +  }
 +  
 +  public synchronized void clear(String zPath) {
 +    
 +    for (Iterator<String> i = cache.keySet().iterator(); i.hasNext();) {
 +      String path = i.next();
 +      if (path.startsWith(zPath))
 +        i.remove();
 +    }
 +    
 +    for (Iterator<String> i = childrenCache.keySet().iterator(); i.hasNext();) {
 +      String path = i.next();
 +      if (path.startsWith(zPath))
 +        i.remove();
 +    }
 +    
 +    for (Iterator<String> i = statCache.keySet().iterator(); i.hasNext();) {
 +      String path = i.next();
 +      if (path.startsWith(zPath))
 +        i.remove();
 +    }
 +  }
 +  
 +  private static Map<String,ZooCache> instances = new HashMap<String,ZooCache>();
 +  
 +  public static synchronized ZooCache getInstance(String zooKeepers, int sessionTimeout) {
 +    String key = zooKeepers + ":" + sessionTimeout;
 +    ZooCache zc = instances.get(key);
 +    if (zc == null) {
 +      zc = new ZooCache(zooKeepers, sessionTimeout);
 +      instances.put(key, zc);
 +    }
 +    
 +    return zc;
 +  }
++  
++  public void close() throws InterruptedException {
++    cache.clear();
++    statCache.clear();
++    childrenCache.clear();
++    zReader.close();
++  }
 +}

http://git-wip-us.apache.org/repos/asf/accumulo/blob/04f81b50/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooReader.java
----------------------------------------------------------------------
diff --cc fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooReader.java
index ab73012,0000000..e11f570
mode 100644,000000..100644
--- a/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooReader.java
+++ b/fate/src/main/java/org/apache/accumulo/fate/zookeeper/ZooReader.java
@@@ -1,105 -1,0 +1,109 @@@
 +/*
 + * 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.fate.zookeeper;
 +
 +import java.util.List;
 +import java.util.concurrent.atomic.AtomicBoolean;
 +import java.util.concurrent.atomic.AtomicInteger;
 +
 +import org.apache.zookeeper.KeeperException;
 +import org.apache.zookeeper.Watcher;
 +import org.apache.zookeeper.ZooKeeper;
 +import org.apache.zookeeper.AsyncCallback.VoidCallback;
 +import org.apache.zookeeper.KeeperException.Code;
 +import org.apache.zookeeper.data.Stat;
 +
 +public class ZooReader implements IZooReader {
 +  
 +  protected String keepers;
 +  protected int timeout;
 +  
 +  protected ZooKeeper getSession(String keepers, int timeout, String scheme, byte[] auth) {
 +    return ZooSession.getSession(keepers, timeout, scheme, auth);
 +  }
 +  
 +  protected ZooKeeper getZooKeeper() {
 +    return getSession(keepers, timeout, null, null);
 +  }
 +  
 +  @Override
 +  public byte[] getData(String zPath, Stat stat) throws KeeperException, InterruptedException {
 +    return getZooKeeper().getData(zPath, false, stat);
 +  }
 +  
 +  @Override
 +  public Stat getStatus(String zPath) throws KeeperException, InterruptedException {
 +    return getZooKeeper().exists(zPath, false);
 +  }
 +  
 +  @Override
 +  public Stat getStatus(String zPath, Watcher watcher) throws KeeperException, InterruptedException {
 +    return getZooKeeper().exists(zPath, watcher);
 +  }
 +  
 +  @Override
 +  public List<String> getChildren(String zPath) throws KeeperException, InterruptedException {
 +    return getZooKeeper().getChildren(zPath, false);
 +  }
 +  
 +  @Override
 +  public List<String> getChildren(String zPath, Watcher watcher) throws KeeperException, InterruptedException {
 +    return getZooKeeper().getChildren(zPath, watcher);
 +  }
 +  
 +  @Override
 +  public boolean exists(String zPath) throws KeeperException, InterruptedException {
 +    return getZooKeeper().exists(zPath, false) != null;
 +  }
 +  
 +  @Override
 +  public boolean exists(String zPath, Watcher watcher) throws KeeperException, InterruptedException {
 +    return getZooKeeper().exists(zPath, watcher) != null;
 +  }
 +  
 +  @Override
 +  public void sync(final String path) throws KeeperException, InterruptedException {
 +    final AtomicInteger rc = new AtomicInteger();
 +    final AtomicBoolean waiter = new AtomicBoolean(false);
 +    getZooKeeper().sync(path, new VoidCallback() {
 +      @Override
 +      public void processResult(int code, String arg1, Object arg2) {
 +        rc.set(code);
 +        synchronized (waiter) {
 +          waiter.set(true);
 +          waiter.notifyAll();
 +        }
 +      }}, null);
 +    synchronized (waiter) {
 +      while (!waiter.get())
 +        waiter.wait();
 +    }
 +    Code code = Code.get(rc.get());
 +    if (code != KeeperException.Code.OK) {
 +      throw KeeperException.create(code);
 +    }
 +  }  
 +  
 +  public ZooReader(String keepers, int timeout) {
 +    this.keepers = keepers;
 +    this.timeout = timeout;
 +  }
++
++  public void close() throws InterruptedException {
++    getZooKeeper().close();
++  }
 +}

http://git-wip-us.apache.org/repos/asf/accumulo/blob/04f81b50/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterGCTest.java
----------------------------------------------------------------------
diff --cc minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterGCTest.java
index a579397,0000000..a1f58f6
mode 100644,000000..100644
--- a/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterGCTest.java
+++ b/minicluster/src/test/java/org/apache/accumulo/minicluster/MiniAccumuloClusterGCTest.java
@@@ -1,129 -1,0 +1,150 @@@
 +/*
 + * 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.minicluster;
 +
 +import java.io.File;
 +import java.util.Map;
 +
 +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.ZooKeeperInstance;
 +import org.apache.accumulo.core.client.security.tokens.PasswordToken;
 +import org.apache.accumulo.core.conf.Property;
 +import org.apache.accumulo.core.data.Mutation;
 +import org.apache.accumulo.server.util.PortUtils;
 +import org.apache.commons.io.FileUtils;
 +import org.apache.commons.io.filefilter.SuffixFileFilter;
 +import org.apache.commons.io.filefilter.TrueFileFilter;
 +import org.apache.log4j.Level;
 +import org.apache.log4j.Logger;
- import org.junit.AfterClass;
 +import org.junit.Assert;
- import org.junit.BeforeClass;
++import org.junit.Ignore;
 +import org.junit.Test;
 +import org.junit.rules.TemporaryFolder;
 +
 +import com.google.common.collect.ImmutableMap;
++import com.google.common.io.Files;
 +
 +/**
 + * 
 + */
 +public class MiniAccumuloClusterGCTest {
 +  
++  @Test
++  public void testGcConfig() throws Exception {
++    File f = Files.createTempDir();
++    f.deleteOnExit();
++    try {
++      MiniAccumuloConfig macConfig = new MiniAccumuloConfig(f, passwd);
++      macConfig.setNumTservers(1);
++  
++      Assert.assertEquals(false, macConfig.shouldRunGC());
++      
++      // Turn on the garbage collector
++      macConfig.runGC(true);
++  
++      Assert.assertEquals(true, macConfig.shouldRunGC());
++    } finally {
++      if (null != f && f.exists()) {
++        f.delete();
++      }
++    }
++  }
++
++  
 +  private static TemporaryFolder tmpDir = new TemporaryFolder();
 +  private static MiniAccumuloConfig macConfig;
 +  private static MiniAccumuloCluster accumulo;
 +  private static final String passwd = "password";
 +  
-   @BeforeClass
 +  public static void setupMiniCluster() throws Exception {
 +    tmpDir.create();
 +    Logger.getLogger("org.apache.zookeeper").setLevel(Level.ERROR);
 +    
 +    macConfig = new MiniAccumuloConfig(tmpDir.getRoot(), passwd);
 +    macConfig.setNumTservers(1);
 +    
 +    // Turn on the garbage collector
 +    macConfig.runGC(true);
 +    
 +    String gcPort = Integer.toString(PortUtils.getRandomFreePort());
 +    
 +    // And tweak the settings to make it run often
 +    Map<String,String> config = ImmutableMap.of(Property.GC_CYCLE_DELAY.getKey(), "1s", Property.GC_CYCLE_START.getKey(), "0s", Property.GC_PORT.getKey(), gcPort);
 +    macConfig.setSiteConfig(config);
 +    
 +    accumulo = new MiniAccumuloCluster(macConfig);
 +    accumulo.start();
 +  }
 +  
-   @AfterClass
 +  public static void tearDownMiniCluster() throws Exception {
 +    accumulo.stop();
 +    tmpDir.delete();
 +  }
 +  
-   @Test(timeout = 20000)
++  // This test seems to be a little too unstable for a unit test
++  @Ignore
 +  public void test() throws Exception {
 +    ZooKeeperInstance inst = new ZooKeeperInstance(accumulo.getInstanceName(), accumulo.getZooKeepers());
 +    Connector c = inst.getConnector("root", new PasswordToken(passwd));
 +    
 +    final String table = "foobar";
 +    c.tableOperations().create(table);
 +    
 +    BatchWriter bw = null;
 +    
 +    // Add some data
 +    try {
 +      bw = c.createBatchWriter(table, new BatchWriterConfig());
 +      Mutation m = new Mutation("a");
 +      for (int i = 0; i < 50; i++) {
 +        m.put("colf", Integer.toString(i), "");
 +      }
 +      
 +      bw.addMutation(m);
 +    } finally {
 +      if (null != bw) {
 +        bw.close();
 +      }
 +    }
 +    
 +    final boolean flush = true, wait = true;
 +    
 +    // Compact the tables to get some rfiles which we can gc
 +    c.tableOperations().compact(table, null, null, flush, wait);
 +    c.tableOperations().compact("!METADATA", null, null, flush, wait);
 +    
 +    File accumuloDir = new File(tmpDir.getRoot().getAbsolutePath(), "accumulo");
 +    File tables = new File(accumuloDir.getAbsolutePath(), "tables");
 +    
 +    int fileCountAfterCompaction = FileUtils.listFiles(tables, new SuffixFileFilter(".rf"), TrueFileFilter.TRUE).size();
 +    
 +    // Sleep for 4s to let the GC do its thing
 +    for (int i = 1; i < 5; i++) {
 +      Thread.sleep(1000);
 +      int fileCountAfterGCWait = FileUtils.listFiles(tables, new SuffixFileFilter(".rf"), TrueFileFilter.TRUE).size();
 +
 +      if (fileCountAfterGCWait < fileCountAfterCompaction) {
 +        return;
 +      }
 +    }
 +    
 +    Assert.fail("Expected to find less files after compaction and pause for GC");
 +  }
 +  
 +}


Mime
View raw message