Apache9 commented on code in PR #8877: URL: https://github.com/apache/hbase/pull/8877#discussion_r4564530415
########## hbase-server/src/test/java/org/apache/hadoop/hbase/master/procedure/TestCreateTableProcedureDescriptorWriteFailure.java: ########## @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.master.procedure; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.util.EnumSet; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.hadoop.fs.CreateFlag; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Options; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.permission.FsPermission; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.MetaTableAccessor; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.master.HMaster; +import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure; +import org.apache.hadoop.hbase.procedure2.Procedure; +import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; +import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; +import org.apache.hadoop.hbase.testclassification.MasterTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.util.CommonFSUtils; +import org.apache.hadoop.hbase.util.FSTableDescriptors; +import org.apache.hadoop.hbase.util.ModifyRegionUtils; +import org.apache.hadoop.hdfs.DistributedFileSystem; +import org.apache.hadoop.util.Progressable; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** + * Test for HBASE-30539: {@link CreateTableProcedure} must not move on to {@code hbase:meta} and + * region assignment when the table descriptor could not be written. + * <p> + * {@code FSTableDescriptors.writeTableDescriptor} retries 10 times. If every attempt fails it must + * throw, so that the create procedure stays in {@code CREATE_TABLE_WRITE_FS_LAYOUT} and retries, + * instead of publishing a table whose region servers can never load a descriptor ("Missing table + * descriptor for ...") and whose assign procedure retries forever. + */ +@Tag(MasterTests.TAG) +@Tag(MediumTests.TAG) +public class TestCreateTableProcedureDescriptorWriteFailure { + + private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); + + /** Same as {@code maxAttempts} in {@code FSTableDescriptors.writeTableDescriptor}. */ + private static final int WRITE_ATTEMPTS = 10; + + /** Substring identifying the file creations to fail; null means do not inject. */ + private static volatile String failPathContains = null; + /** Stop injecting once this many failures were injected; default is to fail forever. */ + private static volatile int maxInjectedFailures = Integer.MAX_VALUE; + private static final AtomicInteger INJECTED_FAILURES = new AtomicInteger(); + + /** + * A {@link DistributedFileSystem} that refuses to create any file whose path contains + * {@link #failPathContains}, simulating the NameNode answering "could only be written to 0 of the + * 1 minReplication nodes". + */ + public static class FailingTableInfoFileSystem extends DistributedFileSystem { + @Override + public FSDataOutputStream create(Path f, FsPermission permission, EnumSet<CreateFlag> cflags, + int bufferSize, short replication, long blockSize, Progressable progress, + Options.ChecksumOpt checksumOpt) throws IOException { + String marker = failPathContains; + if ( + marker != null && f.toString().contains(marker) + && INJECTED_FAILURES.get() < maxInjectedFailures + ) { + INJECTED_FAILURES.incrementAndGet(); + throw new IOException("Injected HDFS failure creating " + f); + } + return super.create(f, permission, cflags, bufferSize, replication, blockSize, progress, + checksumOpt); + } + } + + @BeforeAll + public static void setUp() throws Exception { + UTIL.getConfiguration().setClass("fs.hdfs.impl", FailingTableInfoFileSystem.class, + FileSystem.class); + UTIL.getConfiguration().setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1); + UTIL.startMiniCluster(1); + } + + @AfterAll + public static void tearDownCluster() throws Exception { + failPathContains = null; + UTIL.shutdownMiniCluster(); + } + + @AfterEach + public void resetInjection() { + failPathContains = null; + maxInjectedFailures = Integer.MAX_VALUE; + INJECTED_FAILURES.set(0); + } + + @Test + public void testCreateTableDoesNotProceedWhenTableDescriptorWriteFails() throws Exception { Review Comment: Better also add javadoc to describe the details? Like the two methods below. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
