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, int lookAhead) throws IOException {
48      ColumnTracker exp = new ExplicitColumnTracker(
49        trackColumns, 0, maxVersions, Long.MIN_VALUE, lookAhead);
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, 0);
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, 0);
154   }
155 
156   public void testGet_MultiVersionWithLookAhead() throws IOException{
157     if(PRINT){
158       System.out.println("\nMultiVersion");
159     }
160 
161     //Create tracker
162     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
163     //Looking for every other
164     columns.add(col2);
165     columns.add(col4);
166 
167     List<ScanQueryMatcher.MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
168     expected.add(ScanQueryMatcher.MatchCode.SKIP);
169     expected.add(ScanQueryMatcher.MatchCode.SKIP);
170     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
171 
172     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);                   // col2; 1st version
173     expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL); // col2; 2nd version
174     expected.add(ScanQueryMatcher.MatchCode.SKIP);
175 
176     expected.add(ScanQueryMatcher.MatchCode.SKIP);
177     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
178     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
179 
180     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);                   // col4; 1st version
181     expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW); // col4; 2nd version
182     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
183 
184     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
185     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
186     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
187     int maxVersions = 2;
188 
189     //Create "Scanner"
190     List<byte[]> scanner = new ArrayList<byte[]>();
191     scanner.add(col1);
192     scanner.add(col1);
193     scanner.add(col1);
194     scanner.add(col2);
195     scanner.add(col2);
196     scanner.add(col2);
197     scanner.add(col3);
198     scanner.add(col3);
199     scanner.add(col3);
200     scanner.add(col4);
201     scanner.add(col4);
202     scanner.add(col4);
203     scanner.add(col5);
204     scanner.add(col5);
205     scanner.add(col5);
206 
207     //Initialize result
208     runTest(maxVersions, columns, scanner, expected, 2);
209   }
210 
211   /**
212    * hbase-2259
213    */
214   public void testStackOverflow() throws IOException{
215     int maxVersions = 1;
216     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
217     for (int i = 0; i < 100000; i++) {
218       columns.add(Bytes.toBytes("col"+i));
219     }
220 
221     ColumnTracker explicit = new ExplicitColumnTracker(columns, 0, maxVersions,
222         Long.MIN_VALUE, 0);
223     for (int i = 0; i < 100000; i+=2) {
224       byte [] col = Bytes.toBytes("col"+i);
225       ScanQueryMatcher.checkColumn(explicit, col, 0, col.length, 1, KeyValue.Type.Put.getCode(),
226         false);
227     }
228     explicit.reset();
229 
230     for (int i = 1; i < 100000; i+=2) {
231       byte [] col = Bytes.toBytes("col"+i);
232       ScanQueryMatcher.checkColumn(explicit, col, 0, col.length, 1, KeyValue.Type.Put.getCode(),
233         false);
234     }
235   }
236 
237   /**
238    * Regression test for HBASE-2545
239    */
240   public void testInfiniteLoop() throws IOException {
241     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
242     columns.addAll(Arrays.asList(new byte[][] {
243       col2, col3, col5 }));
244     List<byte[]> scanner = Arrays.<byte[]>asList(
245       new byte[][] { col1, col4 });
246     List<ScanQueryMatcher.MatchCode> expected = Arrays.<ScanQueryMatcher.MatchCode>asList(
247       new ScanQueryMatcher.MatchCode[] {
248         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
249         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL });
250     runTest(1, columns, scanner, expected, 0);
251   }
252 
253 }
254