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.regionserver.wal;
21  
22  import java.io.IOException;
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.HRegionInfo;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.CompactionDescriptor;
32  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.FlushDescriptor;
33  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.RegionEventDescriptor;
34  import org.apache.hadoop.hbase.wal.WAL;
35  import org.apache.hadoop.hbase.wal.WALKey;
36  
37  import com.google.protobuf.TextFormat;
38  
39  /**
40   * Helper methods to ease Region Server integration with the write ahead log.
41   * Note that methods in this class specifically should not require access to anything
42   * other than the API found in {@link WAL}.
43   */
44  @InterfaceAudience.Private
45  public class WALUtil {
46    static final Log LOG = LogFactory.getLog(WALUtil.class);
47  
48    /**
49     * Write the marker that a compaction has succeeded and is about to be committed.
50     * This provides info to the HMaster to allow it to recover the compaction if
51     * this regionserver dies in the middle (This part is not yet implemented). It also prevents
52     * the compaction from finishing if this regionserver has already lost its lease on the log.
53     * @param sequenceId Used by WAL to get sequence Id for the waledit.
54     */
55    public static void writeCompactionMarker(WAL log, HTableDescriptor htd, HRegionInfo info,
56        final CompactionDescriptor c, AtomicLong sequenceId) throws IOException {
57      TableName tn = TableName.valueOf(c.getTableName().toByteArray());
58      // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
59      WALKey key = new HLogKey(info.getEncodedNameAsBytes(), tn);
60      log.append(htd, info, key, WALEdit.createCompaction(info, c), sequenceId, false, null);
61      log.sync();
62      if (LOG.isTraceEnabled()) {
63        LOG.trace("Appended compaction marker " + TextFormat.shortDebugString(c));
64      }
65    }
66  
67    /**
68     * Write a flush marker indicating a start / abort or a complete of a region flush
69     */
70    public static long writeFlushMarker(WAL log, HTableDescriptor htd, HRegionInfo info,
71        final FlushDescriptor f, AtomicLong sequenceId, boolean sync) throws IOException {
72      TableName tn = TableName.valueOf(f.getTableName().toByteArray());
73      // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
74      WALKey key = new HLogKey(info.getEncodedNameAsBytes(), tn);
75      long trx = log.append(htd, info, key, WALEdit.createFlushWALEdit(info, f), sequenceId, false,
76          null);
77      if (sync) log.sync(trx);
78      if (LOG.isTraceEnabled()) {
79        LOG.trace("Appended flush marker " + TextFormat.shortDebugString(f));
80      }
81      return trx;
82    }
83  
84    /**
85     * Write a region open marker indicating that the region is opened
86     */
87    public static long writeRegionEventMarker(WAL log, HTableDescriptor htd, HRegionInfo info,
88        final RegionEventDescriptor r, AtomicLong sequenceId) throws IOException {
89      TableName tn = TableName.valueOf(r.getTableName().toByteArray());
90      // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
91      WALKey key = new HLogKey(info.getEncodedNameAsBytes(), tn);
92      long trx = log.append(htd, info, key, WALEdit.createRegionEventWALEdit(info, r),
93        sequenceId, false, null);
94      log.sync(trx);
95      if (LOG.isTraceEnabled()) {
96        LOG.trace("Appended region event marker " + TextFormat.shortDebugString(r));
97      }
98      return trx;
99    }
100   
101 }