Skip to content

Commit

Permalink
Add configurable store timeout
Browse files Browse the repository at this point in the history
Add a command line option to set the store timeout.
  • Loading branch information
Max committed Mar 4, 2020
1 parent ca4fbe6 commit fe16765
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 7 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/sorintlab/stolon/internal/cluster"
Expand Down Expand Up @@ -50,12 +51,14 @@ type CommonConfig struct {
KubeConfig string
KubeContext string
KubeNamespace string
StoreTimeout time.Duration
}

func AddCommonFlags(cmd *cobra.Command, cfg *CommonConfig) {
cmd.PersistentFlags().StringVar(&cfg.ClusterName, "cluster-name", "", "cluster name")
cmd.PersistentFlags().StringVar(&cfg.StoreBackend, "store-backend", "", "store backend type (etcdv2/etcd, etcdv3, consul or kubernetes)")
cmd.PersistentFlags().StringVar(&cfg.StoreEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (use https scheme for tls communication) (defaults: http://127.0.0.1:2379 for etcd, http://127.0.0.1:8500 for consul)")
cmd.PersistentFlags().DurationVar(&cfg.StoreTimeout, "store-timeout", cluster.DefaultStoreTimeout, "store request timeout")
cmd.PersistentFlags().StringVar(&cfg.StorePrefix, "store-prefix", common.StorePrefix, "the store base prefix")
cmd.PersistentFlags().StringVar(&cfg.StoreCertFile, "store-cert-file", "", "certificate file for client identification to the store")
cmd.PersistentFlags().StringVar(&cfg.StoreKeyFile, "store-key", "", "private key file for client identification to the store")
Expand Down Expand Up @@ -143,6 +146,7 @@ func NewKVStore(cfg *CommonConfig) (store.KVStore, error) {
return store.NewKVStore(store.Config{
Backend: store.Backend(cfg.StoreBackend),
Endpoints: cfg.StoreEndpoints,
Timeout: cfg.StoreTimeout,
CertFile: cfg.StoreCertFile,
KeyFile: cfg.StoreKeyFile,
CAFile: cfg.StoreCAFile,
Expand Down Expand Up @@ -195,7 +199,7 @@ func NewElection(cfg *CommonConfig, uid string) (store.Election, error) {
if err != nil {
return nil, fmt.Errorf("cannot create kv store: %v", err)
}
election = store.NewKVBackedElection(kvstore, filepath.Join(storePath, common.SentinelLeaderKey), uid)
election = store.NewKVBackedElection(kvstore, filepath.Join(storePath, common.SentinelLeaderKey), uid, cfg.StoreTimeout)
case "kubernetes":
kubecli, podName, namespace, err := getKubeValues(cfg)
if err != nil {
Expand All @@ -216,7 +220,8 @@ func getKubeValues(cfg *CommonConfig) (*kubernetes.Clientset, string, string, er
if err != nil {
return nil, "", "", err
}
kubecfg.Timeout = cluster.DefaultStoreTimeout

kubecfg.Timeout = cfg.StoreTimeout
kubecli, err := kubernetes.NewForConfig(kubecfg)
if err != nil {
return nil, "", "", fmt.Errorf("cannot create kubernetes client: %v", err)
Expand Down
9 changes: 5 additions & 4 deletions internal/store/kvbacked.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var URLSchemeRegexp = regexp.MustCompile(`^([a-zA-Z][a-zA-Z0-9+-.]*)://`)
type Config struct {
Backend Backend
Endpoints string
Timeout time.Duration
BasePath string
CertFile string
KeyFile string
Expand Down Expand Up @@ -172,7 +173,7 @@ func NewKVStore(cfg Config) (KVStore, error) {
case CONSUL, ETCDV2:
config := &libkvstore.Config{
TLS: tlsConfig,
ConnectionTimeout: cluster.DefaultStoreTimeout,
ConnectionTimeout: cfg.Timeout,
}

store, err := libkv.NewStore(kvBackend, addrs, config)
Expand All @@ -190,7 +191,7 @@ func NewKVStore(cfg Config) (KVStore, error) {
if err != nil {
return nil, err
}
return &etcdV3Store{c: c, requestTimeout: cluster.DefaultStoreTimeout}, nil
return &etcdV3Store{c: c, requestTimeout: cfg.Timeout}, nil
default:
return nil, fmt.Errorf("Unknown store backend: %q", cfg.Backend)
}
Expand Down Expand Up @@ -344,7 +345,7 @@ func (s *KVBackedStore) GetProxiesInfo(ctx context.Context) (cluster.ProxiesInfo
return psi, nil
}

func NewKVBackedElection(kvStore KVStore, path, candidateUID string) Election {
func NewKVBackedElection(kvStore KVStore, path, candidateUID string, timeout time.Duration) Election {
switch kvStore := kvStore.(type) {
case *libKVStore:
s := kvStore
Expand All @@ -357,7 +358,7 @@ func NewKVBackedElection(kvStore KVStore, path, candidateUID string) Election {
path: path,
candidateUID: candidateUID,
ttl: MinTTL,
requestTimeout: cluster.DefaultStoreTimeout,
requestTimeout: timeout,
}
default:
panic("unknown kvstore")
Expand Down

0 comments on commit fe16765

Please sign in to comment.