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