Skip to content

Commit cb451c9

Browse files
authored
Merge pull request #590 from ddps-lab/azure-collector-fix20250916
Azure SPS 수집 모델, subscription-location 순회 로직 개선
2 parents 06bec43 + 3c7fb40 commit cb451c9

4 files changed

Lines changed: 49 additions & 20 deletions

File tree

collector/spot-dataset/azure/lambda/current_collector/load_price.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def get_price(skip_num):
5858
if response.status_code == 200:
5959
break
6060
else:
61-
time.sleep(1)
61+
time.sleep(2)
6262
response = requests.get(get_link)
6363

6464
if response.status_code != 200:

collector/spot-dataset/azure/lambda/current_collector/load_sps.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ def collect_spot_placement_score_first_time(desired_counts):
112112
elapsed = end_time - start_time
113113
minutes, seconds = divmod(int(elapsed), 60)
114114
print(f"Prepare the request pool. time: {minutes}min {seconds}sec")
115-
116115
return sps_res_availability_zones_true_df
117-
116+
return None
118117

119118
@log_execution_time
120119
def collect_spot_placement_score(desired_counts, instance_types=None):
@@ -459,6 +458,7 @@ def save_tmp_files_to_s3():
459458
f"{base_path}/{az_str}/{AZURE_CONST.S3_INVALID_REGIONS_JSON_FILENAME}": SS_Resources.invalid_regions_tmp,
460459
f"{base_path}/{az_str}/{AZURE_CONST.S3_INVALID_INSTANCE_TYPES_JSON_FILENAME}": SS_Resources.invalid_instance_types_tmp,
461460
f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_CALL_HISTORY_JSON_FILENAME}": SS_Resources.locations_call_history_tmp,
461+
f"{base_path}/{az_str}/{AZURE_CONST.S3_LAST_SUBSCRIPTION_ID_AND_LOCATION_JSON_FILENAME}": SS_Resources.last_subscription_id_and_location,
462462
f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_OVER_LIMIT_JSON_FILENAME}": SS_Resources.locations_over_limit_tmp
463463
}
464464

@@ -475,13 +475,14 @@ def get_variable_from_s3():
475475
instance_types_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_INVALID_INSTANCE_TYPES_JSON_FILENAME}", 'json')
476476
call_history_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_CALL_HISTORY_JSON_FILENAME}", 'json')
477477
over_limit_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_OVER_LIMIT_JSON_FILENAME}", 'json')
478-
last_location_index_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_LAST_SUBSCRIPTION_ID_AND_LOCATION_JSON_FILENAME}", 'json')
478+
location_index_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_LAST_SUBSCRIPTION_ID_AND_LOCATION_JSON_FILENAME}", 'json')
479479
region_map_and_instance_map = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_REGION_MAP_AND_INSTANCE_MAP_JSON_FILENAME}", 'json')
480480

481481
SS_Resources.invalid_regions_tmp = invalid_regions_data
482482
SS_Resources.invalid_instance_types_tmp = instance_types_data
483483
SS_Resources.locations_call_history_tmp = call_history_data
484484
SS_Resources.locations_over_limit_tmp = over_limit_data
485+
SS_Resources.last_location_index_data = location_index_data
485486
SS_Resources.region_map_and_instance_map_tmp = {
486487
"region_map": region_map_and_instance_map.get('region_map'),
487488
"instance_map": region_map_and_instance_map.get('instance_map')
@@ -492,6 +493,7 @@ def get_variable_from_s3():
492493
SS_Resources.invalid_instance_types_tmp,
493494
SS_Resources.locations_call_history_tmp,
494495
SS_Resources.locations_over_limit_tmp,
496+
SS_Resources.last_subscription_id_and_location,
495497
SS_Resources.region_map_and_instance_map_tmp
496498
]):
497499
print("[S3]: Successfully prepared variable from s3.")

collector/spot-dataset/azure/lambda/current_collector/sps_module/sps_location_manager.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def check_and_add_available_locations(az):
4747
return True
4848
else:
4949
print("No new available locations found. locations_call_history_tmp or locations_call_history_tmp unchanged.")
50+
return False
5051

5152
except Exception as e:
5253
print(f"Error in check_and_add_available_locations: {e}")
@@ -70,7 +71,7 @@ def validation_can_call(subscription_id, location):
7071

7172
def get_next_available_location():
7273
"""
73-
이 메서드는 사용 가능한 다음 위치를 리턴합니다.
74+
이 메서드는 사용 가능한 다음 location를 리턴합니다.
7475
호출 이력과 초과 요청 데이터를 기반으로 적절한 위치를 선택합니다.
7576
호출 시 이용하는 location은 구독 내에 지난 호출의 location을 이용 안 해야 하는 로직이 들어갑니다.
7677
@@ -81,34 +82,59 @@ def get_next_available_location():
8182
if SS_Resources.locations_call_history_tmp is None or SS_Resources.locations_over_limit_tmp is None:
8283
return None
8384

84-
# Clean expired data
8585
clean_expired_over_limit_locations()
8686
clean_expired_over_call_history_locations()
8787

88-
start_subscription_index = random.randint(0, len(SS_Resources.subscriptions) - 1)
88+
subs = SS_Resources.subscriptions
89+
locs = SS_Resources.available_locations
90+
if not subs or not locs:
91+
return None
92+
93+
n, m = len(subs), len(locs)
8994

90-
for i in range(len(SS_Resources.subscriptions)):
91-
subscription_index = (start_subscription_index + i) % len(SS_Resources.subscriptions)
92-
subscription_id = SS_Resources.subscriptions[subscription_index]
95+
last_pair = getattr(SS_Resources, "last_subscription_id_and_location", None) or {}
96+
last_sub_id = last_pair.get("last_subscription_id")
97+
last_loc = last_pair.get("last_location")
9398

94-
start_location_index = random.randint(0, len(SS_Resources.available_locations) - 1)
95-
for j in range(len(SS_Resources.available_locations)):
96-
location_index = (start_location_index + j) % len(SS_Resources.available_locations)
97-
location = SS_Resources.available_locations[location_index]
99+
if last_sub_id in subs and last_loc in locs:
100+
s_idx = subs.index(last_sub_id)
101+
l_idx = locs.index(last_loc)
102+
l_idx = (l_idx + 1) % m
103+
if l_idx == 0:
104+
s_idx = (s_idx + 1) % n
105+
else:
106+
s_idx, l_idx = 0, 0
98107

99-
if validation_can_call(subscription_id, location):
100-
SS_Resources.succeed_to_get_next_available_location_count += 1
101-
SS_Resources.succeed_to_get_next_available_location_count_all += 1
102-
SS_Resources.locations_call_history_tmp[subscription_id][location].append(datetime.now(timezone.utc).replace(tzinfo=None).isoformat())
103-
return subscription_id, location
108+
attempts = 0
109+
while attempts < n * m:
110+
sub_id = subs[s_idx]
111+
loc = locs[l_idx]
104112

105-
return None
113+
if validation_can_call(sub_id, loc):
114+
SS_Resources.succeed_to_get_next_available_location_count += 1
115+
SS_Resources.succeed_to_get_next_available_location_count_all += 1
116+
117+
SS_Resources.locations_call_history_tmp[sub_id][loc].append(
118+
datetime.now(timezone.utc).replace(tzinfo=None).isoformat())
119+
120+
SS_Resources.last_subscription_id_and_location = {
121+
"last_subscription_id": sub_id,
122+
"last_location": loc,
123+
}
124+
return sub_id, loc
125+
126+
l_idx = (l_idx + 1) % m
127+
if l_idx == 0:
128+
s_idx = (s_idx + 1) % n
129+
130+
attempts += 1
106131

107132
except Exception as e:
108133
print("\n[ERROR] Exception occurred in get_next_available_location:")
109134
print(traceback.format_exc())
110135
print(f"\n[ERROR] Failed to get_next_available_location: {e}")
111136
return None
137+
return None
112138

113139

114140
def collect_available_locations():

collector/spot-dataset/azure/lambda/current_collector/sps_module/sps_shared_resources.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
invalid_instance_types_tmp = None
1919
locations_call_history_tmp = None
2020
locations_over_limit_tmp = None
21+
last_subscription_id_and_location = None
2122
region_map_and_instance_map_tmp = None
2223
subscriptions = None
2324
available_locations = None

0 commit comments

Comments
 (0)