From 8c0814564c3f984d48460bdc30dcd7f566750134 Mon Sep 17 00:00:00 2001 From: prabatuty Date: Wed, 12 Mar 2008 13:06:05 +0000 Subject: [PATCH] changes for Gateway with OSYNC Mode. Moved the per table propagation mode to per transaction mode. beginTrans() accepts the propagation mode as last argument. --- csqltable.conf | 2 +- include/AbsSqlConnection.h | 8 +++- include/CacheTableLoader.h | 8 ---- include/Network.h | 4 +- include/SqlConnection.h | 3 +- include/SqlGwConnection.h | 21 ++++++--- include/SqlGwStatement.h | 4 +- include/SqlLogConnection.h | 19 ++++++--- include/SqlLogStatement.h | 10 +---- include/SqlOdbcConnection.h | 3 +- include/Util.h | 16 +++---- src/adapter/SqlOdbcConnection.cxx | 6 ++- src/adapter/SqlOdbcStatement.cxx | 78 ++++++++++++++++++++++++++++++++-- src/cache/CacheTableLoader.cxx | 10 ++--- src/network/NetworkTable.cxx | 2 +- src/server/Config.cxx | 6 +-- src/sqllog/SqlLogConnection.cxx | 85 ++++++++++++++++++++++++++----------- src/sqllog/SqlLogStatement.cxx | 89 +++++++-------------------------------- src/tools/cachetable.cxx | 11 +---- src/tools/isql.cxx | 4 +- src/tools/repltable.cxx | 8 ---- 21 files changed, 221 insertions(+), 176 deletions(-) diff --git a/csqltable.conf b/csqltable.conf index 738c5eae..6fff7e0a 100644 --- a/csqltable.conf +++ b/csqltable.conf @@ -1 +1 @@ -1:1:t1 +1:t1 diff --git a/include/AbsSqlConnection.h b/include/AbsSqlConnection.h index 5b904970..4da3f0b3 100644 --- a/include/AbsSqlConnection.h +++ b/include/AbsSqlConnection.h @@ -21,6 +21,11 @@ #define ABSSQLCONNECTION_H #include +enum TransSyncMode { + OSYNC=1, + TSYNC=2 +}; + /** * @class AbsSqlConnection * @@ -84,7 +89,8 @@ class AbsSqlConnection * @param isoLevel isolation level. Default is read committed. * @return DbRetVal */ - virtual DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED) = 0; + virtual DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED, + TransSyncMode mode = OSYNC) = 0; virtual ~AbsSqlConnection(){} }; diff --git a/include/CacheTableLoader.h b/include/CacheTableLoader.h index e649fe52..4ca14ca6 100644 --- a/include/CacheTableLoader.h +++ b/include/CacheTableLoader.h @@ -24,13 +24,10 @@ class CacheTableLoader char userName[IDENTIFIER_LENGTH]; char password[IDENTIFIER_LENGTH]; - DataSyncMode syncMode; - public: CacheTableLoader(){strcpy(tableName,"");} void setConnParam(char *user, char *pass){ strcpy(userName, user); strcpy(password, pass); } void setTable(char *tablename) { strcpy(tableName,tablename); } - void setSyncMode(DataSyncMode mode) { syncMode = mode; } DbRetVal addToCacheTableFile(); DbRetVal removeFromCacheTableFile(); DbRetVal load(bool tabDef=true); @@ -38,12 +35,7 @@ class CacheTableLoader DbRetVal unload(bool tabDefinition = true); DbRetVal refresh(); DbRetVal recoverAllCachedTables(); - - - DbRetVal load(DatabaseManager *dbMgr, bool tabDef); - - }; class BindBuffer diff --git a/include/Network.h b/include/Network.h index af3fb036..c24ad991 100644 --- a/include/Network.h +++ b/include/Network.h @@ -23,11 +23,13 @@ #include #include -enum DataSyncMode { +/*enum DataSyncMode { NOSYNC=0, + OSYNC=1, TSYNC=1, ASYNC=2 }; +*/ enum NetworkPacketType { NW_PKT_PREPARE =1, diff --git a/include/SqlConnection.h b/include/SqlConnection.h index b9e77562..f3b650d7 100644 --- a/include/SqlConnection.h +++ b/include/SqlConnection.h @@ -68,7 +68,8 @@ class SqlConnection : public AbsSqlConnection * @param isoLevel isolation level. Default is read committed. * @return DbRetVal */ - DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED) + DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED, + TransSyncMode mode = OSYNC) { return conn.startTransaction(isoLevel); } Connection& getConnObject(){ return conn; } diff --git a/include/SqlGwConnection.h b/include/SqlGwConnection.h index 0180a154..bd2c0962 100644 --- a/include/SqlGwConnection.h +++ b/include/SqlGwConnection.h @@ -22,11 +22,15 @@ #include #include #include +#include -/** -* @class SqlGwConnection -* -*/ +enum GwHandler +{ + NoHandler =0, + CSqlHandler = 1, + AdapterHandler =2, + CSqlAndAdapterHandler =3 +}; class SqlGwConnection : public AbsSqlConnection { Connection dummyConn; @@ -37,7 +41,11 @@ class SqlGwConnection : public AbsSqlConnection char password[IDENTIFIER_LENGTH]; public: - SqlGwConnection(){innerConn = NULL; } + GwHandler txnHdlr; + TransSyncMode mode; + SqlGwConnection(){innerConn = NULL; mode = OSYNC; } + void setTxnHandler(GwHandler hdlr) { txnHdlr = hdlr; } + GwHandler getTxnHandler() { return txnHdlr; } DbRetVal connect (char *user, char * pass); @@ -47,7 +55,7 @@ class SqlGwConnection : public AbsSqlConnection DbRetVal rollback(); - DbRetVal beginTrans (IsolationLevel isoLevel); + DbRetVal beginTrans (IsolationLevel isoLevel, TransSyncMode mode = OSYNC); friend class SqlFactory; @@ -56,6 +64,7 @@ class SqlGwConnection : public AbsSqlConnection DbRetVal connectCSqlIfNotConnected(); DbRetVal connectAdapterIfNotConnected(); + //Note::forced to implement this as it is pure virtual in base class Connection& getConnObject(){ return dummyConn; } }; diff --git a/include/SqlGwStatement.h b/include/SqlGwStatement.h index e876bffd..f2942251 100644 --- a/include/SqlGwStatement.h +++ b/include/SqlGwStatement.h @@ -26,7 +26,7 @@ class SqlGwStatement: public AbsSqlStatement { AbsSqlStatement *adapter; - bool isAdapterHanding; + GwHandler stmtHdlr; public: SqlGwStatement(){innerStmt = NULL; adapter = NULL; con = NULL;} void setAdapter(AbsSqlStatement *stmt) { adapter = stmt; } @@ -76,6 +76,8 @@ class SqlGwStatement: public AbsSqlStatement bool isSelect(); private: + bool shouldAdapterHandle(); + bool shouldCSqlHandle(); friend class SqlFactory; }; diff --git a/include/SqlLogConnection.h b/include/SqlLogConnection.h index 7a5cd2a2..59f31888 100644 --- a/include/SqlLogConnection.h +++ b/include/SqlLogConnection.h @@ -25,6 +25,11 @@ #include #include +class CachedTable{ + public: + char tableName[IDENTIFIER_LENGTH]; +}; + /** * @class SqlLogConnection * @@ -42,17 +47,21 @@ class SqlLogConnection : public AbsSqlConnection List prepareStore; //sync mode of the current transaction - //will be modified by the SqlLogStatement based on the table - DataSyncMode syncMode; + TransSyncMode syncMode; //stores client objects in it for peer NetworkTable nwTable; static UniqueID txnUID; + static List cacheList; + DbRetVal populateCachedTableList(); + public: SqlLogConnection(){innerConn = NULL; syncMode = TSYNC;} + bool isTableCached(char *name); + //Note::forced to implement this as it is pure virtual in base class Connection& getConnObject(){ return dummyConn; } @@ -64,15 +73,15 @@ class SqlLogConnection : public AbsSqlConnection DbRetVal rollback(); - DbRetVal beginTrans (IsolationLevel isoLevel); + DbRetVal beginTrans (IsolationLevel isoLevel, TransSyncMode mode); DbRetVal addPacket(BasePacket *pkt); DbRetVal addPreparePacket(PacketPrepare *pkt); DbRetVal removePreparePacket(int stmtid); - DbRetVal setSyncMode(DataSyncMode mode); - DataSyncMode getSyncMode() { return syncMode; } + DbRetVal setSyncMode(TransSyncMode mode); + TransSyncMode getSyncMode() { return syncMode; } DbRetVal sendAndReceive(NetworkPacketType type, char *packet, int length); friend class SqlFactory; }; diff --git a/include/SqlLogStatement.h b/include/SqlLogStatement.h index f8cf7d04..f79572bd 100644 --- a/include/SqlLogStatement.h +++ b/include/SqlLogStatement.h @@ -23,11 +23,6 @@ #include #include #include -class TableSyncMode{ - public: - char tableName[IDENTIFIER_LENGTH]; - DataSyncMode mode; -}; class SqlLogStatement: public AbsSqlStatement { public: @@ -40,7 +35,6 @@ class SqlLogStatement: public AbsSqlStatement } bool isNonSelectDML(char *stmtstr); - DbRetVal getTableSyncMode(); DbRetVal prepare(char *stmt); DbRetVal execute(int &rowsAffect); @@ -77,15 +71,13 @@ class SqlLogStatement: public AbsSqlStatement void setTimeStampParam(int paramPos, TimeStamp value); bool isSelect(); + bool isCached; private: - DbRetVal populateSyncModeList(); bool needLog; - DataSyncMode syncMode; int sid; //statement id List paramList; - static List syncModeList; static UniqueID stmtUID; friend class SqlFactory; }; diff --git a/include/SqlOdbcConnection.h b/include/SqlOdbcConnection.h index 95ed0662..7c9c581f 100644 --- a/include/SqlOdbcConnection.h +++ b/include/SqlOdbcConnection.h @@ -36,6 +36,7 @@ class SqlOdbcConnection : public AbsSqlConnection public: SQLHENV envHdl; SQLHDBC dbHdl; + IsolationLevel prevIsoLevel; SqlOdbcConnection(){innerConn = NULL; } //Note::forced to implement this as it is pure virtual in base class @@ -49,7 +50,7 @@ class SqlOdbcConnection : public AbsSqlConnection DbRetVal rollback(); - DbRetVal beginTrans (IsolationLevel isoLevel); + DbRetVal beginTrans (IsolationLevel isoLevel, TransSyncMode mode); friend class SqlFactory; }; diff --git a/include/Util.h b/include/Util.h index 6df4e080..bd4ecf35 100644 --- a/include/Util.h +++ b/include/Util.h @@ -74,14 +74,16 @@ class Identifier class List { ListNode *head; + int totalElements; public: - List() { head = NULL;} + List() { head = NULL; totalElements = 0;} DbRetVal append(void *elem) { ListNode *newNode = new ListNode(); newNode->element = elem; newNode->next = NULL; + totalElements++; //If this is the first node, set it as head if (NULL == head) { head = newNode; return OK; } @@ -106,6 +108,7 @@ class List { prev->next = iter->next; delete iter; + totalElements--; if (iter == head) { head = NULL; return OK;} return OK; } @@ -161,19 +164,12 @@ class List } delete iter; head = NULL; + totalElements = 0; return; } int size() { - int count =1; - if (NULL == head) return 0; - ListNode *iter = head; - while (iter->next != NULL) - { - count++; - iter = iter->next; - } - return count; + return totalElements; } }; diff --git a/src/adapter/SqlOdbcConnection.cxx b/src/adapter/SqlOdbcConnection.cxx index 8dc82bde..a25cff81 100644 --- a/src/adapter/SqlOdbcConnection.cxx +++ b/src/adapter/SqlOdbcConnection.cxx @@ -61,12 +61,13 @@ DbRetVal SqlOdbcConnection::disconnect() SQLFreeHandle (SQL_HANDLE_ENV, envHdl); return rv; } -DbRetVal SqlOdbcConnection::beginTrans(IsolationLevel isoLevel) +DbRetVal SqlOdbcConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode) { + if (prevIsoLevel == isoLevel) return OK; DbRetVal rv = OK; int retVal =0; SQLPOINTER iso; - + switch(isoLevel) { case READ_UNCOMMITTED: @@ -85,6 +86,7 @@ DbRetVal SqlOdbcConnection::beginTrans(IsolationLevel isoLevel) retVal = SQLSetConnectAttr(dbHdl, SQL_ATTR_TXN_ISOLATION, iso, 0); if (!SQL_SUCCEEDED(retVal)) return ErrSysInit; + prevIsoLevel = isoLevel; retVal = SQLTransact (envHdl, dbHdl, SQL_ROLLBACK); if (!SQL_SUCCEEDED(retVal)) rv = ErrSysInit; return rv; diff --git a/src/adapter/SqlOdbcStatement.cxx b/src/adapter/SqlOdbcStatement.cxx index 5f237e72..edec6ee0 100644 --- a/src/adapter/SqlOdbcStatement.cxx +++ b/src/adapter/SqlOdbcStatement.cxx @@ -276,7 +276,50 @@ void* SqlOdbcStatement::fetch() void* SqlOdbcStatement::fetchAndPrint() { - return NULL; + if (!isPrepared) return NULL; + int retValue = SQLFetch (hstmt); + if (retValue) return NULL; + ListIterator iter = bindList.getIterator(); + BindSqlProjectField *bindField = NULL; + void *ptrToFirstField = NULL; + while (iter.hasElement()) + { + bindField = (BindSqlProjectField*)iter.nextElement(); + switch(bindField->type) + { + case typeDate: { + Date dtCSQL; + DATE_STRUCT *dtTarget = (DATE_STRUCT*) bindField->targetvalue; + dtCSQL.set(dtTarget->year,dtTarget->month,dtTarget->day); + AllDataType::printVal(&dtCSQL, bindField->type, bindField->length); + break; + } + case typeTime: { + Time dtCSQL; + TIME_STRUCT *dtTarget = (TIME_STRUCT*) bindField->targetvalue; + dtCSQL.set(dtTarget->hour,dtTarget->minute,dtTarget->second); + AllDataType::printVal(&dtCSQL, bindField->type, bindField->length); + break; + } + case typeTimeStamp: { + TimeStamp dtCSQL; + TIMESTAMP_STRUCT *dtTarget = (TIMESTAMP_STRUCT*) bindField->targetvalue; + dtCSQL.setDate(dtTarget->year,dtTarget->month,dtTarget->day); + dtCSQL.setTime(dtTarget->hour,dtTarget->minute, + dtTarget->second, dtTarget->fraction); + AllDataType::printVal(&dtCSQL, bindField->type, bindField->length); + break; + } + default: { + AllDataType::printVal(bindField->targetvalue, + bindField->type, bindField->length); + break; + } + } + if (ptrToFirstField == NULL) ptrToFirstField=bindField->targetvalue; + } + + return ptrToFirstField; } void* SqlOdbcStatement::next() @@ -310,12 +353,41 @@ int SqlOdbcStatement::noOfParamFields() DbRetVal SqlOdbcStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo) { - return OK; + ListIterator biter = bindList.getIterator(); + BindSqlProjectField *elem = NULL; + int count =0; + while (biter.hasElement()) + { + elem = (BindSqlProjectField*) biter.nextElement(); + if (count == projpos) + { + strcpy(fInfo->fldName, elem->fName); + fInfo->length = elem->length; + fInfo->type =elem->type; + return OK; + } + count++; + } + return ErrNotFound; } DbRetVal SqlOdbcStatement::getParamFldInfo (int parampos, FieldInfo *&fInfo) { - return OK; + ListIterator biter = paramList.getIterator(); + BindSqlField *elem = NULL; + int count =0; + while (biter.hasElement()) + { + elem = (BindSqlField*) biter.nextElement(); + if (count == parampos) + { + fInfo->length = elem->length; + fInfo->type =elem->type; + return OK; + } + count++; + } + return ErrNotFound; } DbRetVal SqlOdbcStatement::free() diff --git a/src/cache/CacheTableLoader.cxx b/src/cache/CacheTableLoader.cxx index ad2ac168..43fcadaa 100644 --- a/src/cache/CacheTableLoader.cxx +++ b/src/cache/CacheTableLoader.cxx @@ -27,7 +27,7 @@ DbRetVal CacheTableLoader::addToCacheTableFile() //TODO::if already table present in the file, it means that the //table is replicated. in this case change mode from //2 to 3 (repl to replcache) - fprintf(fp, "%d:%d:%s\n", 1, syncMode, tableName); + fprintf(fp, "%d:%d:%s\n", 1, tableName); fclose(fp); return OK; } @@ -49,12 +49,11 @@ DbRetVal CacheTableLoader::removeFromCacheTableFile() } char tablename[IDENTIFIER_LENGTH]; int mode; - DataSyncMode syncmode; while(!feof(fp)) { - fscanf(fp, "%d:%d:%s\n", &mode, &syncmode, tablename); + fscanf(fp, "%d:%s\n", &mode, tablename); if (strcmp (tablename, tableName) == 0) continue; - fprintf(tmpfp, "%d:%d:%s\n", mode, syncmode, tablename); + fprintf(tmpfp, "%d:%s\n", mode, tablename); } fclose(tmpfp); fclose(fp); @@ -307,10 +306,9 @@ DbRetVal CacheTableLoader::recoverAllCachedTables() //TODO::take exclusive lock on database char tablename[IDENTIFIER_LENGTH]; int mode; - DataSyncMode syncMode; while(!feof(fp)) { - fscanf(fp, "%d:%d:%s\n", &mode, &syncMode, tablename); + fscanf(fp, "%d:%s\n", &mode, tablename); if (mode ==2 ) //just replicated table and not cached continue; printf("Recovering Table from target db: %s\n", tablename); diff --git a/src/network/NetworkTable.cxx b/src/network/NetworkTable.cxx index 59299856..a13e7fd5 100644 --- a/src/network/NetworkTable.cxx +++ b/src/network/NetworkTable.cxx @@ -52,7 +52,7 @@ DbRetVal NetworkTable::readNetworkConfig() return ErrNotYet; } printf("Count is %d\n", count); - fscanf(fp, "%d:%c:%d:%s\n", &nwid, &port, hostname); + fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname); printf( "%d:%d:%s\n", nwid, port, hostname); NetworkClient* nClient; if (nwid == Conf::config.getNetworkID()) continue; diff --git a/src/server/Config.cxx b/src/server/Config.cxx index f24fe66d..2aaed01f 100644 --- a/src/server/Config.cxx +++ b/src/server/Config.cxx @@ -23,7 +23,7 @@ int Config::readLine(FILE *fp, char * buffer) { char c =0; int count =0; - while ( true) + while (true) { c = fgetc(fp); if (c == '\n') break; @@ -222,7 +222,7 @@ int Config::validateValues() char nwmode; while(!feof(fp)) { - fscanf(fp, "%d:%c:%d:%s\n", &nwid, &nwmode, &port, hostname); + fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname); count++; } if (count >2) { @@ -250,7 +250,7 @@ int Config::validateValues() } bool found = false; while(!feof(fp)) { - fscanf(fp, "%d:%c:%d:%s\n", &nwid, &nwmode, &port, hostname); + fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname); if (cVal.cacheNetworkID == nwid) found = true; } if (!found) return 1; diff --git a/src/sqllog/SqlLogConnection.cxx b/src/sqllog/SqlLogConnection.cxx index bb630644..1f7d7175 100644 --- a/src/sqllog/SqlLogConnection.cxx +++ b/src/sqllog/SqlLogConnection.cxx @@ -22,6 +22,7 @@ #include UniqueID SqlLogConnection::txnUID; +List SqlLogConnection::cacheList; DbRetVal SqlLogConnection::addPacket(BasePacket* pkt) { @@ -53,12 +54,6 @@ DbRetVal SqlLogConnection::removePreparePacket(int stmtid) return OK; } -DbRetVal SqlLogConnection::setSyncMode(DataSyncMode mode) -{ - if (syncMode < mode) syncMode = mode; - //printf("Current syncMode is %d\n", syncMode); - return OK; -} DbRetVal SqlLogConnection::connect (char *user, char * pass) { DbRetVal rv = OK; @@ -66,10 +61,13 @@ DbRetVal SqlLogConnection::connect (char *user, char * pass) if (innerConn) rv = innerConn->connect(user,pass); if (rv != OK) return rv; if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK; - rv = nwTable.initialize(); - if (rv !=OK) { innerConn->disconnect(); return rv; } - nwTable.connect(); - //printf("PRABAAAAAAA==you are here\n"); + //rv = nwTable.initialize(); + //if (rv !=OK) { innerConn->disconnect(); return rv; } + //nwTable.connect(); + + //populate cacheList if not populated by another thread in same process + //TODO::cacheList requires mutex guard + if (0 == cacheList.size()) rv = populateCachedTableList(); return rv; } @@ -80,18 +78,16 @@ DbRetVal SqlLogConnection::disconnect() if (innerConn) rv =innerConn->disconnect(); if (rv != OK) return rv; if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK; - nwTable.disconnect(); - nwTable.destroy(); + //nwTable.disconnect(); + //nwTable.destroy(); return rv; } -DbRetVal SqlLogConnection::beginTrans(IsolationLevel isoLevel) +DbRetVal SqlLogConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode) { DbRetVal rv = OK; - //printf("LOG: beginTrans\n"); if (innerConn) rv = innerConn->beginTrans(isoLevel); if (rv != OK) return rv; - rv = nwTable.connectIfNotConnected(); -printf("RV FROM if not connected %d\n", rv); + /*rv = nwTable.connectIfNotConnected(); if (rv == OK) { //send all prepare packets to client ListIterator iter = prepareStore.getIterator(); @@ -105,22 +101,22 @@ printf("RV FROM if not connected %d\n", rv); delete pkt; } prepareStore.reset(); - } - //reset the mode to the highest mode - //SqlLogStatement will reduce the mode based on the tables it operate on - //all the tables modified in this txn should be TSYNC for txn to be in TSYNC - //if any one table is ASYNC, then it gets further downgraded to ASYNC - syncMode = TSYNC; - + }*/ + syncMode = mode; return OK; } DbRetVal SqlLogConnection::commit() { DbRetVal rv = OK; //printf("LOG: commit %d\n", syncMode); + if (innerConn) rv = innerConn->commit(); + return rv; + + //TODO::TSYNC MODE if (logStore.size() == 0) { - //This means no execution for any statements in this transaction + //This means no execution for any non select statements in + //this transaction //rollback so that subsequent beginTrans will not report that //transaction is already started if (innerConn) rv = innerConn->rollback(); @@ -183,6 +179,47 @@ DbRetVal SqlLogConnection::rollback() logStore.reset(); return rv; } +DbRetVal SqlLogConnection::populateCachedTableList() +{ + FILE *fp = NULL; + fp = fopen(Conf::config.getTableConfigFile(),"r"); + if( fp == NULL ) { + printError(ErrSysInit, "cache.table file does not exist"); + return ErrSysInit; + } + char tablename[IDENTIFIER_LENGTH]; + int cmode; + CachedTable *node; + while(!feof(fp)) + { + fscanf(fp, "%d:%s\n", &cmode, tablename); + node = new CachedTable(); + strcpy(node->tableName, tablename); + cacheList.append(node); + } + fclose(fp); + return OK; +} +bool SqlLogConnection::isTableCached(char *tblName) +{ + if (NULL == tblName) + { + printError(ErrBadArg, "tblName passed is NULL\n"); + return ErrBadArg; + } + ListIterator iter = cacheList.getIterator(); + CachedTable *node; + while (iter.hasElement()) { + node = (CachedTable*)iter.nextElement(); + if (strcmp(node->tableName, tblName) == 0) + { + return true; + } + } + return false; +} + + DbRetVal SqlLogConnection::sendAndReceive(NetworkPacketType type, char *packet, int length) { NetworkClient* nwClient = nwTable.getNetworkClient(); diff --git a/src/sqllog/SqlLogStatement.cxx b/src/sqllog/SqlLogStatement.cxx index 1974d115..b7786ca1 100644 --- a/src/sqllog/SqlLogStatement.cxx +++ b/src/sqllog/SqlLogStatement.cxx @@ -19,7 +19,6 @@ ***************************************************************************/ #include -List SqlLogStatement::syncModeList; UniqueID SqlLogStatement::stmtUID; bool SqlLogStatement::isNonSelectDML(char *stmtstr) @@ -31,84 +30,27 @@ bool SqlLogStatement::isNonSelectDML(char *stmtstr) return false; } -//also sets needLog and syncMod3 -DbRetVal SqlLogStatement::getTableSyncMode() -{ - if (0 == syncModeList.size()) - { - printError(ErrSysInit, "syncModelist not populated\n"); - return ErrSysInit; - } - ListIterator iter = syncModeList.getIterator(); - TableSyncMode *node; - char* tblName = innerStmt->getTableName(); - if (NULL == tblName) - { - printError(ErrSysInit, "Underlying stmt object does not provide tblname\n"); - needLog = false; - syncMode = NOSYNC; - return ErrSysInit; - } - while (iter.hasElement()) { - node = (TableSyncMode*)iter.nextElement(); - //printf("LOOP %s %s %d\n", node->tableName, tblName, node->mode); - if (strcmp(node->tableName, tblName) == 0) - { - if (NOSYNC == node->mode ) break; - else { needLog = true; syncMode = node->mode; return OK;} - } - } - needLog = false; - syncMode = NOSYNC; - return OK; -} -DbRetVal SqlLogStatement::populateSyncModeList() -{ - FILE *fp = NULL; - fp = fopen(Conf::config.getTableConfigFile(),"r"); - if( fp == NULL ) { - printError(ErrSysInit, "cache.table file does not exist"); - return ErrSysInit; - } - char tablename[IDENTIFIER_LENGTH]; - int cmode; - DataSyncMode syncmode; - TableSyncMode *node; - while(!feof(fp)) - { - fscanf(fp, "%d:%d:%s\n", &cmode, &syncmode, tablename); - node = new TableSyncMode(); - strcpy(node->tableName, tablename); - node->mode = syncmode; - syncModeList.append(node); - } - //printf("Loaded tablesyncmode to list. size %d\n", syncModeList.size()); - fclose(fp); - return OK; -} - DbRetVal SqlLogStatement::prepare(char *stmtstr) { DbRetVal rv = OK; if (innerStmt) rv = innerStmt->prepare(stmtstr); if (rv != OK) return rv; - //printf("LOG ENTER: prepare %s %d\n", stmtstr, isNonSelectDML(stmtstr)); + isCached = false; //check if it is INSERT UPDATE DELETE statement + //if not, then no need to generate logs if (!isNonSelectDML(stmtstr)) { return rv;} - needLog = false; if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK; + SqlLogConnection* logConn = (SqlLogConnection*)con; + if (!logConn->isTableCached(innerStmt->getTableName())) return ErrNotCached; + isCached = true; + return OK; - if (0 == syncModeList.size()) rv = populateSyncModeList(); - if (rv != OK) return rv; - rv = getTableSyncMode(); - if (rv != OK) return rv; - if (!needLog) return ErrNotCached; sid = SqlLogStatement::stmtUID.getID(); - //printf("LOG: prepare sid %d needLog %d syncMode %d \n", sid, needLog, syncMode); + //TODO::for TSYNC mode PacketPrepare *pkt = new PacketPrepare(); pkt->stmtID= sid; - pkt->syncMode = syncMode; + pkt->syncMode = TSYNC; pkt->stmtString = stmtstr; pkt->noParams = innerStmt->noOfParamFields(); FieldInfo *info = new FieldInfo(); @@ -129,13 +71,14 @@ DbRetVal SqlLogStatement::prepare(char *stmtstr) } } pkt->marshall(); - SqlLogConnection* logConn = (SqlLogConnection*)con; //printf("Sending PREPARe packet of size %d\n", pkt->getBufferSize()); rv = logConn->sendAndReceive(NW_PKT_PREPARE, pkt->getMarshalledBuffer(), pkt->getBufferSize()); //printf("RV from PREPARE SQLLOG %d\n", rv); - if (rv != OK) { needLog = false; - logConn->addPreparePacket(pkt); free(); - delete info; return rv; + if (rv != OK) { + needLog = false; + logConn->addPreparePacket(pkt); + delete info; + return rv; } delete pkt; delete info; @@ -157,11 +100,11 @@ DbRetVal SqlLogStatement::execute(int &rowsAffected) if (innerStmt) rv = innerStmt->execute(rowsAffected); if (rv != OK) return rv; - if (!needLog) return rv; - - logConn->setSyncMode(syncMode); + if (!needLog) return OK; //no need to generate log if it does not actually modify the table if (rowsAffected == 0 ) return OK; + if (!isCached) return OK; + if (logConn->getSyncMode() == OSYNC) return OK; //printf("LOG:execute\n"); PacketExecute *pkt = new PacketExecute(); diff --git a/src/tools/cachetable.cxx b/src/tools/cachetable.cxx index 8b66f8ea..536b0ebc 100644 --- a/src/tools/cachetable.cxx +++ b/src/tools/cachetable.cxx @@ -19,7 +19,7 @@ void printUsage() { printf("Usage: cachetable [-U username] [-P passwd] -t tablename \n" - " [-m ][-R] [-s] [-r]\n"); + " [-R] [-s] [-r]\n"); printf(" username -> username to connect with csql.\n"); printf(" passwd -> password for the above username to connect with csql.\n"); printf(" tablename -> table name to be cached in csql from target db.\n"); @@ -41,7 +41,6 @@ int main(int argc, char **argv) int c = 0, opt = 10; char tablename[IDENTIFIER_LENGTH]; char syncModeStr[IDENTIFIER_LENGTH]; - DataSyncMode syncMode = ASYNC; bool tableDefinition = true; bool tableNameSpecified = false; while ((c = getopt(argc, argv, "U:P:t:Rsru?")) != EOF) @@ -50,7 +49,6 @@ int main(int argc, char **argv) { case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; } case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; } - case 'm' : { strcpy(syncModeStr, argv[optind - 1]); break; } case 't' : { strcpy(tablename, argv[optind - 1]); if (opt==10) opt = 2; tableNameSpecified = true; @@ -80,13 +78,6 @@ int main(int argc, char **argv) CacheTableLoader cacheLoader; cacheLoader.setConnParam(username, password); if (opt==2) { - if (strncasecmp(syncModeStr, "TSYNC",4) == 0) - syncMode = TSYNC; - else if (strncasecmp(syncModeStr, "ASYNC",4) == 0) - syncMode = ASYNC; - else - printf("Unknown mode. Setting ASYNC mode\n"); - cacheLoader.setSyncMode(syncMode); cacheLoader.setTable(tablename); rv = cacheLoader.load(tableDefinition); if (rv != OK) exit (1); diff --git a/src/tools/isql.cxx b/src/tools/isql.cxx index 46e111c4..056ade62 100644 --- a/src/tools/isql.cxx +++ b/src/tools/isql.cxx @@ -84,10 +84,10 @@ int main(int argc, char **argv) } DbRetVal rv = OK; - conn = SqlFactory::createConnection(CSql); + conn = SqlFactory::createConnection(CSqlGateway); rv = conn->connect(username,password); if (rv != OK) return 1; - stmt = SqlFactory::createStatement(CSql); + stmt = SqlFactory::createStatement(CSqlGateway); stmt->setConnection(conn); rv = conn->beginTrans(); if (rv != OK) return 2; diff --git a/src/tools/repltable.cxx b/src/tools/repltable.cxx index a385637d..d4bc574c 100644 --- a/src/tools/repltable.cxx +++ b/src/tools/repltable.cxx @@ -41,7 +41,6 @@ int main(int argc, char **argv) int c = 0, opt = 10; char tablename[IDENTIFIER_LENGTH]; char syncModeStr[IDENTIFIER_LENGTH]; - DataSyncMode syncMode = ASYNC; bool tableNameSpecified = false; while ((c = getopt(argc, argv, "U:P:t:l:m:u?")) != EOF) { @@ -49,7 +48,6 @@ int main(int argc, char **argv) { case 'U' : { strcpy(username, argv[optind - 1]); opt=10; break; } case 'P' : { strcpy(password, argv[optind - 1]); opt=10; break; } - case 'm' : { strcpy(syncModeStr, argv[optind - 1]); break; } case 't' : { strcpy(tablename, argv[optind - 1]); if (opt==10) opt = 2; tableNameSpecified = true; @@ -75,12 +73,6 @@ int main(int argc, char **argv) } DbRetVal rv = OK; if (opt==2) { - if (strncasecmp(syncModeStr, "TSYNC",4) == 0) - syncMode = TSYNC; - else if (strncasecmp(syncModeStr, "ASYNC",4) == 0) - syncMode = ASYNC; - else - printf("Unknown mode. Setting ASYNC mode\n"); } return 0; } -- 2.11.4.GIT