Skip to content

Commit ba7451c

Browse files
authored
Merge pull request #386 from Enjection/feature/KIKIMR-20854/dynconfig-sdk Add base draft dynconfig sdk impl
2 parents f2f869e + a16c0ac commit ba7451c

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

ydb/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .import_client import * # noqa
1919
from .tracing import * # noqa
2020
from .topic import * # noqa
21+
from .draft import * # noqa
2122

2223
try:
2324
import ydb.aio as aio # noqa

ydb/_apis.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
ydb_topic_v1_pb2_grpc,
1313
)
1414

15+
from ._grpc.v4.draft import (
16+
ydb_dynamic_config_v1_pb2_grpc,
17+
)
18+
1519
from ._grpc.v4.protos import (
1620
ydb_status_codes_pb2,
1721
ydb_discovery_pb2,
@@ -21,6 +25,10 @@
2125
ydb_operation_pb2,
2226
ydb_common_pb2,
2327
)
28+
29+
from ._grpc.v4.draft.protos import (
30+
ydb_dynamic_config_pb2,
31+
)
2432
else:
2533
from ._grpc.common import (
2634
ydb_cms_v1_pb2_grpc,
@@ -31,6 +39,10 @@
3139
ydb_topic_v1_pb2_grpc,
3240
)
3341

42+
from ._grpc.common.draft import (
43+
ydb_dynamic_config_v1_pb2_grpc,
44+
)
45+
3446
from ._grpc.common.protos import (
3547
ydb_status_codes_pb2,
3648
ydb_discovery_pb2,
@@ -41,6 +53,10 @@
4153
ydb_common_pb2,
4254
)
4355

56+
from ._grpc.common.draft.protos import (
57+
ydb_dynamic_config_pb2,
58+
)
59+
4460

4561
StatusIds = ydb_status_codes_pb2.StatusIds
4662
FeatureFlag = ydb_common_pb2.FeatureFlag
@@ -50,6 +66,7 @@
5066
ydb_table = ydb_table_pb2
5167
ydb_discovery = ydb_discovery_pb2
5268
ydb_operation = ydb_operation_pb2
69+
ydb_dynamic_config = ydb_dynamic_config_pb2
5370

5471

5572
class CmsService(object):
@@ -109,3 +126,12 @@ class TopicService(object):
109126
DropTopic = "DropTopic"
110127
StreamRead = "StreamRead"
111128
StreamWrite = "StreamWrite"
129+
130+
131+
class DynamicConfigService(object):
132+
Stub = ydb_dynamic_config_v1_pb2_grpc.DynamicConfigServiceStub
133+
134+
ReplaceConfig = "ReplaceConfig"
135+
SetConfig = "SetConfig"
136+
GetConfig = "GetConfig"
137+
GetNodeLabels = "GetNodeLabels"

ydb/draft/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .dynamic_config import * # noqa

ydb/draft/dynamic_config.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import abc
2+
from abc import abstractmethod
3+
from .. import issues, operation, _apis
4+
5+
6+
class IDynamicConfigClient(abc.ABC):
7+
@abstractmethod
8+
def __init__(self, driver):
9+
pass
10+
11+
@abstractmethod
12+
def replace_config(self, config, dry_run, allow_unknown_fields, settings):
13+
pass
14+
15+
@abstractmethod
16+
def set_config(self, config, dry_run, allow_unknown_fields, settings):
17+
pass
18+
19+
@abstractmethod
20+
def get_config(self, settings):
21+
pass
22+
23+
@abstractmethod
24+
def get_node_labels(self, node_id, settings):
25+
pass
26+
27+
28+
class DynamicConfig(object):
29+
__slots__ = ("version", "cluster", "config")
30+
31+
def __init__(self, version, cluster, config, *args, **kwargs):
32+
self.version = version
33+
self.cluster = cluster
34+
self.config = config
35+
36+
37+
class NodeLabels(object):
38+
__slots__ = "labels"
39+
40+
def __init__(self, labels, *args, **kwargs):
41+
self.labels = labels
42+
43+
44+
def _replace_config_request_factory(config, dry_run, allow_unknown_fields):
45+
request = _apis.ydb_dynamic_config.ReplaceConfigRequest()
46+
request.config = config
47+
request.dry_run = dry_run
48+
request.allow_unknown_fields = allow_unknown_fields
49+
return request
50+
51+
52+
def _set_config_request_factory(config, dry_run, allow_unknown_fields):
53+
request = _apis.ydb_dynamic_config.SetConfigRequest()
54+
request.config = config
55+
request.dry_run = dry_run
56+
request.allow_unknown_fields = allow_unknown_fields
57+
return request
58+
59+
60+
def _get_config_request_factory():
61+
request = _apis.ydb_dynamic_config.GetConfigRequest()
62+
return request
63+
64+
65+
def _get_node_labels_request_factory(node_id):
66+
request = _apis.ydb_dynamic_config.GetNodeLabelsRequest()
67+
request.node_id = node_id
68+
return request
69+
70+
71+
def _wrap_dynamic_config(config_pb, dynamic_config_cls=None, *args, **kwargs):
72+
dynamic_config_cls = DynamicConfig if dynamic_config_cls is None else dynamic_config_cls
73+
return dynamic_config_cls(config_pb.identity.version, config_pb.identity.cluster, config_pb.config, *args, **kwargs)
74+
75+
76+
def _wrap_get_config_response(rpc_state, response):
77+
issues._process_response(response.operation)
78+
message = _apis.ydb_dynamic_config.GetConfigResult()
79+
response.operation.result.Unpack(message)
80+
return _wrap_dynamic_config(message)
81+
82+
83+
def _wrap_node_labels(labels_pb, node_labels_cls=None, *args, **kwargs):
84+
node_labels_cls = NodeLabels if node_labels_cls is None else node_labels_cls
85+
return node_labels_cls(dict([(entry.label, entry.value) for entry in labels_pb.labels]), *args, **kwargs)
86+
87+
88+
def _wrap_get_node_labels_response(rpc_state, response):
89+
issues._process_response(response.operation)
90+
message = _apis.ydb_dynamic_config.GetNodeLabelsResult()
91+
response.operation.result.Unpack(message)
92+
return _wrap_node_labels(message)
93+
94+
95+
class BaseDynamicConfigClient(IDynamicConfigClient):
96+
__slots__ = ("_driver",)
97+
98+
def __init__(self, driver):
99+
self._driver = driver
100+
101+
def replace_config(self, config, dry_run, allow_unknown_fields, settings=None):
102+
return self._driver(
103+
_replace_config_request_factory(config, dry_run, allow_unknown_fields),
104+
_apis.DynamicConfigService.Stub,
105+
_apis.DynamicConfigService.ReplaceConfig,
106+
operation.Operation,
107+
settings,
108+
)
109+
110+
def set_config(self, config, dry_run, allow_unknown_fields, settings=None):
111+
return self._driver(
112+
_set_config_request_factory(config, dry_run, allow_unknown_fields),
113+
_apis.DynamicConfigService.Stub,
114+
_apis.DynamicConfigService.SetConfig,
115+
operation.Operation,
116+
settings,
117+
)
118+
119+
def get_config(self, settings=None):
120+
return self._driver(
121+
_get_config_request_factory(),
122+
_apis.DynamicConfigService.Stub,
123+
_apis.DynamicConfigService.GetConfig,
124+
_wrap_get_config_response,
125+
settings,
126+
)
127+
128+
def get_node_labels(self, node_id, settings=None):
129+
return self._driver(
130+
_get_node_labels_request_factory(node_id),
131+
_apis.DynamicConfigService.Stub,
132+
_apis.DynamicConfigService.GetNodeLabels,
133+
_wrap_get_node_labels_response,
134+
settings,
135+
)
136+
137+
138+
class DynamicConfigClient(BaseDynamicConfigClient):
139+
def async_replace_config(self, config, dry_run, allow_unknown_fields, settings=None):
140+
return self._driver.future(
141+
_replace_config_request_factory(config, dry_run, allow_unknown_fields),
142+
_apis.DynamicConfigService.Stub,
143+
_apis.DynamicConfigService.ReplaceConfig,
144+
operation.Operation,
145+
settings,
146+
)
147+
148+
def async_set_config(self, config, dry_run, allow_unknown_fields, settings=None):
149+
return self._driver.future(
150+
_set_config_request_factory(config, dry_run, allow_unknown_fields),
151+
_apis.DynamicConfigService.Stub,
152+
_apis.DynamicConfigService.SetConfig,
153+
operation.Operation,
154+
settings,
155+
)
156+
157+
def async_get_config(self, settings=None):
158+
return self._driver.future(
159+
_get_config_request_factory(),
160+
_apis.DynamicConfigService.Stub,
161+
_apis.DynamicConfigService.GetConfig,
162+
_wrap_get_config_response,
163+
settings,
164+
)
165+
166+
def async_get_node_labels(self, node_id, settings=None):
167+
return self._driver.future(
168+
_get_node_labels_request_factory(node_id),
169+
_apis.DynamicConfigService.Stub,
170+
_apis.DynamicConfigService.GetNodeLabels,
171+
_wrap_get_node_labels_response,
172+
settings,
173+
)

0 commit comments

Comments
 (0)