-
Notifications
You must be signed in to change notification settings - Fork 390
/
client_info.go
190 lines (169 loc) · 5.46 KB
/
client_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package service
import (
"context"
"go.uber.org/zap"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/model"
"github.com/polarismesh/polaris/common/utils"
)
var (
clientFilterAttributes = map[string]struct{}{
"type": {},
"host": {},
"limit": {},
"offset": {},
"version": {},
}
)
func (s *Server) checkAndStoreClient(ctx context.Context, req *api.Client) *api.Response {
clientId := req.GetId().GetValue()
var needStore bool
client := s.caches.Client().GetClient(clientId)
var resp *api.Response
if nil == client {
needStore = true
} else {
needStore = !clientEquals(client.Proto(), req)
}
if needStore {
client, resp = s.createClient(ctx, req)
}
if resp != nil {
if resp.GetCode().GetValue() != api.ExistedResource {
return resp
}
}
return s.HealthServer().ReportByClient(context.Background(), req)
}
func (s *Server) createClient(ctx context.Context, req *api.Client) (*model.Client, *api.Response) {
if namingServer.bc == nil || !namingServer.bc.ClientRegisterOpen() {
return nil, nil
}
return s.asyncCreateClient(ctx, req) // 批量异步
}
// 异步新建客户端
// 底层函数会合并create请求,增加并发创建的吞吐
// req 原始请求
// ins 包含了req数据与instanceID,serviceToken
func (s *Server) asyncCreateClient(ctx context.Context, req *api.Client) (*model.Client, *api.Response) {
rid := utils.ParseRequestID(ctx)
pid := utils.ParsePlatformID(ctx)
future := s.bc.AsyncRegisterClient(req)
if err := future.Wait(); err != nil {
log.Error("[Server][ReportClient] async create client", zap.Error(err), utils.ZapRequestID(rid),
utils.ZapPlatformID(pid))
if future.Code() == api.ExistedResource {
req.Id = utils.NewStringValue(req.GetId().GetValue())
}
return nil, api.NewClientResponse(future.Code(), req)
}
return future.Client(), nil
}
// GetReportClients create one instance
func (s *Server) GetReportClients(ctx context.Context, query map[string]string) *api.BatchQueryResponse {
searchFilters := make(map[string]string)
var (
offset, limit uint32
err error
)
for key, value := range query {
if _, ok := clientFilterAttributes[key]; !ok {
log.Errorf("[Server][Client] attribute(%s) it not allowed", key)
return api.NewBatchQueryResponseWithMsg(api.InvalidParameter, key+" is not allowed")
}
searchFilters[key] = value
}
var (
total uint32
clients []*model.Client
)
offset, limit, err = utils.ParseOffsetAndLimit(searchFilters)
if err != nil {
return api.NewBatchQueryResponse(api.InvalidParameter)
}
total, services, err := s.caches.Client().GetClientsByFilter(searchFilters, offset, limit)
if err != nil {
log.Errorf("[Server][Client][Query] req(%+v) store err: %s", query, err.Error())
return api.NewBatchQueryResponse(api.StoreLayerException)
}
resp := api.NewBatchQueryResponse(api.ExecuteSuccess)
resp.Amount = utils.NewUInt32Value(total)
resp.Size = utils.NewUInt32Value(uint32(len(services)))
resp.Clients = enhancedClients2Api(clients, client2Api)
return resp
}
type Client2Api func(client *model.Client) *api.Client
// client 数组转为[]*api.Client
func enhancedClients2Api(clients []*model.Client, handler Client2Api) []*api.Client {
out := make([]*api.Client, 0, len(clients))
for _, entry := range clients {
outUser := handler(entry)
out = append(out, outUser)
}
return out
}
// model.Client 转为 api.Client
func client2Api(client *model.Client) *api.Client {
if client == nil {
return nil
}
out := client.Proto()
return out
}
func clientEquals(client1 *api.Client, client2 *api.Client) bool {
if client1.GetId().GetValue() != client2.GetId().GetValue() {
return false
}
if client1.GetHost().GetValue() != client2.GetHost().GetValue() {
return false
}
if client1.GetVersion().GetValue() != client2.GetVersion().GetValue() {
return false
}
if client1.GetType() != client2.GetType() {
return false
}
if client1.GetLocation().GetRegion().GetValue() != client2.GetLocation().GetRegion().GetValue() {
return false
}
if client1.GetLocation().GetZone().GetValue() != client2.GetLocation().GetZone().GetValue() {
return false
}
if client1.GetLocation().GetCampus().GetValue() != client2.GetLocation().GetCampus().GetValue() {
return false
}
if len(client1.Stat) != len(client2.Stat) {
return false
}
for i := 0; i < len(client1.Stat); i++ {
if client1.Stat[i].GetTarget().GetValue() != client2.Stat[i].GetTarget().GetValue() {
return false
}
if client1.Stat[i].GetPort().GetValue() != client2.Stat[i].GetPort().GetValue() {
return false
}
if client1.Stat[i].GetPath().GetValue() != client2.Stat[i].GetPath().GetValue() {
return false
}
if client1.Stat[i].GetProtocol().GetValue() != client2.Stat[i].GetProtocol().GetValue() {
return false
}
}
return true
}