@@ -114,10 +114,10 @@ func (c *Cluster) syncPublication(dbName string, databaseSlotsList map[string]za
114
114
}
115
115
116
116
for slotName , slotAndPublication := range databaseSlotsList {
117
- tables := slotAndPublication .Publication
118
- tableNames := make ([]string , len (tables ))
117
+ newTables := slotAndPublication .Publication
118
+ tableNames := make ([]string , len (newTables ))
119
119
i := 0
120
- for t := range tables {
120
+ for t := range newTables {
121
121
tableName , schemaName := getTableSchema (t )
122
122
tableNames [i ] = fmt .Sprintf ("%s.%s" , schemaName , tableName )
123
123
i ++
@@ -126,6 +126,12 @@ func (c *Cluster) syncPublication(dbName string, databaseSlotsList map[string]za
126
126
tableList := strings .Join (tableNames , ", " )
127
127
128
128
currentTables , exists := currentPublications [slotName ]
129
+ // if newTables is empty it means that it's definition was removed from streams section
130
+ // but when slot is defined in manifest we should sync publications, too
131
+ // by reusing current tables we make sure it is not
132
+ if len (newTables ) == 0 {
133
+ tableList = currentTables
134
+ }
129
135
if ! exists {
130
136
createPublications [slotName ] = tableList
131
137
} else if currentTables != tableList {
@@ -350,16 +356,8 @@ func (c *Cluster) syncStreams() error {
350
356
return nil
351
357
}
352
358
353
- databaseSlots := make (map [string ]map [string ]zalandov1.Slot )
354
- slotsToSync := make (map [string ]map [string ]string )
355
- requiredPatroniConfig := c .Spec .Patroni
356
-
357
- if len (requiredPatroniConfig .Slots ) > 0 {
358
- for slotName , slotConfig := range requiredPatroniConfig .Slots {
359
- slotsToSync [slotName ] = slotConfig
360
- }
361
- }
362
-
359
+ // create map with every database and empty slot defintion
360
+ // we need it to detect removal of streams from databases
363
361
if err := c .initDbConn (); err != nil {
364
362
return fmt .Errorf ("could not init database connection" )
365
363
}
@@ -372,13 +370,28 @@ func (c *Cluster) syncStreams() error {
372
370
if err != nil {
373
371
return fmt .Errorf ("could not get list of databases: %v" , err )
374
372
}
375
- // get database name with empty list of slot, except template0 and template1
373
+ databaseSlots := make ( map [ string ] map [ string ]zalandov1. Slot )
376
374
for dbName := range listDatabases {
377
375
if dbName != "template0" && dbName != "template1" {
378
376
databaseSlots [dbName ] = map [string ]zalandov1.Slot {}
379
377
}
380
378
}
381
379
380
+ // need to take explicitly defined slots into account whey syncing Patroni config
381
+ slotsToSync := make (map [string ]map [string ]string )
382
+ requiredPatroniConfig := c .Spec .Patroni
383
+ if len (requiredPatroniConfig .Slots ) > 0 {
384
+ for slotName , slotConfig := range requiredPatroniConfig .Slots {
385
+ slotsToSync [slotName ] = slotConfig
386
+ if _ , exists := databaseSlots [slotConfig ["database" ]]; exists {
387
+ databaseSlots [slotConfig ["database" ]][slotName ] = zalandov1.Slot {
388
+ Slot : slotConfig ,
389
+ Publication : make (map [string ]acidv1.StreamTable ),
390
+ }
391
+ }
392
+ }
393
+ }
394
+
382
395
// get list of required slots and publications, group by database
383
396
for _ , stream := range c .Spec .Streams {
384
397
if _ , exists := databaseSlots [stream .Database ]; ! exists {
@@ -391,13 +404,13 @@ func (c *Cluster) syncStreams() error {
391
404
"type" : "logical" ,
392
405
}
393
406
slotName := getSlotName (stream .Database , stream .ApplicationId )
394
- if _ , exists := databaseSlots [stream.Database ][slotName ]; ! exists {
407
+ slotAndPublication , exists := databaseSlots [stream.Database ][slotName ]
408
+ if ! exists {
395
409
databaseSlots [stream.Database ][slotName ] = zalandov1.Slot {
396
410
Slot : slot ,
397
411
Publication : stream .Tables ,
398
412
}
399
413
} else {
400
- slotAndPublication := databaseSlots [stream.Database ][slotName ]
401
414
streamTables := slotAndPublication .Publication
402
415
for tableName , table := range stream .Tables {
403
416
if _ , exists := streamTables [tableName ]; ! exists {
@@ -492,16 +505,17 @@ func (c *Cluster) syncStream(appId string) error {
492
505
continue
493
506
}
494
507
streamExists = true
508
+ c .Streams [appId ] = & stream
495
509
desiredStreams := c .generateFabricEventStream (appId )
496
510
if ! reflect .DeepEqual (stream .ObjectMeta .OwnerReferences , desiredStreams .ObjectMeta .OwnerReferences ) {
497
511
c .logger .Infof ("owner references of event streams with applicationId %s do not match the current ones" , appId )
498
512
stream .ObjectMeta .OwnerReferences = desiredStreams .ObjectMeta .OwnerReferences
499
513
c .setProcessName ("updating event streams with applicationId %s" , appId )
500
- stream , err := c .KubeClient .FabricEventStreams (stream .Namespace ).Update (context .TODO (), & stream , metav1.UpdateOptions {})
514
+ updatedStream , err := c .KubeClient .FabricEventStreams (stream .Namespace ).Update (context .TODO (), & stream , metav1.UpdateOptions {})
501
515
if err != nil {
502
516
return fmt .Errorf ("could not update event streams with applicationId %s: %v" , appId , err )
503
517
}
504
- c .Streams [appId ] = stream
518
+ c .Streams [appId ] = updatedStream
505
519
}
506
520
if match , reason := c .compareStreams (& stream , desiredStreams ); ! match {
507
521
c .logger .Infof ("updating event streams with applicationId %s: %s" , appId , reason )
0 commit comments