View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.wal;
21  
22  import java.io.Closeable;
23  import java.io.IOException;
24  import java.util.Set;
25  
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  // imports we use from yet-to-be-moved regionsever.wal
32  import org.apache.hadoop.hbase.regionserver.wal.CompressionContext;
33  import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
34  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
35  import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
36  import org.apache.hadoop.hbase.regionserver.wal.WALCoprocessorHost;
37  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
38  
39  import com.google.common.annotations.VisibleForTesting;
40  
41  /**
42   * A Write Ahead Log (WAL) provides service for reading, writing waledits. This interface provides
43   * APIs for WAL users (such as RegionServer) to use the WAL (do append, sync, etc).
44   *
45   * Note that some internals, such as log rolling and performance evaluation tools, will use
46   * WAL.equals to determine if they have already seen a given WAL.
47   */
48  @InterfaceAudience.Private
49  @InterfaceStability.Evolving
50  public interface WAL extends Closeable {
51  
52    /**
53     * Registers WALActionsListener
54     */
55    void registerWALActionsListener(final WALActionsListener listener);
56  
57    /**
58     * Unregisters WALActionsListener
59     */
60    boolean unregisterWALActionsListener(final WALActionsListener listener);
61  
62    /**
63     * Roll the log writer. That is, start writing log messages to a new file.
64     *
65     * <p>
66     * The implementation is synchronized in order to make sure there's one rollWriter
67     * running at any given time.
68     *
69     * @return If lots of logs, flush the returned regions so next time through we
70     *         can clean logs. Returns null if nothing to flush. Names are actual
71     *         region names as returned by {@link HRegionInfo#getEncodedName()}
72     */
73    byte[][] rollWriter() throws FailedLogCloseException, IOException;
74  
75    /**
76     * Roll the log writer. That is, start writing log messages to a new file.
77     *
78     * <p>
79     * The implementation is synchronized in order to make sure there's one rollWriter
80     * running at any given time.
81     *
82     * @param force
83     *          If true, force creation of a new writer even if no entries have
84     *          been written to the current writer
85     * @return If lots of logs, flush the returned regions so next time through we
86     *         can clean logs. Returns null if nothing to flush. Names are actual
87     *         region names as returned by {@link HRegionInfo#getEncodedName()}
88     */
89    byte[][] rollWriter(boolean force) throws FailedLogCloseException, IOException;
90  
91    /**
92     * Stop accepting new writes. If we have unsynced writes still in buffer, sync them.
93     * Extant edits are left in place in backing storage to be replayed later.
94     */
95    void shutdown() throws IOException;
96  
97    /**
98     * Caller no longer needs any edits from this WAL. Implementers are free to reclaim
99     * underlying resources after this call; i.e. filesystem based WALs can archive or
100    * delete files.
101    */
102   @Override
103   void close() throws IOException;
104 
105   /**
106    * Append a set of edits to the WAL. The WAL is not flushed/sync'd after this transaction
107    * completes BUT on return this edit must have its region edit/sequence id assigned
108    * else it messes up our unification of mvcc and sequenceid.  On return <code>key</code> will
109    * have the region edit/sequence id filled in.
110    * @param info
111    * @param key Modified by this call; we add to it this edits region edit/sequence id.
112    * @param edits Edits to append. MAY CONTAIN NO EDITS for case where we want to get an edit
113    * sequence id that is after all currently appended edits.
114    * @param htd used to give scope for replication TODO refactor out in favor of table name and
115    * info
116    * @param inMemstore Always true except for case where we are writing a compaction completion
117    * record into the WAL; in this case the entry is just so we can finish an unfinished compaction
118    * -- it is not an edit for memstore.
119    * @return Returns a 'transaction id' and <code>key</code> will have the region edit/sequence id
120    * in it.
121    */
122   long append(HTableDescriptor htd, HRegionInfo info, WALKey key, WALEdit edits,
123     boolean inMemstore)
124   throws IOException;
125 
126   /**
127    * Sync what we have in the WAL.
128    * @throws IOException
129    */
130   void sync() throws IOException;
131 
132   /**
133    * Sync the WAL if the txId was not already sync'd.
134    * @param txid Transaction id to sync to.
135    * @throws IOException
136    */
137   void sync(long txid) throws IOException;
138 
139   /**
140    * WAL keeps track of the sequence numbers that are as yet not flushed im memstores
141    * in order to be able to do accounting to figure which WALs can be let go. This method tells WAL
142    * that some region is about to flush. The flush can be the whole region or for a column family
143    * of the region only.
144    *
145    * <p>Currently, it is expected that the update lock is held for the region; i.e. no
146    * concurrent appends while we set up cache flush.
147    * @param families Families to flush. May be a subset of all families in the region.
148    * @return Returns {@link HConstants#NO_SEQNUM} if we are flushing the whole region OR if
149    * we are flushing a subset of all families but there are no edits in those families not
150    * being flushed; in other words, this is effectively same as a flush of all of the region
151    * though we were passed a subset of regions. Otherwise, it returns the sequence id of the
152    * oldest/lowest outstanding edit.
153    * @see #completeCacheFlush(byte[])
154    * @see #abortCacheFlush(byte[])
155    */
156   Long startCacheFlush(final byte[] encodedRegionName, Set<byte[]> families);
157 
158   /**
159    * Complete the cache flush.
160    * @param encodedRegionName Encoded region name.
161    * @see #startCacheFlush(byte[], Set)
162    * @see #abortCacheFlush(byte[])
163    */
164   void completeCacheFlush(final byte[] encodedRegionName);
165 
166   /**
167    * Abort a cache flush. Call if the flush fails. Note that the only recovery
168    * for an aborted flush currently is a restart of the regionserver so the
169    * snapshot content dropped by the failure gets restored to the memstore.
170    * @param encodedRegionName Encoded region name.
171    */
172   void abortCacheFlush(byte[] encodedRegionName);
173 
174   /**
175    * @return Coprocessor host.
176    */
177   WALCoprocessorHost getCoprocessorHost();
178 
179   /**
180    * Gets the earliest unflushed sequence id in the memstore for the region.
181    * @param encodedRegionName The region to get the number for.
182    * @return The earliest/lowest/oldest sequence id if present, HConstants.NO_SEQNUM if absent.
183    * @deprecated Since version 1.2.0. Removing because not used and exposes subtle internal
184    * workings. Use {@link #getEarliestMemstoreSeqNum(byte[], byte[])}
185    */
186   @VisibleForTesting
187   @Deprecated
188   long getEarliestMemstoreSeqNum(byte[] encodedRegionName);
189 
190   /**
191    * Gets the earliest unflushed sequence id in the memstore for the store.
192    * @param encodedRegionName The region to get the number for.
193    * @param familyName The family to get the number for.
194    * @return The earliest/lowest/oldest sequence id if present, HConstants.NO_SEQNUM if absent.
195    */
196   long getEarliestMemstoreSeqNum(byte[] encodedRegionName, byte[] familyName);
197 
198   /**
199    * Human readable identifying information about the state of this WAL.
200    * Implementors are encouraged to include information appropriate for debugging.
201    * Consumers are advised not to rely on the details of the returned String; it does
202    * not have a defined structure.
203    */
204   @Override
205   String toString();
206 
207   /**
208    * When outside clients need to consume persisted WALs, they rely on a provided
209    * Reader.
210    */
211   interface Reader extends Closeable {
212     Entry next() throws IOException;
213     Entry next(Entry reuse) throws IOException;
214     void seek(long pos) throws IOException;
215     long getPosition() throws IOException;
216     void reset() throws IOException;
217   }
218 
219   /**
220    * Utility class that lets us keep track of the edit with it's key.
221    */
222   class Entry {
223     private WALEdit edit;
224     private WALKey key;
225 
226     public Entry() {
227       edit = new WALEdit();
228       // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
229       key = new HLogKey();
230     }
231 
232     /**
233      * Constructor for both params
234      *
235      * @param edit log's edit
236      * @param key log's key
237      */
238     public Entry(WALKey key, WALEdit edit) {
239       super();
240       this.key = key;
241       this.edit = edit;
242     }
243 
244     /**
245      * Gets the edit
246      *
247      * @return edit
248      */
249     public WALEdit getEdit() {
250       return edit;
251     }
252 
253     /**
254      * Gets the key
255      *
256      * @return key
257      */
258     public WALKey getKey() {
259       return key;
260     }
261 
262     /**
263      * Set compression context for this entry.
264      *
265      * @param compressionContext
266      *          Compression context
267      */
268     public void setCompressionContext(CompressionContext compressionContext) {
269       edit.setCompressionContext(compressionContext);
270       key.setCompressionContext(compressionContext);
271     }
272 
273     @Override
274     public String toString() {
275       return this.key + "=" + this.edit;
276     }
277 
278   }
279 
280 }