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
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   *
064   * @return The violation policy in effect.
065   */
066  public String getViolationPolicy() {
067    return this.policyName;
068  }
069
070  private static String parseMessage(String originalMessage) {
071    // Serialization of the exception places a duplicate class name. Try to strip that off if it
072    // exists. Best effort... Looks something like:
073    // "org.apache.hadoop.hbase.quotas.SpaceLimitingException: NO_INSERTS A Put is disallowed due
074    // to a space quota."
075    if (originalMessage != null && originalMessage.startsWith(MESSAGE_PREFIX)) {
076      // If it starts with the class name, rip off the policy too.
077      try {
078        int index = originalMessage.indexOf(' ', MESSAGE_PREFIX.length());
079        return originalMessage.substring(index + 1);
080      } catch (Exception e) {
081        if (LOG.isTraceEnabled()) {
082          LOG.trace("Failed to trim exception message", e);
083        }
084      }
085    }
086    return originalMessage;
087  }
088
089  @Override
090  public String getMessage() {
091    return (policyName == null ? "(unknown policy)" : policyName) + " " + super.getMessage();
092  }
093}