-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry on broken pipe in batch (#1423)
* Release connections on error during Flush() * Add test to illustrate broken batch flushes * Use a dedicated test environment for broken connection test recover #1421 --------- Co-authored-by: Robert Gettys <[email protected]>
- Loading branch information
1 parent
269b0f3
commit 02efdee
Showing
3 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package issues | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver" | ||
"github.com/ClickHouse/clickhouse-go/v2/tests" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/stretchr/testify/require" | ||
"github.com/testcontainers/testcontainers-go" | ||
) | ||
|
||
//goland:noinspection ALL | ||
const insertQry = "INSERT INTO test (foo, foo2)" | ||
|
||
func Test1421BatchFlushBrokenConn(t *testing.T) { | ||
// create a dedicated test environment for this test | ||
// note: test environment management is a bit messy, consider refactoring | ||
env, err := tests.CreateClickHouseTestEnvironment(t.Name()) | ||
tests.SetTestEnvironment(t.Name(), env) | ||
require.NoError(t, tests.CreateDatabase(t.Name())) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, env) | ||
ctx := context.Background() | ||
client, err := testcontainers.NewDockerClientWithOpts(ctx) | ||
require.NoError(t, err) | ||
chClient, err := tests.TestClientWithDefaultSettings(env) | ||
|
||
err = chClient.Exec(ctx, "CREATE TABLE test (foo String, foo2 String) ENGINE = MergeTree ORDER BY (foo)") | ||
require.NoError(t, err) | ||
batch, err := chClient.PrepareBatch(ctx, insertQry, driver.WithCloseOnFlush()) | ||
require.NoError(t, err) | ||
err = batch.Append("bar", "bar") | ||
require.NoError(t, err) | ||
err = batch.Flush() | ||
require.NoError(t, err) | ||
err = batch.Append("bar2", "bar2") | ||
require.NoError(t, err) | ||
err = batch.Flush() | ||
require.NoError(t, err) | ||
|
||
err = batch.Append(RandAsciiString(200000000), RandAsciiString(20000000)) | ||
|
||
require.NoError(t, err) | ||
ch := make(chan struct{}) | ||
go func() { | ||
err = batch.Flush() | ||
close(ch) | ||
}() | ||
//timeout := 0 | ||
err2 := client.ContainerKill(ctx, env.ContainerID, "KILL") | ||
<-ch | ||
require.NoError(t, err2) | ||
require.True(t, errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET)) | ||
err = client.ContainerStart(ctx, env.ContainerID, container.StartOptions{}) | ||
require.NoError(t, err) | ||
err = batch.Flush() | ||
// retry after server is up should have either no error, or a reconnect error (for example because the mapped port | ||
// changed on container startup) | ||
require.True(t, err == nil || errors.Is(err, syscall.ECONNREFUSED) || os.IsTimeout(err), err) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters