18
18
BYTES_1MB ,
19
19
MySQLAddInstanceToClusterError ,
20
20
MySQLCharmBase ,
21
- MySQLConfigureInstanceError ,
22
- MySQLConfigureMySQLUsersError ,
23
21
MySQLCreateClusterError ,
24
22
MySQLGetClusterPrimaryAddressError ,
25
23
MySQLGetMemberStateError ,
72
70
)
73
71
from k8s_helpers import KubernetesHelpers
74
72
from log_rotate_manager import LogRotateManager
75
- from mysql_k8s_helpers import (
76
- MySQL ,
77
- MySQLCreateCustomConfigFileError ,
78
- MySQLInitialiseMySQLDError ,
79
- )
73
+ from mysql_k8s_helpers import MySQL
80
74
from relations .mysql import MySQLRelation
81
75
from relations .mysql_provider import MySQLProvider
82
76
from relations .mysql_root import MySQLRootRelation
@@ -276,7 +270,7 @@ def _is_unit_waiting_to_join_cluster(self) -> bool:
276
270
and not self .unit_peer_data .get ("unit-initialized" )
277
271
)
278
272
279
- def _join_unit_to_cluster (self ) -> None :
273
+ def join_unit_to_cluster (self ) -> None :
280
274
"""Join the unit to the cluster.
281
275
282
276
Try to join the unit from the primary unit.
@@ -485,45 +479,45 @@ def _open_ports(self) -> None:
485
479
except ops .ModelError :
486
480
logger .exception ("failed to open port" )
487
481
488
- def _configure_instance (self , container ) -> bool :
489
- """Configure the instance for use in Group Replication."""
490
- try :
491
- # Run mysqld for the first time to
492
- # bootstrap the data directory and users
493
- logger .debug ("Initializing instance" )
494
- self ._mysql .fix_data_dir (container )
495
- self ._mysql .initialise_mysqld ()
496
-
497
- # Add the pebble layer
498
- logger .debug ("Adding pebble layer" )
499
- container .add_layer (MYSQLD_SAFE_SERVICE , self ._pebble_layer , combine = False )
500
- container .restart (MYSQLD_SAFE_SERVICE )
501
-
502
- logger .debug ("Waiting for instance to be ready" )
503
- self ._mysql .wait_until_mysql_connection (check_port = False )
504
-
505
- logger .info ("Configuring instance" )
506
- # Configure all base users and revoke privileges from the root users
507
- self ._mysql .configure_mysql_users ()
508
- # Configure instance as a cluster node
509
- self ._mysql .configure_instance ()
482
+ def _write_mysqld_configuration (self ):
483
+ """Write the mysqld configuration to the file."""
484
+ memory_limit_bytes = (self .config .profile_limit_memory or 0 ) * BYTES_1MB
485
+ new_config_content , _ = self ._mysql .render_mysqld_configuration (
486
+ profile = self .config .profile ,
487
+ memory_limit = memory_limit_bytes ,
488
+ )
489
+ self ._mysql .write_content_to_file (path = MYSQLD_CONFIG_FILE , content = new_config_content )
510
490
511
- if self .has_cos_relation :
512
- if container .get_services (MYSQLD_EXPORTER_SERVICE )[
513
- MYSQLD_EXPORTER_SERVICE
514
- ].is_running ():
515
- # Restart exporter service after configuration
516
- container .restart (MYSQLD_EXPORTER_SERVICE )
517
- else :
518
- container .start (MYSQLD_EXPORTER_SERVICE )
519
- except (
520
- MySQLConfigureInstanceError ,
521
- MySQLConfigureMySQLUsersError ,
522
- MySQLInitialiseMySQLDError ,
523
- MySQLCreateCustomConfigFileError ,
524
- ) as e :
525
- logger .debug ("Unable to configure instance: {}" .format (e ))
526
- return False
491
+ def _configure_instance (self , container ) -> None :
492
+ """Configure the instance for use in Group Replication."""
493
+ # Run mysqld for the first time to
494
+ # bootstrap the data directory and users
495
+ logger .debug ("Initializing instance" )
496
+ self ._mysql .fix_data_dir (container )
497
+ self ._mysql .initialise_mysqld ()
498
+
499
+ # Add the pebble layer
500
+ logger .debug ("Adding pebble layer" )
501
+ container .add_layer (MYSQLD_SAFE_SERVICE , self ._pebble_layer , combine = True )
502
+ container .restart (MYSQLD_SAFE_SERVICE )
503
+
504
+ logger .debug ("Waiting for instance to be ready" )
505
+ self ._mysql .wait_until_mysql_connection (check_port = False )
506
+
507
+ logger .info ("Configuring instance" )
508
+ # Configure all base users and revoke privileges from the root users
509
+ self ._mysql .configure_mysql_users ()
510
+ # Configure instance as a cluster node
511
+ self ._mysql .configure_instance ()
512
+
513
+ if self .has_cos_relation :
514
+ if container .get_services (MYSQLD_EXPORTER_SERVICE )[
515
+ MYSQLD_EXPORTER_SERVICE
516
+ ].is_running ():
517
+ # Restart exporter service after configuration
518
+ container .restart (MYSQLD_EXPORTER_SERVICE )
519
+ else :
520
+ container .start (MYSQLD_EXPORTER_SERVICE )
527
521
528
522
self ._open_ports ()
529
523
@@ -535,8 +529,6 @@ def _configure_instance(self, container) -> bool:
535
529
# Do not block the charm if the version cannot be retrieved
536
530
pass
537
531
538
- return True
539
-
540
532
def _mysql_pebble_ready_checks (self , event ) -> bool :
541
533
"""Executes some checks to see if it is safe to execute the pebble ready handler."""
542
534
if not self ._is_peer_data_set :
@@ -560,17 +552,13 @@ def _on_mysql_pebble_ready(self, event) -> None:
560
552
event .defer ()
561
553
return
562
554
555
+ if not self .upgrade .idle :
556
+ # when upgrading pebble ready is
557
+ # task delegated to upgrade code
558
+ return
559
+
563
560
container = event .workload
564
- try :
565
- memory_limit_bytes = (self .config .profile_limit_memory or 0 ) * BYTES_1MB
566
- new_config_content , _ = self ._mysql .render_mysqld_configuration (
567
- profile = self .config .profile ,
568
- memory_limit = memory_limit_bytes ,
569
- )
570
- self ._mysql .write_content_to_file (path = MYSQLD_CONFIG_FILE , content = new_config_content )
571
- except MySQLCreateCustomConfigFileError :
572
- logger .exception ("Unable to write custom config file" )
573
- raise
561
+ self ._write_mysqld_configuration ()
574
562
575
563
logger .info ("Setting up the logrotate configurations" )
576
564
self ._mysql .setup_logrotate_config ()
@@ -585,15 +573,13 @@ def _on_mysql_pebble_ready(self, event) -> None:
585
573
self .unit .status = MaintenanceStatus ("Initialising mysqld" )
586
574
587
575
# First run setup
588
- if not self ._configure_instance (container ):
589
- raise
576
+ self ._configure_instance (container )
590
577
591
578
if not self .unit .is_leader ():
592
579
# Non-leader units should wait for leader to add them to the cluster
593
580
self .unit .status = WaitingStatus ("Waiting for instance to join the cluster" )
594
581
self .unit_peer_data .update ({"member-role" : "secondary" , "member-state" : "waiting" })
595
-
596
- self ._join_unit_to_cluster ()
582
+ self .join_unit_to_cluster ()
597
583
return
598
584
599
585
try :
@@ -607,7 +593,6 @@ def _on_mysql_pebble_ready(self, event) -> None:
607
593
self .app_peer_data ["units-added-to-cluster" ] = "1"
608
594
609
595
state , role = self ._mysql .get_member_state ()
610
-
611
596
self .unit_peer_data .update (
612
597
{"member-state" : state , "member-role" : role , "unit-initialized" : "True" }
613
598
)
@@ -702,7 +687,7 @@ def _on_update_status(self, _: Optional[UpdateStatusEvent]) -> None:
702
687
if not self .unit .is_leader () and self ._is_unit_waiting_to_join_cluster ():
703
688
# join cluster test takes precedence over blocked test
704
689
# due to matching criteria
705
- self ._join_unit_to_cluster ()
690
+ self .join_unit_to_cluster ()
706
691
return
707
692
708
693
if self ._is_cluster_blocked ():
@@ -748,7 +733,7 @@ def _on_peer_relation_changed(self, event: RelationChangedEvent) -> None:
748
733
return
749
734
750
735
if self ._is_unit_waiting_to_join_cluster ():
751
- self ._join_unit_to_cluster ()
736
+ self .join_unit_to_cluster ()
752
737
753
738
def _on_database_storage_detaching (self , _ ) -> None :
754
739
"""Handle the database storage detaching event."""
0 commit comments