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.List;
021
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.client.Mutation;
025import org.apache.hadoop.hbase.quotas.SpaceLimitingException;
026import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
027import org.apache.hadoop.hbase.quotas.SpaceViolationPolicyEnforcement;
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  @Override
041  public void disable() throws IOException {}
042
043  @Override
044  public String getPolicyName() {
045    return "BulkLoadVerifying";
046  }
047
048  @Override
049  public void check(Mutation m) throws SpaceLimitingException {}
050
051  @Override
052  public boolean shouldCheckBulkLoads() {
053    // Reference check. The singleton is used when no quota exists to check against
054    return SpaceQuotaSnapshot.getNoSuchSnapshot() != quotaSnapshot;
055  }
056
057  @Override
058  public long computeBulkLoadSize(FileSystem fs, List<String> paths) throws SpaceLimitingException {
059    // Compute the amount of space that could be used to save some arithmetic in the for-loop
060    final long sizeAvailableForBulkLoads = quotaSnapshot.getLimit() - quotaSnapshot.getUsage();
061    long size = 0L;
062    for (String path : paths) {
063      try {
064        size += getFileSize(fs, path);
065      } catch (IOException e) {
066        throw new SpaceLimitingException(
067            getPolicyName(), "Colud not verify length of file to bulk load: " + path, e);
068      }
069      if (size > sizeAvailableForBulkLoads) {
070        throw new SpaceLimitingException(getPolicyName(), "Bulk load of " + paths
071            + " is disallowed because the file(s) exceed the limits of a space quota.");
072      }
073    }
074    return size;
075  }
076}