From f3294ecd2d29ce9dbb02ed43d48881c49f79c698 Mon Sep 17 00:00:00 2001 From: prabatuty Date: Sat, 9 Jun 2007 02:09:14 +0000 Subject: [PATCH] Feature: 1501518 Procmgmt. First patch submission. Not working completely. --- include/Config.h | 3 + include/Database.h | 7 ++ include/DatabaseManagerImpl.h | 21 ++++- include/{SessionImpl.h => Globals.h} | 52 ++--------- include/Makefile.am | 2 +- include/Makefile.in | 2 +- include/{SessionImpl.h => Process.h} | 72 ++++++++-------- include/Session.h | 2 + include/SessionImpl.h | 3 +- include/SqlStatement.h | 8 +- include/os.h | 4 +- src/server/Config.cxx | 7 ++ src/server/Connection.cxx | 7 +- src/server/Database.cxx | 45 ++++++++++ src/server/DatabaseManagerImpl.cxx | 81 ++++++++++++++--- src/server/Makefile | 22 ++--- src/server/Makefile.am | 2 +- src/server/Makefile.in | 6 +- src/server/Process.cxx | 163 +++++++++++++++++++++++++++++++++++ src/server/SessionImpl.cxx | 21 +++-- src/server/os.cxx | 8 +- 21 files changed, 405 insertions(+), 133 deletions(-) copy include/{SessionImpl.h => Globals.h} (53%) copy include/{SessionImpl.h => Process.h} (54%) create mode 100644 src/server/Process.cxx diff --git a/include/Config.h b/include/Config.h index e8b026ac..9a2cf93c 100644 --- a/include/Config.h +++ b/include/Config.h @@ -23,6 +23,7 @@ class ConfigValues int pageSize; int maxTrans; int maxProcs; + int maxThreads; long maxSysSize; long maxDbSize; int sysDbKey; @@ -38,6 +39,7 @@ class ConfigValues pageSize = 8192; maxTrans = 20; maxProcs = 20; + maxThreads = 10; maxSysSize = 1048576; maxDbSize = 1048576; sysDbKey = 2222; @@ -62,6 +64,7 @@ class Config inline int getPageSize() { return cVal.pageSize; } inline int getMaxTrans() { return cVal.maxTrans; } inline int getMaxProcs() { return cVal.maxProcs; } + inline int getMaxThreads() { return cVal.maxThreads; } inline long getMaxSysDbSize() { return cVal.maxSysSize; } inline long getMaxDbSize() { return cVal.maxDbSize; } inline int getSysDbKey() { return cVal.sysDbKey; } diff --git a/include/Database.h b/include/Database.h index 4e3a8c83..17d73081 100644 --- a/include/Database.h +++ b/include/Database.h @@ -64,6 +64,9 @@ class DatabaseMetaData class DatabaseManagerImpl; class Table; +class ProcInfo; +class ThreadInfo; + class Database { private: @@ -83,6 +86,10 @@ class Database Chunk* getSystemDatabaseChunk(int id); Transaction* getSystemDatabaseTrans(int slot); + ProcInfo* getProcInfo(int pidSlot); + ThreadInfo* getThreadInfo(int pidSlot, int thrSlot); + bool isLastThread(); + void createAllCatalogTables(); void* allocLockHashBuckets(); diff --git a/include/DatabaseManagerImpl.h b/include/DatabaseManagerImpl.h index 23e935b3..7728e04a 100644 --- a/include/DatabaseManagerImpl.h +++ b/include/DatabaseManagerImpl.h @@ -19,6 +19,7 @@ #include #include #include +#include class Database; class SessionImpl; @@ -28,7 +29,16 @@ class FieldNameList; class ChunkIterator; class Chunk; class TransactionManager; +class CSqlProcInfo +{ + public: + CSqlProcInfo() { sysDbAttachAddr = userDbAttachAddr = NULL;} + void *sysDbAttachAddr; + void *userDbAttachAddr; +}; +//Global object +static CSqlProcInfo csqlProcInfo; class DatabaseManagerImpl : public DatabaseManager { @@ -43,8 +53,13 @@ class DatabaseManagerImpl : public DatabaseManager TransactionManager *tMgr_; + + ProcessManager *pMgr_; + + //only SessionImpl creates object of this class - DatabaseManagerImpl() { systemDatabase_ = NULL; tMgr_ = NULL; lMgr_ = NULL; db_ = NULL; } + DatabaseManagerImpl() { systemDatabase_ = NULL; tMgr_ = NULL; lMgr_ = NULL; + pMgr_ = NULL; db_ = NULL; } ~DatabaseManagerImpl(); DbRetVal openSystemDatabase(); @@ -80,7 +95,7 @@ class DatabaseManagerImpl : public DatabaseManager DbRetVal deleteDatabase(const char *name); DbRetVal openDatabase(const char *name); - void closeDatabase(); + DbRetVal closeDatabase(); @@ -91,6 +106,8 @@ class DatabaseManagerImpl : public DatabaseManager DbRetVal createIndex(const char *indName, IndexInitInfo *info); DbRetVal dropIndex(const char *name); + DbRetVal registerThread(); + DbRetVal deregisterThread(); friend class SessionImpl; }; diff --git a/include/SessionImpl.h b/include/Globals.h similarity index 53% copy from include/SessionImpl.h copy to include/Globals.h index 0c8b7796..82c73953 100644 --- a/include/SessionImpl.h +++ b/include/Globals.h @@ -13,50 +13,10 @@ * GNU General Public License for more details. * * * ***************************************************************************/ -#ifndef SESSION_IMPL_H -#define SESSION_IMPL_H -#include -#include -#include - -class DatabaseManagerImpl; - -class SessionImpl : public Session -{ - DatabaseManagerImpl *dbMgr; - UserManager *uMgr; - - char userName[IDENTIFIER_LENGTH]; - bool isAuthenticated; - bool isDba; - - public: - SessionImpl() - { - dbMgr = NULL; uMgr = NULL; - } - ~SessionImpl() - { - close(); - - } - //This is used by the server process to initialize and create - //system and user database->create shared memory segment - DbRetVal initSystemDatabase(); - //Removes the shared memory segment->deletes both the databases - DbRetVal destroySystemDatabase(); - - DbRetVal open(const char*username, const char*password); - DbRetVal close(); - - DatabaseManager* getDatabaseManager(); - UserManager* getUserManager(); - - DbRetVal startTransaction(IsolationLevel level); - DbRetVal commit(); - DbRetVal rollback(); - - DbRetVal readConfigFile(); -}; - +#ifndef GLOBAL_H +#define GLOBAL_H +#ifndef APP_PID +#define APP_PID +pid_t appPid = 0; +#endif #endif diff --git a/include/Makefile.am b/include/Makefile.am index 34389f94..ac7cdd7d 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -4,5 +4,5 @@ include_HEADERS = Allocator.h CatalogTables.h CSql.h Database.h \ DatabaseManager.h DatabaseManagerImpl.h DataType.h Debug.h ErrorType.h Field.h Index.h \ Info.h Lock.h Mutex.h NanoTimer.h os.h Predicate.h PredicateImpl.h \ Session.h SessionImpl.h Table.h TableImpl.h Transaction.h UserManager.h \ - UserManagerImpl.h build.h Config.h Util.h \ + UserManagerImpl.h build.h Config.h Process.h Util.h Globals.h \ SqlConnection.h SqlStatement.h diff --git a/include/Makefile.in b/include/Makefile.in index 39846df4..790c0b21 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -172,7 +172,7 @@ include_HEADERS = Allocator.h CatalogTables.h CSql.h Database.h \ DatabaseManager.h DatabaseManagerImpl.h DataType.h Debug.h ErrorType.h Field.h Index.h \ Info.h Lock.h Mutex.h NanoTimer.h os.h Predicate.h PredicateImpl.h \ Session.h SessionImpl.h Table.h TableImpl.h Transaction.h UserManager.h \ - UserManagerImpl.h build.h Config.h Util.h \ + UserManagerImpl.h build.h Config.h Process.h Util.h Globals.h \ SqlConnection.h SqlStatement.h all: all-am diff --git a/include/SessionImpl.h b/include/Process.h similarity index 54% copy from include/SessionImpl.h copy to include/Process.h index 0c8b7796..f345f15e 100644 --- a/include/SessionImpl.h +++ b/include/Process.h @@ -13,50 +13,46 @@ * GNU General Public License for more details. * * * ***************************************************************************/ -#ifndef SESSION_IMPL_H -#define SESSION_IMPL_H -#include -#include -#include +#ifndef PROCESS_H +#define PROCESS_H -class DatabaseManagerImpl; +#include +#include -class SessionImpl : public Session +class ProcInfo { - DatabaseManagerImpl *dbMgr; - UserManager *uMgr; + public: + + pid_t pid_; + + int numThreads_; + +}; + +class ThreadInfo +{ + public: + + pid_t pid_; + + pthread_t thrid_; + + void *want_; //single mutex which we are waiting for. - char userName[IDENTIFIER_LENGTH]; - bool isAuthenticated; - bool isDba; + void *has_; //list of mutexes held +}; +class Database; + +class ProcessManager +{ public: - SessionImpl() - { - dbMgr = NULL; uMgr = NULL; - } - ~SessionImpl() - { - close(); - - } - //This is used by the server process to initialize and create - //system and user database->create shared memory segment - DbRetVal initSystemDatabase(); - //Removes the shared memory segment->deletes both the databases - DbRetVal destroySystemDatabase(); - - DbRetVal open(const char*username, const char*password); - DbRetVal close(); - - DatabaseManager* getDatabaseManager(); - UserManager* getUserManager(); - - DbRetVal startTransaction(IsolationLevel level); - DbRetVal commit(); - DbRetVal rollback(); - - DbRetVal readConfigFile(); + Database *systemDatabase; + ProcInfo *procInfo; + ThreadInfo *thrInfo; + ProcessManager(Database *sysdb) { systemDatabase = sysdb; procInfo = NULL; thrInfo = NULL; } + DbRetVal registerThread(); + DbRetVal deregisterThread(); }; #endif diff --git a/include/Session.h b/include/Session.h index cdbca86e..185cf52e 100644 --- a/include/Session.h +++ b/include/Session.h @@ -130,4 +130,6 @@ class Session }; + + #endif diff --git a/include/SessionImpl.h b/include/SessionImpl.h index 0c8b7796..82b3b534 100644 --- a/include/SessionImpl.h +++ b/include/SessionImpl.h @@ -33,12 +33,11 @@ class SessionImpl : public Session public: SessionImpl() { - dbMgr = NULL; uMgr = NULL; + dbMgr = NULL; uMgr = NULL; } ~SessionImpl() { close(); - } //This is used by the server process to initialize and create //system and user database->create shared memory segment diff --git a/include/SqlStatement.h b/include/SqlStatement.h index 8844821a..7ad79efb 100644 --- a/include/SqlStatement.h +++ b/include/SqlStatement.h @@ -19,9 +19,11 @@ ***************************************************************************/ #ifndef SQLSTATEMENT_H #define SQLSTATEMENT_H -#include -#include "Statement.h" -#include "SqlConnection.h" +#include +//#include "Statement.h" +//#include +class Statement; +class ParsedData; /** * @class SqlStatement diff --git a/include/os.h b/include/os.h index 4cdaa287..e058ab28 100644 --- a/include/os.h +++ b/include/os.h @@ -127,8 +127,8 @@ class os static int gettimeofday(struct timeval *tp); static struct tm* localtime(long *secs); - static int getpid(); - static int getthrid(); + static pid_t getpid(); + static pthread_t getthrid(); static char* getenv(const char *envVarName); }; diff --git a/src/server/Config.cxx b/src/server/Config.cxx index 6575474d..c8125388 100644 --- a/src/server/Config.cxx +++ b/src/server/Config.cxx @@ -38,6 +38,8 @@ int Config::storeKeyVal(char *key, char *value) { cVal.maxTrans = atoi(value); } else if (strcasestr(key, "MAX_PROCS") != NULL) { cVal.maxProcs = atoi(value); } + else if (strcasestr(key, "MAX_THREADS") != NULL) + { cVal.maxThreads = atoi(value); } else if (strcasestr(key, "MAX_SYS_DB_SIZE") != NULL) { cVal.maxSysSize = atol(value); } else if (strcasestr(key, "MAX_DB_SIZE") != NULL) @@ -74,6 +76,11 @@ int Config::validateValues() printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192"); return 1; } + if (cVal.maxThreads < 1 || cVal.maxThreads > 64) + { + printError(ErrBadArg, "MAX_THREADS should be >= 1 and <= 64"); + return 1; + } if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024) { printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB"); diff --git a/src/server/Connection.cxx b/src/server/Connection.cxx index dc31a054..d53f79fa 100644 --- a/src/server/Connection.cxx +++ b/src/server/Connection.cxx @@ -26,7 +26,12 @@ Connection::~Connection() DbRetVal Connection::open(const char *username, const char *password) { - if (session == NULL) session = new SessionImpl(); + if (session == NULL) session = new SessionImpl(); + else + { + printError(ErrAlready, "User already logged in"); + return ErrAlready; + } DbRetVal rv = logger.startLogger(config.getLogFile()); if (rv != OK) { delete session; session = NULL; return rv; } logFinest(logger, "User logged in %s",username); diff --git a/src/server/Database.cxx b/src/server/Database.cxx index 5102d8aa..0ec9fe7e 100644 --- a/src/server/Database.cxx +++ b/src/server/Database.cxx @@ -20,6 +20,8 @@ #include #include #include +#include +extern pid_t appPid; const char* Database::getName() { @@ -394,6 +396,49 @@ Transaction* Database::getSystemDatabaseTrans(int slot) return (Transaction*)(((char*) metaData_) + offset); } +//used in case of system database +ProcInfo* Database::getProcInfo(int pidSlot) +{ + size_t offset = os::alignLong(sizeof (DatabaseMetaData)); + offset = offset + os::alignLong( MAX_CHUNKS * sizeof (Chunk)); + offset = offset + os::alignLong( config.getMaxTrans() * sizeof(Transaction)); + offset = offset + pidSlot * sizeof (ProcInfo); + return (ProcInfo*)(((char*) metaData_) + offset); +} +//used in case of system database +ThreadInfo* Database::getThreadInfo(int pidSlot, int thrSlot) +{ + size_t offset = os::alignLong(sizeof (DatabaseMetaData)); + offset = offset + os::alignLong( MAX_CHUNKS * sizeof (Chunk)); + offset = offset + os::alignLong( config.getMaxTrans() * sizeof(Transaction)); + offset = offset + os::alignLong( config.getMaxProcs() * sizeof(ProcInfo)); + offset = offset + pidSlot * thrSlot * sizeof (ThreadInfo); + return (ThreadInfo*)(((char*) metaData_) + offset); +} + +bool Database::isLastThread() +{ + DbRetVal rv = getProcessTableMutex(); + if (OK != rv) + { + printError(rv, "Unable to get process table mutex from Database::isLastThread()"); + return false; + } + pid_t pid = appPid; + + ThreadInfo *tInfo = getThreadInfo(pid, 0); + int regThr = 0; + for (int i=0; i < config.getMaxThreads(); i++) + { + if (0 != tInfo->thrid_) regThr++; + tInfo++; + } + releaseProcessTableMutex(); + if (regThr < 1) return true; + return false; +} + + bool Database::isValidAddress(void* addr) { if ((char*) addr > ((char*)getMetaDataPtr()) + getMaxSize()) diff --git a/src/server/DatabaseManagerImpl.cxx b/src/server/DatabaseManagerImpl.cxx index 18334d85..92d1ea77 100644 --- a/src/server/DatabaseManagerImpl.cxx +++ b/src/server/DatabaseManagerImpl.cxx @@ -25,6 +25,8 @@ #include #include #include +#include + DatabaseManagerImpl::~DatabaseManagerImpl() { @@ -134,7 +136,9 @@ DbRetVal DatabaseManagerImpl::createDatabase(const char *name, size_t size) { offset = offset + os::alignLong( MAX_CHUNKS * sizeof (Chunk)); offset = offset + os::alignLong( config.getMaxTrans() * sizeof(Transaction)); - offset = offset + os::alignLong( config.getMaxProcs() * sizeof(int)); + offset = offset + os::alignLong( config.getMaxProcs() * sizeof(ProcInfo)); + offset = offset + os::alignLong( config.getMaxProcs() * + config.getMaxThreads() * sizeof(ThreadInfo)); } int multiple = os::floor(offset / PAGE_SIZE); char *curPage = (((char*)rtnAddr) + ((multiple + 1) * PAGE_SIZE)); @@ -204,10 +208,9 @@ DbRetVal DatabaseManagerImpl::openDatabase(const char *name) return ErrAlready; } //system db should be opened before user database files - caddr_t rtnAddr = (caddr_t) NULL; - shared_memory_id shm_id = 0; + shared_memory_id shm_id = 0; shared_memory_key key = 0; if (0 == strcmp(name, SYSTEMDB)) key = config.getSysDbKey(); @@ -220,33 +223,64 @@ DbRetVal DatabaseManagerImpl::openDatabase(const char *name) return ErrOS; } - void *shm_ptr = os::shm_attach(shm_id, startaddr, SHM_RND|SHM_REMAP); + void *shm_ptr = os::shm_attach(shm_id, startaddr, SHM_RND); rtnAddr = (caddr_t) shm_ptr; if (rtnAddr < 0 || shm_ptr == (char*)0xffffffff) { - - printError(ErrOS, "Shared memory attach returned -ve value %x %d", shm_ptr, errno); - return ErrOS; + //check if is the first thread to be registered. + if ( NULL == csqlProcInfo.sysDbAttachAddr) + { + printError(ErrOS, "Shared memory attach returned -ve value %x %d", shm_ptr, errno); + return ErrOS; + } + else + { + db_ = new Database(); + if (0 == strcmp(name, SYSTEMDB)) + db_->setMetaDataPtr((DatabaseMetaData*)csqlProcInfo.sysDbAttachAddr); + else + db_->setMetaDataPtr((DatabaseMetaData*)csqlProcInfo.userDbAttachAddr); + printDebug(DM_Database, "Opening database: %s", name); + logFinest(logger, "Opened database %s" , name); + return OK; + } } - printf("Shared memory attach %x\n", shm_ptr); + + //store it in the global static object. + if (0 == strcmp(name, SYSTEMDB)) + csqlProcInfo.sysDbAttachAddr = rtnAddr; + else + csqlProcInfo.userDbAttachAddr = rtnAddr; + db_ = new Database(); printDebug(DM_Database, "Opening database: %s", name); db_->setMetaDataPtr((DatabaseMetaData*)rtnAddr); + logFinest(logger, "Opened database %s" , name); return OK; } -void DatabaseManagerImpl::closeDatabase() +DbRetVal DatabaseManagerImpl::closeDatabase() { printDebug(DM_Database, "Closing database: %s",(char*)db_->getName()); logFinest(logger, "Closed database"); - if (NULL == db_) return; - os::shm_detach((char*)db_->getMetaDataPtr()); + if (NULL == db_) + { + printError(ErrAlready, "Database is already closed\n"); + return ErrAlready; + } + //check if this is the last thread to be deregistered. + if (db_->isLastThread()) + { + printf("Called last thread and detached db file\n"); + //os::shm_detach((char*)db_->getMetaDataPtr()); + //csqlProcInfo.sysDbAttachAddr = NULL; + //csqlProcInfo.userDbAttachAddr = NULL; + } delete db_; db_ = NULL; - } //Assumes that system database mutex is taken before calling this. Chunk* DatabaseManagerImpl::createUserChunk(size_t size) @@ -747,7 +781,30 @@ DbRetVal DatabaseManagerImpl::dropIndex(const char *name) return OK; } +DbRetVal DatabaseManagerImpl::registerThread() +{ + DbRetVal rv = OK; + if (pMgr_ != NULL) + { + printError(ErrAlready, "Process already registered\n"); + return ErrAlready; + } + pMgr_ = new ProcessManager(sysDb()); + return pMgr_->registerThread(); +} +DbRetVal DatabaseManagerImpl::deregisterThread() +{ + if (pMgr_ == NULL) + { + printError(ErrBadCall, "Process already deregistered or never registered\n"); + return ErrBadCall; + } + DbRetVal rv = pMgr_->deregisterThread(); + delete pMgr_; + pMgr_ = NULL; + return rv; +} ChunkIterator DatabaseManagerImpl::getSystemTableIterator(CatalogTableID id) { diff --git a/src/server/Makefile b/src/server/Makefile index c3674f36..237b66bb 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -63,7 +63,8 @@ am_libcsql_la_OBJECTS = BucketIter.lo BucketList.lo CatalogTables.lo \ FieldList.lo Index.lo LockListIter.lo LockManager.lo Logger.lo \ Mutex.lo os.lo PageInfo.lo PredicateImpl.lo SessionImpl.lo \ TableDef.lo TableImpl.lo Transaction.lo TransactionManager.lo \ - TupleIterator.lo UserManagerImpl.lo HashIndex.lo Config.lo + TupleIterator.lo UserManagerImpl.lo HashIndex.lo Config.lo \ + Process.lo libcsql_la_OBJECTS = $(am_libcsql_la_OBJECTS) binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) PROGRAMS = $(bin_PROGRAMS) @@ -86,14 +87,14 @@ DIST_SOURCES = $(libcsql_la_SOURCES) $(csqlserver_SOURCES) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) -ACLOCAL = ${SHELL} /home/praba/kishore/csql/missing --run aclocal-1.9 +ACLOCAL = ${SHELL} /home/praba/latest/csql/missing --run aclocal-1.9 AMDEP_FALSE = # AMDEP_TRUE = -AMTAR = ${SHELL} /home/praba/kishore/csql/missing --run tar +AMTAR = ${SHELL} /home/praba/latest/csql/missing --run tar AR = ar -AUTOCONF = ${SHELL} /home/praba/kishore/csql/missing --run autoconf -AUTOHEADER = ${SHELL} /home/praba/kishore/csql/missing --run autoheader -AUTOMAKE = ${SHELL} /home/praba/kishore/csql/missing --run automake-1.9 +AUTOCONF = ${SHELL} /home/praba/latest/csql/missing --run autoconf +AUTOHEADER = ${SHELL} /home/praba/latest/csql/missing --run autoheader +AUTOMAKE = ${SHELL} /home/praba/latest/csql/missing --run automake-1.9 AWK = gawk CC = gcc CCDEPMODE = depmode=gcc3 @@ -129,7 +130,7 @@ LIBS = LIBTOOL = $(SHELL) $(top_builddir)/libtool LN_S = ln -s LTLIBOBJS = -MAKEINFO = ${SHELL} /home/praba/kishore/csql/missing --run makeinfo +MAKEINFO = ${SHELL} /home/praba/latest/csql/missing --run makeinfo OBJEXT = o PACKAGE = csql PACKAGE_BUGREPORT = @@ -176,7 +177,7 @@ host_vendor = pc htmldir = ${docdir} includedir = ${prefix}/include infodir = ${datarootdir}/info -install_sh = /home/praba/kishore/csql/install-sh +install_sh = /home/praba/latest/csql/install-sh libdir = ${exec_prefix}/lib libexecdir = ${exec_prefix}/libexec localedir = ${datarootdir}/locale @@ -185,7 +186,7 @@ mandir = ${datarootdir}/man mkdir_p = mkdir -p -- oldincludedir = /usr/include pdfdir = ${docdir} -prefix = /home/praba/kishore/csql/install +prefix = /home/praba/latest/csql/install program_transform_name = s,x,x, psdir = ${docdir} sbindir = ${exec_prefix}/sbin @@ -200,7 +201,7 @@ libcsql_la_SOURCES = BucketIter.cxx BucketList.cxx CatalogTables.cxx Chunk.cxx \ ChunkIterator.cxx Condition.cxx Connection.cxx Database.cxx DatabaseManagerImpl.cxx DataType.cxx \ Debug.cxx FieldList.cxx Index.cxx LockListIter.cxx LockManager.cxx Logger.cxx Mutex.cxx os.cxx \ PageInfo.cxx PredicateImpl.cxx SessionImpl.cxx TableDef.cxx TableImpl.cxx Transaction.cxx \ - TransactionManager.cxx TupleIterator.cxx UserManagerImpl.cxx HashIndex.cxx Config.cxx + TransactionManager.cxx TupleIterator.cxx UserManagerImpl.cxx HashIndex.cxx Config.cxx Process.cxx csqlserver_SOURCES = Server.cxx csqlserver_LDADD = $(top_builddir)/src/server/libcsql.la -lrt -lpthread -lcrypt @@ -325,6 +326,7 @@ include ./$(DEPDIR)/Logger.Plo include ./$(DEPDIR)/Mutex.Plo include ./$(DEPDIR)/PageInfo.Plo include ./$(DEPDIR)/PredicateImpl.Plo +include ./$(DEPDIR)/Process.Plo include ./$(DEPDIR)/Server.Po include ./$(DEPDIR)/SessionImpl.Plo include ./$(DEPDIR)/TableDef.Plo diff --git a/src/server/Makefile.am b/src/server/Makefile.am index 6c45bc9c..fcc2896e 100644 --- a/src/server/Makefile.am +++ b/src/server/Makefile.am @@ -6,7 +6,7 @@ libcsql_la_SOURCES = BucketIter.cxx BucketList.cxx CatalogTables.cxx Chunk.cxx \ ChunkIterator.cxx Condition.cxx Connection.cxx Database.cxx DatabaseManagerImpl.cxx DataType.cxx \ Debug.cxx FieldList.cxx Index.cxx LockListIter.cxx LockManager.cxx Logger.cxx Mutex.cxx os.cxx \ PageInfo.cxx PredicateImpl.cxx SessionImpl.cxx TableDef.cxx TableImpl.cxx Transaction.cxx \ - TransactionManager.cxx TupleIterator.cxx UserManagerImpl.cxx HashIndex.cxx Config.cxx + TransactionManager.cxx TupleIterator.cxx UserManagerImpl.cxx HashIndex.cxx Config.cxx Process.cxx bin_PROGRAMS = csqlserver csqlserver_SOURCES = Server.cxx csqlserver_LDADD = $(top_builddir)/src/server/libcsql.la -lrt -lpthread -lcrypt diff --git a/src/server/Makefile.in b/src/server/Makefile.in index 1e07ee22..d41dbda4 100644 --- a/src/server/Makefile.in +++ b/src/server/Makefile.in @@ -63,7 +63,8 @@ am_libcsql_la_OBJECTS = BucketIter.lo BucketList.lo CatalogTables.lo \ FieldList.lo Index.lo LockListIter.lo LockManager.lo Logger.lo \ Mutex.lo os.lo PageInfo.lo PredicateImpl.lo SessionImpl.lo \ TableDef.lo TableImpl.lo Transaction.lo TransactionManager.lo \ - TupleIterator.lo UserManagerImpl.lo HashIndex.lo Config.lo + TupleIterator.lo UserManagerImpl.lo HashIndex.lo Config.lo \ + Process.lo libcsql_la_OBJECTS = $(am_libcsql_la_OBJECTS) binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) PROGRAMS = $(bin_PROGRAMS) @@ -200,7 +201,7 @@ libcsql_la_SOURCES = BucketIter.cxx BucketList.cxx CatalogTables.cxx Chunk.cxx \ ChunkIterator.cxx Condition.cxx Connection.cxx Database.cxx DatabaseManagerImpl.cxx DataType.cxx \ Debug.cxx FieldList.cxx Index.cxx LockListIter.cxx LockManager.cxx Logger.cxx Mutex.cxx os.cxx \ PageInfo.cxx PredicateImpl.cxx SessionImpl.cxx TableDef.cxx TableImpl.cxx Transaction.cxx \ - TransactionManager.cxx TupleIterator.cxx UserManagerImpl.cxx HashIndex.cxx Config.cxx + TransactionManager.cxx TupleIterator.cxx UserManagerImpl.cxx HashIndex.cxx Config.cxx Process.cxx csqlserver_SOURCES = Server.cxx csqlserver_LDADD = $(top_builddir)/src/server/libcsql.la -lrt -lpthread -lcrypt @@ -325,6 +326,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mutex.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PageInfo.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PredicateImpl.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Process.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Server.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SessionImpl.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TableDef.Plo@am__quote@ diff --git a/src/server/Process.cxx b/src/server/Process.cxx new file mode 100644 index 00000000..6c12556a --- /dev/null +++ b/src/server/Process.cxx @@ -0,0 +1,163 @@ +/*************************************************************************** + * Copyright (C) 2007 by www.databasecache.com * + * Contact: praba_tuty@databasecache.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + ***************************************************************************/ +#include +#include +#include +#include +#include +#include + + +//It does not check for re registering as well as deregistering unregistered threads. +//as it is handled in the connection class open and close methods. +DbRetVal ProcessManager::registerThread() +{ + DbRetVal rv = systemDatabase->getProcessTableMutex(); + if (OK != rv) + { + printError(rv,"Unable to get process table mutex"); + return rv; + } + pid_t pid; + if (appPid == 0) + { + //first thread to register + pid = os::getpid(); + appPid = pid; + } else + { + pid = appPid; + } + pthread_t thrid = os::getthrid(); + printf("global pid is %d\n", appPid); + ProcInfo* pInfo = systemDatabase->getProcInfo(0); + int i=0; + ProcInfo* freeSlot = NULL; + int freeSlotPos =0; + bool freeSlotSelected = false; + for (; i < config.getMaxProcs(); i++) + { + //printf("Reg: Address %x, pid %d lpid %d:\n", pInfo, pInfo->pid_, pid); + if (pInfo->pid_ == pid ) break; + if (!freeSlotSelected && 0 == pInfo->pid_) + { + freeSlot = pInfo; + freeSlotPos = i; + freeSlotSelected=true; + } + pInfo++; + } + if (i == config.getMaxProcs()) + { + //First thread to register. + printf("Register: First thread to register pid %d\n", freeSlotPos); + ThreadInfo *tInfo =systemDatabase->getThreadInfo(freeSlotPos, 0); + tInfo->thrid_ = thrid; + tInfo->pid_ = pid; + freeSlot->pid_ = pid; + //printf("Address:: %x\n", freeSlot); + freeSlot->numThreads_ = 1; + } else + { + ThreadInfo *tInfo = systemDatabase->getThreadInfo(i, 0); + int j =0; + for (; j < config.getMaxThreads(); j++) + { + printf("Reg: Address %x, tid %d :\n", tInfo, tInfo->thrid_); + + if (0 == tInfo->thrid_) + { + tInfo->thrid_ = thrid; + tInfo->pid_ = pid; + break; + } + tInfo++; + } + if ( j == config.getMaxThreads()) + { + systemDatabase->releaseProcessTableMutex(); + printError(ErrNoResource, "No free thread slot. Limit reached"); + return ErrNoResource; + } + printf("Register:pid slot taken is %d and thrslot %d\n", i, j); + + pInfo->numThreads_++; + } + systemDatabase->releaseProcessTableMutex(); + return OK; +} +DbRetVal ProcessManager::deregisterThread() +{ + DbRetVal rv = systemDatabase->getProcessTableMutex(); + if (OK != rv) + { + printError(rv,"Unable to get process table mutex"); + return rv; + } + if (appPid == 0) + { + systemDatabase->releaseProcessTableMutex(); + printError(ErrSysInternal, "Process not registered\n"); + return ErrSysInternal; + } + pid_t pid = appPid; + pthread_t thrid = os::getthrid(); + + ProcInfo* pInfo = systemDatabase->getProcInfo(0); + int i=0; + for (; i < config.getMaxProcs(); i++) + { + //printf("Address %x, pid %d :", pInfo, pInfo->pid_); + if (pInfo->pid_ == pid ) break; + pInfo++; + } + if (i == config.getMaxProcs()) + { + systemDatabase->releaseProcessTableMutex(); + printError(ErrSysFatal, "Degistering process %d is not registered with csql", pid); + return ErrNoResource; + } + ThreadInfo *tInfo = systemDatabase->getThreadInfo(i, 0); + printf("Deregister: pid slot taken is %d ", i); + + i=0; + for (; i < config.getMaxThreads(); i++) + { + if (thrid == tInfo->thrid_) + { + tInfo->thrid_ = 0; + tInfo->pid_ = 0; + tInfo->want_ = NULL; + tInfo->has_ = NULL; + break; + } + tInfo++; + } + if ( i == config.getMaxThreads()) + { + systemDatabase->releaseProcessTableMutex(); + printError(ErrSysFatal, "Degistering Pid %d Thread %d is not registered with csql", pid, thrid); + return ErrNoResource; + } + pInfo->numThreads_--; + printf("and thrslot %d nothreads %d\n", i ,pInfo->numThreads_ ); + + if (0 == pInfo->numThreads_) pInfo->pid_ = 0; + + systemDatabase->releaseProcessTableMutex(); + return OK; +} + diff --git a/src/server/SessionImpl.cxx b/src/server/SessionImpl.cxx index e798dfef..6735a156 100644 --- a/src/server/SessionImpl.cxx +++ b/src/server/SessionImpl.cxx @@ -128,9 +128,12 @@ DbRetVal SessionImpl::open(const char *username, const char *password) return rv; } - //TODO::process registration - //pMgr = new ProcessManager(); - //pMgr->registerProcess(); + rv = dbMgr->registerThread(); + if (OK != rv) + { + printError(rv,"Unable to register to csql server"); + return rv; + } rv = dbMgr->sysDb()->getDatabaseMutex(); if (OK != rv) @@ -162,16 +165,18 @@ DbRetVal SessionImpl::open(const char *username, const char *password) DbRetVal SessionImpl::close() { + DbRetVal rv = OK; if (dbMgr) { - dbMgr->closeDatabase(); - dbMgr->closeSystemDatabase(); + rv = dbMgr->deregisterThread(); + if (rv != OK) return ErrBadCall; + rv = dbMgr->closeDatabase(); + if (rv != OK) return ErrBadCall; + rv = dbMgr->closeSystemDatabase(); + if (rv != OK) return ErrBadCall; delete dbMgr; dbMgr = NULL; } - //pMgr->deregisterProcess(); - //delete pMgr; - //pMgr = NULL; if (uMgr) { delete uMgr; diff --git a/src/server/os.cxx b/src/server/os.cxx index 2a59d977..fcb82ceb 100644 --- a/src/server/os.cxx +++ b/src/server/os.cxx @@ -68,13 +68,13 @@ struct tm* os::localtime(long *secs) return ::localtime(secs); } -int os::getpid() +pid_t os::getpid() { - return ::getpid(); + return ::getpid(); } -int os::getthrid() +pthread_t os::getthrid() { - return pthread_self(); + return ::pthread_self(); } -- 2.11.4.GIT