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 static org.junit.Assert.assertEquals; 021 022import java.io.IOException; 023import org.apache.hadoop.fs.FileStatus; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.util.Bytes; 029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 030import org.apache.hadoop.hbase.wal.WALFactory; 031import org.apache.hadoop.hbase.wal.WALProvider; 032import org.junit.After; 033import org.junit.AfterClass; 034import org.junit.Before; 035import org.junit.BeforeClass; 036import org.junit.Rule; 037import org.junit.Test; 038import org.junit.rules.TestName; 039 040import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.WALTrailer; 041 042/** 043 * WAL tests that can be reused across providers. 044 */ 045public abstract class AbstractTestProtobufLog { 046 protected static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 047 048 protected FileSystem fs; 049 protected Path dir; 050 protected WALFactory wals; 051 052 @Rule 053 public final TestName currentTest = new TestName(); 054 055 @Before 056 public void setUp() throws Exception { 057 fs = TEST_UTIL.getDFSCluster().getFileSystem(); 058 dir = new Path(TEST_UTIL.createRootDir(), currentTest.getMethodName()); 059 wals = new WALFactory(TEST_UTIL.getConfiguration(), currentTest.getMethodName()); 060 } 061 062 @After 063 public void tearDown() throws Exception { 064 wals.close(); 065 FileStatus[] entries = fs.listStatus(new Path("/")); 066 for (FileStatus dir : entries) { 067 fs.delete(dir.getPath(), true); 068 } 069 } 070 071 @BeforeClass 072 public static void setUpBeforeClass() throws Exception { 073 // Make block sizes small. 074 TEST_UTIL.getConfiguration().setInt("dfs.blocksize", 1024 * 1024); 075 // needed for testAppendClose() 076 // quicker heartbeat interval for faster DN death notification 077 TEST_UTIL.getConfiguration().setInt("dfs.namenode.heartbeat.recheck-interval", 5000); 078 TEST_UTIL.getConfiguration().setInt("dfs.heartbeat.interval", 1); 079 TEST_UTIL.getConfiguration().setInt("dfs.client.socket-timeout", 5000); 080 081 // faster failover with cluster.shutdown();fs.close() idiom 082 TEST_UTIL.getConfiguration().setInt("dfs.client.block.recovery.retries", 1); 083 TEST_UTIL.startMiniDFSCluster(3); 084 } 085 086 @AfterClass 087 public static void tearDownAfterClass() throws Exception { 088 TEST_UTIL.shutdownMiniCluster(); 089 } 090 091 /** 092 * Reads the WAL with and without WALTrailer. 093 */ 094 @Test 095 public void testWALTrailer() throws IOException { 096 // make sure that the size for WALTrailer is 0, we need this assumption when reading partial 097 // WALTrailer 098 assertEquals(0, WALTrailer.newBuilder().build().getSerializedSize()); 099 // read With trailer. 100 doRead(true); 101 // read without trailer 102 doRead(false); 103 } 104 105 /** 106 * Appends entries in the WAL and reads it. 107 * @param withTrailer If 'withTrailer' is true, it calls a close on the WALwriter before reading 108 * so that a trailer is appended to the WAL. Otherwise, it starts reading after 109 * the sync call. This means that reader is not aware of the trailer. In this 110 * scenario, if the reader tries to read the trailer in its next() call, it 111 * returns false from ProtoBufLogReader. 112 */ 113 private void doRead(boolean withTrailer) throws IOException { 114 int columnCount = 5; 115 int recordCount = 5; 116 TableName tableName = TableName.valueOf("tablename"); 117 byte[] row = Bytes.toBytes("row"); 118 long timestamp = EnvironmentEdgeManager.currentTime(); 119 Path path = new Path(dir, "tempwal"); 120 // delete the log if already exists, for test only 121 fs.delete(path, true); 122 fs.mkdirs(dir); 123 try (WALProvider.Writer writer = createWriter(path)) { 124 ProtobufLogTestHelper.doWrite(writer, withTrailer, tableName, columnCount, recordCount, row, 125 timestamp); 126 try (ProtobufWALStreamReader reader = 127 (ProtobufWALStreamReader) wals.createStreamReader(fs, path)) { 128 ProtobufLogTestHelper.doRead(reader, withTrailer, tableName, columnCount, recordCount, row, 129 timestamp); 130 } 131 } 132 } 133 134 protected abstract WALProvider.Writer createWriter(Path path) throws IOException; 135}