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.IOException; |
---|
21 | |
---|
22 | import org.apache.commons.logging.Log; |
---|
23 | import org.apache.commons.logging.LogFactory; |
---|
24 | import org.apache.hadoop.conf.Configuration; |
---|
25 | import org.apache.hadoop.fs.*; |
---|
26 | import org.apache.hadoop.hdfs.protocol.LocatedBlock; |
---|
27 | import org.apache.hadoop.hdfs.protocol.LocatedBlocks; |
---|
28 | import org.apache.hadoop.hdfs.server.namenode.NameNode; |
---|
29 | import org.apache.hadoop.util.StringUtils; |
---|
30 | |
---|
31 | public class TestAbandonBlock extends junit.framework.TestCase { |
---|
32 | public static final Log LOG = LogFactory.getLog(TestAbandonBlock.class); |
---|
33 | |
---|
34 | private static final Configuration CONF = new Configuration(); |
---|
35 | static final String FILE_NAME_PREFIX |
---|
36 | = "/" + TestAbandonBlock.class.getSimpleName() + "_"; |
---|
37 | |
---|
38 | public void testAbandonBlock() throws IOException { |
---|
39 | MiniDFSCluster cluster = new MiniDFSCluster(CONF, 2, true, null); |
---|
40 | FileSystem fs = cluster.getFileSystem(); |
---|
41 | |
---|
42 | String src = FILE_NAME_PREFIX + "foo"; |
---|
43 | FSDataOutputStream fout = null; |
---|
44 | try { |
---|
45 | //start writing a a file but not close it |
---|
46 | fout = fs.create(new Path(src), true, 4096, (short)1, 512L); |
---|
47 | for(int i = 0; i < 1024; i++) { |
---|
48 | fout.write(123); |
---|
49 | } |
---|
50 | fout.sync(); |
---|
51 | |
---|
52 | //try reading the block by someone |
---|
53 | final DFSClient dfsclient = new DFSClient(NameNode.getAddress(CONF), CONF); |
---|
54 | LocatedBlocks blocks = dfsclient.namenode.getBlockLocations(src, 0, 1); |
---|
55 | LocatedBlock b = blocks.get(0); |
---|
56 | try { |
---|
57 | dfsclient.namenode.abandonBlock(b.getBlock(), src, "someone"); |
---|
58 | //previous line should throw an exception. |
---|
59 | assertTrue(false); |
---|
60 | } |
---|
61 | catch(IOException ioe) { |
---|
62 | LOG.info("GREAT! " + StringUtils.stringifyException(ioe)); |
---|
63 | } |
---|
64 | } |
---|
65 | finally { |
---|
66 | try{fout.close();} catch(Exception e) {} |
---|
67 | try{fs.close();} catch(Exception e) {} |
---|
68 | try{cluster.shutdown();} catch(Exception e) {} |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|