-
Notifications
You must be signed in to change notification settings - Fork 888
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
[400] Adding redis cluster support #531
[400] Adding redis cluster support #531
Conversation
- Added tests for cluster support
Pull Request Test Coverage Report for Build 1180
💛 - Coveralls |
Codecov Report
@@ Coverage Diff @@
## master #531 +/- ##
==========================================
- Coverage 88.11% 87.17% -0.94%
==========================================
Files 16 16
Lines 1313 1365 +52
==========================================
+ Hits 1157 1190 +33
- Misses 93 104 +11
- Partials 63 71 +8
Continue to review full report at Codecov.
|
@oliver006 I closed the other PR and pulled the changes here. Please review. |
Thanks for cleaning this up! I'll try to review this by the end of this week. |
@ashikjm, great job, thank you. |
exporter/exporter_test.go
Outdated
if err != nil { | ||
t.Errorf("couldn't setup redis for uri %s, err: %s ", uri, err) | ||
return err | ||
log.Printf("Dial failed: %v", err) |
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.
Can you keep this is as Errorf()
please?
exporter/exporter_test.go
Outdated
return nil | ||
|
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.
Remove the blank line plz.
exporter/exporter_test.go
Outdated
@@ -145,6 +161,73 @@ func deleteKeysFromDB(t *testing.T, addr string) error { | |||
|
|||
c.Do("DEL", TestSetName) | |||
c.Do("DEL", TestStreamName) | |||
|
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.
No blank line plz.
exporter/exporter_test.go
Outdated
} | ||
|
||
func setupDBKeysCluster(t *testing.T, uri string) error { | ||
|
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.
no blank line plz
exporter/exporter_test.go
Outdated
|
||
if _, err := c.Do("SELECT", dbNumStr); err != nil { | ||
log.Printf("setupDBKeys() - couldn't setup redis, err: %s ", err) | ||
// not failing on this one - cluster doesn't allow for SELECT so we log and ignore the error | ||
} | ||
|
||
for _, key := range keys { | ||
_, err = c.Do("SET", key, TestValue) | ||
_, err := c.Do("SET", key, TestValue) |
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.
Good catch.
You could even change this to if _, err := c.Do("SET", key, TestValue); err != nil {
to make sure the scoping is as tight as possible.
…tions - Linting the code
@@ -52,6 +64,50 @@ func (e *Exporter) connectToRedis() (redis.Conn, error) { | |||
return c, err | |||
} | |||
|
|||
func (e *Exporter) connectToRedisCluster() (redis.Conn, error) { | |||
uri := e.redisAddr | |||
if strings.Contains(uri, "://") { |
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.
Since you have already copied this code into TestClusterKeyValuesAndSizes()
, I propose to move the stage of determining the address into a separate method (e.g. getClusterAddr()
). In this case, it can also be tested and reused during unit testing:
// redis.go
func (e *Exporter) getClusterAddr() string {
clusterAddr := e.redisAddr
if strings.Contains(clusterAddr, "://") {
...
} else {
...
}
return clusterAddr
}
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'd suggest to get rid of the copy in TestClusterKeyValuesAndSizes()
- we control what uri is being passed to the tests and we shouldn't have to sanitize the input there.
Co-authored-by: Hamidreza Kalantari <hamidreza.kalantari@cafebazaar.ir>
… increase performance (oliver006#538) * Add a type check to only run GET on keys of type string * Add comment for reason why PFCOUNT is ran first and not STRLEN when checking string length
- Optimizing code
@oliver006 I had to rebase to push changes to this branch. It was refusing to accept the push saying the branch was behind. But rebase has pulled a lot of changes from the master branch. Do you know a easy way to clean this? Last time I had to abandon the whole commit and raise a new PR. |
Hmm, not sure what's the easiest way to fix this. My normal flow for this is to first do a I thing you could just leave this as is. I'll squash merge the PR when it's done and then the extra commits will go away. I'll try to get this reviewed by the end of today. |
|
exporter/keys_test.go
Outdated
if strings.Contains(uri, "://") { | ||
url, _ := url.Parse(uri) | ||
if url.Port() == "" { | ||
uri = url.Host + ":6379" | ||
} else { | ||
uri = url.Host | ||
} | ||
} else { | ||
if frags := strings.Split(uri, ":"); len(frags) != 2 { | ||
uri = uri + ":6379" | ||
} | ||
} | ||
|
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 don't think you need all of this, you can expect a correctly formed Uri for the tests.
@@ -28,13 +31,22 @@ func (e *Exporter) connectToRedis() (redis.Conn, error) { | |||
options = append(options, redis.DialPassword(e.options.Password)) | |||
} | |||
|
|||
if e.options.PasswordMap[uri] != "" { |
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.
Don't pass uri
if this is all you need it for, instead pass password
. Move the password check into the caller (and check both, e.options.password
and the map and then pass it to configureOptions).
You should use e.redisAddr
for the password lookup from the map, in that way it's the exact same format as provided by the user and can match what they put in the password file. Makes it easier to keep those two consistent.
Also, change the function signature to func (e *Exporter) configureOptions(uri string) ...
@@ -52,6 +64,50 @@ func (e *Exporter) connectToRedis() (redis.Conn, error) { | |||
return c, err | |||
} | |||
|
|||
func (e *Exporter) connectToRedisCluster() (redis.Conn, error) { | |||
uri := e.redisAddr | |||
if strings.Contains(uri, "://") { |
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'd suggest to get rid of the copy in TestClusterKeyValuesAndSizes()
- we control what uri is being passed to the tests and we shouldn't have to sanitize the input there.
exporter/redis.go
Outdated
} | ||
log.Debugf("Running refresh on cluster object") | ||
if err := cluster.Refresh(); err != nil { | ||
log.Debugf("Cluster refresh failed: %v", err) |
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.
Errorf
exporter/redis.go
Outdated
log.Debugf("Creating redis connection object") | ||
conn, err := cluster.Dial() | ||
if err != nil { | ||
log.Debugf("Dial failed: %v", err) |
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.
Errorf
exporter/redis.go
Outdated
|
||
c, err := redisc.RetryConn(conn, 10, 100*time.Millisecond) | ||
if err != nil { | ||
log.Debugf("RetryConn failed: %v", err) |
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.
Errorf
exporter/exporter_test.go
Outdated
@@ -59,40 +60,57 @@ func getTestExporterWithOptions(opt Options) *Exporter { | |||
return e | |||
} | |||
|
|||
func setupDBKeys(t *testing.T, uri string) error { | |||
c, err := redis.DialURL(uri) | |||
func createClusterClient(uri string) (redis.Conn, error) { |
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.
Why a repeat all the logic here and not use connectToRedisCluster()
instead?
All you need to do is set redisAddr
, do something like this:
e := Exporter{redisAddr: uri}
c, err := e.connectToRedisCluster()
...
* more logging when loading password file * log error when conversion fails
I see you pushed up some new commits (and also some old ones), I'll try to review this in the next couple of days. |
I only rebase from the master before pushing |
This looks good now @ashikjm - thanks for your patience and persistence! |
Released as v1.28.0 |
Thank you for bearing with me @oliver006 :) |
All discussions happened in #467 and #481