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

feat: support configure refinery to use redis in cluster mode #1294

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type Config interface {
// management.
GetRedisHost() string

// GetRedisClusterHosts returns the list of addresses of a Redis cluster, used for
// managing peer cluster membership.
GetRedisClusterHosts() []string

// GetRedisUsername returns the username of a Redis instance to use for peer
// management.
GetRedisUsername() string
Expand Down
22 changes: 22 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/honeycombio/refinery/internal/configwatcher"
"github.com/honeycombio/refinery/pubsub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -454,6 +455,27 @@ func TestDryRun(t *testing.T) {
}
}

func TestRedisClusterHosts(t *testing.T) {
clusterHosts := []string{"localhost:7001", "localhost:7002"}
cm := makeYAML(
"General.ConfigurationVersion", 2,
"PeerManagement.Type", "redis",
"RedisPeerManagement.ClusterHosts", clusterHosts,
"RedisPeerManagement.Prefix", "test",
"RedisPeerManagement.Database", 9,
)
rm := makeYAML("ConfigVersion", 2)
config, rules := createTempConfigs(t, cm, rm)
defer os.Remove(rules)
defer os.Remove(config)
c, err := getConfig([]string{"--no-validate", "--config", config, "--rules_config", rules})
assert.NoError(t, err)

d := c.GetRedisClusterHosts()
require.NotNil(t, d)
require.EqualValues(t, clusterHosts, d)
}

func TestMaxAlloc(t *testing.T) {
cm := makeYAML("General.ConfigurationVersion", 2, "Collection.CacheCapacity", 1000, "Collection.MaxAlloc", 17179869184)
rm := makeYAML("ConfigVersion", 2)
Expand Down
8 changes: 8 additions & 0 deletions config/file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ type PeerManagementConfig struct {

type RedisPeerManagementConfig struct {
Host string `yaml:"Host" cmdenv:"RedisHost"`
ClusterHosts []string `yaml:"ClusterHosts"`
VinozzZ marked this conversation as resolved.
Show resolved Hide resolved
Username string `yaml:"Username" cmdenv:"RedisUsername"`
Password string `yaml:"Password" cmdenv:"RedisPassword"`
AuthCode string `yaml:"AuthCode" cmdenv:"RedisAuthCode"`
Expand Down Expand Up @@ -653,6 +654,13 @@ func (f *fileConfig) GetRedisHost() string {
return f.mainConfig.RedisPeerManagement.Host
}

func (f *fileConfig) GetRedisClusterHosts() []string {
f.mux.RLock()
defer f.mux.RUnlock()

return f.mainConfig.RedisPeerManagement.ClusterHosts
}

func (f *fileConfig) GetRedisUsername() string {
f.mux.RLock()
defer f.mux.RUnlock()
Expand Down
15 changes: 15 additions & 0 deletions config/metadata/configMeta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,20 @@ groups:
description: >
Must be in the form `host:port`.

- name: ClusterHosts
type: stringarray
valuetype: stringarray
example: "- localhost:6379"
firstversion: v2.8
validations:
- type: elementType
arg: hostport
reload: false
summary: is a list of host and port pairs for the instances in a Redis Cluster, used for managing peer cluster membership.
description: >
This configuration enables Refinery to connect to a Redis cluster. Each entry in the list should follow the format `host:port`.
VinozzZ marked this conversation as resolved.
Show resolved Hide resolved
If `ClusterHosts` is specified, the `Host` setting will be ignored.

- name: Username
v1group: PeerManagement
v1name: RedisUsername
Expand Down Expand Up @@ -1253,6 +1267,7 @@ groups:
- name: ShutdownDelay
type: duration
valuetype: nondefault
firstversion: v2.8
default: 15s
reload: true
summary: controls the maximum time Refinery can use while draining traces at shutdown.
Expand Down
7 changes: 7 additions & 0 deletions config/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type MockConfig struct {
GetLoggerLevelVal Level
GetPeersVal []string
GetRedisHostVal string
GetRedisClusterHostsVal []string
GetRedisUsernameVal string
GetRedisPasswordVal string
GetRedisAuthCodeVal string
Expand Down Expand Up @@ -216,6 +217,12 @@ func (m *MockConfig) GetRedisHost() string {
return m.GetRedisHostVal
}

func (m *MockConfig) GetRedisClusterHosts() []string {
VinozzZ marked this conversation as resolved.
Show resolved Hide resolved
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetRedisClusterHostsVal
}
func (m *MockConfig) GetRedisUsername() string {
m.Mux.RLock()
defer m.Mux.RUnlock()
Expand Down
12 changes: 7 additions & 5 deletions pubsub/pubsub_goredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pubsub
import (
"context"
"crypto/tls"
"strings"
"sync"

"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -58,14 +57,17 @@ func (ps *GoRedisPubSub) Start() error {

if ps.Config != nil {
host := ps.Config.GetRedisHost()
hosts := []string{host}
clusterHosts := ps.Config.GetRedisClusterHosts()
// if we have a cluster host, use that instead of the regular host
if len(clusterHosts) > 0 {
hosts = clusterHosts
}

username := ps.Config.GetRedisUsername()
pw := ps.Config.GetRedisPassword()
authcode = ps.Config.GetRedisAuthCode()

// we may have multiple hosts, separated by commas, so split them up and
// use them as the addrs for the client (if there are multiples, it will
// create a cluster client)
hosts := strings.Split(host, ",")
options.Addrs = hosts
options.Username = username
options.Password = pw
Expand Down
Loading