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 static org.junit.Assert.assertEquals;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.TreeSet;
28  import java.util.Arrays;
29  
30  import org.apache.hadoop.hbase.*;
31  import org.apache.hadoop.hbase.regionserver.ScanQueryMatcher.MatchCode;
32  import org.apache.hadoop.hbase.testclassification.SmallTests;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  
38  @Category(SmallTests.class)
39  public class TestExplicitColumnTracker {
40  
41    private final byte[] col1 = Bytes.toBytes("col1");
42    private final byte[] col2 = Bytes.toBytes("col2");
43    private final byte[] col3 = Bytes.toBytes("col3");
44    private final byte[] col4 = Bytes.toBytes("col4");
45    private final byte[] col5 = Bytes.toBytes("col5");
46  
47    private void runTest(int maxVersions,
48                         TreeSet<byte[]> trackColumns,
49                         List<byte[]> scannerColumns,
50                         List<MatchCode> expected) throws IOException {
51      ColumnTracker exp = new ExplicitColumnTracker(
52        trackColumns, 0, maxVersions, Long.MIN_VALUE);
53  
54  
55      //Initialize result
56      List<ScanQueryMatcher.MatchCode> result = new ArrayList<ScanQueryMatcher.MatchCode>();
57  
58      long timestamp = 0;
59      //"Match"
60      for(byte [] col : scannerColumns){
61        result.add(ScanQueryMatcher.checkColumn(exp, col, 0, col.length, ++timestamp,
62          KeyValue.Type.Put.getCode(), false));
63      }
64  
65      assertEquals(expected.size(), result.size());
66      for(int i=0; i< expected.size(); i++){
67        assertEquals(expected.get(i), result.get(i));
68      }
69    }
70  
71    @Test
72    public void testGet_SingleVersion() throws IOException{
73      //Create tracker
74      TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
75      //Looking for every other
76      columns.add(col2);
77      columns.add(col4);
78      List<MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
79      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);             // col1
80      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL); // col2
81      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);             // col3
82      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW); // col4
83      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);             // col5
84      int maxVersions = 1;
85  
86      //Create "Scanner"
87      List<byte[]> scanner = new ArrayList<byte[]>();
88      scanner.add(col1);
89      scanner.add(col2);
90      scanner.add(col3);
91      scanner.add(col4);
92      scanner.add(col5);
93  
94      runTest(maxVersions, columns, scanner, expected);
95    }
96  
97    @Test
98    public void testGet_MultiVersion() throws IOException{
99      //Create tracker
100     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
101     //Looking for every other
102     columns.add(col2);
103     columns.add(col4);
104 
105     List<ScanQueryMatcher.MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
106     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
107     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
108     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
109 
110     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);                   // col2; 1st version
111     expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL); // col2; 2nd version
112     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
113 
114     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
115     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
116     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
117 
118     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);                   // col4; 1st version
119     expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW); // col4; 2nd version
120     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
121 
122     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
123     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
124     expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW);
125     int maxVersions = 2;
126 
127     //Create "Scanner"
128     List<byte[]> scanner = new ArrayList<byte[]>();
129     scanner.add(col1);
130     scanner.add(col1);
131     scanner.add(col1);
132     scanner.add(col2);
133     scanner.add(col2);
134     scanner.add(col2);
135     scanner.add(col3);
136     scanner.add(col3);
137     scanner.add(col3);
138     scanner.add(col4);
139     scanner.add(col4);
140     scanner.add(col4);
141     scanner.add(col5);
142     scanner.add(col5);
143     scanner.add(col5);
144 
145     //Initialize result
146     runTest(maxVersions, columns, scanner, expected);
147   }
148 
149   /**
150    * hbase-2259
151    */
152   @Test
153   public void testStackOverflow() throws IOException{
154     int maxVersions = 1;
155     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
156     for (int i = 0; i < 100000; i++) {
157       columns.add(Bytes.toBytes("col"+i));
158     }
159 
160     ColumnTracker explicit = new ExplicitColumnTracker(columns, 0, maxVersions,
161         Long.MIN_VALUE);
162     for (int i = 0; i < 100000; i+=2) {
163       byte [] col = Bytes.toBytes("col"+i);
164       ScanQueryMatcher.checkColumn(explicit, col, 0, col.length, 1, KeyValue.Type.Put.getCode(),
165         false);
166     }
167     explicit.reset();
168 
169     for (int i = 1; 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   }
175 
176   /**
177    * Regression test for HBASE-2545
178    */
179   @Test
180   public void testInfiniteLoop() throws IOException {
181     TreeSet<byte[]> columns = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
182     columns.addAll(Arrays.asList(new byte[][] {
183       col2, col3, col5 }));
184     List<byte[]> scanner = Arrays.<byte[]>asList(
185       new byte[][] { col1, col4 });
186     List<ScanQueryMatcher.MatchCode> expected = Arrays.<ScanQueryMatcher.MatchCode>asList(
187       new ScanQueryMatcher.MatchCode[] {
188         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
189         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL });
190     runTest(1, columns, scanner, expected);
191   }
192 
193 }
194