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.hdfs.server.namenode; 019 020import org.apache.hadoop.hdfs.protocol.DSQuotaExceededException; 021import org.apache.hadoop.hdfs.protocol.HdfsConstants; 022import org.apache.hadoop.hdfs.protocol.NSQuotaExceededException; 023import org.apache.hadoop.hdfs.protocol.QuotaExceededException; 024import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; 025 026/** 027 * Quota feature for {@link INodeDirectory}. 028 */ 029public final class DirectoryWithQuotaFeature implements INode.Feature { 030 public static final long DEFAULT_NAMESPACE_QUOTA = Long.MAX_VALUE; 031 public static final long DEFAULT_DISKSPACE_QUOTA = HdfsConstants.QUOTA_RESET; 032 033 /** Name space quota */ 034 private long nsQuota = DEFAULT_NAMESPACE_QUOTA; 035 /** Name space count */ 036 private long namespace = 1L; 037 /** Disk space quota */ 038 private long dsQuota = DEFAULT_DISKSPACE_QUOTA; 039 /** Disk space count */ 040 private long diskspace = 0L; 041 042 DirectoryWithQuotaFeature(long nsQuota, long dsQuota) { 043 this.nsQuota = nsQuota; 044 this.dsQuota = dsQuota; 045 } 046 047 /** @return the quota set or -1 if it is not set. */ 048 Quota.Counts getQuota() { 049 return Quota.Counts.newInstance(nsQuota, dsQuota); 050 } 051 052 /** Set this directory's quota 053 * 054 * @param nsQuota Namespace quota to be set 055 * @param dsQuota Diskspace quota to be set 056 */ 057 void setQuota(long nsQuota, long dsQuota) { 058 this.nsQuota = nsQuota; 059 this.dsQuota = dsQuota; 060 } 061 062 Quota.Counts addNamespaceDiskspace(Quota.Counts counts) { 063 counts.add(Quota.NAMESPACE, namespace); 064 counts.add(Quota.DISKSPACE, diskspace); 065 return counts; 066 } 067 068 ContentSummaryComputationContext computeContentSummary(final INodeDirectory dir, 069 final ContentSummaryComputationContext summary) { 070 final long original = summary.getCounts().get(Content.DISKSPACE); 071 long oldYieldCount = summary.getYieldCount(); 072 dir.computeDirectoryContentSummary(summary, Snapshot.CURRENT_STATE_ID); 073 // Check only when the content has not changed in the middle. 074 if (oldYieldCount == summary.getYieldCount()) { 075 checkDiskspace(dir, summary.getCounts().get(Content.DISKSPACE) - original); 076 } 077 return summary; 078 } 079 080 private void checkDiskspace(final INodeDirectory dir, final long computed) { 081 if (-1 != getQuota().get(Quota.DISKSPACE) && diskspace != computed) { 082 NameNode.LOG.error("BUG: Inconsistent diskspace for directory " 083 + dir.getFullPathName() + ". Cached = " + diskspace 084 + " != Computed = " + computed); 085 } 086 } 087 088 void addSpaceConsumed(final INodeDirectory dir, final long nsDelta, 089 final long dsDelta, boolean verify) throws QuotaExceededException { 090 if (dir.isQuotaSet()) { 091 // The following steps are important: 092 // check quotas in this inode and all ancestors before changing counts 093 // so that no change is made if there is any quota violation. 094 095 // (1) verify quota in this inode 096 if (verify) { 097 verifyQuota(nsDelta, dsDelta); 098 } 099 // (2) verify quota and then add count in ancestors 100 dir.addSpaceConsumed2Parent(nsDelta, dsDelta, verify); 101 // (3) add count in this inode 102 addSpaceConsumed2Cache(nsDelta, dsDelta); 103 } else { 104 dir.addSpaceConsumed2Parent(nsDelta, dsDelta, verify); 105 } 106 } 107 108 /** Update the size of the tree 109 * 110 * @param nsDelta the change of the tree size 111 * @param dsDelta change to disk space occupied 112 */ 113 public void addSpaceConsumed2Cache(long nsDelta, long dsDelta) { 114 namespace += nsDelta; 115 diskspace += dsDelta; 116 } 117 118 /** 119 * Sets namespace and diskspace take by the directory rooted 120 * at this INode. This should be used carefully. It does not check 121 * for quota violations. 122 * 123 * @param namespace size of the directory to be set 124 * @param diskspace disk space take by all the nodes under this directory 125 */ 126 void setSpaceConsumed(long namespace, long diskspace) { 127 this.namespace = namespace; 128 this.diskspace = diskspace; 129 } 130 131 /** @return the namespace and diskspace consumed. */ 132 public Quota.Counts getSpaceConsumed() { 133 return Quota.Counts.newInstance(namespace, diskspace); 134 } 135 136 public long getDiskSpaceConsumed() { 137 return diskspace; 138 } 139 140 /** Verify if the namespace quota is violated after applying delta. */ 141 private void verifyNamespaceQuota(long delta) throws NSQuotaExceededException { 142 if (Quota.isViolated(nsQuota, namespace, delta)) { 143 throw new NSQuotaExceededException(nsQuota, namespace + delta); 144 } 145 } 146 /** Verify if the diskspace quota is violated after applying delta. */ 147 private void verifyDiskspaceQuota(long delta) throws DSQuotaExceededException { 148 if (Quota.isViolated(dsQuota, diskspace, delta)) { 149 throw new DSQuotaExceededException(dsQuota, diskspace + delta); 150 } 151 } 152 153 /** 154 * @throws QuotaExceededException if namespace or diskspace quotas is 155 * violated after applying the deltas. 156 */ 157 void verifyQuota(long nsDelta, long dsDelta) throws QuotaExceededException { 158 verifyNamespaceQuota(nsDelta); 159 verifyDiskspaceQuota(dsDelta); 160 } 161 162 boolean isQuotaSet() { 163 return nsQuota >= 0 || dsQuota >= 0; 164 } 165 166 private String namespaceString() { 167 return "namespace: " + (nsQuota < 0? "-": namespace + "/" + nsQuota); 168 } 169 private String diskspaceString() { 170 return "diskspace: " + (dsQuota < 0? "-": diskspace + "/" + dsQuota); 171 } 172 173 @Override 174 public String toString() { 175 return "Quota[" + namespaceString() + ", " + diskspaceString() + "]"; 176 } 177}