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(Path path) throws IOException {
074    Writer writer = super.createWriterInstance(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 void sync(boolean forceSync) throws IOException {
089        writerSyncFlag = forceSync;
090        writer.sync(forceSync);
091      }
092
093      @Override
094      public void append(Entry entry) throws IOException {
095        writer.append(entry);
096      }
097    };
098  }
099
100  @Override
101  public void sync(boolean forceSync) throws IOException {
102    syncFlag = forceSync;
103    super.sync(forceSync);
104  }
105
106  @Override
107  public void sync(long txid, boolean forceSync) throws IOException {
108    syncFlag = forceSync;
109    super.sync(txid, forceSync);
110  }
111
112  void resetSyncFlag() {
113    this.syncFlag = null;
114    this.writerSyncFlag = null;
115  }
116
117  Boolean getSyncFlag() {
118    return syncFlag;
119  }
120
121  Boolean getWriterSyncFlag() {
122    return writerSyncFlag;
123  }
124}