Repository navigation
chore: deprecate withasync helpers
#2096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6bec1cb
chore: explain the README for rationale of deprecation
kavirajk 816c649
chore: update the examples to use plain settings
kavirajk 409ae25
chore: update the benchmarks to use plain settings
kavirajk eb974dd
chore: update the docs to reflect the recommendation
kavirajk 043352b
chore: mark the original api with Deprecated marker
kavirajk 08fa0f4
chore: minor doc change with tests
kavirajk c4d2f0f
chore: doc fixes
kavirajk 64986bb
Merge branch 'main' into kavirajk/deprecate-with-async-helper
kavirajk e4bbef2
chore: agents docs update
kavirajk fd9c560
chore: add additional test for async helper
kavirajk 89e0039
Merge branch 'main' into kavirajk/deprecate-with-async-helper
kavirajk 96f021c
Merge branch 'main' into kavirajk/deprecate-with-async-helper
kavirajk db933f0
Merge branch 'main' into kavirajk/deprecate-with-async-helper
kavirajk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -235,50 +235,40 @@ Finally, note the ability to pass a `Context` to the `Query` and `QueryRow` meth | |||||
|
|
||||||
| ## Async insert {#async-insert} | ||||||
|
|
||||||
| Asynchronous inserts are supported through the Async method. This allows the user to specify whether the client should wait for the server to complete the insert or respond once the data has been received. This effectively controls the parameter [wait_for_async_insert](/reference/settings/session-settings#wait_for_async_insert). | ||||||
| With [async insert](https://clickhouse.com/docs/concepts/features/operations/insert/asyncinserts), the server keeps the data of many small inserts in a buffer and writes them together. Two server settings control it: | ||||||
|
|
||||||
| * [`async_insert`](/reference/settings/session-settings#async_insert): `1` enables async insert. `0` disables it. | ||||||
| * [`wait_for_async_insert`](/reference/settings/session-settings#wait_for_async_insert): `1` makes the server send the response after it writes the data. `0` makes the server send the response when it receives the data. | ||||||
|
|
||||||
| Set these settings with `WithSettings`. They apply to `Exec` and to batches from `PrepareBatch`, on the Native and HTTP protocols. | ||||||
|
|
||||||
| ```go | ||||||
| conn, err := GetNativeConnection(nil, nil, nil) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| ctx := context.Background() | ||||||
| if err := clickhouse_tests.CheckMinServerServerVersion(conn, 21, 12, 0); err != nil { | ||||||
| return nil | ||||||
| } | ||||||
| defer func() { | ||||||
| conn.Exec(ctx, "DROP TABLE example") | ||||||
| }() | ||||||
| conn.Exec(ctx, `DROP TABLE IF EXISTS example`) | ||||||
| const ddl = ` | ||||||
| CREATE TABLE example ( | ||||||
| Col1 UInt64 | ||||||
| , Col2 String | ||||||
| , Col3 Array(UInt8) | ||||||
| , Col4 DateTime | ||||||
| ) ENGINE = Memory | ||||||
| ` | ||||||
| if err := conn.Exec(ctx, ddl); err != nil { | ||||||
| return err | ||||||
| } | ||||||
| ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{ | ||||||
| "async_insert": 1, | ||||||
| "wait_for_async_insert": 1, | ||||||
| })) | ||||||
| for i := 0; i < 100; i++ { | ||||||
| if err := conn.AsyncInsert(ctx, fmt.Sprintf(`INSERT INTO example VALUES ( | ||||||
| %d, '%s', [1, 2, 3, 4, 5, 6, 7, 8, 9], now() | ||||||
| )`, i, "Golang SQL database driver"), false); err != nil { | ||||||
| if err := conn.Exec(ctx, `INSERT INTO example VALUES (?, ?, [1, 2, 3, 4, 5, 6, 7, 8, 9], now())`, | ||||||
| i, "Golang SQL database driver"); err != nil { | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| [Full Example](https://github.com/ClickHouse/clickhouse-go/blob/main/examples/clickhouse_api/async_native.go) | ||||||
|
|
||||||
| If `wait_for_async_insert` is `0`, the insert is fast, but the client does not get insert errors. Look for errors in `system.asynchronous_insert_log`. | ||||||
|
|
||||||
| ### Async insert with a batch {#async-insert-batch} | ||||||
|
|
||||||
| `WithAsync` also applies to batch inserts on the Native and HTTP protocols. A batch uses only the context that you give to `PrepareBatch`. `Send` sends the batch with the settings from that context. | ||||||
| A batch uses only the context that you give to `PrepareBatch`. `Send` sends the batch with the settings from that context. | ||||||
|
|
||||||
| ```go | ||||||
| ctx := clickhouse.Context(context.Background(), clickhouse.WithAsync(true)) | ||||||
| batch, err := conn.PrepareBatch(ctx, "INSERT INTO example") // WithAsync must be on this context | ||||||
| ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{ | ||||||
| "async_insert": 1, | ||||||
| "wait_for_async_insert": 1, | ||||||
| })) | ||||||
| batch, err := conn.PrepareBatch(ctx, "INSERT INTO example") // the settings must be on this context | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
@@ -292,6 +282,57 @@ return batch.Send() | |||||
|
|
||||||
| [Full Example](https://github.com/ClickHouse/clickhouse-go/blob/main/examples/clickhouse_api/async_native.go) | ||||||
|
|
||||||
| ### Async insert is the default on ClickHouse 26.2 and later {#async-insert-default} | ||||||
|
|
||||||
| ClickHouse 26.2 and later set `async_insert=1` and `wait_for_async_insert=1` by default. Thus the server does not write an `INSERT` immediately. It keeps the data in a buffer for at least `async_insert_busy_timeout_min_ms` (50 ms by default). If inserts arrive frequently, the server increases this time up to `async_insert_busy_timeout_max_ms` (200 ms by default). Then it writes the buffer and sends the response. | ||||||
|
|
||||||
| With a low insert rate, each `INSERT` takes 50 ms or more. This occurs on the Native and HTTP protocols and with all clients. The delay occurs one time for each `INSERT`, not for each row. | ||||||
|
|
||||||
| If you send one row in each `Exec` call, one goroutine can do only approximately 5 to 20 inserts each second. To prevent this, do one of these steps: | ||||||
|
|
||||||
| * Send many rows in one `INSERT` with `PrepareBatch`. | ||||||
| * Disable async insert for the query: | ||||||
|
|
||||||
| ```go | ||||||
| ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{ | ||||||
| "async_insert": 0, | ||||||
| })) | ||||||
| if err := conn.Exec(ctx, "INSERT INTO example VALUES (1)"); err != nil { | ||||||
| return err | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| To disable async insert for all queries, set `"async_insert": 0` in `Options.Settings`. | ||||||
| * Set `wait_for_async_insert` to `0`. The insert is fast, but the client does not get insert errors. | ||||||
|
|
||||||
| ### `WithAsync` and `AsyncInsert` are deprecated {#async-insert-deprecated} | ||||||
|
|
||||||
| `WithAsync`, `WithStdAsync` and `Conn.AsyncInsert` are deprecated. They will be removed in the next major version. They only set the two settings above, and `WithAsync(false)` does not disable async insert. It sets `async_insert=1` and `wait_for_async_insert=0`, so the client does not get insert errors. | ||||||
|
|
||||||
| | Deprecated | Replacement | | ||||||
| |---|---| | ||||||
| | `WithAsync(true)` | `WithSettings(clickhouse.Settings{"async_insert": 1, "wait_for_async_insert": 1})`. On ClickHouse 26.2 and later with the default server settings, you can remove it. | | ||||||
| | `WithAsync(false)`, `WithStdAsync(false)` | `WithSettings(clickhouse.Settings{"async_insert": 1, "wait_for_async_insert": 0})` | | ||||||
| | `conn.AsyncInsert(ctx, query, wait)` | `conn.Exec(ctx, query)` with the settings above on `ctx` | | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Nit — nit: migration table omits The table does not show two forms that a caller can have, so the caller must guess the replacement.
Suggested change
|
||||||
| | No replacement before: disable async insert | `WithSettings(clickhouse.Settings{"async_insert": 0})` | | ||||||
|
|
||||||
| `WithSettings` replaces the settings of an earlier `WithSettings`. It does not merge them. If you already use `WithSettings`, add the async insert settings to the same `Settings` map. If you use two `WithSettings` options, the server does not get the settings of the first one: | ||||||
|
|
||||||
| ```go | ||||||
| // Incorrect: the second WithSettings removes async_insert and wait_for_async_insert. | ||||||
| ctx = clickhouse.Context(ctx, | ||||||
| clickhouse.WithSettings(clickhouse.Settings{"async_insert": 1, "wait_for_async_insert": 1}), | ||||||
| clickhouse.WithSettings(clickhouse.Settings{"log_comment": "import"}), | ||||||
| ) | ||||||
|
|
||||||
| // Correct: all settings are in one map. | ||||||
| ctx = clickhouse.Context(ctx, clickhouse.WithSettings(clickhouse.Settings{ | ||||||
| "async_insert": 1, | ||||||
| "wait_for_async_insert": 1, | ||||||
| "log_comment": "import", | ||||||
| })) | ||||||
| ``` | ||||||
|
|
||||||
| ## Columnar insert {#columnar-insert} | ||||||
|
|
||||||
| Inserts can be inserted in column format. This can provide performance benefits if the data is already orientated in this structure by avoiding the need to pivot to rows. | ||||||
|
|
||||||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.