Skip to content

Commit f862283

Browse files
authored
support specification, and add circuitbreaker rule and faultdetect rule feature (polarismesh#888)
1 parent 3a23bdb commit f862283

File tree

294 files changed

+13447
-20487
lines changed

Some content is hidden

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

294 files changed

+13447
-20487
lines changed

.golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ linters:
8080

8181

8282
issues:
83+
max-issues-per-linter: 0
84+
max-same-issues: 0
8385
exclude-rules:
8486
# helpers in tests often (rightfully) pass a *testing.T as their first argument
8587
- path: _test\.go

apiserver/eurekaserver/applications.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import (
2727
"sync/atomic"
2828
"time"
2929

30-
api "github.com/polarismesh/polaris/common/api/v1"
30+
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
31+
3132
"github.com/polarismesh/polaris/common/model"
3233
"github.com/polarismesh/polaris/service"
3334
)
@@ -272,14 +273,14 @@ func (a *ApplicationsBuilder) buildDeltaApps(oldAppsCache *ApplicationsRespCache
272273
return constructResponseCache(newDeltaApps, instCount, true)
273274
}
274275

275-
func parseStatus(instance *api.Instance) string {
276+
func parseStatus(instance *apiservice.Instance) string {
276277
if instance.GetIsolate().GetValue() {
277278
return StatusOutOfService
278279
}
279280
return StatusUp
280281
}
281282

282-
func parsePortWrapper(info *InstanceInfo, instance *api.Instance) {
283+
func parsePortWrapper(info *InstanceInfo, instance *apiservice.Instance) {
283284
metadata := instance.GetMetadata()
284285
var securePortOk bool
285286
var securePortEnabledOk bool
@@ -346,7 +347,7 @@ func parsePortWrapper(info *InstanceInfo, instance *api.Instance) {
346347
}
347348
}
348349

349-
func parseLeaseInfo(leaseInfo *LeaseInfo, instance *api.Instance) {
350+
func parseLeaseInfo(leaseInfo *LeaseInfo, instance *apiservice.Instance) {
350351
var (
351352
metadata = instance.GetMetadata()
352353
durationInSec int
@@ -370,7 +371,7 @@ func parseLeaseInfo(leaseInfo *LeaseInfo, instance *api.Instance) {
370371
}
371372
}
372373

373-
func buildInstance(appName string, instance *api.Instance, lastModifyTime int64) *InstanceInfo {
374+
func buildInstance(appName string, instance *apiservice.Instance, lastModifyTime int64) *InstanceInfo {
374375
eurekaInstanceId := instance.GetId().GetValue()
375376
instanceInfo := &InstanceInfo{
376377
CountryId: DefaultCountryIdInt,
@@ -456,7 +457,7 @@ func buildInstance(appName string, instance *api.Instance, lastModifyTime int64)
456457
return instanceInfo
457458
}
458459

459-
func buildLocationInfo(instanceInfo *InstanceInfo, instance *api.Instance) {
460+
func buildLocationInfo(instanceInfo *InstanceInfo, instance *apiservice.Instance) {
460461
var region string
461462
var zone string
462463
var campus string

apiserver/eurekaserver/applications_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package eurekaserver
1919

2020
import (
21+
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
22+
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
2123
"strconv"
2224
"strings"
2325
"testing"
@@ -28,7 +30,6 @@ import (
2830
"github.com/google/uuid"
2931
"github.com/stretchr/testify/assert"
3032

31-
api "github.com/polarismesh/polaris/common/api/v1"
3233
"github.com/polarismesh/polaris/common/model"
3334
"github.com/polarismesh/polaris/service"
3435
)
@@ -72,7 +73,7 @@ func buildServices(count int, namespace string, services map[svcName]*model.Serv
7273

7374
func buildMockInstance(idx int, svc *model.Service, healthy bool, vipAddresses string, svipAddresses string) *model.Instance {
7475
instance := &model.Instance{
75-
Proto: &api.Instance{
76+
Proto: &apiservice.Instance{
7677
Id: &wrappers.StringValue{Value: uuid.NewString()},
7778
Service: &wrappers.StringValue{Value: svc.Name},
7879
Namespace: &wrappers.StringValue{Value: svc.Namespace},
@@ -82,12 +83,12 @@ func buildMockInstance(idx int, svc *model.Service, healthy bool, vipAddresses s
8283
Version: &wrappers.StringValue{Value: "1.0.0"},
8384
Weight: &wrappers.UInt32Value{Value: 100},
8485
EnableHealthCheck: &wrappers.BoolValue{Value: true},
85-
HealthCheck: &api.HealthCheck{Type: api.HealthCheck_HEARTBEAT, Heartbeat: &api.HeartbeatHealthCheck{
86+
HealthCheck: &apiservice.HealthCheck{Type: apiservice.HealthCheck_HEARTBEAT, Heartbeat: &apiservice.HeartbeatHealthCheck{
8687
Ttl: nil,
8788
}},
8889
Healthy: &wrappers.BoolValue{Value: healthy},
8990
Isolate: &wrappers.BoolValue{Value: false},
90-
Location: &api.Location{
91+
Location: &apimodel.Location{
9192
Region: &wrappers.StringValue{Value: "South China"},
9293
Zone: &wrappers.StringValue{Value: "ShangHai"},
9394
Campus: &wrappers.StringValue{Value: "CampusOne"},

apiserver/eurekaserver/replicate.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/emicklei/go-restful/v3"
2626
"github.com/golang/protobuf/ptypes/wrappers"
27+
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
2728

2829
api "github.com/polarismesh/polaris/common/api/v1"
2930
"github.com/polarismesh/polaris/common/model"
@@ -142,7 +143,7 @@ func (h *EurekaServer) dispatch(replicationInstance *ReplicationInstance,
142143
}
143144

144145
func eventToInstance(event *model.InstanceEvent, appName string, curTimeMilli int64) *InstanceInfo {
145-
instance := &api.Instance{
146+
instance := &apiservice.Instance{
146147
Id: &wrappers.StringValue{Value: event.Id},
147148
Host: &wrappers.StringValue{Value: event.Instance.GetHost().GetValue()},
148149
Port: &wrappers.UInt32Value{Value: event.Instance.GetPort().GetValue()},

apiserver/eurekaserver/write.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import (
2323
"strconv"
2424

2525
"github.com/golang/protobuf/ptypes/wrappers"
26+
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
27+
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
2628

2729
api "github.com/polarismesh/polaris/common/api/v1"
2830
)
2931

30-
func buildBaseInstance(instance *InstanceInfo, namespace string, appId string, replicated bool) *api.Instance {
31-
targetInstance := &api.Instance{}
32+
func buildBaseInstance(instance *InstanceInfo, namespace string, appId string, replicated bool) *apiservice.Instance {
33+
targetInstance := &apiservice.Instance{}
3234
eurekaMetadata := make(map[string]string)
3335

3436
eurekaMetadata[MetadataRegisterFrom] = ServerEureka
@@ -74,7 +76,7 @@ func buildBaseInstance(instance *InstanceInfo, namespace string, appId string, r
7476
targetInstance.Namespace = &wrappers.StringValue{Value: namespace}
7577
targetInstance.Host = &wrappers.StringValue{Value: instance.IpAddr}
7678
if instance.Metadata != nil && len(instance.Metadata.Meta) > 0 {
77-
targetInstance.Location = &api.Location{}
79+
targetInstance.Location = &apimodel.Location{}
7880
for k, v := range instance.Metadata.Meta {
7981
strValue := ObjectToString(v)
8082
switch k {
@@ -94,7 +96,7 @@ func buildBaseInstance(instance *InstanceInfo, namespace string, appId string, r
9496
return targetInstance
9597
}
9698

97-
func buildHealthCheck(instance *InstanceInfo, targetInstance *api.Instance, eurekaMetadata map[string]string) {
99+
func buildHealthCheck(instance *InstanceInfo, targetInstance *apiservice.Instance, eurekaMetadata map[string]string) {
98100
leaseInfo := instance.LeaseInfo
99101
var durationInSecs int
100102
var renewalIntervalInSecs int
@@ -118,13 +120,13 @@ func buildHealthCheck(instance *InstanceInfo, targetInstance *api.Instance, eure
118120
ttl := uint32(math.Min(durationMin, float64(renewalIntervalInSecs)))
119121

120122
targetInstance.EnableHealthCheck = &wrappers.BoolValue{Value: true}
121-
targetInstance.HealthCheck = &api.HealthCheck{
122-
Type: api.HealthCheck_HEARTBEAT,
123-
Heartbeat: &api.HeartbeatHealthCheck{Ttl: &wrappers.UInt32Value{Value: ttl}},
123+
targetInstance.HealthCheck = &apiservice.HealthCheck{
124+
Type: apiservice.HealthCheck_HEARTBEAT,
125+
Heartbeat: &apiservice.HeartbeatHealthCheck{Ttl: &wrappers.UInt32Value{Value: ttl}},
124126
}
125127
}
126128

127-
func buildStatus(instance *InstanceInfo, targetInstance *api.Instance) {
129+
func buildStatus(instance *InstanceInfo, targetInstance *apiservice.Instance) {
128130
// 由于eureka的实例都会自动报心跳,心跳由北极星接管,因此客户端报上来的人工状态OUT_OF_SERVICE,通过isolate来进行代替
129131
status := instance.Status
130132
if status == "OUT_OF_SERVICE" {
@@ -136,7 +138,8 @@ func buildStatus(instance *InstanceInfo, targetInstance *api.Instance) {
136138
}
137139
}
138140

139-
func convertEurekaInstance(instance *InstanceInfo, namespace string, appId string, replicated bool) *api.Instance {
141+
func convertEurekaInstance(
142+
instance *InstanceInfo, namespace string, appId string, replicated bool) *apiservice.Instance {
140143
var secureEnable bool
141144
var securePort int
142145
var insecureEnable bool
@@ -185,10 +188,10 @@ func (h *EurekaServer) registerInstances(ctx context.Context, appId string, inst
185188
}
186189
// 5. 如果报服务不存在,对服务进行注册
187190
if resp.Code.Value == api.NotFoundResource {
188-
svc := &api.Service{}
191+
svc := &apiservice.Service{}
189192
svc.Namespace = &wrappers.StringValue{Value: h.namespace}
190193
svc.Name = &wrappers.StringValue{Value: appId}
191-
svcResp := h.namingServer.CreateServices(ctx, []*api.Service{svc})
194+
svcResp := h.namingServer.CreateServices(ctx, []*apiservice.Service{svc})
192195
svcCreateCode := svcResp.GetCode().GetValue()
193196
if svcCreateCode != api.ExecuteSuccess && svcCreateCode != api.ExistedResource {
194197
return svcCreateCode
@@ -201,7 +204,7 @@ func (h *EurekaServer) registerInstances(ctx context.Context, appId string, inst
201204
}
202205

203206
func (h *EurekaServer) deregisterInstance(ctx context.Context, appId string, instanceId string) uint32 {
204-
resp := h.namingServer.DeregisterInstance(ctx, &api.Instance{Id: &wrappers.StringValue{Value: instanceId}})
207+
resp := h.namingServer.DeregisterInstance(ctx, &apiservice.Instance{Id: &wrappers.StringValue{Value: instanceId}})
205208
return resp.GetCode().GetValue()
206209
}
207210

@@ -211,12 +214,12 @@ func (h *EurekaServer) updateStatus(ctx context.Context, appId string, instanceI
211214
isolated = true
212215
}
213216
resp := h.namingServer.UpdateInstances(ctx,
214-
[]*api.Instance{{Id: &wrappers.StringValue{Value: instanceId}, Isolate: &wrappers.BoolValue{Value: isolated}}})
217+
[]*apiservice.Instance{{Id: &wrappers.StringValue{Value: instanceId}, Isolate: &wrappers.BoolValue{Value: isolated}}})
215218
return resp.GetCode().GetValue()
216219
}
217220

218221
func (h *EurekaServer) renew(ctx context.Context, appId string, instanceId string) uint32 {
219-
resp := h.healthCheckServer.Report(ctx, &api.Instance{Id: &wrappers.StringValue{Value: instanceId}})
222+
resp := h.healthCheckServer.Report(ctx, &apiservice.Instance{Id: &wrappers.StringValue{Value: instanceId}})
220223
code := resp.GetCode().GetValue()
221224

222225
// 如果目标实例存在,但是没有开启心跳,对于 eureka 来说,仍然属于心跳上报成功
@@ -229,6 +232,6 @@ func (h *EurekaServer) renew(ctx context.Context, appId string, instanceId strin
229232

230233
func (h *EurekaServer) updateMetadata(ctx context.Context, instanceId string, metadata map[string]string) uint32 {
231234
resp := h.namingServer.UpdateInstances(ctx,
232-
[]*api.Instance{{Id: &wrappers.StringValue{Value: instanceId}, Metadata: metadata}})
235+
[]*apiservice.Instance{{Id: &wrappers.StringValue{Value: instanceId}, Metadata: metadata}})
233236
return resp.GetCode().GetValue()
234237
}

apiserver/eurekaserver/write_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package eurekaserver
1919

2020
import (
2121
"context"
22+
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
2223
"testing"
2324
"time"
2425

@@ -36,14 +37,14 @@ func TestEurekaServer_renew(t *testing.T) {
3637

3738
ins := &model.Instance{
3839
ServiceID: utils.NewUUID(),
39-
Proto: &api.Instance{
40+
Proto: &apiservice.Instance{
4041
Service: utils.NewStringValue("echo"),
4142
Namespace: utils.NewStringValue("default"),
4243
Host: utils.NewStringValue("127.0.0.1"),
4344
Port: utils.NewUInt32Value(8080),
44-
HealthCheck: &api.HealthCheck{
45-
Type: api.HealthCheck_HEARTBEAT,
46-
Heartbeat: &api.HeartbeatHealthCheck{
45+
HealthCheck: &apiservice.HealthCheck{
46+
Type: apiservice.HealthCheck_HEARTBEAT,
47+
Heartbeat: &apiservice.HeartbeatHealthCheck{
4748
Ttl: &wrapperspb.UInt32Value{
4849
Value: 5,
4950
},
@@ -63,14 +64,14 @@ func TestEurekaServer_renew(t *testing.T) {
6364

6465
disableBeatIns := &model.Instance{
6566
ServiceID: utils.NewUUID(),
66-
Proto: &api.Instance{
67+
Proto: &apiservice.Instance{
6768
Service: utils.NewStringValue("echo"),
6869
Namespace: utils.NewStringValue("default"),
6970
Host: utils.NewStringValue("127.0.0.2"),
7071
Port: utils.NewUInt32Value(8081),
71-
HealthCheck: &api.HealthCheck{
72-
Type: api.HealthCheck_HEARTBEAT,
73-
Heartbeat: &api.HeartbeatHealthCheck{
72+
HealthCheck: &apiservice.HealthCheck{
73+
Type: apiservice.HealthCheck_HEARTBEAT,
74+
Heartbeat: &apiservice.HeartbeatHealthCheck{
7475
Ttl: &wrapperspb.UInt32Value{
7576
Value: 5,
7677
},

apiserver/grpcserver/base.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626
"time"
2727

28+
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
2829
"go.uber.org/zap"
2930
"google.golang.org/grpc"
3031
"google.golang.org/grpc/codes"
@@ -220,13 +221,13 @@ func (b *BaseGrpcServer) unaryInterceptor(ctx context.Context, req interface{},
220221

221222
// 判断是否允许访问
222223
if ok := b.AllowAccess(stream.Method); !ok {
223-
rsp = api.NewResponse(api.ClientAPINotOpen)
224+
rsp = api.NewResponse(apimodel.Code_ClientAPINotOpen)
224225
return
225226
}
226227

227228
// handler执行前,限流
228-
if code := b.EnterRatelimit(stream.ClientIP, stream.Method); code != api.ExecuteSuccess {
229-
rsp = api.NewResponse(code)
229+
if code := b.EnterRatelimit(stream.ClientIP, stream.Method); code != uint32(api.ExecuteSuccess) {
230+
rsp = api.NewResponse(apimodel.Code(code))
230231
return
231232
}
232233
rsp, err = handler(ctx, req)

apiserver/grpcserver/config/client_access.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@ package config
2020
import (
2121
"context"
2222

23+
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
24+
2325
"github.com/polarismesh/polaris/apiserver/grpcserver"
24-
api "github.com/polarismesh/polaris/common/api/v1"
2526
)
2627

2728
// GetConfigFile 拉取配置
2829
func (g *ConfigGRPCServer) GetConfigFile(ctx context.Context,
29-
configFile *api.ClientConfigFileInfo) (*api.ConfigClientResponse, error) {
30+
configFile *apiconfig.ClientConfigFileInfo) (*apiconfig.ConfigClientResponse, error) {
3031
ctx = grpcserver.ConvertContext(ctx)
3132
response := g.configServer.GetConfigFileForClient(ctx, configFile)
3233
return response, nil
3334
}
3435

3536
// WatchConfigFiles 订阅配置变更
3637
func (g *ConfigGRPCServer) WatchConfigFiles(ctx context.Context,
37-
request *api.ClientWatchConfigFileRequest) (*api.ConfigClientResponse, error) {
38+
request *apiconfig.ClientWatchConfigFileRequest) (*apiconfig.ConfigClientResponse, error) {
3839
ctx = grpcserver.ConvertContext(ctx)
3940

4041
// 阻塞等待响应

apiserver/grpcserver/config/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"fmt"
2323
"strings"
2424

25+
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
2526
"google.golang.org/grpc"
2627

2728
"github.com/polarismesh/polaris/apiserver"
2829
"github.com/polarismesh/polaris/apiserver/grpcserver"
2930
"github.com/polarismesh/polaris/bootstrap"
30-
api "github.com/polarismesh/polaris/common/api/v1"
3131
commonlog "github.com/polarismesh/polaris/common/log"
3232
"github.com/polarismesh/polaris/common/model"
3333
"github.com/polarismesh/polaris/config"
@@ -73,7 +73,7 @@ func (g *ConfigGRPCServer) Run(errCh chan error) {
7373
switch name {
7474
case "client":
7575
if apiConfig.Enable {
76-
api.RegisterPolarisConfigGRPCServer(server, g)
76+
apiconfig.RegisterPolarisConfigGRPCServer(server, g)
7777
openMethod, getErr := getConfigClientOpenMethod(g.GetProtocol())
7878
if getErr != nil {
7979
return getErr

0 commit comments

Comments
 (0)