1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.wal;
19
20 import java.io.IOException;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import java.util.concurrent.atomic.AtomicBoolean;
25 import java.util.concurrent.atomic.AtomicLong;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.Cell;
34 import org.apache.hadoop.hbase.CellUtil;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.HRegionInfo;
37 import org.apache.hadoop.hbase.HTableDescriptor;
38 import org.apache.hadoop.hbase.util.FSUtils;
39
40
41 import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
42 import org.apache.hadoop.hbase.regionserver.wal.WALCoprocessorHost;
43 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
44
45
46
47
48
49
50
51 @InterfaceAudience.Private
52 class DisabledWALProvider implements WALProvider {
53
54 private static final Log LOG = LogFactory.getLog(DisabledWALProvider.class);
55
56 WAL disabled;
57
58 @Override
59 public void init(final WALFactory factory, final Configuration conf,
60 final List<WALActionsListener> listeners, String providerId) throws IOException {
61 if (null != disabled) {
62 throw new IllegalStateException("WALProvider.init should only be called once.");
63 }
64 if (null == providerId) {
65 providerId = "defaultDisabled";
66 }
67 disabled = new DisabledWAL(new Path(FSUtils.getRootDir(conf), providerId), conf, null);
68 }
69
70 @Override
71 public WAL getWAL(final byte[] identifier) throws IOException {
72 return disabled;
73 }
74
75 @Override
76 public void close() throws IOException {
77 disabled.close();
78 }
79
80 @Override
81 public void shutdown() throws IOException {
82 disabled.shutdown();
83 }
84
85 private static class DisabledWAL implements WAL {
86 protected final List<WALActionsListener> listeners =
87 new CopyOnWriteArrayList<WALActionsListener>();
88 protected final Path path;
89 protected final WALCoprocessorHost coprocessorHost;
90 protected final AtomicBoolean closed = new AtomicBoolean(false);
91
92 public DisabledWAL(final Path path, final Configuration conf,
93 final List<WALActionsListener> listeners) {
94 this.coprocessorHost = new WALCoprocessorHost(this, conf);
95 this.path = path;
96 if (null != listeners) {
97 for(WALActionsListener listener : listeners) {
98 registerWALActionsListener(listener);
99 }
100 }
101 }
102
103 @Override
104 public void registerWALActionsListener(final WALActionsListener listener) {
105 listeners.add(listener);
106 }
107
108 @Override
109 public boolean unregisterWALActionsListener(final WALActionsListener listener) {
110 return listeners.remove(listener);
111 }
112
113 @Override
114 public byte[][] rollWriter() {
115 if (!listeners.isEmpty()) {
116 for (WALActionsListener listener : listeners) {
117 listener.logRollRequested(false);
118 }
119 for (WALActionsListener listener : listeners) {
120 try {
121 listener.preLogRoll(path, path);
122 } catch (IOException exception) {
123 LOG.debug("Ignoring exception from listener.", exception);
124 }
125 }
126 for (WALActionsListener listener : listeners) {
127 try {
128 listener.postLogRoll(path, path);
129 } catch (IOException exception) {
130 LOG.debug("Ignoring exception from listener.", exception);
131 }
132 }
133 }
134 return null;
135 }
136
137 @Override
138 public byte[][] rollWriter(boolean force) {
139 return rollWriter();
140 }
141
142 @Override
143 public void shutdown() {
144 if(closed.compareAndSet(false, true)) {
145 if (!this.listeners.isEmpty()) {
146 for (WALActionsListener listener : this.listeners) {
147 listener.logCloseRequested();
148 }
149 }
150 }
151 }
152
153 @Override
154 public void close() {
155 shutdown();
156 }
157
158 @Override
159 public long append(HTableDescriptor htd, HRegionInfo info, WALKey key, WALEdit edits,
160 AtomicLong sequenceId, boolean inMemstore, List<Cell> memstoreKVs) {
161 if (!this.listeners.isEmpty()) {
162 final long start = System.nanoTime();
163 long len = 0;
164 for (Cell cell : edits.getCells()) {
165 len += CellUtil.estimatedSerializedSizeOf(cell);
166 }
167 final long elapsed = (System.nanoTime() - start)/1000000l;
168 for (WALActionsListener listener : this.listeners) {
169 listener.postAppend(len, elapsed);
170 }
171 }
172 return -1;
173 }
174
175 @Override
176 public void sync() {
177 if (!this.listeners.isEmpty()) {
178 for (WALActionsListener listener : this.listeners) {
179 listener.postSync(0l, 0);
180 }
181 }
182 }
183
184 @Override
185 public void sync(long txid) {
186 sync();
187 }
188
189 @Override
190 public boolean startCacheFlush(final byte[] encodedRegionName, Set<byte[]> flushedFamilyNames) {
191 return !(closed.get());
192 }
193
194 @Override
195 public void completeCacheFlush(final byte[] encodedRegionName) {
196 }
197
198 @Override
199 public void abortCacheFlush(byte[] encodedRegionName) {
200 }
201
202 @Override
203 public WALCoprocessorHost getCoprocessorHost() {
204 return coprocessorHost;
205 }
206
207 @Override
208 public long getEarliestMemstoreSeqNum(byte[] encodedRegionName) {
209 return HConstants.NO_SEQNUM;
210 }
211
212 @Override
213 public long getEarliestMemstoreSeqNum(byte[] encodedRegionName, byte[] familyName) {
214 return HConstants.NO_SEQNUM;
215 }
216
217 @Override
218 public String toString() {
219 return "WAL disabled.";
220 }
221 }
222 }