001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.quotas;
018
019import java.io.IOException;
020import java.util.Map;
021import java.util.Map.Entry;
022
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.
031 *
032 * An entity is presently a table or a namespace.
033 */
034@InterfaceAudience.Private
035public interface QuotaSnapshotStore<T> {
036
037  /**
038   * Singleton to represent a table without a quota defined. It is never in violation.
039   */
040  public static final SpaceQuotaSnapshot NO_QUOTA = new SpaceQuotaSnapshot(
041      SpaceQuotaStatus.notInViolation(), -1, -1);
042
043  /**
044   * Fetch the Quota for the given {@code subject}. May be null.
045   *
046   * @param subject The object for which the quota should be fetched
047   */
048  SpaceQuota getSpaceQuota(T subject) throws IOException;
049
050  /**
051   * Returns the current {@link SpaceQuotaSnapshot} for the given {@code subject}.
052   *
053   * @param subject The object which the quota snapshot should be fetched
054   */
055  SpaceQuotaSnapshot getCurrentState(T subject);
056
057  /**
058   * Computes the target {@link SpaceQuotaSnapshot} for the given {@code subject} and
059   * {@code spaceQuota}.
060   *
061   * @param subject The object which to determine the target SpaceQuotaSnapshot of
062   * @param spaceQuota The quota "definition" for the {@code subject}
063   */
064  SpaceQuotaSnapshot getTargetState(T subject, SpaceQuota spaceQuota) throws IOException;
065
066  /**
067   * Filters the provided <code>regions</code>, returning those which match the given
068   * <code>subject</code>.
069   *
070   * @param subject The filter criteria. Only regions belonging to this parameter will be returned
071   */
072  Iterable<Entry<RegionInfo,Long>> filterBySubject(T subject);
073
074  /**
075   * Persists the current {@link SpaceQuotaSnapshot} for the {@code subject}.
076   *
077   * @param subject The object which the {@link SpaceQuotaSnapshot} is being persisted for
078   * @param state The current state of the {@code subject}
079   */
080  void setCurrentState(T subject, SpaceQuotaSnapshot state);
081
082  /**
083   * Updates {@code this} with the latest snapshot of filesystem use by region.
084   *
085   * @param regionUsage A map of {@code RegionInfo} objects to their filesystem usage in bytes
086   */
087  void setRegionUsage(Map<RegionInfo,Long> regionUsage);
088}