1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver.wal;
21
22 import java.io.IOException;
23 import java.util.Map;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.HRegionInfo;
30 import org.apache.hadoop.hbase.HTableDescriptor;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.hbase.protobuf.generated.WALProtos;
33 import org.apache.hadoop.hbase.protobuf.generated.WALProtos.CompactionDescriptor;
34 import org.apache.hadoop.hbase.protobuf.generated.WALProtos.FlushDescriptor;
35 import org.apache.hadoop.hbase.protobuf.generated.WALProtos.RegionEventDescriptor;
36 import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
37 import org.apache.hadoop.hbase.util.FSUtils;
38 import org.apache.hadoop.hbase.wal.WAL;
39 import org.apache.hadoop.hbase.wal.WALKey;
40
41 import com.google.protobuf.TextFormat;
42
43
44
45
46
47
48 @InterfaceAudience.Private
49 public class WALUtil {
50 private static final Log LOG = LogFactory.getLog(WALUtil.class);
51
52 private WALUtil() {
53
54 }
55
56
57
58
59
60
61
62
63 public static long writeCompactionMarker(WAL wal, HTableDescriptor htd, HRegionInfo hri,
64 final CompactionDescriptor c, MultiVersionConcurrencyControl mvcc)
65 throws IOException {
66 long trx = writeMarker(wal, htd, hri, WALEdit.createCompaction(hri, c), mvcc, true, null);
67 if (LOG.isTraceEnabled()) {
68 LOG.trace("Appended compaction marker " + TextFormat.shortDebugString(c));
69 }
70 return trx;
71 }
72
73
74
75
76 public static long writeFlushMarker(WAL wal, HTableDescriptor htd, HRegionInfo hri,
77 final FlushDescriptor f, boolean sync, MultiVersionConcurrencyControl mvcc)
78 throws IOException {
79 long trx = writeMarker(wal, htd, hri, WALEdit.createFlushWALEdit(hri, f), mvcc, sync, null);
80 if (LOG.isTraceEnabled()) {
81 LOG.trace("Appended flush marker " + TextFormat.shortDebugString(f));
82 }
83 return trx;
84 }
85
86
87
88
89 public static long writeRegionEventMarker(WAL wal, HTableDescriptor htd, HRegionInfo hri,
90 final RegionEventDescriptor r, final MultiVersionConcurrencyControl mvcc)
91 throws IOException {
92 long trx = writeMarker(wal, htd, hri, WALEdit.createRegionEventWALEdit(hri, r), mvcc, true,
93 null);
94 if (LOG.isTraceEnabled()) {
95 LOG.trace("Appended region event marker " + TextFormat.shortDebugString(r));
96 }
97 return trx;
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public static long writeBulkLoadMarkerAndSync(final WAL wal, final HTableDescriptor htd,
111 final HRegionInfo hri, final WALProtos.BulkLoadDescriptor desc,
112 final MultiVersionConcurrencyControl mvcc)
113 throws IOException {
114 long trx = writeMarker(wal, htd, hri, WALEdit.createBulkLoadEvent(hri, desc), mvcc, true,
115 null);
116 if (LOG.isTraceEnabled()) {
117 LOG.trace("Appended Bulk Load marker " + TextFormat.shortDebugString(desc));
118 }
119 return trx;
120 }
121
122 private static long writeMarker(final WAL wal, final HTableDescriptor htd, final HRegionInfo hri,
123 final WALEdit edit, final MultiVersionConcurrencyControl mvcc, final boolean sync,
124 final Map<String, byte[]> extendedAttributes)
125 throws IOException {
126
127 WALKey key = new WALKey(hri.getEncodedNameAsBytes(), hri.getTable(), System.currentTimeMillis(),
128 mvcc, extendedAttributes);
129
130 long trx = MultiVersionConcurrencyControl.NONE;
131 try {
132 trx = wal.append(htd, hri, key, edit, false);
133 if (sync) wal.sync(trx);
134 } finally {
135
136
137
138 MultiVersionConcurrencyControl.WriteEntry we = key.getWriteEntry();
139 if (mvcc != null && we != null) mvcc.complete(we);
140 }
141 return trx;
142 }
143
144
145
146
147
148
149
150 public static long getWALBlockSize(Configuration conf, FileSystem fs, Path dir)
151 throws IOException {
152 return getWALBlockSize(conf, fs, dir, false);
153 }
154
155
156
157
158
159
160 public static long getWALBlockSize(Configuration conf, FileSystem fs, Path dir,
161 boolean isRecoverEdits) throws IOException {
162 long defaultBlockSize = FSUtils.getDefaultBlockSize(fs, dir) * 2;
163 if (isRecoverEdits) {
164 return conf.getLong("hbase.regionserver.recoverededits.blocksize", defaultBlockSize);
165 }
166 return conf.getLong("hbase.regionserver.hlog.blocksize", defaultBlockSize);
167 }
168 }