[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.util.ConcurrentModificationException; |
---|
| 21 | |
---|
| 22 | import org.apache.hadoop.conf.Configuration; |
---|
| 23 | import org.apache.hadoop.fs.Path; |
---|
| 24 | import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Test empty file creation. |
---|
| 28 | */ |
---|
| 29 | public class TestFileCreationEmpty extends junit.framework.TestCase { |
---|
| 30 | private boolean isConcurrentModificationException = false; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * This test creates three empty files and lets their leases expire. |
---|
| 34 | * This triggers release of the leases. |
---|
| 35 | * The empty files are supposed to be closed by that |
---|
| 36 | * without causing ConcurrentModificationException. |
---|
| 37 | */ |
---|
| 38 | public void testLeaseExpireEmptyFiles() throws Exception { |
---|
| 39 | final Thread.UncaughtExceptionHandler oldUEH = Thread.getDefaultUncaughtExceptionHandler(); |
---|
| 40 | Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { |
---|
| 41 | public void uncaughtException(Thread t, Throwable e) { |
---|
| 42 | if (e instanceof ConcurrentModificationException) { |
---|
| 43 | FSNamesystem.LOG.error("t=" + t, e); |
---|
| 44 | isConcurrentModificationException = true; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | }); |
---|
| 48 | |
---|
| 49 | System.out.println("testLeaseExpireEmptyFiles start"); |
---|
| 50 | final long leasePeriod = 1000; |
---|
| 51 | final int DATANODE_NUM = 3; |
---|
| 52 | |
---|
| 53 | final Configuration conf = new Configuration(); |
---|
| 54 | conf.setInt("heartbeat.recheck.interval", 1000); |
---|
| 55 | conf.setInt("dfs.heartbeat.interval", 1); |
---|
| 56 | |
---|
| 57 | // create cluster |
---|
| 58 | MiniDFSCluster cluster = new MiniDFSCluster(conf, DATANODE_NUM, true, null); |
---|
| 59 | try { |
---|
| 60 | cluster.waitActive(); |
---|
| 61 | DistributedFileSystem dfs = (DistributedFileSystem)cluster.getFileSystem(); |
---|
| 62 | |
---|
| 63 | // create a new file. |
---|
| 64 | TestFileCreation.createFile(dfs, new Path("/foo"), DATANODE_NUM); |
---|
| 65 | TestFileCreation.createFile(dfs, new Path("/foo2"), DATANODE_NUM); |
---|
| 66 | TestFileCreation.createFile(dfs, new Path("/foo3"), DATANODE_NUM); |
---|
| 67 | |
---|
| 68 | // set the soft and hard limit to be 1 second so that the |
---|
| 69 | // namenode triggers lease recovery |
---|
| 70 | cluster.setLeasePeriod(leasePeriod, leasePeriod); |
---|
| 71 | // wait for the lease to expire |
---|
| 72 | try {Thread.sleep(5 * leasePeriod);} catch (InterruptedException e) {} |
---|
| 73 | |
---|
| 74 | assertFalse(isConcurrentModificationException); |
---|
| 75 | } finally { |
---|
| 76 | Thread.setDefaultUncaughtExceptionHandler(oldUEH); |
---|
| 77 | cluster.shutdown(); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|