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.policies;
019
020import java.io.IOException;
021import java.util.Objects;
022import org.apache.hadoop.fs.FileStatus;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.quotas.SpaceLimitingException;
027import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
028import org.apache.hadoop.hbase.quotas.SpaceViolationPolicyEnforcement;
029import org.apache.hadoop.hbase.regionserver.RegionServerServices;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.yetus.audience.InterfaceStability;
032
033/**
034 * Abstract implementation for {@link SpaceViolationPolicyEnforcement}.
035 */
036@InterfaceAudience.Private
037@InterfaceStability.Evolving
038public abstract class AbstractViolationPolicyEnforcement
039  implements SpaceViolationPolicyEnforcement {
040
041  RegionServerServices rss;
042  TableName tableName;
043  SpaceQuotaSnapshot quotaSnapshot;
044
045  public void setRegionServerServices(RegionServerServices rss) {
046    this.rss = Objects.requireNonNull(rss);
047  }
048
049  public void setTableName(TableName tableName) {
050    this.tableName = tableName;
051  }
052
053  public RegionServerServices getRegionServerServices() {
054    return this.rss;
055  }
056
057  public TableName getTableName() {
058    return this.tableName;
059  }
060
061  public void setQuotaSnapshot(SpaceQuotaSnapshot snapshot) {
062    this.quotaSnapshot = Objects.requireNonNull(snapshot);
063  }
064
065  @Override
066  public SpaceQuotaSnapshot getQuotaSnapshot() {
067    return this.quotaSnapshot;
068  }
069
070  @Override
071  public void initialize(RegionServerServices rss, TableName tableName,
072    SpaceQuotaSnapshot snapshot) {
073    setRegionServerServices(rss);
074    setTableName(tableName);
075    setQuotaSnapshot(snapshot);
076  }
077
078  @Override
079  public boolean areCompactionsDisabled() {
080    return false;
081  }
082
083  /**
084   * Computes the size of a single file on the filesystem. If the size cannot be computed for some
085   * reason, a {@link SpaceLimitingException} is thrown, as the file may violate a quota. If the
086   * provided path does not reference a file, an {@link IllegalArgumentException} is thrown.
087   * @param fs   The FileSystem which the path refers to a file upon
088   * @param path The path on the {@code fs} to a file whose size is being checked
089   * @return The size in bytes of the file
090   */
091  long getFileSize(FileSystem fs, String path) throws SpaceLimitingException {
092    final FileStatus status;
093    try {
094      status = fs.getFileStatus(new Path(Objects.requireNonNull(path)));
095    } catch (IOException e) {
096      throw new SpaceLimitingException(getPolicyName(),
097        "Could not verify length of file to bulk load: " + path, e);
098    }
099    if (!status.isFile()) {
100      throw new IllegalArgumentException(path + " is not a file.");
101    }
102    return status.getLen();
103  }
104}