def _ready_for_work(self): """ Method: _ready_for_work Description: 父类实现方法,注册MOC对象 Parameter: 无 Return: 0,成功 其他,失败 Others: """ bf.BasicApp._ready_for_work(self) self.__mit_manager = db_sync_mit.DBSyncMit() ret = self.__mit_manager.reset_NE_state() if ret != 0: return ret # Register MOC objects to be synchonized curdir = os.path.join(self.get_app_top_path(), 'moc_def', 'moc_for_sync') DBSyncUtil.load_mocs(curdir, self.__synchronized_mocs) conn_info = oradb.ConnectionInfo(**db_cfg_info.get_configure(db_cfg_info.ORACLE_SYNC_CON_NAME)) self.__connection_pool = DBConnectionPool() self.__connection_pool.create_connection(self.get_connection_name() , conn_info , oradb.create_connection , 10) self.__data_manager = DBSyncDataManager(self) self.__data_manager.create_schemas(self.__synchronized_mocs.values()) # 初始化同步优先级数据 moc_priority = OraSyncPriority.OraSyncPriority() self.__data_manager.create_schemas([moc_priority], False); self.__priority_mit_manager = db_sync_priority_mit.DBSyncPriorityMit() rows = self.__priority_mit_manager.rdm_find(moc_priority.get_moc_name()) tracelog.info('priority records: %d' % len(rows)) for row in rows: tracelog.info('ne_id: %d, priority: %d, event_id: %d' % (row.ne_id, row.priority, row.event_id)) self.set_sync_sn(row.ne_id, row.priority, row.event_id) # 初始化网元信息 ret = self.__init_NE_info() if ret != 0: tracelog.info("initialize NE information failed.") return ret # Register db sync update worker work_thread = bf.WorkThread() self.register_worker(db_sync_update_worker.DBSyncUpdateWorker(), work_thread, False) self.register_worker(sync_full_exp_worker.SyncFullExpWorker(), work_thread, True) self.register_worker(sync_full_imp_worker.SyncFullImpWorker()) return 0
def _ready_for_work(self): """ Method: _ready_for_work Description: 父类实现方法,注册MOC对象 Parameter: 无 Return: 0,成功 其他,失败 Others: """ tracelog.info('_ready_for_work started.') bf.BasicApp._ready_for_work(self) # 创建数据库连接 self.__connection_pool = DBConnectionPool() conn_info = oradb.ConnectionInfo(**db_cfg_info.get_configure(db_cfg_info.ORACLE_DEFAULT_CON_NAME)) self.__connection_pool.create_connection(db_cfg_info.ORACLE_DEFAULT_CON_NAME , conn_info , oradb.create_connection , 5) conn_info = oradb.ConnectionInfo(**db_cfg_info.get_configure(db_cfg_info.ORACLE_SYNC_CON_NAME)) self.__connection_pool.create_connection(db_cfg_info.ORACLE_SYNC_CON_NAME , conn_info , oradb.create_connection , 1) with self.get_connection(db_cfg_info.ORACLE_SYNC_CON_NAME) as db_conn: db_sync_event_manager = DBSyncEventManager(db_conn) db_sync_event_manager.create_schema() # 加载所有MOC对象 curdir = os.path.join(self.get_app_top_path(), 'moc_def') DBSyncUtil.load_mocs(curdir, self.__synchronized_mocs) self.__init_moc_data_to_sync_tbl_sqls() # Register db sync worker self.register_worker(db_sync_worker.DBSyncWorker()) tracelog.info('_ready_for_work ended.') return 0
def add_insert_event(self, moc): """ Method: _add_event Description: 追加数据同步Event Parameter: moc: MOC对象 Return: Others: """ if moc.__IMC_SYNC_PRIORITY__ == mit.IMC_SYNC_NOT_SYNC: return table = moc.get_moc_name() priority = moc.__IMC_SYNC_PRIORITY__ operation = DBSyncOperationType.INSERT data = DBSyncUtil.serialize(moc.to_db_record()) self._add_event(table, DBSyncType.INCREMENTAL, priority, operation, data, None)
def add_update_event(self, moc): """ Method: add_update_event Description: 更新数据同步Event Parameter: moc: MOC对象 Return: Others: """ if moc.__IMC_SYNC_PRIORITY__ == mit.IMC_SYNC_NOT_SYNC: return table = moc.get_moc_name() priority = moc.__IMC_SYNC_PRIORITY__ operation = DBSyncOperationType.UPDATE data = DBSyncUtil.serialize(moc.to_db_record()) condition = ("\"moid\" = '%s'" % moc.get_moid().replace("'", "''")) self._add_event(table, DBSyncType.INCREMENTAL, priority, operation, data, condition)
def _exists(self, db_query, table, data, ne_id): """ Method: _exists Description: 检查记录是否存在 """ data = DBSyncUtil.deserialize(data) i = 1 moid = None for attr in self.__moc_defs[table]['FIELDS']: if attr.name == 'moid': moid = data[i] break i = i + 1 sql = 'SELECT * FROM user_sync.%s WHERE "moid"=:1 AND "_SYNC_SOURCE"=:2' % (self.__moc_defs[table]['TABLE_NAME']) debug.info(sql) records = db_query.select(sql, [moid, ne_id]) if len(records) > 0: return True return False
def _add(self, db_query, table, data, ne_id): """ Method: _add Description: 追加记录 Parameters: db_query: DB查询对象 table: MOC名 data: 数据 ne_id: 客户端ID """ data = DBSyncUtil.deserialize(data) fields = [] field_indexes = [] i = 1 for attr in self.__moc_defs[table]['FIELDS']: if attr.name == '_SYNC_SOURCE': if i <= len(data): data[i-1] = ne_id else: data.append(ne_id) fields.append('"%s"' % attr.name) field_indexes.append(':%d' % i) i = i + 1 sql = 'INSERT INTO user_sync.%s(%s) VALUES(%s)' % (self.__moc_defs[table]['TABLE_NAME'] ,','.join(fields) ,','.join(field_indexes)) debug.info(sql) debug.info(data) try: db_query.execute(sql, data) except DBQueryError, e: dberror = e.args[0] if issubclass(dberror.__class__, cx_Oracle.DatabaseError): error, = dberror.args # ORA-0001: unique constraint if error.code == 1: tracelog.error('add conflict, full-sync will start') return DBSyncStatus.ERROR_CONFLICT tracelog.exception('database error') return DBSyncStatus.ERROR_FAILED
def _edit(self, db_query, table, data, condition, ne_id): """ Method: _edit Description: 更新记录 Parameters: db_query: DB查询对象 table: MOC名 data: 数据 condition: 更新条件 ne_id: 客户端ID """ data = DBSyncUtil.deserialize(data) fields = [] i = 1 for attr in self.__moc_defs[table]['FIELDS']: if attr.name == '_SYNC_SOURCE': if i <= len(data): data[i-1] = ne_id else: data.append(ne_id) fields.append('"%s"=:%d' % (attr.name, i)) i = i + 1 if condition is None: sql = 'UPDATE user_sync.%s SET %s' % (self.__moc_defs[table]['TABLE_NAME'] ,','.join(fields)) else: sql = 'UPDATE user_sync.%s SET %s WHERE %s' % (self.__moc_defs[table]['TABLE_NAME'] ,','.join(fields) ,condition) debug.info(sql) debug.info(data) cursor = db_query.execute(sql, data) if cursor is not None and cursor.rowcount == 0: tracelog.error('update conflict, full-sync will start') return DBSyncStatus.ERROR_CONFLICT return DBSyncStatus.ERROR_SUCCESS