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.wal;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.testclassification.RegionServerTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.apache.hadoop.hbase.wal.AsyncFSWALProvider;
031import org.apache.hadoop.hbase.wal.AsyncFSWALProvider.AsyncWriter;
032import org.apache.hadoop.hbase.wal.WALFactory;
033import org.junit.jupiter.api.AfterAll;
034import org.junit.jupiter.api.BeforeAll;
035import org.junit.jupiter.api.BeforeEach;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038import org.junit.jupiter.api.TestInfo;
039
040import org.apache.hbase.thirdparty.io.netty.channel.Channel;
041import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
042import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
043import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
044
045@Tag(RegionServerTests.TAG)
046@Tag(MediumTests.TAG)
047public class TestCombinedAsyncWriter {
048
049  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
050
051  private static EventLoopGroup EVENT_LOOP_GROUP;
052
053  private static Class<? extends Channel> CHANNEL_CLASS;
054
055  private static WALFactory WALS;
056
057  private String name;
058
059  @BeforeEach
060  public void initTestName(TestInfo testInfo) {
061    name = testInfo.getTestMethod().get().getName();
062  }
063
064  @BeforeAll
065  public static void setUpBeforeClass() throws Exception {
066    EVENT_LOOP_GROUP = new NioEventLoopGroup();
067    CHANNEL_CLASS = NioSocketChannel.class;
068    UTIL.startMiniDFSCluster(3);
069    UTIL.getTestFileSystem().mkdirs(UTIL.getDataTestDirOnTestFS());
070    WALS = new WALFactory(UTIL.getConfiguration(), TestCombinedAsyncWriter.class.getSimpleName());
071  }
072
073  @AfterAll
074  public static void tearDownAfterClass() throws Exception {
075    if (WALS != null) {
076      WALS.close();
077    }
078    EVENT_LOOP_GROUP.shutdownGracefully().syncUninterruptibly();
079    UTIL.shutdownMiniDFSCluster();
080  }
081
082  @Test
083  public void testWithTrailer() throws IOException {
084    doTest(true);
085  }
086
087  @Test
088  public void testWithoutTrailer() throws IOException {
089    doTest(false);
090  }
091
092  private Path getPath(int index) throws IOException {
093    String methodName = name.replaceAll("[^A-Za-z0-9_-]", "_");
094    return new Path(UTIL.getDataTestDirOnTestFS(), methodName + "-" + index);
095  }
096
097  private void doTest(boolean withTrailer) throws IOException {
098    int columnCount = 5;
099    int recordCount = 5;
100    TableName tableName = TableName.valueOf("tablename");
101    byte[] row = Bytes.toBytes("row");
102    long timestamp = EnvironmentEdgeManager.currentTime();
103    Path path1 = getPath(1);
104    Path path2 = getPath(2);
105    FileSystem fs = UTIL.getTestFileSystem();
106    Configuration conf = UTIL.getConfiguration();
107    try (
108      AsyncWriter writer1 = AsyncFSWALProvider.createAsyncWriter(conf, fs, path1, false,
109        EVENT_LOOP_GROUP.next(), CHANNEL_CLASS);
110      AsyncWriter writer2 = AsyncFSWALProvider.createAsyncWriter(conf, fs, path2, false,
111        EVENT_LOOP_GROUP.next(), CHANNEL_CLASS);
112      CombinedAsyncWriter writer = CombinedAsyncWriter.create(writer1, writer2)) {
113      ProtobufLogTestHelper.doWrite(new WriterOverAsyncWriter(writer), withTrailer, tableName,
114        columnCount, recordCount, row, timestamp);
115      try (ProtobufWALStreamReader reader =
116        (ProtobufWALStreamReader) WALS.createStreamReader(fs, path1)) {
117        ProtobufLogTestHelper.doRead(reader, withTrailer, tableName, columnCount, recordCount, row,
118          timestamp);
119      }
120      try (ProtobufWALStreamReader reader =
121        (ProtobufWALStreamReader) WALS.createStreamReader(fs, path2)) {
122        ProtobufLogTestHelper.doRead(reader, withTrailer, tableName, columnCount, recordCount, row,
123          timestamp);
124      }
125    }
126  }
127}