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.quotas;
019
020import java.io.IOException;
021import java.util.Map;
022import java.util.Map.Entry;
023import org.apache.hadoop.hbase.client.RegionInfo;
024import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceQuota;
028
029/**
030 * A common interface for computing and storing space quota observance/violation for entities. An
031 * entity is presently a table or a namespace.
032 */
033@InterfaceAudience.Private
034public interface QuotaSnapshotStore<T> {
035
036  /**
037   * Singleton to represent a table without a quota defined. It is never in violation.
038   */
039  public static final SpaceQuotaSnapshot NO_QUOTA =
040    new SpaceQuotaSnapshot(SpaceQuotaStatus.notInViolation(), -1, -1);
041
042  /**
043   * Fetch the Quota for the given {@code subject}. May be null.
044   * @param subject The object for which the quota should be fetched
045   */
046  SpaceQuota getSpaceQuota(T subject) throws IOException;
047
048  /**
049   * Returns the current {@link SpaceQuotaSnapshot} for the given {@code subject}.
050   * @param subject The object which the quota snapshot should be fetched
051   */
052  SpaceQuotaSnapshot getCurrentState(T subject);
053
054  /**
055   * Computes the target {@link SpaceQuotaSnapshot} for the given {@code subject} and
056   * {@code spaceQuota}.
057   * @param subject    The object which to determine the target SpaceQuotaSnapshot of
058   * @param spaceQuota The quota "definition" for the {@code subject}
059   */
060  SpaceQuotaSnapshot getTargetState(T subject, SpaceQuota spaceQuota) throws IOException;
061
062  /**
063   * Filters the provided <code>regions</code>, returning those which match the given
064   * <code>subject</code>.
065   * @param subject The filter criteria. Only regions belonging to this parameter will be returned
066   */
067  Iterable<Entry<RegionInfo, Long>> filterBySubject(T subject);
068
069  /**
070   * Persists the current {@link SpaceQuotaSnapshot} for the {@code subject}.
071   * @param subject The object which the {@link SpaceQuotaSnapshot} is being persisted for
072   * @param state   The current state of the {@code subject}
073   */
074  void setCurrentState(T subject, SpaceQuotaSnapshot state);
075
076  /**
077   * Updates {@code this} with the latest snapshot of filesystem use by region.
078   * @param regionUsage A map of {@code RegionInfo} objects to their filesystem usage in bytes
079   */
080  void setRegionUsage(Map<RegionInfo, Long> regionUsage);
081}