Beispiel #1
0
  /**
   * Tests setting end offset - makes sure legal values are set correctly and illegal values are
   * rejected.
   *
   * @throws IOException
   */
  @Test
  public void endOffsetTest() throws IOException {
    String segmentName = "log_current";
    LogSegment segment = getSegment(segmentName, STANDARD_SEGMENT_SIZE, true);
    try {
      long writeStartOffset = segment.getStartOffset();
      int segmentSize = 500;
      appendRandomData(segment, segmentSize);
      assertEquals(
          "End offset is not as expected", writeStartOffset + segmentSize, segment.getEndOffset());

      // should be able to set end offset to something >= initial offset and <= file size
      int[] offsetsToSet = {(int) (writeStartOffset), segmentSize / 2, segmentSize};
      for (int offset : offsetsToSet) {
        segment.setEndOffset(offset);
        assertEquals("End offset is not equal to what was set", offset, segment.getEndOffset());
        assertEquals(
            "File channel positioning is incorrect",
            offset,
            segment.getView().getSecond().position());
      }

      // cannot set end offset to illegal values (< initial offset or > file size)
      int[] invalidOffsets = {(int) (writeStartOffset - 1), (int) (segment.sizeInBytes() + 1)};
      for (int offset : invalidOffsets) {
        try {
          segment.setEndOffset(offset);
          fail("Setting log end offset an invalid offset [" + offset + "] should have failed");
        } catch (IllegalArgumentException e) {
          // expected. Nothing to do.
        }
      }
    } finally {
      closeSegmentAndDeleteFile(segment);
    }
  }
Beispiel #2
0
  /**
   * Tests appending and reading to make sure data is written and the data read is consistent with
   * the data written.
   *
   * @throws IOException
   */
  @Test
  public void basicWriteAndReadTest() throws IOException {
    String segmentName = "log_current";
    LogSegment segment = getSegment(segmentName, STANDARD_SEGMENT_SIZE, true);
    try {
      assertEquals(
          "Name of segment is inconsistent with what was provided", segmentName, segment.getName());
      assertEquals(
          "Capacity of segment is inconsistent with what was provided",
          STANDARD_SEGMENT_SIZE,
          segment.getCapacityInBytes());
      assertEquals(
          "Start offset is not equal to header size",
          LogSegment.HEADER_SIZE,
          segment.getStartOffset());
      int writeSize = 100;
      byte[] buf = TestUtils.getRandomBytes(3 * writeSize);
      long writeStartOffset = segment.getStartOffset();
      // append with buffer
      int written = segment.appendFrom(ByteBuffer.wrap(buf, 0, writeSize));
      assertEquals("Size written did not match size of buffer provided", writeSize, written);
      assertEquals(
          "End offset is not as expected", writeStartOffset + writeSize, segment.getEndOffset());
      readAndEnsureMatch(segment, writeStartOffset, Arrays.copyOfRange(buf, 0, writeSize));

      // append with channel
      segment.appendFrom(
          Channels.newChannel(
              new ByteBufferInputStream(ByteBuffer.wrap(buf, writeSize, writeSize))),
          writeSize);
      assertEquals(
          "End offset is not as expected",
          writeStartOffset + 2 * writeSize,
          segment.getEndOffset());
      readAndEnsureMatch(
          segment, writeStartOffset + writeSize, Arrays.copyOfRange(buf, writeSize, 2 * writeSize));

      // use writeFrom
      segment.writeFrom(
          Channels.newChannel(
              new ByteBufferInputStream(ByteBuffer.wrap(buf, 2 * writeSize, writeSize))),
          segment.getEndOffset(),
          writeSize);
      assertEquals(
          "End offset is not as expected",
          writeStartOffset + 3 * writeSize,
          segment.getEndOffset());
      readAndEnsureMatch(
          segment,
          writeStartOffset + 2 * writeSize,
          Arrays.copyOfRange(buf, 2 * writeSize, buf.length));

      readAndEnsureMatch(segment, writeStartOffset, buf);
      // check file size and end offset (they will not match)
      assertEquals(
          "End offset is not equal to the cumulative bytes written",
          writeStartOffset + 3 * writeSize,
          segment.getEndOffset());
      assertEquals(
          "Size in bytes is not equal to size of the file",
          STANDARD_SEGMENT_SIZE,
          segment.sizeInBytes());

      // ensure flush doesn't throw any errors.
      segment.flush();
      // close and reopen segment and ensure persistence.
      segment.close();
      segment = new LogSegment(segmentName, new File(tempDir, segmentName), metrics);
      segment.setEndOffset(writeStartOffset + buf.length);
      readAndEnsureMatch(segment, writeStartOffset, buf);
    } finally {
      closeSegmentAndDeleteFile(segment);
    }
  }