001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile.bucket;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotEquals;
023import static org.junit.Assert.assertNotNull;
024
025import java.io.File;
026import java.io.IOException;
027import java.nio.ByteBuffer;
028import java.nio.channels.FileChannel;
029import java.util.ArrayList;
030import java.util.List;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.io.hfile.bucket.TestByteBufferIOEngine.BufferGrabbingDeserializer;
033import org.apache.hadoop.hbase.nio.ByteBuff;
034import org.apache.hadoop.hbase.testclassification.IOTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.After;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042/**
043 * Basic test for {@link FileIOEngine}
044 */
045@Category({IOTests.class, SmallTests.class})
046public class TestFileIOEngine {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestFileIOEngine.class);
051
052  private static final long TOTAL_CAPACITY = 6 * 1024 * 1024; // 6 MB
053  private static final String[] FILE_PATHS = {"testFileIOEngine1", "testFileIOEngine2",
054      "testFileIOEngine3"};
055  private static final long SIZE_PER_FILE = TOTAL_CAPACITY / FILE_PATHS.length; // 2 MB per File
056  private final static List<Long> boundaryStartPositions = new ArrayList<Long>();
057  private final static List<Long> boundaryStopPositions = new ArrayList<Long>();
058
059  private FileIOEngine fileIOEngine;
060
061  static {
062    boundaryStartPositions.add(0L);
063    for (int i = 1; i < FILE_PATHS.length; i++) {
064      boundaryStartPositions.add(SIZE_PER_FILE * i - 1);
065      boundaryStartPositions.add(SIZE_PER_FILE * i);
066      boundaryStartPositions.add(SIZE_PER_FILE * i + 1);
067    }
068    for (int i = 1; i < FILE_PATHS.length; i++) {
069      boundaryStopPositions.add(SIZE_PER_FILE * i - 1);
070      boundaryStopPositions.add(SIZE_PER_FILE * i);
071      boundaryStopPositions.add(SIZE_PER_FILE * i + 1);
072    }
073    boundaryStopPositions.add(SIZE_PER_FILE * FILE_PATHS.length - 1);
074  }
075
076  @Before
077  public void setUp() throws IOException {
078    fileIOEngine = new FileIOEngine(TOTAL_CAPACITY, false, FILE_PATHS);
079  }
080
081  @After
082  public void cleanUp() {
083    fileIOEngine.shutdown();
084    for (String filePath : FILE_PATHS) {
085      File file = new File(filePath);
086      if (file.exists()) {
087        file.delete();
088      }
089    }
090  }
091
092  @Test
093  public void testFileIOEngine() throws IOException {
094    for (int i = 0; i < 500; i++) {
095      int len = (int) Math.floor(Math.random() * 100) + 1;
096      long offset = (long) Math.floor(Math.random() * TOTAL_CAPACITY % (TOTAL_CAPACITY - len));
097      if (i < boundaryStartPositions.size()) {
098        // make the boundary start positon
099        offset = boundaryStartPositions.get(i);
100      } else if ((i - boundaryStartPositions.size()) < boundaryStopPositions.size()) {
101        // make the boundary stop positon
102        offset = boundaryStopPositions.get(i - boundaryStartPositions.size()) - len + 1;
103      } else if (i % 2 == 0) {
104        // make the cross-files block writing/reading
105        offset = Math.max(1, i % FILE_PATHS.length) * SIZE_PER_FILE - len / 2;
106      }
107      byte[] data1 = new byte[len];
108      for (int j = 0; j < data1.length; ++j) {
109        data1[j] = (byte) (Math.random() * 255);
110      }
111      fileIOEngine.write(ByteBuffer.wrap(data1), offset);
112      BufferGrabbingDeserializer deserializer = new BufferGrabbingDeserializer();
113      fileIOEngine.read(offset, len, deserializer);
114      ByteBuff data2 = deserializer.getDeserializedByteBuff();
115      assertArrayEquals(data1, data2.array());
116    }
117  }
118
119  @Test
120  public void testFileIOEngineHandlesZeroLengthInput() throws IOException {
121    byte[] data1 = new byte[0];
122
123    fileIOEngine.write(ByteBuffer.wrap(data1), 0);
124    BufferGrabbingDeserializer deserializer = new BufferGrabbingDeserializer();
125    fileIOEngine.read(0, 0, deserializer);
126    ByteBuff data2 = deserializer.getDeserializedByteBuff();
127    assertArrayEquals(data1, data2.array());
128  }
129
130  @Test
131  public void testClosedChannelException() throws IOException {
132    fileIOEngine.closeFileChannels();
133    int len = 5;
134    long offset = 0L;
135    byte[] data1 = new byte[len];
136    for (int j = 0; j < data1.length; ++j) {
137      data1[j] = (byte) (Math.random() * 255);
138    }
139    fileIOEngine.write(ByteBuffer.wrap(data1), offset);
140    BufferGrabbingDeserializer deserializer = new BufferGrabbingDeserializer();
141    fileIOEngine.read(offset, len, deserializer);
142    ByteBuff data2 = deserializer.getDeserializedByteBuff();
143    assertArrayEquals(data1, data2.array());
144  }
145
146  @Test
147  public void testRefreshFileConnection() throws IOException {
148    FileChannel[] fileChannels = fileIOEngine.getFileChannels();
149    FileChannel fileChannel = fileChannels[0];
150    assertNotNull(fileChannel);
151    fileChannel.close();
152    fileIOEngine.refreshFileConnection(0, new IOException("Test Exception"));
153    FileChannel[] reopenedFileChannels = fileIOEngine.getFileChannels();
154    FileChannel reopenedFileChannel = reopenedFileChannels[0];
155    assertNotEquals(fileChannel, reopenedFileChannel);
156    assertEquals(fileChannels.length, reopenedFileChannels.length);
157    for (int i = 1; i < fileChannels.length; i++) {
158      assertEquals(fileChannels[i], reopenedFileChannels[i]);
159    }
160  }
161}