From a15b286228d763a8c4773d0e88795cdd136f24c2 Mon Sep 17 00:00:00 2001 From: rafiramadhana Date: Sun, 5 Nov 2023 00:25:46 +0700 Subject: [PATCH 1/4] Add retry Add retry using performCRUDWithRetry. --- modules/clickhouse/clickhouse_test.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/clickhouse/clickhouse_test.go b/modules/clickhouse/clickhouse_test.go index bd505689dc..ae32dabf0a 100644 --- a/modules/clickhouse/clickhouse_test.go +++ b/modules/clickhouse/clickhouse_test.go @@ -4,11 +4,11 @@ import ( "context" "path/filepath" "testing" + "time" ch "github.com/ClickHouse/clickhouse-go/v2" "github.com/ClickHouse/clickhouse-go/v2/lib/driver" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" ) @@ -89,7 +89,7 @@ func TestClickHouseConnectionHost(t *testing.T) { defer conn.Close() // perform assertions - data, err := performCRUD(conn) + data, err := performCRUDWithRetry(conn) assert.NoError(t, err) assert.Len(t, data, 1) } @@ -121,7 +121,7 @@ func TestClickHouseDSN(t *testing.T) { defer conn.Close() // perform assertions - data, err := performCRUD(conn) + data, err := performCRUDWithRetry(conn) assert.NoError(t, err) assert.Len(t, data, 1) } @@ -210,13 +210,29 @@ func TestClickHouseWithConfigFile(t *testing.T) { defer conn.Close() // perform assertions - data, err := performCRUD(conn) + data, err := performCRUDWithRetry(conn) assert.NoError(t, err) assert.Len(t, data, 1) }) } } +func performCRUDWithRetry(conn driver.Conn) (tt []Test, err error) { + maxAttempt := 5 + delay := 100 * time.Millisecond + + for i := 0; i < maxAttempt; i++ { + time.Sleep(time.Duration(i) * delay) + + tt, err = performCRUD(conn) + if err == nil { + return tt, err + } + } + + return tt, err +} + func performCRUD(conn driver.Conn) ([]Test, error) { err := conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;") if err != nil { From 294725181bc186d4db57301f4fd0b2677b91e067 Mon Sep 17 00:00:00 2001 From: rafiramadhana Date: Fri, 10 Nov 2023 21:28:47 +0700 Subject: [PATCH 2/4] Merge performCRUD and performCRUDWithRetry --- modules/clickhouse/clickhouse_test.go | 51 +++++++++++++-------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/modules/clickhouse/clickhouse_test.go b/modules/clickhouse/clickhouse_test.go index ae32dabf0a..949c40265a 100644 --- a/modules/clickhouse/clickhouse_test.go +++ b/modules/clickhouse/clickhouse_test.go @@ -89,7 +89,7 @@ func TestClickHouseConnectionHost(t *testing.T) { defer conn.Close() // perform assertions - data, err := performCRUDWithRetry(conn) + data, err := performCRUD(conn) assert.NoError(t, err) assert.Len(t, data, 1) } @@ -121,7 +121,7 @@ func TestClickHouseDSN(t *testing.T) { defer conn.Close() // perform assertions - data, err := performCRUDWithRetry(conn) + data, err := performCRUD(conn) assert.NoError(t, err) assert.Len(t, data, 1) } @@ -210,46 +210,43 @@ func TestClickHouseWithConfigFile(t *testing.T) { defer conn.Close() // perform assertions - data, err := performCRUDWithRetry(conn) + data, err := performCRUD(conn) assert.NoError(t, err) assert.Len(t, data, 1) }) } } -func performCRUDWithRetry(conn driver.Conn) (tt []Test, err error) { - maxAttempt := 5 - delay := 100 * time.Millisecond +func performCRUD(conn driver.Conn) ([]Test, error) { + var ( + maxRetry = 5 + delay = 100 * time.Millisecond + err error + rows []Test + ) - for i := 0; i < maxAttempt; i++ { + for i := 0; i < maxRetry; i++ { time.Sleep(time.Duration(i) * delay) - tt, err = performCRUD(conn) - if err == nil { - return tt, err + err = conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;") + if err != nil { + continue } - } - - return tt, err -} -func performCRUD(conn driver.Conn) ([]Test, error) { - err := conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;") - if err != nil { - return nil, err - } + err = conn.Exec(context.Background(), "INSERT INTO test_table (id) VALUES (1);") + if err != nil { + continue + } - err = conn.Exec(context.Background(), "INSERT INTO test_table (id) VALUES (1);") - if err != nil { - return nil, err - } + rows, err = getAllRows(conn) + if err != nil { + continue + } - rows, err := getAllRows(conn) - if err != nil { - return nil, err + return rows, err } - return rows, nil + return rows, err } func getAllRows(conn driver.Conn) ([]Test, error) { From e3d6717e45a6e020424c08b9deb513a538333d84 Mon Sep 17 00:00:00 2001 From: rafiramadhana Date: Sun, 12 Nov 2023 17:10:42 +0700 Subject: [PATCH 3/4] Update retry Update retry to use backoff package. --- modules/clickhouse/clickhouse_test.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/modules/clickhouse/clickhouse_test.go b/modules/clickhouse/clickhouse_test.go index 949c40265a..e284cfd109 100644 --- a/modules/clickhouse/clickhouse_test.go +++ b/modules/clickhouse/clickhouse_test.go @@ -4,10 +4,10 @@ import ( "context" "path/filepath" "testing" - "time" ch "github.com/ClickHouse/clickhouse-go/v2" "github.com/ClickHouse/clickhouse-go/v2/lib/driver" + "github.com/cenkalti/backoff/v4" "github.com/stretchr/testify/assert" "github.com/testcontainers/testcontainers-go" ) @@ -219,32 +219,28 @@ func TestClickHouseWithConfigFile(t *testing.T) { func performCRUD(conn driver.Conn) ([]Test, error) { var ( - maxRetry = 5 - delay = 100 * time.Millisecond - err error - rows []Test + err error + rows []Test ) - for i := 0; i < maxRetry; i++ { - time.Sleep(time.Duration(i) * delay) - + err = backoff.Retry(func() error { err = conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;") if err != nil { - continue + return err } err = conn.Exec(context.Background(), "INSERT INTO test_table (id) VALUES (1);") if err != nil { - continue + return err } rows, err = getAllRows(conn) if err != nil { - continue + return err } - return rows, err - } + return nil + }, backoff.NewExponentialBackOff()) return rows, err } From a941bb212ddb46fe62aa127a1a46f6338c6df284 Mon Sep 17 00:00:00 2001 From: rafiramadhana Date: Mon, 13 Nov 2023 19:58:33 +0700 Subject: [PATCH 4/4] Run linter --- modules/clickhouse/clickhouse_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/clickhouse/clickhouse_test.go b/modules/clickhouse/clickhouse_test.go index e284cfd109..ea2670388b 100644 --- a/modules/clickhouse/clickhouse_test.go +++ b/modules/clickhouse/clickhouse_test.go @@ -9,6 +9,7 @@ import ( "github.com/ClickHouse/clickhouse-go/v2/lib/driver" "github.com/cenkalti/backoff/v4" "github.com/stretchr/testify/assert" + "github.com/testcontainers/testcontainers-go" )