Skip to content

Commit 59ba055

Browse files
authored
Merge pull request #47612 from nextcloud/backport/47611/stable30
[stable30] fix(DB): set sharding parameters only when intended
2 parents 0b5c424 + 656412d commit 59ba055

5 files changed

Lines changed: 36 additions & 19 deletions

File tree

lib/private/DB/Connection.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class Connection extends PrimaryReadReplicaConnection {
9191
protected array $shards = [];
9292
protected ShardConnectionManager $shardConnectionManager;
9393
protected AutoIncrementHandler $autoIncrementHandler;
94+
protected bool $isShardingEnabled;
9495

9596
public const SHARD_PRESETS = [
9697
'filecache' => [
@@ -130,14 +131,17 @@ public function __construct(
130131
parent::__construct($params, $driver, $config, $eventManager);
131132
$this->adapter = new $params['adapter']($this);
132133
$this->tablePrefix = $params['tablePrefix'];
133-
134-
/** @psalm-suppress InvalidArrayOffset */
135-
$this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class);
136-
/** @psalm-suppress InvalidArrayOffset */
137-
$this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler(
138-
Server::get(ICacheFactory::class),
139-
$this->shardConnectionManager,
140-
);
134+
$this->isShardingEnabled = isset($this->params['sharding']) && !empty($this->params['sharding']);
135+
136+
if ($this->isShardingEnabled) {
137+
/** @psalm-suppress InvalidArrayOffset */
138+
$this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class);
139+
/** @psalm-suppress InvalidArrayOffset */
140+
$this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler(
141+
Server::get(ICacheFactory::class),
142+
$this->shardConnectionManager,
143+
);
144+
}
141145
$this->systemConfig = \OC::$server->getSystemConfig();
142146
$this->clock = Server::get(ClockInterface::class);
143147
$this->logger = Server::get(LoggerInterface::class);
@@ -192,10 +196,12 @@ public function __construct(
192196
*/
193197
public function getShardConnections(): array {
194198
$connections = [];
195-
foreach ($this->shards as $shardDefinition) {
196-
foreach ($shardDefinition->getAllShards() as $shard) {
197-
/** @var ConnectionAdapter $connection */
198-
$connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard);
199+
if ($this->isShardingEnabled) {
200+
foreach ($this->shards as $shardDefinition) {
201+
foreach ($shardDefinition->getAllShards() as $shard) {
202+
/** @var ConnectionAdapter $connection */
203+
$connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard);
204+
}
199205
}
200206
}
201207
return $connections;
@@ -255,7 +261,7 @@ public function getQueryBuilder(): IQueryBuilder {
255261
$this->systemConfig,
256262
$this->logger
257263
);
258-
if (count($this->partitions) > 0) {
264+
if ($this->isShardingEnabled && count($this->partitions) > 0) {
259265
$builder = new PartitionedQueryBuilder(
260266
$builder,
261267
$this->shards,

lib/private/DB/ConnectionFactory.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,16 @@ public function createConnectionParams(string $configPrefix = '', array $additio
225225
}
226226

227227
$connectionParams['sharding'] = $this->config->getValue('dbsharding', []);
228-
$connectionParams['shard_connection_manager'] = $this->shardConnectionManager;
229-
$connectionParams['auto_increment_handler'] = new AutoIncrementHandler(
230-
$this->cacheFactory,
231-
$this->shardConnectionManager,
232-
);
228+
if (!empty($connectionParams['sharding'])) {
229+
$connectionParams['shard_connection_manager'] = $this->shardConnectionManager;
230+
$connectionParams['auto_increment_handler'] = new AutoIncrementHandler(
231+
$this->cacheFactory,
232+
$this->shardConnectionManager,
233+
);
234+
} else {
235+
// just in case only the presence could lead to funny behaviour
236+
unset($connectionParams['sharding']);
237+
}
233238

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

tests/lib/DB/QueryBuilder/Partitioned/PartitionedQueryBuilderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class PartitionedQueryBuilderTest extends TestCase {
2626
private AutoIncrementHandler $autoIncrementHandler;
2727

2828
protected function setUp(): void {
29+
if (PHP_INT_SIZE < 8) {
30+
$this->markTestSkipped('Test requires 64bit');
31+
}
2932
$this->connection = Server::get(IDBConnection::class);
3033
$this->shardConnectionManager = Server::get(ShardConnectionManager::class);
3134
$this->autoIncrementHandler = Server::get(AutoIncrementHandler::class);

tests/lib/DB/QueryBuilder/Sharded/SharedQueryBuilderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class SharedQueryBuilderTest extends TestCase {
2727
private AutoIncrementHandler $autoIncrementHandler;
2828

2929
protected function setUp(): void {
30+
if (PHP_INT_SIZE < 8) {
31+
$this->markTestSkipped('Test requires 64bit');
32+
}
3033
$this->connection = Server::get(IDBConnection::class);
3134
$this->autoIncrementHandler = Server::get(AutoIncrementHandler::class);
3235
}

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patch level
1010
// when updating major/minor version number.
1111

12-
$OC_Version = [30, 0, 0, 10];
12+
$OC_Version = [30, 0, 0, 11];
1313

1414
// The human-readable string
1515
$OC_VersionString = '30.0.0 RC2';

0 commit comments

Comments
 (0)