|  | 
|  | 1 | +/** | 
|  | 2 | + * Licensed to the Apache Software Foundation (ASF) under one | 
|  | 3 | + * or more contributor license agreements.  See the NOTICE file | 
|  | 4 | + * distributed with this work for additional information | 
|  | 5 | + * regarding copyright ownership.  The ASF licenses this file | 
|  | 6 | + * to you under the Apache License, Version 2.0 (the | 
|  | 7 | + * "License"); you may not use this file except in compliance | 
|  | 8 | + * with the License.  You may obtain a copy of the License at | 
|  | 9 | + * | 
|  | 10 | + *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | + * | 
|  | 12 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 13 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 15 | + * See the License for the specific language governing permissions and | 
|  | 16 | + * limitations under the License. | 
|  | 17 | + */ | 
|  | 18 | +package org.apache.hadoop.hbase.wal; | 
|  | 19 | + | 
|  | 20 | +import java.io.IOException; | 
|  | 21 | +import java.util.List; | 
|  | 22 | + | 
|  | 23 | +import org.apache.hadoop.conf.Configuration; | 
|  | 24 | +import org.apache.hadoop.fs.FileSystem; | 
|  | 25 | +import org.apache.hadoop.fs.Path; | 
|  | 26 | +import org.apache.hadoop.hbase.HBaseClassTestRule; | 
|  | 27 | +import org.apache.hadoop.hbase.io.ByteBuffAllocator; | 
|  | 28 | +import org.apache.hadoop.hbase.ipc.RpcServerFactory; | 
|  | 29 | +import org.apache.hadoop.hbase.ipc.SimpleRpcServer; | 
|  | 30 | +import org.apache.hadoop.hbase.regionserver.HRegion; | 
|  | 31 | +import org.apache.hadoop.hbase.regionserver.wal.FSHLog; | 
|  | 32 | +import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener; | 
|  | 33 | +import org.apache.hadoop.hbase.testclassification.MediumTests; | 
|  | 34 | +import org.apache.hadoop.hbase.testclassification.RegionServerTests; | 
|  | 35 | +import org.apache.hadoop.hbase.util.CommonFSUtils; | 
|  | 36 | +import org.junit.AfterClass; | 
|  | 37 | +import org.junit.BeforeClass; | 
|  | 38 | +import org.junit.ClassRule; | 
|  | 39 | +import org.junit.experimental.categories.Category; | 
|  | 40 | + | 
|  | 41 | +@Category({ RegionServerTests.class, MediumTests.class }) | 
|  | 42 | +public class TestFSHLogCorruptionWithMultiPutDueToDanglingByteBuffer | 
|  | 43 | +  extends WALCorruptionWithMultiPutDueToDanglingByteBufferTestBase { | 
|  | 44 | + | 
|  | 45 | +  @ClassRule | 
|  | 46 | +  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule | 
|  | 47 | +    .forClass(TestFSHLogCorruptionWithMultiPutDueToDanglingByteBuffer.class); | 
|  | 48 | + | 
|  | 49 | +  public static final class PauseWAL extends FSHLog { | 
|  | 50 | + | 
|  | 51 | +    private int testTableWalAppendsCount = 0; | 
|  | 52 | + | 
|  | 53 | +    public PauseWAL(FileSystem fs, Path rootDir, String logDir, String archiveDir, | 
|  | 54 | +      Configuration conf, List<WALActionsListener> listeners, boolean failIfWALExists, | 
|  | 55 | +      String prefix, String suffix) throws IOException { | 
|  | 56 | +      super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix); | 
|  | 57 | +    } | 
|  | 58 | + | 
|  | 59 | +    @Override | 
|  | 60 | +    protected void atHeadOfRingBufferEventHandlerAppend() { | 
|  | 61 | +      // Let the 1st Append go through. The write thread will wait for this to go through before | 
|  | 62 | +      // calling further put() | 
|  | 63 | +      if (ARRIVE != null) { // Means appends as part of puts in testcase | 
|  | 64 | +        // Sleep for a second so that RS handler thread put all the mini batch WAL appends to ring | 
|  | 65 | +        // buffer. | 
|  | 66 | +        if (testTableWalAppendsCount == 0) { | 
|  | 67 | +          try { | 
|  | 68 | +            Thread.sleep(1000); | 
|  | 69 | +          } catch (InterruptedException e) { | 
|  | 70 | +          } | 
|  | 71 | +        } | 
|  | 72 | +        // Let the first minibatch write go through. When 2nd one comes, notify the waiting test | 
|  | 73 | +        // case for doing further batch puts and make this WAL append thread to pause | 
|  | 74 | +        if (testTableWalAppendsCount == 1) { | 
|  | 75 | +          ARRIVE.countDown(); | 
|  | 76 | +          try { | 
|  | 77 | +            RESUME.await(); | 
|  | 78 | +          } catch (InterruptedException e) { | 
|  | 79 | +          } | 
|  | 80 | +        } | 
|  | 81 | +        testTableWalAppendsCount++; | 
|  | 82 | +      } | 
|  | 83 | +    } | 
|  | 84 | +  } | 
|  | 85 | + | 
|  | 86 | +  public static final class PauseWALProvider extends AbstractFSWALProvider<PauseWAL> { | 
|  | 87 | + | 
|  | 88 | +    @Override | 
|  | 89 | +    protected PauseWAL createWAL() throws IOException { | 
|  | 90 | +      return new PauseWAL(CommonFSUtils.getWALFileSystem(conf), CommonFSUtils.getWALRootDir(conf), | 
|  | 91 | +        getWALDirectoryName(factory.factoryId), getWALArchiveDirectoryName(conf, factory.factoryId), | 
|  | 92 | +        conf, listeners, true, logPrefix, | 
|  | 93 | +        META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null); | 
|  | 94 | +    } | 
|  | 95 | + | 
|  | 96 | +    @Override | 
|  | 97 | +    protected void doInit(Configuration conf) throws IOException { | 
|  | 98 | +    } | 
|  | 99 | +  } | 
|  | 100 | + | 
|  | 101 | +  @BeforeClass | 
|  | 102 | +  public static void setUp() throws Exception { | 
|  | 103 | +    UTIL.getConfiguration().setClass(WALFactory.WAL_PROVIDER, PauseWALProvider.class, | 
|  | 104 | +      WALProvider.class); | 
|  | 105 | +    UTIL.getConfiguration().setInt(HRegion.HBASE_REGIONSERVER_MINIBATCH_SIZE, 1); | 
|  | 106 | +    UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, | 
|  | 107 | +      SimpleRpcServer.class.getName()); | 
|  | 108 | +    UTIL.getConfiguration().setInt(ByteBuffAllocator.MAX_BUFFER_COUNT_KEY, 1); | 
|  | 109 | +    UTIL.getConfiguration().setInt(ByteBuffAllocator.BUFFER_SIZE_KEY, 1024); | 
|  | 110 | +    UTIL.getConfiguration().setInt(ByteBuffAllocator.MIN_ALLOCATE_SIZE_KEY, 500); | 
|  | 111 | +    UTIL.startMiniCluster(1); | 
|  | 112 | +    UTIL.createTable(TABLE_NAME, CF); | 
|  | 113 | +    UTIL.waitTableAvailable(TABLE_NAME); | 
|  | 114 | +  } | 
|  | 115 | + | 
|  | 116 | +  @AfterClass | 
|  | 117 | +  public static void tearDown() throws Exception { | 
|  | 118 | +    UTIL.shutdownMiniCluster(); | 
|  | 119 | +  } | 
|  | 120 | +} | 
|  | 121 | + | 
0 commit comments