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.policies;
018
019import java.io.IOException;
020import java.util.Objects;
021
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(
072      RegionServerServices rss, TableName tableName, 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   *
088   * @param fs The FileSystem which the path refers to a file upon
089   * @param path The path on the {@code fs} to a file whose size is being checked
090   * @return The size in bytes of the file
091   */
092  long getFileSize(FileSystem fs, String path) throws SpaceLimitingException {
093    final FileStatus status;
094    try {
095      status = fs.getFileStatus(new Path(Objects.requireNonNull(path)));
096    } catch (IOException e) {
097      throw new SpaceLimitingException(
098          getPolicyName(), "Could not verify length of file to bulk load: " + path, e);
099    }
100    if (!status.isFile()) {
101      throw new IllegalArgumentException(path + " is not a file.");
102    }
103    return status.getLen();
104  }
105}