forked from typesense/typesense-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.py
More file actions
407 lines (327 loc) · 13.4 KB
/
configuration.py
File metadata and controls
407 lines (327 loc) · 13.4 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
This module provides configuration management for the Typesense Instance.
Classes:
- Config: Handles loading and accessing configuration settings.
- Node: Represents a node in the Typesense cluster.
Functions:
- load_config: Loads configuration from a file.
- get_setting: Retrieves a specific setting from the configuration.
- set_setting: Updates a specific setting in the configuration.
Exceptions:
- ConfigError: Custom exception for configuration-related errors.
"""
from __future__ import annotations
import sys
import time
if sys.version_info >= (3, 11):
import typing
else:
import typing_extensions as typing
from urllib.parse import urlparse
from typesense.exceptions import ConfigError
from typesense.logger import logger
class NodeConfigDict(typing.TypedDict):
"""
A dictionary that represents the configuration for a node in the Typesense cluster.
Attributes:
host (str): The host name of the node.
port (int): The port number of the node.
path (str, optional): The path of the node.
protocol (typing.Literal['http', 'https'] | str): The protocol of the node.
"""
host: str
port: int
path: typing.NotRequired[str]
protocol: typing.Union[typing.Literal["http", "https"], str]
class ConfigDict(typing.TypedDict):
"""
A dictionary that represents the configuration for the Typesense client.
Attributes:
nodes (list[typing.Union[str, NodeConfigDict]]): A list of dictionaries or URLs that
represent the nodes in the cluster.
nearest_node (typing.Union[str, NodeConfigDict]): A dictionary or URL
that represents the nearest node to the client.
api_key (str): The API key to use for authentication.
num_retries (int): The number of retries to attempt before failing.
interval_seconds (int): The interval in seconds between retries.
healthcheck_interval_seconds (int): The interval in seconds between
health checks.
verify (bool): Whether to verify the SSL certificate.
timeout_seconds (int, deprecated): The connection timeout in seconds.
master_node (typing.Union[str, NodeConfigDict], deprecated): A dictionary or
URL that represents the master node.
additional_headers (dict): Additional headers to include in the request.
read_replica_nodes (list[typing.Union[str, NodeConfigDict]], deprecated): A list of
dictionaries or URLs that represent the read replica nodes.
connection_timeout_seconds (float): The connection timeout in seconds.
suppress_deprecation_warnings (bool): Whether to suppress deprecation warnings.
"""
nodes: typing.List[typing.Union[str, NodeConfigDict]]
nearest_node: typing.NotRequired[typing.Union[str, NodeConfigDict]]
api_key: str
num_retries: typing.NotRequired[int]
interval_seconds: typing.NotRequired[int]
healthcheck_interval_seconds: typing.NotRequired[int]
verify: typing.NotRequired[bool]
timeout_seconds: typing.NotRequired[int] # deprecated
master_node: typing.NotRequired[typing.Union[str, NodeConfigDict]] # deprecated
additional_headers: typing.NotRequired[typing.Dict[str, str]]
read_replica_nodes: typing.NotRequired[
typing.List[typing.Union[str, NodeConfigDict]]
] # deprecated
connection_timeout_seconds: typing.NotRequired[float]
suppress_deprecation_warnings: typing.NotRequired[bool]
class Node:
"""
Class for representing a node in the Typesense cluster.
Attributes:
host (str): The host name of the node.
port (str | int): The port number of the node.
path (str): The path of the node.
protocol (typing.Literal['http', 'https'] | str): The protocol of the node.
healthy (bool): Whether the node is healthy or not.
"""
def __init__(
self,
host: str,
port: typing.Union[str, int],
path: str,
protocol: typing.Union[typing.Literal["http", "https"], str],
) -> None:
"""
Initialize a Node object with the specified host, port, path, and protocol.
Args:
host (str): The host name of the node.
port (str | int): The port number of the node.
path (str): The path of the node.
protocol (typing.Literal['http', 'https'] | str): The protocol of the node.
"""
self.host = host
self.port = port
self.path = path
self.protocol = protocol
# Used to skip bad hosts
self.healthy = True
# Used to track the last time this node was accessed
self.last_access_ts: int = int(time.time())
@classmethod
def from_url(cls, url: str) -> "Node":
"""
Initialize a Node object from a URL string.
Args:
url (str): The URL string to parse.
Returns:
Node: The Node object created from the URL string.
Raises:
ConfigError: If the URL does not contain the host name, port number, or protocol.
"""
parsed = urlparse(url)
if not parsed.hostname:
raise ConfigError("Node URL does not contain the host name.")
if not parsed.port:
raise ConfigError("Node URL does not contain the port.")
if not parsed.scheme:
raise ConfigError("Node URL does not contain the protocol.")
return cls(parsed.hostname, parsed.port, parsed.path, parsed.scheme)
def url(self) -> str:
"""
Generate the URL of the node.
Returns:
str: The URL of the node
"""
return f"{self.protocol}://{self.host}:{self.port}{self.path}"
class Configuration:
"""
Class for managing the configuration settings for the Typesense client.
Attributes:
nodes (list[Node]): A list of Node objects representing the nodes in the cluster.
nearest_node (Node | None): The nearest node to the client.
api_key (str): The API key to use for authentication.
connection_timeout_seconds (float): The connection timeout in seconds.
num_retries (int): The number of retries to attempt before failing.
retry_interval_seconds (float): The interval in seconds between retries.
healthcheck_interval_seconds (int): The interval in seconds between health checks.
verify (bool): Whether to verify the SSL certificate.
"""
def __init__(
self,
config_dict: ConfigDict,
) -> None:
"""
Initialize a Configuration object with the specified configuration settings.
Args:
config_dict (ConfigDict): A dictionary containing the configuration settings.
"""
self.validations = ConfigurationValidations
self.validations.show_deprecation_warnings(config_dict)
self.validations.validate_config_dict(config_dict)
self.nodes: typing.List[Node] = [
self._initialize_nodes(node) for node in config_dict["nodes"]
]
nearest_node = config_dict.get("nearest_node", None)
self.nearest_node = self._handle_nearest_node(nearest_node)
self.api_key = config_dict.get("api_key", " ")
self.connection_timeout_seconds = config_dict.get(
"connection_timeout_seconds",
3.0,
)
self.num_retries = config_dict.get("num_retries", 3)
self.retry_interval_seconds = config_dict.get("retry_interval_seconds", 1.0)
self.healthcheck_interval_seconds = config_dict.get(
"healthcheck_interval_seconds",
60,
)
self.verify = config_dict.get("verify", True)
self.additional_headers = config_dict.get("additional_headers", {})
self.suppress_deprecation_warnings = config_dict.get(
"suppress_deprecation_warnings", False
)
def _handle_nearest_node(
self,
nearest_node: typing.Union[str, NodeConfigDict, None],
) -> typing.Union[Node, None]:
"""
Handle the nearest node configuration.
Args:
nearest_node (str | NodeConfigDict): The nearest node configuration.
Returns:
Node | None: The nearest node object if it exists, None otherwise.
"""
if nearest_node is None:
return None
return self._initialize_nodes(nearest_node)
def _initialize_nodes(
self,
node: typing.Union[str, NodeConfigDict],
) -> Node:
"""
Handle the initialization of a node.
Args:
node (Node): The node to initialize.
Returns:
Node: The initialized node.
"""
if isinstance(node, str):
return Node.from_url(node)
return Node(
node["host"],
node["port"],
node.get("path", ""),
node["protocol"],
)
class ConfigurationValidations:
"""Class for validating the configuration dictionary."""
@staticmethod
def validate_config_dict(config_dict: ConfigDict) -> None:
"""
Validate the configuration dictionary to ensure it contains the required fields.
Args:
config_dict (ConfigDict): The configuration dictionary to validate.
Raises:
ConfigError: If the configuration dictionary is missing required fields.
"""
ConfigurationValidations.validate_required_config_fields(config_dict)
ConfigurationValidations.validate_nodes(config_dict["nodes"])
nearest_node = config_dict.get("nearest_node", None)
if nearest_node:
ConfigurationValidations.validate_nearest_node(nearest_node)
@staticmethod
def validate_required_config_fields(config_dict: ConfigDict) -> None:
"""
Validate the presence of required fields in the configuration dictionary.
Args:
config_dict (ConfigDict): The configuration dictionary to validate.
Raises:
ConfigError: If the configuration dictionary is missing required fields.
"""
if not config_dict.get("nodes"):
raise ConfigError("`nodes` is not defined.")
if not config_dict.get("api_key"):
raise ConfigError("`api_key` is not defined.")
@staticmethod
def validate_nodes(nodes: typing.List[typing.Union[str, NodeConfigDict]]) -> None:
"""
Validate the nodes in the configuration dictionary.
Args:
nodes (list): The list of nodes to validate.
Raises:
ConfigError: If any node is invalid.
"""
for node in nodes:
if not ConfigurationValidations.validate_node_fields(node):
raise ConfigError(
" ".join(
[
"`node` entry must be a URL string or a",
"dictionary with the following required keys:",
"host, port, protocol",
],
),
)
@staticmethod
def validate_nearest_node(nearest_node: typing.Union[str, NodeConfigDict]) -> None:
"""
Validate the nearest node in the configuration dictionary.
Args:
nearest_node (dict): The nearest node to validate.
Raises:
ConfigError: If the nearest node is invalid.
"""
if not ConfigurationValidations.validate_node_fields(nearest_node):
raise ConfigError(
" ".join(
[
"`nearest_node` entry must be a URL string or a dictionary",
"with the following required keys:",
"host, port, protocol",
],
),
)
@staticmethod
def validate_node_fields(node: typing.Union[str, NodeConfigDict]) -> bool:
"""
Validate the fields of a node in the configuration dictionary.
Args:
node (str | NodeConfigDict): The node to validate.
Returns:
bool: True if the node is valid, False otherwise.
"""
if isinstance(node, str):
return True
expected_fields = {"host", "port", "protocol"}
return expected_fields.issubset(node)
@staticmethod
def show_deprecation_warnings(config_dict: ConfigDict) -> None:
"""
Show deprecation warnings for deprecated configuration fields.
Args:
config_dict (ConfigDict): The configuration dictionary
to check for deprecated fields.
"""
if config_dict.get("timeout_seconds"):
logger.warning(
" ".join(
[
"Deprecation warning: timeout_seconds is now renamed",
"to connection_timeout_seconds",
],
),
)
if config_dict.get("master_node"):
logger.warning(
" ".join(
[
"Deprecation warning: master_node is now consolidated",
"to nodes,starting with Typesense Server v0.12",
],
),
)
if config_dict.get("read_replica_nodes"):
logger.warning(
" ".join(
[
"Deprecation warning: read_replica_nodes is now",
"consolidated to nodes, starting with Typesense Server v0.12",
],
),
)