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.regionserver;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashSet;
024import java.util.List;
025import java.util.NavigableSet;
026import java.util.Set;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.ExtendedCell;
029import org.apache.hadoop.hbase.client.Scan;
030import org.apache.hadoop.hbase.mob.MobCell;
031import org.apache.hadoop.hbase.mob.MobUtils;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * ReversedMobStoreScanner extends from ReversedStoreScanner, and is used to support reversed
038 * scanning in both the memstore and the MOB store.
039 */
040@InterfaceAudience.Private
041public class ReversedMobStoreScanner extends ReversedStoreScanner {
042
043  private static final Logger LOG = LoggerFactory.getLogger(ReversedMobStoreScanner.class);
044  private boolean cacheMobBlocks = false;
045  private boolean rawMobScan = false;
046  private boolean readEmptyValueOnMobCellMiss = false;
047  private final HMobStore mobStore;
048  private final List<MobCell> referencedMobCells;
049  private final Set<Path> mobFilesRead = new HashSet<>();
050
051  ReversedMobStoreScanner(HStore store, ScanInfo scanInfo, Scan scan, NavigableSet<byte[]> columns,
052    long readPt) throws IOException {
053    super(store, scanInfo, scan, columns, readPt);
054    cacheMobBlocks = MobUtils.isCacheMobBlocks(scan);
055    rawMobScan = MobUtils.isRawMobScan(scan);
056    readEmptyValueOnMobCellMiss = MobUtils.isReadEmptyValueOnMobCellMiss(scan);
057    if (!(store instanceof HMobStore)) {
058      throw new IllegalArgumentException("The store " + store + " is not a HMobStore");
059    }
060    mobStore = (HMobStore) store;
061    this.referencedMobCells = new ArrayList<>();
062  }
063
064  /**
065   * Firstly reads the cells from the HBase. If the cell is a reference cell (which has the
066   * reference tag), the scanner need seek this cell from the mob file, and use the cell found from
067   * the mob file as the result.
068   */
069  @Override
070  public boolean next(List<? super ExtendedCell> outResult, ScannerContext ctx) throws IOException {
071    boolean result = super.next(outResult, ctx);
072    if (!rawMobScan) {
073      // retrieve the mob data
074      if (outResult.isEmpty()) {
075        return result;
076      }
077      long mobKVCount = 0;
078      long mobKVSize = 0;
079      for (int i = 0; i < outResult.size(); i++) {
080        ExtendedCell cell = (ExtendedCell) outResult.get(i);
081        if (MobUtils.isMobReferenceCell(cell)) {
082          MobCell mobCell =
083            mobStore.resolve(cell, cacheMobBlocks, readPt, readEmptyValueOnMobCellMiss);
084          mobKVCount++;
085          mobKVSize += mobCell.getCell().getValueLength();
086          outResult.set(i, mobCell.getCell());
087          // Keep the MobCell here unless we shipped the RPC or close the scanner.
088          referencedMobCells.add(mobCell);
089        }
090      }
091      mobStore.updateMobScanCellsCount(mobKVCount);
092      mobStore.updateMobScanCellsSize(mobKVSize);
093    }
094    return result;
095  }
096
097  private void freeAllReferencedMobCells() throws IOException {
098    for (MobCell mobCell : referencedMobCells) {
099      mobCell.close();
100      mobFilesRead.addAll(mobCell.getFilesRead());
101    }
102    referencedMobCells.clear();
103  }
104
105  /**
106   * Returns the set of store file paths that were successfully read by this scanner. Includes paths
107   * from the underlying store scanner and from resolved MOB cell references; typically populated as
108   * scanners and referenced MOB cells are closed.
109   */
110  @Override
111  public Set<Path> getFilesRead() {
112    Set<Path> allFiles = new HashSet<>(super.getFilesRead());
113    allFiles.addAll(mobFilesRead);
114    return Collections.unmodifiableSet(allFiles);
115  }
116
117  @Override
118  public void shipped() throws IOException {
119    super.shipped();
120    this.freeAllReferencedMobCells();
121  }
122
123  @Override
124  public void close() {
125    super.close();
126    try {
127      this.freeAllReferencedMobCells();
128    } catch (IOException e) {
129      LOG.warn("Failed to free referenced mob cells: ", e);
130    }
131  }
132}