Skip to content

Commit 9679e45

Browse files
authored
等待 apiserver 都启动完成,再自注册。 (#785)
* 等待 apiserver 都启动完成,再自注册。+ 格式化代码 等待 apiserver 都启动完成,再自注册。 1.完善代码规范 2.将cond.add 放到 apiserver的listen后。 完善代码规范 * import_format 格式化 * 添加Licensed * 改用waitgroup Co-authored-by: zhanglei25 <zhanglei25@tal.com>
1 parent db611ac commit 9679e45

File tree

12 files changed

+41
-22
lines changed

12 files changed

+41
-22
lines changed

apiserver/eurekaserver/eureka_suit_test.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ import (
3333
"github.com/polarismesh/polaris/common/utils"
3434
"github.com/polarismesh/polaris/namespace"
3535
"github.com/polarismesh/polaris/plugin"
36-
"github.com/polarismesh/polaris/service"
37-
"github.com/polarismesh/polaris/service/batch"
38-
"github.com/polarismesh/polaris/service/healthcheck"
39-
"github.com/polarismesh/polaris/store"
40-
storemock "github.com/polarismesh/polaris/store/mock"
41-
42-
"github.com/polarismesh/polaris/testdata"
43-
44-
// 注册相关默认插件
4536
_ "github.com/polarismesh/polaris/plugin/cmdb/memory"
4637
_ "github.com/polarismesh/polaris/plugin/discoverevent/local"
4738
_ "github.com/polarismesh/polaris/plugin/discoverstat/discoverlocal"
@@ -52,6 +43,12 @@ import (
5243
_ "github.com/polarismesh/polaris/plugin/ratelimit/lrurate"
5344
_ "github.com/polarismesh/polaris/plugin/ratelimit/token"
5445
_ "github.com/polarismesh/polaris/plugin/statis/local"
46+
"github.com/polarismesh/polaris/service"
47+
"github.com/polarismesh/polaris/service/batch"
48+
"github.com/polarismesh/polaris/service/healthcheck"
49+
"github.com/polarismesh/polaris/store"
50+
storemock "github.com/polarismesh/polaris/store/mock"
51+
"github.com/polarismesh/polaris/testdata"
5552
)
5653

5754
type Bootstrap struct {

apiserver/eurekaserver/server.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ import (
2727
"strings"
2828
"time"
2929

30-
"github.com/emicklei/go-restful/v3"
30+
restful "github.com/emicklei/go-restful/v3"
3131
"go.uber.org/zap"
3232

3333
"github.com/polarismesh/polaris/apiserver"
34+
"github.com/polarismesh/polaris/bootstrap"
3435
"github.com/polarismesh/polaris/common/connlimit"
3536
"github.com/polarismesh/polaris/common/secure"
3637
"github.com/polarismesh/polaris/common/utils"
@@ -253,6 +254,8 @@ func (h *EurekaServer) Run(errCh chan error) {
253254
errCh <- err
254255
return
255256
}
257+
bootstrap.ApiServerWaitGroup.Done()
258+
256259
ln = &tcpKeepAliveListener{ln.(*net.TCPListener)}
257260
// 开启最大连接数限制
258261
if h.connLimitConfig != nil && h.connLimitConfig.OpenConnLimit {

apiserver/grpcserver/config/server.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/polarismesh/polaris/apiserver"
2828
"github.com/polarismesh/polaris/apiserver/grpcserver"
29+
"github.com/polarismesh/polaris/bootstrap"
2930
api "github.com/polarismesh/polaris/common/api/v1"
3031
commonlog "github.com/polarismesh/polaris/common/log"
3132
"github.com/polarismesh/polaris/config"
@@ -65,6 +66,7 @@ func (g *ConfigGRPCServer) Initialize(ctx context.Context, option map[string]int
6566
// Run 启动GRPC API服务器
6667
func (g *ConfigGRPCServer) Run(errCh chan error) {
6768
g.BaseGrpcServer.Run(errCh, g.GetProtocol(), func(server *grpc.Server) error {
69+
defer bootstrap.ApiServerWaitGroup.Done()
6870
for name, apiConfig := range g.openAPI {
6971
switch name {
7072
case "client":

apiserver/grpcserver/discover/server.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/polarismesh/polaris/apiserver/grpcserver"
2929
v1 "github.com/polarismesh/polaris/apiserver/grpcserver/discover/v1"
3030
v2 "github.com/polarismesh/polaris/apiserver/grpcserver/discover/v2"
31+
"github.com/polarismesh/polaris/bootstrap"
3132
apiv1 "github.com/polarismesh/polaris/common/api/v1"
3233
apiv2 "github.com/polarismesh/polaris/common/api/v2"
3334
commonlog "github.com/polarismesh/polaris/common/log"
@@ -106,6 +107,7 @@ func (g *GRPCServer) Initialize(ctx context.Context, option map[string]interface
106107
// Run 启动GRPC API服务器
107108
func (g *GRPCServer) Run(errCh chan error) {
108109
g.BaseGrpcServer.Run(errCh, g.GetProtocol(), func(server *grpc.Server) error {
110+
defer bootstrap.ApiServerWaitGroup.Done()
109111
for name, config := range g.openAPI {
110112
switch name {
111113
case "client":

apiserver/httpserver/server.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"strings"
2727
"time"
2828

29-
"github.com/emicklei/go-restful/v3"
29+
restful "github.com/emicklei/go-restful/v3"
3030
"github.com/pkg/errors"
3131
"go.uber.org/zap"
3232

@@ -35,6 +35,7 @@ import (
3535
v1 "github.com/polarismesh/polaris/apiserver/httpserver/v1"
3636
v2 "github.com/polarismesh/polaris/apiserver/httpserver/v2"
3737
"github.com/polarismesh/polaris/auth"
38+
"github.com/polarismesh/polaris/bootstrap"
3839
api "github.com/polarismesh/polaris/common/api/v1"
3940
"github.com/polarismesh/polaris/common/connlimit"
4041
commonlog "github.com/polarismesh/polaris/common/log"
@@ -220,6 +221,7 @@ func (h *HTTPServer) Run(errCh chan error) {
220221
errCh <- err
221222
return
222223
}
224+
bootstrap.ApiServerWaitGroup.Done()
223225

224226
ln = &tcpKeepAliveListener{ln.(*net.TCPListener)}
225227
// 开启最大连接数限制
@@ -318,7 +320,7 @@ func (h *HTTPServer) createRestfulContainer() (*restful.Container, error) {
318320
Container: wsContainer}
319321
wsContainer.Filter(cors.Filter)
320322

321-
// Add container filter to respond to OPTIONS
323+
// Incr container filter to respond to OPTIONS
322324
wsContainer.Filter(wsContainer.OPTIONSFilter)
323325

324326
wsContainer.Filter(h.process)

apiserver/l5pbserver/server.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"go.uber.org/zap"
2727

2828
"github.com/polarismesh/polaris/apiserver"
29+
"github.com/polarismesh/polaris/bootstrap"
2930
"github.com/polarismesh/polaris/common/api/l5"
3031
"github.com/polarismesh/polaris/plugin"
3132
"github.com/polarismesh/polaris/service"
@@ -105,6 +106,7 @@ func (l *L5pbserver) Run(errCh chan error) {
105106
errCh <- err
106107
return
107108
}
109+
bootstrap.ApiServerWaitGroup.Done()
108110
l.listener = listener
109111

110112
for {

apiserver/prometheussd/server.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
"strings"
2626
"time"
2727

28-
"github.com/emicklei/go-restful/v3"
28+
restful "github.com/emicklei/go-restful/v3"
2929
"go.uber.org/zap"
3030

3131
"github.com/polarismesh/polaris/apiserver"
32+
"github.com/polarismesh/polaris/bootstrap"
3233
"github.com/polarismesh/polaris/common/connlimit"
3334
"github.com/polarismesh/polaris/common/log"
3435
"github.com/polarismesh/polaris/common/utils"
@@ -115,6 +116,7 @@ func (h *PrometheusServer) Run(errCh chan error) {
115116
errCh <- err
116117
return
117118
}
119+
bootstrap.ApiServerWaitGroup.Done()
118120

119121
ln = &tcpKeepAliveListener{ln.(*net.TCPListener)}
120122
// 开启最大连接数限制
@@ -207,7 +209,7 @@ func (h *PrometheusServer) createRestfulContainer() (*restful.Container, error)
207209
Container: wsContainer}
208210
wsContainer.Filter(cors.Filter)
209211

210-
// Add container filter to respond to OPTIONS
212+
// Incr container filter to respond to OPTIONS
211213
wsContainer.Filter(wsContainer.OPTIONSFilter)
212214

213215
wsContainer.Filter(h.process)

apiserver/xdsserverv3/server.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
envoy_type_v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
4545
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
4646
cachev3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
47-
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
47+
resource "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
4848
serverv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
4949
"github.com/golang/protobuf/ptypes"
5050
_struct "github.com/golang/protobuf/ptypes/struct"
@@ -54,6 +54,7 @@ import (
5454
"google.golang.org/protobuf/types/known/anypb"
5555

5656
"github.com/polarismesh/polaris/apiserver"
57+
"github.com/polarismesh/polaris/bootstrap"
5758
"github.com/polarismesh/polaris/cache"
5859
api "github.com/polarismesh/polaris/common/api/v1"
5960
"github.com/polarismesh/polaris/common/connlimit"
@@ -160,6 +161,7 @@ func (x *XDSServer) Run(errCh chan error) {
160161
errCh <- err
161162
return
162163
}
164+
bootstrap.ApiServerWaitGroup.Done()
163165

164166
if x.connLimitConfig != nil && x.connLimitConfig.OpenConnLimit {
165167
log.Infof("grpc server use max connection limit: %d, grpc max limit: %d",

bootstrap/server.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323
"fmt"
2424
"net"
2525
"strings"
26+
"sync"
2627
"time"
2728

2829
"github.com/golang/protobuf/ptypes/wrappers"
29-
"gopkg.in/yaml.v2"
30+
yaml "gopkg.in/yaml.v2"
3031

3132
"github.com/polarismesh/polaris/apiserver"
3233
"github.com/polarismesh/polaris/auth"
@@ -52,6 +53,7 @@ var (
5253
SelfServiceInstance = make([]*api.Instance, 0)
5354
ConfigFilePath = ""
5455
selfHeathChecker *SelfHeathChecker
56+
ApiServerWaitGroup = new(sync.WaitGroup)
5557
)
5658

5759
// Start 启动
@@ -292,6 +294,9 @@ func StartServers(ctx context.Context, cfg *boot_config.Config, errCh chan error
292294
[]apiserver.Apiserver, error) {
293295
// 启动API服务器
294296
var servers []apiserver.Apiserver
297+
298+
// 等待所有ApiServer都监听完成
299+
295300
for _, protocol := range cfg.APIServers {
296301
slot, exist := apiserver.Slots[protocol.Name]
297302
if !exist {
@@ -306,9 +311,10 @@ func StartServers(ctx context.Context, cfg *boot_config.Config, errCh chan error
306311
}
307312

308313
servers = append(servers, slot)
314+
ApiServerWaitGroup.Add(1)
309315
go slot.Run(errCh)
310316
}
311-
317+
ApiServerWaitGroup.Wait()
312318
return servers, nil
313319
}
314320

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
package main
1919

2020
import (
21-
"github.com/polarismesh/polaris/cmd"
22-
2321
_ "go.uber.org/automaxprocs"
22+
23+
"github.com/polarismesh/polaris/cmd"
2424
)
2525

2626
func main() {

plugin/whitelist/ip_whitelist_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
"errors"
2222
"testing"
2323

24-
"github.com/polarismesh/polaris/plugin"
2524
"github.com/stretchr/testify/assert"
25+
26+
"github.com/polarismesh/polaris/plugin"
2627
)
2728

2829
func Test_ipWhitelist_Name(t *testing.T) {

store/sqldb/default.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"errors"
2222
"fmt"
2323

24+
_ "github.com/go-sql-driver/mysql"
25+
2426
"github.com/polarismesh/polaris/plugin"
2527
"github.com/polarismesh/polaris/store"
26-
27-
_ "github.com/go-sql-driver/mysql"
2828
)
2929

3030
const (

0 commit comments

Comments
 (0)