001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.wal; 020 021import java.io.IOException; 022import java.util.ArrayList; 023 024import org.apache.hadoop.hbase.Cell; 025import org.apache.hadoop.hbase.CellUtil; 026import org.apache.hadoop.hbase.HBaseInterfaceAudience; 027import org.apache.hadoop.hbase.PrivateCellUtil; 028import org.apache.hadoop.hbase.KeyValue; 029import org.apache.hadoop.hbase.client.RegionInfo; 030import org.apache.hadoop.hbase.codec.Codec; 031import org.apache.hadoop.hbase.io.HeapSize; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.apache.hadoop.hbase.util.ClassSize; 034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 035import org.apache.yetus.audience.InterfaceAudience; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 039import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos; 040import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.CompactionDescriptor; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.FlushDescriptor; 042import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.RegionEventDescriptor; 043 044 045/** 046 * WALEdit: Used in HBase's transaction log (WAL) to represent 047 * the collection of edits (KeyValue objects) corresponding to a 048 * single transaction. 049 * 050 * All the edits for a given transaction are written out as a single record, in PB format followed 051 * by Cells written via the WALCellEncoder. 052 */ 053// TODO: Do not expose this class to Coprocessors. It has set methods. A CP might meddle. 054@InterfaceAudience.LimitedPrivate({ HBaseInterfaceAudience.REPLICATION, 055 HBaseInterfaceAudience.COPROC }) 056public class WALEdit implements HeapSize { 057 private static final Logger LOG = LoggerFactory.getLogger(WALEdit.class); 058 059 // TODO: Get rid of this; see HBASE-8457 060 public static final byte [] METAFAMILY = Bytes.toBytes("METAFAMILY"); 061 @VisibleForTesting 062 public static final byte [] METAROW = Bytes.toBytes("METAROW"); 063 @VisibleForTesting 064 public static final byte[] COMPACTION = Bytes.toBytes("HBASE::COMPACTION"); 065 @VisibleForTesting 066 public static final byte [] FLUSH = Bytes.toBytes("HBASE::FLUSH"); 067 @VisibleForTesting 068 public static final byte [] REGION_EVENT = Bytes.toBytes("HBASE::REGION_EVENT"); 069 @VisibleForTesting 070 public static final byte [] BULK_LOAD = Bytes.toBytes("HBASE::BULK_LOAD"); 071 072 private final boolean isReplay; 073 074 private ArrayList<Cell> cells = null; 075 076 public WALEdit() { 077 this(false); 078 } 079 080 public WALEdit(boolean isReplay) { 081 this(1, isReplay); 082 } 083 084 public WALEdit(int cellCount) { 085 this(cellCount, false); 086 } 087 088 public WALEdit(int cellCount, boolean isReplay) { 089 this.isReplay = isReplay; 090 cells = new ArrayList<>(cellCount); 091 } 092 093 /** 094 * @param f 095 * @return True is <code>f</code> is {@link #METAFAMILY} 096 */ 097 public static boolean isMetaEditFamily(final byte [] f) { 098 return Bytes.equals(METAFAMILY, f); 099 } 100 101 public static boolean isMetaEditFamily(Cell cell) { 102 return CellUtil.matchingFamily(cell, METAFAMILY); 103 } 104 105 public boolean isMetaEdit() { 106 for (Cell cell: cells) { 107 if (!isMetaEditFamily(cell)) { 108 return false; 109 } 110 } 111 return true; 112 } 113 114 /** 115 * @return True when current WALEdit is created by log replay. Replication skips WALEdits from 116 * replay. 117 */ 118 public boolean isReplay() { 119 return this.isReplay; 120 } 121 122 @InterfaceAudience.Private 123 public WALEdit add(Cell cell) { 124 this.cells.add(cell); 125 return this; 126 } 127 128 public boolean isEmpty() { 129 return cells.isEmpty(); 130 } 131 132 public int size() { 133 return cells.size(); 134 } 135 136 public ArrayList<Cell> getCells() { 137 return cells; 138 } 139 140 /** 141 * This is not thread safe. 142 * This will change the WALEdit and shouldn't be used unless you are sure that nothing 143 * else depends on the contents being immutable. 144 * 145 * @param cells the list of cells that this WALEdit now contains. 146 */ 147 @InterfaceAudience.Private 148 public void setCells(ArrayList<Cell> cells) { 149 this.cells = cells; 150 } 151 152 /** 153 * Reads WALEdit from cells. 154 * @param cellDecoder Cell decoder. 155 * @param expectedCount Expected cell count. 156 * @return Number of KVs read. 157 */ 158 public int readFromCells(Codec.Decoder cellDecoder, int expectedCount) throws IOException { 159 cells.clear(); 160 cells.ensureCapacity(expectedCount); 161 while (cells.size() < expectedCount && cellDecoder.advance()) { 162 cells.add(cellDecoder.current()); 163 } 164 return cells.size(); 165 } 166 167 @Override 168 public long heapSize() { 169 long ret = ClassSize.ARRAYLIST; 170 for (Cell cell : cells) { 171 ret += PrivateCellUtil.estimatedSizeOfCell(cell); 172 } 173 return ret; 174 } 175 176 public long estimatedSerializedSizeOf() { 177 long ret = 0; 178 for (Cell cell: cells) { 179 ret += PrivateCellUtil.estimatedSerializedSizeOf(cell); 180 } 181 return ret; 182 } 183 184 @Override 185 public String toString() { 186 StringBuilder sb = new StringBuilder(); 187 188 sb.append("[#edits: " + cells.size() + " = <"); 189 for (Cell cell : cells) { 190 sb.append(cell); 191 sb.append("; "); 192 } 193 sb.append(">]"); 194 return sb.toString(); 195 } 196 197 public static WALEdit createFlushWALEdit(RegionInfo hri, FlushDescriptor f) { 198 KeyValue kv = new KeyValue(getRowForRegion(hri), METAFAMILY, FLUSH, 199 EnvironmentEdgeManager.currentTime(), f.toByteArray()); 200 return new WALEdit().add(kv); 201 } 202 203 public static FlushDescriptor getFlushDescriptor(Cell cell) throws IOException { 204 if (CellUtil.matchingColumn(cell, METAFAMILY, FLUSH)) { 205 return FlushDescriptor.parseFrom(CellUtil.cloneValue(cell)); 206 } 207 return null; 208 } 209 210 public static WALEdit createRegionEventWALEdit(RegionInfo hri, 211 RegionEventDescriptor regionEventDesc) { 212 KeyValue kv = new KeyValue(getRowForRegion(hri), METAFAMILY, REGION_EVENT, 213 EnvironmentEdgeManager.currentTime(), regionEventDesc.toByteArray()); 214 return new WALEdit().add(kv); 215 } 216 217 public static RegionEventDescriptor getRegionEventDescriptor(Cell cell) throws IOException { 218 if (CellUtil.matchingColumn(cell, METAFAMILY, REGION_EVENT)) { 219 return RegionEventDescriptor.parseFrom(CellUtil.cloneValue(cell)); 220 } 221 return null; 222 } 223 224 /** 225 * Create a compaction WALEdit 226 * @param c 227 * @return A WALEdit that has <code>c</code> serialized as its value 228 */ 229 public static WALEdit createCompaction(final RegionInfo hri, final CompactionDescriptor c) { 230 byte [] pbbytes = c.toByteArray(); 231 KeyValue kv = new KeyValue(getRowForRegion(hri), METAFAMILY, COMPACTION, 232 EnvironmentEdgeManager.currentTime(), pbbytes); 233 return new WALEdit().add(kv); //replication scope null so that this won't be replicated 234 } 235 236 public static byte[] getRowForRegion(RegionInfo hri) { 237 byte[] startKey = hri.getStartKey(); 238 if (startKey.length == 0) { 239 // empty row key is not allowed in mutations because it is both the start key and the end key 240 // we return the smallest byte[] that is bigger (in lex comparison) than byte[0]. 241 return new byte[] {0}; 242 } 243 return startKey; 244 } 245 246 /** 247 * Deserialized and returns a CompactionDescriptor is the KeyValue contains one. 248 * @param kv the key value 249 * @return deserialized CompactionDescriptor or null. 250 */ 251 public static CompactionDescriptor getCompaction(Cell kv) throws IOException { 252 if (isCompactionMarker(kv)) { 253 return CompactionDescriptor.parseFrom(CellUtil.cloneValue(kv)); 254 } 255 return null; 256 } 257 258 /** 259 * Returns true if the given cell is a serialized {@link CompactionDescriptor} 260 * 261 * @see #getCompaction(Cell) 262 */ 263 public static boolean isCompactionMarker(Cell cell) { 264 return CellUtil.matchingColumn(cell, METAFAMILY, COMPACTION); 265 } 266 267 /** 268 * Create a bulk loader WALEdit 269 * 270 * @param hri The RegionInfo for the region in which we are bulk loading 271 * @param bulkLoadDescriptor The descriptor for the Bulk Loader 272 * @return The WALEdit for the BulkLoad 273 */ 274 public static WALEdit createBulkLoadEvent(RegionInfo hri, 275 WALProtos.BulkLoadDescriptor bulkLoadDescriptor) { 276 KeyValue kv = new KeyValue(getRowForRegion(hri), 277 METAFAMILY, 278 BULK_LOAD, 279 EnvironmentEdgeManager.currentTime(), 280 bulkLoadDescriptor.toByteArray()); 281 return new WALEdit().add(kv); 282 } 283 284 /** 285 * Deserialized and returns a BulkLoadDescriptor from the passed in Cell 286 * @param cell the key value 287 * @return deserialized BulkLoadDescriptor or null. 288 */ 289 public static WALProtos.BulkLoadDescriptor getBulkLoadDescriptor(Cell cell) throws IOException { 290 if (CellUtil.matchingColumn(cell, METAFAMILY, BULK_LOAD)) { 291 return WALProtos.BulkLoadDescriptor.parseFrom(CellUtil.cloneValue(cell)); 292 } 293 return null; 294 } 295}