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

Handle Missing Redis Ports by Defaulting to Standard Redis Port in Loki #11414

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
67 changes: 35 additions & 32 deletions pkg/storage/chunk/cache/redis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,41 @@ func NewRedisClient(cfg *RedisConfig) (*RedisClient, error) {
}

func deriveEndpoints(endpoint string, lookup func(host string) ([]string, error)) ([]string, error) {
if lookup == nil {
return nil, fmt.Errorf("lookup function is nil")
}

endpoints := strings.Split(endpoint, ",")

// no endpoints or multiple endpoints will not need derivation
if len(endpoints) != 1 {
return endpoints, nil
}

// Handle single configuration endpoint which resolves multiple nodes.
host, port, err := net.SplitHostPort(endpoints[0])
if err != nil {
return nil, fmt.Errorf("splitting host:port failed :%w", err)
}
addrs, err := lookup(host)
if err != nil {
return nil, fmt.Errorf("could not lookup host: %w", err)
}

// only use the resolved addresses if they are not all loopback addresses;
// multiple addresses invokes cluster mode
allLoopback := allAddrsAreLoopback(addrs)
if len(addrs) > 1 && !allLoopback {
endpoints = nil
for _, addr := range addrs {
endpoints = append(endpoints, net.JoinHostPort(addr, port))
}
}

return endpoints, nil
if lookup == nil {
return nil, fmt.Errorf("lookup function is nil")
}

endpoints := strings.Split(endpoint, ",")

// no endpoints or multiple endpoints will not need derivation
if len(endpoints) != 1 {
return endpoints, nil
}

// Handle single configuration endpoint which resolves multiple nodes.
host, port, err := net.SplitHostPort(endpoints[0])
if err != nil {
// If the port is missing, use the host as is and set a default port, if desired
host = endpoints[0]
// Example: set a default port, or you can choose to handle this differently
port = "6379" // Default Redis port
Comment on lines +101 to +104
Copy link
Contributor

Choose a reason for hiding this comment

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

this feels like something we should do via a validateConfig function before passing the config to the client creation

@dannykopping I think you worked on the caching code the most recently, any thoughts here?

Copy link
Author

Choose a reason for hiding this comment

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

Let me know if you would like me to approach this differently. I would be happy to contribute.

Copy link
Author

Choose a reason for hiding this comment

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

Following up on this @dannykopping and @cstyan

Copy link
Contributor

Choose a reason for hiding this comment

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

I would have a new function that's called before deriveEndpoints in NewRedisClient that logs a warning for every endpoint that doesn't have a port set clarifying that the default port will be used. Alternatively, this new function could be called before NewRedisClient in cache.go New.

}
addrs, err := lookup(host)
if err != nil {
return nil, fmt.Errorf("could not lookup host: %w", err)
}

// only use the resolved addresses if they are not all loopback addresses;
// multiple addresses invokes cluster mode
allLoopback := allAddrsAreLoopback(addrs)
if len(addrs) > 1 && !allLoopback {
endpoints = nil
for _, addr := range addrs {
endpoints = append(endpoints, net.JoinHostPort(addr, port))
}
}

return endpoints, nil
}

func allAddrsAreLoopback(addrs []string) bool {
Expand Down
Loading