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;
19  
20  import java.io.IOException;
21  import java.util.Random;
22  
23  import org.apache.hadoop.fs.FileSystem;
24  import org.apache.hadoop.fs.Path;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.client.*;
31  import org.apache.hadoop.hbase.mob.MobConstants;
32  import org.apache.hadoop.hbase.mob.MobUtils;
33  import org.apache.hadoop.hbase.testclassification.MediumTests;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
36  import org.apache.hadoop.hbase.util.FSUtils;
37  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
38  import org.junit.AfterClass;
39  import org.junit.Assert;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(MediumTests.class)
45  public class TestDeleteMobTable {
46  
47    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48    private final static byte[] FAMILY = Bytes.toBytes("family");
49    private final static byte[] QF = Bytes.toBytes("qualifier");
50    private static Random random = new Random();
51  
52    @BeforeClass
53    public static void setUpBeforeClass() throws Exception {
54      TEST_UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
55      TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.info.port.auto", true);
56      TEST_UTIL.startMiniCluster(1);
57    }
58  
59    @AfterClass
60    public static void tearDownAfterClass() throws Exception {
61      TEST_UTIL.shutdownMiniCluster();
62    }
63  
64    /**
65     * Generate the mob value.
66     *
67     * @param size
68     *          the size of the value
69     * @return the mob value generated
70     */
71    private static byte[] generateMobValue(int size) {
72      byte[] mobVal = new byte[size];
73      random.nextBytes(mobVal);
74      return mobVal;
75    }
76  
77    @Test
78    public void testDeleteMobTable() throws Exception {
79      byte[] tableName = Bytes.toBytes("testDeleteMobTable");
80      TableName tn = TableName.valueOf(tableName);
81      HTableDescriptor htd = new HTableDescriptor(tn);
82      HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
83      hcd.setMobEnabled(true);
84      hcd.setMobThreshold(0);
85      htd.addFamily(hcd);
86      HBaseAdmin admin = null;
87      Table table = null;
88      try {
89        admin = TEST_UTIL.getHBaseAdmin();
90        admin.createTable(htd);
91        table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()).getTable(tn);
92        byte[] value = generateMobValue(10);
93  
94        byte[] row = Bytes.toBytes("row");
95        Put put = new Put(row);
96        put.addColumn(FAMILY, QF, EnvironmentEdgeManager.currentTime(), value);
97        table.put(put);
98  
99        admin.flush(tn);
100 
101       // the mob file exists
102       Assert.assertEquals(1, countMobFiles(tn, hcd.getNameAsString()));
103       Assert.assertEquals(0, countArchiveMobFiles(tn, hcd.getNameAsString()));
104       String fileName = assertHasOneMobRow(table, tn, hcd.getNameAsString());
105       Assert.assertFalse(mobArchiveExist(tn, hcd.getNameAsString(), fileName));
106       Assert.assertTrue(mobTableDirExist(tn));
107       table.close();
108 
109       admin.disableTable(tn);
110       admin.deleteTable(tn);
111 
112       Assert.assertFalse(admin.tableExists(tn));
113       Assert.assertEquals(0, countMobFiles(tn, hcd.getNameAsString()));
114       Assert.assertEquals(1, countArchiveMobFiles(tn, hcd.getNameAsString()));
115       Assert.assertTrue(mobArchiveExist(tn, hcd.getNameAsString(), fileName));
116       Assert.assertFalse(mobTableDirExist(tn));
117     } finally {
118       if (admin != null) {
119         admin.close();
120       }
121     }
122   }
123 
124   @Test
125   public void testDeleteNonMobTable() throws Exception {
126     byte[] tableName = Bytes.toBytes("testDeleteNonMobTable");
127     TableName tn = TableName.valueOf(tableName);
128     HTableDescriptor htd = new HTableDescriptor(tn);
129     HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
130     htd.addFamily(hcd);
131     HBaseAdmin admin = null;
132     Table table = null;
133     try {
134       admin = TEST_UTIL.getHBaseAdmin();
135       admin.createTable(htd);
136       table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()).getTable(tn);
137       byte[] value = generateMobValue(10);
138 
139       byte[] row = Bytes.toBytes("row");
140       Put put = new Put(row);
141       put.addColumn(FAMILY, QF, EnvironmentEdgeManager.currentTime(), value);
142       table.put(put);
143 
144       admin.flush(tn);
145       table.close();
146 
147       // the mob file doesn't exist
148       Assert.assertEquals(0, countMobFiles(tn, hcd.getNameAsString()));
149       Assert.assertEquals(0, countArchiveMobFiles(tn, hcd.getNameAsString()));
150       Assert.assertFalse(mobTableDirExist(tn));
151 
152       admin.disableTable(tn);
153       admin.deleteTable(tn);
154 
155       Assert.assertFalse(admin.tableExists(tn));
156       Assert.assertEquals(0, countMobFiles(tn, hcd.getNameAsString()));
157       Assert.assertEquals(0, countArchiveMobFiles(tn, hcd.getNameAsString()));
158       Assert.assertFalse(mobTableDirExist(tn));
159     } finally {
160       if (admin != null) {
161         admin.close();
162       }
163     }
164   }
165 
166   @Test
167   public void testMobFamilyDelete() throws Exception {
168     byte[] tableName = Bytes.toBytes("testMobFamilyDelete");
169     TableName tn = TableName.valueOf(tableName);
170     HTableDescriptor htd = new HTableDescriptor(tn);
171     HColumnDescriptor hcd = new HColumnDescriptor("one");
172     htd.addFamily(hcd);
173     hcd = new HColumnDescriptor(FAMILY);
174     hcd.setMobEnabled(true);
175     hcd.setMobThreshold(0);
176     htd.addFamily(hcd);
177     HBaseAdmin admin = null;
178     Table table = null;
179     try {
180       admin = TEST_UTIL.getHBaseAdmin();
181       admin.createTable(htd);
182       table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()).getTable(tn);
183       byte[] value = generateMobValue(10);
184 
185       byte[] row = Bytes.toBytes("row");
186       Put put = new Put(row);
187       put.addColumn(FAMILY, QF, EnvironmentEdgeManager.currentTime(), value);
188       table.put(put);
189 
190       admin.flush(tn);
191 
192       // the mob file exists
193       Assert.assertEquals(1, countMobFiles(tn, hcd.getNameAsString()));
194       Assert.assertEquals(0, countArchiveMobFiles(tn, hcd.getNameAsString()));
195       String fileName = assertHasOneMobRow(table, tn, hcd.getNameAsString());
196       Assert.assertFalse(mobArchiveExist(tn, hcd.getNameAsString(), fileName));
197       Assert.assertTrue(mobTableDirExist(tn));
198 
199       admin.deleteColumn(tn, FAMILY);
200 
201       Assert.assertEquals(0, countMobFiles(tn, hcd.getNameAsString()));
202       Assert.assertEquals(1, countArchiveMobFiles(tn, hcd.getNameAsString()));
203       Assert.assertTrue(mobArchiveExist(tn, hcd.getNameAsString(), fileName));
204       Assert.assertFalse(mobColumnFamilyDirExist(tn));
205     } finally {
206       table.close();
207       if (admin != null) {
208         admin.close();
209       }
210       TEST_UTIL.deleteTable(tableName);
211     }
212   }
213 
214   private int countMobFiles(TableName tn, String familyName) throws IOException {
215     FileSystem fs = TEST_UTIL.getTestFileSystem();
216     Path mobFileDir = MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tn, familyName);
217     if (fs.exists(mobFileDir)) {
218       return fs.listStatus(mobFileDir).length;
219     } else {
220       return 0;
221     }
222   }
223 
224   private int countArchiveMobFiles(TableName tn, String familyName)
225       throws IOException {
226     FileSystem fs = TEST_UTIL.getTestFileSystem();
227     Path storePath = HFileArchiveUtil.getStoreArchivePath(TEST_UTIL.getConfiguration(), tn,
228         MobUtils.getMobRegionInfo(tn).getEncodedName(), familyName);
229     if (fs.exists(storePath)) {
230       return fs.listStatus(storePath).length;
231     } else {
232       return 0;
233     }
234   }
235 
236   private boolean mobTableDirExist(TableName tn) throws IOException {
237     FileSystem fs = TEST_UTIL.getTestFileSystem();
238     Path tableDir = FSUtils.getTableDir(MobUtils.getMobHome(TEST_UTIL.getConfiguration()), tn);
239     return fs.exists(tableDir);
240   }
241 
242   private boolean mobColumnFamilyDirExist(TableName tn) throws IOException {
243     FileSystem fs = TEST_UTIL.getTestFileSystem();
244     Path tableDir = FSUtils.getTableDir(MobUtils.getMobHome(TEST_UTIL.getConfiguration()), tn);
245     HRegionInfo mobRegionInfo = MobUtils.getMobRegionInfo(tn);
246     Path mobFamilyDir = new Path(tableDir, new Path(mobRegionInfo.getEncodedName(), Bytes.toString(FAMILY)));
247 
248     return fs.exists(mobFamilyDir);
249   }
250 
251   private boolean mobArchiveExist(TableName tn, String familyName, String fileName)
252       throws IOException {
253     FileSystem fs = TEST_UTIL.getTestFileSystem();
254     Path storePath = HFileArchiveUtil.getStoreArchivePath(TEST_UTIL.getConfiguration(), tn,
255         MobUtils.getMobRegionInfo(tn).getEncodedName(), familyName);
256     return fs.exists(new Path(storePath, fileName));
257   }
258 
259   private String assertHasOneMobRow(Table table, TableName tn, String familyName)
260       throws IOException {
261     Scan scan = new Scan();
262     scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE));
263     ResultScanner rs = table.getScanner(scan);
264     Result r = rs.next();
265     Assert.assertNotNull(r);
266     byte[] value = r.getValue(FAMILY, QF);
267     String fileName = Bytes.toString(value, Bytes.SIZEOF_INT, value.length - Bytes.SIZEOF_INT);
268     Path filePath = new Path(
269         MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tn, familyName), fileName);
270     FileSystem fs = TEST_UTIL.getTestFileSystem();
271     Assert.assertTrue(fs.exists(filePath));
272     r = rs.next();
273     Assert.assertNull(r);
274     return fileName;
275   }
276 }