Skip to content

chore(snowflake): add more randomness in server id fallback#60612

Merged
Altahrim merged 1 commit into
masterfrom
chore/server_id_entropy
May 21, 2026
Merged

chore(snowflake): add more randomness in server id fallback#60612
Altahrim merged 1 commit into
masterfrom
chore/server_id_entropy

Conversation

@Altahrim
Copy link
Copy Markdown
Collaborator

@Altahrim Altahrim commented May 20, 2026

Summary

CRC32 hash function can easily lead to collisions when based on hostname (and even using random strings).

Here is an example which generates a lot of duplicated server IDs from different fake hostnames:

for ($i = 1; $i <= 20; ++$i) {
	$a=sprintf('prod-%02d.nextcloud.com', $i); 
	printf("%s xxh: %3d crc:%3d\n", $a, hexdec(hash('xxh32', $a)) % 512, crc32($a) %512); 
}
prod-01.nextcloud.com xxh: 182 crc:265
prod-02.nextcloud.com xxh: 280 crc:368
prod-03.nextcloud.com xxh: 116 crc:152
prod-04.nextcloud.com xxh: 348 crc:386
prod-05.nextcloud.com xxh: 171 crc:106
prod-06.nextcloud.com xxh: 452 crc: 19
prod-07.nextcloud.com xxh:  71 crc:507
prod-08.nextcloud.com xxh: 292 crc:102
prod-09.nextcloud.com xxh: 363 crc:398
prod-10.nextcloud.com xxh:  68 crc:368
prod-11.nextcloud.com xxh: 406 crc:152
prod-12.nextcloud.com xxh: 213 crc:225
prod-13.nextcloud.com xxh: 179 crc:265
prod-14.nextcloud.com xxh:  64 crc: 19
prod-15.nextcloud.com xxh: 210 crc:507
prod-16.nextcloud.com xxh:  47 crc:386
prod-17.nextcloud.com xxh: 175 crc:106
prod-18.nextcloud.com xxh: 383 crc:503
prod-19.nextcloud.com xxh: 222 crc: 31
prod-20.nextcloud.com xxh: 274 crc:386

With $a=bin2hex(random_bytes(10)); I also spotted one collision with CRC32.

Even if it's only used as a fallback, XXH32 sounds like a better alternative.

Performance
XXH32 should be faster but we need to convert to int:

Summary
  php -r 'for ($a=0;$a<10000;++$a) { crc32($a); }' ran
    1.07 ± 0.15 times faster than php -r 'for ($a=0;$a<10000;++$a) { echo hexdec(hash("xxh32", $a)); }'
    1.11 ± 0.17 times faster than php -r 'for ($a=0;$a<10000;++$a) { echo bindec(hash("xxh32", $a, true)); }'

It's quite close :)

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@Altahrim Altahrim added this to the Nextcloud 35 milestone May 20, 2026
@Altahrim Altahrim self-assigned this May 20, 2026
@Altahrim Altahrim added enhancement 2. developing Work in progress php Pull requests that update Php code labels May 20, 2026
@Altahrim
Copy link
Copy Markdown
Collaborator Author

/backport to stable34

@Altahrim
Copy link
Copy Markdown
Collaborator Author

/backport to stable33

@Altahrim Altahrim added 3. to review Waiting for reviews and removed 2. developing Work in progress labels May 20, 2026
@Altahrim Altahrim marked this pull request as ready for review May 20, 2026 13:27
@Altahrim Altahrim requested a review from a team as a code owner May 20, 2026 13:27
@Altahrim Altahrim requested review from ArtificialOwl, artonge, leftybournes and salmart-dev and removed request for a team May 20, 2026 13:27
@Altahrim Altahrim force-pushed the chore/server_id_entropy branch from 2f4a47d to 37385db Compare May 20, 2026 15:17
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
@Altahrim Altahrim force-pushed the chore/server_id_entropy branch from 37385db to eecbe37 Compare May 21, 2026 07:18
$serverid = hexdec(hash('xxh32', gethostname() ?: random_bytes(8)));
}

/** @var int<0,511> */
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Required as Psalm doesn't compute it correctly:

@Altahrim Altahrim enabled auto-merge May 21, 2026 07:22
@Altahrim Altahrim disabled auto-merge May 21, 2026 08:15
@Altahrim Altahrim merged commit bb63987 into master May 21, 2026
176 of 182 checks passed
@Altahrim Altahrim deleted the chore/server_id_entropy branch May 21, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews enhancement php Pull requests that update Php code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants