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.List;
25  import java.util.Set;
26  import java.util.concurrent.atomic.AtomicLong;
27  
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  
34  // imports we use from yet-to-be-moved regionsever.wal
35  import org.apache.hadoop.hbase.regionserver.wal.CompressionContext;
36  import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
37  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
38  import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
39  import org.apache.hadoop.hbase.regionserver.wal.WALCoprocessorHost;
40  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
41  
42  /**
43   * A Write Ahead Log (WAL) provides service for reading, writing waledits. This interface provides
44   * APIs for WAL users (such as RegionServer) to use the WAL (do append, sync, etc).
45   *
46   * Note that some internals, such as log rolling and performance evaluation tools, will use
47   * WAL.equals to determine if they have already seen a given WAL.
48   */
49  @InterfaceAudience.Private
50  @InterfaceStability.Evolving
51  public interface WAL {
52  
53    /**
54     * Registers WALActionsListener
55     */
56    void registerWALActionsListener(final WALActionsListener listener);
57  
58    /**
59     * Unregisters WALActionsListener
60     */
61    boolean unregisterWALActionsListener(final WALActionsListener listener);
62  
63    /**
64     * Roll the log writer. That is, start writing log messages to a new file.
65     *
66     * <p>
67     * The implementation is synchronized in order to make sure there's one rollWriter
68     * running at any given time.
69     *
70     * @return If lots of logs, flush the returned regions so next time through we
71     *         can clean logs. Returns null if nothing to flush. Names are actual
72     *         region names as returned by {@link HRegionInfo#getEncodedName()}
73     */
74    byte[][] rollWriter() throws FailedLogCloseException, IOException;
75  
76    /**
77     * Roll the log writer. That is, start writing log messages to a new file.
78     *
79     * <p>
80     * The implementation is synchronized in order to make sure there's one rollWriter
81     * running at any given time.
82     *
83     * @param force
84     *          If true, force creation of a new writer even if no entries have
85     *          been written to the current writer
86     * @return If lots of logs, flush the returned regions so next time through we
87     *         can clean logs. Returns null if nothing to flush. Names are actual
88     *         region names as returned by {@link HRegionInfo#getEncodedName()}
89     */
90    byte[][] rollWriter(boolean force) throws FailedLogCloseException, IOException;
91  
92    /**
93     * Stop accepting new writes. If we have unsynced writes still in buffer, sync them.
94     * Extant edits are left in place in backing storage to be replayed later.
95     */
96    void shutdown() throws IOException;
97  
98    /**
99     * Caller no longer needs any edits from this WAL. Implementers are free to reclaim
100    * underlying resources after this call; i.e. filesystem based WALs can archive or
101    * delete files.
102    */
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 info
115    * @param sequenceId A reference to the atomic long the <code>info</code> region is using as
116    * source of its incrementing edits sequence id.  Inside in this call we will increment it and
117    * attach the sequence to the edit we apply the WAL.
118    * @param inMemstore Always true except for case where we are writing a compaction completion
119    * record into the WAL; in this case the entry is just so we can finish an unfinished compaction
120    * -- it is not an edit for memstore.
121    * @param memstoreKVs list of KVs added into memstore
122    * @return Returns a 'transaction id' and <code>key</code> will have the region edit/sequence id
123    * in it.
124    */
125   long append(HTableDescriptor htd, HRegionInfo info, WALKey key, WALEdit edits,
126       AtomicLong sequenceId, boolean inMemstore, List<Cell> memstoreKVs)
127   throws IOException;
128 
129   /**
130    * Sync what we have in the WAL.
131    * @throws IOException
132    */
133   void sync() throws IOException;
134 
135   /**
136    * Sync the WAL if the txId was not already sync'd.
137    * @param txid Transaction id to sync to.
138    * @throws IOException
139    */
140   void sync(long txid) throws IOException;
141 
142   /**
143    * WAL keeps track of the sequence numbers that were not yet flushed from memstores
144    * in order to be able to do cleanup. This method tells WAL that some region is about
145    * to flush memstore.
146    *
147    * <p>We stash the oldest seqNum for the region, and let the the next edit inserted in this
148    * region be recorded in {@link #append(HTableDescriptor, HRegionInfo, WALKey, WALEdit,
149    * AtomicLong, boolean, List)} as new oldest seqnum.
150    * In case of flush being aborted, we put the stashed value back; in case of flush succeeding,
151    * the seqNum of that first edit after start becomes the valid oldest seqNum for this region.
152    *
153    * @return true if the flush can proceed, false in case wal is closing (ususally, when server is
154    * closing) and flush couldn't be started.
155    */
156   boolean startCacheFlush(final byte[] encodedRegionName, Set<byte[]> flushedFamilyNames);
157 
158   /**
159    * Complete the cache flush.
160    * @param encodedRegionName Encoded region name.
161    */
162   void completeCacheFlush(final byte[] encodedRegionName);
163 
164   /**
165    * Abort a cache flush. Call if the flush fails. Note that the only recovery
166    * for an aborted flush currently is a restart of the regionserver so the
167    * snapshot content dropped by the failure gets restored to the memstore.v
168    * @param encodedRegionName Encoded region name.
169    */
170   void abortCacheFlush(byte[] encodedRegionName);
171 
172   /**
173    * @return Coprocessor host.
174    */
175   WALCoprocessorHost getCoprocessorHost();
176 
177 
178   /** Gets the earliest sequence number in the memstore for this particular region.
179    * This can serve as best-effort "recent" WAL number for this region.
180    * @param encodedRegionName The region to get the number for.
181    * @return The number if present, HConstants.NO_SEQNUM if absent.
182    */
183   long getEarliestMemstoreSeqNum(byte[] encodedRegionName);
184 
185   /**
186    * Gets the earliest sequence number in the memstore for this particular region and store.
187    * @param encodedRegionName The region to get the number for.
188    * @param familyName The family to get the number for.
189    * @return The number if present, HConstants.NO_SEQNUM if absent.
190    */
191   long getEarliestMemstoreSeqNum(byte[] encodedRegionName, byte[] familyName);
192 
193   /**
194    * Human readable identifying information about the state of this WAL.
195    * Implementors are encouraged to include information appropriate for debugging.
196    * Consumers are advised not to rely on the details of the returned String; it does
197    * not have a defined structure.
198    */
199   @Override
200   String toString();
201 
202   /**
203    * When outside clients need to consume persisted WALs, they rely on a provided
204    * Reader.
205    */
206   interface Reader extends Closeable {
207     Entry next() throws IOException;
208     Entry next(Entry reuse) throws IOException;
209     void seek(long pos) throws IOException;
210     long getPosition() throws IOException;
211     void reset() throws IOException;
212   }
213 
214   /**
215    * Utility class that lets us keep track of the edit with it's key.
216    */
217   class Entry {
218     private WALEdit edit;
219     private WALKey key;
220 
221     public Entry() {
222       edit = new WALEdit();
223       // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
224       key = new HLogKey();
225     }
226 
227     /**
228      * Constructor for both params
229      *
230      * @param edit log's edit
231      * @param key log's key
232      */
233     public Entry(WALKey key, WALEdit edit) {
234       super();
235       this.key = key;
236       this.edit = edit;
237     }
238 
239     /**
240      * Gets the edit
241      *
242      * @return edit
243      */
244     public WALEdit getEdit() {
245       return edit;
246     }
247 
248     /**
249      * Gets the key
250      *
251      * @return key
252      */
253     public WALKey getKey() {
254       return key;
255     }
256 
257     /**
258      * Set compression context for this entry.
259      *
260      * @param compressionContext
261      *          Compression context
262      */
263     public void setCompressionContext(CompressionContext compressionContext) {
264       edit.setCompressionContext(compressionContext);
265       key.setCompressionContext(compressionContext);
266     }
267 
268     @Override
269     public String toString() {
270       return this.key + "=" + this.edit;
271     }
272 
273   }
274 
275 }