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.wal; 019 020import java.io.Closeable; 021import java.io.IOException; 022import java.util.List; 023import java.util.OptionalLong; 024import java.util.concurrent.CompletableFuture; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.Abortable; 027import org.apache.hadoop.hbase.client.RegionInfo; 028import org.apache.hadoop.hbase.regionserver.wal.AsyncFSWAL; 029import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener; 030import org.apache.hadoop.hbase.replication.regionserver.WALFileLengthProvider; 031import org.apache.yetus.audience.InterfaceAudience; 032 033/** 034 * The Write Ahead Log (WAL) stores all durable edits to the HRegion. This interface provides the 035 * entry point for all WAL implementors. 036 * <p> 037 * See {@link FSHLogProvider} for an example implementation. A single WALProvider will be used for 038 * retrieving multiple WALs in a particular region server and must be threadsafe. 039 */ 040@InterfaceAudience.Private 041public interface WALProvider { 042 043 /** 044 * Set up the provider to create wals. will only be called once per instance. 045 * @param factory factory that made us may not be null 046 * @param conf may not be null 047 * @param providerId differentiate between providers from one factory. may be null 048 */ 049 void init(WALFactory factory, Configuration conf, String providerId, Abortable abortable) 050 throws IOException; 051 052 /** 053 * @param region the region which we want to get a WAL for it. Could be null. 054 * @return a WAL for writing entries for the given region. 055 */ 056 WAL getWAL(RegionInfo region) throws IOException; 057 058 /** Returns the List of WALs that are used by this server */ 059 List<WAL> getWALs(); 060 061 /** 062 * persist outstanding WALs to storage and stop accepting new appends. This method serves as 063 * shorthand for sending a sync to every WAL provided by a given implementation. Those WALs will 064 * also stop accepting new writes. 065 */ 066 void shutdown() throws IOException; 067 068 /** 069 * shutdown utstanding WALs and clean up any persisted state. Call this method only when you will 070 * not need to replay any of the edits to the WALs from this provider. After this call completes, 071 * the underlying resources should have been reclaimed. 072 */ 073 void close() throws IOException; 074 075 interface WriterBase extends Closeable { 076 long getLength(); 077 078 /** 079 * NOTE: We add this method for {@link WALFileLengthProvider} used for replication, considering 080 * the case if we use {@link AsyncFSWAL},we write to 3 DNs concurrently, according to the 081 * visibility guarantee of HDFS, the data will be available immediately when arriving at DN 082 * since all the DNs will be considered as the last one in pipeline. This means replication may 083 * read uncommitted data and replicate it to the remote cluster and cause data inconsistency. 084 * The method {@link WriterBase#getLength} may return length which just in hdfs client buffer 085 * and not successfully synced to HDFS, so we use this method to return the length successfully 086 * synced to HDFS and replication thread could only read writing WAL file limited by this 087 * length. see also HBASE-14004 and this document for more details: 088 * https://docs.google.com/document/d/11AyWtGhItQs6vsLRIx32PwTxmBY3libXwGXI25obVEY/edit# 089 * @return byteSize successfully synced to underlying filesystem. 090 */ 091 long getSyncedLength(); 092 } 093 094 // Writers are used internally. Users outside of the WAL should be relying on the 095 // interface provided by WAL. 096 interface Writer extends WriterBase { 097 void sync(boolean forceSync) throws IOException; 098 099 void append(WAL.Entry entry) throws IOException; 100 } 101 102 interface AsyncWriter extends WriterBase { 103 CompletableFuture<Long> sync(boolean forceSync); 104 105 void append(WAL.Entry entry); 106 } 107 108 /** 109 * Get number of the log files this provider is managing 110 */ 111 long getNumLogFiles(); 112 113 /** 114 * Get size of the log files this provider is managing 115 */ 116 long getLogFileSize(); 117 118 /** 119 * Add a {@link WALActionsListener}. 120 * <p> 121 * Notice that you must call this method before calling {@link #getWAL(RegionInfo)} as this method 122 * will not effect the {@link WAL} which has already been created. And as long as we can only it 123 * when initialization, it is not thread safe. 124 */ 125 void addWALActionsListener(WALActionsListener listener); 126 127 default WALFileLengthProvider getWALFileLengthProvider() { 128 return path -> getWALs().stream().map(w -> w.getLogFileSizeIfBeingWritten(path)) 129 .filter(o -> o.isPresent()).findAny().orElse(OptionalLong.empty()); 130 } 131}