|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations under |
| 15 | + * the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.gluster.test; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import junit.framework.TestCase; |
| 26 | + |
| 27 | +import org.apache.hadoop.conf.Configuration; |
| 28 | +import org.apache.hadoop.fs.BlockLocation; |
| 29 | +import org.apache.hadoop.fs.FSDataOutputStream; |
| 30 | +import org.apache.hadoop.fs.FileStatus; |
| 31 | +import org.apache.hadoop.fs.FileSystem; |
| 32 | +import org.apache.hadoop.fs.Path; |
| 33 | +import org.eclipse.jdt.core.dom.ThisExpression; |
| 34 | + |
| 35 | +/** |
| 36 | + * Testing the correctness of FileSystem.getFileBlockLocations. |
| 37 | + */ |
| 38 | +public class TestGetFileBlockLocations extends TestCase { |
| 39 | + private static String TEST_ROOT_DIR = |
| 40 | + System.getProperty("test.build.data", "/tmp/testGetFileBlockLocations"); |
| 41 | + private static final int FileLength = 4 * 1024 * 1024; // 4MB |
| 42 | + private Configuration conf; |
| 43 | + private Path path; |
| 44 | + private FileSystem fs; |
| 45 | + private Random random; |
| 46 | + |
| 47 | + /** |
| 48 | + * @see TestCase#setUp() |
| 49 | + */ |
| 50 | + @Override |
| 51 | + protected void setUp() throws Exception { |
| 52 | + conf = GFSUtil.createConfiguration(true); |
| 53 | + Path rootPath = new Path(TEST_ROOT_DIR); |
| 54 | + path = new Path(rootPath, "TestGetFileBlockLocations"); |
| 55 | + //Doesn't work with the |
| 56 | + System.out.println(conf.get("fs.glusterfs.name")); |
| 57 | + fs = rootPath.getFileSystem(conf); |
| 58 | + FSDataOutputStream fsdos = fs.create(path, true); |
| 59 | + byte[] buffer = new byte[1024]; |
| 60 | + while (fsdos.getPos() < FileLength) { |
| 61 | + fsdos.write(buffer); |
| 62 | + } |
| 63 | + fsdos.close(); |
| 64 | + random = new Random(System.nanoTime()); |
| 65 | + } |
| 66 | + |
| 67 | + private void oneTest(int offBegin, int offEnd, FileStatus status) |
| 68 | + throws IOException { |
| 69 | + if (offBegin > offEnd) { |
| 70 | + int tmp = offBegin; |
| 71 | + offBegin = offEnd; |
| 72 | + offEnd = tmp; |
| 73 | + } |
| 74 | + BlockLocation[] locations = |
| 75 | + fs.getFileBlockLocations(status, offBegin, offEnd - offBegin); |
| 76 | + if (offBegin < status.getLen()) { |
| 77 | + Arrays.sort(locations, new Comparator<BlockLocation>() { |
| 78 | + public int compare(BlockLocation arg0,BlockLocation arg1){ |
| 79 | + long cmprv = arg0.getOffset() - arg1.getOffset(); |
| 80 | + if (cmprv < 0) return -1; |
| 81 | + if (cmprv > 0) return 1; |
| 82 | + cmprv = arg0.getLength() - arg1.getLength(); |
| 83 | + if (cmprv < 0) return -1; |
| 84 | + if (cmprv > 0) return 1; |
| 85 | + return 0; |
| 86 | + } |
| 87 | + }); |
| 88 | + offBegin = (int) Math.min(offBegin, status.getLen() - 1); |
| 89 | + offEnd = (int) Math.min(offEnd, status.getLen()); |
| 90 | + BlockLocation first = locations[0]; |
| 91 | + BlockLocation last = locations[locations.length - 1]; |
| 92 | + assertTrue(first.getOffset() <= offBegin); |
| 93 | + assertTrue(offEnd <= last.getOffset() + last.getLength()); |
| 94 | + } else { |
| 95 | + assertTrue(locations.length == 0); |
| 96 | + } |
| 97 | + } |
| 98 | + /** |
| 99 | + * @see TestCase#tearDown() |
| 100 | + */ |
| 101 | + @Override |
| 102 | + protected void tearDown() throws IOException { |
| 103 | + fs.delete(path, true); |
| 104 | + fs.close(); |
| 105 | + } |
| 106 | + |
| 107 | + public void testFailureNegativeParameters() throws IOException { |
| 108 | + FileStatus status = fs.getFileStatus(path); |
| 109 | + try { |
| 110 | + BlockLocation[] locations = fs.getFileBlockLocations(status, -1, 100); |
| 111 | + fail("Expecting exception being throw"); |
| 112 | + } catch (IllegalArgumentException e) { |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + BlockLocation[] locations = fs.getFileBlockLocations(status, 100, -1); |
| 118 | + fail("Expecting exception being throw"); |
| 119 | + } catch (IllegalArgumentException e) { |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void testGetFileBlockLocations1() throws IOException { |
| 125 | + FileStatus status = fs.getFileStatus(path); |
| 126 | + oneTest(0, (int) status.getLen(), status); |
| 127 | + oneTest(0, (int) status.getLen() * 2, status); |
| 128 | + oneTest((int) status.getLen() * 2, (int) status.getLen() * 4, status); |
| 129 | + oneTest((int) status.getLen() / 2, (int) status.getLen() * 3, status); |
| 130 | + for (int i = 0; i < 10; ++i) { |
| 131 | + oneTest((int) status.getLen() * i / 10, (int) status.getLen() * (i + 1) |
| 132 | + / 10, status); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public void testGetFileBlockLocations2() throws IOException { |
| 137 | + FileStatus status = fs.getFileStatus(path); |
| 138 | + for (int i = 0; i < 1000; ++i) { |
| 139 | + int offBegin = random.nextInt((int) (2 * status.getLen())); |
| 140 | + int offEnd = random.nextInt((int) (2 * status.getLen())); |
| 141 | + oneTest(offBegin, offEnd, status); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments