Skip to content

Commit efbb614

Browse files
authored
refactor:配置文件缓存逻辑重构 (polarismesh#1202)
1 parent 70d69ce commit efbb614

File tree

182 files changed

+7436
-7564
lines changed

Some content is hidden

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

182 files changed

+7436
-7564
lines changed

.github/workflows/codecov.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,22 @@ jobs:
7575
- name: Vert check
7676
run: bash vert.sh -install && bash vert.sh
7777

78-
- name: Go Test
78+
- name: Standalone Test
7979
env:
8080
MYSQL_DB_USER: root
8181
MYSQL_DB_PWD: root
82+
RUN_MODE: STANDALONE
83+
run: |
84+
# bash coverage.sh
85+
export MYSQL_DB_USER=${{ env.MYSQL_DB_USER }}
86+
export MYSQL_DB_PWD=${{ env.MYSQL_DB_PWD }}
87+
bash test/codecov.sh
88+
89+
- name: Cluster Test
90+
env:
91+
MYSQL_DB_USER: root
92+
MYSQL_DB_PWD: root
93+
RUN_MODE: CLUSTER
8294
run: |
8395
# bash coverage.sh
8496
export MYSQL_DB_USER=${{ env.MYSQL_DB_USER }}

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ linters-settings:
189189
# Max line length, lines longer will be reported.
190190
# '\t' is counted as 1 character by default, and can be changed with the tab-width option.
191191
# Default: 120.
192-
line-length: 120
192+
line-length: 140
193193
# Tab width in spaces.
194194
# Default: 1
195195
tab-width: 1

README-zh.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
README:
1616

17-
- [介绍](#介绍)
18-
- [如何安装](#如何安装)
19-
- [如何开发服务](#如何开发服务)
20-
- [如何集成服务网关](#如何集成服务网关)
21-
- [交流群](#交流群)
17+
- [北极星:服务发现和治理](#北极星服务发现和治理)
18+
- [介绍](#介绍)
19+
- [如何安装](#如何安装)
20+
- [如何开发服务](#如何开发服务)
21+
- [如何集成服务网关](#如何集成服务网关)
22+
- [交流群](#交流群)
2223

2324
更多文档请查看[北极星官网](https://polarismesh.cn)
2425

@@ -93,4 +94,4 @@ README:
9394

9495
扫码二维码,加入北极星开源交流群。欢迎用户反馈使用问题和优化建议。
9596

96-
<img src="https://main.qcloudimg.com/raw/bff4285d70498058caa212805b83a620.jpg" width="20%" height="20%" />
97+
<img src="./qrcode.png" width="20%" height="20%" />

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ English | [简体中文](./README-zh.md)
1414

1515
README:
1616

17-
- [Introduction](#introduction)
18-
- [How to install](#how-to-install)
19-
- [How to develop service](#how-to-develop-service)
20-
- [How to integrate service gateway](#how-to-integrate-service-gateway)
21-
- [Chat group](#chat-group)
17+
- [Polaris: Service Discovery and Governance](#polaris-service-discovery-and-governance)
18+
- [Introduction](#introduction)
19+
- [How to install](#how-to-install)
20+
- [How to develop service](#how-to-develop-service)
21+
- [How to integrate service gateway](#how-to-integrate-service-gateway)
22+
- [Chat group](#chat-group)
2223

2324
Visit [Website](https://polarismesh.cn/) to learn more
2425

@@ -93,4 +94,4 @@ You can integrate service gateways with Polaris service discovery and governance
9394

9495
Please scan the QR code to join the chat group.
9596

96-
<img src="https://main.qcloudimg.com/raw/bff4285d70498058caa212805b83a620.jpg" width="20%" height="20%" />
97+
<img src="./qrcode.png" width="20%" height="20%" />

apiserver/apiserver.go

-33
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ package apiserver
2020
import (
2121
"context"
2222
"fmt"
23-
"strings"
24-
25-
"github.com/polarismesh/polaris/common/log"
2623
)
2724

2825
const (
@@ -75,33 +72,3 @@ func Register(name string, server Apiserver) error {
7572

7673
return nil
7774
}
78-
79-
// GetClientOpenMethod 获取客户端openMethod
80-
func GetClientOpenMethod(include []string, protocol string) (map[string]bool, error) {
81-
clientAccess := make(map[string][]string)
82-
clientAccess[DiscoverAccess] = []string{"Discover", "ReportClient"}
83-
clientAccess[RegisterAccess] = []string{"RegisterInstance", "DeregisterInstance"}
84-
clientAccess[HealthcheckAccess] = []string{"Heartbeat", "BatchHeartbeat", "BatchGetHeartbeat", "BatchDelHeartbeat"}
85-
86-
openMethod := make(map[string]bool)
87-
// 如果为空,开启全部接口
88-
if len(include) == 0 {
89-
for key := range clientAccess {
90-
include = append(include, key)
91-
}
92-
}
93-
94-
for _, item := range include {
95-
if methods, ok := clientAccess[item]; ok {
96-
for _, method := range methods {
97-
method = "/v1.Polaris" + strings.ToUpper(protocol) + "/" + method
98-
openMethod[method] = true
99-
}
100-
} else {
101-
log.Errorf("method %s does not exist in %sserver client access", item, protocol)
102-
return nil, fmt.Errorf("method %s does not exist in %sserver client access", item, protocol)
103-
}
104-
}
105-
106-
return openMethod, nil
107-
}

apiserver/apiserver_test.go

-77
This file was deleted.

apiserver/eurekaserver/eureka_suit_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/polarismesh/polaris/auth"
3232
"github.com/polarismesh/polaris/cache"
33+
cachetypes "github.com/polarismesh/polaris/cache/api"
3334
commonlog "github.com/polarismesh/polaris/common/log"
3435
"github.com/polarismesh/polaris/common/utils"
3536
"github.com/polarismesh/polaris/namespace"
@@ -76,6 +77,7 @@ type EurekaTestSuit struct {
7677
updateCacheInterval time.Duration
7778
cancel context.CancelFunc
7879
storage store.Store
80+
cacheMgr *cache.CacheManager
7981
}
8082

8183
type options func(cfg *TestConfig)
@@ -117,6 +119,7 @@ func (d *EurekaTestSuit) initialize(t *testing.T, callback func(t *testing.T, s
117119
if err != nil {
118120
return err
119121
}
122+
d.cacheMgr = cacheMgn
120123

121124
// 批量控制器
122125
namingBatchConfig, err := batch.ParseBatchConfig(d.cfg.Naming.Batch)
@@ -158,8 +161,8 @@ func (d *EurekaTestSuit) initialize(t *testing.T, callback func(t *testing.T, s
158161
healthCheckServer.SetInstanceCache(cacheMgn.Instance())
159162

160163
// 为 instance 的 cache 添加 健康检查的 Listener
161-
cacheMgn.AddListener(cache.CacheNameInstance, []cache.Listener{cacheProvider})
162-
cacheMgn.AddListener(cache.CacheNameClient, []cache.Listener{cacheProvider})
164+
cacheMgn.AddListener(cachetypes.CacheInstance, []cachetypes.Listener{cacheProvider})
165+
cacheMgn.AddListener(cachetypes.CacheClient, []cachetypes.Listener{cacheProvider})
163166

164167
d.healthSvr = healthCheckServer
165168
time.Sleep(5 * time.Second)
@@ -201,11 +204,6 @@ func replaceEnv(configContent string) string {
201204

202205
func (d *EurekaTestSuit) Destroy() {
203206
d.cancel()
204-
time.Sleep(5 * time.Second)
205-
207+
d.cacheMgr.Close()
206208
d.storage.Destroy()
207-
time.Sleep(5 * time.Second)
208-
209-
healthcheck.TestDestroy()
210-
time.Sleep(5 * time.Second)
211209
}

apiserver/eurekaserver/model.go

-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ type InstanceInfo struct {
286286

287287
LeaseInfo *LeaseInfo `json:"leaseInfo" xml:"leaseInfo,omitempty"`
288288

289-
//nolint:lll
290289
IsCoordinatingDiscoveryServer interface{} `json:"isCoordinatingDiscoveryServer" xml:"isCoordinatingDiscoveryServer,omitempty"`
291290

292291
Metadata *Metadata `json:"metadata" xml:"metadata"`

apiserver/eurekaserver/server.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ type EurekaServer struct {
151151

152152
replicatePeers map[string][]string
153153
generateUniqueInstId bool
154+
subCtxs []*eventhub.SubscribtionContext
154155
}
155156

156157
// GetPort 获取端口
@@ -178,6 +179,7 @@ func (h *EurekaServer) Initialize(ctx context.Context, option map[string]interfa
178179
}
179180
h.option = option
180181
h.openAPI = api
182+
h.subCtxs = make([]*eventhub.SubscribtionContext, 0, 4)
181183

182184
var namespace = DefaultNamespace
183185
if namespaceValue, ok := option[optionNamespace]; ok {
@@ -341,11 +343,12 @@ func (h *EurekaServer) Run(errCh chan error) {
341343
if len(h.replicatePeers) > 0 {
342344
h.eventHandlerHandler = &EurekaInstanceEventHandler{
343345
BaseInstanceEventHandler: service.NewBaseInstanceEventHandler(h.namingServer), svr: h}
344-
if err = eventhub.Subscribe(
345-
eventhub.InstanceEventTopic, "eureka-replication", h.eventHandlerHandler); nil != err {
346+
subCtx, err := eventhub.Subscribe(eventhub.InstanceEventTopic, h.eventHandlerHandler)
347+
if err != nil {
346348
errCh <- err
347349
return
348350
}
351+
h.subCtxs = append(h.subCtxs, subCtx)
349352
}
350353
h.workers = NewApplicationsWorkers(h.refreshInterval, h.deltaExpireInterval, h.enableSelfPreservation,
351354
h.namingServer, h.healthCheckServer, h.namespace)

apiserver/grpcserver/base_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
"google.golang.org/grpc/metadata"
2626

27+
"github.com/polarismesh/polaris/apiserver"
28+
grpchelp "github.com/polarismesh/polaris/apiserver/grpcserver/utils"
2729
"github.com/polarismesh/polaris/common/utils"
2830
)
2931

@@ -120,3 +122,57 @@ func TestConvertContext(t *testing.T) {
120122
})
121123
}
122124
}
125+
126+
func TestGetClientOpenMethod(t *testing.T) {
127+
type args struct {
128+
include []string
129+
protocol string
130+
}
131+
tests := []struct {
132+
name string
133+
args args
134+
want map[string]bool
135+
wantErr bool
136+
}{
137+
{
138+
name: "case=1",
139+
args: args{
140+
include: []string{
141+
apiserver.RegisterAccess,
142+
},
143+
protocol: "grpc",
144+
},
145+
want: map[string]bool{
146+
"/v1.PolarisGRPC/RegisterInstance": true,
147+
"/v1.PolarisGRPC/DeregisterInstance": true,
148+
},
149+
wantErr: false,
150+
},
151+
{
152+
name: "case=1",
153+
args: args{
154+
include: []string{
155+
apiserver.DiscoverAccess,
156+
},
157+
protocol: "grpc",
158+
},
159+
want: map[string]bool{
160+
"/v1.PolarisGRPC/Discover": true,
161+
"/v1.PolarisGRPC/ReportClient": true,
162+
},
163+
wantErr: false,
164+
},
165+
}
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
got, err := grpchelp.GetClientOpenMethod(tt.args.include, tt.args.protocol)
169+
if (err != nil) != tt.wantErr {
170+
t.Errorf("GetClientOpenMethod() error = %v, wantErr %v", err, tt.wantErr)
171+
return
172+
}
173+
if !reflect.DeepEqual(got, tt.want) {
174+
t.Errorf("GetClientOpenMethod() = %v, want %v", got, tt.want)
175+
}
176+
})
177+
}
178+
}

apiserver/grpcserver/cache.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ type Cache interface {
4646

4747
// CacheObject
4848
type CacheObject struct {
49+
// OriginVal
4950
OriginVal proto.Message
50-
51+
// preparedVal
5152
preparedVal *grpc.PreparedMsg
52-
53+
// CacheType
5354
CacheType string
54-
55+
// Key
5556
Key string
5657
}
5758

apiserver/grpcserver/config/server.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (g *ConfigGRPCServer) Run(errCh chan error) {
7272
case "client":
7373
if apiConfig.Enable {
7474
apiconfig.RegisterPolarisConfigGRPCServer(server, g)
75-
openMethod, getErr := getConfigClientOpenMethod(g.GetProtocol())
75+
openMethod, getErr := GetClientOpenMethod(g.GetProtocol())
7676
if getErr != nil {
7777
return getErr
7878
}
@@ -126,9 +126,10 @@ func (g *ConfigGRPCServer) allowAccess(method string) bool {
126126
return g.BaseGrpcServer.AllowAccess(method)
127127
}
128128

129-
func getConfigClientOpenMethod(protocol string) (map[string]bool, error) {
129+
// GetClientOpenMethod .
130+
func GetClientOpenMethod(protocol string) (map[string]bool, error) {
130131
openMethods := []string{"GetConfigFile", "CreateConfigFile",
131-
"UpdateConfigFile", "PublishConfigFile", "WatchConfigFiles"}
132+
"UpdateConfigFile", "PublishConfigFile", "WatchConfigFiles", "GetConfigFileMetadataList"}
132133

133134
openMethod := make(map[string]bool)
134135

0 commit comments

Comments
 (0)