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.Map;
024import java.util.Set;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
028import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
029import org.apache.hadoop.hbase.regionserver.wal.WALCoprocessorHost;
030import org.apache.hadoop.hbase.replication.regionserver.WALFileLengthProvider;
031import org.apache.yetus.audience.InterfaceAudience;
032import org.apache.yetus.audience.InterfaceStability;
033
034import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
035
036/**
037 * A Write Ahead Log (WAL) provides service for reading, writing waledits. This interface provides
038 * APIs for WAL users (such as RegionServer) to use the WAL (do append, sync, etc).
039 *
040 * Note that some internals, such as log rolling and performance evaluation tools, will use
041 * WAL.equals to determine if they have already seen a given WAL.
042 */
043@InterfaceAudience.Private
044@InterfaceStability.Evolving
045public interface WAL extends Closeable, WALFileLengthProvider {
046
047  /**
048   * Registers WALActionsListener
049   */
050  void registerWALActionsListener(final WALActionsListener listener);
051
052  /**
053   * Unregisters WALActionsListener
054   */
055  boolean unregisterWALActionsListener(final WALActionsListener listener);
056
057  /**
058   * Roll the log writer. That is, start writing log messages to a new file.
059   *
060   * <p/>
061   * The implementation is synchronized in order to make sure there's one rollWriter
062   * running at any given time.
063   *
064   * @return If lots of logs, flush the stores of returned regions so next time through we
065   *         can clean logs. Returns null if nothing to flush. Names are actual
066   *         region names as returned by {@link RegionInfo#getEncodedName()}
067   */
068  Map<byte[], List<byte[]>> rollWriter() throws FailedLogCloseException, IOException;
069
070  /**
071   * Roll the log writer. That is, start writing log messages to a new file.
072   *
073   * <p/>
074   * The implementation is synchronized in order to make sure there's one rollWriter
075   * running at any given time.
076   *
077   * @param force
078   *          If true, force creation of a new writer even if no entries have
079   *          been written to the current writer
080   * @return If lots of logs, flush the stores of returned regions so next time through we
081   *         can clean logs. Returns null if nothing to flush. Names are actual
082   *         region names as returned by {@link RegionInfo#getEncodedName()}
083   */
084  Map<byte[], List<byte[]>> rollWriter(boolean force) throws IOException;
085
086  /**
087   * Stop accepting new writes. If we have unsynced writes still in buffer, sync them.
088   * Extant edits are left in place in backing storage to be replayed later.
089   */
090  void shutdown() throws IOException;
091
092  /**
093   * Caller no longer needs any edits from this WAL. Implementers are free to reclaim
094   * underlying resources after this call; i.e. filesystem based WALs can archive or
095   * delete files.
096   */
097  @Override
098  void close() throws IOException;
099
100  /**
101   * Append a set of data edits to the WAL. 'Data' here means that the content in the edits will
102   * also have transitioned through the memstore.
103   * <p/>
104   * The WAL is not flushed/sync'd after this transaction completes BUT on return this edit must
105   * have its region edit/sequence id assigned else it messes up our unification of mvcc and
106   * sequenceid. On return <code>key</code> will have the region edit/sequence id filled in.
107   * @param info the regioninfo associated with append
108   * @param key Modified by this call; we add to it this edits region edit/sequence id.
109   * @param edits Edits to append. MAY CONTAIN NO EDITS for case where we want to get an edit
110   *          sequence id that is after all currently appended edits.
111   * @return Returns a 'transaction id' and <code>key</code> will have the region edit/sequence id
112   *         in it.
113   * @see #appendMarker(RegionInfo, WALKeyImpl, WALEdit)
114   */
115  long appendData(RegionInfo info, WALKeyImpl key, WALEdit edits) throws IOException;
116
117  /**
118   * Append an operational 'meta' event marker edit to the WAL. A marker meta edit could
119   * be a FlushDescriptor, a compaction marker, or a region event marker; e.g. region open
120   * or region close. The difference between a 'marker' append and a 'data' append as in
121   * {@link #appendData(RegionInfo, WALKeyImpl, WALEdit)}is that a marker will not have
122   * transitioned through the memstore.
123   * <p/>
124   * The WAL is not flushed/sync'd after this transaction completes BUT on return this edit must
125   * have its region edit/sequence id assigned else it messes up our unification of mvcc and
126   * sequenceid. On return <code>key</code> will have the region edit/sequence id filled in.
127   * @param info the regioninfo associated with append
128   * @param key Modified by this call; we add to it this edits region edit/sequence id.
129   * @param edits Edits to append. MAY CONTAIN NO EDITS for case where we want to get an edit
130   *          sequence id that is after all currently appended edits.
131   * @return Returns a 'transaction id' and <code>key</code> will have the region edit/sequence id
132   *         in it.
133   * @see #appendData(RegionInfo, WALKeyImpl, WALEdit)
134   */
135  long appendMarker(RegionInfo info, WALKeyImpl key, WALEdit edits) throws IOException;
136
137  /**
138   * updates the seuence number of a specific store.
139   * depending on the flag: replaces current seq number if the given seq id is bigger,
140   * or even if it is lower than existing one
141   */
142  void updateStore(byte[] encodedRegionName, byte[] familyName, Long sequenceid,
143      boolean onlyIfGreater);
144
145  /**
146   * Sync what we have in the WAL.
147   */
148  void sync() throws IOException;
149
150  /**
151   * Sync the WAL if the txId was not already sync'd.
152   * @param txid Transaction id to sync to.
153   */
154  void sync(long txid) throws IOException;
155
156  /**
157   * @param forceSync Flag to force sync rather than flushing to the buffer. Example - Hadoop hflush
158   *          vs hsync.
159   */
160  default void sync(boolean forceSync) throws IOException {
161    sync();
162  }
163
164  /**
165   * @param txid Transaction id to sync to.
166   * @param forceSync Flag to force sync rather than flushing to the buffer. Example - Hadoop hflush
167   *          vs hsync.
168   */
169  default void sync(long txid, boolean forceSync) throws IOException {
170    sync(txid);
171  }
172
173  /**
174   * WAL keeps track of the sequence numbers that are as yet not flushed im memstores
175   * in order to be able to do accounting to figure which WALs can be let go. This method tells WAL
176   * that some region is about to flush. The flush can be the whole region or for a column family
177   * of the region only.
178   *
179   * <p>Currently, it is expected that the update lock is held for the region; i.e. no
180   * concurrent appends while we set up cache flush.
181   * @param families Families to flush. May be a subset of all families in the region.
182   * @return Returns {@link HConstants#NO_SEQNUM} if we are flushing the whole region OR if
183   * we are flushing a subset of all families but there are no edits in those families not
184   * being flushed; in other words, this is effectively same as a flush of all of the region
185   * though we were passed a subset of regions. Otherwise, it returns the sequence id of the
186   * oldest/lowest outstanding edit.
187   * @see #completeCacheFlush(byte[], long)
188   * @see #abortCacheFlush(byte[])
189   */
190  Long startCacheFlush(final byte[] encodedRegionName, Set<byte[]> families);
191
192  Long startCacheFlush(final byte[] encodedRegionName, Map<byte[], Long> familyToSeq);
193
194  /**
195   * Complete the cache flush.
196   * @param encodedRegionName Encoded region name.
197   * @param maxFlushedSeqId The maxFlushedSeqId for this flush. There is no edit in memory that is
198   *          less that this sequence id.
199   * @see #startCacheFlush(byte[], Set)
200   * @see #abortCacheFlush(byte[])
201   */
202  void completeCacheFlush(final byte[] encodedRegionName, long maxFlushedSeqId);
203
204  /**
205   * Abort a cache flush. Call if the flush fails. Note that the only recovery
206   * for an aborted flush currently is a restart of the regionserver so the
207   * snapshot content dropped by the failure gets restored to the memstore.
208   * @param encodedRegionName Encoded region name.
209   */
210  void abortCacheFlush(byte[] encodedRegionName);
211
212  /**
213   * @return Coprocessor host.
214   */
215  WALCoprocessorHost getCoprocessorHost();
216
217  /**
218   * Gets the earliest unflushed sequence id in the memstore for the region.
219   * @param encodedRegionName The region to get the number for.
220   * @return The earliest/lowest/oldest sequence id if present, HConstants.NO_SEQNUM if absent.
221   * @deprecated Since version 1.2.0. Removing because not used and exposes subtle internal
222   * workings. Use {@link #getEarliestMemStoreSeqNum(byte[], byte[])}
223   */
224  @VisibleForTesting
225  @Deprecated
226  long getEarliestMemStoreSeqNum(byte[] encodedRegionName);
227
228  /**
229   * Gets the earliest unflushed sequence id in the memstore for the store.
230   * @param encodedRegionName The region to get the number for.
231   * @param familyName The family to get the number for.
232   * @return The earliest/lowest/oldest sequence id if present, HConstants.NO_SEQNUM if absent.
233   */
234  long getEarliestMemStoreSeqNum(byte[] encodedRegionName, byte[] familyName);
235
236  /**
237   * Human readable identifying information about the state of this WAL.
238   * Implementors are encouraged to include information appropriate for debugging.
239   * Consumers are advised not to rely on the details of the returned String; it does
240   * not have a defined structure.
241   */
242  @Override
243  String toString();
244
245  /**
246   * When outside clients need to consume persisted WALs, they rely on a provided
247   * Reader.
248   */
249  interface Reader extends Closeable {
250    Entry next() throws IOException;
251    Entry next(Entry reuse) throws IOException;
252    void seek(long pos) throws IOException;
253    long getPosition() throws IOException;
254    void reset() throws IOException;
255  }
256
257  /**
258   * Utility class that lets us keep track of the edit with it's key.
259   */
260  class Entry {
261    private final WALEdit edit;
262    private final WALKeyImpl key;
263
264    public Entry() {
265      this(new WALKeyImpl(), new WALEdit());
266    }
267
268    /**
269     * Constructor for both params
270     *
271     * @param edit log's edit
272     * @param key log's key
273     */
274    public Entry(WALKeyImpl key, WALEdit edit) {
275      this.key = key;
276      this.edit = edit;
277    }
278
279    /**
280     * Gets the edit
281     *
282     * @return edit
283     */
284    public WALEdit getEdit() {
285      return edit;
286    }
287
288    /**
289     * Gets the key
290     *
291     * @return key
292     */
293    public WALKeyImpl getKey() {
294      return key;
295    }
296
297    @Override
298    public String toString() {
299      return this.key + "=" + this.edit;
300    }
301  }
302}