HBASE-24640 Purge use of VisibleForTesting (#2695)
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / wal / WALEdit.java
blob61f36fab74af717fe8fbb7422a4ab1029caeae04
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.wal;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import org.apache.hadoop.hbase.Cell;
27 import org.apache.hadoop.hbase.CellUtil;
28 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
29 import org.apache.hadoop.hbase.KeyValue;
30 import org.apache.hadoop.hbase.PrivateCellUtil;
31 import org.apache.hadoop.hbase.client.RegionInfo;
32 import org.apache.hadoop.hbase.codec.Codec;
33 import org.apache.hadoop.hbase.io.HeapSize;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.apache.hadoop.hbase.util.ClassSize;
36 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
37 import org.apache.yetus.audience.InterfaceAudience;
39 import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos;
40 import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.CompactionDescriptor;
41 import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.FlushDescriptor;
42 import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.RegionEventDescriptor;
44 /**
45 * Used in HBase's transaction log (WAL) to represent a collection of edits (Cell/KeyValue objects)
46 * that came in as a single transaction. All the edits for a given transaction are written out as a
47 * single record, in PB format, followed (optionally) by Cells written via the WALCellEncoder.
48 * <p>This class is LimitedPrivate for CPs to read-only. The {@link #add} methods are
49 * classified as private methods, not for use by CPs.</p>
51 * <p>A particular WALEdit 'type' is the 'meta' type used to mark key operational
52 * events in the WAL such as compaction, flush, or region open. These meta types do not traverse
53 * hbase memstores. They are edits made by the hbase system rather than edit data submitted by
54 * clients. They only show in the WAL. These 'Meta' types have not been formally specified
55 * (or made into an explicit class type). They evolved organically. HBASE-8457 suggests codifying
56 * a WALEdit 'type' by adding a type field to WALEdit that gets serialized into the WAL. TODO.
57 * Would have to work on the consumption-side. Reading WALs on replay we seem to consume
58 * a Cell-at-a-time rather than by WALEdit. We are already in the below going out of our
59 * way to figure particular types -- e.g. if a compaction, replay, or close meta Marker -- during
60 * normal processing so would make sense to do this. Current system is an awkward marking of Cell
61 * columnfamily as {@link #METAFAMILY} and then setting qualifier based off meta edit type. For
62 * replay-time where we read Cell-at-a-time, there are utility methods below for figuring
63 * meta type. See also
64 * {@link #createBulkLoadEvent(RegionInfo, WALProtos.BulkLoadDescriptor)}, etc., for where we
65 * create meta WALEdit instances.</p>
67 * <p>WALEdit will accumulate a Set of all column family names referenced by the Cells
68 * {@link #add(Cell)}'d. This is an optimization. Usually when loading a WALEdit, we have the
69 * column family name to-hand.. just shove it into the WALEdit if available. Doing this, we can
70 * save on a parse of each Cell to figure column family down the line when we go to add the
71 * WALEdit to the WAL file. See the hand-off in FSWALEntry Constructor.
72 * @see WALKey
74 // TODO: Do not expose this class to Coprocessors. It has set methods. A CP might meddle.
75 @InterfaceAudience.LimitedPrivate({ HBaseInterfaceAudience.REPLICATION,
76 HBaseInterfaceAudience.COPROC })
77 public class WALEdit implements HeapSize {
78 // Below defines are for writing WALEdit 'meta' Cells..
79 // TODO: Get rid of this system of special 'meta' Cells. See HBASE-8457. It suggests
80 // adding a type to WALEdit itself for use denoting meta Edits and their types.
81 public static final byte [] METAFAMILY = Bytes.toBytes("METAFAMILY");
83 /**
84 * @deprecated Since 2.3.0. Not used.
86 @Deprecated
87 public static final byte [] METAROW = Bytes.toBytes("METAROW");
89 /**
90 * @deprecated Since 2.3.0. Make it protected, internal-use only. Use
91 * {@link #isCompactionMarker(Cell)}
93 @Deprecated
94 @InterfaceAudience.Private
95 public static final byte[] COMPACTION = Bytes.toBytes("HBASE::COMPACTION");
97 /**
98 * @deprecated Since 2.3.0. Make it protected, internal-use only.
100 @Deprecated
101 @InterfaceAudience.Private
102 public static final byte [] FLUSH = Bytes.toBytes("HBASE::FLUSH");
105 * Qualifier for region event meta 'Marker' WALEdits start with the
106 * {@link #REGION_EVENT_PREFIX} prefix ('HBASE::REGION_EVENT::'). After the prefix,
107 * we note the type of the event which we get from the RegionEventDescriptor protobuf
108 * instance type (A RegionEventDescriptor protobuf instance is written as the meta Marker
109 * Cell value). Adding a type suffix means we do not have to deserialize the protobuf to
110 * figure out what type of event this is.. .just read the qualifier suffix. For example,
111 * a close region event descriptor will have a qualifier of HBASE::REGION_EVENT::REGION_CLOSE.
112 * See WAL.proto and the EventType in RegionEventDescriptor protos for all possible
113 * event types.
115 private static final String REGION_EVENT_STR = "HBASE::REGION_EVENT";
116 private static final String REGION_EVENT_PREFIX_STR = REGION_EVENT_STR + "::";
117 private static final byte [] REGION_EVENT_PREFIX = Bytes.toBytes(REGION_EVENT_PREFIX_STR);
120 * @deprecated Since 2.3.0. Remove. Not for external use. Not used.
122 @Deprecated
123 public static final byte [] REGION_EVENT = Bytes.toBytes(REGION_EVENT_STR);
126 * We use this define figuring if we are carrying a close event.
128 private static final byte [] REGION_EVENT_CLOSE =
129 createRegionEventDescriptorQualifier(RegionEventDescriptor.EventType.REGION_CLOSE);
131 @InterfaceAudience.Private
132 public static final byte [] BULK_LOAD = Bytes.toBytes("HBASE::BULK_LOAD");
134 private final transient boolean replay;
136 private ArrayList<Cell> cells;
139 * All the Cell families in <code>cells</code>. Updated by {@link #add(Cell)} and
140 * {@link #add(Map)}. This Set is passed to the FSWALEntry so it does not have
141 * to recalculate the Set of families in a transaction; makes for a bunch of CPU savings.
143 private Set<byte []> families = null;
145 public WALEdit() {
146 this(1, false);
150 * @deprecated since 2.0.1 and will be removed in 4.0.0. Use {@link #WALEdit(int, boolean)}
151 * instead.
152 * @see #WALEdit(int, boolean)
153 * @see <a href="https://issues.apache.org/jira/browse/HBASE-20781">HBASE-20781</a>
155 @Deprecated
156 public WALEdit(boolean replay) {
157 this(1, replay);
161 * @deprecated since 2.0.1 and will be removed in 4.0.0. Use {@link #WALEdit(int, boolean)}
162 * instead.
163 * @see #WALEdit(int, boolean)
164 * @see <a href="https://issues.apache.org/jira/browse/HBASE-20781">HBASE-20781</a>
166 @Deprecated
167 public WALEdit(int cellCount) {
168 this(cellCount, false);
172 * @param cellCount Pass so can pre-size the WALEdit. Optimization.
174 public WALEdit(int cellCount, boolean isReplay) {
175 this.replay = isReplay;
176 cells = new ArrayList<>(cellCount);
179 private Set<byte[]> getOrCreateFamilies() {
180 if (this.families == null) {
181 this.families = new TreeSet<>(Bytes.BYTES_COMPARATOR);
183 return this.families;
187 * For use by FSWALEntry ONLY. An optimization.
188 * @return All families in {@link #getCells()}; may be null.
190 public Set<byte []> getFamilies() {
191 return this.families;
195 * @return True is <code>f</code> is {@link #METAFAMILY}
196 * @deprecated Since 2.3.0. Do not expose. Make protected.
198 @Deprecated
199 public static boolean isMetaEditFamily(final byte [] f) {
200 return Bytes.equals(METAFAMILY, f);
204 * Replaying WALs can read Cell-at-a-time so need this method in those cases.
206 public static boolean isMetaEditFamily(Cell cell) {
207 return CellUtil.matchingFamily(cell, METAFAMILY);
211 * @return True if this is a meta edit; has one edit only and its columnfamily
212 * is {@link #METAFAMILY}.
214 public boolean isMetaEdit() {
215 return this.families != null && this.families.size() == 1 && this.families.contains(METAFAMILY);
219 * @return True when current WALEdit is created by log replay. Replication skips WALEdits from
220 * replay.
222 public boolean isReplay() {
223 return this.replay;
226 @InterfaceAudience.Private
227 public WALEdit add(Cell cell, byte [] family) {
228 getOrCreateFamilies().add(family);
229 return addCell(cell);
232 @InterfaceAudience.Private
233 public WALEdit add(Cell cell) {
234 // We clone Family each time we add a Cell. Expensive but safe. For CPU savings, use
235 // add(Map) or add(Cell, family).
236 return add(cell, CellUtil.cloneFamily(cell));
239 public boolean isEmpty() {
240 return cells.isEmpty();
243 public int size() {
244 return cells.size();
247 public ArrayList<Cell> getCells() {
248 return cells;
252 * This is not thread safe.
253 * This will change the WALEdit and shouldn't be used unless you are sure that nothing
254 * else depends on the contents being immutable.
256 * @param cells the list of cells that this WALEdit now contains.
258 @InterfaceAudience.Private
259 // Used by replay.
260 public void setCells(ArrayList<Cell> cells) {
261 this.cells = cells;
262 this.families = null;
266 * Reads WALEdit from cells.
267 * @param cellDecoder Cell decoder.
268 * @param expectedCount Expected cell count.
269 * @return Number of KVs read.
271 public int readFromCells(Codec.Decoder cellDecoder, int expectedCount) throws IOException {
272 cells.clear();
273 cells.ensureCapacity(expectedCount);
274 while (cells.size() < expectedCount && cellDecoder.advance()) {
275 add(cellDecoder.current());
277 return cells.size();
280 @Override
281 public long heapSize() {
282 long ret = ClassSize.ARRAYLIST;
283 for (Cell cell : cells) {
284 ret += cell.heapSize();
286 return ret;
289 public long estimatedSerializedSizeOf() {
290 long ret = 0;
291 for (Cell cell: cells) {
292 ret += PrivateCellUtil.estimatedSerializedSizeOf(cell);
294 return ret;
297 @Override
298 public String toString() {
299 StringBuilder sb = new StringBuilder();
301 sb.append("[#edits: ").append(cells.size()).append(" = <");
302 for (Cell cell : cells) {
303 sb.append(cell);
304 sb.append("; ");
306 sb.append(">]");
307 return sb.toString();
310 public static WALEdit createFlushWALEdit(RegionInfo hri, FlushDescriptor f) {
311 KeyValue kv = new KeyValue(getRowForRegion(hri), METAFAMILY, FLUSH,
312 EnvironmentEdgeManager.currentTime(), f.toByteArray());
313 return new WALEdit().add(kv, METAFAMILY);
316 public static FlushDescriptor getFlushDescriptor(Cell cell) throws IOException {
317 return CellUtil.matchingColumn(cell, METAFAMILY, FLUSH)?
318 FlushDescriptor.parseFrom(CellUtil.cloneValue(cell)): null;
322 * @return A meta Marker WALEdit that has a single Cell whose value is the passed in
323 * <code>regionEventDesc</code> serialized and whose row is this region,
324 * columnfamily is {@link #METAFAMILY} and qualifier is
325 * {@link #REGION_EVENT_PREFIX} + {@link RegionEventDescriptor#getEventType()};
326 * for example HBASE::REGION_EVENT::REGION_CLOSE.
328 public static WALEdit createRegionEventWALEdit(RegionInfo hri,
329 RegionEventDescriptor regionEventDesc) {
330 return createRegionEventWALEdit(getRowForRegion(hri), regionEventDesc);
333 @InterfaceAudience.Private
334 public static WALEdit createRegionEventWALEdit(byte [] rowForRegion,
335 RegionEventDescriptor regionEventDesc) {
336 KeyValue kv = new KeyValue(rowForRegion, METAFAMILY,
337 createRegionEventDescriptorQualifier(regionEventDesc.getEventType()),
338 EnvironmentEdgeManager.currentTime(), regionEventDesc.toByteArray());
339 return new WALEdit().add(kv, METAFAMILY);
343 * @return Cell qualifier for the passed in RegionEventDescriptor Type; e.g. we'll
344 * return something like a byte array with HBASE::REGION_EVENT::REGION_OPEN in it.
346 @InterfaceAudience.Private
347 public static byte [] createRegionEventDescriptorQualifier(RegionEventDescriptor.EventType t) {
348 return Bytes.toBytes(REGION_EVENT_PREFIX_STR + t.toString());
352 * Public so can be accessed from regionserver.wal package.
353 * @return True if this is a Marker Edit and it is a RegionClose type.
355 public boolean isRegionCloseMarker() {
356 return isMetaEdit() && PrivateCellUtil.matchingQualifier(this.cells.get(0),
357 REGION_EVENT_CLOSE, 0, REGION_EVENT_CLOSE.length);
361 * @return Returns a RegionEventDescriptor made by deserializing the content of the
362 * passed in <code>cell</code>, IFF the <code>cell</code> is a RegionEventDescriptor
363 * type WALEdit.
365 public static RegionEventDescriptor getRegionEventDescriptor(Cell cell) throws IOException {
366 return CellUtil.matchingColumnFamilyAndQualifierPrefix(cell, METAFAMILY, REGION_EVENT_PREFIX)?
367 RegionEventDescriptor.parseFrom(CellUtil.cloneValue(cell)): null;
371 * @return A Marker WALEdit that has <code>c</code> serialized as its value
373 public static WALEdit createCompaction(final RegionInfo hri, final CompactionDescriptor c) {
374 byte [] pbbytes = c.toByteArray();
375 KeyValue kv = new KeyValue(getRowForRegion(hri), METAFAMILY, COMPACTION,
376 EnvironmentEdgeManager.currentTime(), pbbytes);
377 return new WALEdit().add(kv, METAFAMILY); //replication scope null so this won't be replicated
380 public static byte[] getRowForRegion(RegionInfo hri) {
381 byte[] startKey = hri.getStartKey();
382 if (startKey.length == 0) {
383 // empty row key is not allowed in mutations because it is both the start key and the end key
384 // we return the smallest byte[] that is bigger (in lex comparison) than byte[0].
385 return new byte[] {0};
387 return startKey;
391 * Deserialized and returns a CompactionDescriptor is the KeyValue contains one.
392 * @param kv the key value
393 * @return deserialized CompactionDescriptor or null.
395 public static CompactionDescriptor getCompaction(Cell kv) throws IOException {
396 return isCompactionMarker(kv)? CompactionDescriptor.parseFrom(CellUtil.cloneValue(kv)): null;
400 * Returns true if the given cell is a serialized {@link CompactionDescriptor}
402 * @see #getCompaction(Cell)
404 public static boolean isCompactionMarker(Cell cell) {
405 return CellUtil.matchingColumn(cell, METAFAMILY, COMPACTION);
409 * Create a bulk loader WALEdit
411 * @param hri The RegionInfo for the region in which we are bulk loading
412 * @param bulkLoadDescriptor The descriptor for the Bulk Loader
413 * @return The WALEdit for the BulkLoad
415 public static WALEdit createBulkLoadEvent(RegionInfo hri,
416 WALProtos.BulkLoadDescriptor bulkLoadDescriptor) {
417 KeyValue kv = new KeyValue(getRowForRegion(hri), METAFAMILY, BULK_LOAD,
418 EnvironmentEdgeManager.currentTime(), bulkLoadDescriptor.toByteArray());
419 return new WALEdit().add(kv, METAFAMILY);
423 * Deserialized and returns a BulkLoadDescriptor from the passed in Cell
424 * @param cell the key value
425 * @return deserialized BulkLoadDescriptor or null.
427 public static WALProtos.BulkLoadDescriptor getBulkLoadDescriptor(Cell cell) throws IOException {
428 return CellUtil.matchingColumn(cell, METAFAMILY, BULK_LOAD)?
429 WALProtos.BulkLoadDescriptor.parseFrom(CellUtil.cloneValue(cell)): null;
433 * Append the given map of family->edits to a WALEdit data structure.
434 * This does not write to the WAL itself.
435 * Note that as an optimization, we will stamp the Set of column families into the WALEdit
436 * to save on our having to calculate column families subsequently down in the actual WAL
437 * writing.
439 * @param familyMap map of family->edits
441 public void add(Map<byte[], List<Cell>> familyMap) {
442 for (Map.Entry<byte [], List<Cell>> e: familyMap.entrySet()) {
443 // 'foreach' loop NOT used. See HBASE-12023 "...creates too many iterator objects."
444 int listSize = e.getValue().size();
445 // Add all Cells first and then at end, add the family rather than call {@link #add(Cell)}
446 // and have it clone family each time. Optimization!
447 for (int i = 0; i < listSize; i++) {
448 addCell(e.getValue().get(i));
450 addFamily(e.getKey());
454 private void addFamily(byte [] family) {
455 getOrCreateFamilies().add(family);
458 private WALEdit addCell(Cell cell) {
459 this.cells.add(cell);
460 return this;