From: Robin Appelman Date: Thu, 18 Jul 2024 15:16:01 +0000 (+0200) Subject: fix: only allow pre-defined shards X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fc05a67f192eb96870b02f574d14318792c46cf5;p=nextcloud-server.git fix: only allow pre-defined shards Signed-off-by: Robin Appelman --- diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index 447c164c1a4..3cb8ea17aed 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -92,6 +92,21 @@ class Connection extends PrimaryReadReplicaConnection { protected ShardConnectionManager $shardConnectionManager; protected AutoIncrementHandler $autoIncrementHandler; + public const SHARD_PRESETS = [ + 'filecache' => [ + 'companion_keys' => [ + 'file_id', + ], + 'companion_tables' => [ + 'filecache_extended', + 'files_metadata', + ], + 'primary_key' => 'fileid', + 'shard_key' => 'storage', + 'table' => 'filecache', + ], + ]; + /** * Initializes a new instance of the Connection class. * @@ -141,23 +156,30 @@ class Connection extends PrimaryReadReplicaConnection { $this->_config->setSQLLogger($debugStack); } - // todo: only allow specific, pre-defined shard configurations, the current config exists for easy testing setup - $this->shards = array_map(function (array $config) { + /** @var array $shardConfig */ + $shardConfig = $this->params['sharding'] ?? []; + $shardNames = array_keys($shardConfig); + $this->shards = array_map(function (array $config, string $name) { + if (!isset(self::SHARD_PRESETS[$name])) { + throw new \Exception("Shard preset $name not found"); + } + $shardMapperClass = $config['mapper'] ?? RoundRobinShardMapper::class; $shardMapper = Server::get($shardMapperClass); if (!$shardMapper instanceof IShardMapper) { throw new \Exception("Invalid shard mapper: $shardMapperClass"); } return new ShardDefinition( - $config['table'], - $config['primary_key'], - $config['companion_keys'], - $config['shard_key'], + self::SHARD_PRESETS[$name]['table'], + self::SHARD_PRESETS[$name]['primary_key'], + self::SHARD_PRESETS[$name]['companion_keys'], + self::SHARD_PRESETS[$name]['shard_key'], $shardMapper, - $config['companion_tables'], + self::SHARD_PRESETS[$name]['companion_tables'], $config['shards'] ); - }, $this->params['sharding']); + }, $shardConfig, $shardNames); + $this->shards = array_combine($shardNames, $this->shards); $this->partitions = array_map(function (ShardDefinition $shard) { return array_merge([$shard->table], $shard->companionTables); }, $this->shards);