net.sf.farrago.db
Class FarragoDatabase

java.lang.Object
  extended by org.eigenbase.util.CompoundClosableAllocation
      extended by net.sf.farrago.util.FarragoCompoundAllocation
          extended by net.sf.farrago.db.FarragoDbSingleton
              extended by net.sf.farrago.db.FarragoDatabase
All Implemented Interfaces:
FarragoAllocation, FarragoAllocationOwner, ClosableAllocation, ClosableAllocationOwner

public class FarragoDatabase
extends FarragoDbSingleton

FarragoDatabase is a top-level singleton representing an instance of a Farrago database engine.

NOTE jvs 14-Dec-2005: FarragoDatabase inherits from FarragoDbSingleton for backwards compatibility. This tie may eventually be severed so that multiple instances of FarragoDatabase can be created in the same JVM.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/db/FarragoDatabase.java#90 $
Author:
John V. Sichi

Nested Class Summary
private  class FarragoDatabase.CheckpointTask
           
private  class FarragoDatabase.ReposSwitcher
           
private  class FarragoDatabase.WatchdogTask
          1 Hz task for background activities.
 
Field Summary
private  boolean authenticateLocalConnections
           
private  Configuration authenticationConfig
           
private  FarragoObjectCache codeCache
          Cache of all sorts of stuff; see the design docs.
private  FarragoDdlLockManager ddlLockManager
           
private  FennelDbHandle fennelDbHandle
           
private  List<FarragoSessionModelExtension> modelExtensions
           
private  OJRexImplementorTable ojRexImplementorTable
           
private  boolean partialRestore
          Set to true if the system has only been partially restored, in which case, no tables can be accessed and no objects can be created.
private  FarragoPluginClassLoader pluginClassLoader
           
protected  FarragoSessionFactory sessionFactory
           
private  FarragoRepos systemRepos
           
private  File traceConfigFile
          File containing trace configuration.
private  FarragoSessionTxnMgr txnMgr
           
private  AtomicLong uniqueId
          Provides unique identifiers for sessions and statements.
private  FarragoRepos userRepos
           
 
Fields inherited from class net.sf.farrago.db.FarragoDbSingleton
instance, tracer
 
Fields inherited from class org.eigenbase.util.CompoundClosableAllocation
allocations
 
Constructor Summary
FarragoDatabase(FarragoSessionFactory sessionFactory, boolean init)
          Creates a FarragoDatabase.
 
Method Summary
private  void assertNoFennelHandles()
           
 void cleanupBackupData(boolean backupSucceeded, boolean setEndTimestamp)
          Cleans up the system backup catalog by either removing pending backup data if the last backup failed, or by updating the pending data to completed, if the last backup succeeded.
 void close(boolean suppressExcns)
           
 void deallocateOld()
           
private  void dumpTraceConfig()
           
private  void executeFennelSetParam(String paramName, SqlLiteral paramValue)
           
private  void filterMapNullValues(Map<String,Object> configMap)
           
 FarragoSessionExecutingStmtInfo findExecutingStmtInfo(long id)
          Looks up executing statement info by statement id.
 FarragoSessionInfo findSessionInfo(long id)
          look up session info by session id.
 void flushCodeCache()
          Flushes unpinned entries from the cache cache for this database.
 Configuration getAuthenticationConfig()
           
private  File getBootUrlFile()
           
 FarragoObjectCache getCodeCache()
           
private  long getCodeCacheMaxBytes(FemFarragoConfig config)
           
 FarragoObjectCache getDataWrapperCache()
           
 FarragoDdlLockManager getDdlLockManager()
           
 FennelDbHandle getFennelDbHandle()
           
 List<FarragoSessionModelExtension> getModelExtensions()
           
 OJRexImplementorTable getOJRexImplementorTable()
           
 FarragoPluginClassLoader getPluginClassLoader()
           
 FarragoRepos getSystemRepos()
           
 FarragoSessionTxnMgr getTxnMgr()
           
 long getUniqueId()
          Gets a unique identifier: never 0.
 FarragoRepos getUserRepos()
           
 FarragoSessionExecutableStmt implementStmt(FarragoSessionPreparingStmt prep, RelNode rootRel, SqlKind sqlKind, boolean logical, FarragoAllocationOwner owner)
          Implements a logical or physical query plan but does not execute it.
 boolean isAuthenticateLocalConnections()
           
 boolean isAuthenticationEnabled()
           
private  boolean isExecutableStmtStale(FarragoRepos repos, FarragoSessionExecutableStmt stmt)
           
 boolean isPartiallyRestored()
          Determines if the database is in a state where a partial restore has been done, which indicates that the catalog data is not in sync with table data.
private  void kill(FarragoSessionExecutingStmtInfo info, boolean cancelOnly)
           
 void killExecutingStmt(long id, boolean cancelOnly)
          Kill an executing statement: cancel it and deallocate it.
 int killExecutingStmtMatching(String match, String nomatch, boolean cancelOnly)
          Kills all statements that are executing SQL that matches a given pattern, but does not match a second pattern.
 void killSession(long id, boolean cancelOnly)
          Kills a session.
private  void loadBootUrls()
           
private  boolean loadFennel(FarragoCompoundAllocation startOfWorldAllocation, FennelCmdExecutor cmdExecutor, boolean init)
           
private  void loadModelPlugins()
           
static void main(String[] args)
          Main entry point which creates a new Farrago database.
static FarragoSessionFactory newSessionFactory()
           
 FarragoSessionExecutableStmt prepareStmt(FarragoSessionStmtContext stmtContext, FarragoSessionStmtValidator stmtValidator, SqlNode sqlNode, FarragoAllocationOwner owner, FarragoSessionAnalyzedSql analyzedSql)
          Prepares an SQL expression; uses a cached implementation if available, otherwise caches the one generated here.
private  FarragoSessionExecutableStmt prepareStmtImpl(FarragoSessionPreparingStmt stmt, SqlNode sqlNode, FarragoAllocationOwner owner, FarragoSessionAnalyzedSql analyzedSql)
           
 void requestCheckpoint(boolean fuzzy, boolean async)
           
(package private)  void saveBootUrl(String url)
           
 void setAuthenticateLocalConnections(boolean authenticateLocalConnections)
          Turns on/off authentication for in-process jdbc sessions.
 void setFennelDbHandle(FennelDbHandle handle)
          Sets the fennel DB handle used by the database.
 void setTxnMgr(FarragoSessionTxnMgr txnMgr)
          Sets the transaction manager for this database.
 void simulateCatalogRecovery()
          Simulates the catalog recovery which would occur on startup after a crash.
private  void updateCatalog(boolean recoveryRequired)
          Updates the catalog on startup, taking care of any cleanup or recovery.
 void updateSystemParameter(DdlSetSystemParamStmt ddlStmt)
           
private  void warnOnClose(Throwable ex, boolean suppressExcns)
           
 
Methods inherited from class net.sf.farrago.db.FarragoDbSingleton
addSession, disconnectSession, getSessions, getSessions, isReferenced, pinReference, pinReference, setBackupFlag, shutdown, shutdownConditional
 
Methods inherited from class org.eigenbase.util.CompoundClosableAllocation
addAllocation, closeAllocation, forgetAllocation, hasAllocations
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.eigenbase.util.ClosableAllocationOwner
addAllocation
 
Methods inherited from interface org.eigenbase.util.ClosableAllocation
closeAllocation
 
Methods inherited from interface org.eigenbase.util.ClosableAllocation
closeAllocation
 

Field Detail

systemRepos

private FarragoRepos systemRepos

userRepos

private FarragoRepos userRepos

fennelDbHandle

private FennelDbHandle fennelDbHandle

ojRexImplementorTable

private OJRexImplementorTable ojRexImplementorTable

sessionFactory

protected FarragoSessionFactory sessionFactory

pluginClassLoader

private FarragoPluginClassLoader pluginClassLoader

modelExtensions

private List<FarragoSessionModelExtension> modelExtensions

ddlLockManager

private FarragoDdlLockManager ddlLockManager

txnMgr

private FarragoSessionTxnMgr txnMgr

authenticationConfig

private Configuration authenticationConfig

authenticateLocalConnections

private boolean authenticateLocalConnections

partialRestore

private boolean partialRestore
Set to true if the system has only been partially restored, in which case, no tables can be accessed and no objects can be created.


codeCache

private FarragoObjectCache codeCache
Cache of all sorts of stuff; see the design docs.


traceConfigFile

private File traceConfigFile
File containing trace configuration.


uniqueId

private AtomicLong uniqueId
Provides unique identifiers for sessions and statements.

Constructor Detail

FarragoDatabase

public FarragoDatabase(FarragoSessionFactory sessionFactory,
                       boolean init)
Creates a FarragoDatabase.

Parameters:
sessionFactory - factory for various database-level objects
init - whether to initialize the system catalog (the first time the database is started)
Method Detail

getCodeCache

public FarragoObjectCache getCodeCache()
Returns:
the shared code cache for this database

flushCodeCache

public void flushCodeCache()
Flushes unpinned entries from the cache cache for this database.


getDataWrapperCache

public FarragoObjectCache getDataWrapperCache()
Returns:
the shared data wrapper cache for this database

getPluginClassLoader

public FarragoPluginClassLoader getPluginClassLoader()
Returns:
ClassLoader for loading plugins

getModelExtensions

public List<FarragoSessionModelExtension> getModelExtensions()
Returns:
list of installed FarragoSessionModelExtension instances

getTxnMgr

public FarragoSessionTxnMgr getTxnMgr()
Returns:
transaction manager for this database

setTxnMgr

public void setTxnMgr(FarragoSessionTxnMgr txnMgr)
Sets the transaction manager for this database. This is intended only for use by white-box tests; it should not be called otherwise.

Parameters:
txnMgr - new transaction manager

getBootUrlFile

private File getBootUrlFile()

loadBootUrls

private void loadBootUrls()

saveBootUrl

void saveBootUrl(String url)

loadModelPlugins

private void loadModelPlugins()

close

public void close(boolean suppressExcns)

warnOnClose

private void warnOnClose(Throwable ex,
                         boolean suppressExcns)

dumpTraceConfig

private void dumpTraceConfig()

assertNoFennelHandles

private void assertNoFennelHandles()

loadFennel

private boolean loadFennel(FarragoCompoundAllocation startOfWorldAllocation,
                           FennelCmdExecutor cmdExecutor,
                           boolean init)
                    throws Exception
Throws:
Exception

updateCatalog

private void updateCatalog(boolean recoveryRequired)
                    throws Exception
Updates the catalog on startup, taking care of any cleanup or recovery.

Parameters:
recoveryRequired - true if starting up after a crash
Throws:
Exception

simulateCatalogRecovery

public void simulateCatalogRecovery()
                             throws Exception
Simulates the catalog recovery which would occur on startup after a crash. This allows tests to set up a crash representation in the catalog and then invoke recovery without actually having to bring down the JVM.

Throws:
Exception

cleanupBackupData

public void cleanupBackupData(boolean backupSucceeded,
                              boolean setEndTimestamp)
                       throws Exception
Cleans up the system backup catalog by either removing pending backup data if the last backup failed, or by updating the pending data to completed, if the last backup succeeded.

Parameters:
backupSucceeded - whether the last backup was successful
setEndTimestamp - if true, record the current timestamp as the ending timestamp when updating pending data to completed
Throws:
Exception

isPartiallyRestored

public boolean isPartiallyRestored()
Determines if the database is in a state where a partial restore has been done, which indicates that the catalog data is not in sync with table data. Therefore, any attempt to access a table or create/modify an object should result in an error.

Returns:
true if the database is in a partial restore state

filterMapNullValues

private void filterMapNullValues(Map<String,Object> configMap)

getOJRexImplementorTable

public OJRexImplementorTable getOJRexImplementorTable()
Returns:
shared OpenJava implementation table for SQL operators

getSystemRepos

public FarragoRepos getSystemRepos()
Returns:
system repos for this database

getUserRepos

public FarragoRepos getUserRepos()
Returns:
user repos for this database

isAuthenticationEnabled

public boolean isAuthenticationEnabled()
Returns:
true if there is a JAAS configuration entry for application "Farrago".

getAuthenticationConfig

public Configuration getAuthenticationConfig()
Returns:
the JAAS authentication configuration.

getFennelDbHandle

public FennelDbHandle getFennelDbHandle()
Returns:
the Fennel database handle associated with this database

setFennelDbHandle

public void setFennelDbHandle(FennelDbHandle handle)
Sets the fennel DB handle used by the database. Allows non fennel implementations to override the handle and substitute your own implementation


getDdlLockManager

public FarragoDdlLockManager getDdlLockManager()
Returns:
the DDL lock manager associated with this database

getUniqueId

public long getUniqueId()
Gets a unique identifier: never 0.

Returns:
next unique identifier

findSessionInfo

public FarragoSessionInfo findSessionInfo(long id)
look up session info by session id.

Parameters:
id -
Returns:
FarragoSessionInfo

findExecutingStmtInfo

public FarragoSessionExecutingStmtInfo findExecutingStmtInfo(long id)
Looks up executing statement info by statement id.

Parameters:
id -
Returns:
FarragoSessionExecutingStmtInfo

killSession

public void killSession(long id,
                        boolean cancelOnly)
                 throws Throwable
Kills a session.

Parameters:
id - session identifier
cancelOnly - if true, just cancel current execution; if false, destroy session
Throws:
Throwable

kill

private void kill(FarragoSessionExecutingStmtInfo info,
                  boolean cancelOnly)
           throws Throwable
Throws:
Throwable

killExecutingStmt

public void killExecutingStmt(long id,
                              boolean cancelOnly)
                       throws Throwable
Kill an executing statement: cancel it and deallocate it.

Parameters:
id - statement id
cancelOnly - if true, just cancel current execution; if false, destroy statement
Throws:
Throwable

killExecutingStmtMatching

public int killExecutingStmtMatching(String match,
                                     String nomatch,
                                     boolean cancelOnly)
                              throws Throwable
Kills all statements that are executing SQL that matches a given pattern, but does not match a second pattern. Not an error if none match.

Parameters:
match - pattern to match. Null string matches nothing, to be safe.
nomatch - pattern not to match
cancelOnly - if true, just cancel current execution; if false, destroy statement
Returns:
count of killed statements.
Throws:
Throwable

prepareStmt

public FarragoSessionExecutableStmt prepareStmt(FarragoSessionStmtContext stmtContext,
                                                FarragoSessionStmtValidator stmtValidator,
                                                SqlNode sqlNode,
                                                FarragoAllocationOwner owner,
                                                FarragoSessionAnalyzedSql analyzedSql)
Prepares an SQL expression; uses a cached implementation if available, otherwise caches the one generated here.

Parameters:
stmtContext - embracing stmt context
stmtValidator - generic stmt validator
sqlNode - the parsed form of the statement
owner - the FarragoAllocationOwner which will be responsible for the returned stmt
analyzedSql - receives information about a prepared expression
Returns:
statement implementation, or null when analyzedSql is non-null

implementStmt

public FarragoSessionExecutableStmt implementStmt(FarragoSessionPreparingStmt prep,
                                                  RelNode rootRel,
                                                  SqlKind sqlKind,
                                                  boolean logical,
                                                  FarragoAllocationOwner owner)
Implements a logical or physical query plan but does not execute it.

Parameters:
prep - the FarragoSessionPreparingStmt that is managing the query.
rootRel - root of query plan (relational expression)
sqlKind - SqlKind for the relational expression: only SqlKind.Explain and SqlKind.Dml are special cases.
logical - true for a logical query plan (still needs to be optimized), false for a physical plan.
owner - the FarragoAllocationOwner which will be responsible for the returned stmt
Returns:
statement implementation

prepareStmtImpl

private FarragoSessionExecutableStmt prepareStmtImpl(FarragoSessionPreparingStmt stmt,
                                                     SqlNode sqlNode,
                                                     FarragoAllocationOwner owner,
                                                     FarragoSessionAnalyzedSql analyzedSql)

isExecutableStmtStale

private boolean isExecutableStmtStale(FarragoRepos repos,
                                      FarragoSessionExecutableStmt stmt)

updateSystemParameter

public void updateSystemParameter(DdlSetSystemParamStmt ddlStmt)

getCodeCacheMaxBytes

private long getCodeCacheMaxBytes(FemFarragoConfig config)

executeFennelSetParam

private void executeFennelSetParam(String paramName,
                                   SqlLiteral paramValue)

requestCheckpoint

public void requestCheckpoint(boolean fuzzy,
                              boolean async)

deallocateOld

public void deallocateOld()

newSessionFactory

public static FarragoSessionFactory newSessionFactory()

main

public static void main(String[] args)
Main entry point which creates a new Farrago database.

Parameters:
args - ignored

isAuthenticateLocalConnections

public boolean isAuthenticateLocalConnections()

setAuthenticateLocalConnections

public void setAuthenticateLocalConnections(boolean authenticateLocalConnections)
Turns on/off authentication for in-process jdbc sessions. NOTE: If set to off, the origin of the jdbc connection is determined by the remoteProtocol attribute of the jdbc URL.

Parameters:
authenticateLocalConnections -