Skip to content

Commit 5ba14ea

Browse files
committed
add base draft dynconfig sdk impl
1 parent f2f869e commit 5ba14ea

File tree

4 files changed

+212
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)