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 org.apache.hadoop.conf.Configuration;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.yetus.audience.InterfaceStability;
023
024import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Quotas;
025
026/**
027 * In-Memory state of table or namespace quotas
028 */
029@InterfaceAudience.Private
030@InterfaceStability.Evolving
031@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
032    justification = "FindBugs seems confused; says globalLimiter and lastUpdate "
033      + "are mostly synchronized...but to me it looks like they are totally synchronized")
034public class QuotaState {
035  protected QuotaLimiter globalLimiter = NoopQuotaLimiter.get();
036
037  @Override
038  public synchronized String toString() {
039    StringBuilder builder = new StringBuilder();
040    builder.append("QuotaState(");
041    if (isBypass()) {
042      builder.append("bypass");
043    } else {
044      if (globalLimiter != NoopQuotaLimiter.get()) {
045        // builder.append(" global-limiter");
046        builder.append(" " + globalLimiter);
047      }
048    }
049    builder.append(')');
050    return builder.toString();
051  }
052
053  /** Returns true if there is no quota information associated to this object */
054  public synchronized boolean isBypass() {
055    return globalLimiter == NoopQuotaLimiter.get();
056  }
057
058  /**
059   * Setup the global quota information. (This operation is part of the QuotaState setup)
060   */
061  public synchronized void setQuotas(Configuration conf, final Quotas quotas) {
062    if (quotas.hasThrottle()) {
063      globalLimiter = QuotaLimiterFactory.fromThrottle(conf, quotas.getThrottle());
064    } else {
065      globalLimiter = NoopQuotaLimiter.get();
066    }
067  }
068
069  /** visible for testing */
070  void setGlobalLimiter(QuotaLimiter globalLimiter) {
071    this.globalLimiter = globalLimiter;
072  }
073
074  /**
075   * Perform an update of the quota info based on the other quota info object. (This operation is
076   * executed by the QuotaCache)
077   */
078  public synchronized void update(final QuotaState other) {
079    if (globalLimiter == NoopQuotaLimiter.get()) {
080      globalLimiter = other.globalLimiter;
081    } else if (other.globalLimiter == NoopQuotaLimiter.get()) {
082      globalLimiter = NoopQuotaLimiter.get();
083    } else {
084      globalLimiter = QuotaLimiterFactory.update(globalLimiter, other.globalLimiter);
085    }
086  }
087
088  /**
089   * Return the limiter associated with this quota.
090   * @return the quota limiter
091   */
092  public synchronized QuotaLimiter getGlobalLimiter() {
093    return globalLimiter;
094  }
095
096}