-
Notifications
You must be signed in to change notification settings - Fork 27
chores: reduce amount of reconciles, cache CH connections between reconciles #245
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package clickhouse | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "maps" | ||
| "sync" | ||
|
|
||
| "github.com/ClickHouse/clickhouse-go/v2" | ||
| "k8s.io/apimachinery/pkg/types" | ||
|
|
||
| v1 "github.com/ClickHouse/clickhouse-operator/api/v1alpha1" | ||
| "github.com/ClickHouse/clickhouse-operator/internal/controllerutil" | ||
| ) | ||
|
|
||
| type connCache struct { | ||
| mu sync.Mutex | ||
| entries map[types.NamespacedName]*connCacheEntry | ||
| } | ||
|
|
||
| type connCacheEntry struct { | ||
| mu sync.Mutex | ||
| credHash string | ||
| conns map[v1.ClickHouseReplicaID]clickhouse.Conn | ||
| } | ||
|
|
||
| func newConnCache() *connCache { | ||
| return &connCache{entries: map[types.NamespacedName]*connCacheEntry{}} | ||
| } | ||
|
|
||
| func (c *connCache) Get(key types.NamespacedName, credHash string, log controllerutil.Logger) *connCacheEntry { | ||
| if c == nil { // no pool wired (tests) | ||
| return &connCacheEntry{credHash: credHash, conns: map[v1.ClickHouseReplicaID]clickhouse.Conn{}} | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| e, ok := c.entries[key] | ||
| if !ok { | ||
| e = &connCacheEntry{credHash: credHash, conns: map[v1.ClickHouseReplicaID]clickhouse.Conn{}} | ||
| c.entries[key] = e | ||
|
|
||
| return e | ||
| } | ||
|
|
||
| if e.credHash == credHash { | ||
| return e | ||
| } | ||
|
|
||
| e.Close(log) | ||
| e = &connCacheEntry{credHash: credHash, conns: map[v1.ClickHouseReplicaID]clickhouse.Conn{}} | ||
| c.entries[key] = e | ||
|
|
||
| return e | ||
| } | ||
|
|
||
| func (c *connCache) Evict(key types.NamespacedName, log controllerutil.Logger) { | ||
| if c == nil { | ||
| return | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| if e, ok := c.entries[key]; ok { | ||
| e.Close(log) | ||
| delete(c.entries, key) | ||
| } | ||
| } | ||
|
|
||
| func (c *connCache) Close(log controllerutil.Logger) { | ||
| if c == nil { | ||
| return | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| for key, e := range c.entries { | ||
| e.Close(log) | ||
| delete(c.entries, key) | ||
| } | ||
| } | ||
|
|
||
| func (e *connCacheEntry) Conn(id v1.ClickHouseReplicaID, dial func() (clickhouse.Conn, error)) (clickhouse.Conn, error) { | ||
| e.mu.Lock() | ||
| defer e.mu.Unlock() | ||
|
|
||
| if conn, ok := e.conns[id]; ok { | ||
| return conn, nil | ||
| } | ||
|
|
||
| conn, err := dial() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| e.conns[id] = conn | ||
|
|
||
| return conn, nil | ||
| } | ||
|
|
||
| func (e *connCacheEntry) AnyConn(ctx context.Context) (v1.ClickHouseReplicaID, clickhouse.Conn, error) { | ||
| conns := func() map[v1.ClickHouseReplicaID]clickhouse.Conn { | ||
| e.mu.Lock() | ||
| defer e.mu.Unlock() | ||
|
|
||
| return maps.Clone(e.conns) | ||
| }() | ||
|
|
||
| for id, conn := range conns { | ||
| if conn.Ping(ctx) == nil { | ||
| return id, conn, nil | ||
| } | ||
| } | ||
|
|
||
| return v1.ClickHouseReplicaID{}, nil, errors.New("no available connections") | ||
| } | ||
|
|
||
| func (e *connCacheEntry) Close(log controllerutil.Logger) { | ||
| e.mu.Lock() | ||
| defer e.mu.Unlock() | ||
|
|
||
| for id, conn := range e.conns { | ||
| if err := conn.Close(); err != nil { | ||
| log.Warn("error closing pooled connection", "error", err, "replica_id", id) | ||
| } | ||
| } | ||
|
|
||
| clear(e.conns) | ||
| } |
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.