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.regionserver;
019
020import static java.util.Arrays.asList;
021import static org.mockito.ArgumentMatchers.any;
022import static org.mockito.ArgumentMatchers.anyLong;
023import static org.mockito.Mockito.verify;
024import static org.mockito.Mockito.when;
025import static org.mockito.hamcrest.MockitoHamcrest.argThat;
026
027import java.io.FileNotFoundException;
028import java.io.IOException;
029import java.util.ArrayList;
030import java.util.List;
031import org.apache.hadoop.fs.Path;
032import org.apache.hadoop.hbase.DoNotRetryIOException;
033import org.apache.hadoop.hbase.HBaseClassTestRule;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.Pair;
037import org.apache.hadoop.hbase.wal.WALEdit;
038import org.apache.hadoop.hbase.wal.WALKeyImpl;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.mockito.invocation.InvocationOnMock;
043import org.mockito.stubbing.Answer;
044
045/**
046 * This class attempts to unit test bulk HLog loading.
047 */
048@Category(SmallTests.class)
049public class TestBulkLoad extends TestBulkloadBase {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestBulkLoad.class);
054
055  public TestBulkLoad(boolean useFileBasedSFT) {
056    super(useFileBasedSFT);
057  }
058
059  @Test
060  public void verifyBulkLoadEvent() throws IOException {
061    TableName tableName = TableName.valueOf("test", "test");
062    List<Pair<byte[], String>> familyPaths = withFamilyPathsFor(family1);
063    byte[] familyName = familyPaths.get(0).getFirst();
064    String storeFileName = familyPaths.get(0).getSecond();
065    storeFileName = (new Path(storeFileName)).getName();
066    List<String> storeFileNames = new ArrayList<>();
067    storeFileNames.add(storeFileName);
068    when(log.appendMarker(any(), any(),
069      argThat(bulkLogWalEdit(WALEdit.BULK_LOAD, tableName.toBytes(), familyName, storeFileNames))))
070        .thenAnswer(new Answer() {
071          @Override
072          public Object answer(InvocationOnMock invocation) {
073            WALKeyImpl walKey = invocation.getArgument(1);
074            MultiVersionConcurrencyControl mvcc = walKey.getMvcc();
075            if (mvcc != null) {
076              MultiVersionConcurrencyControl.WriteEntry we = mvcc.begin();
077              walKey.setWriteEntry(we);
078            }
079            return 01L;
080          }
081        });
082    testRegionWithFamiliesAndSpecifiedTableName(tableName, family1).bulkLoadHFiles(familyPaths,
083      false, null);
084    verify(log).sync(anyLong());
085  }
086
087  @Test
088  public void bulkHLogShouldThrowNoErrorAndWriteMarkerWithBlankInput() throws IOException {
089    testRegionWithFamilies(family1).bulkLoadHFiles(new ArrayList<>(), false, null);
090  }
091
092  @Test
093  public void shouldBulkLoadSingleFamilyHLog() throws IOException {
094    when(log.appendMarker(any(), any(), argThat(bulkLogWalEditType(WALEdit.BULK_LOAD))))
095      .thenAnswer(new Answer() {
096        @Override
097        public Object answer(InvocationOnMock invocation) {
098          WALKeyImpl walKey = invocation.getArgument(1);
099          MultiVersionConcurrencyControl mvcc = walKey.getMvcc();
100          if (mvcc != null) {
101            MultiVersionConcurrencyControl.WriteEntry we = mvcc.begin();
102            walKey.setWriteEntry(we);
103          }
104          return 01L;
105        }
106      });
107    testRegionWithFamilies(family1).bulkLoadHFiles(withFamilyPathsFor(family1), false, null);
108    verify(log).sync(anyLong());
109  }
110
111  @Test
112  public void shouldBulkLoadManyFamilyHLog() throws IOException {
113    when(log.appendMarker(any(), any(), argThat(bulkLogWalEditType(WALEdit.BULK_LOAD))))
114      .thenAnswer(new Answer() {
115        @Override
116        public Object answer(InvocationOnMock invocation) {
117          WALKeyImpl walKey = invocation.getArgument(1);
118          MultiVersionConcurrencyControl mvcc = walKey.getMvcc();
119          if (mvcc != null) {
120            MultiVersionConcurrencyControl.WriteEntry we = mvcc.begin();
121            walKey.setWriteEntry(we);
122          }
123          return 01L;
124        }
125      });
126    testRegionWithFamilies(family1, family2).bulkLoadHFiles(withFamilyPathsFor(family1, family2),
127      false, null);
128    verify(log).sync(anyLong());
129  }
130
131  @Test
132  public void shouldBulkLoadManyFamilyHLogEvenWhenTableNameNamespaceSpecified() throws IOException {
133    when(log.appendMarker(any(), any(), argThat(bulkLogWalEditType(WALEdit.BULK_LOAD))))
134      .thenAnswer(new Answer() {
135        @Override
136        public Object answer(InvocationOnMock invocation) {
137          WALKeyImpl walKey = invocation.getArgument(1);
138          MultiVersionConcurrencyControl mvcc = walKey.getMvcc();
139          if (mvcc != null) {
140            MultiVersionConcurrencyControl.WriteEntry we = mvcc.begin();
141            walKey.setWriteEntry(we);
142          }
143          return 01L;
144        }
145      });
146    TableName tableName = TableName.valueOf("test", "test");
147    testRegionWithFamiliesAndSpecifiedTableName(tableName, family1, family2)
148      .bulkLoadHFiles(withFamilyPathsFor(family1, family2), false, null);
149    verify(log).sync(anyLong());
150  }
151
152  @Test(expected = DoNotRetryIOException.class)
153  public void shouldCrashIfBulkLoadFamiliesNotInTable() throws IOException {
154    testRegionWithFamilies(family1).bulkLoadHFiles(withFamilyPathsFor(family1, family2), false,
155      null);
156  }
157
158  // after HBASE-24021 will throw DoNotRetryIOException, not MultipleIOException
159  @Test(expected = DoNotRetryIOException.class)
160  public void shouldCrashIfBulkLoadMultiFamiliesNotInTable() throws IOException {
161    testRegionWithFamilies(family1).bulkLoadHFiles(withFamilyPathsFor(family1, family2, family3),
162      false, null);
163  }
164
165  @Test(expected = DoNotRetryIOException.class)
166  public void bulkHLogShouldThrowErrorWhenFamilySpecifiedAndHFileExistsButNotInTableDescriptor()
167    throws IOException {
168    testRegionWithFamilies().bulkLoadHFiles(withFamilyPathsFor(family1), false, null);
169  }
170
171  @Test(expected = DoNotRetryIOException.class)
172  public void shouldThrowErrorIfBadFamilySpecifiedAsFamilyPath() throws IOException {
173    testRegionWithFamilies()
174      .bulkLoadHFiles(asList(withInvalidColumnFamilyButProperHFileLocation(family1)), false, null);
175  }
176
177  @Test(expected = FileNotFoundException.class)
178  public void shouldThrowErrorIfHFileDoesNotExist() throws IOException {
179    List<Pair<byte[], String>> list = asList(withMissingHFileForFamily(family1));
180    testRegionWithFamilies(family1).bulkLoadHFiles(list, false, null);
181  }
182
183  // after HBASE-24021 will throw FileNotFoundException, not MultipleIOException
184  @Test(expected = FileNotFoundException.class)
185  public void shouldThrowErrorIfMultiHFileDoesNotExist() throws IOException {
186    List<Pair<byte[], String>> list = new ArrayList<>();
187    list.addAll(asList(withMissingHFileForFamily(family1)));
188    list.addAll(asList(withMissingHFileForFamily(family2)));
189    testRegionWithFamilies(family1, family2).bulkLoadHFiles(list, false, null);
190  }
191}