diff --git a/Gopkg.lock b/Gopkg.lock index 3cb3d98..7d421a4 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -296,7 +296,7 @@ version = "v2.0.0" [[projects]] - digest = "1:f5f42d335df14458b5b5987a3d4314c3093ca25d7f563f69154152bd391b7313" + digest = "1:128d52b9a5403f1317ca63303eba69d540e4e4ec23fbd3f4961d29b46583dcc5" name = "github.com/integration-system/isp-lib-test" packages = [ "ctx", @@ -305,8 +305,8 @@ "utils/postgres", ] pruneopts = "UT" - revision = "39151a14e595a5993712b449bb44abace58e86f5" - version = "v1.3.0" + revision = "6fa261f607e7948466ebdcac2dad9283705e265f" + version = "v1.4.0" [[projects]] branch = "master" diff --git a/conf/config.yml b/conf/config.yml index 5e32a6a..441f5a6 100644 --- a/conf/config.yml +++ b/conf/config.yml @@ -34,5 +34,6 @@ cluster: inMemory: true outerAddress: 127.0.0.1:9001 connectTimeoutSeconds: 5 + bootstrapCluster: true peers: - 127.0.0.1:9001 diff --git a/conf/configuration.go b/conf/configuration.go index 522c2c6..c755f3e 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -18,7 +18,8 @@ type Configuration struct { type ClusterConfiguration struct { InMemory bool - DataDir string `valid:"required~Required"` + DataDir string `valid:"required~Required"` + BootstrapCluster bool Peers []string `valid:"required~Required"` OuterAddress string `valid:"required~Required"` ConnectTimeoutSeconds int `valid:"required~Required"` diff --git a/config_service_integration_test.go b/config_service_integration_test.go index 3b85999..679abf5 100644 --- a/config_service_integration_test.go +++ b/config_service_integration_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "github.com/integration-system/isp-lib-test/ctx" "github.com/integration-system/isp-lib-test/docker" @@ -11,9 +12,11 @@ import ( "github.com/stretchr/testify/assert" "google.golang.org/grpc" "io" + "io/ioutil" "isp-config-service/conf" "isp-config-service/domain" "log" + "strconv" "strings" "testing" "time" @@ -83,10 +86,12 @@ func setup(testCtx *ctx.TestContext, runTest func() int) int { Grpc: grpcAddr, }, Cluster: conf.ClusterConfiguration{ - InMemory: true, - DataDir: "./data", - Peers: nil, - OuterAddress: httpAddr.GetAddress(), + InMemory: true, + DataDir: "./data", + Peers: nil, + OuterAddress: httpAddr.GetAddress(), + ConnectTimeoutSeconds: 10, + BootstrapCluster: false, }, } configsGrpcAddrs[i] = grpcAddr @@ -95,13 +100,15 @@ func setup(testCtx *ctx.TestContext, runTest func() int) int { peersAddrs[i] = httpAddr.GetAddress() } + configs[0].Cluster.BootstrapCluster = true + for i := 0; i < configsNumber; i++ { c := configs[i] peersAddrsEnv := generatePeersConfigEnv(peersAddrs) ctx := env.RunAppContainer( cfg.Images.Module, c, nil, docker.WithName(c.GrpcOuterAddress.IP), - //docker.WithLogger(NewWriteLogger(strconv.Itoa(i)+"_config:", ioutil.Discard)), + docker.WithLogger(NewWriteLogger(strconv.Itoa(i)+"_config:", ioutil.Discard, "DeleteCommonConfigsCommand")), //docker.PullImage(cfg.Registry.Username, cfg.Registry.Password), docker.WithEnv(peersAddrsEnv), ) @@ -129,20 +136,27 @@ func TestClusterElection(t *testing.T) { }() a := assert.New(t) - testClusterReady(a) + testClusterReady(a, -1) for i := 0; i < configsNumber; i++ { - log.Printf("\nstopping %d container\n", i) - err := configsCtxs[i].StopContainer(20 * time.Second) - a.NoError(err) + fmt.Println() + log.Printf("stopping %d container\n", i) + a.NoError(configsCtxs[i].StopContainer(20 * time.Second)) - time.Sleep(3 * time.Second) + time.Sleep(9 * time.Second) + fmt.Println() + log.Printf("checking cluster except %d\n", i) + ready := testClusterReady(a, i) + if !ready { + return + } + + fmt.Println() log.Printf("starting %d container\n", i) - err = configsCtxs[i].StartContainer() - a.NoError(err) + a.NoError(configsCtxs[i].StartContainer()) log.Println("checking cluster") - ready := testClusterReady(a) + ready = testClusterReady(a, -1) if !ready { return } @@ -156,8 +170,11 @@ func getConfigServiceAddress(num int, port string) structure.AddressConfiguratio } } -func testClusterReady(a *assert.Assertions) bool { +func testClusterReady(a *assert.Assertions, except int) bool { for j := 0; j < configsNumber; j++ { + if j == except { + continue + } configAddr := configsGrpcAddrs[j] client := testGrpcReady(configAddr, a) if client == nil { @@ -189,7 +206,7 @@ func testGrpcReady(configAddr structure.AddressConfiguration, a *assert.Assertio if !a.NoError(err) { return nil } - log.Println("waiting until grpc ready:", time.Now().Sub(start)) + log.Println("waiting until grpc ready:", time.Now().Sub(start).Round(time.Second)) return client } @@ -210,12 +227,14 @@ func testRaftReady(client *backend.InternalGrpcClient, a *assert.Assertions) boo return nil, err } _, _ = await(f, maxAwaitingTime) - log.Println("waiting until raft ready:", time.Now().Sub(start)) + log.Println("waiting until raft ready:", time.Now().Sub(start).Round(time.Second)) return a.NoError(err) } func await(dialer func() (interface{}, error), timeout time.Duration) (interface{}, error) { - return utils.NewRetryer(dialer, timeout).Do() + retryer := utils.NewRetryer(dialer, timeout) + retryer.AttemptTimeout = 300 * time.Millisecond + return retryer.Do() } func generatePeersConfigEnv(peers []string) map[string]string { @@ -229,21 +248,29 @@ func generatePeersConfigEnv(peers []string) map[string]string { type writeLogger struct { prefix string w io.Writer + filter string } -func (l *writeLogger) Write(p []byte) (n int, err error) { - n, err = l.w.Write(p) - if err != nil { - fmt.Printf("%s %s: %v", l.prefix, p[0:n], err) - } else { - fmt.Printf("%s %s", l.prefix, p[0:n]) +func (l *writeLogger) Write(p []byte) (int, error) { + lines := bytes.SplitAfter(p, []byte("\n")) + for _, line := range lines { + s := string(line) + if strings.Contains(s, l.filter) || strings.TrimSpace(s) == "" { + continue + } + _, err := l.w.Write(p) + if err != nil { + fmt.Printf("%s %s: %v", l.prefix, s, err) + } else { + fmt.Printf("%s %s", l.prefix, s) + } } - return + return len(p), nil } // NewWriteLogger returns a writer that behaves like w except // that it logs (using fmt.Printf) each write to standard error, // printing the prefix and the string data written. -func NewWriteLogger(prefix string, w io.Writer) io.Writer { - return &writeLogger{prefix, w} +func NewWriteLogger(prefix string, w io.Writer, filter string) io.Writer { + return &writeLogger{prefix, w, filter} } diff --git a/main.go b/main.go index 52a7059..c2437ad 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,6 @@ import ( "net" "net/http" "os" - "strconv" "strings" ) @@ -150,30 +149,19 @@ func initRaft(listener net.Listener, clusterCfg conf.ClusterConfiguration, decla if err != nil { panic(err) // must never occured } - port := addr.Port - // TODO логика для определения порта пира, т.к всё тестируется на одной машине - //peerNumber := addr.Port - 9000 - //switch peerNumber { - //case 2: - // port = 9022 - //case 3: - // port = 9032 - //case 4: - // port = 9042 - //} - // - port2 := strconv.Itoa(port) - port2 = cfg.GrpcOuterAddress.Port - addressConfiguration := structure.AddressConfiguration{Port: port2, IP: addr.IP.String()} + port := cfg.GrpcOuterAddress.Port + addressConfiguration := structure.AddressConfiguration{Port: port, IP: addr.IP.String()} back := structure.BackendDeclaration{ModuleName: cfg.ModuleName, Address: addressConfiguration} service.ClusterMeshService.HandleDeleteBackendDeclarationCommand(back, s) }) }) holder.ClusterClient = clusterClient - err = r.BootstrapCluster() // err can be ignored - if err != nil { - log.Errorf(codes.BootstrapClusterError, "bootstrap cluster. %v", err) + if clusterCfg.BootstrapCluster { + err = r.BootstrapCluster() // err can be ignored + if err != nil { + log.Errorf(codes.BootstrapClusterError, "bootstrap cluster. %v", err) + } } return clusterClient, raftStore } @@ -194,17 +182,17 @@ func onShutdown(ctx context.Context, sig os.Signal) { backend.StopGrpcServer() holder.EtpServer.Close() - if err := holder.HttpServer.Shutdown(ctx); err != nil { - log.Warnf(codes.ShutdownHttpServerError, "http server shutdown err: %v", err) - } else { - log.Info(codes.ShutdownHttpServerInfo, "http server shutdown success") - } - if err := holder.ClusterClient.Shutdown(); err != nil { log.Warnf(codes.RaftShutdownError, "raft shutdown err: %v", err) } else { log.Info(codes.RaftShutdownInfo, "raft shutdown success") } + + if err := holder.HttpServer.Shutdown(ctx); err != nil { + log.Warnf(codes.ShutdownHttpServerError, "http server shutdown err: %v", err) + } else { + log.Info(codes.ShutdownHttpServerInfo, "http server shutdown success") + } } func getOutboundIp() (string, error) {