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.yetus.audience.InterfaceAudience;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * An Exception that is thrown when a space quota is in violation.
026 */
027@InterfaceAudience.Public
028public class SpaceLimitingException extends QuotaExceededException {
029  private static final long serialVersionUID = 2319438922387583600L;
030  private static final Logger LOG = LoggerFactory.getLogger(SpaceLimitingException.class);
031  private static final String MESSAGE_PREFIX = SpaceLimitingException.class.getName() + ": ";
032
033  private final String policyName;
034
035  public SpaceLimitingException(String msg) {
036    super(parseMessage(msg));
037
038    // Hack around ResponseConverter expecting to invoke a single-arg String constructor
039    // on this class
040    if (msg != null) {
041      for (SpaceViolationPolicy definedPolicy : SpaceViolationPolicy.values()) {
042        if (msg.indexOf(definedPolicy.name()) != -1) {
043          policyName = definedPolicy.name();
044          return;
045        }
046      }
047    }
048    policyName = null;
049  }
050
051  public SpaceLimitingException(String policyName, String msg) {
052    super(msg);
053    this.policyName = policyName;
054  }
055
056  public SpaceLimitingException(String policyName, String msg, Throwable e) {
057    super(msg, e);
058    this.policyName = policyName;
059  }
060
061  /**
062   * Returns the violation policy in effect.
063   * @return The violation policy in effect.
064   */
065  public String getViolationPolicy() {
066    return this.policyName;
067  }
068
069  private static String parseMessage(String originalMessage) {
070    // Serialization of the exception places a duplicate class name. Try to strip that off if it
071    // exists. Best effort... Looks something like:
072    // "org.apache.hadoop.hbase.quotas.SpaceLimitingException: NO_INSERTS A Put is disallowed due
073    // to a space quota."
074    if (originalMessage != null && originalMessage.startsWith(MESSAGE_PREFIX)) {
075      // If it starts with the class name, rip off the policy too.
076      try {
077        int index = originalMessage.indexOf(' ', MESSAGE_PREFIX.length());
078        return originalMessage.substring(index + 1);
079      } catch (Exception e) {
080        if (LOG.isTraceEnabled()) {
081          LOG.trace("Failed to trim exception message", e);
082        }
083      }
084    }
085    return originalMessage;
086  }
087
088  @Override
089  public String getMessage() {
090    return (policyName == null ? "(unknown policy)" : policyName) + " " + super.getMessage();
091  }
092}