|
| 1 | +# Architecture |
| 2 | + |
| 3 | +[MySQL](https://www.mysql.com/) is the world’s most popular open source database. The "[Charmed MySQL](https://charmhub.io/mysql)" is a Juju-based operator to deploy and support MySQL from [day 0 to day 2](https://codilime.com/blog/day-0-day-1-day-2-the-software-lifecycle-in-the-cloud-age/), it is based on the [MySQL Community Edition](https://www.mysql.com/products/community/) using the built-in cluster functionality: [MySQL InnoDB ClusterSet](https://dev.mysql.com/doc/mysql-shell/8.0/en/innodb-clusterset.html). |
| 4 | + |
| 5 | +<a name="hld"></a> |
| 6 | +## HLD (High Level Design) |
| 7 | + |
| 8 | +The charm design leverages on the SNAP “[charmed-mysql](https://snapcraft.io/charmed-mysql)” which is deployed by Juju on the specified VM/MAAS/bare-metal machine based on Ubuntu Jammy/22.04. SNAP allows to run MySQL service(s) in a secure and isolated environment ([strict confinement](https://ubuntu.com/blog/demystifying-snap-confinement)). The installed SNAP: |
| 9 | +``` |
| 10 | +> juju ssh mysql/0 |
| 11 | +> snap list charmed-mysql |
| 12 | +Name Version Rev Tracking Publisher Notes |
| 13 | +charmed-mysql 8.0.34 69 latest/stable dataplatformbot held |
| 14 | +``` |
| 15 | + |
| 16 | +The SNAP ships the following components: |
| 17 | + |
| 18 | +* MySQL Community Edition (based on Ubuntu APT package "[mysql-server-8.0](https://packages.ubuntu.com/jammy/mysql-server-8.0)") |
| 19 | +* MySQL Router (based on Ubuntu APT package "[mysql-router](https://packages.ubuntu.com/jammy/mysql-router)") |
| 20 | +* MySQL Shell (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/mysql-shell)) |
| 21 | +* Percona XtraBackup (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/xtrabackup)) |
| 22 | +* Prometheus MySQLd Exporter (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/mysqld-exporter)) |
| 23 | +* Prometheus MySQL Router Exporter (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/mysqlrouter-exporter)) |
| 24 | +* Prometheus Grafana dashboards and Loki alert rules are part of the charm revision and missing in SNAP. |
| 25 | + |
| 26 | +Versions of all the components above are carefully chosen to fit functionality of each other. |
| 27 | + |
| 28 | +The Charmed MySQL unit consisting of a several services which are enabled/activated accordingly to the setup: |
| 29 | + |
| 30 | +``` |
| 31 | +> snap services charmed-mysql |
| 32 | +Service Startup Current Notes |
| 33 | +charmed-mysql.mysqld enabled active - |
| 34 | +charmed-mysql.mysqld-exporter disabled inactive - |
| 35 | +charmed-mysql.mysqlrouter-service disabled inactive - |
| 36 | +charmed-mysql.mysqlrouterd-exporter disabled inactive - |
| 37 | +``` |
| 38 | + |
| 39 | +The `mysqld` snap service is a main MySQL instance which is normally up and running right after the charm deployment. |
| 40 | + |
| 41 | +The `mysql-router` snap service used in [Charmed MySQL Router](https://charmhub.io/mysql-router?channel=dpe/edge) only and should be stopped on [Charmed MySQL](https://charmhub.io/mysql) deployments. |
| 42 | + |
| 43 | +All `exporter` services are activated after the relation with [COS Monitoring](/t/9900) only. |
| 44 | + |
| 45 | +> **:information_source: Note:** it is possible to star/stop/restart snap services manually but it is NOT recommended to avoid a split brain with a charm state machine! Do it with a caution!!! |
| 46 | +
|
| 47 | +> **:warning: Important:** all snap resources must be executed under the special user `snapd_daemon` only! |
| 48 | +
|
| 49 | +The snap "charmed-mysql" also ships list of tools used by charm: |
| 50 | +* `charmed-mysql.mysql` (alias `mysql`) - mysql client to connect `mysqld`. |
| 51 | +* `charmed-mysql.mysqlsh` - new [mysql-shell](https://dev.mysql.com/doc/mysql-shell/8.0/en/) client to configure MySQL cluster. |
| 52 | +* `charmed-mysql.xbcloud` - a tool to download and upload full or part of xbstream archive from/to the cloud. |
| 53 | +* `charmed-mysql.xbstream` - a tool to support simultaneous compression and streaming. |
| 54 | +* `charmed-mysql.xtrabackup` - a tool to backup/restore MySQL DB. |
| 55 | + |
| 56 | +The `mysql` and `mysqlsh` are well known and popular tools to manage MySQL. |
| 57 | +The `xtrabackup (xbcloud+xbstream)` used for [MySQL Backups](/t/9896) only to store backups on S3 compatible storage. |
| 58 | + |
| 59 | +<a name="integrations"></a> |
| 60 | +## Integrations |
| 61 | + |
| 62 | +### MySQL Router |
| 63 | + |
| 64 | +[MySQL Router](https://dev.mysql.com/doc/mysql-router/8.0/en/) is part of MySQL InnoDB Cluster, and is lightweight middle-ware that provides transparent routing between your application and back-end MySQL Servers. The "[Charmed MySQL Router](https://charmhub.io/mysql-router)" is an independent charm "Charmed MySQL" can be related with. |
| 65 | + |
| 66 | +### TLS Certificates Operator |
| 67 | + |
| 68 | +[TLS Certificates](https://charmhub.io/tls-certificates-operator) charm responsible for distributing certificates through relationship. Certificates are provided by the operator through Juju configs. For the playground deployments, the [self-signed operator](https://charmhub.io/self-signed-certificates) is available as well. |
| 69 | + |
| 70 | +### S3 Integrator |
| 71 | + |
| 72 | +[S3 Integrator](https://charmhub.io/s3-integrator) is an integrator charm for providing S3 credentials to Charmed MySQL which seek to access shared S3 data. Store the credentials centrally in the integrator charm and relate consumer charms as needed. |
| 73 | + |
| 74 | +### Data Integrator |
| 75 | + |
| 76 | +[Data Integrator](https://charmhub.io/data-integrator) charm is a solution to request DB credentials for non-native Juju applications. Not all applications implement a data_interfaces relation but allow setting credentials via config. Also, some of the applications are run outside of juju. This integrator charm allows receiving credentials which can be passed into application config directly without implementing juju-native relation. |
| 77 | + |
| 78 | +### MySQL Test App |
| 79 | + |
| 80 | +The charm "[MySQL Test App](https://charmhub.io/mysql-test-app)" is a Canonical test application to validate the charm installation / functionality and perform the basic performance tests. |
| 81 | + |
| 82 | +### Grafana |
| 83 | + |
| 84 | +Grafana is an open-source visualization tools that allows to query, visualize, alert on, and visualize metrics from mixed datasources in configurable dashboards for observability. This charms is shipped with its own Grafana dashboard and supports integration with the [Grafana Operator](https://charmhub.io/grafana-k8s) to simplify observability. Please follow [COS Monitoring](/t/9900) setup. |
| 85 | + |
| 86 | +### Loki |
| 87 | + |
| 88 | +Loki is an open-source fully-featured logging system. This charms is shipped with support for the [Loki Operator](https://charmhub.io/loki-k8s) to collect the generated logs. Please follow [COS Monitoring](/t/9900) setup. |
| 89 | + |
| 90 | +### Prometheus |
| 91 | + |
| 92 | +Prometheus is an open-source systems monitoring and alerting toolkit with a dimensional data model, flexible query language, efficient time series database and modern alerting approach. This charm is shipped with a Prometheus exporters, alerts and support for integrating with the [Prometheus Operator](https://charmhub.io/prometheus-k8s) to automatically scrape the targets. Please follow [COS Monitoring](/t/9900) setup. |
| 93 | + |
| 94 | +<a name="lld"></a> |
| 95 | +## LLD (Low Level Design) |
| 96 | + |
| 97 | +Please check the charm state machines displayed on [workflow diagrams](/t/10031). The low-level logic is mostly common for both VM and K8s charms. |
| 98 | + |
| 99 | +<!--- TODO: Describe all possible installations? Cross-model/controller? ---> |
| 100 | + |
| 101 | +### Juju Events |
| 102 | + |
| 103 | +Accordingly to the [Juju SDK](https://juju.is/docs/sdk/event): “an event is a data structure that encapsulates part of the execution context of a charm”. |
| 104 | + |
| 105 | +For this charm, the following events are observed: |
| 106 | + |
| 107 | +1. [on_install](https://juju.is/docs/sdk/install-event): install the snap "charmed-mysql" and perform basic preparations to bootstrap the cluster on the first leader (or join the already configured cluster). |
| 108 | +2. [leader-elected](https://juju.is/docs/sdk/leader-elected-event): generate all the secrets to bootstrap the cluster. |
| 109 | +3. [leader-settings-changed](https://juju.is/docs/sdk/leader-settings-changed-event): Handle the leader settings changed event. |
| 110 | +4. [start](https://juju.is/docs/sdk/start-event): Init/setting up the cluster node. |
| 111 | +5. [config_changed](https://juju.is/docs/sdk/config-changed-event): usually fired in response to a configuration change using the GUI or CLI. Create and set default cluster and cluster-set names in the peer relation databag (on the leader only). |
| 112 | +6. [update-status](https://juju.is/docs/sdk/update-status-event): Takes care of workload health checks. |
| 113 | +<!--- 7. database_storage_detaching: TODO: ops? event? |
| 114 | +8. TODO: any other events? |
| 115 | +---> |
| 116 | + |
| 117 | +### Charm Code Overview |
| 118 | + |
| 119 | +The "[src/charm.py](https://github.com/canonical/mysql-operator/blob/main/src/charm.py)" is the default entry point for a charm and has the [MySQLCharmBase](https://github.com/canonical/mysql-operator/blob/main/lib/charms/mysql/v0/mysql.py) Python class which inherits from CharmBase. |
| 120 | + |
| 121 | +CharmBase is the base class from which all Charms are formed, defined by [Ops](https://juju.is/docs/sdk/ops) (Python framework for developing charms). See more information in [Charm](https://juju.is/docs/sdk/constructs#heading--charm). |
| 122 | + |
| 123 | +The `__init__` method guarantees that the charm observes all events relevant to its operation and handles them. |
| 124 | + |
| 125 | +The VM and K8s charm flavors shares the codebase via [charm libraries](https://juju.is/docs/sdk/libraries) in '[lib/charms/mysql/v0/](https://github.com/canonical/mysql-operator/blob/main/lib/charms/mysql/v0/)': |
| 126 | +``` |
| 127 | +charmcraft list-lib mysql |
| 128 | +Library name API Patch |
| 129 | +backups 0 7 |
| 130 | +mysql 0 45 |
| 131 | +s3_helpers 0 4 |
| 132 | +tls 0 2 |
| 133 | +``` |
0 commit comments