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. 046 * The result are groups of files, used as input list for the "export" mappers. 047 * All the groups should have similar amount of data. 048 * 049 * The input list is a pair of file path and length. 050 * The getBalanceSplits() function sort it by length, 051 * and assign to each group a file, going back and forth through the groups. 052 */ 053 @Test 054 public void testBalanceSplit() throws Exception { 055 // Create a list of files 056 List<Pair<SnapshotFileInfo, Long>> files = new ArrayList<>(21); 057 for (long i = 0; i <= 20; i++) { 058 SnapshotFileInfo fileInfo = SnapshotFileInfo.newBuilder() 059 .setType(SnapshotFileInfo.Type.HFILE) 060 .setHfile("file-" + i) 061 .build(); 062 files.add(new Pair<>(fileInfo, i)); 063 } 064 065 // Create 5 groups (total size 210) 066 // group 0: 20, 11, 10, 1 (total size: 42) 067 // group 1: 19, 12, 9, 2 (total size: 42) 068 // group 2: 18, 13, 8, 3 (total size: 42) 069 // group 3: 17, 12, 7, 4 (total size: 42) 070 // group 4: 16, 11, 6, 5 (total size: 42) 071 List<List<Pair<SnapshotFileInfo, Long>>> splits = ExportSnapshot.getBalancedSplits(files, 5); 072 assertEquals(5, splits.size()); 073 074 String[] split0 = new String[] {"file-20", "file-11", "file-10", "file-1", "file-0"}; 075 verifyBalanceSplit(splits.get(0), split0, 42); 076 String[] split1 = new String[] {"file-19", "file-12", "file-9", "file-2"}; 077 verifyBalanceSplit(splits.get(1), split1, 42); 078 String[] split2 = new String[] {"file-18", "file-13", "file-8", "file-3"}; 079 verifyBalanceSplit(splits.get(2), split2, 42); 080 String[] split3 = new String[] {"file-17", "file-14", "file-7", "file-4"}; 081 verifyBalanceSplit(splits.get(3), split3, 42); 082 String[] split4 = new String[] {"file-16", "file-15", "file-6", "file-5"}; 083 verifyBalanceSplit(splits.get(4), split4, 42); 084 } 085 086 private void verifyBalanceSplit(final List<Pair<SnapshotFileInfo, Long>> split, 087 final String[] expected, final long expectedSize) { 088 assertEquals(expected.length, split.size()); 089 long totalSize = 0; 090 for (int i = 0; i < expected.length; ++i) { 091 Pair<SnapshotFileInfo, Long> fileInfo = split.get(i); 092 assertEquals(expected[i], fileInfo.getFirst().getHfile()); 093 totalSize += fileInfo.getSecond(); 094 } 095 assertEquals(expectedSize, totalSize); 096 } 097}