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