001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.wal;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.UUID;
026import org.apache.hadoop.hbase.HBaseInterfaceAudience;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.regionserver.SequenceId;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * Key for WAL Entry.
035 */
036@InterfaceAudience.LimitedPrivate({ HBaseInterfaceAudience.REPLICATION,
037  HBaseInterfaceAudience.COPROC })
038public interface WALKey extends SequenceId, Comparable<WALKey> {
039  /**
040   * Unmodifiable empty list of UUIDs.
041   */
042  List<UUID> EMPTY_UUIDS = Collections.unmodifiableList(new ArrayList<UUID>());
043
044  default long estimatedSerializedSizeOf() {
045    return 0;
046  }
047
048  /** Returns encoded region name */
049  byte[] getEncodedRegionName();
050
051  /** Returns table name */
052  TableName getTableName();
053
054  /** Returns the write time */
055  long getWriteTime();
056
057  /** Returns The nonce group */
058  default long getNonceGroup() {
059    return HConstants.NO_NONCE;
060  }
061
062  /** Returns The nonce */
063  default long getNonce() {
064    return HConstants.NO_NONCE;
065  }
066
067  UUID getOriginatingClusterId();
068
069  /**
070   * Return a positive long if current WALKeyImpl is created from a replay edit; a replay edit is an
071   * edit that came in when replaying WALs of a crashed server.
072   * @return original sequence number of the WALEdit
073   */
074  long getOrigLogSeqNum();
075
076  /**
077   * Add a named String value to this WALKey to be persisted into the WAL
078   * @param attributeKey   Name of the attribute
079   * @param attributeValue Value of the attribute
080   */
081  void addExtendedAttribute(String attributeKey, byte[] attributeValue);
082
083  /**
084   * Return a named String value injected into the WALKey during processing, such as by a
085   * coprocessor
086   * @param attributeKey The key of a key / value pair
087   */
088  default byte[] getExtendedAttribute(String attributeKey) {
089    return null;
090  }
091
092  /**
093   * Returns a map of all extended attributes injected into this WAL key.
094   */
095  default Map<String, byte[]> getExtendedAttributes() {
096    return new HashMap<>();
097  }
098
099  /**
100   * Produces a string map for this key. Useful for programmatic use and manipulation of the data
101   * stored in an WALKeyImpl, for example, printing as JSON.
102   * @return a Map containing data from this key
103   */
104  default Map<String, Object> toStringMap() {
105    Map<String, Object> stringMap = new HashMap<>();
106    stringMap.put("table", getTableName());
107    stringMap.put("region", Bytes.toStringBinary(getEncodedRegionName()));
108    stringMap.put("sequence", getSequenceId());
109    Map<String, byte[]> extendedAttributes = getExtendedAttributes();
110    if (extendedAttributes != null) {
111      for (Map.Entry<String, byte[]> entry : extendedAttributes.entrySet()) {
112        stringMap.put(entry.getKey(), Bytes.toStringBinary(entry.getValue()));
113      }
114    }
115    return stringMap;
116  }
117}