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

Adding Support for Redis Cluster Client #46

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion build.settings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Build configuration

version = 0.6.0
version = 0.6.1

8 changes: 6 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ func main() {
var localOnly = flag.Bool("local", false,
"If set, it only listens to incoming requests from the local host")
var port = flag.Int("http-port", 7399, "HTTP Server port for the REST API")
var redisUrl = flag.String("redis", "", "URI for the Redis cluster (host:port)")
var redisUrl = flag.String("redis", "", "For single node redis instances: URI "+
"for the Redis instance (host:port). For redis clusters: a comma-separated list of redis nodes. "+
"If using an ElastiCache redis cluster with cluster mode enabled, you can supply the configuration endpoint.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"If using an ElastiCache redis cluster with cluster mode enabled, you can supply the configuration endpoint.")
"If using an ElastiCache Redis cluster with cluster mode enabled, you can supply the configuration endpoint.")

Does it apply on any Redis deployment in cluster mode? or just Elasticache? If any, then we can remove the "Elasticache" part

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, updated

var cluster = flag.Bool("cluster", false,
z-cran marked this conversation as resolved.
Show resolved Hide resolved
"Needs to be set if connecting to a Redis instance with cluster mode enabled")
var awsEndpoint = flag.String("endpoint-url", "",
"HTTP URL for AWS SQS to connect to; usually best left undefined, "+
"unless required for local testing purposes (LocalStack uses http://localhost:4566)")
Expand Down Expand Up @@ -108,7 +112,7 @@ func main() {
} else {
logger.Info("Connecting to Redis server at %s", *redisUrl)
logger.Info("with timeout: %s, max-retries: %d", *timeout, *maxRetries)
store = storage.NewRedisStore(*redisUrl, 1, *timeout, *maxRetries)
store = storage.NewRedisStore(*redisUrl, *cluster, 1, *timeout, *maxRetries)
}
server.SetStore(store)

Expand Down
2 changes: 1 addition & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ then
endpoint="--endpoint-url ${AWS_ENDPOINT}"
fi

cmd="./sm-server -http-port ${SERVER_PORT} ${endpoint:-} ${DEBUG} \
cmd="./sm-server -http-port ${SERVER_PORT} ${endpoint:-} ${CLUSTER} ${DEBUG} \
-redis ${REDIS}:${REDIS_PORT} -timeout ${TIMEOUT:-25ms} -max-retries ${RETRIES:-3} \
-events ${EVENTS_Q} -notifications ${ERRORS_Q} \
$@"
Expand Down
5 changes: 3 additions & 2 deletions pubsub/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (listener *EventsListener) ListenForMessages() {
fmt.Sprintf("configuration [%s] could not be found", fsm.ConfigId)))
continue
}
previousState := fsm.State
cfgFsm := ConfiguredStateMachine{
Config: cfg,
FSM: fsm,
Expand All @@ -101,8 +102,8 @@ func (listener *EventsListener) ListenForMessages() {
request.GetEvent().GetTransition().GetEvent(), err)))
continue
}
listener.logger.Info("Event `%s` transitioned FSM [%s] to state `%s` - updating store",
request.Event.Transition.Event, smId, fsm.State)
listener.logger.Info("Event `%s` transitioned FSM [%s] to state `%s` from state `%s` - updating store",
Copy link
Owner

Choose a reason for hiding this comment

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

please use Debug not Info - this gets really noisy quickly (I know I used INFO before, my bad)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

request.Event.Transition.Event, smId, fsm.State, previousState)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add a log line before we try to "SendEvent" ? It seems like it can fail there and we wouldn't have the IDs logged (SM ID, event ID, previous state)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, adding as a debug entry

        listener.logger.Debug("Preparing to send event `%s` for FSM [%s] (current state: %s)",
            request.Event.Transition.Event, smId, previousState)

Copy link
Owner

Choose a reason for hiding this comment

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

not only we capture the error if SendEvent fails, but we also send a notification (L98)

err := listener.store.PutStateMachine(smId, fsm)
if err != nil {
listener.PostNotificationAndReportOutcome(makeResponse(&request,
Expand Down
40 changes: 17 additions & 23 deletions storage/redis_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (

type RedisStore struct {
logger *slf4go.Log
client *redis.Client
client redis.Cmdable
Timeout time.Duration
MaxRetries int
}
Expand Down Expand Up @@ -141,42 +141,36 @@ func (csm *RedisStore) GetTimeout() time.Duration {
}

func NewRedisStoreWithDefaults(address string) StoreManager {
return NewRedisStore(address, DefaultRedisDb, DefaultTimeout, DefaultMaxRetries)
return NewRedisStore(address, false, DefaultRedisDb, DefaultTimeout, DefaultMaxRetries)
}

func NewRedisStore(address string, db int, timeout time.Duration, maxRetries int) StoreManager {

func NewRedisStore(address string, isCluster bool, db int, timeout time.Duration, maxRetries int) StoreManager {
logger := slf4go.NewLog(fmt.Sprintf("redis://%s/%d", address, db))

var tlsConfig *tls.Config
var client redis.Cmdable

if os.Getenv("REDIS_TLS") != "" {
Copy link
Owner

Choose a reason for hiding this comment

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

this is already in main, I believe?
can you please do:

  1. update your main branch to be in sync with this repo's main
  2. rebase your branch add-redis-cluster on your main and resolve conflicts (if any)
  3. make sure you run tests (make test) and they all pass
    thanks.

Copy link
Owner

Choose a reason for hiding this comment

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

yep, see here it's been merged in main and release and it's in Release 0.6.0

logger.Info("Using TLS for Redis connection")
tlsConfig = &tls.Config{MinVersion: tls.VersionTLS12}
}
return &RedisStore{
logger: logger,
client: redis.NewClient(&redis.Options{

if isCluster {
client = redis.NewClusterClient(&redis.ClusterOptions{
TLSConfig: tlsConfig,
Addrs: strings.Split(address, ","),
})
} else {
client = redis.NewClient(&redis.Options{
TLSConfig: tlsConfig,
Addr: address,
DB: db, // 0 means default DB
}),
Timeout: timeout,
MaxRetries: maxRetries,
})
}
}

// FIXME: the "constructor" functions are very similar, the creation pattern will need to be
// refactored to avoid code duplication.

func NewRedisStoreWithCreds(address string, db int, timeout time.Duration, maxRetries int,
username string, password string) StoreManager {
return &RedisStore{
logger: slf4go.NewLog(fmt.Sprintf("redis:%s", address)),
client: redis.NewClient(&redis.Options{
Addr: address,
Username: username,
Password: password,
DB: db,
}),
logger: slf4go.NewLog(fmt.Sprintf("redis://%s/%d", address, db)),
client: client,
Timeout: timeout,
MaxRetries: maxRetries,
}
Expand Down