Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"log/slog"
"math/rand"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -308,6 +308,11 @@ func (ch *clickhouse) dial(ctx context.Context) (conn nativeTransport, err error
}

func DefaultDialStrategy(ctx context.Context, connID int, opt *Options, dial Dial) (r DialResult, err error) {
var offset int
if opt.ConnOpenStrategy == ConnOpenRandom && len(opt.Addr) > 0 {
offset = rand.IntN(len(opt.Addr))
Comment thread
github-actions[bot] marked this conversation as resolved.
Resolved
}

for i := range opt.Addr {
var num int
switch opt.ConnOpenStrategy {
Expand All @@ -316,8 +321,7 @@ func DefaultDialStrategy(ctx context.Context, connID int, opt *Options, dial Dia
case ConnOpenRoundRobin:
num = (connID + i) % len(opt.Addr)
case ConnOpenRandom:
random := rand.Int()
num = (random + i) % len(opt.Addr)
num = (offset + i) % len(opt.Addr)
}

if r, err = dial(ctx, opt.Addr[num], opt); err == nil {
Expand Down
10 changes: 7 additions & 3 deletions clickhouse_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"io"
"log/slog"
"math/rand"
"math/rand/v2"
"net"
"reflect"
"sync/atomic"
Expand Down Expand Up @@ -59,6 +59,11 @@ func (o *stdConnOpener) Connect(ctx context.Context) (_ driver.Conn, err error)
return nil, ErrAcquireConnNoAddress
}

var offset int
if o.opt.ConnOpenStrategy == ConnOpenRandom {
offset = rand.IntN(len(o.opt.Addr))
}

for i := range o.opt.Addr {
var num int
switch o.opt.ConnOpenStrategy {
Expand All @@ -67,8 +72,7 @@ func (o *stdConnOpener) Connect(ctx context.Context) (_ driver.Conn, err error)
case ConnOpenRoundRobin:
num = (connID + i) % len(o.opt.Addr)
case ConnOpenRandom:
random := rand.Int()
num = (random + i) % len(o.opt.Addr)
num = (offset + i) % len(o.opt.Addr)
}
if conn, err = dialFunc(ctx, o.opt.Addr[num], connID, o.opt); err == nil {
// Create a logger with connection-specific context
Expand Down
4 changes: 0 additions & 4 deletions examples/clickhouse_api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
)

func TestMain(m *testing.M) {
ResetRandSeed()
fmt.Printf("using random seed %d for %s tests\n", randSeed, TestSet)

useDocker, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
Expand Down Expand Up @@ -169,7 +166,6 @@ func TestMultiHostConnect(t *testing.T) {
require.NoError(t, MultiHostRoundRobinVersion())
})
t.Run("Random", func(t *testing.T) {
t.Skip("Go 1.25 math/random changes")
require.NoError(t, MultiHostRandomVersion())
})
}
Expand Down
3 changes: 0 additions & 3 deletions examples/clickhouse_api/multi_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package clickhouse_api

import (
"fmt"
"math/rand"

"github.com/ClickHouse/clickhouse-go/v2"
)
Expand All @@ -17,8 +16,6 @@ func MultiHostRoundRobinVersion() error {
}

func MultiHostRandomVersion() error {
rand.Seed(85206178671753424)
defer ResetRandSeed()
connOpenStrategy := clickhouse.ConnOpenRandom
return multiHostVersion(&connOpenStrategy)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/clickhouse_api/qbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package clickhouse_api
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"

"github.com/ClickHouse/clickhouse-go/v2"
)
Expand Down
8 changes: 0 additions & 8 deletions examples/clickhouse_api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package clickhouse_api

import (
"crypto/tls"
"math/rand"
"time"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
Expand Down Expand Up @@ -31,9 +29,3 @@ func GetNativeConnectionWithOptions(settings clickhouse.Settings, tlsConfig *tls
func CheckMinServerVersion(conn driver.Conn, major, minor, patch uint64) bool {
return clickhouse_tests.CheckMinServerServerVersion(conn, major, minor, patch)
}

var randSeed = time.Now().UnixNano()

func ResetRandSeed() {
rand.Seed(randSeed)
}
5 changes: 0 additions & 5 deletions examples/std/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ package std
import (
"context"
"fmt"
"math/rand"
"os"
"strconv"
"testing"
"time"

clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
seed := time.Now().UnixNano()
fmt.Printf("using random seed %d for %s tests\n", seed, TestSet)
rand.Seed(seed)
useDocker, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/std/qbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"math/rand"
"math/rand/v2"

"github.com/ClickHouse/clickhouse-go/v2"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/bfloat16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"database/sql"
"fmt"
"math"
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"
Expand Down
11 changes: 3 additions & 8 deletions tests/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"math/rand"
"math/rand/v2"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -226,11 +225,9 @@ func TestBoolFlush(t *testing.T) {
batch, err := conn.PrepareBatch(ctx, "INSERT INTO bool_flush")
require.NoError(t, err)
vals := [1000]bool{}
var src = rand.NewSource(time.Now().UnixNano())
var r = rand.New(src)

for i := 0; i < 1000; i++ {
vals[i] = r.Intn(2) != 0
vals[i] = rand.IntN(2) != 0
require.NoError(t, batch.Append(vals[i]))
require.Equal(t, 1, batch.Rows())
require.NoError(t, batch.Flush())
Expand Down Expand Up @@ -286,11 +283,9 @@ func TestBoolValuer(t *testing.T) {
batch, err := conn.PrepareBatch(ctx, "INSERT INTO bool_flush")
require.NoError(t, err)
vals := [1000]bool{}
var src = rand.NewSource(time.Now().UnixNano())
var r = rand.New(src)

for i := 0; i < 1000; i++ {
vals[i] = r.Intn(2) != 0
vals[i] = rand.IntN(2) != 0
require.NoError(t, batch.Append(testBoolSerializer{val: vals[i]}))
}
batch.Send()
Expand Down
7 changes: 0 additions & 7 deletions tests/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,11 @@ func TestConnFailoverRoundRobin(t *testing.T) {
}

func TestConnFailoverRandom(t *testing.T) {
t.Skip("Go 1.25 math/random changes")
//rand.Seed(85206178671753423)
//defer ResetRandSeed()
testConnFailover(t, clickhouse.ConnOpenRandom)
}

func testConnFailover(t *testing.T, connOpenStrategy clickhouse.ConnOpenStrategy) {
TestProtocols(t, func(t *testing.T, protocol clickhouse.Protocol) {
if connOpenStrategy == clickhouse.ConnOpenRandom {
SkipOnHTTP(t, protocol, "random seed")
}

env, err := GetNativeTestEnvironment()
require.NoError(t, err)
useSSL, err := strconv.ParseBool(GetEnv("CLICKHOUSE_USE_SSL", "false"))
Expand Down
2 changes: 1 addition & 1 deletion tests/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion tests/geo_multi_linestring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql/driver"
"fmt"
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion tests/geo_multipolygon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql/driver"
"fmt"
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion tests/geo_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql/driver"
"fmt"
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion tests/geo_polygon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql/driver"
"fmt"
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/require"
Expand Down
10 changes: 4 additions & 6 deletions tests/issues/1271_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package issues
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -91,15 +91,13 @@ func RandAsciiString(n int) string {
return randString(n, letterBytes)
}

var src = rand.NewSource(time.Now().UnixNano())

func randString(n int, bytes string) string {
sb := strings.Builder{}
sb.Grow(n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
// A rand.Int64() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, rand.Int64(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
cache, remain = rand.Int64(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(bytes) {
sb.WriteByte(bytes[idx])
Expand Down
2 changes: 1 addition & 1 deletion tests/issues/741_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package issues
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"net"
"strconv"
"strings"
Expand Down
Loading
Loading