HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / regionserver / TestDeleteMobTable.java
blobfaf61b6e36b93675f5d9ffb934da48d1f658b924
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
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.regionserver;
20 import java.io.IOException;
21 import java.util.Random;
22 import org.apache.hadoop.fs.FileSystem;
23 import org.apache.hadoop.fs.Path;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseTestingUtil;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
28 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
29 import org.apache.hadoop.hbase.client.Put;
30 import org.apache.hadoop.hbase.client.Result;
31 import org.apache.hadoop.hbase.client.ResultScanner;
32 import org.apache.hadoop.hbase.client.Scan;
33 import org.apache.hadoop.hbase.client.Table;
34 import org.apache.hadoop.hbase.client.TableDescriptor;
35 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
36 import org.apache.hadoop.hbase.mob.MobConstants;
37 import org.apache.hadoop.hbase.mob.MobUtils;
38 import org.apache.hadoop.hbase.testclassification.MediumTests;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.apache.hadoop.hbase.util.CommonFSUtils;
41 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
42 import org.apache.hadoop.hbase.util.HFileArchiveUtil;
43 import org.junit.AfterClass;
44 import org.junit.Assert;
45 import org.junit.BeforeClass;
46 import org.junit.ClassRule;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50 import org.junit.rules.TestName;
52 @Category(MediumTests.class)
53 public class TestDeleteMobTable {
55 @ClassRule
56 public static final HBaseClassTestRule CLASS_RULE =
57 HBaseClassTestRule.forClass(TestDeleteMobTable.class);
59 private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
60 private final static byte[] FAMILY = Bytes.toBytes("family");
61 private final static byte[] QF = Bytes.toBytes("qualifier");
62 private static Random random = new Random();
64 @Rule
65 public TestName name = new TestName();
67 @BeforeClass
68 public static void setUpBeforeClass() throws Exception {
69 TEST_UTIL.startMiniCluster(1);
72 @AfterClass
73 public static void tearDownAfterClass() throws Exception {
74 TEST_UTIL.shutdownMiniCluster();
77 /**
78 * Generate the mob value.
80 * @param size
81 * the size of the value
82 * @return the mob value generated
84 private static byte[] generateMobValue(int size) {
85 byte[] mobVal = new byte[size];
86 random.nextBytes(mobVal);
87 return mobVal;
90 private TableDescriptor createTableDescriptor(TableName tableName, boolean hasMob) {
91 ColumnFamilyDescriptorBuilder builder = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY);
92 if (hasMob) {
93 builder.setMobEnabled(true);
94 builder.setMobThreshold(0);
96 return TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(builder.build()).build();
99 private Table createTableWithOneFile(TableDescriptor tableDescriptor) throws IOException {
100 Table table = TEST_UTIL.createTable(tableDescriptor, null);
101 try {
102 // insert data
103 byte[] value = generateMobValue(10);
104 byte[] row = Bytes.toBytes("row");
105 Put put = new Put(row);
106 put.addColumn(FAMILY, QF, EnvironmentEdgeManager.currentTime(), value);
107 table.put(put);
109 // create an hfile
110 TEST_UTIL.getAdmin().flush(tableDescriptor.getTableName());
111 } catch (IOException e) {
112 table.close();
113 throw e;
115 return table;
118 @Test
119 public void testDeleteMobTable() throws Exception {
120 final TableName tableName = TableName.valueOf(name.getMethodName());
121 TableDescriptor tableDescriptor = createTableDescriptor(tableName, true);
122 ColumnFamilyDescriptor familyDescriptor = tableDescriptor.getColumnFamily(FAMILY);
124 String fileName = null;
125 Table table = createTableWithOneFile(tableDescriptor);
126 try {
127 // the mob file exists
128 Assert.assertEquals(1, countMobFiles(tableName, familyDescriptor.getNameAsString()));
129 Assert.assertEquals(0, countArchiveMobFiles(tableName, familyDescriptor.getNameAsString()));
130 fileName = assertHasOneMobRow(table, tableName, familyDescriptor.getNameAsString());
131 Assert.assertFalse(mobArchiveExist(tableName, familyDescriptor.getNameAsString(), fileName));
132 Assert.assertTrue(mobTableDirExist(tableName));
133 } finally {
134 table.close();
135 TEST_UTIL.deleteTable(tableName);
138 Assert.assertFalse(TEST_UTIL.getAdmin().tableExists(tableName));
139 Assert.assertEquals(0, countMobFiles(tableName, familyDescriptor.getNameAsString()));
140 Assert.assertEquals(1, countArchiveMobFiles(tableName, familyDescriptor.getNameAsString()));
141 Assert.assertTrue(mobArchiveExist(tableName, familyDescriptor.getNameAsString(), fileName));
142 Assert.assertFalse(mobTableDirExist(tableName));
145 @Test
146 public void testDeleteNonMobTable() throws Exception {
147 final TableName tableName = TableName.valueOf(name.getMethodName());
148 TableDescriptor htd = createTableDescriptor(tableName, false);
149 ColumnFamilyDescriptor hcd = htd.getColumnFamily(FAMILY);
151 Table table = createTableWithOneFile(htd);
152 try {
153 // the mob file doesn't exist
154 Assert.assertEquals(0, countMobFiles(tableName, hcd.getNameAsString()));
155 Assert.assertEquals(0, countArchiveMobFiles(tableName, hcd.getNameAsString()));
156 Assert.assertFalse(mobTableDirExist(tableName));
157 } finally {
158 table.close();
159 TEST_UTIL.deleteTable(tableName);
162 Assert.assertFalse(TEST_UTIL.getAdmin().tableExists(tableName));
163 Assert.assertEquals(0, countMobFiles(tableName, hcd.getNameAsString()));
164 Assert.assertEquals(0, countArchiveMobFiles(tableName, hcd.getNameAsString()));
165 Assert.assertFalse(mobTableDirExist(tableName));
168 @Test
169 public void testMobFamilyDelete() throws Exception {
170 final TableName tableName = TableName.valueOf(name.getMethodName());
171 TableDescriptor tableDescriptor = createTableDescriptor(tableName, true);
172 ColumnFamilyDescriptor familyDescriptor = tableDescriptor.getColumnFamily(FAMILY);
173 tableDescriptor = TableDescriptorBuilder.newBuilder(tableDescriptor)
174 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("family2"))).build();
176 Table table = createTableWithOneFile(tableDescriptor);
177 try {
178 // the mob file exists
179 Assert.assertEquals(1, countMobFiles(tableName, familyDescriptor.getNameAsString()));
180 Assert.assertEquals(0, countArchiveMobFiles(tableName, familyDescriptor.getNameAsString()));
181 String fileName = assertHasOneMobRow(table, tableName, familyDescriptor.getNameAsString());
182 Assert.assertFalse(mobArchiveExist(tableName, familyDescriptor.getNameAsString(), fileName));
183 Assert.assertTrue(mobTableDirExist(tableName));
185 TEST_UTIL.getAdmin().deleteColumnFamily(tableName, FAMILY);
187 Assert.assertEquals(0, countMobFiles(tableName, familyDescriptor.getNameAsString()));
188 Assert.assertEquals(1, countArchiveMobFiles(tableName, familyDescriptor.getNameAsString()));
189 Assert.assertTrue(mobArchiveExist(tableName, familyDescriptor.getNameAsString(), fileName));
190 Assert.assertFalse(mobColumnFamilyDirExist(tableName, familyDescriptor.getNameAsString()));
191 } finally {
192 table.close();
193 TEST_UTIL.deleteTable(tableName);
197 private int countMobFiles(TableName tn, String familyName) throws IOException {
198 FileSystem fs = TEST_UTIL.getTestFileSystem();
199 Path mobFileDir = MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tn, familyName);
200 if (fs.exists(mobFileDir)) {
201 return fs.listStatus(mobFileDir).length;
203 return 0;
206 private int countArchiveMobFiles(TableName tn, String familyName)
207 throws IOException {
208 FileSystem fs = TEST_UTIL.getTestFileSystem();
209 Path storePath = HFileArchiveUtil.getStoreArchivePath(TEST_UTIL.getConfiguration(), tn,
210 MobUtils.getMobRegionInfo(tn).getEncodedName(), familyName);
211 if (fs.exists(storePath)) {
212 return fs.listStatus(storePath).length;
214 return 0;
217 private boolean mobTableDirExist(TableName tn) throws IOException {
218 FileSystem fs = TEST_UTIL.getTestFileSystem();
219 Path tableDir =
220 CommonFSUtils.getTableDir(MobUtils.getMobHome(TEST_UTIL.getConfiguration()), tn);
221 return fs.exists(tableDir);
224 private boolean mobColumnFamilyDirExist(TableName tn, String familyName) throws IOException {
225 FileSystem fs = TEST_UTIL.getTestFileSystem();
226 Path mobFamilyDir = MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tn, familyName);
227 return fs.exists(mobFamilyDir);
230 private boolean mobArchiveExist(TableName tn, String familyName, String fileName)
231 throws IOException {
232 FileSystem fs = TEST_UTIL.getTestFileSystem();
233 Path storePath = HFileArchiveUtil.getStoreArchivePath(TEST_UTIL.getConfiguration(), tn,
234 MobUtils.getMobRegionInfo(tn).getEncodedName(), familyName);
235 return fs.exists(new Path(storePath, fileName));
238 private String assertHasOneMobRow(Table table, TableName tn, String familyName)
239 throws IOException {
240 Scan scan = new Scan();
241 scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE));
242 ResultScanner rs = table.getScanner(scan);
243 Result r = rs.next();
244 Assert.assertNotNull(r);
245 String fileName = MobUtils.getMobFileName(r.getColumnLatestCell(FAMILY, QF));
246 Path filePath = new Path(
247 MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tn, familyName), fileName);
248 FileSystem fs = TEST_UTIL.getTestFileSystem();
249 Assert.assertTrue(fs.exists(filePath));
250 r = rs.next();
251 Assert.assertNull(r);
252 return fileName;