Skip to content
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

Merged
merged 20 commits into from
Oct 14, 2021

Conversation

ashikjm
Copy link
Contributor

@ashikjm ashikjm commented Jul 7, 2021

  • Added tests for cluster support
  • Introducing isCluster flag
  • Using redisc module to implement cluster support

All discussions happened in #467 and #481

- Added tests for cluster support
@coveralls
Copy link

coveralls commented Jul 7, 2021

Pull Request Test Coverage Report for Build 1180

  • 42 of 59 (71.19%) changed or added relevant lines in 3 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.8%) to 91.244%

Changes Missing Coverage Covered Lines Changed/Added Lines %
exporter/exporter.go 7 10 70.0%
exporter/redis.go 31 45 68.89%
Files with Coverage Reduction New Missed Lines %
exporter/redis.go 1 77.01%
Totals Coverage Status
Change from base Build 1173: -0.8%
Covered Lines: 1511
Relevant Lines: 1656

💛 - Coveralls

@codecov
Copy link

codecov bot commented Jul 7, 2021

Codecov Report

Merging #531 (369d9bd) into master (ef05f86) will decrease coverage by 0.93%.
The diff coverage is 71.64%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
exporter/redis.go 70.00% <56.75%> (-16.12%) ⬇️
exporter/exporter.go 91.32% <66.66%> (-0.64%) ⬇️
exporter/keys.go 79.19% <100.00%> (+0.42%) ⬆️
exporter/lua.go 100.00% <100.00%> (ø)
exporter/pwd_file.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef05f86...369d9bd. Read the comment docs.

@ashikjm
Copy link
Contributor Author

ashikjm commented Jul 7, 2021

@oliver006 I closed the other PR and pulled the changes here. Please review.

@oliver006
Copy link
Owner

Thanks for cleaning this up! I'll try to review this by the end of this week.

@moish83
Copy link

moish83 commented Jul 14, 2021

@ashikjm, great job, thank you.
when do you think this will be merged?
Another question, how will the exposed metrics differentiate between shards? Will we get another Prometheus label for shard number or something like that?

if err != nil {
t.Errorf("couldn't setup redis for uri %s, err: %s ", uri, err)
return err
log.Printf("Dial failed: %v", err)
Copy link
Owner

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?

return nil

Copy link
Owner

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.

@@ -145,6 +161,73 @@ func deleteKeysFromDB(t *testing.T, addr string) error {

c.Do("DEL", TestSetName)
c.Do("DEL", TestStreamName)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blank line plz.

}

func setupDBKeysCluster(t *testing.T, uri string) error {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no blank line plz


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)
Copy link
Owner

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.

@@ -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, "://") {
Copy link
Contributor

@houstonheat houstonheat Jul 25, 2021

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
}

Copy link
Owner

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.

@ashikjm
Copy link
Contributor Author

ashikjm commented Sep 4, 2021

@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.

@oliver006
Copy link
Owner

Hmm, not sure what's the easiest way to fix this. My normal flow for this is to first do a git fetch or git pull and then git rebase -i origin/master and then you replay your changes on top of master (and you also squash a few commits).

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.

@houstonheat
Copy link
Contributor

git pull --rebase origin master for short :)

Comment on lines 62 to 74
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"
}
}

Copy link
Owner

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] != "" {
Copy link
Owner

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, "://") {
Copy link
Owner

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.

}
log.Debugf("Running refresh on cluster object")
if err := cluster.Refresh(); err != nil {
log.Debugf("Cluster refresh failed: %v", err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errorf

log.Debugf("Creating redis connection object")
conn, err := cluster.Dial()
if err != nil {
log.Debugf("Dial failed: %v", err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errorf


c, err := redisc.RetryConn(conn, 10, 100*time.Millisecond)
if err != nil {
log.Debugf("RetryConn failed: %v", err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errorf

@@ -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) {
Copy link
Owner

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
@oliver006
Copy link
Owner

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.

@ashikjm
Copy link
Contributor Author

ashikjm commented Oct 7, 2021

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 git pull --rebase origin master. But then my commits get rejected saying 'Updates were rejected because the tip of your current branch is behind its remote counterpart.'. Then I have to git pull and then all these get pulled in.

@oliver006
Copy link
Owner

This looks good now @ashikjm - thanks for your patience and persistence!

@oliver006
Copy link
Owner

Released as v1.28.0

@ashikjm
Copy link
Contributor Author

ashikjm commented Nov 1, 2021

This looks good now @ashikjm - thanks for your patience and persistence!

Thank you for bearing with me @oliver006 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants