Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GOVCMSD8-556]: Add read replica access. #83

Merged
merged 5 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions .docker/images/govcms8/scripts/govcms-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ mkdir -p /app/web/sites/default/files/private/tmp/
config_count=`ls -1 /app/config/default/*.yml 2>/dev/null | wc -l`
dev_config_count=`ls -1 /app/config/dev/*.yml 2>/dev/null | wc -l`

# Database options to configure the remote to use the read replica when
# performing read operations.
sync_opts=""
read_replica_enabled () {
if [[ -z "$MARIADB_READREPLICA_HOSTS" ]]; then
return
fi

if tables=$(drush @govcms.prod sqlq 'show tables;' --database=read 2> /dev/null) && [ -n "$tables" ]; then
echo "Replica is available, using for database operations."
sync_opts="--source-database=read"
fi
}

# Database updates, cache rebuild, optional config imports.
common_deploy () {
drush updb -y
Expand All @@ -41,11 +55,14 @@ common_deploy () {

# Non production environments.
if [[ "$LAGOON_ENVIRONMENT_TYPE" != "production" ]]; then

read_replica_enabled $sync_opts

if ! drush status --fields=bootstrap | grep -q "Successful"; then
# Import prod db in Lagoon development environments.
if [[ ! -z "$LAGOON_ENVIRONMENT_TYPE" && "$LAGOON_ENVIRONMENT_TYPE" != "local" ]]; then
drush sql-drop -y
drush sql-sync @govcms.prod @self -y
drush sql-sync @govcms.prod @self $sync_opts -y
common_deploy
else
echo "Drupal not installed."
Expand All @@ -55,7 +72,7 @@ if [[ "$LAGOON_ENVIRONMENT_TYPE" != "production" ]]; then
if [[ "$GOVCMS_TEST_CANARY" = TRUE ]]; then
echo "GOVCMS_TEST_CANARY is set, syncing the database."
drush sql-drop -y
drush sql-sync @govcms.prod @self -y
drush sql-sync @govcms.prod @self $sync_opts -y
fi
common_deploy
fi
Expand Down
31 changes: 29 additions & 2 deletions .docker/images/govcms8/settings/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,43 @@ public function __toString() {

// Lagoon Database connection
if (getenv('LAGOON')) {
$databases['default']['default'] = [
$db_conf = [
'driver' => 'mysql',
'database' => getenv('MARIADB_DATABASE') ?: 'drupal',
'username' => getenv('MARIADB_USERNAME') ?: 'drupal',
'password' => getenv('MARIADB_PASSWORD') ?: 'drupal',
'host' => getenv('MARIADB_HOST') ?: 'mariadb',
'port' => 3306,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
];


$databases['default']['default'] = array_merge($db_conf, [
'host' => getenv('MARIADB_HOST') ?: 'mariadb'
]);

if (getenv('MARIADB_READREPLICA_HOSTS')) {
$replica_hosts = explode(' ', getenv('MARIADB_READREPLICA_HOSTS'));
$replica_hosts = array_map('trim', $replica_hosts);

if (!empty($replica_hosts)) {
// Add a standalone connection to the read replica. This allows Drush to target
// the readers directly with --database=read.
$databases['read']['default'] = array_merge($db_conf, [
'host' => $replica_hosts[0],
]);

foreach ($replica_hosts as $replica_host) {
// Add replica support to the default database connection. This allows services
// to use the database.replica service for particular operations.
// @TODO: Lagoon should expose MARAIDB replica hosts as an array so we can
// scale the replicas horizontally.
$databases['default']['replica'][] = array_merge($db_conf, [
'host' => $replica_host,
]);
}
}
}
}

// Lagoon Solr connection
Expand Down