예제 #1
0
  @Test
  public void open_ignores_rollback() {
    File f = TT.tempDbFile();
    WriteAheadLog wal = new WriteAheadLog(f.getPath());
    wal.walPutLong(1L, 11L);
    wal.commit();
    wal.walPutLong(2L, 33L);
    wal.rollback();
    wal.walPutLong(3L, 33L);
    wal.commit();
    wal.seal();
    wal.close();

    wal = new WriteAheadLog(f.getPath());
    wal.open(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeLong, 1L, 11L},
            new Object[] {WALSequence.commit},
            // 2L is ignored, rollback section is skipped on hard replay
            new Object[] {WALSequence.writeLong, 3L, 33L},
            new Object[] {WALSequence.commit}));
    wal.destroyWalFiles();
    wal.close();

    f.delete();
  }
예제 #2
0
  @Test
  public void skip_rollback_last_rollback() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.walPutLong(1L, 11L);
    wal.commit();
    long o1 = wal.fileOffset;
    wal.walPutLong(2L, 33L);
    wal.commit();
    long o2 = wal.fileOffset;
    wal.walPutLong(3L, 33L);
    wal.rollback();
    wal.seal();

    assertEquals(o1, wal.skipRollbacks(o1));
    assertEquals(0, wal.skipRollbacks(o2));
  }
예제 #3
0
  @Test
  public void commitChecksum() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.open(WriteAheadLog.NOREPLAY);
    wal.startNextFile();

    wal.walPutLong(111L, 1000);
    wal.commit();
    long offset1 = wal.fileOffset - 5;
    int checksum1 = DataIO.longHash(wal.curVol.hash(16, offset1 - 16, 111L));

    assertEquals(checksum1, wal.curVol.getInt(offset1 + 1));
    wal.walPutLong(111L, 1000);
    wal.commit();
    long offset2 = wal.fileOffset - 5;
    int checksum2 =
        checksum1 + DataIO.longHash(wal.curVol.hash(offset1 + 5, offset2 - offset1 - 5, 111L));
    assertEquals(checksum2, wal.curVol.getInt(offset2 + 1));
  }
예제 #4
0
  @Test
  public void cut_broken_end_rollback() {
    String f = TT.tempDbFile().getPath();
    WriteAheadLog wal = new WriteAheadLog(f);
    wal.walPutLong(1L, 11L);
    wal.commit();
    wal.walPutLong(2L, 22L);
    wal.commit();
    wal.walPutLong(3L, 33L);
    wal.rollback();
    wal.walPutLong(4L, 44L);
    wal.curVol.sync();
    wal.close();

    wal = new WriteAheadLog(f);
    wal.open(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeLong, 1L, 11L},
            new Object[] {WALSequence.commit},
            new Object[] {WALSequence.writeLong, 2L, 22L},
            new Object[] {WALSequence.commit}));
  }
예제 #5
0
  @Test
  public void commit() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.open(WriteAheadLog.NOREPLAY);
    wal.walPutLong(111L, 1111L);
    wal.commit();
    wal.seal();

    wal.replayWAL(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeLong, 111L, 1111L},
            new Object[] {WALSequence.commit}));
  }
예제 #6
0
  @Test
  public void replay_commit_over_file_edge() {
    String f = TT.tempDbFile().getPath();
    WriteAheadLog wal = new WriteAheadLog(f);

    byte[] b = TT.randomByteArray(20 * 1024 * 1024);
    wal.walPutRecord(11L, b, 0, b.length);
    wal.walPutRecord(33L, b, 0, b.length);
    wal.commit();
    wal.close();

    wal = new WriteAheadLog(f);
    wal.open(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeRecord, 11L, 16L, b},
            new Object[] {WALSequence.writeRecord, 33L, 4294967312L, b},
            new Object[] {WALSequence.commit}));
  }
예제 #7
0
  @Test
  public void empty_commit() {
    String f = TT.tempDbFile().getPath();
    WriteAheadLog wal = new WriteAheadLog(f);

    byte[] b = TT.randomByteArray(1024);
    wal.walPutRecord(33L, b, 0, b.length);
    wal.commit();
    wal.commit();
    wal.seal();
    wal.close();

    wal = new WriteAheadLog(f);
    wal.open(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeRecord, 33L, 16L, b},
            new Object[] {WALSequence.commit},
            new Object[] {WALSequence.commit}));
  }
예제 #8
0
  @Test
  public void test() {
    File f = TT.tempDbFile();
    WriteAheadLog wal = new WriteAheadLog(f.getPath());

    for (int i = 0; i < commitNum; i++) {
      for (int j = 0; j < 6; j++) {
        wal.walPutLong(111L, i);
      }
      wal.commit();
    }

    int cutPoint = new Random(cutPointSeed).nextInt((int) wal.curVol.length());
    wal.curVol.sync();
    wal.curVol.clear(cutPoint, wal.curVol.length());
    File f2 = wal.curVol.getFile();
    wal.close();

    wal = new WriteAheadLog(f.getPath());
    final AtomicLong i = new AtomicLong();
    final AtomicLong c = new AtomicLong();
    wal.open(
        new WriteAheadLog.WALReplay() {
          @Override
          public void beforeReplayStart() {}

          @Override
          public void afterReplayFinished() {}

          @Override
          public void writeLong(long offset, long value) {
            assertEquals(111L, offset);
            assertEquals(i.get(), value);
            assertTrue(c.getAndIncrement() < 6);
          }

          @Override
          public void writeRecord(long recid, long walId, Volume vol, long volOffset, int length) {
            fail();
          }

          @Override
          public void writeByteArray(
              long offset, long walId, Volume vol, long volOffset, int length) {
            fail();
          }

          @Override
          public void commit() {
            assertEquals(6, c.get());
            c.set(0);
            i.incrementAndGet();
          }

          @Override
          public void rollback() {
            fail();
          }

          @Override
          public void writeTombstone(long recid) {
            fail();
          }

          @Override
          public void writePreallocate(long recid) {
            fail();
          }
        });

    assertEquals(0, c.get());

    f.delete();
    f2.delete();
  }