View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.TreeSet;
26  import java.util.Arrays;
27  
28  import org.apache.hadoop.hbase.*;
29  import org.apache.hadoop.hbase.regionserver.ScanQueryMatcher.MatchCode;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.experimental.categories.Category;
32  
33  
34  @Category(SmallTests.class)
35  public class TestExplicitColumnTracker extends HBaseTestCase {
36    private boolean PRINT = false;
37  
38    private final byte[] col1 = Bytes.toBytes("col1");
39    private final byte[] col2 = Bytes.toBytes("col2");
40    private final byte[] col3 = Bytes.toBytes("col3");
41    private final byte[] col4 = Bytes.toBytes("col4");
42    private final byte[] col5 = Bytes.toBytes("col5");
43  
44    private void runTest(int maxVersions,
45                         TreeSet<byte[]> trackColumns,
46                         List<byte[]> scannerColumns,
47                         List<MatchCode> expected) throws IOException {
48      ColumnTracker exp = new ExplicitColumnTracker(
49        trackColumns, 0, maxVersions, Long.MIN_VALUE);
50  
51  
52      //Initialize result
53      List<ScanQueryMatcher.MatchCode> result = new ArrayList<ScanQueryMatcher.MatchCode>();
54  
55      long timestamp = 0;
56      //"Match"
57      for(byte [] col : scannerColumns){
58        result.add(ScanQueryMatcher.checkColumn(exp, col, 0, col.length, ++timestamp,
59          KeyValue.Type.Put.getCode(), false));
60      }
61  
62      assertEquals(expected.size(), result.size());
63      for(int i=0; i< expected.size(); i++){
64        assertEquals(expected.get(i), result.get(i));
65        if(PRINT){
66          System.out.println("Expected " +expected.get(i) + ", actual " +
67              result.get(i));
68        }
69      }
70    }
71  
72    public void testGet_SingleVersion() throws IOException{
73      if(PRINT){
74        System.out.println("SingleVersion");
75      }
76  
77      //Create tracker
78      TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
79      //Looking for every other
80      columns.add(col2);
81      columns.add(col4);
82      List<MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
83      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);             // col1
84      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL); // col2
85      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);             // col3
86      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW); // col4
87      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);             // col5
88      int maxVersions = 1;
89  
90      //Create "Scanner"
91      List<byte[]> scanner = new ArrayList<byte[]>();
92      scanner.add(col1);
93      scanner.add(col2);
94      scanner.add(col3);
95      scanner.add(col4);
96      scanner.add(col5);
97  
98      runTest(maxVersions, columns, scanner, expected);
99    }
100 
101   public void testGet_MultiVersion() throws IOException{
102     if(PRINT){
103       System.out.println("\nMultiVersion");
104     }
105 
106     //Create tracker
107     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
108     //Looking for every other
109     columns.add(col2);
110     columns.add(col4);
111 
112     List<ScanQueryMatcher.MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
113     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
114     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
115     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
116 
117     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);                   // col2; 1st version
118     expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL); // col2; 2nd version
119     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
120 
121     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
122     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
123     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
124 
125     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);                   // col4; 1st version
126     expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW); // col4; 2nd version
127     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
128 
129     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
130     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
131     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
132     int maxVersions = 2;
133 
134     //Create "Scanner"
135     List<byte[]> scanner = new ArrayList<byte[]>();
136     scanner.add(col1);
137     scanner.add(col1);
138     scanner.add(col1);
139     scanner.add(col2);
140     scanner.add(col2);
141     scanner.add(col2);
142     scanner.add(col3);
143     scanner.add(col3);
144     scanner.add(col3);
145     scanner.add(col4);
146     scanner.add(col4);
147     scanner.add(col4);
148     scanner.add(col5);
149     scanner.add(col5);
150     scanner.add(col5);
151 
152     //Initialize result
153     runTest(maxVersions, columns, scanner, expected);
154   }
155 
156 
157   /**
158    * hbase-2259
159    */
160   public void testStackOverflow() throws IOException{
161     int maxVersions = 1;
162     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
163     for (int i = 0; i < 100000; i++) {
164       columns.add(Bytes.toBytes("col"+i));
165     }
166 
167     ColumnTracker explicit = new ExplicitColumnTracker(columns, 0, maxVersions,
168         Long.MIN_VALUE);
169     for (int i = 0; i < 100000; i+=2) {
170       byte [] col = Bytes.toBytes("col"+i);
171       ScanQueryMatcher.checkColumn(explicit, col, 0, col.length, 1, KeyValue.Type.Put.getCode(),
172         false);
173     }
174     explicit.reset();
175 
176     for (int i = 1; i < 100000; i+=2) {
177       byte [] col = Bytes.toBytes("col"+i);
178       ScanQueryMatcher.checkColumn(explicit, col, 0, col.length, 1, KeyValue.Type.Put.getCode(),
179         false);
180     }
181   }
182 
183   /**
184    * Regression test for HBASE-2545
185    */
186   public void testInfiniteLoop() throws IOException {
187     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
188     columns.addAll(Arrays.asList(new byte[][] {
189       col2, col3, col5 }));
190     List<byte[]> scanner = Arrays.<byte[]>asList(
191       new byte[][] { col1, col4 });
192     List<ScanQueryMatcher.MatchCode> expected = Arrays.<ScanQueryMatcher.MatchCode>asList(
193       new ScanQueryMatcher.MatchCode[] {
194         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
195         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL });
196     runTest(1, columns, scanner, expected);
197   }
198 
199 }
200