Skip to content

Commit

Permalink
Merge pull request #15384 from craftcms/feature/db2
Browse files Browse the repository at this point in the history
Share the mutex connection for element bulk ops
  • Loading branch information
brandonkelly authored Jul 24, 2024
2 parents e75a51a + abd68cf commit 85523ea
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- Added `craft\services\Entries::EVENT_AFTER_MOVE_TO_SECTION`.
- Added `craft\services\Entries::EVENT_BEFORE_MOVE_TO_SECTION`.
- Added `craft\services\Entries::moveEntryToSection()`.
- Added `craft\base\ApplicationTrait::getDb2()`. ([#15384](https://github.com/craftcms/cms/pull/15384))
- `craft\helpers\DateTimeHelper::toIso8601()` now has a `$setToUtc` argument.
- `craft\helpers\UrlHelper::cpUrl()` now returns URLs based on the primary site’s base URL (if it has one), for console requests if the `baseCpUrl` config setting isn’t set, and the `@web` alias wasn’t explicitly defined. ([#15374](https://github.com/craftcms/cms/issues/15374))
- Deprecated `craft\fields\BaseRelationField::$localizeRelations`.
Expand All @@ -85,3 +86,4 @@
- Updated Yii to 2.0.51.
- Updated yii2-debug to 2.1.25.
- Updated svg-sanitizer to 0.19.
- Fixed a bug where element operations could cause deadlocks when multiple authors were working simultaneously. ([#15329](https://github.com/craftcms/cms/issues/15329))
15 changes: 15 additions & 0 deletions src/base/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
* @property-read Conditions $conditions The conditions service
* @property-read Config $config The config service
* @property-read Connection $db The database connection component
* @property-read Connection $db2 The database connection component used for mutex locks and element bulk op records
* @property-read Dashboard $dashboard The dashboard service
* @property-read Deprecator $deprecator The deprecator service
* @property-read Drafts $drafts The drafts service
Expand Down Expand Up @@ -1104,6 +1105,20 @@ public function getDashboard(): Dashboard
return $this->get('dashboard');
}

/**
* Returns the database connection used for mutex locks and element bulk op records.
*
* This helps avoid erratic behavior when locks are used during transactions
* (see https://makandracards.com/makandra/17437-mysql-careful-when-using-database-locks-in-transactions).
*
* @return Connection
* @since 5.3.0
*/
public function getDb2(): Connection
{
return $this->get('db2');
}

/**
* Returns the deprecator service.
*
Expand Down
7 changes: 6 additions & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.2.8',
'schemaVersion' => '5.0.0.21',
'schemaVersion' => '5.3.0.0',
'minVersionRequired' => '4.5.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down Expand Up @@ -219,6 +219,11 @@
return Craft::createObject($config);
},

'db2' => function() {
$config = craft\helpers\App::dbConfig();
return Craft::createObject($config);
},

'formatter' => function() {
return Craft::$app->getFormattingLocale()->getFormatter();
},
Expand Down
8 changes: 2 additions & 6 deletions src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,21 +1005,17 @@ public static function mailerConfig(?MailSettings $settings = null): array
*/
public static function dbMutexConfig(): array
{
// Use a dedicated connection, to avoid erratic behavior when locks are used during transactions
// https://makandracards.com/makandra/17437-mysql-careful-when-using-database-locks-in-transactions
$dbConfig = static::dbConfig();

if (Craft::$app->getDb()->getIsMysql()) {
return [
'class' => MysqlMutex::class,
'db' => $dbConfig,
'db' => 'db2',
'keyPrefix' => Craft::$app->id,
];
}

return [
'class' => PgsqlMutex::class,
'db' => $dbConfig,
'db' => 'db2',
];
}

Expand Down
1 change: 0 additions & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,6 @@ public function addForeignKeys(): void
$this->addForeignKey(null, Table::ELEMENTS, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['revisionId'], Table::REVISIONS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['fieldLayoutId'], Table::FIELDLAYOUTS, ['id'], 'SET NULL', null);
$this->addForeignKey(null, Table::ELEMENTS_BULKOPS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS_OWNERS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS_OWNERS, ['ownerId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS_SITES, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/m240723_214330_drop_bulkop_fk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m240723_214330_drop_bulkop_fk migration.
*/
class m240723_214330_drop_bulkop_fk extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->dropForeignKeyIfExists(Table::ELEMENTS_BULKOPS, ['elementId']);
return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m240723_214330_drop_bulkop_fk cannot be reverted.\n";
return false;
}
}
22 changes: 20 additions & 2 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\base\NestedElementInterface;
use craft\behaviors\DraftBehavior;
use craft\behaviors\RevisionBehavior;
use craft\db\Connection;
use craft\db\Query;
use craft\db\QueryAbortedException;
use craft\db\Table;
Expand Down Expand Up @@ -77,6 +78,7 @@
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\caching\TagDependency;
use yii\di\Instance;

/**
* The Elements service provides APIs for managing elements.
Expand Down Expand Up @@ -513,6 +515,15 @@ class Elements extends Component
*/
private ?bool $_updateSearchIndex = null;

/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->bulkOpDb = Instance::ensure($this->bulkOpDb, Connection::class);
}

/**
* Creates an element with a given config.
*
Expand Down Expand Up @@ -548,6 +559,13 @@ public function createElementQuery(string $elementType): ElementQueryInterface
return $elementType::find();
}

/**
* @var Connection|array|string the DB connection object or the application component ID of the DB connection
* that should be used to store element bulk op records.
* @since 5.3.0
*/
public Connection|array|string $bulkOpDb = 'db2';

// Element caches
// -------------------------------------------------------------------------

Expand Down Expand Up @@ -1135,7 +1153,7 @@ public function endBulkOp(string $key): void
]));
}

Db::delete(Table::ELEMENTS_BULKOPS, ['key' => $key]);
Db::delete(Table::ELEMENTS_BULKOPS, ['key' => $key], db: $this->bulkOpDb);
}

/**
Expand All @@ -1157,7 +1175,7 @@ public function trackElementInBulkOps(ElementInterface $element): void
'elementId' => $element->id,
'key' => $key,
'timestamp' => $timestamp,
]);
], db: $this->bulkOpDb);
}
}

Expand Down

0 comments on commit 85523ea

Please sign in to comment.