com.sleepycat.je.dbi
Class EnvironmentImpl

java.lang.Object
  extended by com.sleepycat.je.dbi.EnvironmentImpl
All Implemented Interfaces:
EnvConfigObserver

public class EnvironmentImpl
extends java.lang.Object
implements EnvConfigObserver

Underlying Environment implementation. There is a single instance for any database environment opened by the application.


Field Summary
static boolean IS_DALVIK
           
protected  LogManager logManager
           
 RunRecoveryException SAVED_RRE
           
static boolean USE_JAVA5_ADLER32
           
 
Constructor Summary
EnvironmentImpl(java.io.File envHome, EnvironmentConfig envConfig, EnvironmentImpl sharedCacheEnv, boolean replicationIntended)
          Create a database environment to represent the data in envHome.
 
Method Summary
 void abnormalClose()
          Used by tests to close an environment to simulate a crash.
 void acquireAPIReadLock(Locker who)
           
 void acquireAPIWriteLock(int time, java.util.concurrent.TimeUnit units)
           
 void addConfigObserver(EnvConfigObserver o)
          Adds an observer of mutable config changes.
 void addToCompressorQueue(BIN bin, Key deletedKey, boolean doWakeup)
          Tells the asynchronous IN compressor thread about a BIN with a deleted entry.
 void addToCompressorQueue(BINReference binRef, boolean doWakeup)
          Tells the asynchronous IN compressor thread about a BINReference with a deleted entry.
 void addToCompressorQueue(java.util.Collection<BINReference> binRefs, boolean doWakeup)
          Tells the asynchronous IN compressor thread about a collections of BINReferences with deleted entries.
 void checkIfInvalid()
           
 void checkImmutablePropsForEquality(EnvironmentConfig config)
          Throws an exception if an immutable property is changed.
 void checkNotClosed()
           
 EnvironmentConfig cloneConfig()
          Clones the current configuration.
 EnvironmentMutableConfig cloneMutableConfig()
          Clones the current mutable configuration.
 void close()
          Decrements the reference count and closes the enviornment when it reaches zero.
 void close(boolean doCheckpoint)
          Decrements the reference count and closes the environment when it reaches zero.
 void closeAfterRunRecovery()
           
 void closeLogger()
          Close down the logger.
static void decThreadLocalReferenceCount()
           
 void enableDebugLoggingToDbLog()
          Add the database log as one of the debug logging destinations when the logging system is sufficiently initialized.
 void envConfigUpdate(DbConfigManager mgr, EnvironmentMutableConfig newConfig)
          Respond to config updates.
 long forceLogFileFlip()
          Flip the log to a new file, forcing an fsync.
static int getAdler32ChunkSize()
           
 Checkpointer getCheckpointer()
           
 Cleaner getCleaner()
           
 DbConfigManager getConfigManager()
          Returns the config manager for the current base configuration.
 boolean getDbEviction()
          Returns whether DB/MapLN eviction is enabled.
 DbTree getDbTree()
           
 java.io.File getEnvironmentHome()
          Get the environment home directory.
 Evictor getEvictor()
           
 ExceptionListener getExceptionListener()
           
static boolean getFairLatches()
           
 FileManager getFileManager()
           
 INCompressor getINCompressor()
          Return the incompressor.
 int getINCompressorQueueSize()
           
 INList getInMemoryINs()
           
 RecoveryInfo getLastRecoveryInfo()
          Info about the last recovery
 long getLockTimeout()
           
 java.util.logging.Logger getLogger()
           
 LogManager getLogManager()
           
 MemoryBudget getMemoryBudget()
           
static boolean getNoComparators()
           
 NodeSequence getNodeSequence()
           
 ReplicatorInstance getReplicator()
           
 long getRootLsn()
           
 boolean getSharedCache()
           
static boolean getSharedLatches()
           
static int getThreadLocalReferenceCount()
           
 SharedLatch getTriggerLatch()
          Returns the shared trigger latch.
 TxnManager getTxnManager()
           
 long getTxnTimeout()
           
 UtilizationProfile getUtilizationProfile()
          Returns the UtilizationProfile.
 UtilizationTracker getUtilizationTracker()
          Returns the UtilizationTracker.
static void incThreadLocalReferenceCount()
           
 void invalidate(java.lang.Error e)
           
 void invalidate(RunRecoveryException e)
          Invalidate the environment.
 boolean invokeCheckpoint(CheckpointConfig config, boolean flushAll, java.lang.String invokingSource)
          Invoke a checkpoint programatically.
 int invokeCleaner()
           
 boolean invokeCompressor()
          Invoke a compress programatically.
 void invokeEvictor()
           
 boolean isClosed()
           
 boolean isClosing()
           
 boolean isMemOnly()
           
 boolean isNoLocking()
           
 boolean isOpen()
           
 boolean isReadOnly()
           
 boolean isReplicated()
          Return true if this environment is part of a replication group.
 boolean isTransactional()
           
 void lazyCompress(IN in, LocalUtilizationTracker localTracker)
          Do lazy compression at opportune moments.
 EnvironmentStats loadStats(StatsConfig config)
          Retrieve and return stat information.
 LockStats lockStat(StatsConfig config)
          Retrieve lock statistics
 void logMapTreeRoot()
          Logs the map tree root and saves the LSN.
 void logMapTreeRoot(long ifBeforeLsn)
          Logs the map tree root, but only if its current LSN is before the ifBeforeLsn parameter or ifBeforeLsn is NULL_LSN.
static boolean maybeForceYield()
          For stress testing.
 boolean mayNotWrite()
          When a RunRecoveryException occurs or the environment is closed, further writing can cause log corruption.
 void open()
          Not much to do, mark state.
 void readMapTreeFromLog(long rootLsn, boolean replicationIntended)
          Set the mapping tree from the log.
 void reinit(boolean replicationIntended)
          Reinitialize after an Internal Init copies new *.jdb files into envhome.
 boolean releaseAPIReadLock(Locker who)
           
 void releaseAPIWriteLock()
           
 void removeConfigObserver(EnvConfigObserver o)
          Removes an observer of mutable config changes.
 void rewriteMapTreeRoot(long cleanerTargetLsn)
          Force a rewrite of the map tree root if required.
 boolean scanLog(long startPosition, long endPosition, LogScanConfig config, LogScanner scanner)
           
 void setBackgroundSleepHook(TestHook hook)
           
 void setExceptionListener(ExceptionListener exceptionListener)
           
 void setMutableConfig(EnvironmentMutableConfig config)
          Changes the mutable config properties that are present in the given config, and notifies all config observer.
 void setReplicator(ReplicatorInstance repInstance)
           
 void setupAPILock()
           
 void shutdownCleaner()
          public for unit tests.
 void sleepAfterBackgroundIO()
          If the sleep backlog is non-zero (set by updateBackgroundReads or updateBackgroundWrites), sleep for the configured interval and decrement the backlog.
 void stopDaemons()
          For unit testing -- shuts down daemons completely but leaves environment usable since environment references are not nulled out.
 Txn txnBegin(Transaction parent, TransactionConfig txnConfig)
          Transactional services.
 TransactionStats txnStat(StatsConfig config)
          Retrieve txn statistics
 void updateBackgroundReads(int nReads)
          If a background read limit has been configured and that limit is exceeded when the cumulative total is incremented by the given number of reads, increment the sleep backlog to cause a sleep to occur.
 void updateBackgroundWrites(int writeSize, int logBufferSize)
          If a background write limit has been configured and that limit is exceeded when the given amount written is added to the cumulative total, increment the sleep backlog to cause a sleep to occur.
 boolean useDirectNIO()
           
 boolean verify(VerifyConfig config, java.io.PrintStream out)
           
 void verifyCursors()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logManager

protected LogManager logManager

SAVED_RRE

public final RunRecoveryException SAVED_RRE

USE_JAVA5_ADLER32

public static final boolean USE_JAVA5_ADLER32

IS_DALVIK

public static final boolean IS_DALVIK
Constructor Detail

EnvironmentImpl

public EnvironmentImpl(java.io.File envHome,
                       EnvironmentConfig envConfig,
                       EnvironmentImpl sharedCacheEnv,
                       boolean replicationIntended)
                throws DatabaseException
Create a database environment to represent the data in envHome. dbHome. Properties from the je.properties file in that directory are used to initialize the system wide property bag. Properties passed to this method are used to influence the open itself.

Parameters:
envHome - absolute path of the database environment home directory
envConfig -
sharedCacheEnv - if non-null, is another environment that is sharing the cache with this environment; if null, this environment is not sharing the cache or is the first environment to share the cache.
Throws:
DatabaseException - on all other failures
Method Detail

reinit

public void reinit(boolean replicationIntended)
            throws DatabaseException
Reinitialize after an Internal Init copies new *.jdb files into envhome.

Throws:
DatabaseException

envConfigUpdate

public void envConfigUpdate(DbConfigManager mgr,
                            EnvironmentMutableConfig newConfig)
                     throws DatabaseException
Respond to config updates.

Specified by:
envConfigUpdate in interface EnvConfigObserver
Throws:
DatabaseException

getINCompressor

public INCompressor getINCompressor()
Return the incompressor. In general, don't use this directly because it's easy to forget that the incompressor can be null at times (i.e during the shutdown procedure. Instead, wrap the functionality within this class, like lazyCompress.


getUtilizationTracker

public UtilizationTracker getUtilizationTracker()
Returns the UtilizationTracker.


getUtilizationProfile

public UtilizationProfile getUtilizationProfile()
Returns the UtilizationProfile.


updateBackgroundReads

public void updateBackgroundReads(int nReads)
If a background read limit has been configured and that limit is exceeded when the cumulative total is incremented by the given number of reads, increment the sleep backlog to cause a sleep to occur. Called by background activities such as the cleaner after performing a file read operation.

See Also:
sleepAfterBackgroundIO()

updateBackgroundWrites

public void updateBackgroundWrites(int writeSize,
                                   int logBufferSize)
If a background write limit has been configured and that limit is exceeded when the given amount written is added to the cumulative total, increment the sleep backlog to cause a sleep to occur. Called by background activities such as the checkpointer and evictor after performing a file write operation.

The number of writes is estimated by dividing the bytes written by the log buffer size. Since the log write buffer is shared by all writers, this is the best approximation possible.

See Also:
sleepAfterBackgroundIO()

sleepAfterBackgroundIO

public void sleepAfterBackgroundIO()
If the sleep backlog is non-zero (set by updateBackgroundReads or updateBackgroundWrites), sleep for the configured interval and decrement the backlog.

If two threads call this method and the first call causes a sleep, the call by the second thread will block until the first thread's sleep interval is over. When the call by the second thread is unblocked, if another sleep is needed then the second thread will sleep again. In other words, when lots of sleeps are needed, background threads may backup. This is intended to give foreground threads a chance to "catch up" when background threads are doing a lot of IO.


setBackgroundSleepHook

public void setBackgroundSleepHook(TestHook hook)

setupAPILock

public void setupAPILock()
                  throws DatabaseException
Throws:
DatabaseException

acquireAPIWriteLock

public void acquireAPIWriteLock(int time,
                                java.util.concurrent.TimeUnit units)
                         throws DatabaseException
Throws:
DatabaseException

releaseAPIWriteLock

public void releaseAPIWriteLock()
                         throws DatabaseException
Throws:
DatabaseException

acquireAPIReadLock

public void acquireAPIReadLock(Locker who)
                        throws DatabaseException
Throws:
DatabaseException

releaseAPIReadLock

public boolean releaseAPIReadLock(Locker who)
                           throws DatabaseException
Throws:
DatabaseException

scanLog

public boolean scanLog(long startPosition,
                       long endPosition,
                       LogScanConfig config,
                       LogScanner scanner)
                throws DatabaseException
Throws:
DatabaseException

logMapTreeRoot

public void logMapTreeRoot()
                    throws DatabaseException
Logs the map tree root and saves the LSN.

Throws:
DatabaseException

logMapTreeRoot

public void logMapTreeRoot(long ifBeforeLsn)
                    throws DatabaseException
Logs the map tree root, but only if its current LSN is before the ifBeforeLsn parameter or ifBeforeLsn is NULL_LSN.

Throws:
DatabaseException

rewriteMapTreeRoot

public void rewriteMapTreeRoot(long cleanerTargetLsn)
                        throws DatabaseException
Force a rewrite of the map tree root if required.

Throws:
DatabaseException

getRootLsn

public long getRootLsn()
Returns:
the mapping tree root LSN.

readMapTreeFromLog

public void readMapTreeFromLog(long rootLsn,
                               boolean replicationIntended)
                        throws DatabaseException
Set the mapping tree from the log. Called during recovery.

Throws:
DatabaseException

addToCompressorQueue

public void addToCompressorQueue(BIN bin,
                                 Key deletedKey,
                                 boolean doWakeup)
                          throws DatabaseException
Tells the asynchronous IN compressor thread about a BIN with a deleted entry.

Throws:
DatabaseException

addToCompressorQueue

public void addToCompressorQueue(BINReference binRef,
                                 boolean doWakeup)
                          throws DatabaseException
Tells the asynchronous IN compressor thread about a BINReference with a deleted entry.

Throws:
DatabaseException

addToCompressorQueue

public void addToCompressorQueue(java.util.Collection<BINReference> binRefs,
                                 boolean doWakeup)
                          throws DatabaseException
Tells the asynchronous IN compressor thread about a collections of BINReferences with deleted entries.

Throws:
DatabaseException

lazyCompress

public void lazyCompress(IN in,
                         LocalUtilizationTracker localTracker)
                  throws DatabaseException
Do lazy compression at opportune moments.

Throws:
DatabaseException

enableDebugLoggingToDbLog

public void enableDebugLoggingToDbLog()
                               throws DatabaseException
Add the database log as one of the debug logging destinations when the logging system is sufficiently initialized.

Throws:
DatabaseException

closeLogger

public void closeLogger()
Close down the logger.


open

public void open()
Not much to do, mark state.


invalidate

public void invalidate(RunRecoveryException e)
Invalidate the environment. Done when a fatal exception (RunRecoveryException) is thrown.


invalidate

public void invalidate(java.lang.Error e)

isOpen

public boolean isOpen()
Returns:
true if environment is open.

isClosing

public boolean isClosing()
Returns:
true if close has begun, although the state may still be open.

isClosed

public boolean isClosed()

mayNotWrite

public boolean mayNotWrite()
When a RunRecoveryException occurs or the environment is closed, further writing can cause log corruption.


checkIfInvalid

public void checkIfInvalid()
                    throws RunRecoveryException
Throws:
RunRecoveryException

checkNotClosed

public void checkNotClosed()
                    throws DatabaseException
Throws:
DatabaseException

close

public void close()
           throws DatabaseException
Decrements the reference count and closes the enviornment when it reaches zero. A checkpoint is always performed when closing.

Throws:
DatabaseException

close

public void close(boolean doCheckpoint)
           throws DatabaseException
Decrements the reference count and closes the environment when it reaches zero. A checkpoint when closing is optional.

Throws:
DatabaseException

abnormalClose

public void abnormalClose()
                   throws DatabaseException
Used by tests to close an environment to simulate a crash. Database handles do not have to be closed before calling this method. A checkpoint is not performed.

Throws:
DatabaseException

closeAfterRunRecovery

public void closeAfterRunRecovery()
                           throws DatabaseException
Throws:
DatabaseException

getThreadLocalReferenceCount

public static int getThreadLocalReferenceCount()

incThreadLocalReferenceCount

public static void incThreadLocalReferenceCount()

decThreadLocalReferenceCount

public static void decThreadLocalReferenceCount()

getNoComparators

public static boolean getNoComparators()

invokeCheckpoint

public boolean invokeCheckpoint(CheckpointConfig config,
                                boolean flushAll,
                                java.lang.String invokingSource)
                         throws DatabaseException
Invoke a checkpoint programatically. Note that only one checkpoint may run at a time.

Throws:
DatabaseException

forceLogFileFlip

public long forceLogFileFlip()
                      throws DatabaseException
Flip the log to a new file, forcing an fsync. Return the LSN of the trace record in the new file.

Throws:
DatabaseException

invokeCompressor

public boolean invokeCompressor()
                         throws DatabaseException
Invoke a compress programatically. Note that only one compress may run at a time.

Throws:
DatabaseException

invokeEvictor

public void invokeEvictor()
                   throws DatabaseException
Throws:
DatabaseException

invokeCleaner

public int invokeCleaner()
                  throws DatabaseException
Throws:
DatabaseException

stopDaemons

public void stopDaemons()
                 throws java.lang.InterruptedException
For unit testing -- shuts down daemons completely but leaves environment usable since environment references are not nulled out.

Throws:
java.lang.InterruptedException

shutdownCleaner

public void shutdownCleaner()
                     throws java.lang.InterruptedException
public for unit tests.

Throws:
java.lang.InterruptedException

isNoLocking

public boolean isNoLocking()

isTransactional

public boolean isTransactional()

isReadOnly

public boolean isReadOnly()

isMemOnly

public boolean isMemOnly()

getFairLatches

public static boolean getFairLatches()

getSharedLatches

public static boolean getSharedLatches()

getDbEviction

public boolean getDbEviction()
Returns whether DB/MapLN eviction is enabled.


useDirectNIO

public boolean useDirectNIO()

getAdler32ChunkSize

public static int getAdler32ChunkSize()

getSharedCache

public boolean getSharedCache()

txnBegin

public Txn txnBegin(Transaction parent,
                    TransactionConfig txnConfig)
             throws DatabaseException
Transactional services.

Throws:
DatabaseException

getLogManager

public LogManager getLogManager()

getFileManager

public FileManager getFileManager()

getDbTree

public DbTree getDbTree()

getConfigManager

public DbConfigManager getConfigManager()
Returns the config manager for the current base configuration.

The configuration can change, but changes are made by replacing the config manager object with a enw one. To use a consistent set of properties, call this method once and query the returned manager repeatedly for each property, rather than getting the config manager via this method for each property individually.


getNodeSequence

public NodeSequence getNodeSequence()

cloneConfig

public EnvironmentConfig cloneConfig()
Clones the current configuration.


cloneMutableConfig

public EnvironmentMutableConfig cloneMutableConfig()
Clones the current mutable configuration.


checkImmutablePropsForEquality

public void checkImmutablePropsForEquality(EnvironmentConfig config)
                                    throws java.lang.IllegalArgumentException
Throws an exception if an immutable property is changed.

Throws:
java.lang.IllegalArgumentException

setMutableConfig

public void setMutableConfig(EnvironmentMutableConfig config)
                      throws DatabaseException
Changes the mutable config properties that are present in the given config, and notifies all config observer.

Throws:
DatabaseException

setExceptionListener

public void setExceptionListener(ExceptionListener exceptionListener)

getExceptionListener

public ExceptionListener getExceptionListener()

addConfigObserver

public void addConfigObserver(EnvConfigObserver o)
Adds an observer of mutable config changes.


removeConfigObserver

public void removeConfigObserver(EnvConfigObserver o)
Removes an observer of mutable config changes.


getInMemoryINs

public INList getInMemoryINs()

getTxnManager

public TxnManager getTxnManager()

getCheckpointer

public Checkpointer getCheckpointer()

getCleaner

public Cleaner getCleaner()

getMemoryBudget

public MemoryBudget getMemoryBudget()

getLogger

public java.util.logging.Logger getLogger()
Returns:
environment Logger, for use in debugging output.

verify

public boolean verify(VerifyConfig config,
                      java.io.PrintStream out)
               throws DatabaseException
Throws:
DatabaseException

verifyCursors

public void verifyCursors()
                   throws DatabaseException
Throws:
DatabaseException

loadStats

public EnvironmentStats loadStats(StatsConfig config)
                           throws DatabaseException
Retrieve and return stat information.

Throws:
DatabaseException

lockStat

public LockStats lockStat(StatsConfig config)
                   throws DatabaseException
Retrieve lock statistics

Throws:
DatabaseException

txnStat

public TransactionStats txnStat(StatsConfig config)
                         throws DatabaseException
Retrieve txn statistics

Throws:
DatabaseException

getINCompressorQueueSize

public int getINCompressorQueueSize()
                             throws DatabaseException
Throws:
DatabaseException

getLastRecoveryInfo

public RecoveryInfo getLastRecoveryInfo()
Info about the last recovery


getEnvironmentHome

public java.io.File getEnvironmentHome()
Get the environment home directory.


getTxnTimeout

public long getTxnTimeout()

getLockTimeout

public long getLockTimeout()

getTriggerLatch

public SharedLatch getTriggerLatch()
Returns the shared trigger latch.


getEvictor

public Evictor getEvictor()

isReplicated

public boolean isReplicated()
Return true if this environment is part of a replication group.


getReplicator

public ReplicatorInstance getReplicator()

setReplicator

public void setReplicator(ReplicatorInstance repInstance)

maybeForceYield

public static boolean maybeForceYield()
For stress testing. Should only ever be called from an assert.