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

fix(DB): set sharding parameters only when intended #47611

Merged
merged 4 commits into from
Aug 29, 2024
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
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 = [31, 0, 0, 1];
$OC_Version = [31, 0, 0, 2];

// The human-readable string
$OC_VersionString = '31.0.0 dev';
Expand Down
Loading