@@ -33,21 +33,22 @@ func setupProcessorPartitionMigrator(ctx context.Context,
3333 gwRODB , gwWODB ,
3434 rtRWDB , brtRWDB jobsdb.JobsDB ,
3535 etcdClientProvider func () (etcdclient.Client , error ),
36- ) (partitionMigrator PartitionMigrator , gwDB , rtDB , brtDB jobsdb.JobsDB , err error ) {
36+ ) (partitionMigrator PartitionMigrator , gwDB , rtDB , brtDB jobsdb.JobsDB , finally func (), err error ) {
3737 if ! config .GetBool ("PartitionMigration.enabled" , false ) {
38+ finally = func () {}
3839 if gwWODB == nil {
3940 // caller expects to get reader gw db back if writer is nil
40- return & noOpPartitionMigrator {}, gwRODB , rtRWDB , brtRWDB , nil
41+ return & noOpPartitionMigrator {}, gwRODB , rtRWDB , brtRWDB , finally , nil
4142 } else {
4243 // caller expects to get writer gw db back if writer is not nil
43- return & noOpPartitionMigrator {}, gwWODB , rtRWDB , brtRWDB , nil
44+ return & noOpPartitionMigrator {}, gwWODB , rtRWDB , brtRWDB , finally , nil
4445 }
4546 }
4647
4748 log := logger .NewLogger ().Child ("partitionmigrator" )
4849 partitionCount := config .GetIntVar (0 , 1 , "JobsDB.partitionCount" )
4950 if partitionCount == 0 {
50- return nil , nil , nil , nil , fmt .Errorf ("partition migrator needs partition count > 0" )
51+ return nil , nil , nil , nil , nil , fmt .Errorf ("partition migrator needs partition count > 0" )
5152 }
5253 bufferFlushBatchSize := config .GetReloadableIntVar (20000 , 1 , "PartitionMigration.bufferFlushBatchSize" )
5354 bufferFlushPayloadSize := config .GetReloadableInt64Var (500 , bytesize .MB , "PartitionMigration.bufferFlushPayloadSize" )
@@ -66,6 +67,12 @@ func setupProcessorPartitionMigrator(ctx context.Context,
6667 jobsdb .WithStats (stats ),
6768 jobsdb .WithDBHandle (dbPool ),
6869 )
70+ if err := gwBuffRWHandle .Start (); err != nil {
71+ return nil , nil , nil , nil , finally , fmt .Errorf ("starting gw buffer jobsdb handle: %w" , err )
72+ }
73+ finally = func () {
74+ gwBuffRWHandle .Stop ()
75+ }
6976 gwSetupOpt = partitionbuffer .WithSeparateReaderAndWriterPrimaryJobsDBs (gwRODB , gwWODB , gwBuffRWHandle )
7077 } else {
7178 // we have only a reader gw DB, so we create a buffer with reader and
@@ -98,7 +105,7 @@ func setupProcessorPartitionMigrator(ctx context.Context,
98105 partitionbuffer .WithFlushMoveTimeout (bufferFlushMoveTimeout ),
99106 )
100107 if err != nil {
101- return nil , nil , nil , nil , fmt .Errorf ("creating gw partition buffer: %w" , err )
108+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating gw partition buffer: %w" , err )
102109 }
103110
104111 // setup partition buffer for router jobsDB
@@ -120,7 +127,7 @@ func setupProcessorPartitionMigrator(ctx context.Context,
120127 partitionbuffer .WithFlushMoveTimeout (bufferFlushMoveTimeout ),
121128 )
122129 if err != nil {
123- return nil , nil , nil , nil , fmt .Errorf ("creating rt partition buffer: %w" , err )
130+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating rt partition buffer: %w" , err )
124131 }
125132
126133 // setup partition buffer for batchrouter jobsDB
@@ -142,21 +149,21 @@ func setupProcessorPartitionMigrator(ctx context.Context,
142149 partitionbuffer .WithFlushMoveTimeout (bufferFlushMoveTimeout ),
143150 )
144151 if err != nil {
145- return nil , nil , nil , nil , fmt .Errorf ("creating batch rt partition buffer: %w" , err )
152+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating batch rt partition buffer: %w" , err )
146153 }
147154
148155 // setup partition migrator
149156 etcdClient , err := etcdClientProvider ()
150157 if err != nil {
151- return nil , nil , nil , nil , fmt .Errorf ("getting etcd client: %w" , err )
158+ return nil , nil , nil , nil , finally , fmt .Errorf ("getting etcd client: %w" , err )
152159 }
153160 nodeIndex := config .GetIntVar (- 1 , 1 , "PROCESSOR_INDEX" )
154161 if nodeIndex < 0 {
155- return nil , nil , nil , nil , fmt .Errorf ("got invalid node index from config: %d" , nodeIndex )
162+ return nil , nil , nil , nil , finally , fmt .Errorf ("got invalid node index from config: %d" , nodeIndex )
156163 }
157164 nodeName := config .GetStringVar ("" , "HOSTNAME" )
158165 if nodeName == "" {
159- return nil , nil , nil , nil , fmt .Errorf ("got empty node name from config" )
166+ return nil , nil , nil , nil , finally , fmt .Errorf ("got empty node name from config" )
160167 }
161168
162169 targetURLProvider , err := func () (func (targetNodeIndex int ) (string , error ), error ) {
@@ -177,7 +184,7 @@ func setupProcessorPartitionMigrator(ctx context.Context,
177184 }, nil
178185 }()
179186 if err != nil {
180- return nil , nil , nil , nil , fmt .Errorf ("creating target URL provider: %w" , err )
187+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating target URL provider: %w" , err )
181188 }
182189
183190 sourceMigrator , err := sourcenode .NewMigratorBuilder (nodeIndex , nodeName ).
@@ -190,7 +197,7 @@ func setupProcessorPartitionMigrator(ctx context.Context,
190197 WithReaderJobsDBs ([]jobsdb.JobsDB {gwRODB , rtRWDB , brtRWDB }).
191198 Build ()
192199 if err != nil {
193- return nil , nil , nil , nil , fmt .Errorf ("creating source node migrator: %w" , err )
200+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating source node migrator: %w" , err )
194201 }
195202
196203 targetMigrator , err := targetnode .NewMigratorBuilder (nodeIndex , nodeName ).
@@ -205,7 +212,7 @@ func setupProcessorPartitionMigrator(ctx context.Context,
205212 WithUnbufferedJobsDBs ([]jobsdb.JobsDB {rtRWDB , brtRWDB , gwWODB }).
206213 Build ()
207214 if err != nil {
208- return nil , nil , nil , nil , fmt .Errorf ("creating target node migrator: %w" , err )
215+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating target node migrator: %w" , err )
209216 }
210217 ppm , err := processor .NewProcessorPartitionMigratorBuilder (nodeIndex , nodeName ).
211218 WithConfig (config ).
@@ -216,9 +223,9 @@ func setupProcessorPartitionMigrator(ctx context.Context,
216223 WithTargetMigrator (targetMigrator ).
217224 Build ()
218225 if err != nil {
219- return nil , nil , nil , nil , fmt .Errorf ("creating processor partition migrator: %w" , err )
226+ return nil , nil , nil , nil , finally , fmt .Errorf ("creating processor partition migrator: %w" , err )
220227 }
221- return ppm , gwPartitionBuffer , rtPartitionBuffer , brtPartitionBuffer , nil
228+ return ppm , gwPartitionBuffer , rtPartitionBuffer , brtPartitionBuffer , finally , nil
222229}
223230
224231func setupGatewayPartitionMigrator (_ context.Context , _ * config.Config , gwWODB jobsdb.JobsDB ,
0 commit comments