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.util.Iterator;
021import java.util.SortedSet;
022import java.util.concurrent.atomic.AtomicBoolean;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellComparator;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.PrivateCellUtil;
029import org.apache.hadoop.hbase.util.ClassSize;
030import org.apache.yetus.audience.InterfaceAudience;
031
032/**
033 * A mutable segment in memstore, specifically the active segment.
034 */
035@InterfaceAudience.Private
036public class MutableSegment extends Segment {
037
038  private final AtomicBoolean flushed = new AtomicBoolean(false);
039
040  public final static long DEEP_OVERHEAD =
041    ClassSize.align(Segment.DEEP_OVERHEAD + ClassSize.CONCURRENT_SKIPLISTMAP
042      + ClassSize.SYNC_TIMERANGE_TRACKER + ClassSize.REFERENCE + ClassSize.ATOMIC_BOOLEAN);
043
044  protected MutableSegment(CellSet cellSet, CellComparator comparator, MemStoreLAB memStoreLAB,
045    MemStoreSizing memstoreSizing) {
046    super(cellSet, comparator, memStoreLAB, TimeRangeTracker.create(TimeRangeTracker.Type.SYNC));
047    incMemStoreSize(0, DEEP_OVERHEAD, 0, 0); // update the mutable segment metadata
048    if (memstoreSizing != null) {
049      memstoreSizing.incMemStoreSize(0, DEEP_OVERHEAD, 0, 0);
050    }
051  }
052
053  /**
054   * Adds the given cell into the segment
055   * @param cell      the cell to add
056   * @param mslabUsed whether using MSLAB
057   */
058  public void add(Cell cell, boolean mslabUsed, MemStoreSizing memStoreSizing,
059    boolean sizeAddedPreOperation) {
060    internalAdd(cell, mslabUsed, memStoreSizing, sizeAddedPreOperation);
061  }
062
063  public void upsert(Cell cell, long readpoint, MemStoreSizing memStoreSizing,
064    boolean sizeAddedPreOperation) {
065    internalAdd(cell, false, memStoreSizing, sizeAddedPreOperation);
066
067    // Get the Cells for the row/family/qualifier regardless of timestamp.
068    // For this case we want to clean up any other puts
069    Cell firstCell = PrivateCellUtil.createFirstOnRowColTS(cell, HConstants.LATEST_TIMESTAMP);
070    SortedSet<Cell> ss = this.tailSet(firstCell);
071    Iterator<Cell> it = ss.iterator();
072    // versions visible to oldest scanner
073    int versionsVisible = 0;
074    while (it.hasNext()) {
075      Cell cur = it.next();
076
077      if (cell == cur) {
078        // ignore the one just put in
079        continue;
080      }
081      // check that this is the row and column we are interested in, otherwise bail
082      if (CellUtil.matchingRows(cell, cur) && CellUtil.matchingQualifier(cell, cur)) {
083        // only remove Puts that concurrent scanners cannot possibly see
084        if (cur.getTypeByte() == KeyValue.Type.Put.getCode() && cur.getSequenceId() <= readpoint) {
085          if (versionsVisible >= 1) {
086            // if we get here we have seen at least one version visible to the oldest scanner,
087            // which means we can prove that no scanner will see this version
088
089            // false means there was a change, so give us the size.
090            // TODO when the removed cell ie.'cur' having its data in MSLAB, we can not release that
091            // area. Only the Cell object as such going way. We need to consider cellLen to be
092            // decreased there as 0 only. Just keeping it as existing code now. We need to know the
093            // removed cell is from MSLAB or not. Will do once HBASE-16438 is in
094            int cellLen = getCellLength(cur);
095            long heapSize = heapSizeChange(cur, true);
096            long offHeapSize = offHeapSizeChange(cur, true);
097            incMemStoreSize(-cellLen, -heapSize, -offHeapSize, -1);
098            if (memStoreSizing != null) {
099              memStoreSizing.decMemStoreSize(cellLen, heapSize, offHeapSize, 1);
100            }
101            it.remove();
102          } else {
103            versionsVisible++;
104          }
105        }
106      } else {
107        // past the row or column, done
108        break;
109      }
110    }
111  }
112
113  public boolean setInMemoryFlushed() {
114    return flushed.compareAndSet(false, true);
115  }
116
117  /**
118   * Returns the first cell in the segment
119   * @return the first cell in the segment
120   */
121  Cell first() {
122    return this.getCellSet().first();
123  }
124
125  @Override
126  protected long indexEntrySize() {
127    return ClassSize.CONCURRENT_SKIPLISTMAP_ENTRY;
128  }
129}