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.List;
023import java.util.NavigableSet;
024import org.apache.hadoop.hbase.ExtendedCell;
025import org.apache.hadoop.hbase.client.Scan;
026import org.apache.hadoop.hbase.mob.MobCell;
027import org.apache.hadoop.hbase.mob.MobUtils;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Scanner scans both the memstore and the MOB Store. Coalesce KeyValue stream into
034 * {@code List<KeyValue>} for a single row.
035 */
036@InterfaceAudience.Private
037public class MobStoreScanner extends StoreScanner {
038
039  private static final Logger LOG = LoggerFactory.getLogger(MobStoreScanner.class);
040
041  private boolean cacheMobBlocks = false;
042  private boolean rawMobScan = false;
043  private boolean readEmptyValueOnMobCellMiss = false;
044  private final HMobStore mobStore;
045  private final List<MobCell> referencedMobCells;
046
047  public MobStoreScanner(HStore store, ScanInfo scanInfo, Scan scan,
048    final NavigableSet<byte[]> columns, long readPt) throws IOException {
049    super(store, scanInfo, scan, columns, readPt);
050    cacheMobBlocks = MobUtils.isCacheMobBlocks(scan);
051    rawMobScan = MobUtils.isRawMobScan(scan);
052    readEmptyValueOnMobCellMiss = MobUtils.isReadEmptyValueOnMobCellMiss(scan);
053    if (!(store instanceof HMobStore)) {
054      throw new IllegalArgumentException("The store " + store + " is not a HMobStore");
055    }
056    mobStore = (HMobStore) store;
057    this.referencedMobCells = new ArrayList<>();
058  }
059
060  /**
061   * Firstly reads the cells from the HBase. If the cell are a reference cell (which has the
062   * reference tag), the scanner need seek this cell from the mob file, and use the cell found from
063   * the mob file as the result.
064   */
065  @Override
066  public boolean next(List<? super ExtendedCell> outResult, ScannerContext ctx) throws IOException {
067    boolean result = super.next(outResult, ctx);
068    if (!rawMobScan) {
069      // retrieve the mob data
070      if (outResult.isEmpty()) {
071        return result;
072      }
073      long mobKVCount = 0;
074      long mobKVSize = 0;
075      for (int i = 0; i < outResult.size(); i++) {
076        // At server side, we should only get ExtendedCell
077        ExtendedCell cell = (ExtendedCell) outResult.get(i);
078        if (MobUtils.isMobReferenceCell(cell)) {
079          MobCell mobCell =
080            mobStore.resolve(cell, cacheMobBlocks, readPt, readEmptyValueOnMobCellMiss);
081          mobKVCount++;
082          mobKVSize += mobCell.getCell().getValueLength();
083          outResult.set(i, mobCell.getCell());
084          // Keep the MobCell here unless we shipped the RPC or close the scanner.
085          referencedMobCells.add(mobCell);
086        }
087      }
088      mobStore.updateMobScanCellsCount(mobKVCount);
089      mobStore.updateMobScanCellsSize(mobKVSize);
090    }
091    return result;
092  }
093
094  private void freeAllReferencedMobCells() throws IOException {
095    for (MobCell cell : referencedMobCells) {
096      cell.close();
097    }
098    referencedMobCells.clear();
099  }
100
101  @Override
102  public void shipped() throws IOException {
103    super.shipped();
104    this.freeAllReferencedMobCells();
105  }
106
107  @Override
108  public void close() {
109    super.close();
110    try {
111      this.freeAllReferencedMobCells();
112    } catch (IOException e) {
113      LOG.warn("Failed to free referenced mob cells: ", e);
114    }
115  }
116}