Package org.apache.bookkeeper.bookie
Class BookieImpl
- java.lang.Object
-
- org.apache.bookkeeper.bookie.BookieImpl
-
- All Implemented Interfaces:
Bookie
- Direct Known Subclasses:
ReadOnlyBookie
public class BookieImpl extends java.lang.Object implements Bookie
Implements a bookie.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
BookieImpl.NopWriteCallback
-
Nested classes/interfaces inherited from interface org.apache.bookkeeper.bookie.Bookie
Bookie.NoEntryException, Bookie.NoLedgerException
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.function.Supplier<BookieServiceInfo>
bookieServiceInfoProvider
(package private) ServerConfiguration
conf
(package private) LedgerDirsMonitor
dirsMonitor
(package private) boolean
entryLogPerLedgerEnabled
(package private) HandleFactory
handles
(package private) java.util.List<java.io.File>
journalDirectories
(package private) java.util.List<Journal>
journals
(package private) LedgerStorage
ledgerStorage
(package private) java.util.concurrent.locks.ReentrantLock
lock
static long
METAENTRY_ID_FENCE_KEY
static long
METAENTRY_ID_FORCE_LEDGER
(package private) static long
METAENTRY_ID_LEDGER_EXPLICITLAC
static long
METAENTRY_ID_LEDGER_KEY
(package private) RegistrationManager
registrationManager
(package private) java.util.concurrent.atomic.AtomicBoolean
shutdownTriggered
protected StateManager
stateManager
(package private) StatsLogger
statsLogger
(package private) SyncThread
syncThread
-
Constructor Summary
Constructors Constructor Description BookieImpl(ServerConfiguration conf, RegistrationManager registrationManager, LedgerStorage storage, DiskChecker diskChecker, LedgerDirsManager ledgerDirsManager, LedgerDirsManager indexDirsManager, StatsLogger statsLogger, io.netty.buffer.ByteBufAllocator allocator, java.util.function.Supplier<BookieServiceInfo> bookieServiceInfoProvider)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEntry(io.netty.buffer.ByteBuf entry, boolean ackBeforeSync, BookkeeperInternalCallbacks.WriteCallback cb, java.lang.Object ctx, byte[] masterKey)
Add entry to a ledger.void
cancelWaitForLastAddConfirmedUpdate(long ledgerId, Watcher<LastAddConfirmedUpdateNotification> watcher)
static void
checkDirectoryStructure(java.io.File dir)
io.netty.buffer.ByteBuf
createExplicitLACEntry(long ledgerId, io.netty.buffer.ByteBuf explicitLac)
io.netty.buffer.ByteBuf
createMasterKeyEntry(long ledgerId, byte[] masterKey)
java.util.concurrent.CompletableFuture<java.lang.Boolean>
fenceLedger(long ledgerId, byte[] masterKey)
Fences a ledger.void
forceLedger(long ledgerId, BookkeeperInternalCallbacks.WriteCallback cb, java.lang.Object ctx)
Force sync given 'ledgerId' entries on the journal to the disk.static boolean
format(ServerConfiguration conf, boolean isInteractive, boolean force)
Format the bookie server data.io.netty.buffer.ByteBufAllocator
getAllocator()
static BookieSocketAddress
getBookieAddress(ServerConfiguration conf)
Return the configured address of the bookie.static BookieId
getBookieId(ServerConfiguration conf)
static java.io.File[]
getCurrentDirectories(java.io.File[] dirs)
static java.io.File
getCurrentDirectory(java.io.File dir)
int
getExitCode()
Returns exit code - cause of failure.io.netty.buffer.ByteBuf
getExplicitLac(long ledgerId)
(package private) LedgerDirsManager
getIndexDirsManager()
java.util.List<Journal>
getJournals()
LedgerDirsManager
getLedgerDirsManager()
(package private) LedgerDescriptor
getLedgerForEntry(io.netty.buffer.ByteBuf entry, byte[] masterKey)
Retrieve the ledger descriptor for the ledger which entry should be added to.LedgerStorage
getLedgerStorage()
java.util.PrimitiveIterator.OfLong
getListOfEntriesOfLedger(long ledgerId)
BookieStateManager
getStateManager()
long
getTotalDiskSpace()
long
getTotalFreeSpace()
(package private) StateManager
initializeStateManager()
boolean
isAlive()
boolean
isAvailableForHighPriorityWrites()
Check whether Bookie is available for high priority writes.boolean
isReadOnly()
boolean
isRunning()
void
join()
static LedgerStorage
mountLedgerStorageOffline(ServerConfiguration conf, LedgerStorage ledgerStorage)
Initialize LedgerStorage instance without checkpointing for use within the shell and other RO users.static BookieImpl
newBookieImpl(ServerConfiguration conf, RegistrationManager registrationManager, LedgerStorage storage, DiskChecker diskChecker, LedgerDirsManager ledgerDirsManager, LedgerDirsManager indexDirsManager, StatsLogger statsLogger, io.netty.buffer.ByteBufAllocator allocator, java.util.function.Supplier<BookieServiceInfo> bookieServiceInfoProvider)
io.netty.buffer.ByteBuf
readEntry(long ledgerId, long entryId)
(package private) void
readJournal()
long
readLastAddConfirmed(long ledgerId)
void
recoveryAddEntry(io.netty.buffer.ByteBuf entry, BookkeeperInternalCallbacks.WriteCallback cb, java.lang.Object ctx, byte[] masterKey)
Add entry to a ledger, even if the ledger has previous been fenced.void
run()
void
setExplicitLac(io.netty.buffer.ByteBuf entry, BookkeeperInternalCallbacks.WriteCallback writeCallback, java.lang.Object ctx, byte[] masterKey)
int
shutdown()
(package private) int
shutdown(int exitCode)
void
start()
(package private) void
triggerBookieShutdown(int exitCode)
boolean
waitForLastAddConfirmedUpdate(long ledgerId, long previousLAC, Watcher<LastAddConfirmedUpdateNotification> watcher)
-
-
-
Field Detail
-
journalDirectories
final java.util.List<java.io.File> journalDirectories
-
conf
final ServerConfiguration conf
-
syncThread
final SyncThread syncThread
-
ledgerStorage
final LedgerStorage ledgerStorage
-
registrationManager
final RegistrationManager registrationManager
-
journals
final java.util.List<Journal> journals
-
handles
final HandleFactory handles
-
entryLogPerLedgerEnabled
final boolean entryLogPerLedgerEnabled
-
METAENTRY_ID_LEDGER_KEY
public static final long METAENTRY_ID_LEDGER_KEY
- See Also:
- Constant Field Values
-
METAENTRY_ID_FENCE_KEY
public static final long METAENTRY_ID_FENCE_KEY
- See Also:
- Constant Field Values
-
METAENTRY_ID_FORCE_LEDGER
public static final long METAENTRY_ID_FORCE_LEDGER
- See Also:
- Constant Field Values
-
METAENTRY_ID_LEDGER_EXPLICITLAC
static final long METAENTRY_ID_LEDGER_EXPLICITLAC
- See Also:
- Constant Field Values
-
bookieServiceInfoProvider
protected final java.util.function.Supplier<BookieServiceInfo> bookieServiceInfoProvider
-
dirsMonitor
LedgerDirsMonitor dirsMonitor
-
stateManager
protected StateManager stateManager
-
statsLogger
final StatsLogger statsLogger
-
shutdownTriggered
java.util.concurrent.atomic.AtomicBoolean shutdownTriggered
-
lock
java.util.concurrent.locks.ReentrantLock lock
-
-
Constructor Detail
-
BookieImpl
public BookieImpl(ServerConfiguration conf, RegistrationManager registrationManager, LedgerStorage storage, DiskChecker diskChecker, LedgerDirsManager ledgerDirsManager, LedgerDirsManager indexDirsManager, StatsLogger statsLogger, io.netty.buffer.ByteBufAllocator allocator, java.util.function.Supplier<BookieServiceInfo> bookieServiceInfoProvider) throws java.io.IOException, java.lang.InterruptedException, BookieException
- Throws:
java.io.IOException
java.lang.InterruptedException
BookieException
-
-
Method Detail
-
checkDirectoryStructure
public static void checkDirectoryStructure(java.io.File dir) throws java.io.IOException
- Throws:
java.io.IOException
-
getBookieId
public static BookieId getBookieId(ServerConfiguration conf) throws java.net.UnknownHostException
- Throws:
java.net.UnknownHostException
-
getBookieAddress
public static BookieSocketAddress getBookieAddress(ServerConfiguration conf) throws java.net.UnknownHostException
Return the configured address of the bookie.- Throws:
java.net.UnknownHostException
-
getLedgerDirsManager
public LedgerDirsManager getLedgerDirsManager()
-
getIndexDirsManager
LedgerDirsManager getIndexDirsManager()
-
getTotalDiskSpace
public long getTotalDiskSpace() throws java.io.IOException
- Specified by:
getTotalDiskSpace
in interfaceBookie
- Throws:
java.io.IOException
-
getTotalFreeSpace
public long getTotalFreeSpace() throws java.io.IOException
- Specified by:
getTotalFreeSpace
in interfaceBookie
- Throws:
java.io.IOException
-
getCurrentDirectory
public static java.io.File getCurrentDirectory(java.io.File dir)
-
getCurrentDirectories
public static java.io.File[] getCurrentDirectories(java.io.File[] dirs)
-
mountLedgerStorageOffline
public static LedgerStorage mountLedgerStorageOffline(ServerConfiguration conf, LedgerStorage ledgerStorage) throws java.io.IOException
Initialize LedgerStorage instance without checkpointing for use within the shell and other RO users. ledgerStorage must not have already been initialized.The caller is responsible for disposing of the ledgerStorage object.
- Parameters:
conf
- Bookie config.ledgerStorage
- Instance to initialize.- Returns:
- Passed ledgerStorage instance
- Throws:
java.io.IOException
-
newBookieImpl
public static BookieImpl newBookieImpl(ServerConfiguration conf, RegistrationManager registrationManager, LedgerStorage storage, DiskChecker diskChecker, LedgerDirsManager ledgerDirsManager, LedgerDirsManager indexDirsManager, StatsLogger statsLogger, io.netty.buffer.ByteBufAllocator allocator, java.util.function.Supplier<BookieServiceInfo> bookieServiceInfoProvider) throws java.io.IOException, java.lang.InterruptedException, BookieException
- Throws:
java.io.IOException
java.lang.InterruptedException
BookieException
-
initializeStateManager
StateManager initializeStateManager() throws java.io.IOException
- Throws:
java.io.IOException
-
readJournal
void readJournal() throws java.io.IOException, BookieException
- Throws:
java.io.IOException
BookieException
-
join
public void join() throws java.lang.InterruptedException
-
isAlive
public boolean isAlive()
-
isReadOnly
public boolean isReadOnly()
- Specified by:
isReadOnly
in interfaceBookie
-
isAvailableForHighPriorityWrites
public boolean isAvailableForHighPriorityWrites()
Check whether Bookie is available for high priority writes.- Specified by:
isAvailableForHighPriorityWrites
in interfaceBookie
- Returns:
- true if the bookie is able to take high priority writes.
-
run
public void run()
-
triggerBookieShutdown
void triggerBookieShutdown(int exitCode)
-
shutdown
int shutdown(int exitCode)
-
getLedgerForEntry
LedgerDescriptor getLedgerForEntry(io.netty.buffer.ByteBuf entry, byte[] masterKey) throws java.io.IOException, BookieException
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().- Throws:
BookieException
- if masterKey does not match the master key of the ledgerjava.io.IOException
-
createMasterKeyEntry
public io.netty.buffer.ByteBuf createMasterKeyEntry(long ledgerId, byte[] masterKey)
-
recoveryAddEntry
public void recoveryAddEntry(io.netty.buffer.ByteBuf entry, BookkeeperInternalCallbacks.WriteCallback cb, java.lang.Object ctx, byte[] masterKey) throws java.io.IOException, BookieException, java.lang.InterruptedException
Add entry to a ledger, even if the ledger has previous been fenced. This should only happen in bookie recovery or ledger recovery cases, where entries are being replicates so that they exist on a quorum of bookies. The corresponding client side call for this is not exposed to users.- Specified by:
recoveryAddEntry
in interfaceBookie
- Throws:
java.io.IOException
BookieException
java.lang.InterruptedException
-
createExplicitLACEntry
public io.netty.buffer.ByteBuf createExplicitLACEntry(long ledgerId, io.netty.buffer.ByteBuf explicitLac)
-
setExplicitLac
public void setExplicitLac(io.netty.buffer.ByteBuf entry, BookkeeperInternalCallbacks.WriteCallback writeCallback, java.lang.Object ctx, byte[] masterKey) throws java.io.IOException, java.lang.InterruptedException, BookieException
- Specified by:
setExplicitLac
in interfaceBookie
- Throws:
java.io.IOException
java.lang.InterruptedException
BookieException
-
getExplicitLac
public io.netty.buffer.ByteBuf getExplicitLac(long ledgerId) throws java.io.IOException, Bookie.NoLedgerException, BookieException
- Specified by:
getExplicitLac
in interfaceBookie
- Throws:
java.io.IOException
Bookie.NoLedgerException
BookieException
-
forceLedger
public void forceLedger(long ledgerId, BookkeeperInternalCallbacks.WriteCallback cb, java.lang.Object ctx)
Force sync given 'ledgerId' entries on the journal to the disk. It works like a regular addEntry with ackBeforeSync=false. This is useful for ledgers with DEFERRED_SYNC write flag.- Specified by:
forceLedger
in interfaceBookie
-
addEntry
public void addEntry(io.netty.buffer.ByteBuf entry, boolean ackBeforeSync, BookkeeperInternalCallbacks.WriteCallback cb, java.lang.Object ctx, byte[] masterKey) throws java.io.IOException, BookieException, java.lang.InterruptedException
Add entry to a ledger.- Specified by:
addEntry
in interfaceBookie
- Throws:
java.io.IOException
BookieException
java.lang.InterruptedException
-
fenceLedger
public java.util.concurrent.CompletableFuture<java.lang.Boolean> fenceLedger(long ledgerId, byte[] masterKey) throws java.io.IOException, BookieException
Fences a ledger. From this point on, clients will be unable to write to this ledger. Only recoveryAddEntry will be able to add entries to the ledger. This method is idempotent. Once a ledger is fenced, it can never be unfenced. Fencing a fenced ledger has no effect.- Specified by:
fenceLedger
in interfaceBookie
- Returns:
- Throws:
java.io.IOException
BookieException
-
readEntry
public io.netty.buffer.ByteBuf readEntry(long ledgerId, long entryId) throws java.io.IOException, Bookie.NoLedgerException, BookieException
- Specified by:
readEntry
in interfaceBookie
- Throws:
java.io.IOException
Bookie.NoLedgerException
BookieException
-
readLastAddConfirmed
public long readLastAddConfirmed(long ledgerId) throws java.io.IOException, BookieException
- Specified by:
readLastAddConfirmed
in interfaceBookie
- Throws:
java.io.IOException
BookieException
-
waitForLastAddConfirmedUpdate
public boolean waitForLastAddConfirmedUpdate(long ledgerId, long previousLAC, Watcher<LastAddConfirmedUpdateNotification> watcher) throws java.io.IOException
- Specified by:
waitForLastAddConfirmedUpdate
in interfaceBookie
- Throws:
java.io.IOException
-
cancelWaitForLastAddConfirmedUpdate
public void cancelWaitForLastAddConfirmedUpdate(long ledgerId, Watcher<LastAddConfirmedUpdateNotification> watcher) throws java.io.IOException
- Specified by:
cancelWaitForLastAddConfirmedUpdate
in interfaceBookie
- Throws:
java.io.IOException
-
getLedgerStorage
public LedgerStorage getLedgerStorage()
- Specified by:
getLedgerStorage
in interfaceBookie
-
getStateManager
public BookieStateManager getStateManager()
- Specified by:
getStateManager
in interfaceBookie
-
getAllocator
public io.netty.buffer.ByteBufAllocator getAllocator()
-
format
public static boolean format(ServerConfiguration conf, boolean isInteractive, boolean force)
Format the bookie server data.- Parameters:
conf
- ServerConfigurationisInteractive
- Whether format should ask prompt for confirmation if old data exists or not.force
- If non interactive and force is true, then old data will be removed without confirm prompt.- Returns:
- Returns true if the format is success else returns false
-
getExitCode
public int getExitCode()
Returns exit code - cause of failure.- Specified by:
getExitCode
in interfaceBookie
- Returns:
ExitCode
-
getListOfEntriesOfLedger
public java.util.PrimitiveIterator.OfLong getListOfEntriesOfLedger(long ledgerId) throws java.io.IOException, Bookie.NoLedgerException
- Specified by:
getListOfEntriesOfLedger
in interfaceBookie
- Throws:
java.io.IOException
Bookie.NoLedgerException
-
getJournals
public java.util.List<Journal> getJournals()
-
-