[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 | |
---|
| 19 | package org.apache.hadoop.hdfs; |
---|
| 20 | |
---|
| 21 | import java.io.BufferedReader; |
---|
| 22 | import java.io.ByteArrayOutputStream; |
---|
| 23 | import java.io.File; |
---|
| 24 | import java.io.FileReader; |
---|
| 25 | import java.io.IOException; |
---|
| 26 | import java.net.URL; |
---|
| 27 | import java.net.URLConnection; |
---|
| 28 | import java.util.Random; |
---|
| 29 | import junit.framework.TestCase; |
---|
| 30 | import org.apache.hadoop.hdfs.DFSClient.DFSDataInputStream; |
---|
| 31 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
| 32 | import org.apache.hadoop.io.IOUtils; |
---|
| 33 | import org.apache.hadoop.fs.FSDataInputStream; |
---|
| 34 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
| 35 | import org.apache.hadoop.fs.FileSystem; |
---|
| 36 | import org.apache.hadoop.fs.Path; |
---|
| 37 | import org.apache.hadoop.fs.BlockLocation; |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | */ |
---|
| 41 | public class DFSTestUtil extends TestCase { |
---|
| 42 | |
---|
| 43 | private static Random gen = new Random(); |
---|
| 44 | private static String[] dirNames = { |
---|
| 45 | "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | private int maxLevels;// = 3; |
---|
| 49 | private int maxSize;// = 8*1024; |
---|
| 50 | private int nFiles; |
---|
| 51 | private MyFile[] files; |
---|
| 52 | |
---|
| 53 | /** Creates a new instance of DFSTestUtil |
---|
| 54 | * |
---|
| 55 | * @param testName Name of the test from where this utility is used |
---|
| 56 | * @param nFiles Number of files to be created |
---|
| 57 | * @param maxLevels Maximum number of directory levels |
---|
| 58 | * @param maxSize Maximum size for file |
---|
| 59 | */ |
---|
| 60 | public DFSTestUtil(String testName, int nFiles, int maxLevels, int maxSize) { |
---|
| 61 | this.nFiles = nFiles; |
---|
| 62 | this.maxLevels = maxLevels; |
---|
| 63 | this.maxSize = maxSize; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** class MyFile contains enough information to recreate the contents of |
---|
| 67 | * a single file. |
---|
| 68 | */ |
---|
| 69 | private class MyFile { |
---|
| 70 | |
---|
| 71 | private String name = ""; |
---|
| 72 | private int size; |
---|
| 73 | private long seed; |
---|
| 74 | |
---|
| 75 | MyFile() { |
---|
| 76 | int nLevels = gen.nextInt(maxLevels); |
---|
| 77 | if (nLevels != 0) { |
---|
| 78 | int[] levels = new int[nLevels]; |
---|
| 79 | for (int idx = 0; idx < nLevels; idx++) { |
---|
| 80 | levels[idx] = gen.nextInt(10); |
---|
| 81 | } |
---|
| 82 | StringBuffer sb = new StringBuffer(); |
---|
| 83 | for (int idx = 0; idx < nLevels; idx++) { |
---|
| 84 | sb.append(dirNames[levels[idx]]); |
---|
| 85 | sb.append("/"); |
---|
| 86 | } |
---|
| 87 | name = sb.toString(); |
---|
| 88 | } |
---|
| 89 | long fidx = -1; |
---|
| 90 | while (fidx < 0) { fidx = gen.nextLong(); } |
---|
| 91 | name = name + Long.toString(fidx); |
---|
| 92 | size = gen.nextInt(maxSize); |
---|
| 93 | seed = gen.nextLong(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | String getName() { return name; } |
---|
| 97 | int getSize() { return size; } |
---|
| 98 | long getSeed() { return seed; } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | public void createFiles(FileSystem fs, String topdir) throws IOException { |
---|
| 102 | createFiles(fs, topdir, (short)3); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** create nFiles with random names and directory hierarchies |
---|
| 106 | * with random (but reproducible) data in them. |
---|
| 107 | */ |
---|
| 108 | void createFiles(FileSystem fs, String topdir, |
---|
| 109 | short replicationFactor) throws IOException { |
---|
| 110 | files = new MyFile[nFiles]; |
---|
| 111 | |
---|
| 112 | for (int idx = 0; idx < nFiles; idx++) { |
---|
| 113 | files[idx] = new MyFile(); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | Path root = new Path(topdir); |
---|
| 117 | |
---|
| 118 | for (int idx = 0; idx < nFiles; idx++) { |
---|
| 119 | createFile(fs, new Path(root, files[idx].getName()), files[idx].getSize(), |
---|
| 120 | replicationFactor, files[idx].getSeed()); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | public static void createFile(FileSystem fs, Path fileName, long fileLen, |
---|
| 125 | short replFactor, long seed) throws IOException { |
---|
| 126 | if (!fs.mkdirs(fileName.getParent())) { |
---|
| 127 | throw new IOException("Mkdirs failed to create " + |
---|
| 128 | fileName.getParent().toString()); |
---|
| 129 | } |
---|
| 130 | FSDataOutputStream out = null; |
---|
| 131 | try { |
---|
| 132 | out = fs.create(fileName, replFactor); |
---|
| 133 | byte[] toWrite = new byte[1024]; |
---|
| 134 | Random rb = new Random(seed); |
---|
| 135 | long bytesToWrite = fileLen; |
---|
| 136 | while (bytesToWrite>0) { |
---|
| 137 | rb.nextBytes(toWrite); |
---|
| 138 | int bytesToWriteNext = (1024<bytesToWrite)?1024:(int)bytesToWrite; |
---|
| 139 | |
---|
| 140 | out.write(toWrite, 0, bytesToWriteNext); |
---|
| 141 | bytesToWrite -= bytesToWriteNext; |
---|
| 142 | } |
---|
| 143 | out.close(); |
---|
| 144 | out = null; |
---|
| 145 | } finally { |
---|
| 146 | IOUtils.closeStream(out); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** check if the files have been copied correctly. */ |
---|
| 151 | public boolean checkFiles(FileSystem fs, String topdir) throws IOException { |
---|
| 152 | |
---|
| 153 | //Configuration conf = new Configuration(); |
---|
| 154 | Path root = new Path(topdir); |
---|
| 155 | |
---|
| 156 | for (int idx = 0; idx < nFiles; idx++) { |
---|
| 157 | Path fPath = new Path(root, files[idx].getName()); |
---|
| 158 | FSDataInputStream in = fs.open(fPath); |
---|
| 159 | byte[] toRead = new byte[files[idx].getSize()]; |
---|
| 160 | byte[] toCompare = new byte[files[idx].getSize()]; |
---|
| 161 | Random rb = new Random(files[idx].getSeed()); |
---|
| 162 | rb.nextBytes(toCompare); |
---|
| 163 | in.readFully(0, toRead); |
---|
| 164 | in.close(); |
---|
| 165 | for (int i = 0; i < toRead.length; i++) { |
---|
| 166 | if (toRead[i] != toCompare[i]) { |
---|
| 167 | return false; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | toRead = null; |
---|
| 171 | toCompare = null; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | return true; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void setReplication(FileSystem fs, String topdir, short value) |
---|
| 178 | throws IOException { |
---|
| 179 | Path root = new Path(topdir); |
---|
| 180 | for (int idx = 0; idx < nFiles; idx++) { |
---|
| 181 | Path fPath = new Path(root, files[idx].getName()); |
---|
| 182 | fs.setReplication(fPath, value); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | // waits for the replication factor of all files to reach the |
---|
| 187 | // specified target |
---|
| 188 | // |
---|
| 189 | public void waitReplication(FileSystem fs, String topdir, short value) |
---|
| 190 | throws IOException { |
---|
| 191 | Path root = new Path(topdir); |
---|
| 192 | |
---|
| 193 | /** wait for the replication factor to settle down */ |
---|
| 194 | for (int idx = 0; idx < nFiles; idx++) { |
---|
| 195 | waitReplication(fs, new Path(root, files[idx].getName()), value); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** return list of filenames created as part of createFiles */ |
---|
| 200 | public String[] getFileNames(String topDir) { |
---|
| 201 | if (nFiles == 0) |
---|
| 202 | return new String[]{}; |
---|
| 203 | else { |
---|
| 204 | String[] fileNames = new String[nFiles]; |
---|
| 205 | for (int idx=0; idx < nFiles; idx++) { |
---|
| 206 | fileNames[idx] = topDir + "/" + files[idx].getName(); |
---|
| 207 | } |
---|
| 208 | return fileNames; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** wait for the file's replication to be done */ |
---|
| 213 | public static void waitReplication(FileSystem fs, Path fileName, |
---|
| 214 | short replFactor) throws IOException { |
---|
| 215 | boolean good; |
---|
| 216 | do { |
---|
| 217 | good = true; |
---|
| 218 | BlockLocation locs[] = fs.getFileBlockLocations( |
---|
| 219 | fs.getFileStatus(fileName), 0, Long.MAX_VALUE); |
---|
| 220 | for (int j = 0; j < locs.length; j++) { |
---|
| 221 | String[] loc = locs[j].getHosts(); |
---|
| 222 | if (loc.length != replFactor) { |
---|
| 223 | System.out.println("File " + fileName + " has replication factor " + |
---|
| 224 | loc.length); |
---|
| 225 | good = false; |
---|
| 226 | try { |
---|
| 227 | System.out.println("Waiting for replication factor to drain"); |
---|
| 228 | Thread.sleep(100); |
---|
| 229 | } catch (InterruptedException e) {} |
---|
| 230 | break; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } while(!good); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** delete directory and everything underneath it.*/ |
---|
| 237 | public void cleanup(FileSystem fs, String topdir) throws IOException { |
---|
| 238 | Path root = new Path(topdir); |
---|
| 239 | fs.delete(root, true); |
---|
| 240 | files = null; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | public static Block getFirstBlock(FileSystem fs, Path path) throws IOException { |
---|
| 244 | DFSDataInputStream in = |
---|
| 245 | (DFSDataInputStream) ((DistributedFileSystem)fs).open(path); |
---|
| 246 | in.readByte(); |
---|
| 247 | return in.getCurrentBlock(); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | static void setLogLevel2All(org.apache.commons.logging.Log log) { |
---|
| 251 | ((org.apache.commons.logging.impl.Log4JLogger)log |
---|
| 252 | ).getLogger().setLevel(org.apache.log4j.Level.ALL); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | static String readFile(File f) throws IOException { |
---|
| 256 | StringBuilder b = new StringBuilder(); |
---|
| 257 | BufferedReader in = new BufferedReader(new FileReader(f)); |
---|
| 258 | for(int c; (c = in.read()) != -1; b.append((char)c)); |
---|
| 259 | in.close(); |
---|
| 260 | return b.toString(); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | // Returns url content as string. |
---|
| 264 | public static String urlGet(URL url) throws IOException { |
---|
| 265 | URLConnection conn = url.openConnection(); |
---|
| 266 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 267 | IOUtils.copyBytes(conn.getInputStream(), out, 4096, true); |
---|
| 268 | return out.toString(); |
---|
| 269 | } |
---|
| 270 | } |
---|