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