Skip to content

Commit 44a20a6

Browse files
committed
add alter node
1 parent 9fd7073 commit 44a20a6

File tree

7 files changed

+102
-47
lines changed

7 files changed

+102
-47
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import ydb
2+
from ydb import _apis
3+
4+
5+
def test_coordination_alter_node(driver_sync: ydb.Driver):
6+
client = driver_sync.coordination_client
7+
node_path = "/local/test_alter_node"
8+
9+
try:
10+
client.delete_node(node_path)
11+
except ydb.SchemeError:
12+
pass
13+
14+
client.create_node(node_path)
15+
16+
new_config = _apis.ydb_coordination.Config(
17+
session_grace_period_millis=12345,
18+
attach_consistency_mode=_apis.ydb_coordination.ConsistencyMode.CONSISTENCY_MODE_STRICT,
19+
read_consistency_mode=_apis.ydb_coordination.ConsistencyMode.CONSISTENCY_MODE_RELAXED,
20+
)
21+
22+
alter_res = client.alter_node(node_path, new_config)
23+
24+
assert alter_res.status == ydb.StatusCode.SUCCESS, f"Alter operation failed: {alter_res.status}"
25+
26+
27+
node = client.describe_node(node_path)
28+
assert node.config.session_grace_period_millis == 12345, "Session grace period not updated"
29+
assert node.config.attach_consistency_mode == _apis.ydb_coordination.ConsistencyMode.CONSISTENCY_MODE_STRICT, \
30+
"Attach consistency mode not updated"
31+
assert node.config.read_consistency_mode == _apis.ydb_coordination.ConsistencyMode.CONSISTENCY_MODE_RELAXED, \
32+
"Read consistency mode not updated"
33+
34+
client.delete_node(node_path)

ydb/_grpc/grpcwrapper/ydb_coordination.py

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ def to_proto(self) -> ydb_coordination_pb2.CreateNodeRequest:
2828
operation_params=self.operation_params,
2929
)
3030

31+
@dataclass
32+
class AlterNodeRequest(IToProto):
33+
path: str
34+
config: typing.Optional[public_types.NodeConfig] = None
35+
operation_params: typing.Any = None
36+
37+
def to_proto(self) -> ydb_coordination_pb2.AlterNodeRequest:
38+
cfg_proto = self.config.to_proto() if self.config else None
39+
return ydb_coordination_pb2.AlterNodeRequest(
40+
path=self.path,
41+
config=cfg_proto,
42+
operation_params=self.operation_params,
43+
)
44+
45+
3146

3247
@dataclass
3348
class DescribeNodeRequest(IToProto):
@@ -53,41 +68,3 @@ def to_proto(self) -> ydb_coordination_pb2.DropNodeRequest:
5368
)
5469

5570

56-
@dataclass
57-
class CreateNodeResponse(IFromProto):
58-
operation : ydb.Operation
59-
OPERATION_FIELD_NUMBER : int
60-
61-
@staticmethod
62-
def from_proto(msg: ydb_coordination_pb2.CreateNodeResponse) -> "CreateNodeResponse":
63-
return CreateNodeResponse(
64-
operation=msg.operation,
65-
OPERATION_FIELD_NUMBER=msg.OPERATION_FIELD_NUMBER
66-
)
67-
68-
69-
@dataclass
70-
class DescribeNodeResponse(IFromProto):
71-
operation : ydb.Operation
72-
OPERATION_FIELD_NUMBER : int
73-
74-
@staticmethod
75-
def from_proto(msg: "ydb_coordination_pb2.DescribeNodeResponse") -> "DescribeNodeResponse":
76-
return DescribeNodeResponse(
77-
operation=msg.operation,
78-
OPERATION_FIELD_NUMBER=msg.OPERATION_FIELD_NUMBER
79-
)
80-
81-
82-
@dataclass
83-
class DropNodeResponse(IFromProto):
84-
operation : ydb.Operation
85-
OPERATION_FIELD_NUMBER : int
86-
87-
@staticmethod
88-
def from_proto(msg: ydb_coordination_pb2.DropNodeResponse) -> "DropNodeResponse":
89-
return DropNodeResponse(
90-
operation=msg.operation,
91-
OPERATION_FIELD_NUMBER=msg.OPERATION_FIELD_NUMBER
92-
)
93-

ydb/coordination/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .coordination_client import CoordinationClient
2+
3+
__all__ = [
4+
"CoordinationClient",
5+
]

ydb/coordination/coordination_client.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from ydb import _apis, issues
55

66

7-
from .operations import DescribeNodeOperation, CreateNodeOperation, DropNodeOperation
8-
7+
from .operations import DescribeNodeOperation, CreateNodeOperation, DropNodeOperation, AlterNodeOperation
98

109
if typing.TYPE_CHECKING:
1110
import ydb
@@ -25,6 +24,10 @@ def wrapper_delete_node(rpc_state, response_pb, path, *_args, **_kwargs):
2524
issues._process_response(response_pb.operation)
2625
return DropNodeOperation(rpc_state, response_pb, path)
2726

27+
def wrapper_alter_node(rpc_state, response_pb, path, *_args, **_kwargs):
28+
issues._process_response(response_pb.operation)
29+
return AlterNodeOperation(rpc_state, response_pb, path)
30+
2831

2932
class CoordinationClient:
3033
def __init__(self, driver: "ydb.Driver"):
@@ -103,3 +106,30 @@ def delete_node(
103106
settings=settings,
104107
)
105108

109+
def alter_node(
110+
self,
111+
path: str,
112+
new_config: typing.Optional[typing.Any] = None,
113+
operation_params: typing.Optional[typing.Any] = None,
114+
settings: Optional["ydb.BaseRequestSettings"] = None,
115+
):
116+
"""
117+
Alter node configuration.
118+
"""
119+
request = _apis.ydb_coordination.AlterNodeRequest(
120+
path=path,
121+
config=new_config,
122+
operation_params=operation_params,
123+
)
124+
125+
return self._call_node(
126+
request,
127+
_apis.CoordinationService.AlterNode,
128+
wrapper_alter_node,
129+
wrap_args=(path,),
130+
settings=settings,
131+
)
132+
133+
def close(self):
134+
pass
135+

ydb/coordination/operations.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,16 @@ def __init__(self, rpc_state, response, path, driver=None):
4848

4949
def __repr__(self):
5050
return f"DropNodeOperation<id={self.id}, path={self.path}, status={self.status}>"
51+
52+
class AlterNodeOperation(ydb_op.Operation):
53+
def __init__(self, rpc_state, response, path, driver=None):
54+
super().__init__(rpc_state, response, driver)
55+
self.path = path
56+
self.status = response.operation.status
57+
58+
def __repr__(self):
59+
return f"AlterNodeOperation<id={self.id}, path={self.path}, status={self.status}>"
60+
61+
__str__ = __repr__
62+
63+

ydb/driver.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from . import tracing
99
from . import iam
1010
from . import _utilities
11-
from .coordination.coordination_client import CoordinationClient
11+
from . import coordination
1212

1313

1414

@@ -289,16 +289,12 @@ def __init__(
289289
self._credentials = driver_config.credentials
290290

291291
self.scheme_client = scheme.SchemeClient(self)
292-
self.coordination_client = CoordinationClient(self)
292+
self.coordination_client = coordination.CoordinationClient(self)
293293
self.table_client = table.TableClient(self, driver_config.table_client_settings)
294294
self.topic_client = topic.TopicClient(self, driver_config.topic_client_settings)
295295

296296
def stop(self, timeout=10):
297297
self.table_client._stop_pool_if_needed(timeout=timeout)
298298
self.topic_client.close()
299-
if hasattr(self, "coordination_client"):
300-
try:
301-
self.coordination_client.close()
302-
except Exception as e:
303-
logger.warning(f"Failed to close coordination client: {e}")
299+
self.coordination_client.close()
304300
super().stop(timeout=timeout)

0 commit comments

Comments
 (0)