diff --git a/lib/private/AppConfig.php b/lib/private/AppConfig.php index 2280ac1a79fb7..b8c8c2173f161 100644 --- a/lib/private/AppConfig.php +++ b/lib/private/AppConfig.php @@ -1027,7 +1027,7 @@ public function updateLazy(string $app, string $key, bool $lazy): bool { if ($lazy === $this->isLazy($app, $key)) { return false; } - } catch (AppConfigUnknownKeyException $e) { + } catch (AppConfigUnknownKeyException) { return false; } @@ -1724,6 +1724,12 @@ private function matchAndApplyLexiconDefinition( $this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.'); } + if ($lazy && isset($this->fastCache[$app][$key])) { + // while the Lexicon indicate that the config value is expected Lazy, we could + // have a previous entry still in fast cache. Updating Laziness. + $this->updateLazy($app, $key, true); + } + return true; } diff --git a/lib/private/Config/UserConfig.php b/lib/private/Config/UserConfig.php index 04ba0e29db0d2..eea96ef9d6e09 100644 --- a/lib/private/Config/UserConfig.php +++ b/lib/private/Config/UserConfig.php @@ -1929,6 +1929,19 @@ private function matchAndApplyLexiconDefinition( $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.'); } + // There should be no downside to load all config values if search for + // a lazy config value while fast value are still not loaded. + if ($lazy && !($this->fastLoaded[$userId] ?? false)) { + $this->loadConfigAll($userId); + } + + // while the Lexicon indicate that the config value is expected Lazy, we could + // have a previous entry still in fast cache. Updating Laziness for all users. + if ($lazy && isset($this->fastCache[$userId][$app][$key])) { + $this->updateGlobalLazy($app, $key, true); + } + + // TODO: remove this feature before 32 if https://github.com/nextcloud/server/issues/51804 is implemented $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false; if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) { // if key exists there should be no need to extract default diff --git a/tests/lib/Config/LexiconTest.php b/tests/lib/Config/LexiconTest.php index def9e152853da..838b25e49b0fc 100644 --- a/tests/lib/Config/LexiconTest.php +++ b/tests/lib/Config/LexiconTest.php @@ -10,6 +10,7 @@ use OC\AppConfig; use OC\AppFramework\Bootstrap\Coordinator; use OC\Config\ConfigManager; +use OC\Config\UserConfig; use OCP\Config\Exceptions\TypeConflictException; use OCP\Config\Exceptions\UnknownKeyException; use OCP\Config\IUserConfig; @@ -17,7 +18,11 @@ use OCP\Exceptions\AppConfigTypeConflictException; use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Security\ICrypto; use OCP\Server; +use Psr\Log\LoggerInterface; use Test\TestCase; /** @@ -68,6 +73,81 @@ public function testAppLexiconSetCorrect() { $this->appConfig->deleteKey(TestLexicon_E::APPID, 'key1'); } + public function testAppConfigMigrationToLazy() { + $app = TestConfigLexicon_Migration::APPID . '_app'; + // to avoid filling cache with an empty Lexicon, we use a new IAppConfig + $appConfig = new AppConfig( + Server::get(IDBConnection::class), + Server::get(IConfig::class), + Server::get(LoggerInterface::class), + Server::get(ICrypto::class), + ); + $this->assertSame(true, $appConfig->setValueString($app, 'key1', 'value1')); + + // confirm that key1 is not lazy, even after a refresh of the cache + $appConfig->clearCache(); + $this->assertSame('value1', $appConfig->getValueString($app, 'key1')); + $this->assertSame(true, array_key_exists('key1', $appConfig->statusCache()['fastCache'][$app])); + + // loading new Lexicon that set key1 as lazy + $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon($app, TestConfigLexicon_Migration::class); + + // caching + $this->assertSame('default0', $this->appConfig->getValueString($app, 'key0')); + // still not lazy + $this->assertSame(true, array_key_exists('key1', $this->appConfig->statusCache()['fastCache'][$app])); + // trigger update of status + $this->assertSame('value1', $this->appConfig->getValueString($app, 'key1')); + // should be lazy now + $this->assertSame(true, array_key_exists('key1', $this->appConfig->statusCache()['lazyCache'][$app])); + + $this->appConfig->clearCache(); + $this->assertSame('default0', $this->appConfig->getValueString($app, 'key0')); + // definitively lazy + $this->assertSame(false, array_key_exists($app, $this->appConfig->statusCache()['fastCache'])); + + $this->appConfig->deleteKey($app, 'key1'); + } + + public function testUserConfigMigrationToLazy() { + $app = TestConfigLexicon_Migration::APPID . '_user'; + // to avoid filling cache with an empty Lexicon, we use a new IAppConfig + $userConfig = new UserConfig( + Server::get(IDBConnection::class), + Server::get(IConfig::class), + Server::get(LoggerInterface::class), + Server::get(ICrypto::class), + ); + $this->assertSame(true, $userConfig->setValueString('user1', $app, 'key1', 'value1')); + + // confirm that key1 is not lazy, even after a refresh of Lexicon + $userConfig->clearCache('user1'); + $this->assertSame('value1', $userConfig->getValueString('user1', $app, 'key1')); + $this->assertSame(true, array_key_exists('key1', $userConfig->statusCache()['fastCache']['user1'][$app])); + + // loading new Lexicon that set key1 as lazy + $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon($app, TestConfigLexicon_Migration::class); + + // caching + $this->assertSame('default0', $this->userConfig->getValueString('user1', $app, 'key0')); + // still not lazy + $this->assertSame(true, array_key_exists('key1', $this->userConfig->statusCache()['fastCache']['user1'][$app])); + // trigger update of status + $this->assertSame('value1', $this->userConfig->getValueString('user1', $app, 'key1')); + // should be lazy now + $this->assertSame(true, array_key_exists('key1', $this->userConfig->statusCache()['lazyCache']['user1'][$app])); + + $this->userConfig->clearCache('user1'); + $this->assertSame('default0', $this->userConfig->getValueString('user1', $app, 'key0')); + // definitively lazy + $this->assertSame(false, array_key_exists($app, $this->userConfig->statusCache()['fastCache']['user1'])); + + $this->userConfig->deleteKey($app, 'key1'); + } + + public function testAppLexiconGetCorrect() { $this->assertSame('abcde', $this->appConfig->getValueString(TestLexicon_E::APPID, 'key1', 'default')); } diff --git a/tests/lib/Config/TestConfigLexicon_Migration.php b/tests/lib/Config/TestConfigLexicon_Migration.php new file mode 100644 index 0000000000000..dfd34bd70d134 --- /dev/null +++ b/tests/lib/Config/TestConfigLexicon_Migration.php @@ -0,0 +1,36 @@ +