Skip to content

Commit

Permalink
refactor(mwcp): merge DbManager and DbSourceManager
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Oct 18, 2024
1 parent 6f3c177 commit a5aa2d8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 84 deletions.
4 changes: 2 additions & 2 deletions packages/midway-component-kmore/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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

Expand Down
65 changes: 0 additions & 65 deletions packages/midway-component-kmore/src/lib/db-manager.ts

This file was deleted.

53 changes: 40 additions & 13 deletions packages/midway-component-kmore/src/lib/db-source-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import assert from 'node:assert'

import {
Expand All @@ -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,
Expand All @@ -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<SourceName extends string = string> extends DataSourceManager<Kmore> {
export class DbManager<SourceName extends string = string, D extends object = object> extends DataSourceManager<Kmore> {

@MConfig(ConfigKey.config) private readonly sourceConfig: KmoreSourceConfig<SourceName>

Expand All @@ -39,11 +39,11 @@ export class DbSourceManager<SourceName extends string = string> 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()
Expand All @@ -56,7 +56,7 @@ export class DbSourceManager<SourceName extends string = string> 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
Expand Down Expand Up @@ -95,7 +95,7 @@ export class DbSourceManager<SourceName extends string = string> extends DataSou
/**
* 创建单个实例
*/
@TraceInit<DbSourceManager['_createDataSource']>({
@TraceInit<DbManager['_createDataSource']>({
spanName: ([, dataSourceName]) => `INIT ${ConfigKey.namespace}.DbSourceManager._createDataSource():${dataSourceName}`,
before: (args) => {
if (! args[0].traceInitConnection) { return }
Expand Down Expand Up @@ -146,10 +146,8 @@ export class DbSourceManager<SourceName extends string = string> extends DataSou
return inst
}

// #region trxSpanMap

getName(): string {
return 'dbSourceManager'
return 'dbManager'
}

protected async checkConnected(dataSource: Kmore): Promise<boolean> {
Expand Down Expand Up @@ -183,5 +181,34 @@ export class DbSourceManager<SourceName extends string = string> extends DataSou
}
}

@Trace<DbManager['getDataSource']>({
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<Db extends object = D>(this: DbManager<SourceName, D>, dataSourceName: SourceName): Kmore<Db> {
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<Db>
}

}

3 changes: 1 addition & 2 deletions packages/midway-component-kmore/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions packages/midway-component-kmore/src/util/database.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Context } from '@mwcp/share'

import { DbSourceManager } from '##/lib/db-source-manager.js'
import { DbManager } from '##/lib/db-source-manager.js'


/**
* Rollback or Commit all uncommitted transaction
*/
export async function processUnCommittedTransaction(ctx: Context): Promise<void> {
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)
Expand Down

0 comments on commit a5aa2d8

Please sign in to comment.