From a5aa2d82ecdf0a2dc0d4c9155eac9b82dfe602e4 Mon Sep 17 00:00:00 2001 From: waiting <1661926154@qq.com> Date: Fri, 18 Oct 2024 15:50:40 +0800 Subject: [PATCH] refactor(mwcp): merge DbManager and DbSourceManager --- .../src/configuration.ts | 4 +- .../src/lib/db-manager.ts | 65 ------------------- .../src/lib/db-source-manager.ts | 53 +++++++++++---- .../midway-component-kmore/src/lib/index.ts | 3 +- .../src/util/database.ts | 4 +- 5 files changed, 45 insertions(+), 84 deletions(-) delete mode 100644 packages/midway-component-kmore/src/lib/db-manager.ts diff --git a/packages/midway-component-kmore/src/configuration.ts b/packages/midway-component-kmore/src/configuration.ts index e4c73920..5dccaba6 100644 --- a/packages/midway-component-kmore/src/configuration.ts +++ b/packages/midway-component-kmore/src/configuration.ts @@ -23,7 +23,7 @@ import * as DefaultConfig from './config/config.default.js' import * as LocalConfig from './config/config.local.js' import * as UnittestConfig from './config/config.unittest.js' import { useComponents } from './imports.js' -import { DbSourceManager } from './lib/db-source-manager.js' +import { DbManager } from './lib/db-source-manager.js' import { Config, ConfigKey, KmorePropagationConfig, KmoreSourceConfig } from './lib/index.js' import { KmoreMiddleware } from './middleware/index.middleware.js' @@ -54,7 +54,7 @@ export class AutoConfiguration implements ILifeCycle { @MConfig(ConfigKey.propagationConfig) protected readonly propagationConfig: KmorePropagationConfig - @Inject() readonly dbSourceManager: DbSourceManager + @Inject() readonly dbSourceManager: DbManager @Inject() decoratorService: MidwayDecoratorService diff --git a/packages/midway-component-kmore/src/lib/db-manager.ts b/packages/midway-component-kmore/src/lib/db-manager.ts deleted file mode 100644 index 0c1e58cb..00000000 --- a/packages/midway-component-kmore/src/lib/db-manager.ts +++ /dev/null @@ -1,65 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unnecessary-type-parameters */ -import assert from 'node:assert' - -import { Inject, Singleton } from '@midwayjs/core' -import { Attributes, SpanKind, Trace } from '@mwcp/otel' -import { MConfig } from '@mwcp/share' -import type { Kmore } from 'kmore' - -import { DbSourceManager } from './db-source-manager.js' -import { eventNeedTrace, genCommonAttr } from './trace.helper.js' -import { type KmoreSourceConfig, ConfigKey, DbConfig, KmoreAttrNames } from './types.js' - - -@Singleton() -export class DbManager { - - @MConfig(ConfigKey.config) private readonly sourceConfig: KmoreSourceConfig - @Inject() protected readonly dbSourceManager: DbSourceManager - - getName(): string { return 'dbManager' } - - /** - * Check the data source is connected - */ - async isConnected(dataSourceName: SourceName): Promise { - return this.dbSourceManager.isConnected(dataSourceName) - } - - getDbConfigByDbId(dbId: SourceName): DbConfig | undefined { - assert(dbId) - const dbConfig = this.sourceConfig.dataSource[dbId] - return dbConfig - } - - @Trace({ - spanName: () => 'DbManager getDataSource', - startActiveSpan: false, - kind: SpanKind.INTERNAL, - before([dataSourceName]) { - const dbConfig = this.getDbConfigByDbId(dataSourceName) - if (dbConfig && ! eventNeedTrace(KmoreAttrNames.getDataSourceStart, dbConfig)) { return } - - const attrs: Attributes = { - dbId: dataSourceName, - } - const events = genCommonAttr(KmoreAttrNames.getDataSourceStart) - return { attrs, events } - }, - after([dataSourceName]) { - const dbConfig = this.getDbConfigByDbId(dataSourceName) - if (dbConfig && ! eventNeedTrace(KmoreAttrNames.getDataSourceStart, dbConfig)) { return } - - const events = genCommonAttr(KmoreAttrNames.getDataSourceEnd) - return { events } - }, - }) - getDataSource(this: DbManager, dataSourceName: SourceName): Kmore { - const db = this.dbSourceManager.getDataSource(dataSourceName) - assert(db, `[${ConfigKey.componentName}] getDataSource() db source empty: "${dataSourceName}"`) - assert(db.dbId === dataSourceName, `[${ConfigKey.componentName}] getDataSource() db source id not match: "${dataSourceName}"`) - return db as Kmore - } - -} - diff --git a/packages/midway-component-kmore/src/lib/db-source-manager.ts b/packages/midway-component-kmore/src/lib/db-source-manager.ts index 3f9b0b61..d89ff5b5 100644 --- a/packages/midway-component-kmore/src/lib/db-source-manager.ts +++ b/packages/midway-component-kmore/src/lib/db-source-manager.ts @@ -1,4 +1,3 @@ - import assert from 'node:assert' import { @@ -12,7 +11,7 @@ import { Singleton, } from '@midwayjs/core' import { ILogger } from '@midwayjs/logger' -import { Attributes, TraceInit } from '@mwcp/otel' +import { Attributes, SpanKind, Trace, TraceInit } from '@mwcp/otel' import { Application, Context, MConfig, getWebContext } from '@mwcp/share' import { type EventCallbacks, @@ -25,12 +24,13 @@ import { import { DbEvent } from './db-event.js' import { DbHook } from './db-hook/index.db-hook.js' +import { eventNeedTrace, genCommonAttr } from './trace.helper.js' import { TrxStatusService } from './trx-status.service.js' -import { ConfigKey, DbConfig, KmoreSourceConfig } from './types.js' +import { type KmoreSourceConfig, ConfigKey, DbConfig, KmoreAttrNames } from './types.js' @Singleton() -export class DbSourceManager extends DataSourceManager { +export class DbManager extends DataSourceManager { @MConfig(ConfigKey.config) private readonly sourceConfig: KmoreSourceConfig @@ -39,11 +39,11 @@ export class DbSourceManager extends DataSou @_Logger() private readonly logger: ILogger - @Inject() baseDir: string + @Inject() readonly baseDir: string - @Inject() readonly dbEvent: DbEvent - @Inject() readonly dbHook: DbHook - @Inject() readonly trxStatusSvc: TrxStatusService + @Inject() private readonly dbEvent: DbEvent + @Inject() private readonly dbHook: DbHook + @Inject() private readonly trxStatusSvc: TrxStatusService @TraceInit(`INIT ${ConfigKey.namespace}.DbSourceManager.init()`) @Init() @@ -56,7 +56,7 @@ export class DbSourceManager extends DataSou await this.initDataSource(this.sourceConfig, '') } - protected getDbConfigByDbId(dbId: SourceName): DbConfig | undefined { + getDbConfigByDbId(dbId: SourceName): DbConfig | undefined { assert(dbId) const dbConfig = this.sourceConfig.dataSource[dbId] return dbConfig @@ -95,7 +95,7 @@ export class DbSourceManager extends DataSou /** * 创建单个实例 */ - @TraceInit({ + @TraceInit({ spanName: ([, dataSourceName]) => `INIT ${ConfigKey.namespace}.DbSourceManager._createDataSource():${dataSourceName}`, before: (args) => { if (! args[0].traceInitConnection) { return } @@ -146,10 +146,8 @@ export class DbSourceManager extends DataSou return inst } - // #region trxSpanMap - getName(): string { - return 'dbSourceManager' + return 'dbManager' } protected async checkConnected(dataSource: Kmore): Promise { @@ -183,5 +181,34 @@ export class DbSourceManager extends DataSou } } + @Trace({ + spanName: () => 'DbManager getDataSource', + startActiveSpan: false, + kind: SpanKind.INTERNAL, + before([dataSourceName]) { + const dbConfig = this.getDbConfigByDbId(dataSourceName) + if (dbConfig && ! eventNeedTrace(KmoreAttrNames.getDataSourceStart, dbConfig)) { return } + + const attrs: Attributes = { + dbId: dataSourceName, + } + const events = genCommonAttr(KmoreAttrNames.getDataSourceStart) + return { attrs, events } + }, + after([dataSourceName]) { + const dbConfig = this.getDbConfigByDbId(dataSourceName) + if (dbConfig && ! eventNeedTrace(KmoreAttrNames.getDataSourceStart, dbConfig)) { return } + + const events = genCommonAttr(KmoreAttrNames.getDataSourceEnd) + return { events } + }, + }) + override getDataSource(this: DbManager, dataSourceName: SourceName): Kmore { + const db = super.getDataSource(dataSourceName) + assert(db, `[${ConfigKey.componentName}] getDataSource() db source empty: "${dataSourceName}"`) + assert(db.dbId === dataSourceName, `[${ConfigKey.componentName}] getDataSource() db source id not match: "${dataSourceName}"`) + return db as Kmore + } + } diff --git a/packages/midway-component-kmore/src/lib/index.ts b/packages/midway-component-kmore/src/lib/index.ts index 71fe0f56..da901fb2 100644 --- a/packages/midway-component-kmore/src/lib/index.ts +++ b/packages/midway-component-kmore/src/lib/index.ts @@ -2,8 +2,7 @@ export * from './config.js' export * from './types.js' export { CallerService } from './caller.service.js' -export { DbSourceManager } from './db-source-manager.js' -export { DbManager } from './db-manager.js' +export { DbManager } from './db-source-manager.js' export { DbEvent } from './db-event.js' export * from './db-hook/index.db-hook.js' export { TrxStatusService } from './trx-status.service.js' diff --git a/packages/midway-component-kmore/src/util/database.ts b/packages/midway-component-kmore/src/util/database.ts index 253e901d..d24e50e7 100644 --- a/packages/midway-component-kmore/src/util/database.ts +++ b/packages/midway-component-kmore/src/util/database.ts @@ -1,6 +1,6 @@ import type { Context } from '@mwcp/share' -import { DbSourceManager } from '##/lib/db-source-manager.js' +import { DbManager } from '##/lib/db-source-manager.js' /** @@ -8,7 +8,7 @@ import { DbSourceManager } from '##/lib/db-source-manager.js' */ export async function processUnCommittedTransaction(ctx: Context): Promise { const container = ctx.app.getApplicationContext() - const dbSourceManager = await container.getAsync(DbSourceManager) + const dbSourceManager = await container.getAsync(DbManager) for (const [name, kmore] of dbSourceManager.getAllDataSources()) { const trxSet = kmore.getTrxListByScope(ctx)