001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static org.apache.hadoop.hbase.util.Threads.sleep; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import java.util.List; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.io.ByteBuffAllocator; 033import org.apache.hadoop.hbase.io.DeallocateRewriteByteBuffAllocator; 034import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding; 035import org.apache.hadoop.hbase.io.hfile.BlockCacheFactory; 036import org.apache.hadoop.hbase.regionserver.HRegion; 037import org.apache.hadoop.hbase.regionserver.HRegionFileSystem; 038import org.apache.hadoop.hbase.regionserver.RegionServerServices; 039import org.apache.hadoop.hbase.testclassification.ClientTests; 040import org.apache.hadoop.hbase.testclassification.LargeTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.apache.hadoop.hbase.wal.WAL; 043import org.junit.jupiter.api.AfterAll; 044import org.junit.jupiter.api.BeforeAll; 045import org.junit.jupiter.api.Tag; 046import org.junit.jupiter.api.Test; 047import org.junit.jupiter.api.TestInfo; 048 049@Tag(LargeTests.TAG) 050@Tag(ClientTests.TAG) 051public class TestCheckAndMutateWithByteBuff { 052 053 private static final byte[] CF = Bytes.toBytes("CF"); 054 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 055 private static final Configuration conf = TEST_UTIL.getConfiguration(); 056 private static Admin admin = null; 057 058 @BeforeAll 059 public static void setupBeforeClass() throws Exception { 060 conf.set(HConstants.REGION_IMPL, TestCheckAndMutateRegion.class.getName()); 061 conf.set(ByteBuffAllocator.BYTEBUFF_ALLOCATOR_CLASS, 062 DeallocateRewriteByteBuffAllocator.class.getName()); 063 conf.setBoolean(ByteBuffAllocator.ALLOCATOR_POOL_ENABLED_KEY, true); 064 conf.setInt(ByteBuffAllocator.MIN_ALLOCATE_SIZE_KEY, 1); 065 conf.setInt(BlockCacheFactory.BUCKET_CACHE_WRITER_THREADS_KEY, 20); 066 conf.setInt(ByteBuffAllocator.BUFFER_SIZE_KEY, 1024); 067 conf.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap"); 068 conf.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, 64); 069 conf.setInt("hbase.client.retries.number", 1); 070 TEST_UTIL.startMiniCluster(); 071 admin = TEST_UTIL.getAdmin(); 072 } 073 074 @AfterAll 075 public static void tearDownAfterClass() throws Exception { 076 TEST_UTIL.shutdownMiniCluster(); 077 } 078 079 @Test 080 public void testCheckAndMutateWithByteBuffNoEncode(TestInfo testInfo) throws Exception { 081 testCheckAndMutateWithByteBuff(TableName.valueOf(testInfo.getTestMethod().get().getName()), 082 DataBlockEncoding.NONE); 083 } 084 085 @Test 086 public void testCheckAndMutateWithByteBuffEncode(TestInfo testInfo) throws Exception { 087 // Tests for HBASE-26777. 088 // As most HBase.getRegion() calls have been factored out from HBase, you'd need to revert 089 // both HBASE-26777, and the HBase.get() replacements from HBASE-26036 for this test to fail 090 testCheckAndMutateWithByteBuff(TableName.valueOf(testInfo.getTestMethod().get().getName()), 091 DataBlockEncoding.FAST_DIFF); 092 } 093 094 private void testCheckAndMutateWithByteBuff(TableName tableName, DataBlockEncoding dbe) 095 throws Exception { 096 Table testTable = createTable(tableName, dbe); 097 byte[] checkRow = Bytes.toBytes("checkRow"); 098 byte[] checkQualifier = Bytes.toBytes("cq"); 099 byte[] checkValue = Bytes.toBytes("checkValue"); 100 101 Put put = new Put(checkRow); 102 put.addColumn(CF, checkQualifier, checkValue); 103 testTable.put(put); 104 admin.flush(testTable.getName()); 105 106 assertTrue(testTable.checkAndMutate(checkRow, CF).qualifier(checkQualifier).ifEquals(checkValue) 107 .thenPut(new Put(checkRow).addColumn(CF, Bytes.toBytes("q1"), Bytes.toBytes("testValue")))); 108 } 109 110 private Table createTable(TableName tableName, DataBlockEncoding dbe) throws IOException { 111 TableDescriptor td = 112 TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder 113 .newBuilder(CF).setBlocksize(100).setDataBlockEncoding(dbe).build()).build(); 114 return TEST_UTIL.createTable(td, null); 115 } 116 117 /** 118 * An override of HRegion to allow sleep after get(), waiting for the release of DBB 119 */ 120 public static class TestCheckAndMutateRegion extends HRegion { 121 public TestCheckAndMutateRegion(Path tableDir, WAL log, FileSystem fs, Configuration confParam, 122 RegionInfo info, TableDescriptor htd, RegionServerServices rsServices) { 123 super(tableDir, log, fs, confParam, info, htd, rsServices); 124 } 125 126 public TestCheckAndMutateRegion(HRegionFileSystem fs, WAL wal, Configuration confParam, 127 TableDescriptor htd, RegionServerServices rsServices) { 128 super(fs, wal, confParam, htd, rsServices); 129 } 130 131 @Override 132 public List<Cell> get(Get get, boolean withCoprocessor) throws IOException { 133 List<Cell> cells = super.get(get, withCoprocessor); 134 sleep(600); 135 return cells; 136 } 137 } 138}