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