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.regionserver.RegionServerServices; 026import org.apache.hadoop.hbase.testclassification.MediumTests; 027import org.apache.hadoop.hbase.wal.WALProvider.Writer; 028import org.junit.ClassRule; 029import org.junit.experimental.categories.Category; 030 031@Category({ RegionServerServices.class, MediumTests.class }) 032public class TestFSHLogDurability extends WALDurabilityTestBase<CustomFSHLog> { 033 034 @ClassRule 035 public static final HBaseClassTestRule CLASS_RULE = 036 HBaseClassTestRule.forClass(TestFSHLogDurability.class); 037 038 @Override 039 protected CustomFSHLog getWAL(FileSystem fs, Path root, String logDir, Configuration conf) 040 throws IOException { 041 CustomFSHLog wal = new CustomFSHLog(fs, root, logDir, conf); 042 wal.init(); 043 return wal; 044 } 045 046 @Override 047 protected void resetSyncFlag(CustomFSHLog wal) { 048 wal.resetSyncFlag(); 049 } 050 051 @Override 052 protected Boolean getSyncFlag(CustomFSHLog wal) { 053 return wal.getSyncFlag(); 054 } 055 056 @Override 057 protected Boolean getWriterSyncFlag(CustomFSHLog wal) { 058 return wal.getWriterSyncFlag(); 059 } 060} 061 062class CustomFSHLog extends FSHLog { 063 private Boolean syncFlag; 064 065 private Boolean writerSyncFlag; 066 067 public CustomFSHLog(FileSystem fs, Path root, String logDir, Configuration conf) 068 throws IOException { 069 super(fs, root, logDir, conf); 070 } 071 072 @Override 073 protected Writer createWriterInstance(FileSystem fs, Path path) throws IOException { 074 Writer writer = super.createWriterInstance(fs, path); 075 return new Writer() { 076 077 @Override 078 public void close() throws IOException { 079 writer.close(); 080 } 081 082 @Override 083 public long getLength() { 084 return writer.getLength(); 085 } 086 087 @Override 088 public long getSyncedLength() { 089 return writer.getSyncedLength(); 090 } 091 092 @Override 093 public void sync(boolean forceSync) throws IOException { 094 writerSyncFlag = forceSync; 095 writer.sync(forceSync); 096 } 097 098 @Override 099 public void append(Entry entry) throws IOException { 100 writer.append(entry); 101 } 102 }; 103 } 104 105 @Override 106 protected void doSync(boolean forceSync) throws IOException { 107 syncFlag = forceSync; 108 super.doSync(forceSync); 109 } 110 111 @Override 112 protected void doSync(long txid, boolean forceSync) throws IOException { 113 syncFlag = forceSync; 114 super.doSync(txid, forceSync); 115 } 116 117 void resetSyncFlag() { 118 this.syncFlag = null; 119 this.writerSyncFlag = null; 120 } 121 122 Boolean getSyncFlag() { 123 return syncFlag; 124 } 125 126 Boolean getWriterSyncFlag() { 127 return writerSyncFlag; 128 } 129}