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