Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit23c2ee0

Browse files
committed
[Core][Label scheduling 8/n]Add length and illegal letters validation to the node labels
Signed-off-by: LarryLian <554538252@qq.com>
1 parent905796e commit23c2ee0

File tree

6 files changed

+125
-20
lines changed

6 files changed

+125
-20
lines changed

‎python/ray/_private/ray_constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,6 @@ def gcs_actor_scheduling_enabled():
479479
"RAY_WORKER_PROCESS_SETUP_HOOK_LOAD_TIMEOUT"# noqa
480480
)
481481

482-
RAY_DEFAULT_LABEL_KEYS_PREFIX="ray.io/"
483-
484482
RAY_TPU_MAX_CONCURRENT_CONNECTIONS_ENV_VAR="RAY_TPU_MAX_CONCURRENT_ACTIVE_CONNECTIONS"
485483

486484

‎python/ray/_private/utils.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,16 +1940,40 @@ def parse_node_labels_json(
19401940
returnlabels
19411941

19421942

1943+
defis_valid_label_string(s):
1944+
iflen(s)==0:
1945+
returnTrue
1946+
pattern=r"^[a-zA-Z0-9\-_./]+$"
1947+
returnbool(re.match(pattern,s))
1948+
1949+
19431950
defvalidate_node_labels(labels:Dict[str,str]):
19441951
iflabelsisNone:
19451952
return
1946-
forkeyinlabels.keys():
1947-
ifkey.startswith(ray_constants.RAY_DEFAULT_LABEL_KEYS_PREFIX):
1953+
forkey,valueinlabels.items():
1954+
ifkey.startswith(ray._raylet.RAY_DEFAULT_LABEL_KEY_PREFIX):
19481955
raiseValueError(
19491956
f"Custom label keys `{key}` cannot start with the prefix "
1950-
f"`{ray_constants.RAY_DEFAULT_LABEL_KEYS_PREFIX}`. "
1957+
f"`{ray._raylet.RAY_DEFAULT_LABEL_KEY_PREFIX}`. "
19511958
f"This is reserved for Ray defined labels."
19521959
)
1960+
if (
1961+
len(key)>=ray._raylet.RAY_LABEL_MAX_LENGTH
1962+
orlen(key)==0
1963+
orlen(value)>=ray._raylet.RAY_LABEL_MAX_LENGTH
1964+
):
1965+
raiseValueError(
1966+
"The keys and values length of custom lables "
1967+
"cannot be empty or exceed "
1968+
f"{ray._raylet.RAY_LABEL_MAX_LENGTH-1} characters, "
1969+
f"invalid label:{key}:{value}."
1970+
)
1971+
if (notis_valid_label_string(key))or (notis_valid_label_string(value)):
1972+
raiseValueError(
1973+
"The keys and values of custom lables must "
1974+
"consist of letters from `a-z0-9A-Z` or `-` or `_` or `.` or `/`, "
1975+
f"invalid label:{key}:{value}."
1976+
)
19531977

19541978

19551979
defpasre_pg_formatted_resources_to_original(

‎python/ray/includes/common.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,5 @@ cdef extern from "ray/common/constants.h" nogil:
572572
cdefint kResourceUnitScaling
573573
cdef constchar[] kImplicitResourcePrefix
574574
cdefint kStreamingGeneratorReturn
575+
cdefint kRayLabelMaxLength
576+
cdef constchar[] kRayDefaultLabelKeyPrefix

‎python/ray/includes/common.pxi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ from ray.includes.common cimport (
1212
kResourceUnitScaling,
1313
kImplicitResourcePrefix,
1414
kStreamingGeneratorReturn,
15+
kRayLabelMaxLength,
16+
kRayDefaultLabelKeyPrefix,
1517
)
1618

1719

@@ -40,3 +42,5 @@ WORKER_PROCESS_SETUP_HOOK_KEY_NAME_GCS = str(kWorkerSetupHookKeyName)
4042
RESOURCE_UNIT_SCALING= kResourceUnitScaling
4143
IMPLICIT_RESOURCE_PREFIX= kImplicitResourcePrefix.decode()
4244
STREAMING_GENERATOR_RETURN= kStreamingGeneratorReturn
45+
RAY_LABEL_MAX_LENGTH= kRayLabelMaxLength
46+
RAY_DEFAULT_LABEL_KEY_PREFIX= kRayDefaultLabelKeyPrefix.decode()

‎python/ray/tests/test_node_labels.py

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
1+
importcopy
12
importos
23
importsys
34
importpytest
45
importsubprocess
6+
importjson
57

68
importray
79
fromray.cluster_utilsimportAutoscalingCluster
810
fromray._private.test_utilsimportwait_for_condition
911

12+
LONG_STR_VALID="".join("a"for_inrange(ray._raylet.RAY_LABEL_MAX_LENGTH-1))
13+
LONG_STR_EXCEEDED="".join("a"for_inrange(ray._raylet.RAY_LABEL_MAX_LENGTH))
14+
ALL_LETTERS_VALID_STR="abc.com/1234567890-_.abcdefghijklmnopqrstuvwxyzABC"
15+
VALID_LABELS= {
16+
"gpu_type":"A100",
17+
"region":"us",
18+
ALL_LETTERS_VALID_STR:ALL_LETTERS_VALID_STR,
19+
LONG_STR_VALID:LONG_STR_VALID,
20+
"empty_value":"",
21+
}
22+
VALID_LABELS_JSON=json.dumps(VALID_LABELS,separators=(",",":"))
23+
24+
ILLEGA_LENGTH_PROMPT= (
25+
f"cannot be empty or exceed{ray._raylet.RAY_LABEL_MAX_LENGTH-1} characters"
26+
)
27+
ILLEGA_LETTERS_PROMPT= (
28+
"consist of letters from `a-z0-9A-Z` or `-` or `_` or `.` or `/`"
29+
)
30+
1031

1132
defcheck_cmd_stderr(cmd):
1233
returnsubprocess.run(cmd,stderr=subprocess.PIPE).stderr.decode("utf-8")
1334

1435

1536
defadd_default_labels(node_info,labels):
16-
labels["ray.io/node_id"]=node_info["NodeID"]
17-
returnlabels
37+
final_labels=copy.deepcopy(labels)
38+
final_labels["ray.io/node_id"]=node_info["NodeID"]
39+
returnfinal_labels
1840

1941

2042
@pytest.mark.parametrize(
2143
"call_ray_start",
22-
['ray start --head --labels={"gpu_type":"A100","region":"us"}'],
44+
[f"ray start --head --labels={VALID_LABELS_JSON}"],
2345
indirect=True,
2446
)
2547
deftest_ray_start_set_node_labels(call_ray_start):
2648
ray.init(address=call_ray_start)
2749
node_info=ray.nodes()[0]
28-
assertnode_info["Labels"]==add_default_labels(
29-
node_info, {"gpu_type":"A100","region":"us"}
30-
)
50+
assertnode_info["Labels"]==add_default_labels(node_info,VALID_LABELS)
3151

3252

3353
@pytest.mark.parametrize(
@@ -44,10 +64,9 @@ def test_ray_start_set_empty_node_labels(call_ray_start):
4464

4565

4666
deftest_ray_init_set_node_labels(shutdown_only):
47-
labels= {"gpu_type":"A100","region":"us"}
48-
ray.init(labels=labels)
67+
ray.init(labels=VALID_LABELS)
4968
node_info=ray.nodes()[0]
50-
assertnode_info["Labels"]==add_default_labels(node_info,labels)
69+
assertnode_info["Labels"]==add_default_labels(node_info,VALID_LABELS)
5170
ray.shutdown()
5271
ray.init(labels={})
5372
node_info=ray.nodes()[0]
@@ -57,20 +76,55 @@ def test_ray_init_set_node_labels(shutdown_only):
5776
deftest_ray_init_set_node_labels_value_error(ray_start_cluster):
5877
cluster=ray_start_cluster
5978

79+
# cluster.add_node api of node labels.
6080
key="ray.io/node_id"
6181
withpytest.raises(
6282
ValueError,
6383
match=f"Custom label keys `{key}` cannot start with the prefix `ray.io/`",
6484
):
6585
cluster.add_node(num_cpus=1,labels={key:"111111"})
6686

87+
withpytest.raises(
88+
ValueError,
89+
match=ILLEGA_LENGTH_PROMPT,
90+
):
91+
cluster.add_node(num_cpus=1,labels={LONG_STR_EXCEEDED:"111111"})
92+
93+
withpytest.raises(
94+
ValueError,
95+
match=ILLEGA_LETTERS_PROMPT,
96+
):
97+
cluster.add_node(num_cpus=1,labels={"key":"aa%$22"})
98+
99+
# ray.init api of node labels.
67100
key="ray.io/other_key"
68101
withpytest.raises(
69102
ValueError,
70103
match=f"Custom label keys `{key}` cannot start with the prefix `ray.io/`",
71104
):
72105
ray.init(labels={key:"value"})
73106

107+
withpytest.raises(ValueError,match=ILLEGA_LENGTH_PROMPT):
108+
ray.init(labels={LONG_STR_EXCEEDED:"123"})
109+
110+
withpytest.raises(ValueError,match=ILLEGA_LENGTH_PROMPT):
111+
ray.init(labels={"":"123"})
112+
113+
withpytest.raises(ValueError,match=ILLEGA_LENGTH_PROMPT):
114+
ray.init(labels={"key":LONG_STR_EXCEEDED})
115+
116+
withpytest.raises(
117+
ValueError,
118+
match=ILLEGA_LETTERS_PROMPT,
119+
):
120+
ray.init(labels={"key":"abc@ss"})
121+
122+
withpytest.raises(
123+
ValueError,
124+
match=ILLEGA_LETTERS_PROMPT,
125+
):
126+
ray.init(labels={"k y":"123"})
127+
74128
cluster.add_node(num_cpus=1)
75129
withpytest.raises(ValueError,match="labels must not be provided"):
76130
ray.init(address=cluster.address,labels={"gpu_type":"A100"})
@@ -96,15 +150,35 @@ def test_ray_start_set_node_labels_value_error():
96150
)
97151
assert"cannot start with the prefix `ray.io/`"inout
98152

153+
# Validate illegal length of keys and values.
154+
out=check_cmd_stderr(
155+
["ray","start","--head",f'--labels={{"{LONG_STR_EXCEEDED}":"111"}}']
156+
)
157+
assertILLEGA_LENGTH_PROMPTinout
158+
159+
out=check_cmd_stderr(
160+
["ray","start","--head",f'--labels={{"key":"{LONG_STR_EXCEEDED}"}}']
161+
)
162+
assertILLEGA_LENGTH_PROMPTinout
163+
164+
out=check_cmd_stderr(["ray","start","--head",'--labels={"":"111"}'])
165+
assertILLEGA_LENGTH_PROMPTinout
166+
167+
# Validate illegal letters of keys and values.
168+
out=check_cmd_stderr(["ray","start","--head",'--labels={"?$#s":"123"}'])
169+
assertILLEGA_LETTERS_PROMPTinout
170+
171+
out=check_cmd_stderr(["ray","start","--head",'--labels={"key":"`1~~"}'])
172+
assertILLEGA_LETTERS_PROMPTinout
173+
99174

100175
deftest_cluster_add_node_with_labels(ray_start_cluster):
101-
labels= {"gpu_type":"A100","region":"us"}
102176
cluster=ray_start_cluster
103-
cluster.add_node(num_cpus=1,labels=labels)
177+
cluster.add_node(num_cpus=1,labels=VALID_LABELS)
104178
cluster.wait_for_nodes()
105179
ray.init(address=cluster.address)
106180
node_info=ray.nodes()[0]
107-
assertnode_info["Labels"]==add_default_labels(node_info,labels)
181+
assertnode_info["Labels"]==add_default_labels(node_info,VALID_LABELS)
108182
head_node_id=ray.nodes()[0]["NodeID"]
109183

110184
cluster.add_node(num_cpus=1,labels={})

‎src/ray/common/constants.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ constexpr char kLibraryPathEnvName[] = "PATH";
9393
constexprcharkLibraryPathEnvName[] ="LD_LIBRARY_PATH";
9494
#endif
9595

96-
#defineRAY_LABEL_KEY_PREFIX"ray.io/"
96+
#defineRAY_DEFAULT_LABEL_KEY_PREFIX"ray.io/"
97+
constexprcharkRayDefaultLabelKeyPrefix[] = RAY_DEFAULT_LABEL_KEY_PREFIX;
9798
/// Default node label key: node_id
98-
constexprcharkLabelKeyNodeID[] = RAY_LABEL_KEY_PREFIX"node_id";
99-
#undef RAY_LABEL_KEY_PREFIX
99+
constexprcharkLabelKeyNodeID[] = RAY_DEFAULT_LABEL_KEY_PREFIX"node_id";
100+
#undef RAY_DEFAULT_LABEL_KEY_PREFIX
101+
102+
constexprintkRayLabelMaxLength =128;
100103

101104
/// All nodes implicitly have resources with this prefix and the quantity is 1.
102105
/// NOTE: DON'T CHANGE THIS since autoscaler depends on it.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp