001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.wal; 020 021import java.io.Closeable; 022import java.io.IOException; 023import java.util.List; 024import java.util.OptionalLong; 025import java.util.concurrent.CompletableFuture; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.client.RegionInfo; 028import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener; 029import org.apache.hadoop.hbase.replication.regionserver.WALFileLengthProvider; 030import org.apache.yetus.audience.InterfaceAudience; 031 032/** 033 * The Write Ahead Log (WAL) stores all durable edits to the HRegion. This interface provides the 034 * entry point for all WAL implementors. 035 * <p> 036 * See {@link FSHLogProvider} for an example implementation. A single WALProvider will be used for 037 * retrieving multiple WALs in a particular region server and must be threadsafe. 038 */ 039@InterfaceAudience.Private 040public interface WALProvider { 041 042 /** 043 * Set up the provider to create wals. will only be called once per instance. 044 * @param factory factory that made us may not be null 045 * @param conf may not be null 046 * @param providerId differentiate between providers from one factory. may be null 047 */ 048 void init(WALFactory factory, Configuration conf, String providerId) throws IOException; 049 050 /** 051 * @param region the region which we want to get a WAL for it. Could be null. 052 * @return a WAL for writing entries for the given region. 053 */ 054 WAL getWAL(RegionInfo region) throws IOException; 055 056 /** 057 * @return the List of WALs that are used by this server 058 */ 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 // Writers are used internally. Users outside of the WAL should be relying on the 080 // interface provided by WAL. 081 interface Writer extends WriterBase { 082 void sync(boolean forceSync) throws IOException; 083 084 void append(WAL.Entry entry) throws IOException; 085 } 086 087 interface AsyncWriter extends WriterBase { 088 CompletableFuture<Long> sync(boolean forceSync); 089 090 void append(WAL.Entry entry); 091 } 092 093 /** 094 * Get number of the log files this provider is managing 095 */ 096 long getNumLogFiles(); 097 098 /** 099 * Get size of the log files this provider is managing 100 */ 101 long getLogFileSize(); 102 103 /** 104 * Add a {@link WALActionsListener}. 105 * <p> 106 * Notice that you must call this method before calling {@link #getWAL(RegionInfo)} as this method 107 * will not effect the {@link WAL} which has already been created. And as long as we can only it 108 * when initialization, it is not thread safe. 109 */ 110 void addWALActionsListener(WALActionsListener listener); 111 112 default WALFileLengthProvider getWALFileLengthProvider() { 113 return path -> getWALs().stream().map(w -> w.getLogFileSizeIfBeingWritten(path)) 114 .filter(o -> o.isPresent()).findAny().orElse(OptionalLong.empty()); 115 } 116}