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  
19  package org.apache.hadoop.hbase.snapshot;
20  
21  import com.google.common.collect.Lists;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.concurrent.atomic.AtomicBoolean;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileStatus;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.client.Put;
36  import org.apache.hadoop.hbase.client.Table;
37  import org.apache.hadoop.hbase.master.HMaster;
38  import org.apache.hadoop.hbase.master.snapshot.SnapshotHFileCleaner;
39  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
40  import org.apache.hadoop.hbase.testclassification.LargeTests;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.hbase.util.FSUtils;
43  import org.apache.hadoop.hbase.util.FSVisitor;
44  import org.apache.hadoop.hbase.util.FSVisitor.StoreFileVisitor;
45  import org.junit.AfterClass;
46  import org.junit.Assert;
47  import org.junit.BeforeClass;
48  import org.junit.Test;
49  import org.junit.experimental.categories.Category;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * Test Case for HBASE-21387
56   */
57  @Category({ LargeTests.class })
58  public class TestSnapshotWhenChoreCleaning {
59  
60    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
61    private static final Configuration CONF = TEST_UTIL.getConfiguration();
62    private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotClientRetries.class);
63    private static final TableName TABLE_NAME = TableName.valueOf("testTable");
64    private static final int MAX_SPLIT_KEYS_NUM = 100;
65    private static final byte[] FAMILY = Bytes.toBytes("family");
66    private static final byte[] QUALIFIER = Bytes.toBytes("qualifier");
67    private static final byte[] VALUE = Bytes.toBytes("value");
68    private static Table TABLE;
69  
70    @BeforeClass
71    public static void setUp() throws Exception {
72      // Set the hbase.snapshot.thread.pool.max to 1;
73      CONF.setInt("hbase.snapshot.thread.pool.max", 1);
74      // Enable snapshot
75      CONF.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
76      // Start MiniCluster.
77      TEST_UTIL.startMiniCluster(3);
78      // Create talbe
79      createTable();
80    }
81  
82    private static byte[] integerToBytes(int i) {
83      return Bytes.toBytes(String.format("%06d", i));
84    }
85  
86    private static void createTable() throws IOException {
87      byte[][] splitKeys = new byte[MAX_SPLIT_KEYS_NUM][];
88      for (int i = 0; i < splitKeys.length; i++) {
89        splitKeys[i] = integerToBytes(i);
90      }
91      TABLE = TEST_UTIL.createTable(TABLE_NAME, FAMILY, splitKeys);
92    }
93  
94    @AfterClass
95    public static void tearDown() throws Exception {
96      TEST_UTIL.shutdownMiniCluster();
97    }
98  
99    private static void loadDataAndFlush() throws IOException {
100     for (int i = 0; i < MAX_SPLIT_KEYS_NUM; i++) {
101       Put put = new Put(integerToBytes(i)).addColumn(FAMILY, QUALIFIER,
102         Bytes.add(VALUE, Bytes.toBytes(i)));
103       TABLE.put(put);
104     }
105     TEST_UTIL.flush(TABLE_NAME);
106   }
107 
108   private static List<Path> listHFileNames(final FileSystem fs, final Path tableDir)
109       throws IOException {
110     final List<Path> hfiles = new ArrayList<>();
111     FSVisitor.visitTableStoreFiles(fs, tableDir, new StoreFileVisitor() {
112 
113       @Override
114       public void storeFile(String region, String family, String hfileName) throws IOException {
115         hfiles.add(new Path(new Path(new Path(tableDir, region), family), hfileName));
116       }
117     });
118     Collections.sort(hfiles);
119     return hfiles;
120   }
121 
122   private static boolean isAnySnapshots(FileSystem fs) throws IOException {
123     Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(FSUtils.getRootDir(CONF));
124     FileStatus[] snapFiles = fs.listStatus(snapshotDir);
125     if (snapFiles.length == 0) {
126       return false;
127     }
128     Path firstPath = snapFiles[0].getPath();
129     LOG.info("firstPath in isAnySnapshots: " + firstPath);
130     if (snapFiles.length == 1 && firstPath.getName().equals(".tmp")) {
131       FileStatus[] tmpSnapFiles = fs.listStatus(firstPath);
132       return tmpSnapFiles != null && tmpSnapFiles.length > 0;
133     }
134     return true;
135   }
136 
137   @Test
138   public void testSnapshotWhenSnapshotHFileCleanerRunning() throws Exception {
139     // Load data and flush to generate huge number of HFiles.
140     loadDataAndFlush();
141 
142     final SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
143     Map<String, Object> params = new HashMap<String, Object>();
144     params.put(HMaster.MASTER, TEST_UTIL.getHBaseCluster().getMaster());
145     cleaner.init(params);
146     cleaner.setConf(CONF);
147 
148     final FileSystem fs = FSUtils.getCurrentFileSystem(CONF);
149     List<Path> fileNames =
150         listHFileNames(fs, FSUtils.getTableDir(FSUtils.getRootDir(CONF), TABLE_NAME));
151     final List<FileStatus> files = new ArrayList<>();
152     for (Path fileName : fileNames) {
153       files.add(fs.getFileStatus(fileName));
154     }
155 
156     TEST_UTIL.getHBaseAdmin().snapshot("snapshotName_prev", TABLE_NAME);
157     Assert.assertEquals(Lists.newArrayList(cleaner.getDeletableFiles(files)).size(), 0);
158     TEST_UTIL.getHBaseAdmin().deleteSnapshot("snapshotName_prev");
159     cleaner.getFileCacheForTesting().triggerCacheRefreshForTesting();
160     Assert.assertEquals(Lists.newArrayList(cleaner.getDeletableFiles(files)).size(), 100);
161 
162     Runnable snapshotRunnable = new Runnable() {
163       @Override
164       public void run() {
165         try {
166           // The thread will be busy on taking snapshot;
167           for (int k = 0; k < 5; k++) {
168             TEST_UTIL.getHBaseAdmin().snapshot("snapshotName_" + k, TABLE_NAME);
169           }
170         } catch (Exception e) {
171           LOG.error("Snapshot failed: ", e);
172         }
173       }
174     };
175     final AtomicBoolean success = new AtomicBoolean(true);
176     Runnable cleanerRunnable = new Runnable() {
177       @Override
178       public void run() {
179         try {
180           while (!isAnySnapshots(fs)) {
181             LOG.info("Not found any snapshot, sleep 100ms");
182             Thread.sleep(100);
183           }
184           for (int k = 0; k < 5; k++) {
185             cleaner.getFileCacheForTesting().triggerCacheRefreshForTesting();
186             Iterable<FileStatus> toDeleteFiles = cleaner.getDeletableFiles(files);
187             List<FileStatus> deletableFiles = Lists.newArrayList(toDeleteFiles);
188             LOG.info("Size of deletableFiles is: " + deletableFiles.size());
189             for (int i = 0; i < deletableFiles.size(); i++) {
190               LOG.debug("toDeleteFiles[{}] is: {}", i, deletableFiles.get(i));
191             }
192             if (deletableFiles.size() > 0) {
193               success.set(false);
194             }
195           }
196         } catch (Exception e) {
197           LOG.error("Chore cleaning failed: ", e);
198         }
199       }
200     };
201     Thread t1 = new Thread(snapshotRunnable);
202     t1.start();
203     Thread t2 = new Thread(cleanerRunnable);
204     t2.start();
205     t1.join();
206     t2.join();
207     Assert.assertTrue(success.get());
208   }
209 }