View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver.querymatcher;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.KeepDeletedCells;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.KeyValueUtil;
34  import org.apache.hadoop.hbase.regionserver.ScanInfo;
35  import org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode;
36  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
37  import org.apache.hadoop.hbase.testclassification.SmallTests;
38  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  @Category({ RegionServerTests.class, SmallTests.class })
43  public class TestUserScanQueryMatcher extends AbstractTestScanQueryMatcher {
44  
45    private static final Log LOG = LogFactory.getLog(TestUserScanQueryMatcher.class);
46  
47    /**
48     * This is a cryptic test. It is checking that we don't include a fake cell, one that has a
49     * timestamp of {@link HConstants#OLDEST_TIMESTAMP}. See HBASE-16074 for background.
50     * @throws IOException
51     */
52    @Test
53    public void testNeverIncludeFakeCell() throws IOException {
54      long now = EnvironmentEdgeManager.currentTime();
55      // Do with fam2 which has a col2 qualifier.
56      UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
57        new ScanInfo(this.conf, fam2, 10, 1, ttl, KeepDeletedCells.FALSE, 0, rowComparator),
58        get.getFamilyMap().get(fam2), now - ttl, now, null);
59      Cell kv = new KeyValue(row1, fam2, col2, 1, data);
60      Cell cell = KeyValueUtil.createLastOnRowCol(kv);
61      qm.setToNewRow(kv);
62      MatchCode code = qm.match(cell);
63      assertFalse(code.compareTo(MatchCode.SEEK_NEXT_COL) != 0);
64    }
65  
66    @Test
67    public void testMatchExplicitColumns() throws IOException {
68      // Moving up from the Tracker by using Gets and List<KeyValue> instead
69      // of just byte []
70  
71      // Expected result
72      List<MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
73      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
74      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL);
75      expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
76      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL);
77      expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW);
78      expected.add(ScanQueryMatcher.MatchCode.DONE);
79  
80      long now = EnvironmentEdgeManager.currentTime();
81      // 2,4,5
82      UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
83        new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE, 0, rowComparator),
84        get.getFamilyMap().get(fam2), now - ttl, now, null);
85  
86      List<KeyValue> memstore = new ArrayList<KeyValue>();
87      memstore.add(new KeyValue(row1, fam2, col1, 1, data));
88      memstore.add(new KeyValue(row1, fam2, col2, 1, data));
89      memstore.add(new KeyValue(row1, fam2, col3, 1, data));
90      memstore.add(new KeyValue(row1, fam2, col4, 1, data));
91      memstore.add(new KeyValue(row1, fam2, col5, 1, data));
92  
93      memstore.add(new KeyValue(row2, fam1, col1, data));
94  
95      List<ScanQueryMatcher.MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>();
96      KeyValue k = memstore.get(0);
97      qm.setToNewRow(k);
98  
99      for (KeyValue kv : memstore) {
100       actual.add(qm.match(kv));
101     }
102 
103     assertEquals(expected.size(), actual.size());
104     for (int i = 0; i < expected.size(); i++) {
105       LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
106       assertEquals(expected.get(i), actual.get(i));
107     }
108   }
109 
110   @Test
111   public void testMatch_Wildcard() throws IOException {
112     // Moving up from the Tracker by using Gets and List<KeyValue> instead
113     // of just byte []
114 
115     // Expected result
116     List<MatchCode> expected = new ArrayList<ScanQueryMatcher.MatchCode>();
117     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
118     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
119     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
120     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
121     expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
122     expected.add(ScanQueryMatcher.MatchCode.DONE);
123 
124     long now = EnvironmentEdgeManager.currentTime();
125     UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
126       new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE, 0, rowComparator), null,
127       now - ttl, now, null);
128 
129     List<KeyValue> memstore = new ArrayList<KeyValue>();
130     memstore.add(new KeyValue(row1, fam2, col1, 1, data));
131     memstore.add(new KeyValue(row1, fam2, col2, 1, data));
132     memstore.add(new KeyValue(row1, fam2, col3, 1, data));
133     memstore.add(new KeyValue(row1, fam2, col4, 1, data));
134     memstore.add(new KeyValue(row1, fam2, col5, 1, data));
135     memstore.add(new KeyValue(row2, fam1, col1, 1, data));
136 
137     List<ScanQueryMatcher.MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>();
138 
139     KeyValue k = memstore.get(0);
140     qm.setToNewRow(k);
141 
142     for (KeyValue kv : memstore) {
143       actual.add(qm.match(kv));
144     }
145 
146     assertEquals(expected.size(), actual.size());
147     for (int i = 0; i < expected.size(); i++) {
148       LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
149       assertEquals(expected.get(i), actual.get(i));
150     }
151   }
152 
153   /**
154    * Verify that {@link ScanQueryMatcher} only skips expired KeyValue instances and does not exit
155    * early from the row (skipping later non-expired KeyValues). This version mimics a Get with
156    * explicitly specified column qualifiers.
157    * @throws IOException
158    */
159   @Test
160   public void testMatch_ExpiredExplicit() throws IOException {
161 
162     long testTTL = 1000;
163     MatchCode[] expected = new MatchCode[] { ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
164         ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL,
165         ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
166         ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL,
167         ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW, ScanQueryMatcher.MatchCode.DONE };
168 
169     long now = EnvironmentEdgeManager.currentTime();
170     UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
171       new ScanInfo(this.conf, fam2, 0, 1, testTTL, KeepDeletedCells.FALSE, 0, rowComparator),
172       get.getFamilyMap().get(fam2), now - testTTL, now, null);
173 
174     KeyValue[] kvs = new KeyValue[] { new KeyValue(row1, fam2, col1, now - 100, data),
175         new KeyValue(row1, fam2, col2, now - 50, data),
176         new KeyValue(row1, fam2, col3, now - 5000, data),
177         new KeyValue(row1, fam2, col4, now - 500, data),
178         new KeyValue(row1, fam2, col5, now - 10000, data),
179         new KeyValue(row2, fam1, col1, now - 10, data) };
180 
181     KeyValue k = kvs[0];
182     qm.setToNewRow(k);
183 
184     List<MatchCode> actual = new ArrayList<MatchCode>(kvs.length);
185     for (KeyValue kv : kvs) {
186       actual.add(qm.match(kv));
187     }
188 
189     assertEquals(expected.length, actual.size());
190     for (int i = 0; i < expected.length; i++) {
191       LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
192       assertEquals(expected[i], actual.get(i));
193     }
194   }
195 
196   /**
197    * Verify that {@link ScanQueryMatcher} only skips expired KeyValue instances and does not exit
198    * early from the row (skipping later non-expired KeyValues). This version mimics a Get with
199    * wildcard-inferred column qualifiers.
200    * @throws IOException
201    */
202   @Test
203   public void testMatch_ExpiredWildcard() throws IOException {
204 
205     long testTTL = 1000;
206     MatchCode[] expected = new MatchCode[] { ScanQueryMatcher.MatchCode.INCLUDE,
207         ScanQueryMatcher.MatchCode.INCLUDE, ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
208         ScanQueryMatcher.MatchCode.INCLUDE, ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
209         ScanQueryMatcher.MatchCode.DONE };
210 
211     long now = EnvironmentEdgeManager.currentTime();
212     UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
213       new ScanInfo(this.conf, fam2, 0, 1, testTTL, KeepDeletedCells.FALSE, 0, rowComparator), null,
214       now - testTTL, now, null);
215 
216     KeyValue[] kvs = new KeyValue[] { new KeyValue(row1, fam2, col1, now - 100, data),
217         new KeyValue(row1, fam2, col2, now - 50, data),
218         new KeyValue(row1, fam2, col3, now - 5000, data),
219         new KeyValue(row1, fam2, col4, now - 500, data),
220         new KeyValue(row1, fam2, col5, now - 10000, data),
221         new KeyValue(row2, fam1, col1, now - 10, data) };
222     KeyValue k = kvs[0];
223     qm.setToNewRow(k);
224 
225     List<ScanQueryMatcher.MatchCode> actual = new ArrayList<ScanQueryMatcher.MatchCode>(kvs.length);
226     for (KeyValue kv : kvs) {
227       actual.add(qm.match(kv));
228     }
229 
230     assertEquals(expected.length, actual.size());
231     for (int i = 0; i < expected.length; i++) {
232       LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
233       assertEquals(expected[i], actual.get(i));
234     }
235   }
236 }