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.List;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.hbase.client.Mutation;
024import org.apache.hadoop.hbase.quotas.SpaceLimitingException;
025import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
026import org.apache.hadoop.hbase.quotas.SpaceViolationPolicyEnforcement;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * The default implementation for {@link SpaceViolationPolicyEnforcement}. This is done because all
031 * tables, whether or not they're in violation now, should be checking bulk loads to proactively
032 * catch a swell of files that would push the table into violation.
033 */
034@InterfaceAudience.Private
035public class DefaultViolationPolicyEnforcement extends AbstractViolationPolicyEnforcement {
036
037  @Override
038  public void enable() throws IOException {
039  }
040
041  @Override
042  public void disable() throws IOException {
043  }
044
045  @Override
046  public String getPolicyName() {
047    return "BulkLoadVerifying";
048  }
049
050  @Override
051  public void check(Mutation m) throws SpaceLimitingException {
052  }
053
054  @Override
055  public boolean shouldCheckBulkLoads() {
056    // Reference check. The singleton is used when no quota exists to check against
057    return SpaceQuotaSnapshot.getNoSuchSnapshot() != quotaSnapshot;
058  }
059
060  @Override
061  public long computeBulkLoadSize(FileSystem fs, List<String> paths) throws SpaceLimitingException {
062    // Compute the amount of space that could be used to save some arithmetic in the for-loop
063    final long sizeAvailableForBulkLoads = quotaSnapshot.getLimit() - quotaSnapshot.getUsage();
064    long size = 0L;
065    for (String path : paths) {
066      try {
067        size += getFileSize(fs, path);
068      } catch (IOException e) {
069        throw new SpaceLimitingException(getPolicyName(),
070          "Colud not verify length of file to bulk load: " + path, e);
071      }
072      if (size > sizeAvailableForBulkLoads) {
073        throw new SpaceLimitingException(getPolicyName(), "Bulk load of " + paths
074          + " is disallowed because the file(s) exceed the limits of a space quota.");
075      }
076    }
077    return size;
078  }
079}