1 | package org.apache.hadoop.filecache; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.net.URI; |
---|
5 | import java.util.Random; |
---|
6 | |
---|
7 | import org.apache.hadoop.conf.Configuration; |
---|
8 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
9 | import org.apache.hadoop.fs.FileStatus; |
---|
10 | import org.apache.hadoop.fs.FileSystem; |
---|
11 | import org.apache.hadoop.fs.Path; |
---|
12 | |
---|
13 | import junit.framework.TestCase; |
---|
14 | |
---|
15 | public class TestDistributedCache extends TestCase { |
---|
16 | |
---|
17 | static final URI LOCAL_FS = URI.create("file:///"); |
---|
18 | private static String TEST_CACHE_BASE_DIR = |
---|
19 | new Path(System.getProperty("test.build.data","/tmp/cachebasedir")) |
---|
20 | .toString().replace(' ', '+'); |
---|
21 | private static String TEST_ROOT_DIR = |
---|
22 | System.getProperty("test.build.data", "/tmp/distributedcache"); |
---|
23 | private static final int TEST_FILE_SIZE = 4 * 1024; // 4K |
---|
24 | private static final int LOCAL_CACHE_LIMIT = 5 * 1024; //5K |
---|
25 | private Configuration conf; |
---|
26 | private Path firstCacheFile; |
---|
27 | private Path secondCacheFile; |
---|
28 | private FileSystem localfs; |
---|
29 | |
---|
30 | /** |
---|
31 | * @see TestCase#setUp() |
---|
32 | */ |
---|
33 | @Override |
---|
34 | protected void setUp() throws IOException { |
---|
35 | conf = new Configuration(); |
---|
36 | conf.setLong("local.cache.size", LOCAL_CACHE_LIMIT); |
---|
37 | localfs = FileSystem.get(LOCAL_FS, conf); |
---|
38 | firstCacheFile = new Path(TEST_ROOT_DIR+"/firstcachefile"); |
---|
39 | secondCacheFile = new Path(TEST_ROOT_DIR+"/secondcachefile"); |
---|
40 | createTempFile(localfs, firstCacheFile); |
---|
41 | createTempFile(localfs, secondCacheFile); |
---|
42 | } |
---|
43 | |
---|
44 | /** test delete cache */ |
---|
45 | public void testDeleteCache() throws Exception { |
---|
46 | DistributedCache.getLocalCache(firstCacheFile.toUri(), conf, new Path(TEST_CACHE_BASE_DIR), |
---|
47 | false, System.currentTimeMillis(), new Path(TEST_ROOT_DIR)); |
---|
48 | DistributedCache.releaseCache(firstCacheFile.toUri(), conf); |
---|
49 | //in above code,localized a file of size 4K and then release the cache which will cause the cache |
---|
50 | //be deleted when the limit goes out. The below code localize another cache which's designed to |
---|
51 | //sweep away the first cache. |
---|
52 | DistributedCache.getLocalCache(secondCacheFile.toUri(), conf, new Path(TEST_CACHE_BASE_DIR), |
---|
53 | false, System.currentTimeMillis(), new Path(TEST_ROOT_DIR)); |
---|
54 | FileStatus[] dirStatuses = localfs.listStatus(new Path(TEST_CACHE_BASE_DIR)); |
---|
55 | assertTrue("DistributedCache failed deleting old cache when the cache store is full.", |
---|
56 | dirStatuses.length > 1); |
---|
57 | } |
---|
58 | |
---|
59 | private void createTempFile(FileSystem fs, Path p) throws IOException { |
---|
60 | FSDataOutputStream out = fs.create(p); |
---|
61 | byte[] toWrite = new byte[TEST_FILE_SIZE]; |
---|
62 | new Random().nextBytes(toWrite); |
---|
63 | out.write(toWrite); |
---|
64 | out.close(); |
---|
65 | FileSystem.LOG.info("created: " + p + ", size=" + TEST_FILE_SIZE); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * @see TestCase#tearDown() |
---|
70 | */ |
---|
71 | @Override |
---|
72 | protected void tearDown() throws IOException { |
---|
73 | localfs.delete(firstCacheFile, true); |
---|
74 | localfs.delete(secondCacheFile, true); |
---|
75 | localfs.close(); |
---|
76 | } |
---|
77 | } |
---|