List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:org.apache.activemq.artemis.utils.UTF8Util.java
License:Apache License
public static String readUTF(final ActiveMQBuffer input) { StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer(); final int size = input.readUnsignedShort(); if (UTF8Util.isTrace) { // This message is too verbose for debug, that's why we are using trace here ActiveMQUtilLogger.LOGGER.trace("Reading string with utfSize=" + size); }//w w w . j av a2s . c om if (PlatformDependent.hasUnsafe() && input.byteBuf() != null && input.byteBuf().hasMemoryAddress()) { final ByteBuf byteBuf = input.byteBuf(); final long addressBytes = byteBuf.memoryAddress(); final int index = byteBuf.readerIndex(); byteBuf.skipBytes(size); final char[] chars = buffer.borrowCharBuffer(size); return unsafeOffHeapReadUTF(addressBytes, index, chars, size); } final byte[] bytes; final int index; if (input.byteBuf() != null && input.byteBuf().hasArray()) { final ByteBuf byteBuf = input.byteBuf(); bytes = byteBuf.array(); index = byteBuf.arrayOffset() + byteBuf.readerIndex(); byteBuf.skipBytes(size); } else { bytes = buffer.borrowByteBuffer(size); index = 0; input.readBytes(bytes, 0, size); } final char[] chars = buffer.borrowCharBuffer(size); if (PlatformDependent.hasUnsafe()) { return unsafeOnHeapReadUTF(bytes, index, chars, size); } else { return readUTF(bytes, index, chars, size); } }
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 w w .ja v a 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 {//from ww w . j a v a 2s . c om 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 w ww . j a v a 2 s . com * @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 w ww.ja va 2s . com*/ @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.BufferedChannel.java
License:Apache License
/** * Write all the data in src to the {@link FileChannel}. Note that this function can * buffer or re-order writes based on the implementation. These writes will be flushed * to the disk only when flush() is invoked. * * @param src The source ByteBuffer which contains the data to be written. * @throws IOException if a write operation fails. *///w w w . j a va 2 s . c o m public void write(ByteBuf src) throws IOException { int copied = 0; boolean shouldForceWrite = false; synchronized (this) { int len = src.readableBytes(); while (copied < len) { int bytesToCopy = Math.min(src.readableBytes() - copied, writeBuffer.writableBytes()); writeBuffer.writeBytes(src, src.readerIndex() + copied, bytesToCopy); copied += bytesToCopy; // if we have run out of buffer space, we should flush to the // file if (!writeBuffer.isWritable()) { flush(); } } position.addAndGet(copied); unpersistedBytes.addAndGet(copied); if (doRegularFlushes) { if (unpersistedBytes.get() >= unpersistedBytesBound) { flush(); shouldForceWrite = true; } } } if (shouldForceWrite) { forceWrite(false); } }
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 www . j a v a 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.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.Journal.java
License:Apache License
/** * record an add entry operation in journal. *///ww w. j a va 2 s . c o m public void logAddEntry(ByteBuf entry, boolean ackBeforeSync, WriteCallback cb, Object ctx) throws InterruptedException { long ledgerId = entry.getLong(entry.readerIndex() + 0); long entryId = entry.getLong(entry.readerIndex() + 8); logAddEntry(ledgerId, entryId, entry, ackBeforeSync, cb, ctx); }
From source file:org.apache.bookkeeper.bookie.LedgerDescriptorImpl.java
License:Apache License
@Override long addEntry(ByteBuf entry) throws IOException, BookieException { long ledgerId = entry.getLong(entry.readerIndex()); if (ledgerId != this.ledgerId) { throw new IOException("Entry for ledger " + ledgerId + " was sent to " + this.ledgerId); }//from w w w. j av a 2 s. co m return ledgerStorage.addEntry(entry); }