View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
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   * Basic test for {@link FileIOEngine}
35   */
36  @Category(SmallTests.class)
37  public class TestFileIOEngine {
38    @Test
39    public void testFileIOEngine() throws IOException {
40      long totalCapacity = 6 * 1024 * 1024; // 6 MB
41      String[] filePaths = { "testFileIOEngine1", "testFileIOEngine2",
42          "testFileIOEngine3" };
43      long sizePerFile = totalCapacity / filePaths.length; // 2 MB per File
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            // make the boundary start positon
65            offset = boundaryStartPositions.get(i);
66          } else if ((i - boundaryStartPositions.size()) < boundaryStopPositions.size()) {
67            // make the boundary stop positon
68            offset = boundaryStopPositions.get(i - boundaryStartPositions.size()) - len + 1;
69          } else if (i % 2 == 0) {
70            // make the cross-files block writing/reading
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  }