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.replication.regionserver;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.wal.WAL.Entry;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * Holds a batch of WAL entries to replicate, along with some statistics
030 */
031@InterfaceAudience.Private
032class WALEntryBatch {
033
034  // used by recovered replication queue to indicate that all the entries have been read.
035  public static final WALEntryBatch NO_MORE_DATA = new WALEntryBatch(0, null);
036
037  private List<Entry> walEntries;
038  // last WAL that was read
039  private Path lastWalPath;
040  // position in WAL of last entry in this batch
041  private long lastWalPosition = 0;
042  // number of distinct row keys in this batch
043  private int nbRowKeys = 0;
044  // number of HFiles
045  private int nbHFiles = 0;
046  // heap size of data we need to replicate
047  private long heapSize = 0;
048  // save the last sequenceid for each region if the table has serial-replication scope
049  private Map<String, Long> lastSeqIds = new HashMap<>();
050  // indicate that this is the end of the current file
051  private boolean endOfFile;
052
053  /**
054   * @param lastWalPath Path of the WAL the last entry in this batch was read from
055   */
056  WALEntryBatch(int maxNbEntries, Path lastWalPath) {
057    this.walEntries = new ArrayList<>(maxNbEntries);
058    this.lastWalPath = lastWalPath;
059  }
060
061
062  static WALEntryBatch endOfFile(Path lastWalPath) {
063    WALEntryBatch batch = new WALEntryBatch(0, lastWalPath);
064    batch.setLastWalPosition(-1L);
065    batch.setEndOfFile(true);
066    return batch;
067  }
068
069  public void addEntry(Entry entry) {
070    walEntries.add(entry);
071  }
072
073  /**
074   * @return the WAL Entries.
075   */
076  public List<Entry> getWalEntries() {
077    return walEntries;
078  }
079
080  /**
081   * @return the path of the last WAL that was read.
082   */
083  public Path getLastWalPath() {
084    return lastWalPath;
085  }
086
087  /**
088   * @return the position in the last WAL that was read.
089   */
090  public long getLastWalPosition() {
091    return lastWalPosition;
092  }
093
094  public void setLastWalPosition(long lastWalPosition) {
095    this.lastWalPosition = lastWalPosition;
096  }
097
098  public int getNbEntries() {
099    return walEntries.size();
100  }
101
102  /**
103   * @return the number of distinct row keys in this batch
104   */
105  public int getNbRowKeys() {
106    return nbRowKeys;
107  }
108
109  /**
110   * @return the number of HFiles in this batch
111   */
112  public int getNbHFiles() {
113    return nbHFiles;
114  }
115
116  /**
117   * @return total number of operations in this batch
118   */
119  public int getNbOperations() {
120    return getNbRowKeys() + getNbHFiles();
121  }
122
123  /**
124   * @return the heap size of this batch
125   */
126  public long getHeapSize() {
127    return heapSize;
128  }
129
130  /**
131   * @return the last sequenceid for each region if the table has serial-replication scope
132   */
133  public Map<String, Long> getLastSeqIds() {
134    return lastSeqIds;
135  }
136
137  public boolean isEndOfFile() {
138    return endOfFile;
139  }
140
141  public void setEndOfFile(boolean endOfFile) {
142    this.endOfFile = endOfFile;
143  }
144
145  public void incrementNbRowKeys(int increment) {
146    nbRowKeys += increment;
147  }
148
149  public void incrementNbHFiles(int increment) {
150    nbHFiles += increment;
151  }
152
153  public void incrementHeapSize(long increment) {
154    heapSize += increment;
155  }
156
157  public void setLastSeqId(String region, long sequenceId) {
158    lastSeqIds.put(region, sequenceId);
159  }
160
161  @Override
162  public String toString() {
163    return "WALEntryBatch [walEntries=" + walEntries + ", lastWalPath=" + lastWalPath +
164      ", lastWalPosition=" + lastWalPosition + ", nbRowKeys=" + nbRowKeys + ", nbHFiles=" +
165      nbHFiles + ", heapSize=" + heapSize + ", lastSeqIds=" + lastSeqIds + ", endOfFile=" +
166      endOfFile + "]";
167  }
168}