Skip to content

Upgrade from math/rand to math/rand/v2#1788

Open
Onyx2406 wants to merge 1 commit intoClickHouse:mainfrom
Onyx2406:fix/upgrade-math-rand-v2
Open

Upgrade from math/rand to math/rand/v2#1788
Onyx2406 wants to merge 1 commit intoClickHouse:mainfrom
Onyx2406:fix/upgrade-math-rand-v2

Conversation

@Onyx2406
Copy link

@Onyx2406 Onyx2406 commented Mar 7, 2026

Summary

  • Migrate all math/rand usages to math/rand/v2 across the entire codebase (19 files)
  • Fix ConnOpenRandom strategy to pick random offset once outside the loop
  • Re-enable skipped random failover tests

Motivation

Go 1.25 broke rand.Seed which caused ConnOpenRandom failover tests to be skipped (#1652). The math/rand package is deprecated in favor of math/rand/v2 which provides automatic seeding and better APIs.

As discussed in #1653, the ConnOpenRandom strategy also had a bug where rand.Int() was called on every loop iteration, meaning a connection attempt could pick the same (broken) address multiple times and fail to try all addresses.

Changes

Core library (clickhouse.go, clickhouse_std.go):

  • Replace math/rand with math/rand/v2
  • Fix ConnOpenRandom: pick random starting offset once via rand.IntN(len(addrs)) before the loop, then iterate through all addresses deterministically from that offset. This ensures every address is tried exactly once before failing.

Tests (11 files):

  • Replace rand.NewSource/rand.New with rand.NewPCG/rand.New
  • Replace rand.Intn(n) with rand.IntN(n) (v2 naming)
  • Replace src.Int63() with rng.Int64() in string generators
  • Remove rand.Seed calls (v2 auto-seeds)
  • Re-enable TestConnFailoverRandom and TestStdConnFailoverRandom

Examples (5 files):

  • Remove rand.Seed calls from multi-host and test setup
  • Update ResetRandSeed() to no-op (v2 has no rand.Seed)
  • Remove unused imports (time, math/rand)

Test Plan

  • go build ./... passes cleanly
  • No remaining math/rand v1 imports (grep confirms zero matches)
  • TestConnFailoverRandom and TestStdConnFailoverRandom re-enabled (previously skipped)
  • CI integration tests (require ClickHouse Docker container)

Fixes #1653

Migrate all usages of the deprecated math/rand package to math/rand/v2
across the codebase. This fixes the Go 1.25 compatibility issue where
rand.Seed no longer works (ClickHouse#1652).

Key changes:
- Replace math/rand imports with math/rand/v2 in all 19 affected files
- Use rand.IntN instead of rand.Intn (v2 naming convention)
- Replace rand.NewSource with rand.NewPCG for local random generators
- Remove all rand.Seed calls (v2 auto-seeds the global source)
- Fix ConnOpenRandom strategy: pick random offset once outside the
  loop instead of on every iteration, ensuring all addresses are
  tried before failing over
- Re-enable TestConnFailoverRandom and TestStdConnFailoverRandom
  tests that were skipped due to Go 1.25 math/random changes

Fixes ClickHouse#1653
@CLAassistant
Copy link

CLAassistant commented Mar 7, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upgrade to math/rand/v2

2 participants