Annotation of parser3/src/main/pa_sql_driver_manager.C, revision 1.64

1.54      paf         1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
1.60      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.61      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.63      paf         6: */
1.54      paf         7: 
1.64    ! paf         8: static const char* IDENT_SQL_DRIVER_MANAGER_C="$Date: pa_sql_driver_manager.C,v 1.63 2002/08/01 11:26:51 paf Exp $";
1.54      paf         9: 
                     10: #include "pa_sql_driver_manager.h"
                     11: #include "ltdl.h"
                     12: #include "pa_sql_connection.h"
                     13: #include "pa_exception.h"
                     14: #include "pa_common.h"
                     15: #include "pa_threads.h"
                     16: #include "pa_stack.h"
                     17: #include "pa_vhash.h"
                     18: #include "pa_vtable.h"
                     19: 
                     20: // globals
                     21: 
                     22: SQL_Driver_manager *SQL_driver_manager;
                     23: 
                     24: // consts
                     25: 
                     26: const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
                     27: const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
                     28: 
                     29: // helpers
                     30: 
                     31: const String& url_without_login(Pool& pool, const String& url) {
                     32:        String& result=*new(pool) String(pool);
                     33:        result << url.mid(0, url.pos(":")) << "://****";
                     34: 
                     35:        int at_pos=url.pos("@");
                     36:        if(at_pos>0)
                     37:                result << url.mid(at_pos, url.size());
                     38: 
                     39:        return result;
                     40: }
                     41: 
                     42: /// SQL_Driver_services Pooled implementation
                     43: class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
                     44: public:
                     45:        SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
                     46:                furl(aurl) {
                     47:        }
                     48: 
                     49:        virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); }
                     50:        virtual void *calloc(size_t size) { return Pooled::calloc(size); }
                     51: 
                     52:        /**
                     53:                the idea is to #1 jump to C++ some function to main body, where
                     54:                every function stack frame has exception unwind information
                     55:                and from there... #2 propagate_exception()
                     56:        */
                     57:        virtual void _throw(const char *comment) { 
                     58:                // hiding passwords and addresses from accidental show [imagine user forgot @exception]
1.62      paf        59:                e=Exception("sql.connect", 
1.54      paf        60:                        &url_without_login(pool(), furl),
                     61:                        comment); 
                     62: 
                     63:                longjmp(mark, 1);
                     64:        }
                     65:        virtual void propagate_exception() {
                     66:                throw e;
                     67:        }
                     68: 
                     69: private:
                     70:        const String& furl;
                     71:        Exception e;
                     72: };
                     73: 
                     74: // helpers
                     75: 
                     76: static void expire_connection(Array::Item *value, void *info) {
                     77:        SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
                     78:        time_t older_dies=reinterpret_cast<time_t>(info);
                     79: 
                     80:        if(connection.connected() && connection.expired(older_dies))
                     81:                connection.disconnect();
                     82: }
                     83: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
                     84:        Stack& stack=*static_cast<Stack *>(value);
                     85:        for(int i=0; i<=stack.top_index(); i++)
                     86:                expire_connection(stack.get(i), info);
                     87: }
                     88: 
                     89: // SQL_Driver_manager
                     90: 
                     91: SQL_Driver_manager::SQL_Driver_manager(Pool& apool) : Cache_manager(apool),
                     92:                driver_cache(apool),
                     93:                connection_cache(apool),
                     94:                prev_expiration_pass_time(0) {
                     95: }
                     96: 
                     97: SQL_Driver_manager::~SQL_Driver_manager() {
                     98:        connection_cache.for_each(expire_connections, 
1.56      paf        99:                reinterpret_cast<void *>(time(0)+1/*=in future=expire all*/));
1.54      paf       100: }
                    101: 
                    102: /// @param request_url protocol://[driver-dependent]
1.58      paf       103: SQL_Connection_ptr SQL_Driver_manager::get_connection(const String& request_url,
1.54      paf       104:                                                                                                   const String& request_origin,
                    105:                                                                                                   Table *protocol2driver_and_client) {
                    106:        Pool& pool=request_origin.pool(); // request pool                                                                                          
                    107: 
                    108:        // we have table for locating protocol's library
                    109:        if(!protocol2driver_and_client)
1.62      paf       110:                throw Exception("parser.runtime",
1.54      paf       111:                        &request_url,
                    112:                        "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
                    113: 
                    114:        // construct services[request]  (deassociates at close)
                    115:        SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url); 
                    116: 
                    117:        // first trying to get cached connection
1.58      paf       118:        SQL_Connection *connection=get_connection_from_cache(request_url);
                    119:        if(connection) {
                    120:                connection->set_services(services);
                    121:                if(!connection->ping()) { // we have some cached connection, is it pingable?
                    122:                        connection->disconnect(); // kill unpingabe=dead connection
                    123:                        connection=0;
1.54      paf       124:                }
                    125:        }
                    126: 
                    127:        char *request_url_cstr;
1.58      paf       128:        if(connection)
1.54      paf       129:                request_url_cstr=0; // calm, compiler
                    130:        else { // no cached connection or it were unpingabe: connect/reconnect
                    131:                int pos=request_url.pos("://", 3);
                    132:                if(pos<0)
1.62      paf       133:                        throw Exception("parser.runtime",
1.54      paf       134:                                request_url.size()?&request_url:&request_origin,
1.62      paf       135:                                "connection string must start with protocol://");
1.54      paf       136: 
                    137:                // make global_url C-string on global pool
                    138:                request_url_cstr=request_url.cstr();
                    139:                char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
                    140:                strcpy(global_url_cstr, request_url_cstr);
                    141:                // make global_url string on global pool
                    142:                String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
                    143:                
                    144:                char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
                    145:                // skip "//" after ':'
                    146:                while(*request_url_cstr=='/')
                    147:                        request_url_cstr++;
                    148:                // make global_protocol C-string on global pool
                    149:                char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
                    150:                strcpy(global_protocol_cstr, request_protocol_cstr);
                    151:                // make global_protocol string on global pool
                    152:                String& global_protocol=*new(this->pool()) String(this->pool(), 
                    153:                        global_protocol_cstr);
                    154: 
                    155:                SQL_Driver *driver;
                    156:                // first trying to get cached driver
                    157:                if(!(driver=get_driver_from_cache(global_protocol))) {
                    158:                        // no cached
                    159:                        const String *library=0;
                    160:                        const String *dlopen_file_spec=0;
                    161:                        if(protocol2driver_and_client->locate(0, global_protocol)) {
                    162:                                if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
1.62      paf       163:                                        throw Exception("parser.runtime",
1.54      paf       164:                                                protocol2driver_and_client->origin_string(),
                    165:                                                "driver library column for protocol '%s' is empty", 
                    166:                                                        request_protocol_cstr);
                    167:                                dlopen_file_spec=protocol2driver_and_client->item(2);
                    168:                        } else
1.62      paf       169:                                throw Exception("parser.runtime",
1.54      paf       170:                                        &request_url,
                    171:                                        "undefined protocol '%s'", 
                    172:                                                request_protocol_cstr);
                    173: 
                    174:                        if(lt_dlinit())
1.62      paf       175:                                throw Exception(0,
1.54      paf       176:                                        library,
                    177:                                        "prepare to dynamic loading failed, %s", lt_dlerror());
                    178: 
                    179:                        const char *filename=library->cstr(String::UL_FILE_SPEC);
                    180:                        lt_dlhandle handle=lt_dlopen(filename);
                    181:                        if (!handle)
1.62      paf       182:                                throw Exception(0,
1.54      paf       183:                                        library,
                    184:                                        "can not open the module, %s", lt_dlerror());
                    185: 
                    186:                        SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle, 
                    187:                                SQL_DRIVER_CREATE_NAME);
                    188:                        if(!create)
1.62      paf       189:                                throw Exception(0,
1.54      paf       190:                                        library,
                    191:                                        "function '"SQL_DRIVER_CREATE_NAME"' was not found");
                    192: 
                    193:                        // create library-driver!
                    194:                        driver=(*create)();
                    195: 
                    196:                        // validate driver api version
                    197:                        int driver_api_version=driver->api_version();
                    198:                        if(driver_api_version!=SQL_DRIVER_API_VERSION)
1.62      paf       199:                                throw Exception(0,
1.54      paf       200:                                        library,
                    201:                                        "driver implements API version 0x%04X not equal to 0x%04X",
                    202:                                                driver_api_version, SQL_DRIVER_API_VERSION);
                    203: 
                    204:                        // initialise by connecting to sql client dynamic link library
                    205:                        char *dlopen_file_spec_cstr=
                    206:                                dlopen_file_spec && dlopen_file_spec->size()?
                    207:                                dlopen_file_spec->cstr(String::UL_AS_IS):0;
                    208:                        if(const char *error=driver->initialize(
                    209:                                dlopen_file_spec_cstr))
1.62      paf       210:                                throw Exception(0,
1.54      paf       211:                                        library,
                    212:                                        "driver failed to initialize client library '%s', %s",
                    213:                                                dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed", 
                    214:                                                error);
                    215: 
                    216:                        // cache it
                    217:                        put_driver_to_cache(global_protocol, *driver);
                    218:                }
                    219:        
                    220:                // allocate in global pool 
                    221:                // associate with services[request]
                    222:                // NOTE: never freed up!
1.58      paf       223:                connection=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
1.54      paf       224:                // associate with services[request]  (deassociates at close)
1.58      paf       225:                connection->set_services(services); 
1.54      paf       226:        }
                    227: 
1.58      paf       228:        // if not connected yet, do that now, when connection has services
                    229:        if(!connection->connected())
                    230:                connection->connect(request_url_cstr);
                    231:        // return autoclosing object for it
1.59      paf       232:        return SQL_Connection_ptr(connection);
1.54      paf       233: }
                    234: 
                    235: void SQL_Driver_manager::close_connection(const String& url, 
                    236:                                                                                  SQL_Connection& connection) {
                    237:        // deassociate from services[request]
                    238:        connection.set_services(0);
                    239:        put_connection_to_cache(url, connection);
                    240: }
                    241: 
                    242: 
                    243: // driver cache
                    244: 
                    245: SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
                    246:        SYNCHRONIZED;
                    247: 
                    248:        return static_cast<SQL_Driver *>(driver_cache.get(protocol));
                    249: }
                    250: 
                    251: void SQL_Driver_manager::put_driver_to_cache(const String& protocol, 
                    252:                                                                                         SQL_Driver& driver) {
                    253:        SYNCHRONIZED;
                    254: 
                    255:        driver_cache.put(protocol, &driver);
                    256: }
                    257: 
                    258: // connection cache
                    259: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
                    260: SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) { 
                    261:        SYNCHRONIZED;
                    262: 
                    263:        if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
                    264:                while(connections->top_index()>=0) { // there are cached connections to that 'url'
                    265:                        SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());
                    266:                        if(result->connected()) // not expired?
                    267:                                return result;
                    268:                }
                    269: 
                    270:        return 0;
                    271: }
                    272: 
                    273: void SQL_Driver_manager::put_connection_to_cache(const String& url, 
                    274:                                                                                                 SQL_Connection& connection) { 
                    275:        SYNCHRONIZED;
                    276: 
                    277:        Stack *connections=static_cast<Stack *>(connection_cache.get(url));
                    278:        if(!connections) { // there are no cached connections to that 'url' yet?
                    279:                connections=NEW Stack(pool()); // NOTE: never freed up!
                    280:                connection_cache.put(url, connections);
                    281:        }       
                    282:        connections->push(&connection);
                    283: }
                    284: 
                    285: void SQL_Driver_manager::maybe_expire_cache() {
                    286:        time_t now=time(0);
                    287: 
                    288:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
                    289:                connection_cache.for_each(expire_connections, 
                    290:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
                    291: 
                    292:                prev_expiration_pass_time=now;
                    293:        }
                    294: }
                    295: 
                    296: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
                    297:        SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
                    298:        Table& table=*static_cast<Table *>(info);
                    299: 
                    300:        if(connection.connected()) {
                    301:                Pool& pool=table.pool();
                    302:                Array& row=*new(pool) Array(pool);
                    303: 
                    304:                // url
                    305:                row+=&url_without_login(pool, connection.get_url());
                    306:                // time
1.58      paf       307:                time_t time_used=connection.get_time_used();
                    308:                const char *unsafe_time_cstr=ctime(&time_used);
1.54      paf       309:                int time_buf_size=strlen(unsafe_time_cstr);
                    310:                char *safe_time_buf=(char *)pool.malloc(time_buf_size);
                    311:                memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
                    312:                row+=new(pool) String(pool, safe_time_buf, time_buf_size);
                    313: 
                    314:                table+=&row;
                    315:        }
                    316: }
                    317: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
                    318:        Stack& stack=*static_cast<Stack *>(value);
                    319:        Array_iter iter(stack);
                    320:        for(int countdown=stack.top_index(); countdown-->=0; )
                    321:                add_connection_to_status_cache_table(iter.next(), info);
                    322: }
                    323: Value& SQL_Driver_manager::get_status(Pool& pool, const String *source) {
                    324:        VHash& result=*new(pool) VHash(pool);
                    325:        
                    326:        // cache
                    327:        {
                    328:                Array& columns=*new(pool) Array(pool);
                    329:                columns+=new(pool) String(pool, "url");
                    330:                columns+=new(pool) String(pool, "time");
                    331:                Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
                    332: 
                    333:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
                    334: 
                    335:                result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
                    336:        }
                    337: 
                    338:        return result;
1.57      paf       339: }

E-mail: