Skip to content

Commit

Permalink
Merge pull request #47612 from nextcloud/backport/47611/stable30
Browse files Browse the repository at this point in the history
[stable30] fix(DB): set sharding parameters only when intended
  • Loading branch information
blizzz committed Aug 29, 2024
2 parents 0b5c424 + 656412d commit 59ba055
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
32 changes: 19 additions & 13 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Connection extends PrimaryReadReplicaConnection {
protected array $shards = [];
protected ShardConnectionManager $shardConnectionManager;
protected AutoIncrementHandler $autoIncrementHandler;
protected bool $isShardingEnabled;

public const SHARD_PRESETS = [
'filecache' => [
Expand Down Expand Up @@ -130,14 +131,17 @@ public function __construct(
parent::__construct($params, $driver, $config, $eventManager);
$this->adapter = new $params['adapter']($this);
$this->tablePrefix = $params['tablePrefix'];

/** @psalm-suppress InvalidArrayOffset */
$this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class);
/** @psalm-suppress InvalidArrayOffset */
$this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler(
Server::get(ICacheFactory::class),
$this->shardConnectionManager,
);
$this->isShardingEnabled = isset($this->params['sharding']) && !empty($this->params['sharding']);

if ($this->isShardingEnabled) {
/** @psalm-suppress InvalidArrayOffset */
$this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class);
/** @psalm-suppress InvalidArrayOffset */
$this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler(
Server::get(ICacheFactory::class),
$this->shardConnectionManager,
);
}
$this->systemConfig = \OC::$server->getSystemConfig();
$this->clock = Server::get(ClockInterface::class);
$this->logger = Server::get(LoggerInterface::class);
Expand Down Expand Up @@ -192,10 +196,12 @@ public function __construct(
*/
public function getShardConnections(): array {
$connections = [];
foreach ($this->shards as $shardDefinition) {
foreach ($shardDefinition->getAllShards() as $shard) {
/** @var ConnectionAdapter $connection */
$connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard);
if ($this->isShardingEnabled) {
foreach ($this->shards as $shardDefinition) {
foreach ($shardDefinition->getAllShards() as $shard) {
/** @var ConnectionAdapter $connection */
$connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard);
}
}
}
return $connections;
Expand Down Expand Up @@ -255,7 +261,7 @@ public function getQueryBuilder(): IQueryBuilder {
$this->systemConfig,
$this->logger
);
if (count($this->partitions) > 0) {
if ($this->isShardingEnabled && count($this->partitions) > 0) {
$builder = new PartitionedQueryBuilder(
$builder,
$this->shards,
Expand Down
15 changes: 10 additions & 5 deletions lib/private/DB/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,16 @@ public function createConnectionParams(string $configPrefix = '', array $additio
}

$connectionParams['sharding'] = $this->config->getValue('dbsharding', []);
$connectionParams['shard_connection_manager'] = $this->shardConnectionManager;
$connectionParams['auto_increment_handler'] = new AutoIncrementHandler(
$this->cacheFactory,
$this->shardConnectionManager,
);
if (!empty($connectionParams['sharding'])) {
$connectionParams['shard_connection_manager'] = $this->shardConnectionManager;
$connectionParams['auto_increment_handler'] = new AutoIncrementHandler(
$this->cacheFactory,
$this->shardConnectionManager,
);
} else {
// just in case only the presence could lead to funny behaviour
unset($connectionParams['sharding']);
}

$connectionParams = array_merge($connectionParams, $additionalConnectionParams);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class PartitionedQueryBuilderTest extends TestCase {
private AutoIncrementHandler $autoIncrementHandler;

protected function setUp(): void {
if (PHP_INT_SIZE < 8) {
$this->markTestSkipped('Test requires 64bit');
}
$this->connection = Server::get(IDBConnection::class);
$this->shardConnectionManager = Server::get(ShardConnectionManager::class);
$this->autoIncrementHandler = Server::get(AutoIncrementHandler::class);
Expand Down
3 changes: 3 additions & 0 deletions tests/lib/DB/QueryBuilder/Sharded/SharedQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class SharedQueryBuilderTest extends TestCase {
private AutoIncrementHandler $autoIncrementHandler;

protected function setUp(): void {
if (PHP_INT_SIZE < 8) {
$this->markTestSkipped('Test requires 64bit');
}
$this->connection = Server::get(IDBConnection::class);
$this->autoIncrementHandler = Server::get(AutoIncrementHandler::class);
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patch level
// when updating major/minor version number.

$OC_Version = [30, 0, 0, 10];
$OC_Version = [30, 0, 0, 11];

// The human-readable string
$OC_VersionString = '30.0.0 RC2';
Expand Down

0 comments on commit 59ba055

Please sign in to comment.