001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.snapshot; 019 020import static org.junit.Assert.assertEquals; 021 022import java.util.ArrayList; 023import java.util.List; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.testclassification.RegionServerTests; 026import org.apache.hadoop.hbase.testclassification.SmallTests; 027import org.apache.hadoop.hbase.util.Pair; 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotFileInfo; 033 034/** 035 * Test Export Snapshot Tool helpers 036 */ 037@Category({ RegionServerTests.class, SmallTests.class }) 038public class TestExportSnapshotHelpers { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestExportSnapshotHelpers.class); 043 044 /** 045 * Verfy the result of getBalanceSplits() method. The result are groups of files, used as input 046 * list for the "export" mappers. All the groups should have similar amount of data. The input 047 * list is a pair of file path and length. The getBalanceSplits() function sort it by length, and 048 * assign to each group a file, going back and forth through the groups. 049 */ 050 @Test 051 public void testBalanceSplit() throws Exception { 052 // Create a list of files 053 List<Pair<SnapshotFileInfo, Long>> files = new ArrayList<>(21); 054 for (long i = 0; i <= 20; i++) { 055 SnapshotFileInfo fileInfo = SnapshotFileInfo.newBuilder().setType(SnapshotFileInfo.Type.HFILE) 056 .setHfile("file-" + i).build(); 057 files.add(new Pair<>(fileInfo, i)); 058 } 059 060 // Create 5 groups (total size 210) 061 // group 0: 20, 11, 10, 1 (total size: 42) 062 // group 1: 19, 12, 9, 2 (total size: 42) 063 // group 2: 18, 13, 8, 3 (total size: 42) 064 // group 3: 17, 12, 7, 4 (total size: 42) 065 // group 4: 16, 11, 6, 5 (total size: 42) 066 List<List<Pair<SnapshotFileInfo, Long>>> splits = ExportSnapshot.getBalancedSplits(files, 5); 067 assertEquals(5, splits.size()); 068 069 String[] split0 = new String[] { "file-20", "file-11", "file-10", "file-1", "file-0" }; 070 verifyBalanceSplit(splits.get(0), split0, 42); 071 String[] split1 = new String[] { "file-19", "file-12", "file-9", "file-2" }; 072 verifyBalanceSplit(splits.get(1), split1, 42); 073 String[] split2 = new String[] { "file-18", "file-13", "file-8", "file-3" }; 074 verifyBalanceSplit(splits.get(2), split2, 42); 075 String[] split3 = new String[] { "file-17", "file-14", "file-7", "file-4" }; 076 verifyBalanceSplit(splits.get(3), split3, 42); 077 String[] split4 = new String[] { "file-16", "file-15", "file-6", "file-5" }; 078 verifyBalanceSplit(splits.get(4), split4, 42); 079 } 080 081 private void verifyBalanceSplit(final List<Pair<SnapshotFileInfo, Long>> split, 082 final String[] expected, final long expectedSize) { 083 assertEquals(expected.length, split.size()); 084 long totalSize = 0; 085 for (int i = 0; i < expected.length; ++i) { 086 Pair<SnapshotFileInfo, Long> fileInfo = split.get(i); 087 assertEquals(expected[i], fileInfo.getFirst().getHfile()); 088 totalSize += fileInfo.getSecond(); 089 } 090 assertEquals(expectedSize, totalSize); 091 } 092}