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 com.google.errorprone.annotations.RestrictedApi; 021import java.util.List; 022import java.util.concurrent.atomic.AtomicBoolean; 023import org.apache.hadoop.hbase.ExtendedCell; 024import org.apache.hadoop.hbase.nio.RefCnt; 025import org.apache.yetus.audience.InterfaceAudience; 026 027/** 028 * A MemStoreLAB implementation which wraps N MemStoreLABs. Its main duty is in proper managing the 029 * close of the individual MemStoreLAB. This is treated as an immutable one and so do not allow to 030 * add any more Cells into it. {@link #copyCellInto(ExtendedCell)} throws Exception 031 */ 032@InterfaceAudience.Private 033public class ImmutableMemStoreLAB implements MemStoreLAB { 034 035 private final RefCnt refCnt; 036 private final AtomicBoolean closed = new AtomicBoolean(false); 037 038 private final List<MemStoreLAB> mslabs; 039 040 public ImmutableMemStoreLAB(List<MemStoreLAB> mslabs) { 041 this.mslabs = mslabs; 042 this.refCnt = RefCnt.create(() -> { 043 closeMSLABs(); 044 }); 045 } 046 047 @Override 048 public ExtendedCell copyCellInto(ExtendedCell cell) { 049 throw new IllegalStateException("This is an Immutable MemStoreLAB."); 050 } 051 052 /** 053 * The process of merging assumes all cells are allocated on mslab. There is a rare case in which 054 * the first immutable segment, participating in a merge, is a CSLM. Since the CSLM hasn't been 055 * flattened yet, and there is no point in flattening it (since it is going to be merged), its big 056 * cells (for whom size > maxAlloc) must be copied into mslab. This method copies the passed cell 057 * into the first mslab in the mslabs list, returning either a new cell instance over the copied 058 * data, or null when this cell cannt be copied. 059 */ 060 @Override 061 public ExtendedCell forceCopyOfBigCellInto(ExtendedCell cell) { 062 MemStoreLAB mslab = this.mslabs.get(0); 063 return mslab.forceCopyOfBigCellInto(cell); 064 } 065 066 /* 067 * Returning a new pool chunk, without replacing current chunk, meaning MSLABImpl does not make 068 * the returned chunk as CurChunk. The space on this chunk will be allocated externally. The 069 * interface is only for external callers. 070 */ 071 @Override 072 public Chunk getNewExternalChunk(ChunkCreator.ChunkType chunkType) { 073 MemStoreLAB mslab = this.mslabs.get(0); 074 return mslab.getNewExternalChunk(chunkType); 075 } 076 077 /* 078 * Returning a new chunk, without replacing current chunk, meaning MSLABImpl does not make the 079 * returned chunk as CurChunk. The space on this chunk will be allocated externally. The interface 080 * is only for external callers. 081 */ 082 @Override 083 public Chunk getNewExternalChunk(int size) { 084 MemStoreLAB mslab = this.mslabs.get(0); 085 return mslab.getNewExternalChunk(size); 086 } 087 088 @Override 089 public void close() { 090 // 'refCnt' here tracks the scanners opened on segments which directly refer to this 091 // MSLAB. The individual MSLABs this refers also having its own 'refCnt'. The usage of 092 // the variable in close() and decScannerCount() is as as that in HeapMemstoreLAB. Here the 093 // close just delegates the call to the individual MSLABs. The actual return of the chunks to 094 // MSLABPool will happen within individual MSLABs only (which is at the leaf level). 095 // Say an ImmutableMemStoreLAB is created over 2 HeapMemStoreLABs at some point and at that time 096 // both of them were referred by ongoing scanners. So they have > 0 'refCnt'. Now over 097 // the new Segment some scanners come in and this MSLABs 'refCnt' also goes up and 098 // then come down on finish of scanners. Now a close() call comes to this Immutable MSLAB. As 099 // it's 'refCnt' is zero it will call close() on both of the Heap MSLABs. Say by that 100 // time the old scanners on one of the MSLAB got over where as on the other, still an old 101 // scanner is going on. The call close() on that MSLAB will not close it immediately but will 102 // just decrease it's 'refCnt' and it's 'refCnt' still > 0. Later once the old scan is 103 // over, the decScannerCount() call will do the actual close and return of the chunks. 104 if (!this.closed.compareAndSet(false, true)) { 105 return; 106 } 107 // When there are still on going scanners over this MSLAB, we will defer the close until all 108 // scanners finish. We will just decrease it's 'refCnt'. See #decScannerCount(). This will be 109 // called at end of every scan. When it's 'refCnt' reached 0, we will do the actual close then. 110 this.refCnt.release(); 111 } 112 113 private void closeMSLABs() { 114 for (MemStoreLAB mslab : this.mslabs) { 115 mslab.close(); 116 } 117 } 118 119 @Override 120 public void incScannerCount() { 121 this.refCnt.retain(); 122 } 123 124 @Override 125 public void decScannerCount() { 126 this.refCnt.release(); 127 } 128 129 @Override 130 public boolean isOnHeap() { 131 return !isOffHeap(); 132 } 133 134 @Override 135 public boolean isOffHeap() { 136 return ChunkCreator.getInstance().isOffheap(); 137 } 138 139 @RestrictedApi(explanation = "Should only be called in tests", link = "", 140 allowedOnPath = ".*/src/test/.*") 141 int getRefCntValue() { 142 return this.refCnt.refCnt(); 143 } 144 145 @RestrictedApi(explanation = "Should only be called in tests", link = "", 146 allowedOnPath = ".*/src/test/.*") 147 boolean isClosed() { 148 return this.closed.get(); 149 } 150 151}