[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.IOException; |
---|
| 21 | import java.io.InputStream; |
---|
| 22 | import java.io.OutputStream; |
---|
| 23 | import java.util.Random; |
---|
| 24 | |
---|
| 25 | import junit.framework.TestCase; |
---|
| 26 | |
---|
| 27 | import org.apache.commons.logging.Log; |
---|
| 28 | import org.apache.commons.logging.LogFactory; |
---|
| 29 | import org.apache.hadoop.conf.Configuration; |
---|
| 30 | import org.apache.hadoop.fs.FileStatus; |
---|
| 31 | import org.apache.hadoop.fs.FileSystem; |
---|
| 32 | import org.apache.hadoop.fs.Path; |
---|
| 33 | import org.apache.hadoop.security.UnixUserGroupInformation; |
---|
| 34 | import org.apache.hadoop.security.UserGroupInformation; |
---|
| 35 | |
---|
| 36 | /** Utilities for append-related tests */ |
---|
| 37 | class AppendTestUtil { |
---|
| 38 | /** For specifying the random number generator seed, |
---|
| 39 | * change the following value: |
---|
| 40 | */ |
---|
| 41 | static final Long RANDOM_NUMBER_GENERATOR_SEED = null; |
---|
| 42 | |
---|
| 43 | static final Log LOG = LogFactory.getLog(AppendTestUtil.class); |
---|
| 44 | |
---|
| 45 | private static final Random SEED = new Random(); |
---|
| 46 | static { |
---|
| 47 | final long seed = RANDOM_NUMBER_GENERATOR_SEED == null? |
---|
| 48 | SEED.nextLong(): RANDOM_NUMBER_GENERATOR_SEED; |
---|
| 49 | LOG.info("seed=" + seed); |
---|
| 50 | SEED.setSeed(seed); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | private static final ThreadLocal<Random> RANDOM = new ThreadLocal<Random>() { |
---|
| 54 | protected Random initialValue() { |
---|
| 55 | final Random r = new Random(); |
---|
| 56 | synchronized(SEED) { |
---|
| 57 | final long seed = SEED.nextLong(); |
---|
| 58 | r.setSeed(seed); |
---|
| 59 | LOG.info(Thread.currentThread().getName() + ": seed=" + seed); |
---|
| 60 | } |
---|
| 61 | return r; |
---|
| 62 | } |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | static int nextInt() {return RANDOM.get().nextInt();} |
---|
| 66 | static int nextInt(int n) {return RANDOM.get().nextInt(n);} |
---|
| 67 | static int nextLong() {return RANDOM.get().nextInt();} |
---|
| 68 | |
---|
| 69 | static byte[] randomBytes(long seed, int size) { |
---|
| 70 | LOG.info("seed=" + seed + ", size=" + size); |
---|
| 71 | final byte[] b = new byte[size]; |
---|
| 72 | final Random rand = new Random(seed); |
---|
| 73 | rand.nextBytes(b); |
---|
| 74 | return b; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static void sleep(long ms) { |
---|
| 78 | try { |
---|
| 79 | Thread.sleep(ms); |
---|
| 80 | } catch (InterruptedException e) { |
---|
| 81 | LOG.info("ms=" + ms, e); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | static FileSystem createHdfsWithDifferentUsername(Configuration conf |
---|
| 86 | ) throws IOException { |
---|
| 87 | Configuration conf2 = new Configuration(conf); |
---|
| 88 | String username = UserGroupInformation.getCurrentUGI().getUserName()+"_XXX"; |
---|
| 89 | UnixUserGroupInformation.saveToConf(conf2, |
---|
| 90 | UnixUserGroupInformation.UGI_PROPERTY_NAME, |
---|
| 91 | new UnixUserGroupInformation(username, new String[]{"supergroup"})); |
---|
| 92 | return FileSystem.get(conf2); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | static void write(OutputStream out, int offset, int length) throws IOException { |
---|
| 96 | final byte[] bytes = new byte[length]; |
---|
| 97 | for(int i = 0; i < length; i++) { |
---|
| 98 | bytes[i] = (byte)(offset + i); |
---|
| 99 | } |
---|
| 100 | out.write(bytes); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | static void check(FileSystem fs, Path p, long length) throws IOException { |
---|
| 104 | int i = -1; |
---|
| 105 | try { |
---|
| 106 | final FileStatus status = fs.getFileStatus(p); |
---|
| 107 | TestCase.assertEquals(length, status.getLen()); |
---|
| 108 | InputStream in = fs.open(p); |
---|
| 109 | for(i++; i < length; i++) { |
---|
| 110 | TestCase.assertEquals((byte)i, (byte)in.read()); |
---|
| 111 | } |
---|
| 112 | i = -(int)length; |
---|
| 113 | TestCase.assertEquals(-1, in.read()); //EOF |
---|
| 114 | in.close(); |
---|
| 115 | } catch(IOException ioe) { |
---|
| 116 | throw new IOException("p=" + p + ", length=" + length + ", i=" + i, ioe); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|