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.Closeable;
021import java.io.IOException;
022import java.util.List;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * The MemStore holds in-memory modifications to the Store. Modifications are {@link Cell}s.
029 * <p>
030 * The MemStore functions should not be called in parallel. Callers should hold write and read
031 * locks. This is done in {@link HStore}.
032 * </p>
033 */
034@InterfaceAudience.Private
035public interface MemStore extends Closeable {
036
037  /**
038   * Creates a snapshot of the current memstore. Snapshot must be cleared by call to
039   * {@link #clearSnapshot(long)}.
040   * @return {@link MemStoreSnapshot}
041   */
042  MemStoreSnapshot snapshot();
043
044  /**
045   * Clears the current snapshot of the Memstore.
046   * @see #snapshot()
047   */
048  void clearSnapshot(long id) throws UnexpectedStateException;
049
050  /**
051   * Flush will first clear out the data in snapshot if any (It will take a second flush invocation
052   * to clear the current Cell set). If snapshot is empty, current Cell set will be flushed.
053   * @return On flush, how much memory we will clear.
054   */
055  MemStoreSize getFlushableSize();
056
057  /**
058   * Return the size of the snapshot(s) if any
059   * @return size of the memstore snapshot
060   */
061  MemStoreSize getSnapshotSize();
062
063  /**
064   * Write an update
065   * @param memstoreSizing The delta in memstore size will be passed back via this. This will
066   *                       include both data size and heap overhead delta.
067   */
068  void add(final Cell cell, MemStoreSizing memstoreSizing);
069
070  /**
071   * Write the updates
072   * @param memstoreSizing The delta in memstore size will be passed back via this. This will
073   *                       include both data size and heap overhead delta.
074   */
075  void add(Iterable<Cell> cells, MemStoreSizing memstoreSizing);
076
077  /** Returns Oldest timestamp of all the Cells in the MemStore */
078  long timeOfOldestEdit();
079
080  /**
081   * Update or insert the specified cells.
082   * <p>
083   * For each Cell, insert into MemStore. This will atomically upsert the value for that
084   * row/family/qualifier. If a Cell did already exist, it will then be removed.
085   * <p>
086   * Currently the memstoreTS is kept at 0 so as each insert happens, it will be immediately
087   * visible. May want to change this so it is atomic across all KeyValues.
088   * <p>
089   * This is called under row lock, so Get operations will still see updates atomically. Scans will
090   * only see each KeyValue update as atomic.
091   * @param readpoint      readpoint below which we can safely remove duplicate Cells.
092   * @param memstoreSizing The delta in memstore size will be passed back via this. This will
093   *                       include both data size and heap overhead delta.
094   */
095  void upsert(Iterable<Cell> cells, long readpoint, MemStoreSizing memstoreSizing);
096
097  /**
098   * @return scanner over the memstore. This might include scanner over the snapshot when one is
099   *         present.
100   */
101  List<KeyValueScanner> getScanners(long readPt) throws IOException;
102
103  /**
104   * @return Total memory occupied by this MemStore. This won't include any size occupied by the
105   *         snapshot. We assume the snapshot will get cleared soon. This is not thread safe and the
106   *         memstore may be changed while computing its size. It is the responsibility of the
107   *         caller to make sure this doesn't happen.
108   */
109  MemStoreSize size();
110
111  /**
112   * This method is called before the flush is executed.
113   * @return an estimation (lower bound) of the unflushed sequence id in memstore after the flush is
114   *         executed. if memstore will be cleared returns {@code HConstants.NO_SEQNUM}.
115   */
116  long preFlushSeqIDEstimation();
117
118  /* Return true if the memstore may use some extra memory space */
119  boolean isSloppy();
120
121  /**
122   * This message intends to inform the MemStore that next coming updates are going to be part of
123   * the replaying edits from WAL
124   */
125  default void startReplayingFromWAL() {
126    return;
127  }
128
129  /**
130   * This message intends to inform the MemStore that the replaying edits from WAL are done
131   */
132  default void stopReplayingFromWAL() {
133    return;
134  }
135
136  /**
137   * Close the memstore.
138   * <p>
139   * Usually this should only be called when there is nothing in the memstore, unless we are going
140   * to abort ourselves.
141   * <p>
142   * For normal cases, this method is only used to fix the reference counting, see HBASE-27941.
143   */
144  @Override
145  void close();
146}