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

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

E-mail: