Skip to content

Conversation

@blizzz
Copy link
Member

@blizzz blizzz commented Aug 14, 2025

Summary

We save that looking up DN by user/group ID is taking up to 5% of all database queries. Albeit they are simple and do not transfer much data, it is still worthwhile to reduce them, since this data barely ever changes.

Checklist

@blizzz blizzz added this to the Nextcloud 32 milestone Aug 14, 2025
@blizzz blizzz requested a review from a team as a code owner August 14, 2025 16:56
@blizzz blizzz added the 2. developing Work in progress label Aug 14, 2025
@blizzz blizzz requested review from Altahrim and removed request for a team August 14, 2025 16:56
@blizzz blizzz requested review from come-nc and yemkareems August 14, 2025 16:56
@blizzz blizzz marked this pull request as draft August 14, 2025 16:57
@blizzz blizzz force-pushed the feat/noid/cache-ldap-mapping branch 3 times, most recently from 29d5976 to 4e03364 Compare August 15, 2025 16:44
Copy link
Contributor

@come-nc come-nc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to be a bit scared about this kind of things, we cache stuff from LDAP to DB to avoid too many LDAP queries, and then we cache stuff from DB to Cache to avoid DB queries.

Why local cache and not distributed? Is it because the cache is local that there is the FIXME about broadcasting?

Why the limit to 2000 by default, what is the risk of putting too much lines in Cache? 1MiB does not sound like that much.

/** @var array caches Names (value) by DN (key) */
protected $cache = [];
protected function initLocalCache(): void {
if ($this->isCLI || !$this->cacheFactory->isLocalCacheAvailable()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not in cli?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For APCu does not have a shared cache. There is no benefit over the runtime cache that was already present.

return;
}

$section = \str_contains($this->getTableName(), 'user') ? 'u/' : 'g/';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$section = \str_contains($this->getTableName(), 'user') ? 'u/' : 'g/';
$section = $this->getTableName();

Why make it more complicated than this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make it more complicated than this?

It is shorter and thus may save allocation of bytes in APCU.

@skjnldsv skjnldsv modified the milestones: Nextcloud 32, Nextcloud 33 Sep 28, 2025
@blizzz
Copy link
Member Author

blizzz commented Oct 15, 2025

I tend to be a bit scared about this kind of things, we cache stuff from LDAP to DB to avoid too many LDAP queries, and then we cache stuff from DB to Cache to avoid DB queries.

It showed that 5% of our queries are just those lookups.

UPDATE looking at current data it is 7% of all queries that are SELECT ldap_dn FROM oc_ldap_user_mapping WHERE owncloud_name = ? – and primarly during business hours.

Why local cache and not distributed? Is it because the cache is local that there is the FIXME about broadcasting?

Local cache is faster, and does not require any networking. There is also no global state here, but data that barely changes (and for that we have logic already).

Why the limit to 2000 by default, what is the risk of putting too much lines in Cache? 1MiB does not sound like that much.

When APCu overflows, then the entire cache is flushed, which then is quite expensive across the whole instance.

@blizzz blizzz force-pushed the feat/noid/cache-ldap-mapping branch from 4e03364 to 7d99e6e Compare October 15, 2025 13:01
@blizzz
Copy link
Member Author

blizzz commented Oct 15, 2025

/backport to stable32

@blizzz blizzz force-pushed the feat/noid/cache-ldap-mapping branch from 7d99e6e to 8fc543b Compare October 15, 2025 15:57
@blizzz blizzz added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Oct 15, 2025
@blizzz blizzz marked this pull request as ready for review October 15, 2025 15:57
@blizzz

This comment was marked as resolved.

@blizzz blizzz force-pushed the feat/noid/cache-ldap-mapping branch from 8fc543b to d91ce32 Compare October 15, 2025 16:45
@blizzz blizzz requested a review from CarlSchwan October 15, 2025 16:51
@blizzz blizzz mentioned this pull request Oct 16, 2025
Copy link
Contributor

@artonge artonge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me besides nitpick

Does it mean that admin would need to flush the cache if they change one user's display name? If so, we'll need to update the documentation.

return;
}

$useLocalCache = $this->config->getValueString('user_ldap', 'use_local_mapping_cache', 'auto', false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Register it in the Lexicon?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll check. I saw lexicon flying by, but don't really know what it does yet.

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a first glance, I suppose Lexicon support would be a follow-PR as we have other switches as well.

@blizzz
Copy link
Member Author

blizzz commented Oct 16, 2025

Does it mean that admin would need to flush the cache if they change one user's display name? If so, we'll need to update the documentation.

No, this is unrelated to the display name. It only stores DNs to a userid, or false if there is none (e.g. local users are thrown against this through the user abstraction layer on userExists calls).

@blizzz blizzz removed the 3. to review Waiting for reviews label Oct 16, 2025
@blizzz blizzz added the 2. developing Work in progress label Oct 16, 2025
@blizzz

This comment was marked as resolved.

@blizzz blizzz force-pushed the feat/noid/cache-ldap-mapping branch 2 times, most recently from b3483f0 to 81e7d06 Compare October 16, 2025 11:45
@blizzz

This comment was marked as resolved.

- has the advantage that queries will be reported in the query.log when
  configured

Signed-off-by: Arthur Schiwon <[email protected]>
@blizzz blizzz force-pushed the feat/noid/cache-ldap-mapping branch from 81e7d06 to 49f1c3f Compare October 17, 2025 09:09
@blizzz
Copy link
Member Author

blizzz commented Oct 17, 2025

I think I can simulate a case where

* the local cache is initilized with false

* the dn has changed

that fails. It is a bit constructed. And actually, I can reproduce with plain master.

But I don't like the way it fails and getting out of this scenario might not be inconvenient.

Solved with https://github.com/nextcloud/server/pull/54429/files#diff-607694866b0eede88107508c374cd8cb64bd1518dda2bd98a6dae3dae25c4e27R73-R75

@blizzz blizzz added 3. to review Waiting for reviews 4. to release Ready to be released and/or waiting for tests to finish and removed 2. developing Work in progress 3. to review Waiting for reviews labels Oct 17, 2025
@blizzz blizzz mentioned this pull request Oct 17, 2025
11 tasks
@blizzz blizzz merged commit 9d63530 into master Oct 17, 2025
209 of 219 checks passed
@blizzz blizzz deleted the feat/noid/cache-ldap-mapping branch October 17, 2025 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4. to release Ready to be released and/or waiting for tests to finish performance 🚀

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants