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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.thrift2
;
20 import static java
.nio
.ByteBuffer
.wrap
;
21 import static org
.apache
.hadoop
.hbase
.thrift
.HBaseServiceHandler
.CLEANUP_INTERVAL
;
22 import static org
.apache
.hadoop
.hbase
.thrift
.HBaseServiceHandler
.MAX_IDLETIME
;
23 import static org
.apache
.hadoop
.hbase
.thrift2
.ThriftUtilities
.deleteFromThrift
;
24 import static org
.apache
.hadoop
.hbase
.thrift2
.ThriftUtilities
.getFromThrift
;
25 import static org
.apache
.hadoop
.hbase
.thrift2
.ThriftUtilities
.incrementFromThrift
;
26 import static org
.apache
.hadoop
.hbase
.thrift2
.ThriftUtilities
.putFromThrift
;
27 import static org
.apache
.hadoop
.hbase
.thrift2
.ThriftUtilities
.scanFromThrift
;
28 import static org
.junit
.Assert
.assertArrayEquals
;
29 import static org
.junit
.Assert
.assertEquals
;
30 import static org
.junit
.Assert
.assertFalse
;
31 import static org
.junit
.Assert
.assertNull
;
32 import static org
.junit
.Assert
.assertTrue
;
33 import static org
.junit
.Assert
.fail
;
35 import java
.io
.IOException
;
36 import java
.io
.InterruptedIOException
;
37 import java
.net
.InetAddress
;
38 import java
.nio
.ByteBuffer
;
39 import java
.util
.ArrayList
;
40 import java
.util
.Arrays
;
41 import java
.util
.Collections
;
42 import java
.util
.Comparator
;
43 import java
.util
.HashMap
;
44 import java
.util
.List
;
46 import java
.util
.Optional
;
47 import java
.util
.concurrent
.TimeUnit
;
48 import org
.apache
.hadoop
.conf
.Configuration
;
49 import org
.apache
.hadoop
.hbase
.Cell
;
50 import org
.apache
.hadoop
.hbase
.CompatibilityFactory
;
51 import org
.apache
.hadoop
.hbase
.CoprocessorEnvironment
;
52 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
53 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
54 import org
.apache
.hadoop
.hbase
.TableName
;
55 import org
.apache
.hadoop
.hbase
.client
.Admin
;
56 import org
.apache
.hadoop
.hbase
.client
.ColumnFamilyDescriptorBuilder
;
57 import org
.apache
.hadoop
.hbase
.client
.Consistency
;
58 import org
.apache
.hadoop
.hbase
.client
.Delete
;
59 import org
.apache
.hadoop
.hbase
.client
.Durability
;
60 import org
.apache
.hadoop
.hbase
.client
.Get
;
61 import org
.apache
.hadoop
.hbase
.client
.Increment
;
62 import org
.apache
.hadoop
.hbase
.client
.Put
;
63 import org
.apache
.hadoop
.hbase
.client
.Scan
;
64 import org
.apache
.hadoop
.hbase
.client
.Table
;
65 import org
.apache
.hadoop
.hbase
.client
.TableDescriptor
;
66 import org
.apache
.hadoop
.hbase
.client
.TableDescriptorBuilder
;
67 import org
.apache
.hadoop
.hbase
.coprocessor
.ObserverContext
;
68 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionCoprocessor
;
69 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionCoprocessorEnvironment
;
70 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionObserver
;
71 import org
.apache
.hadoop
.hbase
.filter
.ParseFilter
;
72 import org
.apache
.hadoop
.hbase
.security
.UserProvider
;
73 import org
.apache
.hadoop
.hbase
.test
.MetricsAssertHelper
;
74 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
75 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
76 import org
.apache
.hadoop
.hbase
.thrift
.ErrorThrowingGetObserver
;
77 import org
.apache
.hadoop
.hbase
.thrift
.HBaseThriftTestingUtility
;
78 import org
.apache
.hadoop
.hbase
.thrift
.HbaseHandlerMetricsProxy
;
79 import org
.apache
.hadoop
.hbase
.thrift
.ThriftMetrics
;
80 import org
.apache
.hadoop
.hbase
.thrift
.ThriftMetrics
.ThriftServerType
;
81 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TAppend
;
82 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TColumn
;
83 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TColumnFamilyDescriptor
;
84 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TColumnIncrement
;
85 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TColumnValue
;
86 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TCompareOperator
;
87 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TConsistency
;
88 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TDataBlockEncoding
;
89 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TDelete
;
90 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TDeleteType
;
91 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TDurability
;
92 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TGet
;
93 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.THBaseService
;
94 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TIOError
;
95 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TIllegalArgument
;
96 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TIncrement
;
97 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TMutation
;
98 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TNamespaceDescriptor
;
99 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TPut
;
100 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TReadType
;
101 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TResult
;
102 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TRowMutations
;
103 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TScan
;
104 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TTableDescriptor
;
105 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TTableName
;
106 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TThriftServerType
;
107 import org
.apache
.hadoop
.hbase
.thrift2
.generated
.TTimeRange
;
108 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
109 import org
.apache
.thrift
.TException
;
110 import org
.apache
.thrift
.protocol
.TBinaryProtocol
;
111 import org
.apache
.thrift
.protocol
.TProtocol
;
112 import org
.apache
.thrift
.transport
.TSocket
;
113 import org
.apache
.thrift
.transport
.TTransport
;
114 import org
.junit
.AfterClass
;
115 import org
.junit
.Before
;
116 import org
.junit
.BeforeClass
;
117 import org
.junit
.ClassRule
;
118 import org
.junit
.Rule
;
119 import org
.junit
.Test
;
120 import org
.junit
.experimental
.categories
.Category
;
121 import org
.junit
.rules
.TestName
;
122 import org
.slf4j
.Logger
;
123 import org
.slf4j
.LoggerFactory
;
125 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Lists
;
126 import org
.apache
.hbase
.thirdparty
.org
.apache
.commons
.collections4
.CollectionUtils
;
129 * Unit testing for ThriftServer.HBaseServiceHandler, a part of the org.apache.hadoop.hbase.thrift2
132 @Category({ClientTests
.class, MediumTests
.class})
133 public class TestThriftHBaseServiceHandler
{
136 public static final HBaseClassTestRule CLASS_RULE
=
137 HBaseClassTestRule
.forClass(TestThriftHBaseServiceHandler
.class);
139 private static final Logger LOG
= LoggerFactory
.getLogger(TestThriftHBaseServiceHandler
.class);
140 private static final HBaseTestingUtility UTIL
= new HBaseTestingUtility();
142 // Static names for tables, columns, rows, and values
143 private static byte[] tableAname
= Bytes
.toBytes("tableA");
144 private static byte[] familyAname
= Bytes
.toBytes("familyA");
145 private static byte[] familyBname
= Bytes
.toBytes("familyB");
146 private static byte[] qualifierAname
= Bytes
.toBytes("qualifierA");
147 private static byte[] qualifierBname
= Bytes
.toBytes("qualifierB");
148 private static byte[] valueAname
= Bytes
.toBytes("valueA");
149 private static byte[] valueBname
= Bytes
.toBytes("valueB");
150 private static ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor
[] families
=
151 new ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor
[]{
152 new ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor(familyAname
)
154 new ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor(familyBname
)
159 private static final MetricsAssertHelper metricsHelper
=
160 CompatibilityFactory
.getInstance(MetricsAssertHelper
.class);
163 public TestName name
= new TestName();
166 public void assertTColumnValuesEqual(List
<TColumnValue
> columnValuesA
,
167 List
<TColumnValue
> columnValuesB
) {
168 assertEquals(columnValuesA
.size(), columnValuesB
.size());
169 Comparator
<TColumnValue
> comparator
= new Comparator
<TColumnValue
>() {
171 public int compare(TColumnValue o1
, TColumnValue o2
) {
172 return Bytes
.compareTo(Bytes
.add(o1
.getFamily(), o1
.getQualifier()),
173 Bytes
.add(o2
.getFamily(), o2
.getQualifier()));
176 Collections
.sort(columnValuesA
, comparator
);
177 Collections
.sort(columnValuesB
, comparator
);
179 for (int i
= 0; i
< columnValuesA
.size(); i
++) {
180 TColumnValue a
= columnValuesA
.get(i
);
181 TColumnValue b
= columnValuesB
.get(i
);
182 assertTColumnValueEqual(a
, b
);
186 public void assertTColumnValueEqual(TColumnValue a
, TColumnValue b
) {
187 assertArrayEquals(a
.getFamily(), b
.getFamily());
188 assertArrayEquals(a
.getQualifier(), b
.getQualifier());
189 assertArrayEquals(a
.getValue(), b
.getValue());
193 public static void beforeClass() throws Exception
{
194 UTIL
.getConfiguration().set("hbase.client.retries.number", "3");
195 UTIL
.startMiniCluster();
196 TableDescriptorBuilder
.ModifyableTableDescriptor tableDescriptor
=
197 new TableDescriptorBuilder
.ModifyableTableDescriptor(TableName
.valueOf(tableAname
));
198 for (ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor family
: families
) {
199 tableDescriptor
.setColumnFamily(family
);
201 try (Admin admin
= UTIL
.getAdmin()) {
202 admin
.createTable(tableDescriptor
);
207 public static void afterClass() throws Exception
{
208 UTIL
.shutdownMiniCluster();
212 public void setup() throws Exception
{
216 private ThriftHBaseServiceHandler
createHandler() throws TException
{
218 Configuration conf
= UTIL
.getConfiguration();
219 return new ThriftHBaseServiceHandler(conf
, UserProvider
.instantiate(conf
));
220 } catch (IOException ie
) {
221 throw new TException(ie
);
226 public void testExists() throws TIOError
, TException
{
227 ThriftHBaseServiceHandler handler
= createHandler();
228 byte[] rowName
= Bytes
.toBytes("testExists");
229 ByteBuffer table
= wrap(tableAname
);
231 TGet get
= new TGet(wrap(rowName
));
232 assertFalse(handler
.exists(table
, get
));
234 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
235 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
236 columnValues
.add(new TColumnValue(wrap(familyBname
), wrap(qualifierBname
), wrap(valueBname
)));
237 TPut put
= new TPut(wrap(rowName
), columnValues
);
238 put
.setColumnValues(columnValues
);
240 handler
.put(table
, put
);
242 assertTrue(handler
.exists(table
, get
));
246 public void testExistsAll() throws TIOError
, TException
{
247 ThriftHBaseServiceHandler handler
= createHandler();
248 byte[] rowName1
= Bytes
.toBytes("testExistsAll1");
249 byte[] rowName2
= Bytes
.toBytes("testExistsAll2");
250 ByteBuffer table
= wrap(tableAname
);
252 List
<TGet
> gets
= new ArrayList
<>();
253 gets
.add(new TGet(wrap(rowName2
)));
254 gets
.add(new TGet(wrap(rowName2
)));
255 List
<Boolean
> existsResult1
= handler
.existsAll(table
, gets
);
256 assertFalse(existsResult1
.get(0));
257 assertFalse(existsResult1
.get(1));
259 List
<TColumnValue
> columnValues
= new ArrayList
<TColumnValue
>();
260 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
261 columnValues
.add(new TColumnValue(wrap(familyBname
), wrap(qualifierBname
), wrap(valueBname
)));
262 List
<TPut
> puts
= new ArrayList
<TPut
>();
263 puts
.add(new TPut(wrap(rowName1
), columnValues
));
264 puts
.add(new TPut(wrap(rowName2
), columnValues
));
266 handler
.putMultiple(table
, puts
);
267 List
<Boolean
> existsResult2
= handler
.existsAll(table
, gets
);
269 assertTrue(existsResult2
.get(0));
270 assertTrue(existsResult2
.get(1));
274 public void testPutGet() throws Exception
{
275 ThriftHBaseServiceHandler handler
= createHandler();
276 byte[] rowName
= Bytes
.toBytes("testPutGet");
277 ByteBuffer table
= wrap(tableAname
);
279 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
280 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
281 columnValues
.add(new TColumnValue(wrap(familyBname
), wrap(qualifierBname
), wrap(valueBname
)));
282 TPut put
= new TPut(wrap(rowName
), columnValues
);
284 put
.setColumnValues(columnValues
);
286 handler
.put(table
, put
);
288 TGet get
= new TGet(wrap(rowName
));
290 TResult result
= handler
.get(table
, get
);
291 assertArrayEquals(rowName
, result
.getRow());
292 List
<TColumnValue
> returnedColumnValues
= result
.getColumnValues();
293 assertTColumnValuesEqual(columnValues
, returnedColumnValues
);
297 public void testPutGetMultiple() throws Exception
{
298 ThriftHBaseServiceHandler handler
= createHandler();
299 ByteBuffer table
= wrap(tableAname
);
300 byte[] rowName1
= Bytes
.toBytes("testPutGetMultiple1");
301 byte[] rowName2
= Bytes
.toBytes("testPutGetMultiple2");
303 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
304 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
305 columnValues
.add(new TColumnValue(wrap(familyBname
), wrap(qualifierBname
), wrap(valueBname
)));
306 List
<TPut
> puts
= new ArrayList
<>(2);
307 puts
.add(new TPut(wrap(rowName1
), columnValues
));
308 puts
.add(new TPut(wrap(rowName2
), columnValues
));
310 handler
.putMultiple(table
, puts
);
312 List
<TGet
> gets
= new ArrayList
<>(2);
313 gets
.add(new TGet(wrap(rowName1
)));
314 gets
.add(new TGet(wrap(rowName2
)));
316 List
<TResult
> results
= handler
.getMultiple(table
, gets
);
317 assertEquals(2, results
.size());
319 assertArrayEquals(rowName1
, results
.get(0).getRow());
320 assertTColumnValuesEqual(columnValues
, results
.get(0).getColumnValues());
322 assertArrayEquals(rowName2
, results
.get(1).getRow());
323 assertTColumnValuesEqual(columnValues
, results
.get(1).getColumnValues());
327 public void testDeleteMultiple() throws Exception
{
328 ThriftHBaseServiceHandler handler
= createHandler();
329 ByteBuffer table
= wrap(tableAname
);
330 byte[] rowName1
= Bytes
.toBytes("testDeleteMultiple1");
331 byte[] rowName2
= Bytes
.toBytes("testDeleteMultiple2");
333 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
334 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
335 columnValues
.add(new TColumnValue(wrap(familyBname
), wrap(qualifierBname
), wrap(valueBname
)));
336 List
<TPut
> puts
= new ArrayList
<>(2);
337 puts
.add(new TPut(wrap(rowName1
), columnValues
));
338 puts
.add(new TPut(wrap(rowName2
), columnValues
));
340 handler
.putMultiple(table
, puts
);
342 List
<TDelete
> deletes
= new ArrayList
<>(2);
343 deletes
.add(new TDelete(wrap(rowName1
)));
344 deletes
.add(new TDelete(wrap(rowName2
)));
346 List
<TDelete
> deleteResults
= handler
.deleteMultiple(table
, deletes
);
347 // 0 means they were all successfully applies
348 assertEquals(0, deleteResults
.size());
350 assertFalse(handler
.exists(table
, new TGet(wrap(rowName1
))));
351 assertFalse(handler
.exists(table
, new TGet(wrap(rowName2
))));
355 public void testDelete() throws Exception
{
356 ThriftHBaseServiceHandler handler
= createHandler();
357 byte[] rowName
= Bytes
.toBytes("testDelete");
358 ByteBuffer table
= wrap(tableAname
);
360 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
361 TColumnValue columnValueA
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
363 TColumnValue columnValueB
= new TColumnValue(wrap(familyBname
), wrap(qualifierBname
),
365 columnValues
.add(columnValueA
);
366 columnValues
.add(columnValueB
);
367 TPut put
= new TPut(wrap(rowName
), columnValues
);
369 put
.setColumnValues(columnValues
);
371 handler
.put(table
, put
);
373 TDelete delete
= new TDelete(wrap(rowName
));
374 List
<TColumn
> deleteColumns
= new ArrayList
<>(1);
375 TColumn deleteColumn
= new TColumn(wrap(familyAname
));
376 deleteColumn
.setQualifier(qualifierAname
);
377 deleteColumns
.add(deleteColumn
);
378 delete
.setColumns(deleteColumns
);
380 handler
.deleteSingle(table
, delete
);
382 TGet get
= new TGet(wrap(rowName
));
383 TResult result
= handler
.get(table
, get
);
384 assertArrayEquals(rowName
, result
.getRow());
385 List
<TColumnValue
> returnedColumnValues
= result
.getColumnValues();
386 List
<TColumnValue
> expectedColumnValues
= new ArrayList
<>(1);
387 expectedColumnValues
.add(columnValueB
);
388 assertTColumnValuesEqual(expectedColumnValues
, returnedColumnValues
);
392 public void testDeleteAllTimestamps() throws Exception
{
393 ThriftHBaseServiceHandler handler
= createHandler();
394 byte[] rowName
= Bytes
.toBytes("testDeleteAllTimestamps");
395 ByteBuffer table
= wrap(tableAname
);
397 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
398 TColumnValue columnValueA
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
400 columnValueA
.setTimestamp(System
.currentTimeMillis() - 10);
401 columnValues
.add(columnValueA
);
402 TPut put
= new TPut(wrap(rowName
), columnValues
);
404 put
.setColumnValues(columnValues
);
406 handler
.put(table
, put
);
407 columnValueA
.setTimestamp(System
.currentTimeMillis());
408 handler
.put(table
, put
);
410 TGet get
= new TGet(wrap(rowName
));
411 get
.setMaxVersions(2);
412 TResult result
= handler
.get(table
, get
);
413 assertEquals(2, result
.getColumnValuesSize());
415 TDelete delete
= new TDelete(wrap(rowName
));
416 List
<TColumn
> deleteColumns
= new ArrayList
<>(1);
417 TColumn deleteColumn
= new TColumn(wrap(familyAname
));
418 deleteColumn
.setQualifier(qualifierAname
);
419 deleteColumns
.add(deleteColumn
);
420 delete
.setColumns(deleteColumns
);
421 delete
.setDeleteType(TDeleteType
.DELETE_COLUMNS
); // This is the default anyway.
423 handler
.deleteSingle(table
, delete
);
425 get
= new TGet(wrap(rowName
));
426 result
= handler
.get(table
, get
);
427 assertNull(result
.getRow());
428 assertEquals(0, result
.getColumnValuesSize());
432 public void testDeleteSingleTimestamp() throws Exception
{
433 ThriftHBaseServiceHandler handler
= createHandler();
434 byte[] rowName
= Bytes
.toBytes("testDeleteSingleTimestamp");
435 ByteBuffer table
= wrap(tableAname
);
437 long timestamp1
= System
.currentTimeMillis() - 10;
438 long timestamp2
= System
.currentTimeMillis();
440 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
441 TColumnValue columnValueA
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
443 columnValueA
.setTimestamp(timestamp1
);
444 columnValues
.add(columnValueA
);
445 TPut put
= new TPut(wrap(rowName
), columnValues
);
447 put
.setColumnValues(columnValues
);
449 handler
.put(table
, put
);
450 columnValueA
.setTimestamp(timestamp2
);
451 handler
.put(table
, put
);
453 TGet get
= new TGet(wrap(rowName
));
454 get
.setMaxVersions(2);
455 TResult result
= handler
.get(table
, get
);
456 assertEquals(2, result
.getColumnValuesSize());
458 TDelete delete
= new TDelete(wrap(rowName
));
459 List
<TColumn
> deleteColumns
= new ArrayList
<>(1);
460 TColumn deleteColumn
= new TColumn(wrap(familyAname
));
461 deleteColumn
.setQualifier(qualifierAname
);
462 deleteColumns
.add(deleteColumn
);
463 delete
.setColumns(deleteColumns
);
464 delete
.setDeleteType(TDeleteType
.DELETE_COLUMN
);
466 handler
.deleteSingle(table
, delete
);
468 get
= new TGet(wrap(rowName
));
469 result
= handler
.get(table
, get
);
470 assertArrayEquals(rowName
, result
.getRow());
471 assertEquals(1, result
.getColumnValuesSize());
472 // the older timestamp should remain.
473 assertEquals(timestamp1
, result
.getColumnValues().get(0).getTimestamp());
477 public void testDeleteFamily() throws Exception
{
478 ThriftHBaseServiceHandler handler
= createHandler();
479 byte[] rowName
= Bytes
.toBytes("testDeleteFamily");
480 ByteBuffer table
= wrap(tableAname
);
482 long timestamp1
= System
.currentTimeMillis() - 10;
483 long timestamp2
= System
.currentTimeMillis();
485 List
<TColumnValue
> columnValues
= new ArrayList
<>();
486 TColumnValue columnValueA
=
487 new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
));
488 columnValueA
.setTimestamp(timestamp1
);
489 columnValues
.add(columnValueA
);
490 TPut put
= new TPut(wrap(rowName
), columnValues
);
492 put
.setColumnValues(columnValues
);
494 handler
.put(table
, put
);
495 columnValueA
.setTimestamp(timestamp2
);
496 handler
.put(table
, put
);
498 TGet get
= new TGet(wrap(rowName
));
499 get
.setMaxVersions(2);
500 TResult result
= handler
.get(table
, get
);
501 assertEquals(2, result
.getColumnValuesSize());
503 TDelete delete
= new TDelete(wrap(rowName
));
504 List
<TColumn
> deleteColumns
= new ArrayList
<>();
505 TColumn deleteColumn
= new TColumn(wrap(familyAname
));
506 deleteColumns
.add(deleteColumn
);
507 delete
.setColumns(deleteColumns
);
508 delete
.setDeleteType(TDeleteType
.DELETE_FAMILY
);
510 handler
.deleteSingle(table
, delete
);
512 get
= new TGet(wrap(rowName
));
513 result
= handler
.get(table
, get
);
514 assertArrayEquals(null, result
.getRow());
515 assertEquals(0, result
.getColumnValuesSize());
519 public void testDeleteFamilyVersion() throws Exception
{
520 ThriftHBaseServiceHandler handler
= createHandler();
521 byte[] rowName
= Bytes
.toBytes("testDeleteFamilyVersion");
522 ByteBuffer table
= wrap(tableAname
);
524 long timestamp1
= System
.currentTimeMillis() - 10;
525 long timestamp2
= System
.currentTimeMillis();
527 List
<TColumnValue
> columnValues
= new ArrayList
<>();
528 TColumnValue columnValueA
=
529 new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
));
530 columnValueA
.setTimestamp(timestamp1
);
531 columnValues
.add(columnValueA
);
532 TPut put
= new TPut(wrap(rowName
), columnValues
);
534 put
.setColumnValues(columnValues
);
536 handler
.put(table
, put
);
537 columnValueA
.setTimestamp(timestamp2
);
538 handler
.put(table
, put
);
540 TGet get
= new TGet(wrap(rowName
));
541 get
.setMaxVersions(2);
542 TResult result
= handler
.get(table
, get
);
543 assertEquals(2, result
.getColumnValuesSize());
545 TDelete delete
= new TDelete(wrap(rowName
));
546 List
<TColumn
> deleteColumns
= new ArrayList
<>();
547 TColumn deleteColumn
= new TColumn(wrap(familyAname
));
548 deleteColumn
.setTimestamp(timestamp1
);
549 deleteColumns
.add(deleteColumn
);
550 delete
.setColumns(deleteColumns
);
551 delete
.setDeleteType(TDeleteType
.DELETE_FAMILY_VERSION
);
553 handler
.deleteSingle(table
, delete
);
555 get
= new TGet(wrap(rowName
));
556 result
= handler
.get(table
, get
);
557 assertArrayEquals(rowName
, result
.getRow());
558 assertEquals(1, result
.getColumnValuesSize());
559 assertEquals(timestamp2
, result
.getColumnValues().get(0).getTimestamp());
563 public void testIncrement() throws Exception
{
564 ThriftHBaseServiceHandler handler
= createHandler();
565 byte[] rowName
= Bytes
.toBytes("testIncrement");
566 ByteBuffer table
= wrap(tableAname
);
568 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
569 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
570 wrap(Bytes
.toBytes(1L))));
571 TPut put
= new TPut(wrap(rowName
), columnValues
);
572 put
.setColumnValues(columnValues
);
573 handler
.put(table
, put
);
575 List
<TColumnIncrement
> incrementColumns
= new ArrayList
<>(1);
576 incrementColumns
.add(new TColumnIncrement(wrap(familyAname
), wrap(qualifierAname
)));
577 TIncrement increment
= new TIncrement(wrap(rowName
), incrementColumns
);
578 handler
.increment(table
, increment
);
580 TGet get
= new TGet(wrap(rowName
));
581 TResult result
= handler
.get(table
, get
);
583 assertArrayEquals(rowName
, result
.getRow());
584 assertEquals(1, result
.getColumnValuesSize());
585 TColumnValue columnValue
= result
.getColumnValues().get(0);
586 assertArrayEquals(Bytes
.toBytes(2L), columnValue
.getValue());
590 public void testAppend() throws Exception
{
591 ThriftHBaseServiceHandler handler
= createHandler();
592 byte[] rowName
= Bytes
.toBytes("testAppend");
593 ByteBuffer table
= wrap(tableAname
);
594 byte[] v1
= Bytes
.toBytes("42");
595 byte[] v2
= Bytes
.toBytes("23");
596 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
597 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(v1
)));
598 TPut put
= new TPut(wrap(rowName
), columnValues
);
599 put
.setColumnValues(columnValues
);
600 handler
.put(table
, put
);
602 List
<TColumnValue
> appendColumns
= new ArrayList
<>(1);
603 appendColumns
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(v2
)));
604 TAppend append
= new TAppend(wrap(rowName
), appendColumns
);
605 handler
.append(table
, append
);
607 TGet get
= new TGet(wrap(rowName
));
608 TResult result
= handler
.get(table
, get
);
610 assertArrayEquals(rowName
, result
.getRow());
611 assertEquals(1, result
.getColumnValuesSize());
612 TColumnValue columnValue
= result
.getColumnValues().get(0);
613 assertArrayEquals(Bytes
.add(v1
, v2
), columnValue
.getValue());
617 * check that checkAndPut fails if the cell does not exist, then put in the cell, then check
618 * that the checkAndPut succeeds.
621 public void testCheckAndPut() throws Exception
{
622 ThriftHBaseServiceHandler handler
= createHandler();
623 byte[] rowName
= Bytes
.toBytes("testCheckAndPut");
624 ByteBuffer table
= wrap(tableAname
);
626 List
<TColumnValue
> columnValuesA
= new ArrayList
<>(1);
627 TColumnValue columnValueA
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
629 columnValuesA
.add(columnValueA
);
630 TPut putA
= new TPut(wrap(rowName
), columnValuesA
);
631 putA
.setColumnValues(columnValuesA
);
633 List
<TColumnValue
> columnValuesB
= new ArrayList
<>(1);
634 TColumnValue columnValueB
= new TColumnValue(wrap(familyBname
), wrap(qualifierBname
),
636 columnValuesB
.add(columnValueB
);
637 TPut putB
= new TPut(wrap(rowName
), columnValuesB
);
638 putB
.setColumnValues(columnValuesB
);
640 assertFalse(handler
.checkAndPut(table
, wrap(rowName
), wrap(familyAname
),
641 wrap(qualifierAname
), wrap(valueAname
), putB
));
643 TGet get
= new TGet(wrap(rowName
));
644 TResult result
= handler
.get(table
, get
);
645 assertEquals(0, result
.getColumnValuesSize());
647 handler
.put(table
, putA
);
649 assertTrue(handler
.checkAndPut(table
, wrap(rowName
), wrap(familyAname
),
650 wrap(qualifierAname
), wrap(valueAname
), putB
));
652 result
= handler
.get(table
, get
);
653 assertArrayEquals(rowName
, result
.getRow());
654 List
<TColumnValue
> returnedColumnValues
= result
.getColumnValues();
655 List
<TColumnValue
> expectedColumnValues
= new ArrayList
<>(2);
656 expectedColumnValues
.add(columnValueA
);
657 expectedColumnValues
.add(columnValueB
);
658 assertTColumnValuesEqual(expectedColumnValues
, returnedColumnValues
);
662 * check that checkAndDelete fails if the cell does not exist, then put in the cell, then
663 * check that the checkAndDelete succeeds.
666 public void testCheckAndDelete() throws Exception
{
667 ThriftHBaseServiceHandler handler
= createHandler();
668 byte[] rowName
= Bytes
.toBytes("testCheckAndDelete");
669 ByteBuffer table
= wrap(tableAname
);
671 List
<TColumnValue
> columnValuesA
= new ArrayList
<>(1);
672 TColumnValue columnValueA
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
674 columnValuesA
.add(columnValueA
);
675 TPut putA
= new TPut(wrap(rowName
), columnValuesA
);
676 putA
.setColumnValues(columnValuesA
);
678 List
<TColumnValue
> columnValuesB
= new ArrayList
<>(1);
679 TColumnValue columnValueB
= new TColumnValue(wrap(familyBname
), wrap(qualifierBname
),
681 columnValuesB
.add(columnValueB
);
682 TPut putB
= new TPut(wrap(rowName
), columnValuesB
);
683 putB
.setColumnValues(columnValuesB
);
685 // put putB so that we know whether the row has been deleted or not
686 handler
.put(table
, putB
);
688 TDelete delete
= new TDelete(wrap(rowName
));
690 assertFalse(handler
.checkAndDelete(table
, wrap(rowName
), wrap(familyAname
),
691 wrap(qualifierAname
), wrap(valueAname
), delete
));
693 TGet get
= new TGet(wrap(rowName
));
694 TResult result
= handler
.get(table
, get
);
695 assertArrayEquals(rowName
, result
.getRow());
696 assertTColumnValuesEqual(columnValuesB
, result
.getColumnValues());
698 handler
.put(table
, putA
);
700 assertTrue(handler
.checkAndDelete(table
, wrap(rowName
), wrap(familyAname
),
701 wrap(qualifierAname
), wrap(valueAname
), delete
));
703 result
= handler
.get(table
, get
);
704 assertFalse(result
.isSetRow());
705 assertEquals(0, result
.getColumnValuesSize());
709 public void testScan() throws Exception
{
710 ThriftHBaseServiceHandler handler
= createHandler();
711 ByteBuffer table
= wrap(tableAname
);
714 TColumnValue columnValue
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
716 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
717 columnValues
.add(columnValue
);
718 for (int i
= 0; i
< 10; i
++) {
719 TPut put
= new TPut(wrap(Bytes
.toBytes("testScan" + i
)), columnValues
);
720 handler
.put(table
, put
);
723 // create scan instance
724 TScan scan
= new TScan();
725 List
<TColumn
> columns
= new ArrayList
<>(1);
726 TColumn column
= new TColumn();
727 column
.setFamily(familyAname
);
728 column
.setQualifier(qualifierAname
);
730 scan
.setColumns(columns
);
731 scan
.setStartRow(Bytes
.toBytes("testScan"));
732 scan
.setStopRow(Bytes
.toBytes("testScan\uffff"));
734 // get scanner and rows
735 int scanId
= handler
.openScanner(table
, scan
);
736 List
<TResult
> results
= handler
.getScannerRows(scanId
, 10);
737 assertEquals(10, results
.size());
738 for (int i
= 0; i
< 10; i
++) {
739 // check if the rows are returned and in order
740 assertArrayEquals(Bytes
.toBytes("testScan" + i
), results
.get(i
).getRow());
743 // check that we are at the end of the scan
744 results
= handler
.getScannerRows(scanId
, 10);
745 assertEquals(0, results
.size());
747 // close scanner and check that it was indeed closed
748 handler
.closeScanner(scanId
);
750 handler
.getScannerRows(scanId
, 10);
751 fail("Scanner id should be invalid");
752 } catch (TIllegalArgument e
) {
757 * Tests keeping a HBase scanner alive for long periods of time. Each call to getScannerRow()
758 * should reset the ConnectionCache timeout for the scanner's connection.
761 public void testLongLivedScan() throws Exception
{
763 int trialPause
= 1000;
764 int cleanUpInterval
= 100;
765 Configuration conf
= new Configuration(UTIL
.getConfiguration());
766 // Set the ConnectionCache timeout to trigger halfway through the trials
767 conf
.setInt(MAX_IDLETIME
, (numTrials
/ 2) * trialPause
);
768 conf
.setInt(CLEANUP_INTERVAL
, cleanUpInterval
);
769 ThriftHBaseServiceHandler handler
= new ThriftHBaseServiceHandler(conf
,
770 UserProvider
.instantiate(conf
));
772 ByteBuffer table
= wrap(tableAname
);
774 TColumnValue columnValue
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
776 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
777 columnValues
.add(columnValue
);
778 for (int i
= 0; i
< numTrials
; i
++) {
779 TPut put
= new TPut(wrap(Bytes
.toBytes("testScan" + i
)), columnValues
);
780 handler
.put(table
, put
);
783 // create scan instance
784 TScan scan
= new TScan();
785 List
<TColumn
> columns
= new ArrayList
<>(1);
786 TColumn column
= new TColumn();
787 column
.setFamily(familyAname
);
788 column
.setQualifier(qualifierAname
);
790 scan
.setColumns(columns
);
791 scan
.setStartRow(Bytes
.toBytes("testScan"));
792 scan
.setStopRow(Bytes
.toBytes("testScan\uffff"));
793 // Prevent the scanner from caching results
796 // get scanner and rows
797 int scanId
= handler
.openScanner(table
, scan
);
798 for (int i
= 0; i
< numTrials
; i
++) {
799 // Make sure that the Scanner doesn't throw an exception after the ConnectionCache timeout
800 List
<TResult
> results
= handler
.getScannerRows(scanId
, 1);
801 assertArrayEquals(Bytes
.toBytes("testScan" + i
), results
.get(0).getRow());
802 Thread
.sleep(trialPause
);
807 public void testReverseScan() throws Exception
{
808 ThriftHBaseServiceHandler handler
= createHandler();
809 ByteBuffer table
= wrap(tableAname
);
812 TColumnValue columnValue
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
814 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
815 columnValues
.add(columnValue
);
816 for (int i
= 0; i
< 10; i
++) {
817 TPut put
= new TPut(wrap(Bytes
.toBytes("testReverseScan" + i
)), columnValues
);
818 handler
.put(table
, put
);
821 // create reverse scan instance
822 TScan scan
= new TScan();
823 scan
.setReversed(true);
824 List
<TColumn
> columns
= new ArrayList
<>(1);
825 TColumn column
= new TColumn();
826 column
.setFamily(familyAname
);
827 column
.setQualifier(qualifierAname
);
829 scan
.setColumns(columns
);
830 scan
.setStartRow(Bytes
.toBytes("testReverseScan\uffff"));
831 scan
.setStopRow(Bytes
.toBytes("testReverseScan"));
833 // get scanner and rows
834 int scanId
= handler
.openScanner(table
, scan
);
835 List
<TResult
> results
= handler
.getScannerRows(scanId
, 10);
836 assertEquals(10, results
.size());
837 for (int i
= 0; i
< 10; i
++) {
838 // check if the rows are returned and in order
839 assertArrayEquals(Bytes
.toBytes("testReverseScan" + (9 - i
)), results
.get(i
).getRow());
842 // check that we are at the end of the scan
843 results
= handler
.getScannerRows(scanId
, 10);
844 assertEquals(0, results
.size());
846 // close scanner and check that it was indeed closed
847 handler
.closeScanner(scanId
);
849 handler
.getScannerRows(scanId
, 10);
850 fail("Scanner id should be invalid");
851 } catch (TIllegalArgument e
) {
856 public void testScanWithFilter() throws Exception
{
857 ThriftHBaseServiceHandler handler
= createHandler();
858 ByteBuffer table
= wrap(tableAname
);
861 TColumnValue columnValue
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
863 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
864 columnValues
.add(columnValue
);
865 for (int i
= 0; i
< 10; i
++) {
866 TPut put
= new TPut(wrap(Bytes
.toBytes("testScanWithFilter" + i
)), columnValues
);
867 handler
.put(table
, put
);
870 // create scan instance with filter
871 TScan scan
= new TScan();
872 List
<TColumn
> columns
= new ArrayList
<>(1);
873 TColumn column
= new TColumn();
874 column
.setFamily(familyAname
);
875 column
.setQualifier(qualifierAname
);
877 scan
.setColumns(columns
);
878 scan
.setStartRow(Bytes
.toBytes("testScanWithFilter"));
879 scan
.setStopRow(Bytes
.toBytes("testScanWithFilter\uffff"));
880 // only get the key part
881 scan
.setFilterString(wrap(Bytes
.toBytes("KeyOnlyFilter()")));
883 // get scanner and rows
884 int scanId
= handler
.openScanner(table
, scan
);
885 List
<TResult
> results
= handler
.getScannerRows(scanId
, 10);
886 assertEquals(10, results
.size());
887 for (int i
= 0; i
< 10; i
++) {
888 // check if the rows are returned and in order
889 assertArrayEquals(Bytes
.toBytes("testScanWithFilter" + i
), results
.get(i
).getRow());
890 // check that the value is indeed stripped by the filter
891 assertEquals(0, results
.get(i
).getColumnValues().get(0).getValue().length
);
894 // check that we are at the end of the scan
895 results
= handler
.getScannerRows(scanId
, 10);
896 assertEquals(0, results
.size());
898 // close scanner and check that it was indeed closed
899 handler
.closeScanner(scanId
);
901 handler
.getScannerRows(scanId
, 10);
902 fail("Scanner id should be invalid");
903 } catch (TIllegalArgument e
) {
908 public void testScanWithColumnFamilyTimeRange() throws Exception
{
909 ThriftHBaseServiceHandler handler
= createHandler();
910 ByteBuffer table
= wrap(tableAname
);
913 TColumnValue familyAColumnValue
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
915 TColumnValue familyBColumnValue
= new TColumnValue(wrap(familyBname
), wrap(qualifierBname
),
917 long minTimestamp
= System
.currentTimeMillis();
918 for (int i
= 0; i
< 10; i
++) {
919 familyAColumnValue
.setTimestamp(minTimestamp
+ i
);
920 familyBColumnValue
.setTimestamp(minTimestamp
+ i
);
921 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
922 columnValues
.add(familyAColumnValue
);
923 columnValues
.add(familyBColumnValue
);
924 TPut put
= new TPut(wrap(Bytes
.toBytes("testScanWithColumnFamilyTimeRange" + i
)),
926 handler
.put(table
, put
);
929 // create scan instance with column family time range
930 TScan scan
= new TScan();
931 Map
<ByteBuffer
,TTimeRange
> colFamTimeRangeMap
= new HashMap
<>(2);
932 colFamTimeRangeMap
.put(wrap(familyAname
), new TTimeRange(minTimestamp
+ 3, minTimestamp
+ 5));
933 colFamTimeRangeMap
.put(wrap(familyBname
), new TTimeRange(minTimestamp
+ 6, minTimestamp
+ 9));
934 scan
.setColFamTimeRangeMap(colFamTimeRangeMap
);
936 // get scanner and rows
937 int scanId
= handler
.openScanner(table
, scan
);
938 List
<TResult
> results
= handler
.getScannerRows(scanId
, 5);
939 assertEquals(5, results
.size());
940 int familyACount
= 0;
941 int familyBCount
= 0;
942 for (TResult result
: results
) {
943 List
<TColumnValue
> columnValues
= result
.getColumnValues();
944 if (CollectionUtils
.isNotEmpty(columnValues
)) {
945 if (Bytes
.equals(familyAname
, columnValues
.get(0).getFamily())) {
947 } else if (Bytes
.equals(familyBname
, columnValues
.get(0).getFamily())) {
952 assertEquals(2, familyACount
);
953 assertEquals(3, familyBCount
);
955 // check that we are at the end of the scan
956 results
= handler
.getScannerRows(scanId
, 1);
957 assertEquals(0, results
.size());
959 // close scanner and check that it was indeed closed
960 handler
.closeScanner(scanId
);
962 handler
.getScannerRows(scanId
, 1);
963 fail("Scanner id should be invalid");
964 } catch (TIllegalArgument e
) {
969 public void testSmallScan() throws Exception
{
970 ThriftHBaseServiceHandler handler
= createHandler();
971 ByteBuffer table
= wrap(tableAname
);
974 TColumnValue columnValue
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
976 List
<TColumnValue
> columnValues
= new ArrayList
<>();
977 columnValues
.add(columnValue
);
978 for (int i
= 0; i
< 10; i
++) {
979 TPut put
= new TPut(wrap(Bytes
.toBytes("testSmallScan" + i
)), columnValues
);
980 handler
.put(table
, put
);
983 // small scan instance
984 TScan scan
= new TScan();
985 scan
.setStartRow(Bytes
.toBytes("testSmallScan"));
986 scan
.setStopRow(Bytes
.toBytes("testSmallScan\uffff"));
987 scan
.setReadType(TReadType
.PREAD
);
990 // get scanner and rows
991 int scanId
= handler
.openScanner(table
, scan
);
992 List
<TResult
> results
= handler
.getScannerRows(scanId
, 10);
993 assertEquals(10, results
.size());
994 for (int i
= 0; i
< 10; i
++) {
995 // check if the rows are returned and in order
996 assertArrayEquals(Bytes
.toBytes("testSmallScan" + i
), results
.get(i
).getRow());
999 // check that we are at the end of the scan
1000 results
= handler
.getScannerRows(scanId
, 10);
1001 assertEquals(0, results
.size());
1003 // close scanner and check that it was indeed closed
1004 handler
.closeScanner(scanId
);
1006 handler
.getScannerRows(scanId
, 10);
1007 fail("Scanner id should be invalid");
1008 } catch (TIllegalArgument e
) {
1013 public void testPutTTL() throws Exception
{
1014 ThriftHBaseServiceHandler handler
= createHandler();
1015 byte[] rowName
= Bytes
.toBytes("testPutTTL");
1016 ByteBuffer table
= wrap(tableAname
);
1017 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
1019 // Add some dummy data
1023 wrap(qualifierAname
),
1024 wrap(Bytes
.toBytes(1L))));
1027 TPut put
= new TPut(wrap(rowName
), columnValues
);
1028 put
.setColumnValues(columnValues
);
1030 Map
<ByteBuffer
, ByteBuffer
> attributes
= new HashMap
<>();
1032 // Time in ms for the kv's to live.
1033 long ttlTimeMs
= 2000L;
1035 // the _ttl attribute is a number of ms ttl for key values in this put.
1036 attributes
.put(wrap(Bytes
.toBytes("_ttl")), wrap(Bytes
.toBytes(ttlTimeMs
)));
1037 // Attach the attributes
1038 put
.setAttributes(attributes
);
1040 handler
.put(table
, put
);
1042 // Now get the data back
1043 TGet getOne
= new TGet(wrap(rowName
));
1044 TResult resultOne
= handler
.get(table
, getOne
);
1047 assertArrayEquals(rowName
, resultOne
.getRow());
1048 assertEquals(1, resultOne
.getColumnValuesSize());
1050 // Sleep 30 seconds just to make 100% sure that the key value should be expired.
1051 Thread
.sleep(ttlTimeMs
* 15);
1053 TGet getTwo
= new TGet(wrap(rowName
));
1054 TResult resultTwo
= handler
.get(table
, getTwo
);
1057 // Nothing should be there since it's ttl'd out.
1058 assertNull(resultTwo
.getRow());
1059 assertEquals(0, resultTwo
.getColumnValuesSize());
1063 * Padding numbers to make comparison of sort order easier in a for loop
1065 * @param n The number to pad.
1066 * @param pad The length to pad up to.
1067 * @return The padded number as a string.
1069 private String
pad(int n
, byte pad
) {
1070 String res
= Integer
.toString(n
);
1072 while (res
.length() < pad
) {
1080 public void testScanWithBatchSize() throws Exception
{
1081 ThriftHBaseServiceHandler handler
= createHandler();
1082 ByteBuffer table
= wrap(tableAname
);
1085 List
<TColumnValue
> columnValues
= new ArrayList
<>(100);
1086 for (int i
= 0; i
< 100; i
++) {
1087 String colNum
= pad(i
, (byte) 3);
1088 TColumnValue columnValue
= new TColumnValue(wrap(familyAname
),
1089 wrap(Bytes
.toBytes("col" + colNum
)), wrap(Bytes
.toBytes("val" + colNum
)));
1090 columnValues
.add(columnValue
);
1092 TPut put
= new TPut(wrap(Bytes
.toBytes("testScanWithBatchSize")), columnValues
);
1093 handler
.put(table
, put
);
1095 // create scan instance
1096 TScan scan
= new TScan();
1097 List
<TColumn
> columns
= new ArrayList
<>(1);
1098 TColumn column
= new TColumn();
1099 column
.setFamily(familyAname
);
1100 columns
.add(column
);
1101 scan
.setColumns(columns
);
1102 scan
.setStartRow(Bytes
.toBytes("testScanWithBatchSize"));
1103 scan
.setStopRow(Bytes
.toBytes("testScanWithBatchSize\uffff"));
1104 // set batch size to 10 columns per call
1105 scan
.setBatchSize(10);
1108 int scanId
= handler
.openScanner(table
, scan
);
1109 List
<TResult
> results
= null;
1110 for (int i
= 0; i
< 10; i
++) {
1111 // get batch for single row (10x10 is what we expect)
1112 results
= handler
.getScannerRows(scanId
, 1);
1113 assertEquals(1, results
.size());
1114 // check length of batch
1115 List
<TColumnValue
> cols
= results
.get(0).getColumnValues();
1116 assertEquals(10, cols
.size());
1117 // check if the columns are returned and in order
1118 for (int y
= 0; y
< 10; y
++) {
1119 int colNum
= y
+ (10 * i
);
1120 String colNumPad
= pad(colNum
, (byte) 3);
1121 assertArrayEquals(Bytes
.toBytes("col" + colNumPad
), cols
.get(y
).getQualifier());
1125 // check that we are at the end of the scan
1126 results
= handler
.getScannerRows(scanId
, 1);
1127 assertEquals(0, results
.size());
1129 // close scanner and check that it was indeed closed
1130 handler
.closeScanner(scanId
);
1132 handler
.getScannerRows(scanId
, 1);
1133 fail("Scanner id should be invalid");
1134 } catch (TIllegalArgument e
) {
1139 public void testGetScannerResults() throws Exception
{
1140 ThriftHBaseServiceHandler handler
= createHandler();
1141 ByteBuffer table
= wrap(tableAname
);
1144 TColumnValue columnValue
=
1145 new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
));
1146 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
1147 columnValues
.add(columnValue
);
1148 for (int i
= 0; i
< 20; i
++) {
1150 new TPut(wrap(Bytes
.toBytes("testGetScannerResults" + pad(i
, (byte) 2))), columnValues
);
1151 handler
.put(table
, put
);
1154 // create scan instance
1155 TScan scan
= new TScan();
1156 List
<TColumn
> columns
= new ArrayList
<>(1);
1157 TColumn column
= new TColumn();
1158 column
.setFamily(familyAname
);
1159 column
.setQualifier(qualifierAname
);
1160 columns
.add(column
);
1161 scan
.setColumns(columns
);
1162 scan
.setStartRow(Bytes
.toBytes("testGetScannerResults"));
1164 // get 5 rows and check the returned results
1165 scan
.setStopRow(Bytes
.toBytes("testGetScannerResults05"));
1166 List
<TResult
> results
= handler
.getScannerResults(table
, scan
, 5);
1167 assertEquals(5, results
.size());
1168 for (int i
= 0; i
< 5; i
++) {
1169 // check if the rows are returned and in order
1170 assertArrayEquals(Bytes
.toBytes("testGetScannerResults" + pad(i
, (byte) 2)), results
.get(i
)
1174 // get 10 rows and check the returned results
1175 scan
.setStopRow(Bytes
.toBytes("testGetScannerResults10"));
1176 results
= handler
.getScannerResults(table
, scan
, 10);
1177 assertEquals(10, results
.size());
1178 for (int i
= 0; i
< 10; i
++) {
1179 // check if the rows are returned and in order
1180 assertArrayEquals(Bytes
.toBytes("testGetScannerResults" + pad(i
, (byte) 2)), results
.get(i
)
1184 // get 20 rows and check the returned results
1185 scan
.setStopRow(Bytes
.toBytes("testGetScannerResults20"));
1186 results
= handler
.getScannerResults(table
, scan
, 20);
1187 assertEquals(20, results
.size());
1188 for (int i
= 0; i
< 20; i
++) {
1189 // check if the rows are returned and in order
1190 assertArrayEquals(Bytes
.toBytes("testGetScannerResults" + pad(i
, (byte) 2)), results
.get(i
)
1196 scan
.setColumns(columns
);
1197 scan
.setReversed(true);
1198 scan
.setStartRow(Bytes
.toBytes("testGetScannerResults20"));
1199 scan
.setStopRow(Bytes
.toBytes("testGetScannerResults"));
1200 results
= handler
.getScannerResults(table
, scan
, 20);
1201 assertEquals(20, results
.size());
1202 for (int i
= 0; i
< 20; i
++) {
1203 // check if the rows are returned and in order
1204 assertArrayEquals(Bytes
.toBytes("testGetScannerResults" + pad(19 - i
, (byte) 2)),
1205 results
.get(i
).getRow());
1210 public void testFilterRegistration() throws Exception
{
1211 Configuration conf
= UTIL
.getConfiguration();
1212 conf
.set("hbase.thrift.filters", "MyFilter:filterclass");
1213 ThriftServer
.registerFilters(conf
);
1214 Map
<String
, String
> registeredFilters
= ParseFilter
.getAllFilters();
1215 assertEquals("filterclass", registeredFilters
.get("MyFilter"));
1219 public void testMetrics() throws Exception
{
1220 Configuration conf
= UTIL
.getConfiguration();
1221 ThriftMetrics metrics
= getMetrics(conf
);
1222 ThriftHBaseServiceHandler hbaseHandler
= createHandler();
1223 THBaseService
.Iface handler
=
1224 HbaseHandlerMetricsProxy
.newInstance(hbaseHandler
, metrics
, conf
);
1225 byte[] rowName
= Bytes
.toBytes("testMetrics");
1226 ByteBuffer table
= wrap(tableAname
);
1228 TGet get
= new TGet(wrap(rowName
));
1229 assertFalse(handler
.exists(table
, get
));
1231 List
<TColumnValue
> columnValues
= new ArrayList
<>(2);
1232 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
1233 columnValues
.add(new TColumnValue(wrap(familyBname
), wrap(qualifierBname
), wrap(valueBname
)));
1234 TPut put
= new TPut(wrap(rowName
), columnValues
);
1235 put
.setColumnValues(columnValues
);
1237 handler
.put(table
, put
);
1239 assertTrue(handler
.exists(table
, get
));
1240 metricsHelper
.assertCounter("put_num_ops", 1, metrics
.getSource());
1241 metricsHelper
.assertCounter("exists_num_ops", 2, metrics
.getSource());
1244 private static ThriftMetrics
getMetrics(Configuration conf
) throws Exception
{
1245 ThriftMetrics m
= new ThriftMetrics(conf
, ThriftMetrics
.ThriftServerType
.TWO
);
1246 m
.getSource().init(); //Clear all the metrics
1251 public void testMetricsWithException() throws Exception
{
1252 byte[] rowkey
= Bytes
.toBytes("row1");
1253 byte[] family
= Bytes
.toBytes("f");
1254 byte[] col
= Bytes
.toBytes("c");
1255 // create a table which will throw exceptions for requests
1256 TableName tableName
= TableName
.valueOf(name
.getMethodName());
1257 TableDescriptorBuilder
.ModifyableTableDescriptor tableDesc
=
1258 new TableDescriptorBuilder
.ModifyableTableDescriptor(tableName
);
1259 tableDesc
.setCoprocessor(ErrorThrowingGetObserver
.class.getName());
1260 tableDesc
.setColumnFamily(
1261 new ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor(family
));
1263 Table table
= UTIL
.createTable(tableDesc
, null);
1264 table
.put(new Put(rowkey
).addColumn(family
, col
, Bytes
.toBytes("val1")));
1266 ThriftHBaseServiceHandler hbaseHandler
= createHandler();
1267 ThriftMetrics metrics
= getMetrics(UTIL
.getConfiguration());
1268 THBaseService
.Iface handler
=
1269 HbaseHandlerMetricsProxy
.newInstance(hbaseHandler
, metrics
, null);
1270 ByteBuffer tTableName
= wrap(tableName
.getName());
1272 // check metrics increment with a successful get
1273 long preGetCounter
= metricsHelper
.checkCounterExists("get_num_ops", metrics
.getSource()) ?
1274 metricsHelper
.getCounter("get_num_ops", metrics
.getSource()) :
1276 TGet tGet
= new TGet(wrap(rowkey
));
1277 TResult tResult
= handler
.get(tTableName
, tGet
);
1279 List
<TColumnValue
> expectedColumnValues
= Lists
.newArrayList(
1280 new TColumnValue(wrap(family
), wrap(col
), wrap(Bytes
.toBytes("val1")))
1282 assertArrayEquals(rowkey
, tResult
.getRow());
1283 List
<TColumnValue
> returnedColumnValues
= tResult
.getColumnValues();
1284 assertTColumnValuesEqual(expectedColumnValues
, returnedColumnValues
);
1286 metricsHelper
.assertCounter("get_num_ops", preGetCounter
+ 1, metrics
.getSource());
1288 // check metrics increment when the get throws each exception type
1289 for (ErrorThrowingGetObserver
.ErrorType type
: ErrorThrowingGetObserver
.ErrorType
.values()) {
1290 testExceptionType(handler
, metrics
, tTableName
, rowkey
, type
);
1294 private void testExceptionType(THBaseService
.Iface handler
, ThriftMetrics metrics
,
1295 ByteBuffer tTableName
, byte[] rowkey
, ErrorThrowingGetObserver
.ErrorType errorType
) {
1296 long preGetCounter
= metricsHelper
.getCounter("get_num_ops", metrics
.getSource());
1297 String exceptionKey
= errorType
.getMetricName();
1298 long preExceptionCounter
= metricsHelper
.checkCounterExists(exceptionKey
, metrics
.getSource()) ?
1299 metricsHelper
.getCounter(exceptionKey
, metrics
.getSource()) :
1301 TGet tGet
= new TGet(wrap(rowkey
));
1302 Map
<ByteBuffer
, ByteBuffer
> attributes
= new HashMap
<>();
1303 attributes
.put(wrap(Bytes
.toBytes(ErrorThrowingGetObserver
.SHOULD_ERROR_ATTRIBUTE
)),
1304 wrap(Bytes
.toBytes(errorType
.name())));
1305 tGet
.setAttributes(attributes
);
1307 TResult tResult
= handler
.get(tTableName
, tGet
);
1308 fail("Get with error attribute should have thrown an exception");
1309 } catch (TException e
) {
1310 LOG
.info("Received exception: ", e
);
1311 metricsHelper
.assertCounter("get_num_ops", preGetCounter
+ 1, metrics
.getSource());
1312 metricsHelper
.assertCounter(exceptionKey
, preExceptionCounter
+ 1, metrics
.getSource());
1320 * Latency metrics were capped at ~ 2 seconds due to the use of an int variable to capture the
1324 public void testMetricsPrecision() throws Exception
{
1325 byte[] rowkey
= Bytes
.toBytes("row1");
1326 byte[] family
= Bytes
.toBytes("f");
1327 byte[] col
= Bytes
.toBytes("c");
1328 // create a table which will throw exceptions for requests
1329 TableName tableName
= TableName
.valueOf("testMetricsPrecision");
1330 TableDescriptorBuilder
.ModifyableTableDescriptor tableDescriptor
=
1331 new TableDescriptorBuilder
.ModifyableTableDescriptor(tableName
);
1332 tableDescriptor
.setCoprocessor(DelayingRegionObserver
.class.getName());
1333 tableDescriptor
.setColumnFamily(
1334 new ColumnFamilyDescriptorBuilder
.ModifyableColumnFamilyDescriptor(family
));
1338 table
= UTIL
.createTable(tableDescriptor
, null);
1340 table
.put(new Put(rowkey
).addColumn(family
, col
, Bytes
.toBytes("val1")));
1342 ThriftHBaseServiceHandler hbaseHandler
= createHandler();
1343 ThriftMetrics metrics
= getMetrics(UTIL
.getConfiguration());
1344 THBaseService
.Iface handler
=
1345 HbaseHandlerMetricsProxy
.newInstance(hbaseHandler
, metrics
, null);
1346 ByteBuffer tTableName
= wrap(tableName
.getName());
1348 // check metrics latency with a successful get
1349 TGet tGet
= new TGet(wrap(rowkey
));
1350 TResult tResult
= handler
.get(tTableName
, tGet
);
1352 List
<TColumnValue
> expectedColumnValues
= Lists
.newArrayList(
1353 new TColumnValue(wrap(family
), wrap(col
), wrap(Bytes
.toBytes("val1")))
1355 assertArrayEquals(rowkey
, tResult
.getRow());
1356 List
<TColumnValue
> returnedColumnValues
= tResult
.getColumnValues();
1357 assertTColumnValuesEqual(expectedColumnValues
, returnedColumnValues
);
1359 metricsHelper
.assertGaugeGt("get_max", 3000L, metrics
.getSource());
1361 if (table
!= null) {
1364 } catch (IOException ignored
) {
1366 UTIL
.deleteTable(tableName
);
1373 public void testAttribute() throws Exception
{
1374 byte[] rowName
= Bytes
.toBytes("testAttribute");
1375 byte[] attributeKey
= Bytes
.toBytes("attribute1");
1376 byte[] attributeValue
= Bytes
.toBytes("value1");
1377 Map
<ByteBuffer
, ByteBuffer
> attributes
= new HashMap
<>();
1378 attributes
.put(wrap(attributeKey
), wrap(attributeValue
));
1380 TGet tGet
= new TGet(wrap(rowName
));
1381 tGet
.setAttributes(attributes
);
1382 Get get
= getFromThrift(tGet
);
1383 assertArrayEquals(get
.getAttribute("attribute1"), attributeValue
);
1385 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
1386 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
1387 TPut tPut
= new TPut(wrap(rowName
) , columnValues
);
1388 tPut
.setAttributes(attributes
);
1389 Put put
= putFromThrift(tPut
);
1390 assertArrayEquals(put
.getAttribute("attribute1"), attributeValue
);
1392 TScan tScan
= new TScan();
1393 tScan
.setAttributes(attributes
);
1394 Scan scan
= scanFromThrift(tScan
);
1395 assertArrayEquals(scan
.getAttribute("attribute1"), attributeValue
);
1397 List
<TColumnIncrement
> incrementColumns
= new ArrayList
<>(1);
1398 incrementColumns
.add(new TColumnIncrement(wrap(familyAname
), wrap(qualifierAname
)));
1399 TIncrement tIncrement
= new TIncrement(wrap(rowName
), incrementColumns
);
1400 tIncrement
.setAttributes(attributes
);
1401 Increment increment
= incrementFromThrift(tIncrement
);
1402 assertArrayEquals(increment
.getAttribute("attribute1"), attributeValue
);
1404 TDelete tDelete
= new TDelete(wrap(rowName
));
1405 tDelete
.setAttributes(attributes
);
1406 Delete delete
= deleteFromThrift(tDelete
);
1407 assertArrayEquals(delete
.getAttribute("attribute1"), attributeValue
);
1411 * Put valueA to a row, make sure put has happened, then create a mutation object to put valueB
1412 * and delete ValueA, then check that the row value is only valueB.
1415 public void testMutateRow() throws Exception
{
1416 ThriftHBaseServiceHandler handler
= createHandler();
1417 byte[] rowName
= Bytes
.toBytes("testMutateRow");
1418 ByteBuffer table
= wrap(tableAname
);
1420 List
<TColumnValue
> columnValuesA
= new ArrayList
<>(1);
1421 TColumnValue columnValueA
= new TColumnValue(wrap(familyAname
), wrap(qualifierAname
),
1423 columnValuesA
.add(columnValueA
);
1424 TPut putA
= new TPut(wrap(rowName
), columnValuesA
);
1425 putA
.setColumnValues(columnValuesA
);
1427 handler
.put(table
,putA
);
1429 TGet get
= new TGet(wrap(rowName
));
1430 TResult result
= handler
.get(table
, get
);
1431 assertArrayEquals(rowName
, result
.getRow());
1432 List
<TColumnValue
> returnedColumnValues
= result
.getColumnValues();
1434 List
<TColumnValue
> expectedColumnValues
= new ArrayList
<>(1);
1435 expectedColumnValues
.add(columnValueA
);
1436 assertTColumnValuesEqual(expectedColumnValues
, returnedColumnValues
);
1438 List
<TColumnValue
> columnValuesB
= new ArrayList
<>(1);
1439 TColumnValue columnValueB
= new TColumnValue(wrap(familyAname
), wrap(qualifierBname
),
1441 columnValuesB
.add(columnValueB
);
1442 TPut putB
= new TPut(wrap(rowName
), columnValuesB
);
1443 putB
.setColumnValues(columnValuesB
);
1445 TDelete delete
= new TDelete(wrap(rowName
));
1446 List
<TColumn
> deleteColumns
= new ArrayList
<>(1);
1447 TColumn deleteColumn
= new TColumn(wrap(familyAname
));
1448 deleteColumn
.setQualifier(qualifierAname
);
1449 deleteColumns
.add(deleteColumn
);
1450 delete
.setColumns(deleteColumns
);
1452 List
<TMutation
> mutations
= new ArrayList
<>(2);
1453 TMutation mutationA
= TMutation
.put(putB
);
1454 mutations
.add(mutationA
);
1456 TMutation mutationB
= TMutation
.deleteSingle(delete
);
1457 mutations
.add(mutationB
);
1459 TRowMutations tRowMutations
= new TRowMutations(wrap(rowName
),mutations
);
1460 handler
.mutateRow(table
,tRowMutations
);
1462 result
= handler
.get(table
, get
);
1463 assertArrayEquals(rowName
, result
.getRow());
1464 returnedColumnValues
= result
.getColumnValues();
1466 expectedColumnValues
= new ArrayList
<>(1);
1467 expectedColumnValues
.add(columnValueB
);
1468 assertTColumnValuesEqual(expectedColumnValues
, returnedColumnValues
);
1472 * Create TPut, TDelete , TIncrement objects, set durability then call ThriftUtility
1473 * functions to get Put , Delete and Increment respectively. Use getDurability to make sure
1474 * the returned objects have the appropriate durability setting.
1477 public void testDurability() throws Exception
{
1478 byte[] rowName
= Bytes
.toBytes("testDurability");
1479 List
<TColumnValue
> columnValues
= new ArrayList
<>(1);
1480 columnValues
.add(new TColumnValue(wrap(familyAname
), wrap(qualifierAname
), wrap(valueAname
)));
1482 List
<TColumnIncrement
> incrementColumns
= new ArrayList
<>(1);
1483 incrementColumns
.add(new TColumnIncrement(wrap(familyAname
), wrap(qualifierAname
)));
1485 TDelete tDelete
= new TDelete(wrap(rowName
));
1486 tDelete
.setDurability(TDurability
.SKIP_WAL
);
1487 Delete delete
= deleteFromThrift(tDelete
);
1488 assertEquals(Durability
.SKIP_WAL
, delete
.getDurability());
1490 tDelete
.setDurability(TDurability
.ASYNC_WAL
);
1491 delete
= deleteFromThrift(tDelete
);
1492 assertEquals(Durability
.ASYNC_WAL
, delete
.getDurability());
1494 tDelete
.setDurability(TDurability
.SYNC_WAL
);
1495 delete
= deleteFromThrift(tDelete
);
1496 assertEquals(Durability
.SYNC_WAL
, delete
.getDurability());
1498 tDelete
.setDurability(TDurability
.FSYNC_WAL
);
1499 delete
= deleteFromThrift(tDelete
);
1500 assertEquals(Durability
.FSYNC_WAL
, delete
.getDurability());
1502 TPut tPut
= new TPut(wrap(rowName
), columnValues
);
1503 tPut
.setDurability(TDurability
.SKIP_WAL
);
1504 Put put
= putFromThrift(tPut
);
1505 assertEquals(Durability
.SKIP_WAL
, put
.getDurability());
1507 tPut
.setDurability(TDurability
.ASYNC_WAL
);
1508 put
= putFromThrift(tPut
);
1509 assertEquals(Durability
.ASYNC_WAL
, put
.getDurability());
1511 tPut
.setDurability(TDurability
.SYNC_WAL
);
1512 put
= putFromThrift(tPut
);
1513 assertEquals(Durability
.SYNC_WAL
, put
.getDurability());
1515 tPut
.setDurability(TDurability
.FSYNC_WAL
);
1516 put
= putFromThrift(tPut
);
1517 assertEquals(Durability
.FSYNC_WAL
, put
.getDurability());
1519 TIncrement tIncrement
= new TIncrement(wrap(rowName
), incrementColumns
);
1521 tIncrement
.setDurability(TDurability
.SKIP_WAL
);
1522 Increment increment
= incrementFromThrift(tIncrement
);
1523 assertEquals(Durability
.SKIP_WAL
, increment
.getDurability());
1525 tIncrement
.setDurability(TDurability
.ASYNC_WAL
);
1526 increment
= incrementFromThrift(tIncrement
);
1527 assertEquals(Durability
.ASYNC_WAL
, increment
.getDurability());
1529 tIncrement
.setDurability(TDurability
.SYNC_WAL
);
1530 increment
= incrementFromThrift(tIncrement
);
1531 assertEquals(Durability
.SYNC_WAL
, increment
.getDurability());
1533 tIncrement
.setDurability(TDurability
.FSYNC_WAL
);
1534 increment
= incrementFromThrift(tIncrement
);
1535 assertEquals(Durability
.FSYNC_WAL
, increment
.getDurability());
1539 public void testCheckAndMutate() throws Exception
{
1540 ThriftHBaseServiceHandler handler
= createHandler();
1541 ByteBuffer table
= wrap(tableAname
);
1542 ByteBuffer row
= wrap(Bytes
.toBytes("row"));
1543 ByteBuffer family
= wrap(familyAname
);
1544 ByteBuffer qualifier
= wrap(qualifierAname
);
1545 ByteBuffer value
= wrap(valueAname
);
1547 // Create a mutation to write to 'B', our "mutate" of "checkAndMutate"
1548 List
<TColumnValue
> columnValuesB
= new ArrayList
<>(1);
1549 TColumnValue columnValueB
= new TColumnValue(family
, wrap(qualifierBname
), wrap(valueBname
));
1550 columnValuesB
.add(columnValueB
);
1551 TPut putB
= new TPut(row
, columnValuesB
);
1552 putB
.setColumnValues(columnValuesB
);
1554 TRowMutations tRowMutations
= new TRowMutations(row
,
1555 Arrays
.<TMutation
> asList(TMutation
.put(putB
)));
1557 // Empty table when we begin
1558 TResult result
= handler
.get(table
, new TGet(row
));
1559 assertEquals(0, result
.getColumnValuesSize());
1561 // checkAndMutate -- condition should fail because the value doesn't exist.
1562 assertFalse("Expected condition to not pass",
1563 handler
.checkAndMutate(table
, row
, family
, qualifier
, TCompareOperator
.EQUAL
, value
,
1566 List
<TColumnValue
> columnValuesA
= new ArrayList
<>(1);
1567 TColumnValue columnValueA
= new TColumnValue(family
, qualifier
, value
);
1568 columnValuesA
.add(columnValueA
);
1570 // Put an update 'A'
1571 handler
.put(table
, new TPut(row
, columnValuesA
));
1573 // Verify that the update is there
1574 result
= handler
.get(table
, new TGet(row
));
1575 assertEquals(1, result
.getColumnValuesSize());
1576 assertTColumnValueEqual(columnValueA
, result
.getColumnValues().get(0));
1578 // checkAndMutate -- condition should pass since we added the value
1579 assertTrue("Expected condition to pass",
1580 handler
.checkAndMutate(table
, row
, family
, qualifier
, TCompareOperator
.EQUAL
, value
,
1583 result
= handler
.get(table
, new TGet(row
));
1584 assertEquals(2, result
.getColumnValuesSize());
1585 assertTColumnValueEqual(columnValueA
, result
.getColumnValues().get(0));
1586 assertTColumnValueEqual(columnValueB
, result
.getColumnValues().get(1));
1590 public void testConsistency() throws Exception
{
1591 byte[] rowName
= Bytes
.toBytes("testConsistency");
1592 TGet tGet
= new TGet(wrap(rowName
));
1593 tGet
.setConsistency(TConsistency
.STRONG
);
1594 Get get
= getFromThrift(tGet
);
1595 assertEquals(Consistency
.STRONG
, get
.getConsistency());
1597 tGet
.setConsistency(TConsistency
.TIMELINE
);
1598 tGet
.setTargetReplicaId(1);
1599 get
= getFromThrift(tGet
);
1600 assertEquals(Consistency
.TIMELINE
, get
.getConsistency());
1601 assertEquals(1, get
.getReplicaId());
1603 TScan tScan
= new TScan();
1604 tScan
.setConsistency(TConsistency
.STRONG
);
1605 Scan scan
= scanFromThrift(tScan
);
1606 assertEquals(Consistency
.STRONG
, scan
.getConsistency());
1608 tScan
.setConsistency(TConsistency
.TIMELINE
);
1609 tScan
.setTargetReplicaId(1);
1610 scan
= scanFromThrift(tScan
);
1611 assertEquals(Consistency
.TIMELINE
, scan
.getConsistency());
1612 assertEquals(1, scan
.getReplicaId());
1614 TResult tResult
= new TResult();
1615 assertFalse(tResult
.isSetStale());
1616 tResult
.setStale(true);
1617 assertTrue(tResult
.isSetStale());
1621 public void testDDLOpertions() throws Exception
{
1622 String namespace
= "testDDLOpertionsNamespace";
1623 String table
= "testDDLOpertionsTable";
1624 TTableName tTableName
= new TTableName();
1625 tTableName
.setNs(Bytes
.toBytes(namespace
));
1626 tTableName
.setQualifier(Bytes
.toBytes(table
));
1627 ThriftHBaseServiceHandler handler
= createHandler();
1629 TNamespaceDescriptor namespaceDescriptor
= new TNamespaceDescriptor();
1630 namespaceDescriptor
.setName(namespace
);
1631 namespaceDescriptor
.putToConfiguration("key1", "value1");
1632 namespaceDescriptor
.putToConfiguration("key2", "value2");
1633 handler
.createNamespace(namespaceDescriptor
);
1635 List
<TNamespaceDescriptor
> namespaceDescriptors
= handler
.listNamespaceDescriptors();
1636 // should have 3 namespace, default hbase and testDDLOpertionsNamespace
1637 assertTrue(namespaceDescriptors
.size() == 3);
1639 namespaceDescriptor
.putToConfiguration("kye3", "value3");
1640 handler
.modifyNamespace(namespaceDescriptor
);
1642 TNamespaceDescriptor namespaceDescriptorReturned
= handler
.getNamespaceDescriptor(namespace
);
1643 assertTrue(namespaceDescriptorReturned
.getConfiguration().size() == 3);
1645 TTableDescriptor tableDescriptor
= new TTableDescriptor();
1646 tableDescriptor
.setTableName(tTableName
);
1647 TColumnFamilyDescriptor columnFamilyDescriptor1
= new TColumnFamilyDescriptor();
1648 columnFamilyDescriptor1
.setName(familyAname
);
1649 columnFamilyDescriptor1
.setDataBlockEncoding(TDataBlockEncoding
.DIFF
);
1650 tableDescriptor
.addToColumns(columnFamilyDescriptor1
);
1651 List
<ByteBuffer
> splitKeys
= new ArrayList
<>();
1652 splitKeys
.add(ByteBuffer
.wrap(Bytes
.toBytes(5)));
1653 handler
.createTable(tableDescriptor
, splitKeys
);
1655 tableDescriptor
.setDurability(TDurability
.ASYNC_WAL
);
1656 handler
.modifyTable(tableDescriptor
);
1657 //modify column family
1658 columnFamilyDescriptor1
.setInMemory(true);
1659 handler
.modifyColumnFamily(tTableName
, columnFamilyDescriptor1
);
1661 TColumnFamilyDescriptor columnFamilyDescriptor2
= new TColumnFamilyDescriptor();
1662 columnFamilyDescriptor2
.setName(familyBname
);
1663 columnFamilyDescriptor2
.setDataBlockEncoding(TDataBlockEncoding
.PREFIX
);
1664 handler
.addColumnFamily(tTableName
, columnFamilyDescriptor2
);
1665 //get table descriptor
1666 TTableDescriptor tableDescriptorReturned
= handler
.getTableDescriptor(tTableName
);
1667 assertTrue(tableDescriptorReturned
.getColumns().size() == 2);
1668 assertTrue(tableDescriptorReturned
.getDurability() == TDurability
.ASYNC_WAL
);
1669 TColumnFamilyDescriptor columnFamilyDescriptor1Returned
= tableDescriptorReturned
.getColumns()
1670 .stream().filter(desc
-> Bytes
.equals(desc
.getName(), familyAname
)).findFirst().get();
1671 assertTrue(columnFamilyDescriptor1Returned
.isInMemory() == true);
1672 //delete column family
1673 handler
.deleteColumnFamily(tTableName
, ByteBuffer
.wrap(familyBname
));
1674 tableDescriptorReturned
= handler
.getTableDescriptor(tTableName
);
1675 assertTrue(tableDescriptorReturned
.getColumns().size() == 1);
1677 handler
.disableTable(tTableName
);
1678 assertTrue(handler
.isTableDisabled(tTableName
));
1680 handler
.enableTable(tTableName
);
1681 assertTrue(handler
.isTableEnabled(tTableName
));
1682 assertTrue(handler
.isTableAvailable(tTableName
));
1684 handler
.disableTable(tTableName
);
1685 handler
.truncateTable(tTableName
, true);
1686 assertTrue(handler
.isTableAvailable(tTableName
));
1688 handler
.disableTable(tTableName
);
1689 handler
.deleteTable(tTableName
);
1690 assertFalse(handler
.tableExists(tTableName
));
1692 handler
.deleteNamespace(namespace
);
1693 namespaceDescriptors
= handler
.listNamespaceDescriptors();
1694 // should have 2 namespace, default and hbase
1695 assertTrue(namespaceDescriptors
.size() == 2);
1699 public void testGetTableDescriptor() throws Exception
{
1700 ThriftHBaseServiceHandler handler
= createHandler();
1701 TTableDescriptor tableDescriptor
= handler
1702 .getTableDescriptor(ThriftUtilities
.tableNameFromHBase(TableName
.valueOf(tableAname
)));
1703 TableDescriptor table
= ThriftUtilities
.tableDescriptorFromThrift(tableDescriptor
);
1704 assertTrue(table
.getTableName().equals(TableName
.valueOf(tableAname
)));
1705 assertTrue(table
.getColumnFamilies().length
== 2);
1706 assertTrue(table
.getColumnFamily(familyAname
).getMaxVersions() == 3);
1707 assertTrue(table
.getColumnFamily(familyBname
).getMaxVersions() == 2);
1711 public void testGetThriftServerType() throws Exception
{
1712 ThriftHBaseServiceHandler handler
= createHandler();
1713 assertEquals(TThriftServerType
.TWO
, handler
.getThriftServerType());
1717 * Verify that thrift2 client calling thrift server can get the thrift server type correctly.
1720 public void testGetThriftServerOneType() throws Exception
{
1722 // start a thrift server
1723 HBaseThriftTestingUtility THRIFT_TEST_UTIL
= new HBaseThriftTestingUtility();
1725 LOG
.info("Starting HBase Thrift server One");
1726 THRIFT_TEST_UTIL
.startThriftServer(UTIL
.getConfiguration(), ThriftServerType
.ONE
);
1727 try (TTransport transport
= new TSocket(InetAddress
.getLocalHost().getHostName(),
1728 THRIFT_TEST_UTIL
.getServerPort())){
1729 TProtocol protocol
= new TBinaryProtocol(transport
);
1730 // This is our thrift2 client.
1731 THBaseService
.Iface client
= new THBaseService
.Client(protocol
);
1732 // open the transport
1734 assertEquals(TThriftServerType
.ONE
.name(), client
.getThriftServerType().name());
1736 THRIFT_TEST_UTIL
.stopThriftServer();
1740 public static class DelayingRegionObserver
implements RegionCoprocessor
, RegionObserver
{
1741 private static final Logger LOG
= LoggerFactory
.getLogger(DelayingRegionObserver
.class);
1742 // sleep time in msec
1743 private long delayMillis
;
1746 public Optional
<RegionObserver
> getRegionObserver() {
1747 return Optional
.of(this);
1751 public void start(CoprocessorEnvironment e
) throws IOException
{
1752 this.delayMillis
= e
.getConfiguration()
1753 .getLong("delayingregionobserver.delay", 3000);
1757 public void preGetOp(ObserverContext
<RegionCoprocessorEnvironment
> e
, Get get
,
1758 List
<Cell
> results
) throws IOException
{
1760 long start
= System
.currentTimeMillis();
1761 TimeUnit
.MILLISECONDS
.sleep(delayMillis
);
1762 if (LOG
.isTraceEnabled()) {
1763 LOG
.trace("Slept for " + (System
.currentTimeMillis() - start
) + " msec");
1765 } catch (InterruptedException ie
) {
1766 throw new InterruptedIOException("Interrupted while sleeping");