Skip to content

Commit

Permalink
fix(DB): do not assume sharding is always enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Aug 29, 2024
1 parent 79b34aa commit edaa4d1
Showing 1 changed file with 19 additions and 13 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

0 comments on commit edaa4d1

Please sign in to comment.