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