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

[etcd] Expose cached etcd clients in cluster etcd config service #2975

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 42 additions & 2 deletions src/cluster/client/etcd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"math/rand"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -55,12 +56,23 @@ const (

var errInvalidNamespace = errors.New("invalid namespace")

// make sure m3cluster and etcd client interfaces are implemented, and that
// Client is a superset of cluster.Client.
var _ client.Client = Client((*csclient)(nil))

type newClientFn func(cluster Cluster) (*clientv3.Client, error)

type cacheFileForZoneFn func(zone string) etcdkv.CacheFileFn

// NewConfigServiceClient returns a ConfigServiceClient.
func NewConfigServiceClient(opts Options) (client.Client, error) {
// ZoneClient is a cached etcd client for a zone.
type ZoneClient struct {
Client *clientv3.Client
Zone string
}

// NewEtcdConfigServiceClient returns a new etcd-backed cluster client.
//nolint:golint
func NewEtcdConfigServiceClient(opts Options) (*csclient, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
Expand All @@ -83,6 +95,11 @@ func NewConfigServiceClient(opts Options) (client.Client, error) {
}, nil
}

// NewConfigServiceClient returns a ConfigServiceClient.
func NewConfigServiceClient(opts Options) (client.Client, error) {
return NewEtcdConfigServiceClient(opts)
}

type csclient struct {
sync.RWMutex
clis map[string]*clientv3.Client
Expand Down Expand Up @@ -276,6 +293,29 @@ func (c *csclient) etcdClientGen(zone string) (*clientv3.Client, error) {
return cli, nil
}

// Clients returns all currently cached etcd clients.
func (c *csclient) Clients() []ZoneClient {
c.Lock()
defer c.Unlock()

var (
zones = make([]string, 0, len(c.clis))
clients = make([]ZoneClient, 0, len(c.clis))
)

for k := range c.clis {
zones = append(zones, k)
}

sort.Strings(zones)

for _, zone := range zones {
clients = append(clients, ZoneClient{Zone: zone, Client: c.clis[zone]})
}

return clients
}

func newClient(cluster Cluster) (*clientv3.Client, error) {
tls, err := cluster.TLSOptions().Config()
if err != nil {
Expand Down
26 changes: 25 additions & 1 deletion src/cluster/client/etcd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/services"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/integration"
"github.com/stretchr/testify/require"
)

func TestETCDClientGen(t *testing.T) {
Expand Down Expand Up @@ -297,6 +298,29 @@ func TestReuseKVStore(t *testing.T) {
client.storeLock.Unlock()
}

func TestGetEtcdClients(t *testing.T) {
opts := testOptions()
c, err := NewEtcdConfigServiceClient(opts)
require.NoError(t, err)

c1, err := c.etcdClientGen("zone2")
require.NoError(t, err)
require.Equal(t, 1, len(c.clis))

c2, err := c.etcdClientGen("zone1")
require.NoError(t, err)
require.Equal(t, 2, len(c.clis))
require.False(t, c1 == c2)

clients := c.Clients()
require.Len(t, clients, 2)

assert.Equal(t, clients[0].Zone, "zone1")
assert.Equal(t, clients[0].Client, c2)
assert.Equal(t, clients[1].Zone, "zone2")
assert.Equal(t, clients[1].Client, c1)
}

func TestValidateNamespace(t *testing.T) {
inputs := []struct {
ns string
Expand Down
8 changes: 8 additions & 0 deletions src/cluster/client/etcd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"time"

"github.com/m3db/m3/src/cluster/client"
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/x/instrument"
"github.com/m3db/m3/src/x/retry"
Expand Down Expand Up @@ -135,3 +136,10 @@ type Cluster interface {
DialTimeout() time.Duration
SetDialTimeout(value time.Duration) Cluster
}

// Client is an etcd-backed m3cluster client.
type Client interface {
client.Client

Clients() []ZoneClient
}