Example usage for io.netty.buffer ByteBuf writeLong

List of usage examples for io.netty.buffer ByteBuf writeLong

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeLong.

Prototype

public abstract ByteBuf writeLong(long value);

Source Link

Document

Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer.

Usage

From source file:org.apache.bookkeeper.bookie.LedgerDescriptor.java

License:Apache License

static ByteBuf createLedgerFenceEntry(Long ledgerId) {
    ByteBuf bb = Unpooled.buffer(8 + 8);
    bb.writeLong(ledgerId);
    bb.writeLong(METAENTRY_ID_FENCE_KEY);
    return bb;/*from  w  w w . ja  va2 s  .  c  o m*/
}

From source file:org.apache.bookkeeper.bookie.SortedLedgerStorageCheckpointTest.java

License:Apache License

ByteBuf prepareEntry(long ledgerId, long entryId) {
    ByteBuf entry = Unpooled.buffer(4 * Long.BYTES);
    // ledger id, entry id, lac
    entry.writeLong(ledgerId);
    entry.writeLong(entryId);//from  ww w .j a v a2 s  .  c om
    entry.writeLong(entryId - 1);
    // data
    entry.writeLong(entryId);
    return entry;
}

From source file:org.apache.bookkeeper.bookie.SortedLedgerStorageTest.java

License:Apache License

@Test
public void testGetListOfEntriesOfLedger() throws Exception {
    long nonExistingLedgerId = 123456L;
    OfLong entriesItr = sortedLedgerStorage.getListOfEntriesOfLedger(nonExistingLedgerId);
    assertFalse("There shouldn't be any entries for this ledger", entriesItr.hasNext());
    // Insert some ledger & entries in the interleaved storage
    for (long entryId = 0; entryId < numWrites; entryId++) {
        for (long ledgerId = 0; ledgerId < numOfLedgers; ledgerId++) {
            if (entryId == 0) {
                sortedLedgerStorage.setMasterKey(ledgerId, ("ledger-" + ledgerId).getBytes());
                sortedLedgerStorage.setFenced(ledgerId);
            }/*from   ww  w. j a  v a2 s. c  o  m*/
            ByteBuf entry = Unpooled.buffer(128);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId * entriesPerWrite);
            entry.writeBytes(("entry-" + entryId).getBytes());

            sortedLedgerStorage.addEntry(entry);
        }
    }

    for (long ledgerId = 0; ledgerId < numOfLedgers; ledgerId++) {
        OfLong entriesOfLedger = sortedLedgerStorage.getListOfEntriesOfLedger(ledgerId);
        ArrayList<Long> arrayList = new ArrayList<Long>();
        Consumer<Long> addMethod = arrayList::add;
        entriesOfLedger.forEachRemaining(addMethod);
        assertEquals("Number of entries", numWrites, arrayList.size());
        assertTrue("Entries of Ledger", IntStream.range(0, arrayList.size()).allMatch(i -> {
            return arrayList.get(i).longValue() == (i * entriesPerWrite);
        }));
    }

    nonExistingLedgerId = 456789L;
    entriesItr = sortedLedgerStorage.getListOfEntriesOfLedger(nonExistingLedgerId);
    assertFalse("There shouldn't be any entry", entriesItr.hasNext());
}

From source file:org.apache.bookkeeper.bookie.SortedLedgerStorageTest.java

License:Apache License

@Test
public void testGetListOfEntriesOfLedgerAfterFlush() throws IOException {
    // Insert some ledger & entries in the interleaved storage
    for (long entryId = 0; entryId < numWrites; entryId++) {
        for (long ledgerId = 0; ledgerId < numOfLedgers; ledgerId++) {
            if (entryId == 0) {
                sortedLedgerStorage.setMasterKey(ledgerId, ("ledger-" + ledgerId).getBytes());
                sortedLedgerStorage.setFenced(ledgerId);
            }//from  w ww.j a va2s .co  m
            ByteBuf entry = Unpooled.buffer(128);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId * entriesPerWrite);
            entry.writeBytes(("entry-" + entryId).getBytes());

            sortedLedgerStorage.addEntry(entry);
        }
    }

    sortedLedgerStorage.flush();

    // Insert some more ledger & entries in the interleaved storage
    for (long entryId = numWrites; entryId < moreNumOfWrites; entryId++) {
        for (long ledgerId = 0; ledgerId < numOfLedgers; ledgerId++) {
            ByteBuf entry = Unpooled.buffer(128);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId * entriesPerWrite);
            entry.writeBytes(("entry-" + entryId).getBytes());

            sortedLedgerStorage.addEntry(entry);
        }
    }

    for (long ledgerId = 0; ledgerId < numOfLedgers; ledgerId++) {
        OfLong entriesOfLedger = sortedLedgerStorage.getListOfEntriesOfLedger(ledgerId);
        ArrayList<Long> arrayList = new ArrayList<Long>();
        Consumer<Long> addMethod = arrayList::add;
        entriesOfLedger.forEachRemaining(addMethod);
        assertEquals("Number of entries", moreNumOfWrites, arrayList.size());
        assertTrue("Entries of Ledger", IntStream.range(0, arrayList.size()).allMatch(i -> {
            return arrayList.get(i).longValue() == (i * entriesPerWrite);
        }));
    }
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.ConversionRollbackTest.java

License:Apache License

@Test
public void convertFromDbStorageToInterleaved() throws Exception {
    File tmpDir = File.createTempFile("bkTest", ".dir");
    tmpDir.delete();/*from w w w .ja v  a 2 s  .  c om*/
    tmpDir.mkdir();
    File curDir = Bookie.getCurrentDirectory(tmpDir);
    Bookie.checkDirectoryStructure(curDir);

    log.info("Using temp directory: {}", tmpDir);

    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setLedgerDirNames(new String[] { tmpDir.toString() });
    LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(),
            new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold()));

    DbLedgerStorage dbStorage = new DbLedgerStorage();
    dbStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource, checkpointer,
            NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    // Insert some ledger & entries in the dbStorage
    for (long ledgerId = 0; ledgerId < 5; ledgerId++) {
        dbStorage.setMasterKey(ledgerId, ("ledger-" + ledgerId).getBytes());
        dbStorage.setFenced(ledgerId);

        for (long entryId = 0; entryId < 10000; entryId++) {
            ByteBuf entry = Unpooled.buffer(128);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId);
            entry.writeBytes(("entry-" + entryId).getBytes());

            dbStorage.addEntry(entry);
        }
    }

    dbStorage.flush();
    dbStorage.shutdown();

    // Run conversion tool
    BookieShell shell = new BookieShell();
    shell.setConf(conf);
    int res = shell.run(new String[] { "convert-to-interleaved-storage" });

    Assert.assertEquals(0, res);

    // Verify that interleaved storage index has the same entries
    InterleavedLedgerStorage interleavedStorage = new InterleavedLedgerStorage();
    interleavedStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource,
            checkpointer, NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    Set<Long> ledgers = Sets.newTreeSet(interleavedStorage.getActiveLedgersInRange(0, Long.MAX_VALUE));
    Assert.assertEquals(Sets.newTreeSet(Lists.newArrayList(0L, 1L, 2L, 3L, 4L)), ledgers);

    for (long ledgerId = 0; ledgerId < 5; ledgerId++) {
        Assert.assertEquals(true, interleavedStorage.isFenced(ledgerId));
        Assert.assertEquals("ledger-" + ledgerId, new String(interleavedStorage.readMasterKey(ledgerId)));

        for (long entryId = 0; entryId < 10000; entryId++) {
            ByteBuf entry = Unpooled.buffer(1024);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId);
            entry.writeBytes(("entry-" + entryId).getBytes());

            ByteBuf result = interleavedStorage.getEntry(ledgerId, entryId);
            Assert.assertEquals(entry, result);
        }
    }

    interleavedStorage.shutdown();
    FileUtils.forceDelete(tmpDir);
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.ConversionTest.java

License:Apache License

@Test
public void test() throws Exception {
    File tmpDir = File.createTempFile("bkTest", ".dir");
    tmpDir.delete();// w ww  .ja v  a 2s .c om
    tmpDir.mkdir();
    File curDir = Bookie.getCurrentDirectory(tmpDir);
    Bookie.checkDirectoryStructure(curDir);

    System.out.println(tmpDir);

    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setLedgerDirNames(new String[] { tmpDir.toString() });
    LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(),
            new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold()));

    InterleavedLedgerStorage interleavedStorage = new InterleavedLedgerStorage();
    interleavedStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource,
            checkpointer, NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    // Insert some ledger & entries in the interleaved storage
    for (long ledgerId = 0; ledgerId < 5; ledgerId++) {
        interleavedStorage.setMasterKey(ledgerId, ("ledger-" + ledgerId).getBytes());
        interleavedStorage.setFenced(ledgerId);

        for (long entryId = 0; entryId < 10000; entryId++) {
            ByteBuf entry = Unpooled.buffer(128);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId);
            entry.writeBytes(("entry-" + entryId).getBytes());

            interleavedStorage.addEntry(entry);
        }
    }

    interleavedStorage.flush();
    interleavedStorage.shutdown();

    // Run conversion tool
    BookieShell shell = new BookieShell();
    shell.setConf(conf);
    int res = shell.run(new String[] { "convert-to-db-storage" });

    Assert.assertEquals(0, res);

    // Verify that db index has the same entries
    DbLedgerStorage dbStorage = new DbLedgerStorage();
    dbStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource, checkpointer,
            NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    interleavedStorage = new InterleavedLedgerStorage();
    interleavedStorage.initialize(conf, null, ledgerDirsManager, ledgerDirsManager, null, checkpointSource,
            checkpointer, NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT);

    Set<Long> ledgers = Sets.newTreeSet(dbStorage.getActiveLedgersInRange(0, Long.MAX_VALUE));
    Assert.assertEquals(Sets.newTreeSet(Lists.newArrayList(0L, 1L, 2L, 3L, 4L)), ledgers);

    ledgers = Sets.newTreeSet(interleavedStorage.getActiveLedgersInRange(0, Long.MAX_VALUE));
    Assert.assertEquals(Sets.newTreeSet(), ledgers);

    for (long ledgerId = 0; ledgerId < 5; ledgerId++) {
        Assert.assertEquals(true, dbStorage.isFenced(ledgerId));
        Assert.assertEquals("ledger-" + ledgerId, new String(dbStorage.readMasterKey(ledgerId)));

        for (long entryId = 0; entryId < 10000; entryId++) {
            ByteBuf entry = Unpooled.buffer(1024);
            entry.writeLong(ledgerId);
            entry.writeLong(entryId);
            entry.writeBytes(("entry-" + entryId).getBytes());

            ByteBuf result = dbStorage.getEntry(ledgerId, entryId);
            Assert.assertEquals(entry, result);
            result.release();

            try {
                interleavedStorage.getEntry(ledgerId, entryId);
                Assert.fail("entry should not exist");
            } catch (NoLedgerException e) {
                // Ok
            }
        }
    }

    interleavedStorage.shutdown();
    dbStorage.shutdown();
    FileUtils.forceDelete(tmpDir);
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageTest.java

License:Apache License

@Test
public void simple() throws Exception {
    assertEquals(false, storage.ledgerExists(3));
    try {// w  ww  . j  av  a 2 s .  c  om
        storage.isFenced(3);
        fail("should have failed");
    } catch (Bookie.NoLedgerException nle) {
        // OK
    }
    assertEquals(false, storage.ledgerExists(3));
    try {
        storage.setFenced(3);
        fail("should have failed");
    } catch (Bookie.NoLedgerException nle) {
        // OK
    }
    storage.setMasterKey(3, "key".getBytes());
    try {
        storage.setMasterKey(3, "other-key".getBytes());
        fail("should have failed");
    } catch (IOException ioe) {
        assertTrue(ioe.getCause() instanceof BookieException.BookieIllegalOpException);
    }
    // setting the same key is NOOP
    storage.setMasterKey(3, "key".getBytes());
    assertEquals(true, storage.ledgerExists(3));
    assertEquals(true, storage.setFenced(3));
    assertEquals(true, storage.isFenced(3));
    assertEquals(false, storage.setFenced(3));

    storage.setMasterKey(4, "key".getBytes());
    assertEquals(false, storage.isFenced(4));
    assertEquals(true, storage.ledgerExists(4));

    assertEquals("key", new String(storage.readMasterKey(4)));

    assertEquals(Lists.newArrayList(4L, 3L), Lists.newArrayList(storage.getActiveLedgersInRange(0, 100)));
    assertEquals(Lists.newArrayList(4L, 3L), Lists.newArrayList(storage.getActiveLedgersInRange(3, 100)));
    assertEquals(Lists.newArrayList(3L), Lists.newArrayList(storage.getActiveLedgersInRange(0, 4)));

    // Add / read entries
    ByteBuf entry = Unpooled.buffer(1024);
    entry.writeLong(4); // ledger id
    entry.writeLong(1); // entry id
    entry.writeLong(0); // lac
    entry.writeBytes("entry-1".getBytes());

    assertEquals(false, ((DbLedgerStorage) storage).isFlushRequired());

    assertEquals(1, storage.addEntry(entry));

    assertEquals(true, ((DbLedgerStorage) storage).isFlushRequired());

    // Read from write cache
    ByteBuf res = storage.getEntry(4, 1);
    assertEquals(entry, res);

    storage.flush();

    assertEquals(false, ((DbLedgerStorage) storage).isFlushRequired());

    // Read from db
    res = storage.getEntry(4, 1);
    assertEquals(entry, res);

    try {
        storage.getEntry(4, 2);
        fail("Should have thrown exception");
    } catch (NoEntryException e) {
        // ok
    }

    ByteBuf entry2 = Unpooled.buffer(1024);
    entry2.writeLong(4); // ledger id
    entry2.writeLong(2); // entry id
    entry2.writeLong(1); // lac
    entry2.writeBytes("entry-2".getBytes());

    storage.addEntry(entry2);

    // Read last entry in ledger
    res = storage.getEntry(4, BookieProtocol.LAST_ADD_CONFIRMED);
    assertEquals(entry2, res);

    // Read last add confirmed in ledger
    assertEquals(1L, storage.getLastAddConfirmed(4));

    ByteBuf entry3 = Unpooled.buffer(1024);
    entry3.writeLong(4); // ledger id
    entry3.writeLong(3); // entry id
    entry3.writeLong(2); // lac
    entry3.writeBytes("entry-3".getBytes());
    storage.addEntry(entry3);

    ByteBuf entry4 = Unpooled.buffer(1024);
    entry4.writeLong(4); // ledger id
    entry4.writeLong(4); // entry id
    entry4.writeLong(3); // lac
    entry4.writeBytes("entry-4".getBytes());
    storage.addEntry(entry4);

    res = storage.getEntry(4, 4);
    assertEquals(entry4, res);

    assertEquals(3, storage.getLastAddConfirmed(4));

    // Delete
    assertEquals(true, storage.ledgerExists(4));
    storage.deleteLedger(4);
    assertEquals(false, storage.ledgerExists(4));

    // Should not throw exception event if the ledger was deleted
    storage.getEntry(4, 4);
    assertEquals(3, storage.getLastAddConfirmed(4));

    storage.addEntry(Unpooled.wrappedBuffer(entry2));
    res = storage.getEntry(4, BookieProtocol.LAST_ADD_CONFIRMED);
    assertEquals(entry4, res);
    assertEquals(3, storage.getLastAddConfirmed(4));

    // Get last entry from storage
    storage.flush();

    try {
        storage.getEntry(4, 4);
        fail("Should have thrown exception since the ledger was deleted");
    } catch (NoEntryException e) {
        // ok
    }
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageTest.java

License:Apache License

@Test
public void testBookieCompaction() throws Exception {
    storage.setMasterKey(4, "key".getBytes());

    ByteBuf entry3 = Unpooled.buffer(1024);
    entry3.writeLong(4); // ledger id
    entry3.writeLong(3); // entry id
    entry3.writeBytes("entry-3".getBytes());
    storage.addEntry(entry3);//ww w.j  ava2  s .c om

    // Simulate bookie compaction
    SingleDirectoryDbLedgerStorage singleDirStorage = ((DbLedgerStorage) storage).getLedgerStorageList().get(0);
    EntryLogger entryLogger = singleDirStorage.getEntryLogger();
    // Rewrite entry-3
    ByteBuf newEntry3 = Unpooled.buffer(1024);
    newEntry3.writeLong(4); // ledger id
    newEntry3.writeLong(3); // entry id
    newEntry3.writeBytes("new-entry-3".getBytes());
    long location = entryLogger.addEntry(4L, newEntry3, false);

    List<EntryLocation> locations = Lists.newArrayList(new EntryLocation(4, 3, location));
    singleDirStorage.updateEntriesLocations(locations);

    ByteBuf res = storage.getEntry(4, 3);
    System.out.println("res:       " + ByteBufUtil.hexDump(res));
    System.out.println("newEntry3: " + ByteBufUtil.hexDump(newEntry3));
    assertEquals(newEntry3, res);
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageTest.java

License:Apache License

@Test
public void testRewritingEntries() throws Exception {
    storage.setMasterKey(1, "key".getBytes());

    try {/*from   ww  w .  j  ava 2  s.com*/
        storage.getEntry(1, -1);
        fail("Should throw exception");
    } catch (Bookie.NoEntryException e) {
        // ok
    }

    ByteBuf entry1 = Unpooled.buffer(1024);
    entry1.writeLong(1); // ledger id
    entry1.writeLong(1); // entry id
    entry1.writeBytes("entry-1".getBytes());

    storage.addEntry(entry1);
    storage.flush();

    ByteBuf newEntry1 = Unpooled.buffer(1024);
    newEntry1.writeLong(1); // ledger id
    newEntry1.writeLong(1); // entry id
    newEntry1.writeBytes("new-entry-1".getBytes());

    storage.addEntry(newEntry1);
    storage.flush();

    ByteBuf response = storage.getEntry(1, 1);
    assertEquals(newEntry1, response);
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageTest.java

License:Apache License

@Test
public void testEntriesOutOfOrder() throws Exception {
    storage.setMasterKey(1, "key".getBytes());

    ByteBuf entry2 = Unpooled.buffer(1024);
    entry2.writeLong(1); // ledger id
    entry2.writeLong(2); // entry id
    entry2.writeBytes("entry-2".getBytes());

    storage.addEntry(entry2);/*from   w w w  . j  av  a  2s  . c om*/

    try {
        storage.getEntry(1, 1);
        fail("Entry doesn't exist");
    } catch (NoEntryException e) {
        // Ok, entry doesn't exist
    }

    ByteBuf res = storage.getEntry(1, 2);
    assertEquals(entry2, res);

    ByteBuf entry1 = Unpooled.buffer(1024);
    entry1.writeLong(1); // ledger id
    entry1.writeLong(1); // entry id
    entry1.writeBytes("entry-1".getBytes());

    storage.addEntry(entry1);

    res = storage.getEntry(1, 1);
    assertEquals(entry1, res);

    res = storage.getEntry(1, 2);
    assertEquals(entry2, res);

    storage.flush();

    res = storage.getEntry(1, 1);
    assertEquals(entry1, res);

    res = storage.getEntry(1, 2);
    assertEquals(entry2, res);
}