Skip to content

Commit 08f88c4

Browse files
authored
Merge pull request #128 from andrewshan/main
#104: fix health status not changed in some cases
2 parents 0ed386d + 4ed3cf6 commit 08f88c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2373
-1657
lines changed

.github/workflows/testing.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
pushd ./naming/cache
4242
go test -v
4343
popd
44-
pushd ./plugin/ratelimit/tokenbucket
44+
pushd ./plugin/ratelimit/token
4545
go test -v
4646
popd
4747
pushd ./store/sqldb

apiserver/grpcserver/client_access.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (g *GRPCServer) Discover(server api.PolarisGRPC_DiscoverServer) error {
138138
* @brief 上报心跳
139139
*/
140140
func (g *GRPCServer) Heartbeat(ctx context.Context, in *api.Instance) (*api.Response, error) {
141-
out := g.namingServer.Heartbeat(convertContext(ctx), in)
141+
out := g.healthCheckServer.Report(convertContext(ctx), in)
142142
return out, nil
143143
}
144144

apiserver/grpcserver/server.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package grpcserver
2020
import (
2121
"context"
2222
"fmt"
23+
"github.com/polarismesh/polaris-server/healthcheck"
2324
"io"
2425
"net"
2526
"net/http"
@@ -49,32 +50,27 @@ type GRPCServer struct {
4950
restart bool
5051
exitCh chan struct{}
5152

52-
server *grpc.Server
53-
namingServer *naming.Server
54-
statis plugin.Statis
55-
ratelimit plugin.Ratelimit
53+
server *grpc.Server
54+
namingServer *naming.Server
55+
healthCheckServer *healthcheck.Server
56+
statis plugin.Statis
57+
ratelimit plugin.Ratelimit
5658

5759
openAPI map[string]apiserver.APIConfig
5860
openMethod map[string]bool
5961
}
6062

61-
/**
62-
* @brief 获取端口
63-
*/
63+
// GetPort 获取端口
6464
func (g *GRPCServer) GetPort() uint32 {
6565
return g.listenPort
6666
}
6767

68-
/**
69-
* @brief 获取Server的协议
70-
*/
68+
// GetProtocol 获取Server的协议
7169
func (g *GRPCServer) GetProtocol() string {
7270
return "grpc"
7371
}
7472

75-
/**
76-
* Initialize 初始化GRPC API服务器
77-
*/
73+
// Initialize 初始化GRPC API服务器
7874
func (g *GRPCServer) Initialize(_ context.Context, option map[string]interface{},
7975
api map[string]apiserver.APIConfig) error {
8076
g.listenIP = option["listenIP"].(string)
@@ -110,6 +106,12 @@ func (g *GRPCServer) Run(errCh chan error) {
110106

111107
var err error
112108
// 引入功能模块和插件
109+
g.healthCheckServer, err = healthcheck.GetServer()
110+
if err != nil {
111+
log.Errorf("%v", err)
112+
errCh <- err
113+
return
114+
}
113115
g.namingServer, err = naming.GetServer()
114116
if err != nil {
115117
log.Errorf("%v", err)

apiserver/httpserver/client_access.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,5 @@ func (h *HTTPServer) Heartbeat(req *restful.Request, rsp *restful.Response) {
184184
return
185185
}
186186

187-
handler.WriteHeaderAndProto(h.namingServer.Heartbeat(ctx, instance))
187+
handler.WriteHeaderAndProto(h.healthCheckServer.Report(ctx, instance))
188188
}

apiserver/httpserver/console_access.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ package httpserver
2020
import (
2121
"context"
2222
"fmt"
23+
proto "github.com/golang/protobuf/proto"
24+
"github.com/polarismesh/polaris-server/common/log"
2325
"net/http"
2426

2527
"github.com/emicklei/go-restful"
26-
"github.com/golang/protobuf/proto"
2728
api "github.com/polarismesh/polaris-server/common/api/v1"
28-
"github.com/polarismesh/polaris-server/common/log"
2929
"github.com/polarismesh/polaris-server/common/utils"
3030
)
3131

@@ -34,9 +34,7 @@ const (
3434
defaultAccess string = "default"
3535
)
3636

37-
/**
38-
* GetConsoleAccessServer 注册管理端接口
39-
*/
37+
// GetConsoleAccessServer 注册管理端接口
4038
func (h *HTTPServer) GetConsoleAccessServer(include []string) (*restful.WebService, error) {
4139
consoleAccess := []string{defaultAccess}
4240

@@ -72,9 +70,7 @@ func (h *HTTPServer) GetConsoleAccessServer(include []string) (*restful.WebServi
7270
return ws, nil
7371
}
7472

75-
/**
76-
* addDefaultReadAccess 增加默认读接口
77-
*/
73+
// addDefaultReadAccess 增加默认读接口
7874
func (h *HTTPServer) addDefaultReadAccess(ws *restful.WebService) {
7975
// 管理端接口:只包含读接口
8076
ws.Route(ws.GET("/namespaces").To(h.GetNamespaces))
@@ -107,9 +103,7 @@ func (h *HTTPServer) addDefaultReadAccess(ws *restful.WebService) {
107103
ws.Route(ws.GET("/platform/token").To(h.GetPlatformToken))
108104
}
109105

110-
/**
111-
* addDefaultAccess 增加默认接口
112-
*/
106+
// addDefaultAccess 增加默认接口
113107
func (h *HTTPServer) addDefaultAccess(ws *restful.WebService) {
114108
// 管理端接口:增删改查请求全部操作存储层
115109
ws.Route(ws.POST("/namespaces").To(h.CreateNamespaces))

apiserver/httpserver/handler.go

+14-21
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,13 @@ import (
3333
"go.uber.org/zap"
3434
)
3535

36-
/**
37-
* Handler HTTP请求/回复处理器
38-
*/
36+
// Handler HTTP请求/回复处理器
3937
type Handler struct {
4038
*restful.Request
4139
*restful.Response
4240
}
4341

44-
/**
45-
* Parse 解析请求
46-
*/
47-
func (h *Handler) Parse(message proto.Message) (context.Context, error) {
48-
requestID := h.Request.HeaderParameter("Request-Id")
49-
if err := jsonpb.Unmarshal(h.Request.Request.Body, message); err != nil {
50-
log.Error(err.Error(), zap.String("request-id", requestID))
51-
return nil, err
52-
}
53-
return h.postParseMessage(requestID)
54-
}
55-
42+
// ParseArray 解析PB数组对象
5643
func (h *Handler) ParseArray(createMessage func() proto.Message) (context.Context, error) {
5744
requestID := h.Request.HeaderParameter("Request-Id")
5845

@@ -102,9 +89,17 @@ func (h *Handler) postParseMessage(requestID string) (context.Context, error) {
10289
return ctx, nil
10390
}
10491

105-
/**
106-
* WriteHeader 仅返回Code
107-
*/
92+
// Parse 解析请求
93+
func (h *Handler) Parse(message proto.Message) (context.Context, error) {
94+
requestID := h.Request.HeaderParameter("Request-Id")
95+
if err := jsonpb.Unmarshal(h.Request.Request.Body, message); err != nil {
96+
log.Error(err.Error(), zap.String("request-id", requestID))
97+
return nil, err
98+
}
99+
return h.postParseMessage(requestID)
100+
}
101+
102+
// WriteHeader 仅返回Code
108103
func (h *Handler) WriteHeader(polarisCode uint32, httpStatus int) {
109104
requestID := h.Request.HeaderParameter(utils.PolarisRequestID)
110105
h.Request.SetAttribute(utils.PolarisCode, polarisCode) // api统计的时候,用该code
@@ -118,9 +113,7 @@ func (h *Handler) WriteHeader(polarisCode uint32, httpStatus int) {
118113
h.Response.WriteHeader(httpStatus)
119114
}
120115

121-
/**
122-
* WriteHeaderAndProto 返回Code和Proto
123-
*/
116+
// WriteHeaderAndProto 返回Code和Proto
124117
func (h *Handler) WriteHeaderAndProto(obj api.ResponseMessage) {
125118
requestID := h.Request.HeaderParameter(utils.PolarisRequestID)
126119
h.Request.SetAttribute(utils.PolarisCode, obj.GetCode().GetValue())

apiserver/httpserver/maintain_access.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (h *HTTPServer) GetLastHeartbeat(req *restful.Request, rsp *restful.Respons
215215
instance := &api.Instance{}
216216
if id, ok := params["id"]; ok && id != "" {
217217
instance.Id = utils.NewStringValue(id)
218-
ret := h.namingServer.GetLastHeartbeat(instance)
218+
ret := h.healthCheckServer.GetLastHeartbeat(instance)
219219
handler.WriteHeaderAndProto(ret)
220220
return
221221
}
@@ -227,6 +227,6 @@ func (h *HTTPServer) GetLastHeartbeat(req *restful.Request, rsp *restful.Respons
227227
port, _ := strconv.Atoi(params["port"])
228228
instance.Port = utils.NewUInt32Value(uint32(port))
229229

230-
ret := h.namingServer.GetLastHeartbeat(instance)
230+
ret := h.healthCheckServer.GetLastHeartbeat(instance)
231231
handler.WriteHeaderAndProto(ret)
232232
}

apiserver/httpserver/server.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package httpserver
2020
import (
2121
"context"
2222
"fmt"
23+
"github.com/polarismesh/polaris-server/healthcheck"
2324
"net"
2425
"net/http"
2526
"net/http/pprof"
@@ -58,11 +59,12 @@ type HTTPServer struct {
5859

5960
freeMemMu *sync.Mutex
6061

61-
server *http.Server
62-
namingServer *naming.Server
63-
rateLimit plugin.Ratelimit
64-
statis plugin.Statis
65-
auth plugin.Auth
62+
server *http.Server
63+
namingServer *naming.Server
64+
healthCheckServer *healthcheck.Server
65+
rateLimit plugin.Ratelimit
66+
statis plugin.Statis
67+
auth plugin.Auth
6668
}
6769

6870
const (
@@ -137,6 +139,12 @@ func (h *HTTPServer) Run(errCh chan error) {
137139
errCh <- err
138140
return
139141
}
142+
h.healthCheckServer, err = healthcheck.GetServer()
143+
if err != nil {
144+
log.Errorf("%v", err)
145+
errCh <- err
146+
return
147+
}
140148
h.statis = plugin.GetStatis()
141149

142150
// 初始化http server
@@ -159,7 +167,6 @@ func (h *HTTPServer) Run(errCh chan error) {
159167
}
160168

161169
ln = &tcpKeepAliveListener{ln.(*net.TCPListener)}
162-
163170
// 开启最大连接数限制
164171
if h.connLimitConfig != nil && h.connLimitConfig.OpenConnLimit {
165172
log.Infof("http server use max connection limit per ip: %d, http max limit: %d",

bootstrap/server.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"github.com/polarismesh/polaris-server/common/model"
25+
"github.com/polarismesh/polaris-server/healthcheck"
2426
"net"
2527
"strings"
2628
"time"
@@ -105,15 +107,11 @@ func Start(configFilePath string) {
105107
fmt.Printf("[ERROR] %v\n", err)
106108
return
107109
}
108-
109-
cfg.Naming.HealthCheck.LocalHost = LocalHost // 补充healthCheck的配置
110-
naming.SetHealthCheckConfig(&cfg.Naming.HealthCheck)
111-
err = naming.Initialize(ctx, &cfg.Naming, &cfg.Cache)
110+
err = StartComponents(ctx, cfg)
112111
if err != nil {
113112
fmt.Printf("[ERROR] %v\n", err)
114113
return
115114
}
116-
117115
errCh := make(chan error, len(cfg.APIServers))
118116
servers, err := StartServers(ctx, cfg, errCh)
119117
if err != nil {
@@ -130,7 +128,32 @@ func Start(configFilePath string) {
130128
RunMainLoop(servers, errCh)
131129
}
132130

133-
// 启动server
131+
// StartComponents start healthcheck and naming components
132+
func StartComponents(ctx context.Context, cfg *config.Config) error {
133+
var err error
134+
if len(cfg.HealthChecks.LocalHost) == 0 {
135+
cfg.HealthChecks.LocalHost = LocalHost // 补充healthCheck的配置
136+
}
137+
err = healthcheck.Initialize(ctx, &cfg.HealthChecks, cfg.Cache.Open)
138+
if err != nil {
139+
return err
140+
}
141+
healthCheckServer, err := healthcheck.GetServer()
142+
if err != nil {
143+
return err
144+
}
145+
cacheProvider, err := healthCheckServer.CacheProvider()
146+
if err != nil {
147+
return err
148+
}
149+
err = naming.Initialize(ctx, &cfg.Naming, &cfg.Cache, cacheProvider)
150+
if err != nil {
151+
return err
152+
}
153+
return nil
154+
}
155+
156+
// StartServers 启动server
134157
func StartServers(ctx context.Context, cfg *config.Config, errCh chan error) (
135158
[]apiserver.Apiserver, error) {
136159
// 启动API服务器
@@ -362,7 +385,8 @@ func selfRegister(host string, port uint32, protocol string, isolated bool, pola
362385
Version: utils.NewStringValue(version.Get()),
363386
Isolate: utils.NewBoolValue(isolated), // 自注册,默认是隔离的
364387
Metadata: map[string]string{
365-
"build-revision": version.GetRevision(),
388+
model.MetaKeyBuildRevision: version.GetRevision(),
389+
model.MetaKeyPolarisService: name,
366390
},
367391
}
368392

common/api/v1/codeinfo.go

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const (
6161
HealthCheckNotOpen = 400140
6262
HeartbeatOnDisabledIns = 400141
6363
HeartbeatExceedLimit = 400142
64+
HeartbeatTypeNotFound = 400143
6465
InvalidMetadata = 400150
6566
InvalidRateLimitID = 400151
6667
InvalidRateLimitLabels = 400152

common/model/metadata.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package model
19+
20+
const (
21+
// MetaKeyPolarisService service identifier by self registration
22+
MetaKeyPolarisService = "polaris_service"
23+
24+
// MetaKeyBuildRevision build revision for server
25+
MetaKeyBuildRevision = "build-revision"
26+
)

0 commit comments

Comments
 (0)