Package org.apache.bookkeeper.bookie
Class DefaultEntryLogger
- java.lang.Object
-
- org.apache.bookkeeper.bookie.DefaultEntryLogger
-
- All Implemented Interfaces:
java.lang.AutoCloseable,EntryLogger
- Direct Known Subclasses:
ReadOnlyDefaultEntryLogger
public class DefaultEntryLogger extends java.lang.Object implements EntryLogger
This class manages the writing of the bookkeeper entries. All the new entries are written to a common log. The LedgerCache will have pointers into files created by this class with offsets into the files to find the actual ledger entry. The entry log files created by this class are identified by a long.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classDefaultEntryLogger.BufferedLogChannel(package private) static interfaceDefaultEntryLogger.EntryLogListenerEntry Log Listener.(package private) static classDefaultEntryLogger.EntryLookupExceptionException type for representing lookup errors.(package private) static classDefaultEntryLogger.RecentEntryLogsStatusDatastructure which maintains the status of logchannels.
-
Field Summary
Fields Modifier and Type Field Description (package private) ServerConfigurationconf(package private) EntryLoggerAllocatorentryLoggerAllocator(package private) static intHEADER_CURRENT_VERSION(package private) static intHEADER_VERSION_POSITION(package private) static longINVALID_LID(package private) static longLEDGERS_MAP_ENTRY_ID(package private) static intLEDGERS_MAP_ENTRY_SIZE(package private) static intLEDGERS_MAP_HEADER_SIZELedgers map is composed of multiple parts that can be split into separated entries.(package private) static intLEDGERS_MAP_MAX_BATCH_SIZE(package private) static intLEDGERS_MAP_OFFSET_POSITION(package private) static intLOGFILE_HEADER_SIZEThe 1K block at the head of the entry logger file that contains the fingerprint and meta-data.(package private) static longMB(package private) static intMIN_SANE_ENTRY_SIZE(package private) DefaultEntryLogger.RecentEntryLogsStatusrecentlyCreatedEntryLogsStatus(package private) static intUNINITIALIZED_LOG_ID-
Fields inherited from interface org.apache.bookkeeper.bookie.storage.EntryLogger
LOG_FILE_SUFFIX, UNASSIGNED_LEDGERID
-
-
Constructor Summary
Constructors Constructor Description DefaultEntryLogger(ServerConfiguration conf)DefaultEntryLogger(ServerConfiguration conf, LedgerDirsManager ledgerDirsManager)Create an EntryLogger that stores it's log files in the given directories.DefaultEntryLogger(ServerConfiguration conf, LedgerDirsManager ledgerDirsManager, DefaultEntryLogger.EntryLogListener listener, StatsLogger statsLogger, io.netty.buffer.ByteBufAllocator allocator)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description longaddEntry(long ledger, io.netty.buffer.ByteBuf entry)Add an entry for ledger ```ledgerId``` to the entrylog.(package private) longaddEntry(long ledger, io.netty.buffer.ByteBuf entry, boolean rollLog)(package private) longaddEntry(long ledger, java.nio.ByteBuffer entry)(package private) voidaddListener(DefaultEntryLogger.EntryLogListener listener)(package private) voidcheckEntry(long ledgerId, long entryId, long location)(package private) voidcheckpoint()Flushes all rotated log channels.(package private) voidclearCompactingLogId()voidclose()Shutdown method to gracefully stop entry logger.(package private) booleancommitEntryMemTableFlush()(package private) EntryLogMetadataextractEntryLogMetadataFromIndex(long entryLogId)(package private) static longfileName2LogId(java.lang.String fileName)Convert log filename (hex format with suffix) to logId in long.voidflush()Flush any outstanding writes to disk.(package private) BufferedReadChannelgetChannelForLogId(long entryLogId)(package private) EntryLoggerAllocatorgetEntryLoggerAllocator()get EntryLoggerAllocator, Just for tests.(package private) EntryLogManagergetEntryLogManager()EntryLogMetadatagetEntryLogMetadata(long entryLogId, AbstractLogCompactor.Throttler throttler)Retrieve metadata for the given entrylog ID.java.util.Set<java.lang.Long>getEntryLogsSet()Returns a set with the ids of all the entry log files.java.util.Set<java.lang.Long>getFlushedLogIds()Get the log ids for the set of logs which have been completely flushed to disk.BufferedReadChannelgetFromChannels(long logId)(package private) longgetLeastUnflushedLogId()protected LedgerDirsManagergetLedgerDirsManager()(package private) longgetPreviousAllocatedEntryLogId()java.util.Collection<CompactionEntryLog>incompleteCompactionLogs()Return a collection of all the compaction entry logs which have been compacted, but have not been cleaned up.booleanlogExists(long logId)Whether the log file exists or not.(package private) static java.lang.StringlogId2HexString(long logId)Convert log Id to hex string.(package private) static longlogIdForOffset(long offset)CompactionEntryLognewCompactionLog(long logToCompact)Create a new entrylog into which compacted entries can be added.(package private) static longposForOffset(long location)(package private) voidprepareEntryMemTableFlush()(package private) voidprepareSortedLedgerStorageCheckpoint(long numBytesFlushed)BufferedReadChannelputInReadChannels(long logId, BufferedReadChannel bc)Put the logId, bc pair in the map responsible for the current thread.io.netty.buffer.ByteBufreadEntry(long location)Read an entry from an entrylog location.io.netty.buffer.ByteBufreadEntry(long ledgerId, long entryId, long entryLocation)Read an entry from an entrylog location, and verify that is matches the expected ledger and entry ID.booleanremoveEntryLog(long entryLogId)Remove entry log.voidremoveFromChannelsAndClose(long logId)Remove all entries for this log file in each thread's cache.voidscanEntryLog(long entryLogId, EntryLogScanner scanner)Scan entry log.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.bookkeeper.bookie.storage.EntryLogger
getEntryLogMetadata
-
-
-
-
Field Detail
-
UNINITIALIZED_LOG_ID
static final int UNINITIALIZED_LOG_ID
- See Also:
- Constant Field Values
-
recentlyCreatedEntryLogsStatus
final DefaultEntryLogger.RecentEntryLogsStatus recentlyCreatedEntryLogsStatus
-
entryLoggerAllocator
final EntryLoggerAllocator entryLoggerAllocator
-
HEADER_CURRENT_VERSION
static final int HEADER_CURRENT_VERSION
- See Also:
- Constant Field Values
-
LOGFILE_HEADER_SIZE
static final int LOGFILE_HEADER_SIZE
The 1K block at the head of the entry logger file that contains the fingerprint and meta-data.Header is composed of: Fingerprint: 4 bytes "BKLO" Log file HeaderVersion enum: 4 bytes Ledger map offset: 8 bytes Ledgers Count: 4 bytes
- See Also:
- Constant Field Values
-
HEADER_VERSION_POSITION
static final int HEADER_VERSION_POSITION
- See Also:
- Constant Field Values
-
LEDGERS_MAP_OFFSET_POSITION
static final int LEDGERS_MAP_OFFSET_POSITION
- See Also:
- Constant Field Values
-
LEDGERS_MAP_HEADER_SIZE
static final int LEDGERS_MAP_HEADER_SIZE
Ledgers map is composed of multiple parts that can be split into separated entries. Each of them is composed of:length: (4 bytes) [0-3] ledger id (-1): (8 bytes) [4 - 11] entry id: (8 bytes) [12-19] num ledgers stored in current metadata entry: (4 bytes) [20 - 23] ledger entries: sequence of (ledgerid, size) (8 + 8 bytes each) [24..]
- See Also:
- Constant Field Values
-
LEDGERS_MAP_ENTRY_SIZE
static final int LEDGERS_MAP_ENTRY_SIZE
- See Also:
- Constant Field Values
-
LEDGERS_MAP_MAX_BATCH_SIZE
static final int LEDGERS_MAP_MAX_BATCH_SIZE
- See Also:
- Constant Field Values
-
INVALID_LID
static final long INVALID_LID
- See Also:
- Constant Field Values
-
LEDGERS_MAP_ENTRY_ID
static final long LEDGERS_MAP_ENTRY_ID
- See Also:
- Constant Field Values
-
MIN_SANE_ENTRY_SIZE
static final int MIN_SANE_ENTRY_SIZE
- See Also:
- Constant Field Values
-
MB
static final long MB
- See Also:
- Constant Field Values
-
conf
final ServerConfiguration conf
-
-
Constructor Detail
-
DefaultEntryLogger
public DefaultEntryLogger(ServerConfiguration conf) throws java.io.IOException
- Throws:
java.io.IOException
-
DefaultEntryLogger
public DefaultEntryLogger(ServerConfiguration conf, LedgerDirsManager ledgerDirsManager) throws java.io.IOException
Create an EntryLogger that stores it's log files in the given directories.- Throws:
java.io.IOException
-
DefaultEntryLogger
public DefaultEntryLogger(ServerConfiguration conf, LedgerDirsManager ledgerDirsManager, DefaultEntryLogger.EntryLogListener listener, StatsLogger statsLogger, io.netty.buffer.ByteBufAllocator allocator) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
getEntryLogManager
EntryLogManager getEntryLogManager()
-
addListener
void addListener(DefaultEntryLogger.EntryLogListener listener)
-
putInReadChannels
public BufferedReadChannel putInReadChannels(long logId, BufferedReadChannel bc)
Put the logId, bc pair in the map responsible for the current thread.- Parameters:
logId-bc-
-
removeFromChannelsAndClose
public void removeFromChannelsAndClose(long logId)
Remove all entries for this log file in each thread's cache.- Parameters:
logId-
-
getFromChannels
public BufferedReadChannel getFromChannels(long logId)
-
getLeastUnflushedLogId
long getLeastUnflushedLogId()
-
getFlushedLogIds
public java.util.Set<java.lang.Long> getFlushedLogIds()
Description copied from interface:EntryLoggerGet the log ids for the set of logs which have been completely flushed to disk. Only log ids in this set are considered for either compaction or garbage collection.- Specified by:
getFlushedLogIdsin interfaceEntryLogger
-
getPreviousAllocatedEntryLogId
long getPreviousAllocatedEntryLogId()
-
prepareSortedLedgerStorageCheckpoint
void prepareSortedLedgerStorageCheckpoint(long numBytesFlushed) throws java.io.IOException- Throws:
java.io.IOException
-
prepareEntryMemTableFlush
void prepareEntryMemTableFlush()
-
commitEntryMemTableFlush
boolean commitEntryMemTableFlush() throws java.io.IOException- Throws:
java.io.IOException
-
getEntryLoggerAllocator
EntryLoggerAllocator getEntryLoggerAllocator()
get EntryLoggerAllocator, Just for tests.
-
removeEntryLog
public boolean removeEntryLog(long entryLogId)
Remove entry log.- Specified by:
removeEntryLogin interfaceEntryLogger- Parameters:
entryLogId- Entry Log File Id- Returns:
- false if the entrylog doesn't exist.
-
clearCompactingLogId
void clearCompactingLogId()
-
checkpoint
void checkpoint() throws java.io.IOExceptionFlushes all rotated log channels. After log channels are flushed, move leastUnflushedLogId ptr to current logId.- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOExceptionDescription copied from interface:EntryLoggerFlush any outstanding writes to disk.- Specified by:
flushin interfaceEntryLogger- Throws:
java.io.IOException
-
addEntry
long addEntry(long ledger, java.nio.ByteBuffer entry) throws java.io.IOException- Throws:
java.io.IOException
-
addEntry
long addEntry(long ledger, io.netty.buffer.ByteBuf entry, boolean rollLog) throws java.io.IOException- Throws:
java.io.IOException
-
addEntry
public long addEntry(long ledger, io.netty.buffer.ByteBuf entry) throws java.io.IOExceptionDescription copied from interface:EntryLoggerAdd an entry for ledger ```ledgerId``` to the entrylog.- Specified by:
addEntryin interfaceEntryLogger- Parameters:
ledger- the ledger for which the entry is being addedentry- the contents of the entry (this method does not take ownership of the refcount)- Returns:
- the location in the entry log of the added entry
- Throws:
java.io.IOException
-
logIdForOffset
static long logIdForOffset(long offset)
-
posForOffset
static long posForOffset(long location)
-
checkEntry
void checkEntry(long ledgerId, long entryId, long location) throws DefaultEntryLogger.EntryLookupException, java.io.IOException- Throws:
DefaultEntryLogger.EntryLookupExceptionjava.io.IOException
-
readEntry
public io.netty.buffer.ByteBuf readEntry(long ledgerId, long entryId, long entryLocation) throws java.io.IOException, Bookie.NoEntryExceptionDescription copied from interface:EntryLoggerRead an entry from an entrylog location, and verify that is matches the expected ledger and entry ID.- Specified by:
readEntryin interfaceEntryLogger- Parameters:
ledgerId- the ledgerID to matchentryId- the entryID to matchentryLocation- the location from which to read the entry- Returns:
- the entry
- Throws:
java.io.IOExceptionBookie.NoEntryException
-
readEntry
public io.netty.buffer.ByteBuf readEntry(long location) throws java.io.IOException, Bookie.NoEntryExceptionDescription copied from interface:EntryLoggerRead an entry from an entrylog location.- Specified by:
readEntryin interfaceEntryLogger- Parameters:
location- the location from which to read the entry- Returns:
- the entry
- Throws:
java.io.IOExceptionBookie.NoEntryException
-
getChannelForLogId
BufferedReadChannel getChannelForLogId(long entryLogId) throws java.io.IOException
- Throws:
java.io.IOException
-
logExists
public boolean logExists(long logId)
Whether the log file exists or not.- Specified by:
logExistsin interfaceEntryLogger
-
getEntryLogsSet
public java.util.Set<java.lang.Long> getEntryLogsSet() throws java.io.IOExceptionReturns a set with the ids of all the entry log files.- Throws:
java.io.IOException
-
scanEntryLog
public void scanEntryLog(long entryLogId, EntryLogScanner scanner) throws java.io.IOExceptionScan entry log.- Specified by:
scanEntryLogin interfaceEntryLogger- Parameters:
entryLogId- Entry Log Idscanner- Entry Log Scanner- Throws:
java.io.IOException
-
getEntryLogMetadata
public EntryLogMetadata getEntryLogMetadata(long entryLogId, AbstractLogCompactor.Throttler throttler) throws java.io.IOException
Description copied from interface:EntryLoggerRetrieve metadata for the given entrylog ID. The metadata contains the size of the log, the size of the data in the log which is still active, and a list of all the ledgers contained in the log and the size of the data stored for each ledger.- Specified by:
getEntryLogMetadatain interfaceEntryLogger- Throws:
java.io.IOException
-
extractEntryLogMetadataFromIndex
EntryLogMetadata extractEntryLogMetadataFromIndex(long entryLogId) throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close()
Shutdown method to gracefully stop entry logger.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceEntryLogger
-
getLedgerDirsManager
protected LedgerDirsManager getLedgerDirsManager()
-
fileName2LogId
static long fileName2LogId(java.lang.String fileName)
Convert log filename (hex format with suffix) to logId in long.
-
logId2HexString
static java.lang.String logId2HexString(long logId)
Convert log Id to hex string.
-
newCompactionLog
public CompactionEntryLog newCompactionLog(long logToCompact) throws java.io.IOException
Description copied from interface:EntryLoggerCreate a new entrylog into which compacted entries can be added. There is a 1-1 mapping between logs that are being compacted and the log the compacted entries are written to.- Specified by:
newCompactionLogin interfaceEntryLogger- Throws:
java.io.IOException
-
incompleteCompactionLogs
public java.util.Collection<CompactionEntryLog> incompleteCompactionLogs()
Description copied from interface:EntryLoggerReturn a collection of all the compaction entry logs which have been compacted, but have not been cleaned up.- Specified by:
incompleteCompactionLogsin interfaceEntryLogger
-
-