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.mob;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.util.Collections;
023import java.util.Set;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.ExtendedCell;
027import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * The MobCell will maintain a {@link Cell} and a {@link StoreFileScanner} inside. Now, the mob cell
032 * is backend by NIO ByteBuffers which are allocated from ByteBuffAllocator, so we cannot just read
033 * the cell and close the MOB file scanner because the MOB file scanner closing will deallocate the
034 * NIO ByteBuffers, which resulting memory leak.
035 * <p>
036 * Actually, the right solution is: <br>
037 * 1. Read the normal cell; <br>
038 * 2. Parse the value of normal cell and get MOB fileName,offset,length; <br>
039 * 3. Open scanner to read the mob value; <br>
040 * 4. Construct the response cell whose key is from the normal cell and value is from the mob cell.
041 * <br>
042 * 5. Ship the response cell to HBase client. <br>
043 * 6. Release both normal cell's block and mob cell's block. <br>
044 * <p>
045 * For mob cell, the block releasing just means closing the the mob scanner, so here we need to keep
046 * the {@link StoreFileScanner} inside and close only when we're ensure that the MobCell has been
047 * shipped to RPC client.
048 */
049@InterfaceAudience.Private
050public class MobCell implements Closeable {
051
052  private final ExtendedCell cell;
053  private final StoreFileScanner sfScanner;
054
055  public MobCell(ExtendedCell cell) {
056    this.cell = cell;
057    this.sfScanner = null;
058  }
059
060  public MobCell(ExtendedCell cell, StoreFileScanner sfScanner) {
061    this.cell = cell;
062    this.sfScanner = sfScanner;
063  }
064
065  public ExtendedCell getCell() {
066    return cell;
067  }
068
069  /**
070   * Returns the set of file paths successfully read by the underlying MOB store file scanner.
071   * Should be called after {@link #close()} to get the path of the MOB file that was read.
072   */
073  public Set<Path> getFilesRead() {
074    return sfScanner != null ? sfScanner.getFilesRead() : Collections.emptySet();
075  }
076
077  @Override
078  public void close() throws IOException {
079    if (this.sfScanner != null) {
080      this.sfScanner.close();
081    }
082  }
083}