From 16450d94a02aa98f53b8af42c3b41214f3fcf086 Mon Sep 17 00:00:00 2001 From: Leonardo Hahn Date: Fri, 8 Nov 2019 17:30:01 -0300 Subject: [PATCH] Pass blacklist in config instead of parameter --- app.go | 1 - app_test.go | 10 ++++---- cluster/etcd_service_discovery.go | 25 ++++++++++--------- cluster/etcd_service_discovery_test.go | 33 +++++++++++++------------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/app.go b/app.go index f4bb8522..a440b732 100644 --- a/app.go +++ b/app.go @@ -294,7 +294,6 @@ func startDefaultSD() { app.config, app.server, app.dieChan, - nil, ) if err != nil { logger.Log.Fatalf("error starting cluster service discovery component: %s", err.Error()) diff --git a/app_test.go b/app_test.go index 23014867..93782454 100644 --- a/app_test.go +++ b/app_test.go @@ -79,7 +79,7 @@ func setup() { initApp() Configure(true, "testtype", Cluster, map[string]string{}, viper.New()) - etcdSD, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan, nil) + etcdSD, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan) if err != nil { panic(err) } @@ -202,7 +202,7 @@ func TestGetMetricsReporters(t *testing.T) { assert.Equal(t, app.metricsReporters, GetMetricsReporters()) } func TestGetServerByID(t *testing.T) { - r, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan, nil) + r, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan) assert.NoError(t, err) assert.NotNil(t, r) SetServiceDiscoveryClient(r) @@ -212,7 +212,7 @@ func TestGetServerByID(t *testing.T) { } func TestGetServersByType(t *testing.T) { - r, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan, nil) + r, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan) assert.NoError(t, err) assert.NotNil(t, r) SetServiceDiscoveryClient(r) @@ -251,7 +251,7 @@ func TestSetRPCClient(t *testing.T) { func TestSetServiceDiscovery(t *testing.T) { initApp() Configure(true, "testtype", Cluster, map[string]string{}, viper.New()) - r, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan, nil) + r, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan) assert.NoError(t, err) assert.NotNil(t, r) SetServiceDiscoveryClient(r) @@ -418,7 +418,7 @@ func TestStartAndListenCluster(t *testing.T) { initApp() Configure(true, "testtype", Cluster, map[string]string{}, cfg) - etcdSD, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan, nil, cli) + etcdSD, err := cluster.NewEtcdServiceDiscovery(app.config, app.server, app.dieChan, cli) assert.NoError(t, err) SetServiceDiscoveryClient(etcdSD) diff --git a/cluster/etcd_service_discovery.go b/cluster/etcd_service_discovery.go index 96d289cc..4d64eb04 100644 --- a/cluster/etcd_service_discovery.go +++ b/cluster/etcd_service_discovery.go @@ -72,7 +72,6 @@ func NewEtcdServiceDiscovery( config *config.Config, server *Server, appDieChan chan bool, - serverTypesBlacklist []string, cli ...*clientv3.Client, ) (ServiceDiscovery, error) { var client *clientv3.Client @@ -80,16 +79,15 @@ func NewEtcdServiceDiscovery( client = cli[0] } sd := &etcdServiceDiscovery{ - config: config, - running: false, - server: server, - serverMapByType: make(map[string]map[string]*Server), - listeners: make([]SDListener, 0), - stopChan: make(chan bool), - stopLeaseChan: make(chan bool), - appDieChan: appDieChan, - cli: client, - serverTypesBlacklist: serverTypesBlacklist, + config: config, + running: false, + server: server, + serverMapByType: make(map[string]map[string]*Server), + listeners: make([]SDListener, 0), + stopChan: make(chan bool), + stopLeaseChan: make(chan bool), + appDieChan: appDieChan, + cli: client, } sd.configure() @@ -111,6 +109,11 @@ func (sd *etcdServiceDiscovery) configure() { sd.grantLeaseMaxRetries = sd.config.GetInt("pitaya.cluster.sd.etcd.grantlease.maxretries") sd.grantLeaseInterval = sd.config.GetDuration("pitaya.cluster.sd.etcd.grantlease.retryinterval") sd.shutdownDelay = sd.config.GetDuration("pitaya.cluster.sd.etcd.shutdown.delay") + sd.serverTypesBlacklist = sd.config.GetStringSlice("pitaya.cluster.sd.etcd.servertypeblacklist") + + if len(sd.serverTypesBlacklist) > 0 { + logger.Log.Info("using server types blacklist: %s", sd.serverTypesBlacklist) + } } func (sd *etcdServiceDiscovery) watchLeaseChan(c <-chan *clientv3.LeaseKeepAliveResponse) { diff --git a/cluster/etcd_service_discovery_test.go b/cluster/etcd_service_discovery_test.go index 1a6771de..61489ff1 100644 --- a/cluster/etcd_service_discovery_test.go +++ b/cluster/etcd_service_discovery_test.go @@ -100,10 +100,10 @@ func getConfig(conf ...*viper.Viper) *config.Config { return config } -func getEtcdSD(t *testing.T, config *config.Config, server *Server, blacklist []string, cli *clientv3.Client) *etcdServiceDiscovery { +func getEtcdSD(t *testing.T, config *config.Config, server *Server, cli *clientv3.Client) *etcdServiceDiscovery { t.Helper() appDieChan := make(chan bool) - e, err := NewEtcdServiceDiscovery(config, server, appDieChan, blacklist, cli) + e, err := NewEtcdServiceDiscovery(config, server, appDieChan, cli) assert.NoError(t, err) return e.(*etcdServiceDiscovery) } @@ -115,7 +115,7 @@ func TestNewEtcdServiceDiscovery(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) assert.NotNil(t, e) }) } @@ -128,7 +128,7 @@ func TestEtcdSDBootstrapLease(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) err := e.grantLease() assert.NoError(t, err) assert.NotEmpty(t, e.leaseID) @@ -143,7 +143,7 @@ func TestEtcdSDBootstrapLeaseError(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) err := e.grantLease() assert.Error(t, err) }) @@ -157,7 +157,7 @@ func TestEtcdSDBootstrapServer(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.grantLease() err := e.bootstrapServer(table.server) assert.NoError(t, err) @@ -182,7 +182,7 @@ func TestEtcdSDDeleteServer(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.grantLease() err := e.bootstrapServer(table.server) assert.NoError(t, err) @@ -221,7 +221,7 @@ func TestEtcdSDDeleteLocalInvalidServers(t *testing.T) { t.Run(table.server.ID, func(t *testing.T) { config := getConfig() _, cli := helpers.GetTestEtcd(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) invalidServer := &Server{ ID: "invalid", Type: "bla", @@ -242,7 +242,7 @@ func TestEtcdSDGetServer(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.grantLease() e.bootstrapServer(table.server) sv, err := e.GetServer(table.server.ID) @@ -258,7 +258,7 @@ func TestEtcdSDGetServers(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, &Server{}, nil, cli) + e := getEtcdSD(t, config, &Server{}, cli) e.grantLease() for _, server := range table.servers { e.bootstrapServer(server) @@ -277,7 +277,7 @@ func TestEtcdSDInit(t *testing.T) { config := getConfig(conf) c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.Init() // should set running assert.True(t, e.running) @@ -304,7 +304,7 @@ func TestEtcdBeforeShutdown(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.Init() assert.True(t, e.running) e.BeforeShutdown() @@ -322,7 +322,7 @@ func TestEtcdShutdown(t *testing.T) { config := getConfig() c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.Init() assert.True(t, e.running) e.Shutdown() @@ -340,7 +340,7 @@ func TestEtcdWatchChangesAddNewServers(t *testing.T) { config := getConfig(conf) c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.running = true e.bootstrapServer(table.server) e.watchEtcdChanges() @@ -374,7 +374,7 @@ func TestEtcdWatchChangesDeleteServers(t *testing.T) { config := getConfig(conf) c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, nil, cli) + e := getEtcdSD(t, config, table.server, cli) e.running = true e.bootstrapServer(table.server) e.watchEtcdChanges() @@ -411,10 +411,11 @@ func TestEtcdWatchChangesWithBlacklist(t *testing.T) { t.Run(table.name, func(t *testing.T) { conf := viper.New() conf.Set("pitaya.cluster.sd.etcd.syncservers.interval", "10ms") + conf.Set("pitaya.cluster.sd.etcd.serverTypeBlacklist", table.serverTypeBlacklist) config := getConfig(conf) c, cli := helpers.GetTestEtcd(t) defer c.Terminate(t) - e := getEtcdSD(t, config, table.server, table.serverTypeBlacklist, cli) + e := getEtcdSD(t, config, table.server, cli) e.running = true _ = e.bootstrapServer(table.server) e.watchEtcdChanges()