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 class
DefaultEntryLogger.BufferedLogChannel
(package private) static interface
DefaultEntryLogger.EntryLogListener
Entry Log Listener.(package private) static class
DefaultEntryLogger.EntryLookupException
Exception type for representing lookup errors.(package private) static class
DefaultEntryLogger.RecentEntryLogsStatus
Datastructure which maintains the status of logchannels.
-
Field Summary
Fields Modifier and Type Field Description (package private) ServerConfiguration
conf
(package private) EntryLoggerAllocator
entryLoggerAllocator
(package private) static int
HEADER_CURRENT_VERSION
(package private) static int
HEADER_VERSION_POSITION
(package private) static long
INVALID_LID
(package private) static long
LEDGERS_MAP_ENTRY_ID
(package private) static int
LEDGERS_MAP_ENTRY_SIZE
(package private) static int
LEDGERS_MAP_HEADER_SIZE
Ledgers map is composed of multiple parts that can be split into separated entries.(package private) static int
LEDGERS_MAP_MAX_BATCH_SIZE
(package private) static int
LEDGERS_MAP_OFFSET_POSITION
(package private) static int
LOGFILE_HEADER_SIZE
The 1K block at the head of the entry logger file that contains the fingerprint and meta-data.(package private) static long
MB
(package private) static int
MIN_SANE_ENTRY_SIZE
(package private) DefaultEntryLogger.RecentEntryLogsStatus
recentlyCreatedEntryLogsStatus
(package private) static int
UNINITIALIZED_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 long
addEntry(long ledger, io.netty.buffer.ByteBuf entry)
Add an entry for ledger ```ledgerId``` to the entrylog.(package private) long
addEntry(long ledger, io.netty.buffer.ByteBuf entry, boolean rollLog)
(package private) long
addEntry(long ledger, java.nio.ByteBuffer entry)
(package private) void
addListener(DefaultEntryLogger.EntryLogListener listener)
(package private) void
checkEntry(long ledgerId, long entryId, long location)
(package private) void
checkpoint()
Flushes all rotated log channels.(package private) void
clearCompactingLogId()
void
close()
Shutdown method to gracefully stop entry logger.(package private) boolean
commitEntryMemTableFlush()
(package private) EntryLogMetadata
extractEntryLogMetadataFromIndex(long entryLogId)
(package private) static long
fileName2LogId(java.lang.String fileName)
Convert log filename (hex format with suffix) to logId in long.void
flush()
Flush any outstanding writes to disk.(package private) BufferedReadChannel
getChannelForLogId(long entryLogId)
(package private) EntryLoggerAllocator
getEntryLoggerAllocator()
get EntryLoggerAllocator, Just for tests.(package private) EntryLogManager
getEntryLogManager()
EntryLogMetadata
getEntryLogMetadata(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.BufferedReadChannel
getFromChannels(long logId)
(package private) long
getLeastUnflushedLogId()
protected LedgerDirsManager
getLedgerDirsManager()
(package private) long
getPreviousAllocatedEntryLogId()
java.util.Collection<CompactionEntryLog>
incompleteCompactionLogs()
Return a collection of all the compaction entry logs which have been compacted, but have not been cleaned up.boolean
logExists(long logId)
Whether the log file exists or not.(package private) static java.lang.String
logId2HexString(long logId)
Convert log Id to hex string.(package private) static long
logIdForOffset(long offset)
CompactionEntryLog
newCompactionLog(long logToCompact)
Create a new entrylog into which compacted entries can be added.(package private) static long
posForOffset(long location)
(package private) void
prepareEntryMemTableFlush()
(package private) void
prepareSortedLedgerStorageCheckpoint(long numBytesFlushed)
BufferedReadChannel
putInReadChannels(long logId, BufferedReadChannel bc)
Put the logId, bc pair in the map responsible for the current thread.io.netty.buffer.ByteBuf
readEntry(long location)
Read an entry from an entrylog location.io.netty.buffer.ByteBuf
readEntry(long ledgerId, long entryId, long entryLocation)
Read an entry from an entrylog location, and verify that is matches the expected ledger and entry ID.boolean
removeEntryLog(long entryLogId)
Remove entry log.void
removeFromChannelsAndClose(long logId)
Remove all entries for this log file in each thread's cache.void
scanEntryLog(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:EntryLogger
Get 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:
getFlushedLogIds
in 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:
removeEntryLog
in interfaceEntryLogger
- Parameters:
entryLogId
- Entry Log File Id- Returns:
- false if the entrylog doesn't exist.
-
clearCompactingLogId
void clearCompactingLogId()
-
checkpoint
void checkpoint() throws java.io.IOException
Flushes 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.IOException
Description copied from interface:EntryLogger
Flush any outstanding writes to disk.- Specified by:
flush
in 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.IOException
Description copied from interface:EntryLogger
Add an entry for ledger ```ledgerId``` to the entrylog.- Specified by:
addEntry
in 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.EntryLookupException
java.io.IOException
-
readEntry
public io.netty.buffer.ByteBuf readEntry(long ledgerId, long entryId, long entryLocation) throws java.io.IOException, Bookie.NoEntryException
Description copied from interface:EntryLogger
Read an entry from an entrylog location, and verify that is matches the expected ledger and entry ID.- Specified by:
readEntry
in 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.IOException
Bookie.NoEntryException
-
readEntry
public io.netty.buffer.ByteBuf readEntry(long location) throws java.io.IOException, Bookie.NoEntryException
Description copied from interface:EntryLogger
Read an entry from an entrylog location.- Specified by:
readEntry
in interfaceEntryLogger
- Parameters:
location
- the location from which to read the entry- Returns:
- the entry
- Throws:
java.io.IOException
Bookie.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:
logExists
in interfaceEntryLogger
-
getEntryLogsSet
public java.util.Set<java.lang.Long> getEntryLogsSet() throws java.io.IOException
Returns 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.IOException
Scan entry log.- Specified by:
scanEntryLog
in 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:EntryLogger
Retrieve 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:
getEntryLogMetadata
in 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in 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:EntryLogger
Create 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:
newCompactionLog
in interfaceEntryLogger
- Throws:
java.io.IOException
-
incompleteCompactionLogs
public java.util.Collection<CompactionEntryLog> incompleteCompactionLogs()
Description copied from interface:EntryLogger
Return a collection of all the compaction entry logs which have been compacted, but have not been cleaned up.- Specified by:
incompleteCompactionLogs
in interfaceEntryLogger
-
-