[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.hdfs; |
---|
| 19 | |
---|
| 20 | import java.io.OutputStream; |
---|
| 21 | |
---|
| 22 | import org.apache.hadoop.conf.Configuration; |
---|
| 23 | import org.apache.hadoop.fs.ContentSummary; |
---|
| 24 | import org.apache.hadoop.fs.FileSystem; |
---|
| 25 | import org.apache.hadoop.fs.Path; |
---|
| 26 | import org.apache.hadoop.hdfs.protocol.FSConstants; |
---|
| 27 | import org.apache.hadoop.hdfs.protocol.QuotaExceededException; |
---|
| 28 | import org.apache.hadoop.hdfs.tools.DFSAdmin; |
---|
| 29 | import org.apache.hadoop.io.IOUtils; |
---|
| 30 | import org.apache.hadoop.security.UnixUserGroupInformation; |
---|
| 31 | import org.apache.hadoop.hdfs.protocol.NSQuotaExceededException; |
---|
| 32 | import org.apache.hadoop.hdfs.protocol.DSQuotaExceededException; |
---|
| 33 | |
---|
| 34 | import junit.framework.TestCase; |
---|
| 35 | |
---|
| 36 | /** A class for testing quota-related commands */ |
---|
| 37 | public class TestQuota extends TestCase { |
---|
| 38 | |
---|
| 39 | private void runCommand(DFSAdmin admin, boolean expectError, String... args) |
---|
| 40 | throws Exception { |
---|
| 41 | runCommand(admin, args, expectError); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | private void runCommand(DFSAdmin admin, String args[], boolean expectEror) |
---|
| 45 | throws Exception { |
---|
| 46 | int val = admin.run(args); |
---|
| 47 | if (expectEror) { |
---|
| 48 | assertEquals(val, -1); |
---|
| 49 | } else { |
---|
| 50 | assertTrue(val>=0); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** Test quota related commands: |
---|
| 55 | * setQuota, clrQuota, setSpaceQuota, clrSpaceQuota, and count |
---|
| 56 | */ |
---|
| 57 | public void testQuotaCommands() throws Exception { |
---|
| 58 | final Configuration conf = new Configuration(); |
---|
| 59 | // set a smaller block size so that we can test with smaller |
---|
| 60 | // Space quotas |
---|
| 61 | conf.set("dfs.block.size", "512"); |
---|
| 62 | conf.setBoolean("dfs.support.append", true); |
---|
| 63 | final MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
| 64 | final FileSystem fs = cluster.getFileSystem(); |
---|
| 65 | assertTrue("Not a HDFS: "+fs.getUri(), |
---|
| 66 | fs instanceof DistributedFileSystem); |
---|
| 67 | final DistributedFileSystem dfs = (DistributedFileSystem)fs; |
---|
| 68 | DFSAdmin admin = new DFSAdmin(conf); |
---|
| 69 | |
---|
| 70 | try { |
---|
| 71 | final int fileLen = 1024; |
---|
| 72 | final short replication = 5; |
---|
| 73 | final long spaceQuota = fileLen * replication * 15 / 8; |
---|
| 74 | |
---|
| 75 | // 1: create a directory /test and set its quota to be 3 |
---|
| 76 | final Path parent = new Path("/test"); |
---|
| 77 | assertTrue(dfs.mkdirs(parent)); |
---|
| 78 | String[] args = new String[]{"-setQuota", "3", parent.toString()}; |
---|
| 79 | runCommand(admin, args, false); |
---|
| 80 | |
---|
| 81 | //try setting space quota with a 'binary prefix' |
---|
| 82 | runCommand(admin, false, "-setSpaceQuota", "2t", parent.toString()); |
---|
| 83 | assertEquals(2L<<40, dfs.getContentSummary(parent).getSpaceQuota()); |
---|
| 84 | |
---|
| 85 | // set diskspace quota to 10000 |
---|
| 86 | runCommand(admin, false, "-setSpaceQuota", |
---|
| 87 | Long.toString(spaceQuota), parent.toString()); |
---|
| 88 | |
---|
| 89 | // 2: create directory /test/data0 |
---|
| 90 | final Path childDir0 = new Path(parent, "data0"); |
---|
| 91 | assertTrue(dfs.mkdirs(childDir0)); |
---|
| 92 | |
---|
| 93 | // 3: create a file /test/datafile0 |
---|
| 94 | final Path childFile0 = new Path(parent, "datafile0"); |
---|
| 95 | DFSTestUtil.createFile(fs, childFile0, fileLen, replication, 0); |
---|
| 96 | |
---|
| 97 | // 4: count -q /test |
---|
| 98 | ContentSummary c = dfs.getContentSummary(parent); |
---|
| 99 | assertEquals(c.getFileCount()+c.getDirectoryCount(), 3); |
---|
| 100 | assertEquals(c.getQuota(), 3); |
---|
| 101 | assertEquals(c.getSpaceConsumed(), fileLen*replication); |
---|
| 102 | assertEquals(c.getSpaceQuota(), spaceQuota); |
---|
| 103 | |
---|
| 104 | // 5: count -q /test/data0 |
---|
| 105 | c = dfs.getContentSummary(childDir0); |
---|
| 106 | assertEquals(c.getFileCount()+c.getDirectoryCount(), 1); |
---|
| 107 | assertEquals(c.getQuota(), -1); |
---|
| 108 | // check disk space consumed |
---|
| 109 | c = dfs.getContentSummary(parent); |
---|
| 110 | assertEquals(c.getSpaceConsumed(), fileLen*replication); |
---|
| 111 | |
---|
| 112 | // 6: create a directory /test/data1 |
---|
| 113 | final Path childDir1 = new Path(parent, "data1"); |
---|
| 114 | boolean hasException = false; |
---|
| 115 | try { |
---|
| 116 | assertFalse(dfs.mkdirs(childDir1)); |
---|
| 117 | } catch (NSQuotaExceededException e) { |
---|
| 118 | hasException = true; |
---|
| 119 | } |
---|
| 120 | assertTrue(hasException); |
---|
| 121 | |
---|
| 122 | OutputStream fout; |
---|
| 123 | |
---|
| 124 | // 7: create a file /test/datafile1 |
---|
| 125 | final Path childFile1 = new Path(parent, "datafile1"); |
---|
| 126 | hasException = false; |
---|
| 127 | try { |
---|
| 128 | fout = dfs.create(childFile1); |
---|
| 129 | } catch (NSQuotaExceededException e) { |
---|
| 130 | hasException = true; |
---|
| 131 | } |
---|
| 132 | assertTrue(hasException); |
---|
| 133 | |
---|
| 134 | // 8: clear quota /test |
---|
| 135 | runCommand(admin, new String[]{"-clrQuota", parent.toString()}, false); |
---|
| 136 | c = dfs.getContentSummary(parent); |
---|
| 137 | assertEquals(c.getQuota(), -1); |
---|
| 138 | assertEquals(c.getSpaceQuota(), spaceQuota); |
---|
| 139 | |
---|
| 140 | // 9: clear quota /test/data0 |
---|
| 141 | runCommand(admin, new String[]{"-clrQuota", childDir0.toString()}, false); |
---|
| 142 | c = dfs.getContentSummary(childDir0); |
---|
| 143 | assertEquals(c.getQuota(), -1); |
---|
| 144 | |
---|
| 145 | // 10: create a file /test/datafile1 |
---|
| 146 | fout = dfs.create(childFile1, replication); |
---|
| 147 | |
---|
| 148 | // 10.s: but writing fileLen bytes should result in an quota exception |
---|
| 149 | hasException = false; |
---|
| 150 | try { |
---|
| 151 | fout.write(new byte[fileLen]); |
---|
| 152 | fout.close(); |
---|
| 153 | } catch (DSQuotaExceededException e) { |
---|
| 154 | hasException = true; |
---|
| 155 | IOUtils.closeStream(fout); |
---|
| 156 | } |
---|
| 157 | assertTrue(hasException); |
---|
| 158 | |
---|
| 159 | //delete the file |
---|
| 160 | dfs.delete(childFile1, false); |
---|
| 161 | |
---|
| 162 | // 9.s: clear diskspace quota |
---|
| 163 | runCommand(admin, false, "-clrSpaceQuota", parent.toString()); |
---|
| 164 | c = dfs.getContentSummary(parent); |
---|
| 165 | assertEquals(c.getQuota(), -1); |
---|
| 166 | assertEquals(c.getSpaceQuota(), -1); |
---|
| 167 | |
---|
| 168 | // now creating childFile1 should succeed |
---|
| 169 | DFSTestUtil.createFile(dfs, childFile1, fileLen, replication, 0); |
---|
| 170 | |
---|
| 171 | // 11: set the quota of /test to be 1 |
---|
| 172 | // HADOOP-5872 - we can set quota even if it is immediately violated |
---|
| 173 | args = new String[]{"-setQuota", "1", parent.toString()}; |
---|
| 174 | runCommand(admin, args, false); |
---|
| 175 | runCommand(admin, false, "-setSpaceQuota", // for space quota |
---|
| 176 | Integer.toString(fileLen), args[2]); |
---|
| 177 | |
---|
| 178 | // 12: set the quota of /test/data0 to be 1 |
---|
| 179 | args = new String[]{"-setQuota", "1", childDir0.toString()}; |
---|
| 180 | runCommand(admin, args, false); |
---|
| 181 | |
---|
| 182 | // 13: not able create a directory under data0 |
---|
| 183 | hasException = false; |
---|
| 184 | try { |
---|
| 185 | assertFalse(dfs.mkdirs(new Path(childDir0, "in"))); |
---|
| 186 | } catch (NSQuotaExceededException e) { |
---|
| 187 | hasException = true; |
---|
| 188 | } |
---|
| 189 | assertTrue(hasException); |
---|
| 190 | c = dfs.getContentSummary(childDir0); |
---|
| 191 | assertEquals(c.getDirectoryCount()+c.getFileCount(), 1); |
---|
| 192 | assertEquals(c.getQuota(), 1); |
---|
| 193 | |
---|
| 194 | // 14a: set quota on a non-existent directory |
---|
| 195 | Path nonExistentPath = new Path("/test1"); |
---|
| 196 | assertFalse(dfs.exists(nonExistentPath)); |
---|
| 197 | args = new String[]{"-setQuota", "1", nonExistentPath.toString()}; |
---|
| 198 | runCommand(admin, args, true); |
---|
| 199 | runCommand(admin, true, "-setSpaceQuota", "1g", // for space quota |
---|
| 200 | nonExistentPath.toString()); |
---|
| 201 | |
---|
| 202 | // 14b: set quota on a file |
---|
| 203 | assertTrue(dfs.isFile(childFile0)); |
---|
| 204 | args[1] = childFile0.toString(); |
---|
| 205 | runCommand(admin, args, true); |
---|
| 206 | // same for space quota |
---|
| 207 | runCommand(admin, true, "-setSpaceQuota", "1t", args[1]); |
---|
| 208 | |
---|
| 209 | // 15a: clear quota on a file |
---|
| 210 | args[0] = "-clrQuota"; |
---|
| 211 | runCommand(admin, args, true); |
---|
| 212 | runCommand(admin, true, "-clrSpaceQuota", args[1]); |
---|
| 213 | |
---|
| 214 | // 15b: clear quota on a non-existent directory |
---|
| 215 | args[1] = nonExistentPath.toString(); |
---|
| 216 | runCommand(admin, args, true); |
---|
| 217 | runCommand(admin, true, "-clrSpaceQuota", args[1]); |
---|
| 218 | |
---|
| 219 | // 16a: set the quota of /test to be 0 |
---|
| 220 | args = new String[]{"-setQuota", "0", parent.toString()}; |
---|
| 221 | runCommand(admin, args, true); |
---|
| 222 | runCommand(admin, true, "-setSpaceQuota", "0", args[2]); |
---|
| 223 | |
---|
| 224 | // 16b: set the quota of /test to be -1 |
---|
| 225 | args[1] = "-1"; |
---|
| 226 | runCommand(admin, args, true); |
---|
| 227 | runCommand(admin, true, "-setSpaceQuota", args[1], args[2]); |
---|
| 228 | |
---|
| 229 | // 16c: set the quota of /test to be Long.MAX_VALUE+1 |
---|
| 230 | args[1] = String.valueOf(Long.MAX_VALUE+1L); |
---|
| 231 | runCommand(admin, args, true); |
---|
| 232 | runCommand(admin, true, "-setSpaceQuota", args[1], args[2]); |
---|
| 233 | |
---|
| 234 | // 16d: set the quota of /test to be a non integer |
---|
| 235 | args[1] = "33aa1.5"; |
---|
| 236 | runCommand(admin, args, true); |
---|
| 237 | runCommand(admin, true, "-setSpaceQuota", args[1], args[2]); |
---|
| 238 | |
---|
| 239 | // 16e: set space quota with a value larger than Long.MAX_VALUE |
---|
| 240 | runCommand(admin, true, "-setSpaceQuota", |
---|
| 241 | (Long.MAX_VALUE/1024/1024 + 1024) + "m", args[2]); |
---|
| 242 | |
---|
| 243 | // 17: setQuota by a non-administrator |
---|
| 244 | UnixUserGroupInformation.saveToConf(conf, |
---|
| 245 | UnixUserGroupInformation.UGI_PROPERTY_NAME, |
---|
| 246 | new UnixUserGroupInformation(new String[]{"userxx\n", "groupyy\n"})); |
---|
| 247 | DFSAdmin userAdmin = new DFSAdmin(conf); |
---|
| 248 | args[1] = "100"; |
---|
| 249 | runCommand(userAdmin, args, true); |
---|
| 250 | runCommand(userAdmin, true, "-setSpaceQuota", "1g", args[2]); |
---|
| 251 | |
---|
| 252 | // 18: clrQuota by a non-administrator |
---|
| 253 | args = new String[] {"-clrQuota", parent.toString()}; |
---|
| 254 | runCommand(userAdmin, args, true); |
---|
| 255 | runCommand(userAdmin, true, "-clrSpaceQuota", args[1]); |
---|
| 256 | } finally { |
---|
| 257 | cluster.shutdown(); |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** Test commands that change the size of the name space: |
---|
| 262 | * mkdirs, rename, and delete */ |
---|
| 263 | public void testNamespaceCommands() throws Exception { |
---|
| 264 | final Configuration conf = new Configuration(); |
---|
| 265 | final MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
| 266 | final FileSystem fs = cluster.getFileSystem(); |
---|
| 267 | assertTrue("Not a HDFS: "+fs.getUri(), |
---|
| 268 | fs instanceof DistributedFileSystem); |
---|
| 269 | final DistributedFileSystem dfs = (DistributedFileSystem)fs; |
---|
| 270 | |
---|
| 271 | try { |
---|
| 272 | // 1: create directory /nqdir0/qdir1/qdir20/nqdir30 |
---|
| 273 | assertTrue(dfs.mkdirs(new Path("/nqdir0/qdir1/qdir20/nqdir30"))); |
---|
| 274 | |
---|
| 275 | // 2: set the quota of /nqdir0/qdir1 to be 6 |
---|
| 276 | final Path quotaDir1 = new Path("/nqdir0/qdir1"); |
---|
| 277 | dfs.setQuota(quotaDir1, 6, FSConstants.QUOTA_DONT_SET); |
---|
| 278 | ContentSummary c = dfs.getContentSummary(quotaDir1); |
---|
| 279 | assertEquals(c.getDirectoryCount(), 3); |
---|
| 280 | assertEquals(c.getQuota(), 6); |
---|
| 281 | |
---|
| 282 | // 3: set the quota of /nqdir0/qdir1/qdir20 to be 7 |
---|
| 283 | final Path quotaDir2 = new Path("/nqdir0/qdir1/qdir20"); |
---|
| 284 | dfs.setQuota(quotaDir2, 7, FSConstants.QUOTA_DONT_SET); |
---|
| 285 | c = dfs.getContentSummary(quotaDir2); |
---|
| 286 | assertEquals(c.getDirectoryCount(), 2); |
---|
| 287 | assertEquals(c.getQuota(), 7); |
---|
| 288 | |
---|
| 289 | // 4: Create directory /nqdir0/qdir1/qdir21 and set its quota to 2 |
---|
| 290 | final Path quotaDir3 = new Path("/nqdir0/qdir1/qdir21"); |
---|
| 291 | assertTrue(dfs.mkdirs(quotaDir3)); |
---|
| 292 | dfs.setQuota(quotaDir3, 2, FSConstants.QUOTA_DONT_SET); |
---|
| 293 | c = dfs.getContentSummary(quotaDir3); |
---|
| 294 | assertEquals(c.getDirectoryCount(), 1); |
---|
| 295 | assertEquals(c.getQuota(), 2); |
---|
| 296 | |
---|
| 297 | // 5: Create directory /nqdir0/qdir1/qdir21/nqdir32 |
---|
| 298 | Path tempPath = new Path(quotaDir3, "nqdir32"); |
---|
| 299 | assertTrue(dfs.mkdirs(tempPath)); |
---|
| 300 | c = dfs.getContentSummary(quotaDir3); |
---|
| 301 | assertEquals(c.getDirectoryCount(), 2); |
---|
| 302 | assertEquals(c.getQuota(), 2); |
---|
| 303 | |
---|
| 304 | // 6: Create directory /nqdir0/qdir1/qdir21/nqdir33 |
---|
| 305 | tempPath = new Path(quotaDir3, "nqdir33"); |
---|
| 306 | boolean hasException = false; |
---|
| 307 | try { |
---|
| 308 | assertFalse(dfs.mkdirs(tempPath)); |
---|
| 309 | } catch (NSQuotaExceededException e) { |
---|
| 310 | hasException = true; |
---|
| 311 | } |
---|
| 312 | assertTrue(hasException); |
---|
| 313 | c = dfs.getContentSummary(quotaDir3); |
---|
| 314 | assertEquals(c.getDirectoryCount(), 2); |
---|
| 315 | assertEquals(c.getQuota(), 2); |
---|
| 316 | |
---|
| 317 | // 7: Create directory /nqdir0/qdir1/qdir20/nqdir31 |
---|
| 318 | tempPath = new Path(quotaDir2, "nqdir31"); |
---|
| 319 | assertTrue(dfs.mkdirs(tempPath)); |
---|
| 320 | c = dfs.getContentSummary(quotaDir2); |
---|
| 321 | assertEquals(c.getDirectoryCount(), 3); |
---|
| 322 | assertEquals(c.getQuota(), 7); |
---|
| 323 | c = dfs.getContentSummary(quotaDir1); |
---|
| 324 | assertEquals(c.getDirectoryCount(), 6); |
---|
| 325 | assertEquals(c.getQuota(), 6); |
---|
| 326 | |
---|
| 327 | // 8: Create directory /nqdir0/qdir1/qdir20/nqdir33 |
---|
| 328 | tempPath = new Path(quotaDir2, "nqdir33"); |
---|
| 329 | hasException = false; |
---|
| 330 | try { |
---|
| 331 | assertFalse(dfs.mkdirs(tempPath)); |
---|
| 332 | } catch (NSQuotaExceededException e) { |
---|
| 333 | hasException = true; |
---|
| 334 | } |
---|
| 335 | assertTrue(hasException); |
---|
| 336 | |
---|
| 337 | // 9: Move /nqdir0/qdir1/qdir21/nqdir32 /nqdir0/qdir1/qdir20/nqdir30 |
---|
| 338 | tempPath = new Path(quotaDir2, "nqdir30"); |
---|
| 339 | dfs.rename(new Path(quotaDir3, "nqdir32"), tempPath); |
---|
| 340 | c = dfs.getContentSummary(quotaDir2); |
---|
| 341 | assertEquals(c.getDirectoryCount(), 4); |
---|
| 342 | assertEquals(c.getQuota(), 7); |
---|
| 343 | c = dfs.getContentSummary(quotaDir1); |
---|
| 344 | assertEquals(c.getDirectoryCount(), 6); |
---|
| 345 | assertEquals(c.getQuota(), 6); |
---|
| 346 | |
---|
| 347 | // 10: Move /nqdir0/qdir1/qdir20/nqdir30 to /nqdir0/qdir1/qdir21 |
---|
| 348 | hasException = false; |
---|
| 349 | try { |
---|
| 350 | assertFalse(dfs.rename(tempPath, quotaDir3)); |
---|
| 351 | } catch (NSQuotaExceededException e) { |
---|
| 352 | hasException = true; |
---|
| 353 | } |
---|
| 354 | assertTrue(hasException); |
---|
| 355 | assertTrue(dfs.exists(tempPath)); |
---|
| 356 | assertFalse(dfs.exists(new Path(quotaDir3, "nqdir30"))); |
---|
| 357 | |
---|
| 358 | // 10.a: Rename /nqdir0/qdir1/qdir20/nqdir30 to /nqdir0/qdir1/qdir21/nqdir32 |
---|
| 359 | hasException = false; |
---|
| 360 | try { |
---|
| 361 | assertFalse(dfs.rename(tempPath, new Path(quotaDir3, "nqdir32"))); |
---|
| 362 | } catch (NSQuotaExceededException e) { |
---|
| 363 | hasException = true; |
---|
| 364 | } |
---|
| 365 | assertTrue(hasException); |
---|
| 366 | assertTrue(dfs.exists(tempPath)); |
---|
| 367 | assertFalse(dfs.exists(new Path(quotaDir3, "nqdir32"))); |
---|
| 368 | |
---|
| 369 | // 11: Move /nqdir0/qdir1/qdir20/nqdir30 to /nqdir0 |
---|
| 370 | assertTrue(dfs.rename(tempPath, new Path("/nqdir0"))); |
---|
| 371 | c = dfs.getContentSummary(quotaDir2); |
---|
| 372 | assertEquals(c.getDirectoryCount(), 2); |
---|
| 373 | assertEquals(c.getQuota(), 7); |
---|
| 374 | c = dfs.getContentSummary(quotaDir1); |
---|
| 375 | assertEquals(c.getDirectoryCount(), 4); |
---|
| 376 | assertEquals(c.getQuota(), 6); |
---|
| 377 | |
---|
| 378 | // 12: Create directory /nqdir0/nqdir30/nqdir33 |
---|
| 379 | assertTrue(dfs.mkdirs(new Path("/nqdir0/nqdir30/nqdir33"))); |
---|
| 380 | |
---|
| 381 | // 13: Move /nqdir0/nqdir30 /nqdir0/qdir1/qdir20/qdir30 |
---|
| 382 | hasException = false; |
---|
| 383 | try { |
---|
| 384 | assertFalse(dfs.rename(new Path("/nqdir0/nqdir30"), tempPath)); |
---|
| 385 | } catch (NSQuotaExceededException e) { |
---|
| 386 | hasException = true; |
---|
| 387 | } |
---|
| 388 | assertTrue(hasException); |
---|
| 389 | |
---|
| 390 | // 14: Move /nqdir0/qdir1/qdir21 /nqdir0/qdir1/qdir20 |
---|
| 391 | assertTrue(dfs.rename(quotaDir3, quotaDir2)); |
---|
| 392 | c = dfs.getContentSummary(quotaDir1); |
---|
| 393 | assertEquals(c.getDirectoryCount(), 4); |
---|
| 394 | assertEquals(c.getQuota(), 6); |
---|
| 395 | c = dfs.getContentSummary(quotaDir2); |
---|
| 396 | assertEquals(c.getDirectoryCount(), 3); |
---|
| 397 | assertEquals(c.getQuota(), 7); |
---|
| 398 | tempPath = new Path(quotaDir2, "qdir21"); |
---|
| 399 | c = dfs.getContentSummary(tempPath); |
---|
| 400 | assertEquals(c.getDirectoryCount(), 1); |
---|
| 401 | assertEquals(c.getQuota(), 2); |
---|
| 402 | |
---|
| 403 | // 15: Delete /nqdir0/qdir1/qdir20/qdir21 |
---|
| 404 | dfs.delete(tempPath, true); |
---|
| 405 | c = dfs.getContentSummary(quotaDir2); |
---|
| 406 | assertEquals(c.getDirectoryCount(), 2); |
---|
| 407 | assertEquals(c.getQuota(), 7); |
---|
| 408 | c = dfs.getContentSummary(quotaDir1); |
---|
| 409 | assertEquals(c.getDirectoryCount(), 3); |
---|
| 410 | assertEquals(c.getQuota(), 6); |
---|
| 411 | |
---|
| 412 | // 16: Move /nqdir0/qdir30 /nqdir0/qdir1/qdir20 |
---|
| 413 | assertTrue(dfs.rename(new Path("/nqdir0/nqdir30"), quotaDir2)); |
---|
| 414 | c = dfs.getContentSummary(quotaDir2); |
---|
| 415 | assertEquals(c.getDirectoryCount(), 5); |
---|
| 416 | assertEquals(c.getQuota(), 7); |
---|
| 417 | c = dfs.getContentSummary(quotaDir1); |
---|
| 418 | assertEquals(c.getDirectoryCount(), 6); |
---|
| 419 | assertEquals(c.getQuota(), 6); |
---|
| 420 | } finally { |
---|
| 421 | cluster.shutdown(); |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | /** |
---|
| 426 | * Test HDFS operations that change disk space consumed by a directory tree. |
---|
| 427 | * namely create, rename, delete, append, and setReplication. |
---|
| 428 | * |
---|
| 429 | * This is based on testNamespaceCommands() above. |
---|
| 430 | */ |
---|
| 431 | public void testSpaceCommands() throws Exception { |
---|
| 432 | final Configuration conf = new Configuration(); |
---|
| 433 | // set a smaller block size so that we can test with smaller |
---|
| 434 | // diskspace quotas |
---|
| 435 | conf.set("dfs.block.size", "512"); |
---|
| 436 | conf.setBoolean("dfs.support.append", true); |
---|
| 437 | final MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
| 438 | final FileSystem fs = cluster.getFileSystem(); |
---|
| 439 | assertTrue("Not a HDFS: "+fs.getUri(), |
---|
| 440 | fs instanceof DistributedFileSystem); |
---|
| 441 | final DistributedFileSystem dfs = (DistributedFileSystem)fs; |
---|
| 442 | |
---|
| 443 | try { |
---|
| 444 | int fileLen = 1024; |
---|
| 445 | short replication = 3; |
---|
| 446 | int fileSpace = fileLen * replication; |
---|
| 447 | |
---|
| 448 | // create directory /nqdir0/qdir1/qdir20/nqdir30 |
---|
| 449 | assertTrue(dfs.mkdirs(new Path("/nqdir0/qdir1/qdir20/nqdir30"))); |
---|
| 450 | |
---|
| 451 | // set the quota of /nqdir0/qdir1 to 4 * fileSpace |
---|
| 452 | final Path quotaDir1 = new Path("/nqdir0/qdir1"); |
---|
| 453 | dfs.setQuota(quotaDir1, FSConstants.QUOTA_DONT_SET, 4 * fileSpace); |
---|
| 454 | ContentSummary c = dfs.getContentSummary(quotaDir1); |
---|
| 455 | assertEquals(c.getSpaceQuota(), 4 * fileSpace); |
---|
| 456 | |
---|
| 457 | // set the quota of /nqdir0/qdir1/qdir20 to 6 * fileSpace |
---|
| 458 | final Path quotaDir20 = new Path("/nqdir0/qdir1/qdir20"); |
---|
| 459 | dfs.setQuota(quotaDir20, FSConstants.QUOTA_DONT_SET, 6 * fileSpace); |
---|
| 460 | c = dfs.getContentSummary(quotaDir20); |
---|
| 461 | assertEquals(c.getSpaceQuota(), 6 * fileSpace); |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | // Create /nqdir0/qdir1/qdir21 and set its space quota to 2 * fileSpace |
---|
| 465 | final Path quotaDir21 = new Path("/nqdir0/qdir1/qdir21"); |
---|
| 466 | assertTrue(dfs.mkdirs(quotaDir21)); |
---|
| 467 | dfs.setQuota(quotaDir21, FSConstants.QUOTA_DONT_SET, 2 * fileSpace); |
---|
| 468 | c = dfs.getContentSummary(quotaDir21); |
---|
| 469 | assertEquals(c.getSpaceQuota(), 2 * fileSpace); |
---|
| 470 | |
---|
| 471 | // 5: Create directory /nqdir0/qdir1/qdir21/nqdir32 |
---|
| 472 | Path tempPath = new Path(quotaDir21, "nqdir32"); |
---|
| 473 | assertTrue(dfs.mkdirs(tempPath)); |
---|
| 474 | |
---|
| 475 | // create a file under nqdir32/fileDir |
---|
| 476 | DFSTestUtil.createFile(dfs, new Path(tempPath, "fileDir/file1"), fileLen, |
---|
| 477 | replication, 0); |
---|
| 478 | c = dfs.getContentSummary(quotaDir21); |
---|
| 479 | assertEquals(c.getSpaceConsumed(), fileSpace); |
---|
| 480 | |
---|
| 481 | // Create a larger file /nqdir0/qdir1/qdir21/nqdir33/ |
---|
| 482 | boolean hasException = false; |
---|
| 483 | try { |
---|
| 484 | DFSTestUtil.createFile(dfs, new Path(quotaDir21, "nqdir33/file2"), |
---|
| 485 | 2*fileLen, replication, 0); |
---|
| 486 | } catch (DSQuotaExceededException e) { |
---|
| 487 | hasException = true; |
---|
| 488 | } |
---|
| 489 | assertTrue(hasException); |
---|
| 490 | // delete nqdir33 |
---|
| 491 | assertTrue(dfs.delete(new Path(quotaDir21, "nqdir33"), true)); |
---|
| 492 | c = dfs.getContentSummary(quotaDir21); |
---|
| 493 | assertEquals(c.getSpaceConsumed(), fileSpace); |
---|
| 494 | assertEquals(c.getSpaceQuota(), 2*fileSpace); |
---|
| 495 | |
---|
| 496 | // Verify space before the move: |
---|
| 497 | c = dfs.getContentSummary(quotaDir20); |
---|
| 498 | assertEquals(c.getSpaceConsumed(), 0); |
---|
| 499 | |
---|
| 500 | // Move /nqdir0/qdir1/qdir21/nqdir32 /nqdir0/qdir1/qdir20/nqdir30 |
---|
| 501 | Path dstPath = new Path(quotaDir20, "nqdir30"); |
---|
| 502 | Path srcPath = new Path(quotaDir21, "nqdir32"); |
---|
| 503 | assertTrue(dfs.rename(srcPath, dstPath)); |
---|
| 504 | |
---|
| 505 | // verify space after the move |
---|
| 506 | c = dfs.getContentSummary(quotaDir20); |
---|
| 507 | assertEquals(c.getSpaceConsumed(), fileSpace); |
---|
| 508 | // verify space for its parent |
---|
| 509 | c = dfs.getContentSummary(quotaDir1); |
---|
| 510 | assertEquals(c.getSpaceConsumed(), fileSpace); |
---|
| 511 | // verify space for source for the move |
---|
| 512 | c = dfs.getContentSummary(quotaDir21); |
---|
| 513 | assertEquals(c.getSpaceConsumed(), 0); |
---|
| 514 | |
---|
| 515 | final Path file2 = new Path(dstPath, "fileDir/file2"); |
---|
| 516 | int file2Len = 2 * fileLen; |
---|
| 517 | // create a larger file under /nqdir0/qdir1/qdir20/nqdir30 |
---|
| 518 | DFSTestUtil.createFile(dfs, file2, file2Len, replication, 0); |
---|
| 519 | |
---|
| 520 | c = dfs.getContentSummary(quotaDir20); |
---|
| 521 | assertEquals(c.getSpaceConsumed(), 3 * fileSpace); |
---|
| 522 | c = dfs.getContentSummary(quotaDir21); |
---|
| 523 | assertEquals(c.getSpaceConsumed(), 0); |
---|
| 524 | |
---|
| 525 | // Reverse: Move /nqdir0/qdir1/qdir20/nqdir30 to /nqdir0/qdir1/qdir21/ |
---|
| 526 | hasException = false; |
---|
| 527 | try { |
---|
| 528 | assertFalse(dfs.rename(dstPath, srcPath)); |
---|
| 529 | } catch (DSQuotaExceededException e) { |
---|
| 530 | hasException = true; |
---|
| 531 | } |
---|
| 532 | assertTrue(hasException); |
---|
| 533 | |
---|
| 534 | // make sure no intermediate directories left by failed rename |
---|
| 535 | assertFalse(dfs.exists(srcPath)); |
---|
| 536 | // directory should exist |
---|
| 537 | assertTrue(dfs.exists(dstPath)); |
---|
| 538 | |
---|
| 539 | // verify space after the failed move |
---|
| 540 | c = dfs.getContentSummary(quotaDir20); |
---|
| 541 | assertEquals(c.getSpaceConsumed(), 3 * fileSpace); |
---|
| 542 | c = dfs.getContentSummary(quotaDir21); |
---|
| 543 | assertEquals(c.getSpaceConsumed(), 0); |
---|
| 544 | |
---|
| 545 | // Test Append : |
---|
| 546 | |
---|
| 547 | // verify space quota |
---|
| 548 | c = dfs.getContentSummary(quotaDir1); |
---|
| 549 | assertEquals(c.getSpaceQuota(), 4 * fileSpace); |
---|
| 550 | |
---|
| 551 | // verify space before append; |
---|
| 552 | c = dfs.getContentSummary(dstPath); |
---|
| 553 | assertEquals(c.getSpaceConsumed(), 3 * fileSpace); |
---|
| 554 | |
---|
| 555 | OutputStream out = dfs.append(file2); |
---|
| 556 | // appending 1 fileLen should succeed |
---|
| 557 | out.write(new byte[fileLen]); |
---|
| 558 | out.close(); |
---|
| 559 | |
---|
| 560 | file2Len += fileLen; // after append |
---|
| 561 | |
---|
| 562 | // verify space after append; |
---|
| 563 | c = dfs.getContentSummary(dstPath); |
---|
| 564 | assertEquals(c.getSpaceConsumed(), 4 * fileSpace); |
---|
| 565 | |
---|
| 566 | // now increase the quota for quotaDir1 |
---|
| 567 | dfs.setQuota(quotaDir1, FSConstants.QUOTA_DONT_SET, 5 * fileSpace); |
---|
| 568 | // Now, appending more than 1 fileLen should result in an error |
---|
| 569 | out = dfs.append(file2); |
---|
| 570 | hasException = false; |
---|
| 571 | try { |
---|
| 572 | out.write(new byte[fileLen + 1024]); |
---|
| 573 | out.flush(); |
---|
| 574 | out.close(); |
---|
| 575 | } catch (DSQuotaExceededException e) { |
---|
| 576 | hasException = true; |
---|
| 577 | IOUtils.closeStream(out); |
---|
| 578 | } |
---|
| 579 | assertTrue(hasException); |
---|
| 580 | |
---|
| 581 | file2Len += fileLen; // after partial append |
---|
| 582 | |
---|
| 583 | // verify space after partial append |
---|
| 584 | c = dfs.getContentSummary(dstPath); |
---|
| 585 | assertEquals(c.getSpaceConsumed(), 5 * fileSpace); |
---|
| 586 | |
---|
| 587 | // Test set replication : |
---|
| 588 | |
---|
| 589 | // first reduce the replication |
---|
| 590 | dfs.setReplication(file2, (short)(replication-1)); |
---|
| 591 | |
---|
| 592 | // verify that space is reduced by file2Len |
---|
| 593 | c = dfs.getContentSummary(dstPath); |
---|
| 594 | assertEquals(c.getSpaceConsumed(), 5 * fileSpace - file2Len); |
---|
| 595 | |
---|
| 596 | // now try to increase the replication and and expect an error. |
---|
| 597 | hasException = false; |
---|
| 598 | try { |
---|
| 599 | dfs.setReplication(file2, (short)(replication+1)); |
---|
| 600 | } catch (DSQuotaExceededException e) { |
---|
| 601 | hasException = true; |
---|
| 602 | } |
---|
| 603 | assertTrue(hasException); |
---|
| 604 | |
---|
| 605 | // verify space consumed remains unchanged. |
---|
| 606 | c = dfs.getContentSummary(dstPath); |
---|
| 607 | assertEquals(c.getSpaceConsumed(), 5 * fileSpace - file2Len); |
---|
| 608 | |
---|
| 609 | // now increase the quota for quotaDir1 and quotaDir20 |
---|
| 610 | dfs.setQuota(quotaDir1, FSConstants.QUOTA_DONT_SET, 10 * fileSpace); |
---|
| 611 | dfs.setQuota(quotaDir20, FSConstants.QUOTA_DONT_SET, 10 * fileSpace); |
---|
| 612 | |
---|
| 613 | // then increasing replication should be ok. |
---|
| 614 | dfs.setReplication(file2, (short)(replication+1)); |
---|
| 615 | // verify increase in space |
---|
| 616 | c = dfs.getContentSummary(dstPath); |
---|
| 617 | assertEquals(c.getSpaceConsumed(), 5 * fileSpace + file2Len); |
---|
| 618 | |
---|
| 619 | } finally { |
---|
| 620 | cluster.shutdown(); |
---|
| 621 | } |
---|
| 622 | } |
---|
| 623 | } |
---|