Skip to content

Commit

Permalink
fix(db): Prevent two connections for single node databases
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed May 28, 2024
1 parent f0ec548 commit fe94824
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\Server;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
use function count;
use function in_array;

class Connection extends PrimaryReadReplicaConnection {
Expand Down Expand Up @@ -73,7 +74,7 @@ class Connection extends PrimaryReadReplicaConnection {
* @throws \Exception
*/
public function __construct(
array $params,
private array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
Expand Down Expand Up @@ -138,6 +139,15 @@ public function connect($connectionName = null) {
}
}

protected function performConnect(?string $connectionName = null): bool {
if (($connectionName ?? 'replica') === 'replica'
&& count($this->params['replica']) === 1
&& $this->params['primary'] === $this->params['replica'][0]) {
return parent::performConnect('primary');
}
return parent::performConnect($connectionName);
}

public function getStats(): array {
return [
'built' => $this->queriesBuilt,
Expand Down
98 changes: 98 additions & 0 deletions tests/lib/DB/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\DB;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use OC\DB\Adapter;
use OC\DB\Connection;
use Test\TestCase;

/**
* @group DB
*/
class ConnectionTest extends TestCase {

public function testSingleNodeConnectsToPrimaryOnly(): void {
$connectionParams = [
'user' => 'test',
'password' => 'topsecret',
'host' => 'test',
];
$adapter = $this->createMock(Adapter::class);
$driver = $this->createMock(Driver::class);
$configuration = $this->createMock(Configuration::class);
$connection = $this->getMockBuilder(Connection::class)
->onlyMethods(['connectTo'])
->setConstructorArgs([
[
'adapter' => $adapter,
'tablePrefix' => 'nctest',
'primary' => $connectionParams,
'replica' => [
$connectionParams,
],
],
$driver,
$configuration,
])
->getMock();
$driverConnection = $this->createMock(DriverConnection::class);
$connection->expects(self::once())
->method('connectTo')
->with('primary')
->willReturn($driverConnection);

$connection->ensureConnectedToReplica();
$connection->ensureConnectedToPrimary();
$connection->ensureConnectedToReplica();
}

public function testClusterConnectsToPrimaryAndReplica(): void {
$connectionParamsPrimary = [
'user' => 'test',
'password' => 'topsecret',
'host' => 'testprimary',
];
$connectionParamsReplica = [
'user' => 'test',
'password' => 'topsecret',
'host' => 'testreplica',
];
$adapter = $this->createMock(Adapter::class);
$driver = $this->createMock(Driver::class);
$configuration = $this->createMock(Configuration::class);
$connection = $this->getMockBuilder(Connection::class)
->onlyMethods(['connectTo'])
->setConstructorArgs([
[
'adapter' => $adapter,
'tablePrefix' => 'nctest',
'primary' => $connectionParamsPrimary,
'replica' => [
$connectionParamsReplica,
],
],
$driver,
$configuration,
])
->getMock();
$driverConnection = $this->createMock(DriverConnection::class);
$connection->expects(self::exactly(2))
->method('connectTo')
->willReturn($driverConnection);

$connection->ensureConnectedToReplica();
$connection->ensureConnectedToPrimary();
$connection->ensureConnectedToReplica();
}

}

0 comments on commit fe94824

Please sign in to comment.