Example usage for io.netty.buffer ByteBuf getLong

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

Introduction

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

Prototype

public abstract long getLong(int index);

Source Link

Document

Gets a 64-bit long integer at the specified absolute index in this buffer.

Usage

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Interval binaryDecodeINTERVAL(int index, int len, ByteBuf buff) {
    Duration duration = Duration.of(buff.getLong(index), ChronoUnit.MICROS);
    final long hours = duration.toHours();
    duration = duration.minusHours(hours);
    final long minutes = duration.toMinutes();
    duration = duration.minusMinutes(minutes);
    final long seconds = NANOSECONDS.toSeconds(duration.toNanos());
    duration = duration.minusSeconds(seconds);
    final long microseconds = NANOSECONDS.toMicros(duration.toNanos());
    int days = buff.getInt(index + 8);
    int months = buff.getInt(index + 12);
    Period monthYear = Period.of(0, months, days).normalized();
    return new Interval(monthYear.getYears(), monthYear.getMonths(), monthYear.getDays(), (int) hours,
            (int) minutes, (int) seconds, (int) microseconds);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static UUID binaryDecodeUUID(int index, int len, ByteBuf buff) {
    return new UUID(buff.getLong(index), buff.getLong(index + 8));
}

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

License:Apache License

/**
 * Retrieve the ledger descriptor for the ledger which entry should be added to.
 * The LedgerDescriptor returned from this method should be eventually freed with
 * #putHandle()./*from   w  ww. j ava 2  s  .c om*/
 *
 * @throws BookieException if masterKey does not match the master key of the ledger
 */
@VisibleForTesting
LedgerDescriptor getLedgerForEntry(ByteBuf entry, final byte[] masterKey) throws IOException, BookieException {
    final long ledgerId = entry.getLong(entry.readerIndex());

    return handles.getHandle(ledgerId, masterKey);
}

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

License:Apache License

public void setExplicitLac(ByteBuf entry, WriteCallback writeCallback, Object ctx, byte[] masterKey)
        throws IOException, InterruptedException, BookieException {
    try {/*  w  w w  . j a  v a 2s .  c o  m*/
        long ledgerId = entry.getLong(entry.readerIndex());
        LedgerDescriptor handle = handles.getHandle(ledgerId, masterKey);
        synchronized (handle) {
            entry.markReaderIndex();
            handle.setExplicitLac(entry);
            entry.resetReaderIndex();
            ByteBuf explicitLACEntry = createExplicitLACEntry(ledgerId, entry);
            getJournal(ledgerId).logAddEntry(explicitLACEntry, false /* ackBeforeSync */, writeCallback, ctx);
        }
    } catch (NoWritableLedgerDirException e) {
        stateManager.transitionToReadOnlyMode();
        throw new IOException(e);
    }
}

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

License:Apache License

/**
 * Scan over an entry log file for a particular entry.
 *
 * @param logId Entry Log File id./*from   ww  w  .j  a v a2  s  .c  om*/
 * @param ledgerId id of the ledger
 * @param entryId entryId of the ledger we are looking for (-1 for all of the entries of the ledger)
 * @param printMsg Whether printing the entry data.
 * @throws Exception
 */
protected void scanEntryLogForSpecificEntry(long logId, final long ledgerId, final long entryId,
        final boolean printMsg) throws Exception {
    System.out.println("Scan entry log " + logId + " (" + Long.toHexString(logId) + ".log)" + " for LedgerId "
            + ledgerId + ((entryId == -1) ? "" : " for EntryId " + entryId));
    final MutableBoolean entryFound = new MutableBoolean(false);
    scanEntryLog(logId, new EntryLogScanner() {
        @Override
        public boolean accept(long candidateLedgerId) {
            return ((candidateLedgerId == ledgerId) && ((!entryFound.booleanValue()) || (entryId == -1)));
        }

        @Override
        public void process(long candidateLedgerId, long startPos, ByteBuf entry) {
            long entrysLedgerId = entry.getLong(entry.readerIndex());
            long entrysEntryId = entry.getLong(entry.readerIndex() + 8);
            if ((candidateLedgerId == entrysLedgerId) && (candidateLedgerId == ledgerId)
                    && ((entrysEntryId == entryId) || (entryId == -1))) {
                entryFound.setValue(true);
                formatEntry(startPos, entry, printMsg);
            }
        }
    });
    if (!entryFound.booleanValue()) {
        System.out.println("LedgerId " + ledgerId + ((entryId == -1) ? "" : " EntryId " + entryId)
                + " is not available in the entry log " + logId + " (" + Long.toHexString(logId) + ".log)");
    }
}

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

License:Apache License

/**
 * test that Bookie calls correctly Journal.logAddEntry about "ackBeforeSync" parameter.
 *//*from  ww w . j a  v  a 2 s  .c om*/
@Test
public void testJournalLogAddEntryCalledCorrectly() throws Exception {

    File journalDir = tempDir.newFolder();
    Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
    File ledgerDir = tempDir.newFolder();
    Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setJournalDirName(journalDir.getPath()).setLedgerDirNames(new String[] { ledgerDir.getPath() })
            .setMetadataServiceUri(null);
    BookieSocketAddress bookieAddress = Bookie.getBookieAddress(conf);
    CountDownLatch journalJoinLatch = new CountDownLatch(1);
    Journal journal = mock(Journal.class);
    MutableBoolean effectiveAckBeforeSync = new MutableBoolean(false);
    doAnswer((Answer) (InvocationOnMock iom) -> {
        ByteBuf entry = iom.getArgument(0);
        long ledgerId = entry.getLong(entry.readerIndex() + 0);
        long entryId = entry.getLong(entry.readerIndex() + 8);
        boolean ackBeforeSync = iom.getArgument(1);
        WriteCallback callback = iom.getArgument(2);
        Object ctx = iom.getArgument(3);

        effectiveAckBeforeSync.setValue(ackBeforeSync);
        callback.writeComplete(BKException.Code.OK, ledgerId, entryId, bookieAddress, ctx);
        return null;
    }).when(journal).logAddEntry(any(ByteBuf.class), anyBoolean(), any(WriteCallback.class), any());

    // bookie will continue to work as soon as the journal thread is alive
    doAnswer((Answer) (InvocationOnMock iom) -> {
        journalJoinLatch.await();
        return null;
    }).when(journal).joinThread();

    whenNew(Journal.class).withAnyArguments().thenReturn(journal);

    Bookie b = new Bookie(conf);
    b.start();

    long ledgerId = 1;
    long entryId = 0;
    Object expectedCtx = "foo";
    byte[] masterKey = new byte[64];
    for (boolean ackBeforeSync : new boolean[] { true, false }) {
        CountDownLatch latch = new CountDownLatch(1);
        final ByteBuf data = buildEntry(ledgerId, entryId, -1);
        final long expectedEntryId = entryId;
        b.addEntry(data, ackBeforeSync,
                (int rc, long ledgerId1, long entryId1, BookieSocketAddress addr, Object ctx) -> {
                    assertSame(expectedCtx, ctx);
                    assertEquals(ledgerId, ledgerId1);
                    assertEquals(expectedEntryId, entryId1);
                    latch.countDown();
                }, expectedCtx, masterKey);
        latch.await(30, TimeUnit.SECONDS);
        assertEquals(ackBeforeSync, effectiveAckBeforeSync.booleanValue());
        entryId++;
    }
    // let bookie exit main thread
    journalJoinLatch.countDown();
    b.shutdown();
}

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

License:Apache License

@Before
public void setup() throws Exception {
    File bkDir = testDir.newFolder("dbLedgerStorageCheckpointTest");
    File curDir = Bookie.getCurrentDirectory(bkDir);
    Bookie.checkDirectoryStructure(curDir);

    int gcWaitTime = 1000;
    conf = TestBKConfiguration.newServerConfiguration();
    conf.setGcWaitTime(gcWaitTime);/*from   w w w  .  j  a  va  2  s.c o m*/
    conf.setLedgerStorageClass(InterleavedLedgerStorage.class.getName());
    conf.setJournalDirsName(new String[] { bkDir.toString() });
    conf.setLedgerDirNames(new String[] { bkDir.toString() });
    conf.setEntryLogSizeLimit(10 * 1024);

    bookie = spy(new Bookie(conf));
    bookie.start();

    getLedgerDescCalledLatch = new CountDownLatch(1);
    getLedgerDescWaitLatch = new CountDownLatch(1);

    // spy `getLedgerForEntry`
    doAnswer(invocationOnMock -> {
        ByteBuf entry = invocationOnMock.getArgument(0);
        long ledgerId = entry.getLong(entry.readerIndex());

        LedgerDescriptor ld = (LedgerDescriptor) invocationOnMock.callRealMethod();

        if (ledgerId % 2 == 1) {
            getLedgerDescCalledLatch.countDown();
            getLedgerDescWaitLatch.await();
        }

        return ld;
    }).when(bookie).getLedgerForEntry(any(ByteBuf.class), any(byte[].class));
}

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

License:Apache License

private EntryLogEntry getFCForEntryInternal(long ledgerId, long entryId, long entryLogId, long pos)
        throws EntryLookupException, IOException {
    ByteBuf sizeBuff = sizeBuffer.get();
    sizeBuff.clear();//from   w  ww.  j a  v a 2 s. com
    pos -= 4; // we want to get the entrySize as well as the ledgerId and entryId
    BufferedReadChannel fc;
    try {
        fc = getChannelForLogId(entryLogId);
    } catch (FileNotFoundException e) {
        throw new EntryLookupException.MissingLogFileException(ledgerId, entryId, entryLogId, pos);
    }

    try {
        if (readFromLogChannel(entryLogId, fc, sizeBuff, pos) != sizeBuff.capacity()) {
            throw new EntryLookupException.MissingEntryException(ledgerId, entryId, entryLogId, pos);
        }
    } catch (BufferedChannelBase.BufferedChannelClosedException | AsynchronousCloseException e) {
        throw new EntryLookupException.MissingLogFileException(ledgerId, entryId, entryLogId, pos);
    }
    pos += 4;
    int entrySize = sizeBuff.readInt();

    // entrySize does not include the ledgerId
    if (entrySize > maxSaneEntrySize) {
        LOG.warn("Sanity check failed for entry size of " + entrySize + " at location " + pos + " in "
                + entryLogId);
    }
    if (entrySize < MIN_SANE_ENTRY_SIZE) {
        LOG.error("Read invalid entry length {}", entrySize);
        throw new EntryLookupException.InvalidEntryLengthException(ledgerId, entryId, entryLogId, pos);
    }

    long thisLedgerId = sizeBuff.getLong(4);
    long thisEntryId = sizeBuff.getLong(12);
    if (thisLedgerId != ledgerId || thisEntryId != entryId) {
        throw new EntryLookupException.WrongEntryException(thisEntryId, thisLedgerId, ledgerId, entryId,
                entryLogId, pos);
    }
    return new EntryLogEntry(entrySize, fc);
}

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

License:Apache License

@Override
public long addEntry(ByteBuf entry) throws IOException {
    long ledgerId = entry.getLong(entry.readerIndex() + 0);
    long entryId = entry.getLong(entry.readerIndex() + 8);
    long lac = entry.getLong(entry.readerIndex() + 16);

    processEntry(ledgerId, entryId, entry);

    ledgerCache.updateLastAddConfirmed(ledgerId, lac);
    return entryId;
}

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

License:Apache License

public void initiate(boolean dryRun) throws IOException {
    LOG.info("Starting index rebuilding");

    DiskChecker diskChecker = Bookie.createDiskChecker(conf);
    LedgerDirsManager ledgerDirsManager = Bookie.createLedgerDirsManager(conf, diskChecker,
            NullStatsLogger.INSTANCE);/*from  w w  w  . ja v a2s  .  c  om*/
    LedgerDirsManager indexDirsManager = Bookie.createIndexDirsManager(conf, diskChecker,
            NullStatsLogger.INSTANCE, ledgerDirsManager);
    EntryLogger entryLogger = new EntryLogger(conf, ledgerDirsManager);
    final LedgerCache ledgerCache;
    if (dryRun) {
        ledgerCache = new DryRunLedgerCache();
    } else {
        ledgerCache = new LedgerCacheImpl(conf, new SnapshotMap<Long, Boolean>(), indexDirsManager,
                NullStatsLogger.INSTANCE);
    }

    Set<Long> entryLogs = entryLogger.getEntryLogsSet();

    int totalEntryLogs = entryLogs.size();
    int completedEntryLogs = 0;
    long startTime = System.nanoTime();

    LOG.info("Scanning {} entry logs", totalEntryLogs);

    Map<Long, RecoveryStats> stats = new HashMap<>();
    for (long entryLogId : entryLogs) {
        LOG.info("Scanning {}", entryLogId);
        entryLogger.scanEntryLog(entryLogId, new EntryLogScanner() {
            @Override
            public void process(long ledgerId, long offset, ByteBuf entry) throws IOException {
                long entryId = entry.getLong(8);

                stats.computeIfAbsent(ledgerId, (ignore) -> new RecoveryStats()).registerEntry(entryId);

                // Actual location indexed is pointing past the entry size
                long location = (entryLogId << 32L) | (offset + 4);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Rebuilding {}:{} at location {} / {}", ledgerId, entryId, location >> 32,
                            location & (Integer.MAX_VALUE - 1));
                }

                if (!ledgerCache.ledgerExists(ledgerId)) {
                    ledgerCache.setMasterKey(ledgerId, masterKey);
                    ledgerCache.setFenced(ledgerId);
                }
                ledgerCache.putEntryOffset(ledgerId, entryId, location);
            }

            @Override
            public boolean accept(long ledgerId) {
                return ledgerIds.contains(ledgerId);
            }
        });

        ledgerCache.flushLedger(true);

        ++completedEntryLogs;
        LOG.info("Completed scanning of log {}.log -- {} / {}", Long.toHexString(entryLogId),
                completedEntryLogs, totalEntryLogs);
    }

    LOG.info("Rebuilding indices done");
    for (long ledgerId : ledgerIds) {
        RecoveryStats ledgerStats = stats.get(ledgerId);
        if (ledgerStats == null || ledgerStats.getNumEntries() == 0) {
            LOG.info(" {} - No entries found", ledgerId);
        } else {
            LOG.info(" {} - Found {} entries, from {} to {}", ledgerId, ledgerStats.getNumEntries(),
                    ledgerStats.getFirstEntry(), ledgerStats.getLastEntry());
        }
    }
    LOG.info("Total time: {}", DurationFormatUtils
            .formatDurationHMS(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)));
}