polaris-python 0.0.1.dev3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- polaris/__init__.py +74 -0
- polaris/_version.py +16 -0
- polaris/api/__init__.py +55 -0
- polaris/api/_log_file.py +232 -0
- polaris/api/config.py +145 -0
- polaris/api/consumer.py +240 -0
- polaris/api/log.py +414 -0
- polaris/api/lossless.py +73 -0
- polaris/api/provider.py +152 -0
- polaris/api/skill.py +162 -0
- polaris/pkg/__init__.py +15 -0
- polaris/pkg/common/__init__.py +15 -0
- polaris/pkg/common/plugin.py +33 -0
- polaris/pkg/config/__init__.py +15 -0
- polaris/pkg/config/api.py +1205 -0
- polaris/pkg/config/default.py +92 -0
- polaris/pkg/config/event.py +76 -0
- polaris/pkg/config/loader.py +392 -0
- polaris/pkg/config/stat_reporter.py +100 -0
- polaris/pkg/flow/__init__.py +15 -0
- polaris/pkg/flow/admin_server.py +104 -0
- polaris/pkg/flow/async_flow.py +55 -0
- polaris/pkg/flow/base_flow.py +111 -0
- polaris/pkg/flow/impl.py +1277 -0
- polaris/pkg/flow/lossless.py +605 -0
- polaris/pkg/flow/notify.py +46 -0
- polaris/pkg/flow/register.py +210 -0
- polaris/pkg/flow/skill.py +110 -0
- polaris/pkg/flow/sync_flow.py +20 -0
- polaris/pkg/model/__init__.py +15 -0
- polaris/pkg/model/context.py +53 -0
- polaris/pkg/model/contract.py +89 -0
- polaris/pkg/model/core.py +51 -0
- polaris/pkg/model/engine.py +83 -0
- polaris/pkg/model/error.py +94 -0
- polaris/pkg/model/event.py +159 -0
- polaris/pkg/model/lossless.py +102 -0
- polaris/pkg/model/rule.py +152 -0
- polaris/pkg/model/server.py +288 -0
- polaris/pkg/model/service.py +820 -0
- polaris/pkg/model/skill.py +179 -0
- polaris/pkg/model/stat.py +168 -0
- polaris/pkg/model/util.py +58 -0
- polaris/pkg/version/__init__.py +18 -0
- polaris/plugin/__init__.py +32 -0
- polaris/plugin/circuit_breaker/__init__.py +19 -0
- polaris/plugin/circuit_breaker/base.py +54 -0
- polaris/plugin/circuit_breaker/composite.py +265 -0
- polaris/plugin/circuit_breaker/config.py +36 -0
- polaris/plugin/event/__init__.py +21 -0
- polaris/plugin/event/pushgateway.py +202 -0
- polaris/plugin/load_balancer/__init__.py +24 -0
- polaris/plugin/load_balancer/base.py +30 -0
- polaris/plugin/load_balancer/random.py +39 -0
- polaris/plugin/load_balancer/weighted_random.py +50 -0
- polaris/plugin/local_registry/__init__.py +18 -0
- polaris/plugin/local_registry/base.py +47 -0
- polaris/plugin/local_registry/file_cache.py +335 -0
- polaris/plugin/local_registry/local_cache.py +433 -0
- polaris/plugin/server_connector/__init__.py +20 -0
- polaris/plugin/server_connector/base.py +104 -0
- polaris/plugin/server_connector/convert.py +149 -0
- polaris/plugin/server_connector/grpc_connector/__init__.py +21 -0
- polaris/plugin/server_connector/grpc_connector/connection.py +401 -0
- polaris/plugin/server_connector/grpc_connector/grpc_client.py +384 -0
- polaris/plugin/server_connector/grpc_connector/tls.py +45 -0
- polaris/plugin/service_router/__init__.py +28 -0
- polaris/plugin/service_router/base.py +105 -0
- polaris/plugin/service_router/isolated.py +35 -0
- polaris/plugin/service_router/metadata.py +84 -0
- polaris/plugin/service_router/recover.py +62 -0
- polaris/plugin/service_router/util.py +54 -0
- polaris/plugin/skill/__init__.py +16 -0
- polaris/plugin/skill/connector.py +127 -0
- polaris/plugin/skill/persistence.py +94 -0
- polaris/plugin/stat_reporter/__init__.py +16 -0
- polaris/plugin/stat_reporter/base.py +34 -0
- polaris/plugin/stat_reporter/prometheus.py +375 -0
- polaris/plugin_manager.py +146 -0
- polaris_python-0.0.1.dev3.dist-info/METADATA +213 -0
- polaris_python-0.0.1.dev3.dist-info/RECORD +84 -0
- polaris_python-0.0.1.dev3.dist-info/WHEEL +5 -0
- polaris_python-0.0.1.dev3.dist-info/licenses/LICENSE +18 -0
- polaris_python-0.0.1.dev3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# Tencent is pleased to support the open source community by making polaris-python available.
|
|
2
|
+
#
|
|
3
|
+
# Copyright (C) 2021 Tencent. All rights reserved.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the BSD 3-Clause License (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://opensource.org/licenses/BSD-3-Clause
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software distributed
|
|
12
|
+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
13
|
+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
14
|
+
# specific language governing permissions and limitations under the License.
|
|
15
|
+
|
|
16
|
+
from typing import Dict
|
|
17
|
+
|
|
18
|
+
from polaris.pkg.model.error import ErrorCode, SDKError
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# InstanceDeRegisterRequest 反注册服务请求
|
|
22
|
+
class InstanceDeRegisterRequest:
|
|
23
|
+
def __init__(self, service: str, service_token: str, namespace: str, host: str, port: int, instance_id: str = '',
|
|
24
|
+
timeout: int = 0, retry_count: int = 0):
|
|
25
|
+
# 服务名
|
|
26
|
+
self.service = service
|
|
27
|
+
# 服务访问Token
|
|
28
|
+
self.service_token = service_token
|
|
29
|
+
# 命名空间
|
|
30
|
+
self.namespace = namespace
|
|
31
|
+
# 服务实例ID
|
|
32
|
+
self.instance_id = instance_id
|
|
33
|
+
# 服务实例ip
|
|
34
|
+
self.host = host
|
|
35
|
+
# 服务实例端口
|
|
36
|
+
self.port = port
|
|
37
|
+
# 可选,单次查询超时时间,默认直接获取全局的超时配置
|
|
38
|
+
# 用户总最大超时时间为(1+RetryCount) * Timeout
|
|
39
|
+
self.timeout = timeout
|
|
40
|
+
# 可选,重试次数,默认直接获取全局的超时配置
|
|
41
|
+
self.retry_count = retry_count
|
|
42
|
+
|
|
43
|
+
def set_timeout(self, timeout: int):
|
|
44
|
+
self.timeout = timeout
|
|
45
|
+
|
|
46
|
+
def set_retry_count(self, retry_count: int):
|
|
47
|
+
self.retry_count = retry_count
|
|
48
|
+
|
|
49
|
+
# 验证request是否合理,返回SDKERROR
|
|
50
|
+
def validate(self):
|
|
51
|
+
if not isinstance(self.service_token, str):
|
|
52
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
53
|
+
"InstanceDeRegisterRequest: serviceToken must be a string")
|
|
54
|
+
if len(self.instance_id) > 0:
|
|
55
|
+
return
|
|
56
|
+
if len(self.service) == 0:
|
|
57
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
58
|
+
"InstanceDeRegisterRequest: serviceName should not be empty when instanceId is empty")
|
|
59
|
+
if len(self.namespace) == 0:
|
|
60
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
61
|
+
"InstanceDeRegisterRequest: namespace should not be empty when instanceId is empty")
|
|
62
|
+
if len(self.host) == 0:
|
|
63
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
64
|
+
"InstanceDeRegisterRequest: host should not be empty when instanceId is empty")
|
|
65
|
+
if self.port <= 0 or self.port >= 65536:
|
|
66
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
67
|
+
"InstanceDeRegisterRequest: port should be in range (0, 65536)")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# 最小权重值
|
|
71
|
+
MinWeight = 0
|
|
72
|
+
# 最大权重值
|
|
73
|
+
MaxWeight = 10000
|
|
74
|
+
# 最小优先级
|
|
75
|
+
MinPriority = 0
|
|
76
|
+
# 最大优先级
|
|
77
|
+
MaxPriority = 9
|
|
78
|
+
# 健康检查类型:心跳
|
|
79
|
+
HealthCheckTypeHeartBeat = 0
|
|
80
|
+
DefaultHeartbeatTTL = 5
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# InstanceRegisterRequest 注册服务请求
|
|
84
|
+
class InstanceRegisterRequest:
|
|
85
|
+
def __init__(self, service: str, service_token: str, namespace: str, host: str, port: int, protocol: str = '',
|
|
86
|
+
weight: int = 100, priority: int = 0, version: str = '', metadata: Dict[str, str] = None,
|
|
87
|
+
healthy: bool = True, ttl: int | None = None, timeout: int = 0, retry_count: int = 0,
|
|
88
|
+
auto_heartbeat: bool = False, instance_id: str = ''):
|
|
89
|
+
# 服务名
|
|
90
|
+
self.service = service
|
|
91
|
+
# 服务访问Token
|
|
92
|
+
self.service_token = service_token
|
|
93
|
+
# 命名空间
|
|
94
|
+
self.namespace = namespace
|
|
95
|
+
# 服务实例ip
|
|
96
|
+
self.host = host
|
|
97
|
+
# 服务实例端口
|
|
98
|
+
self.port = port
|
|
99
|
+
|
|
100
|
+
# 以下字段可选,默认nil表示客户端不配置,使用服务端配置
|
|
101
|
+
# 服务协议
|
|
102
|
+
self.protocol = protocol
|
|
103
|
+
# 服务权重,默认100,范围0-10000
|
|
104
|
+
self.weight = weight
|
|
105
|
+
# 实例优先级,默认为0,数值越小,优先级越高
|
|
106
|
+
self.priority = priority
|
|
107
|
+
# 实例提供服务版本号
|
|
108
|
+
self.version = version
|
|
109
|
+
# 用户自定义metadata信息
|
|
110
|
+
if metadata is None:
|
|
111
|
+
self.metadata = {}
|
|
112
|
+
else:
|
|
113
|
+
self.metadata = metadata
|
|
114
|
+
# 该服务实例是否健康,默认健康
|
|
115
|
+
self.healthy = healthy
|
|
116
|
+
# ttl超时时间,单位:秒
|
|
117
|
+
self.ttl = ttl
|
|
118
|
+
self.auto_heartbeat = auto_heartbeat
|
|
119
|
+
self.instance_id = instance_id
|
|
120
|
+
|
|
121
|
+
# 可选,单次查询超时时间,默认直接获取全局的超时配置
|
|
122
|
+
# 用户总最大超时时间为(1+RetryCount) * Timeout
|
|
123
|
+
self.timeout = timeout
|
|
124
|
+
# 可选,重试次数,默认直接获取全局的超时配置
|
|
125
|
+
self.retry_count = retry_count
|
|
126
|
+
|
|
127
|
+
def set_timeout(self, timeout: int):
|
|
128
|
+
self.timeout = timeout
|
|
129
|
+
|
|
130
|
+
def set_retry_count(self, retry_count: int):
|
|
131
|
+
self.retry_count = retry_count
|
|
132
|
+
|
|
133
|
+
# 验证request是否合理,返回SDKERROR
|
|
134
|
+
def validate(self):
|
|
135
|
+
if self.ttl is not None and (
|
|
136
|
+
isinstance(self.ttl, bool) or not isinstance(self.ttl, int) or not 0 < self.ttl <= 0xFFFFFFFF
|
|
137
|
+
):
|
|
138
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
139
|
+
"InstanceRegisterRequest: heartbeat ttl must be a positive uint32 number of seconds")
|
|
140
|
+
if not isinstance(self.auto_heartbeat, bool):
|
|
141
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
142
|
+
"InstanceRegisterRequest: auto_heartbeat must be a boolean")
|
|
143
|
+
if not isinstance(self.service_token, str):
|
|
144
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
145
|
+
"InstanceRegisterRequest: serviceToken must be a string")
|
|
146
|
+
if len(self.service) == 0:
|
|
147
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
148
|
+
"InstanceRegisterRequest: serviceName should not be empty when instanceId is empty")
|
|
149
|
+
if len(self.namespace) == 0:
|
|
150
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
151
|
+
"InstanceRegisterRequest: namespace should not be empty when instanceId is empty")
|
|
152
|
+
if len(self.host) == 0:
|
|
153
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
154
|
+
"InstanceRegisterRequest: host should not be empty when instanceId is empty")
|
|
155
|
+
if self.port <= 0 or self.port >= 65536:
|
|
156
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
157
|
+
"InstanceRegisterRequest: port should be in range (0, 65536)")
|
|
158
|
+
if self.weight < MinWeight or self.weight > MaxWeight:
|
|
159
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
160
|
+
"InstanceRegisterRequest: weight should be in range [%d, %d]" % (MinWeight, MaxWeight))
|
|
161
|
+
if self.priority < MinPriority or self.priority > MaxPriority:
|
|
162
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
163
|
+
"InstanceRegisterRequest: priority should be in range [%d, %d]" % (MinPriority, MaxPriority))
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# 校验元数据的key是否为空
|
|
167
|
+
def validate_metadata(prefix: str, metadata: Dict[str, str]):
|
|
168
|
+
for key in metadata:
|
|
169
|
+
if len(key) == 0:
|
|
170
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
171
|
+
"validate_metadata: %s: metadata has empty key" % prefix)
|
|
172
|
+
return True
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
# InstanceRegisterResponse 注册服务响应
|
|
176
|
+
class InstanceRegisterResponse:
|
|
177
|
+
def __init__(self, instance_id):
|
|
178
|
+
self.instance_id = instance_id
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
# InstanceDiscoverRequest 服务发现请求
|
|
182
|
+
class InstanceDiscoverRequest:
|
|
183
|
+
def __init__(self, type, name, namespace, revision=""):
|
|
184
|
+
self.type = type
|
|
185
|
+
self.name = name
|
|
186
|
+
self.namespace = namespace
|
|
187
|
+
self.revision = revision
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# ReportClientRequest 客户端上报请求信息
|
|
191
|
+
class ReportClientRequest:
|
|
192
|
+
def __init__(self, version: str, host: str = "", timeout: int = 0, persist_handler=None,
|
|
193
|
+
client_id: str = "", reporter_meta_infos=None):
|
|
194
|
+
self.client_id = client_id
|
|
195
|
+
self.reporter_meta_infos = reporter_meta_infos or []
|
|
196
|
+
# 客户端IP地址
|
|
197
|
+
self.host = host
|
|
198
|
+
# 客户端版本信息
|
|
199
|
+
self.version = version
|
|
200
|
+
# 可选,单次查询超时时间,默认直接获取全局的超时配置
|
|
201
|
+
# 用户总最大超时时间为(1+RetryCount) * Timeout
|
|
202
|
+
self.timeout = timeout
|
|
203
|
+
# 持久化回调
|
|
204
|
+
self.persist_handler = persist_handler
|
|
205
|
+
|
|
206
|
+
def set_timeout(self, timeout: int):
|
|
207
|
+
self.timeout = timeout
|
|
208
|
+
|
|
209
|
+
def validate(self):
|
|
210
|
+
if len(self.version) == 0:
|
|
211
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
212
|
+
"ReportClientRequest: version should not be empty")
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# ReportClientRequest 客户端上报响应信息
|
|
216
|
+
class ReportClientResponse:
|
|
217
|
+
def __init__(self, region: str, zone: str, campus: str, mode: int = 0, version: str = ""):
|
|
218
|
+
self.mode = mode
|
|
219
|
+
self.version = version
|
|
220
|
+
self.region = region
|
|
221
|
+
self.zone = zone
|
|
222
|
+
self.campus = campus
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
# 心跳上报请求
|
|
226
|
+
class InstanceHeartbeatRequest:
|
|
227
|
+
def __init__(self, service: str, service_token: str, namespace: str, instance_id: str, host: str, port: int,
|
|
228
|
+
timeout: int = 0, retry_count: int = 0):
|
|
229
|
+
# 必选,服务名
|
|
230
|
+
self.service = service
|
|
231
|
+
# 必选,服务访问Token
|
|
232
|
+
self.service_token = service_token
|
|
233
|
+
# 必选,命名空间
|
|
234
|
+
self.namespace = namespace
|
|
235
|
+
# 必选,服务实例ID
|
|
236
|
+
self.instance_id = instance_id
|
|
237
|
+
# 必选,服务实例ip
|
|
238
|
+
self.host = host
|
|
239
|
+
# 必选,服务实例端口
|
|
240
|
+
self.port = port
|
|
241
|
+
# 可选,单次查询超时时间,默认直接获取全局的超时配置
|
|
242
|
+
# 用户总最大超时时间为(1+RetryCount) * Timeout
|
|
243
|
+
self.timeout = timeout
|
|
244
|
+
# 可选,重试次数,默认直接获取全局的超时配置
|
|
245
|
+
self.retry_count = retry_count
|
|
246
|
+
|
|
247
|
+
def set_timeout(self, duration: int):
|
|
248
|
+
self.timeout = duration
|
|
249
|
+
|
|
250
|
+
def set_retry_count(self, retry_count: int):
|
|
251
|
+
self.retry_count = retry_count
|
|
252
|
+
|
|
253
|
+
def validate(self):
|
|
254
|
+
if not isinstance(self.service_token, str):
|
|
255
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
256
|
+
"InstanceHeartbeatRequest: serviceToken must be a string")
|
|
257
|
+
if len(self.instance_id) > 0:
|
|
258
|
+
return
|
|
259
|
+
if len(self.service) == 0:
|
|
260
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
261
|
+
"InstanceHeartbeatRequest: serviceName should not be empty when instanceId is empty")
|
|
262
|
+
if len(self.namespace) == 0:
|
|
263
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
264
|
+
"InstanceHeartbeatRequest: namespace should not be empty when instanceId is empty")
|
|
265
|
+
if len(self.host) == 0:
|
|
266
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
267
|
+
"InstanceHeartbeatRequest: host should not be empty when instanceId is empty")
|
|
268
|
+
if self.port <= 0 or self.port >= 65536:
|
|
269
|
+
raise SDKError(ErrorCode.ErrCodeAPIInvalidArgument,
|
|
270
|
+
"InstanceHeartbeatRequest: port should be in range (0, 65536)")
|
|
271
|
+
|
|
272
|
+
# class ServiceStr:
|
|
273
|
+
# def __init__(self, name, namespace):
|
|
274
|
+
# self.name = name
|
|
275
|
+
# self.namespace = namespace
|
|
276
|
+
# self.metadata = None
|
|
277
|
+
# self.ports = ''
|
|
278
|
+
# self.business = ''
|
|
279
|
+
# self.department = ''
|
|
280
|
+
# self.cmdb_mod1 = ''
|
|
281
|
+
# self.cmdb_mod2 = ''
|
|
282
|
+
# self.cmd_mod3 = ''
|
|
283
|
+
# self.comment = ''
|
|
284
|
+
# self.owners = ''
|
|
285
|
+
# self.token = ''
|
|
286
|
+
# self.ctime = ''
|
|
287
|
+
# self.mtime = ''
|
|
288
|
+
# self.revision = ''
|