1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io.hfile.bucket;
20
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32
33
34
35
36 @Category(SmallTests.class)
37 public class TestFileIOEngine {
38 @Test
39 public void testFileIOEngine() throws IOException {
40 long totalCapacity = 6 * 1024 * 1024;
41 String[] filePaths = { "testFileIOEngine1", "testFileIOEngine2",
42 "testFileIOEngine3" };
43 long sizePerFile = totalCapacity / filePaths.length;
44 List<Long> boundaryStartPositions = new ArrayList<Long>();
45 boundaryStartPositions.add(0L);
46 for (int i = 1; i < filePaths.length; i++) {
47 boundaryStartPositions.add(sizePerFile * i - 1);
48 boundaryStartPositions.add(sizePerFile * i);
49 boundaryStartPositions.add(sizePerFile * i + 1);
50 }
51 List<Long> boundaryStopPositions = new ArrayList<Long>();
52 for (int i = 1; i < filePaths.length; i++) {
53 boundaryStopPositions.add(sizePerFile * i - 1);
54 boundaryStopPositions.add(sizePerFile * i);
55 boundaryStopPositions.add(sizePerFile * i + 1);
56 }
57 boundaryStopPositions.add(sizePerFile * filePaths.length - 1);
58 FileIOEngine fileIOEngine = new FileIOEngine(totalCapacity, filePaths);
59 try {
60 for (int i = 0; i < 500; i++) {
61 int len = (int) Math.floor(Math.random() * 100);
62 long offset = (long) Math.floor(Math.random() * totalCapacity % (totalCapacity - len));
63 if (i < boundaryStartPositions.size()) {
64
65 offset = boundaryStartPositions.get(i);
66 } else if ((i - boundaryStartPositions.size()) < boundaryStopPositions.size()) {
67
68 offset = boundaryStopPositions.get(i - boundaryStartPositions.size()) - len + 1;
69 } else if (i % 2 == 0) {
70
71 offset = Math.max(1, i % filePaths.length) * sizePerFile - len / 2;
72 }
73 byte[] data1 = new byte[len];
74 for (int j = 0; j < data1.length; ++j) {
75 data1[j] = (byte) (Math.random() * 255);
76 }
77 byte[] data2 = new byte[len];
78 fileIOEngine.write(ByteBuffer.wrap(data1), offset);
79 fileIOEngine.read(ByteBuffer.wrap(data2), offset);
80 for (int j = 0; j < data1.length; ++j) {
81 assertTrue(data1[j] == data2[j]);
82 }
83 }
84 } finally {
85 fileIOEngine.shutdown();
86 for (String filePath : filePaths) {
87 File file = new File(filePath);
88 if (file.exists()) {
89 file.delete();
90 }
91 }
92 }
93
94 }
95 }