Skip to content

Commit 293b11e

Browse files
authored
feat: support update node weight (#432)
* feat: support update node weight * update check
1 parent 361dde9 commit 293b11e

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

oocana/oocana/context.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
from .throttler import throttle
1111
from .preview import PreviewPayload, DataFrame, PreviewPayloadInternal, ShapeDataFrame
1212
from .data import EXECUTOR_NAME
13+
from .internal import random_string, InternalAPI
1314
import os.path
1415
import logging
15-
import random
16-
import string
1716
import hashlib
1817

1918
__all__ = ["Context", "HandleDefDict", "BlockJob", "BlockExecuteException"]
2019

21-
def random_string(length=8):
22-
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
2320

2421
def string_hash(text: str) -> str:
2522
"""
@@ -194,6 +191,7 @@ def __init__(
194191
) -> None:
195192

196193
self.__block_info = blockInfo
194+
self.internal: InternalAPI = InternalAPI(mainframe, blockInfo.job_info())
197195

198196
self.__mainframe = mainframe
199197
self.__store = store

oocana/oocana/internal.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .mainframe import Mainframe
2+
from .data import JobDict
3+
import random
4+
import string
5+
import math
6+
7+
class InternalAPI:
8+
9+
def __init__(self, client: Mainframe, job_id: JobDict) -> None:
10+
self._client = client
11+
self._job_id = job_id
12+
13+
# keep this method async for future use
14+
async def update_node_weight(self, node_id: str, weight: int | float) -> None:
15+
"""
16+
Update the weight of a node.
17+
:param node_id: The ID of the node to update.
18+
:param weight: The new weight for the node.
19+
"""
20+
if not isinstance(weight, (int, float)) or not math.isfinite(weight) or weight < 0:
21+
raise ValueError("Weight must be a non-negative finite number.")
22+
23+
self._client.send(self._job_id, {
24+
"type": "BlockRequest",
25+
"action": "UpdateNodeWeight",
26+
"node_id": node_id,
27+
"weight": weight,
28+
"request_id": random_string(16),
29+
})
30+
31+
def random_string(length=8):
32+
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

0 commit comments

Comments
 (0)