24
24
25
25
import java .io .IOException ;
26
26
import java .util .ArrayList ;
27
+ import java .util .HashMap ;
27
28
import java .util .HashSet ;
28
29
import java .util .List ;
30
+ import java .util .Map ;
29
31
import java .util .Objects ;
32
+ import java .util .Optional ;
30
33
import java .util .Set ;
31
34
import java .util .stream .Collectors ;
32
35
import org .apache .hadoop .conf .Configuration ;
@@ -230,6 +233,37 @@ public void testExportWithTargetName() throws Exception {
230
233
testExportFileSystemState (tableName , snapshotName , targetName , tableNumFiles );
231
234
}
232
235
236
+ @ Test
237
+ public void testExportWithResetTtl () throws Exception {
238
+ String name = "testExportWithResetTtl" ;
239
+ TableName tableName = TableName .valueOf (name );
240
+ String snapshotName = "snaptb-" + name ;
241
+ Long ttl = 100000L ;
242
+
243
+ try {
244
+ // create Table
245
+ createTable (tableName );
246
+ SnapshotTestingUtils .loadData (TEST_UTIL , tableName , 50 , FAMILY );
247
+ int tableNumFiles = admin .getRegions (tableName ).size ();
248
+ // take a snapshot with TTL
249
+ Map <String , Object > props = new HashMap <>();
250
+ props .put ("TTL" , ttl );
251
+ admin .snapshot (snapshotName , tableName , props );
252
+ Optional <Long > ttlOpt =
253
+ admin .listSnapshots ().stream ().filter (s -> s .getName ().equals (snapshotName ))
254
+ .map (org .apache .hadoop .hbase .client .SnapshotDescription ::getTtl ).findAny ();
255
+ assertTrue (ttlOpt .isPresent ());
256
+ assertEquals (ttl , ttlOpt .get ());
257
+
258
+ testExportFileSystemState (tableName , snapshotName , snapshotName , tableNumFiles ,
259
+ getHdfsDestinationDir (), false , true );
260
+ } catch (Exception e ) {
261
+ throw e ;
262
+ } finally {
263
+ TEST_UTIL .deleteTable (tableName );
264
+ }
265
+ }
266
+
233
267
private void testExportFileSystemState (final TableName tableName , final String snapshotName ,
234
268
final String targetName , int filesExpected ) throws Exception {
235
269
testExportFileSystemState (tableName , snapshotName , targetName , filesExpected ,
@@ -238,8 +272,15 @@ private void testExportFileSystemState(final TableName tableName, final String s
238
272
239
273
protected void testExportFileSystemState (final TableName tableName , final String snapshotName ,
240
274
final String targetName , int filesExpected , Path copyDir , boolean overwrite ) throws Exception {
275
+ testExportFileSystemState (tableName , snapshotName , targetName , filesExpected , copyDir ,
276
+ overwrite , false );
277
+ }
278
+
279
+ protected void testExportFileSystemState (final TableName tableName , final String snapshotName ,
280
+ final String targetName , int filesExpected , Path copyDir , boolean overwrite , boolean resetTtl )
281
+ throws Exception {
241
282
testExportFileSystemState (TEST_UTIL .getConfiguration (), tableName , snapshotName , targetName ,
242
- filesExpected , TEST_UTIL .getDefaultRootDirPath (), copyDir , overwrite ,
283
+ filesExpected , TEST_UTIL .getDefaultRootDirPath (), copyDir , overwrite , resetTtl ,
243
284
getBypassRegionPredicate (), true );
244
285
}
245
286
@@ -249,7 +290,8 @@ protected void testExportFileSystemState(final TableName tableName, final String
249
290
protected static void testExportFileSystemState (final Configuration conf ,
250
291
final TableName tableName , final String snapshotName , final String targetName ,
251
292
final int filesExpected , final Path srcDir , Path rawTgtDir , final boolean overwrite ,
252
- final RegionPredicate bypassregionPredicate , boolean success ) throws Exception {
293
+ final boolean resetTtl , final RegionPredicate bypassregionPredicate , boolean success )
294
+ throws Exception {
253
295
FileSystem tgtFs = rawTgtDir .getFileSystem (conf );
254
296
FileSystem srcFs = srcDir .getFileSystem (conf );
255
297
Path tgtDir = rawTgtDir .makeQualified (tgtFs .getUri (), tgtFs .getWorkingDirectory ());
@@ -267,6 +309,9 @@ protected static void testExportFileSystemState(final Configuration conf,
267
309
if (overwrite ) {
268
310
opts .add ("--overwrite" );
269
311
}
312
+ if (resetTtl ) {
313
+ opts .add ("--reset-ttl" );
314
+ }
270
315
271
316
// Export Snapshot
272
317
int res = run (conf , new ExportSnapshot (), opts .toArray (new String [opts .size ()]));
@@ -295,7 +340,7 @@ protected static void testExportFileSystemState(final Configuration conf,
295
340
final Path targetDir = new Path (HConstants .SNAPSHOT_DIR_NAME , targetName );
296
341
verifySnapshotDir (srcFs , new Path (srcDir , snapshotDir ), tgtFs , new Path (tgtDir , targetDir ));
297
342
Set <String > snapshotFiles =
298
- verifySnapshot (conf , tgtFs , tgtDir , tableName , targetName , bypassregionPredicate );
343
+ verifySnapshot (conf , tgtFs , tgtDir , tableName , targetName , resetTtl , bypassregionPredicate );
299
344
assertEquals (filesExpected , snapshotFiles .size ());
300
345
}
301
346
@@ -312,7 +357,7 @@ protected static void verifySnapshotDir(final FileSystem fs1, final Path root1,
312
357
*/
313
358
protected static Set <String > verifySnapshot (final Configuration conf , final FileSystem fs ,
314
359
final Path rootDir , final TableName tableName , final String snapshotName ,
315
- final RegionPredicate bypassregionPredicate ) throws IOException {
360
+ final boolean resetTtl , final RegionPredicate bypassregionPredicate ) throws IOException {
316
361
final Path exportedSnapshot =
317
362
new Path (rootDir , new Path (HConstants .SNAPSHOT_DIR_NAME , snapshotName ));
318
363
final Set <String > snapshotFiles = new HashSet <>();
@@ -354,6 +399,9 @@ private void verifyNonEmptyFile(final Path path) throws IOException {
354
399
SnapshotDescription desc = SnapshotDescriptionUtils .readSnapshotInfo (fs , exportedSnapshot );
355
400
assertTrue (desc .getName ().equals (snapshotName ));
356
401
assertTrue (desc .getTable ().equals (tableName .getNameAsString ()));
402
+ if (resetTtl ) {
403
+ assertEquals (HConstants .DEFAULT_SNAPSHOT_TTL , desc .getTtl ());
404
+ }
357
405
return snapshotFiles ;
358
406
}
359
407
0 commit comments