-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
add global connection pool for mysql #6327
Open
robpickerill
wants to merge
12
commits into
kedacore:main
Choose a base branch
from
robpickerill:global-mysql-conn-pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+37,828
−14,752
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
79304e9
add global connection pool for mysql
robpickerill 25d68fe
include pool settings in cache key
robpickerill c988e5e
remove parameters for global connection pool
robpickerill c7693f4
Refactor New Relic scaler (#6326)
rickbrouwer b27f5e7
Upgrade to the latest new-relic-go v2 client (#6325)
smcavallo 3fda9cc
fix: Paused ScaledObject count is reported correctly after operator r…
JorTurFer d12dba9
fix: ensure consistent JSON log format for automaxprocs (#6335)
omerap12 0d08f7b
feat: add nsq scaler (#6230)
Ulminator 3469716
fix: scaledjobs stuck as not ready (#6329)
msvticket 090afdf
Deprecate Stan scaler (#6363)
rickbrouwer 2e78157
refactor aws sqs queue scaler configuration (#6358)
omerap12 63ef804
Refactor AWS DynamoDB Streams scaler configuration (#6351)
omerap12 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 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
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,123 @@ | ||
package util | ||
|
||
//nolint:depguard // sync/atomic | ||
import ( | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// refCountedValue manages a reference-counted value with a cleanup function. | ||
type refCountedValue[V any] struct { | ||
value V | ||
refCount atomic.Int64 | ||
closeFunc func(V) error // Cleanup function to call when count reaches zero | ||
} | ||
|
||
// Add increments the reference count. | ||
func (r *refCountedValue[V]) Add() { | ||
r.refCount.Add(1) | ||
} | ||
|
||
// Remove decrements the reference count and invokes closeFunc if the count | ||
// reaches zero. | ||
func (r *refCountedValue[V]) Remove() error { | ||
if r.refCount.Add(-1) == 0 { | ||
return r.closeFunc(r.value) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Value returns the underlying value. | ||
func (r *refCountedValue[V]) Value() V { | ||
return r.value | ||
} | ||
|
||
// RefMap manages reference-counted items in a concurrent-safe map. | ||
type RefMap[K comparable, V any] struct { | ||
data map[K]*refCountedValue[V] | ||
mu sync.RWMutex | ||
} | ||
|
||
// NewRefMap initializes a new RefMap. A RefMap is an atomic reference-counted | ||
// concurrent hashmap. The general usage pattern is to Store a value with a | ||
// close function, once the value is contained within the RefMap, it can be | ||
// accessed via the Load method. The AddRef method signals ownership of the | ||
// value and increments the reference count. The RemoveRef method decrements | ||
// the reference count. When the reference count reaches zero, the close | ||
// function is called and the value is removed from the map. | ||
func NewRefMap[K comparable, V any]() *RefMap[K, V] { | ||
return &RefMap[K, V]{ | ||
data: make(map[K]*refCountedValue[V]), | ||
} | ||
} | ||
|
||
// Store adds a new item with an initial reference count of 1 and a close | ||
// function. | ||
func (r *RefMap[K, V]) Store(key K, value V, closeFunc func(V) error) error { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
if _, exists := r.data[key]; exists { | ||
return fmt.Errorf("key already exists: %v", key) | ||
} | ||
|
||
r.data[key] = &refCountedValue[V]{value: value, refCount: atomic.Int64{}, closeFunc: closeFunc} | ||
r.data[key].Add() // Set initial reference count to 1 | ||
|
||
return nil | ||
} | ||
|
||
// Load retrieves a value by key without modifying the reference count, | ||
// returning the value and a boolean indicating if it was found. The reference | ||
// count not being modified means that a check for the existence of a key | ||
// can be performed without signalling ownership of the value. If the value is | ||
// used after this method, it is recommended to call AddRef to increment the | ||
// reference | ||
func (r *RefMap[K, V]) Load(key K) (V, bool) { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
|
||
if refValue, found := r.data[key]; found { | ||
return refValue.Value(), true | ||
} | ||
var zero V | ||
|
||
return zero, false | ||
} | ||
|
||
// AddRef increments the reference count for a key if it exists. Ensure | ||
// to call RemoveRef when done with the value to prevent memory leaks. | ||
func (r *RefMap[K, V]) AddRef(key K) error { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
|
||
refValue, found := r.data[key] | ||
if !found { | ||
return fmt.Errorf("key not found: %v", key) | ||
} | ||
|
||
refValue.Add() | ||
return nil | ||
} | ||
|
||
// RemoveRef decrements the reference count and deletes the entry if count | ||
// reaches zero. | ||
func (r *RefMap[K, V]) RemoveRef(key K) error { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
refValue, found := r.data[key] | ||
if !found { | ||
return fmt.Errorf("key not found: %v", key) | ||
} | ||
|
||
err := refValue.Remove() | ||
|
||
if refValue.refCount.Load() == 0 { | ||
delete(r.data, key) | ||
} | ||
|
||
return err // returns the error from closeFunc | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Reconcile these values when scalers share pools but present different connection pool settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that maybe we could hide this parameter for the moment and share them if there is any request from users. As they can have different values, this could be so difficult to debug.
@kedacore/keda-core-contributors ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, yeah, just to clarify, did you mean to remove all the new parameters, thus making the globally shared connection pools the default, or just the connection pool tuning parameters?
I also thought a MySQL scaler v2 might fit as well for this, if that's of interest at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if adding a v2 scaler makes sense as we could potentially implement a connection pool as part of #6360.
Definitivelly, supporting this in mysql/postgre scalers is worth, but adding v2 scaler to include this feature could be an overkill as it won't make sense if we address a global connection pool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, was just a musing thought, and I lean on your thoughts here as you know the project much better than I
I was exploring how I might even end to end test this so I'll poke at that a bit more then move it out of draft when its all done. I'm also not entirely convinced by the global scope of the connection pool cache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kedacore/keda-core-contributors ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @kedacore/keda-core-contributors ?