@@ -27,6 +27,9 @@ import { logger } from "../../../logutils";
2727import { WrapperProperties } from "../../wrapper_property" ;
2828import { ClientWrapper } from "../../client_wrapper" ;
2929import { FailoverRestriction } from "./failover_restriction" ;
30+ import { FullServicesContainer } from "../../utils/full_services_container" ;
31+ import { ServiceUtils } from "../../utils/service_utils" ;
32+ import { DatabaseDialect } from "../../database_dialect/database_dialect" ;
3033
3134export interface WriterFailoverHandler {
3235 failover ( currentTopology : HostInfo [ ] ) : Promise < WriterFailoverResult > ;
@@ -47,6 +50,7 @@ export class ClusterAwareWriterFailoverHandler implements WriterFailoverHandler
4750 static readonly RECONNECT_WRITER_TASK = "TaskA" ;
4851 static readonly WAIT_NEW_WRITER_TASK = "TaskB" ;
4952 private readonly pluginService : PluginService ;
53+ private readonly servicesContainer : FullServicesContainer ;
5054 private readonly readerFailoverHandler : ClusterAwareReaderFailoverHandler ;
5155 private readonly initialConnectionProps : Map < string , any > ;
5256 maxFailoverTimeoutMs = 60000 ; // 60 sec
@@ -55,30 +59,45 @@ export class ClusterAwareWriterFailoverHandler implements WriterFailoverHandler
5559
5660 constructor (
5761 pluginService : PluginService ,
62+ servicesContainer : FullServicesContainer ,
5863 readerFailoverHandler : ClusterAwareReaderFailoverHandler ,
5964 initialConnectionProps : Map < string , any > ,
6065 failoverTimeoutMs ?: number ,
6166 readTopologyIntervalMs ?: number ,
6267 reconnectWriterIntervalMs ?: number
6368 ) {
6469 this . pluginService = pluginService ;
70+ this . servicesContainer = servicesContainer ;
6571 this . readerFailoverHandler = readerFailoverHandler ;
6672 this . initialConnectionProps = initialConnectionProps ;
6773 this . maxFailoverTimeoutMs = failoverTimeoutMs ?? this . maxFailoverTimeoutMs ;
6874 this . readTopologyIntervalMs = readTopologyIntervalMs ?? this . readTopologyIntervalMs ;
6975 this . reconnectionWriterIntervalMs = reconnectWriterIntervalMs ?? this . reconnectionWriterIntervalMs ;
7076 }
7177
78+ protected async newServicesContainer ( ) : Promise < FullServicesContainer > {
79+ const container = ServiceUtils . instance . createMinimalServiceContainerFrom ( this . servicesContainer , this . initialConnectionProps ) ;
80+ await container . pluginManager . init ( ) ;
81+ const initialHostInfo = this . pluginService . getInitialConnectionHostInfo ( ) ;
82+ if ( initialHostInfo ) {
83+ container . hostListProviderService . setInitialConnectionHostInfo ( initialHostInfo ) ;
84+ }
85+ return container ;
86+ }
87+
7288 async failover ( currentTopology : HostInfo [ ] ) : Promise < WriterFailoverResult > {
7389 if ( ! currentTopology || currentTopology . length == 0 ) {
7490 logger . error ( Messages . get ( "ClusterAwareWriterFailoverHandler.failoverCalledWithInvalidTopology" ) ) ;
7591 return ClusterAwareWriterFailoverHandler . DEFAULT_RESULT ;
7692 }
7793
94+ const taskAContainer = await this . newServicesContainer ( ) ;
95+ const taskBContainer = await this . newServicesContainer ( ) ;
96+
7897 const reconnectToWriterHandlerTask = new ReconnectToWriterHandlerTask (
7998 currentTopology ,
8099 getWriter ( currentTopology ) ,
81- this . pluginService ,
100+ taskAContainer . pluginService ,
82101 this . initialConnectionProps ,
83102 this . reconnectionWriterIntervalMs ,
84103 Date . now ( ) + this . maxFailoverTimeoutMs
@@ -88,7 +107,7 @@ export class ClusterAwareWriterFailoverHandler implements WriterFailoverHandler
88107 currentTopology ,
89108 getWriter ( currentTopology ) ,
90109 this . readerFailoverHandler ,
91- this . pluginService ,
110+ taskBContainer . pluginService ,
92111 this . initialConnectionProps ,
93112 this . readTopologyIntervalMs ,
94113 Date . now ( ) + this . maxFailoverTimeoutMs
@@ -379,7 +398,7 @@ class WaitForNewWriterHandlerTask {
379398 async refreshTopologyAndConnectToNewWriter ( ) : Promise < boolean > {
380399 const allowOldWriter : boolean = this . pluginService . getDialect ( ) . getFailoverRestrictions ( ) . includes ( FailoverRestriction . ENABLE_WRITER_IN_TASK_B ) ;
381400
382- while ( this . pluginService . getCurrentClient ( ) && Date . now ( ) < this . endTime && ! this . failoverCompleted ) {
401+ while ( Date . now ( ) < this . endTime && ! this . failoverCompleted ) {
383402 try {
384403 if ( this . currentReaderTargetClient ) {
385404 await this . pluginService . forceRefreshHostList ( ) ;
0 commit comments