INT32 expRoutine::_connectDB( sdbConnectionHandle &hConn ) { INT32 rc = SDB_OK ; const expOptions &options = _options ; #ifdef SDB_SSL if ( options.useSSL() ) { rc = sdbSecureConnect( options.hostName().c_str(), options.svcName().c_str(), options.user().c_str(), options.password().c_str(), &hConn ) ; } else #endif { rc = sdbConnect( options.hostName().c_str(), options.svcName().c_str(), options.user().c_str(), options.password().c_str(), &hConn ) ; } if ( SDB_OK != rc ) { PD_LOG ( PDERROR, "Failed to connect database %s:%s, rc = %d", options.hostName().c_str(), options.svcName().c_str(), rc ) ; goto error ; } done: return rc ; error: goto done ; }
INT32 migExport::_connectDB() { INT32 rc = SDB_OK ; bson obj ; bson_init( &obj ) ; // connection is established #ifdef SDB_SSL if ( _pMigArg->useSSL ) { rc = sdbSecureConnect ( _pMigArg->pHostname, _pMigArg->pSvcname, _pMigArg->pUser, _pMigArg->pPassword, &_gConnection ) ; } else #endif { rc = sdbConnect ( _pMigArg->pHostname, _pMigArg->pSvcname, _pMigArg->pUser, _pMigArg->pPassword, &_gConnection ) ; } if ( rc ) { PD_LOG ( PDERROR, "Failed to connect database %s:%s, rc = %d", _pMigArg->pHostname, _pMigArg->pSvcname, rc ) ; goto error ; } // set prefer instance if( _pMigArg->pPrefInst ) { if ( FALSE == jsonToBson2 ( &obj, _pMigArg->pPrefInst, 0, 1 ) ) { rc = SDB_INVALIDARG ; ossPrintf ( "Error: prefered instance's format error"OSS_NEWLINE ) ; PD_LOG ( PDERROR, "prefered instance's format error" ) ; goto error ; } rc = sdbSetSessionAttr ( _gConnection, &obj ) ; if ( SDB_OK != rc ) { PD_LOG ( PDERROR, "Failed to set session attribute, rc = %d", rc ) ; goto error ; } } else { bson_empty( &obj ) ; } done: bson_destroy ( &obj ) ; return rc ; error: goto done ; }
INT32 RecordSharding::init(const vector<Host>& hosts, const string& user, const string& password, const string& csname, const string& clname, BOOLEAN useSSL) { INT32 rc = SDB_OK; sdbConnectionHandle conn = SDB_INVALID_HANDLE; sdbCursorHandle cursor = SDB_INVALID_HANDLE; bson cataObj; INT32 cataCount = 0; SDB_ASSERT(!_inited, "alreay inited"); _hosts = &hosts; _user = user; _password = password; _csname = csname; _clname = clname; _useSSL = useSSL; bson_init(&cataObj); for (vector<Host>::const_iterator it = hosts.begin(); it != hosts.end(); it++) { const Host& host = *it; if (SDB_INVALID_HANDLE != cursor) { sdbCloseCursor(cursor); sdbReleaseCursor(cursor); cursor = SDB_INVALID_HANDLE; } if (SDB_INVALID_HANDLE != conn) { sdbDisconnect(conn); sdbReleaseConnection(conn); conn = SDB_INVALID_HANDLE; } if (_useSSL) { rc = sdbSecureConnect(host.hostname.c_str(), host.svcname.c_str(), _user.c_str(), _password.c_str(), &conn); } else { rc = sdbConnect(host.hostname.c_str(), host.svcname.c_str(), _user.c_str(), _password.c_str(), &conn); } if (SDB_OK != rc) { PD_LOG(PDWARNING, "failed to connect to server %s:%s, rc=%d, usessl=%d", host.hostname.c_str(), host.svcname.c_str(), rc, _useSSL); rc = SDB_OK; continue; } rc = sdbGetSnapshot(conn, SDB_SNAP_CATALOG, NULL, NULL, NULL, &cursor); if (SDB_OK != rc) { if (SDB_INVALID_HANDLE != cursor) { sdbCloseCursor(cursor); sdbReleaseCursor(cursor); cursor = SDB_INVALID_HANDLE; } if (SDB_RTN_COORD_ONLY == rc) { PD_LOG(PDWARNING, "%s:%s is not coordinator", host.hostname.c_str(), host.svcname.c_str()); rc = SDB_OK; continue; } PD_LOG(PDWARNING, "failed to get coordinator group from %s:%s, rc = %d", host.hostname.c_str(), host.svcname.c_str(), rc); rc = SDB_OK; continue; } break; } if (SDB_INVALID_HANDLE == cursor) { rc = SDB_OK; PD_LOG(PDWARNING, "failed to get coordinator group"); goto done; } for(;;) { rc = sdbNext(cursor, &cataObj); if (SDB_OK != rc) { if (SDB_DMS_EOC == rc) { rc = SDB_OK; break; } PD_LOG(PDERROR, "failed to get cataObj from cursor, rc=%d", rc); goto error; } rc = _cataAgent.updateCatalog(bson_data(&cataObj)); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to update catalog agent, rc=%d", rc); goto error; } cataCount++; } if (0 == cataCount) { _inited = TRUE; goto done; } _collectionName = _csname + "." + _clname; rc = _cataAgent.getCataInfo(_collectionName, _cataInfo); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to get catalog info, rc=%d", rc); goto error; } _isMainCL = _cataInfo.isMainCL(); if (_cataInfo.isMainCL()) { vector<string> subCLList; INT32 subGroupNum = 0; rc = _cataInfo.getSubCLList(subCLList); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to get group by record, rc=%d", rc); goto error; } for (vector<string>::iterator it = subCLList.begin(); it != subCLList.end(); it++) { CataInfo cataInfo; rc = _cataAgent.getCataInfo(*it, cataInfo); if (SDB_OK != rc) { PD_LOG(PDERROR, "failed to get catalog info, rc=%d", rc); goto error; } subGroupNum += cataInfo.getGroupNum(); _subCataInfo[*it] = cataInfo; } _groupNum = subGroupNum; } else { _groupNum = _cataInfo.getGroupNum(); } _inited = TRUE; done: bson_destroy(&cataObj); if (SDB_INVALID_HANDLE != cursor) { sdbCloseCursor(cursor); sdbReleaseCursor(cursor); cursor = SDB_INVALID_HANDLE; } if (SDB_INVALID_HANDLE != conn) { sdbDisconnect(conn); sdbReleaseConnection(conn); conn = SDB_INVALID_HANDLE; } return rc; error: goto done; }