Skip to content

Commit 815a5d6

Browse files
authored
Merge pull request #592 from ddps-lab/azure-collector-fix20250916
Azure SPS 수집 모델, subscription-location 순회 로직 개선(2차)
2 parents 04d4947 + 8f11bc6 commit 815a5d6

4 files changed

Lines changed: 49 additions & 26 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def collect_spot_placement_score_first_time(desired_counts):
114114
print(f"Prepare the request pool. time: {minutes}min {seconds}sec")
115115

116116
return sps_res_availability_zones_true_df
117+
return None
117118

118119

119120
@log_execution_time
@@ -459,6 +460,7 @@ def save_tmp_files_to_s3():
459460
f"{base_path}/{az_str}/{AZURE_CONST.S3_INVALID_REGIONS_JSON_FILENAME}": SS_Resources.invalid_regions_tmp,
460461
f"{base_path}/{az_str}/{AZURE_CONST.S3_INVALID_INSTANCE_TYPES_JSON_FILENAME}": SS_Resources.invalid_instance_types_tmp,
461462
f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_CALL_HISTORY_JSON_FILENAME}": SS_Resources.locations_call_history_tmp,
463+
f"{base_path}/{az_str}/{AZURE_CONST.S3_LAST_SUBSCRIPTION_ID_AND_LOCATION_JSON_FILENAME}": SS_Resources.last_subscription_id_and_location_tmp,
462464
f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_OVER_LIMIT_JSON_FILENAME}": SS_Resources.locations_over_limit_tmp
463465
}
464466

@@ -475,13 +477,14 @@ def get_variable_from_s3():
475477
instance_types_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_INVALID_INSTANCE_TYPES_JSON_FILENAME}", 'json')
476478
call_history_data = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_LOCATIONS_CALL_HISTORY_JSON_FILENAME}", 'json')
477479
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')
480+
last_subscription_id_and_location = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_LAST_SUBSCRIPTION_ID_AND_LOCATION_JSON_FILENAME}", 'json')
479481
region_map_and_instance_map = S3.read_file(f"{base_path}/{az_str}/{AZURE_CONST.S3_REGION_MAP_AND_INSTANCE_MAP_JSON_FILENAME}", 'json')
480482

481483
SS_Resources.invalid_regions_tmp = invalid_regions_data
482484
SS_Resources.invalid_instance_types_tmp = instance_types_data
483485
SS_Resources.locations_call_history_tmp = call_history_data
484486
SS_Resources.locations_over_limit_tmp = over_limit_data
487+
SS_Resources.last_subscription_id_and_location_tmp = last_subscription_id_and_location
485488
SS_Resources.region_map_and_instance_map_tmp = {
486489
"region_map": region_map_and_instance_map.get('region_map'),
487490
"instance_map": region_map_and_instance_map.get('instance_map')
@@ -492,6 +495,7 @@ def get_variable_from_s3():
492495
SS_Resources.invalid_instance_types_tmp,
493496
SS_Resources.locations_call_history_tmp,
494497
SS_Resources.locations_over_limit_tmp,
498+
SS_Resources.last_subscription_id_and_location_tmp,
495499
SS_Resources.region_map_and_instance_map_tmp
496500
]):
497501
print("[S3]: Successfully prepared variable from s3.")

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

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
22
import requests
33
import traceback
4-
import random
54
from sps_module import sps_shared_resources
65
from datetime import datetime, timedelta, timezone
76
from utils.pub_service import S3, AZURE_CONST
@@ -47,6 +46,7 @@ def check_and_add_available_locations(az):
4746
return True
4847
else:
4948
print("No new available locations found. locations_call_history_tmp or locations_call_history_tmp unchanged.")
49+
return True
5050

5151
except Exception as e:
5252
print(f"Error in check_and_add_available_locations: {e}")
@@ -69,46 +69,63 @@ def validation_can_call(subscription_id, location):
6969

7070

7171
def get_next_available_location():
72-
"""
73-
이 메서드는 사용 가능한 다음 위치를 리턴합니다.
74-
호출 이력과 초과 요청 데이터를 기반으로 적절한 위치를 선택합니다.
75-
호출 시 이용하는 location은 구독 내에 지난 호출의 location을 이용 안 해야 하는 로직이 들어갑니다.
76-
77-
이유:
78-
단 기간에 같은 location으로 호출 시, timeout율이 놉습니다.
79-
"""
8072
try:
8173
if SS_Resources.locations_call_history_tmp is None or SS_Resources.locations_over_limit_tmp is None:
8274
return None
8375

84-
# Clean expired data
8576
clean_expired_over_limit_locations()
8677
clean_expired_over_call_history_locations()
8778

88-
start_subscription_index = random.randint(0, len(SS_Resources.subscriptions) - 1)
79+
subs = SS_Resources.subscriptions
80+
locs = SS_Resources.available_locations
81+
if not subs or not locs:
82+
return None
8983

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]
84+
n, m = len(subs), len(locs)
9385

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]
86+
last_pair = getattr(SS_Resources, "last_subscription_id_and_location_tmp", None) or {}
87+
last_sub_id = last_pair.get("last_subscription_id")
88+
last_loc = last_pair.get("last_location")
9889

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
90+
if last_sub_id in subs and last_loc in locs:
91+
s_idx = subs.index(last_sub_id)
92+
l_idx = locs.index(last_loc)
93+
l_idx = (l_idx + 1) % m
94+
if l_idx == 0:
95+
s_idx = (s_idx + 1) % n
96+
else:
97+
s_idx, l_idx = 0, 0
10498

105-
return None
99+
attempts = 0
100+
while attempts < n * m:
101+
sub_id = subs[s_idx]
102+
loc = locs[l_idx]
103+
104+
if validation_can_call(sub_id, loc):
105+
SS_Resources.succeed_to_get_next_available_location_count += 1
106+
SS_Resources.succeed_to_get_next_available_location_count_all += 1
107+
108+
SS_Resources.locations_call_history_tmp[sub_id][loc].append(
109+
datetime.now(timezone.utc).replace(tzinfo=None).isoformat())
110+
111+
SS_Resources.last_subscription_id_and_location = {
112+
"last_subscription_id": sub_id,
113+
"last_location": loc,
114+
}
115+
return sub_id, loc
116+
117+
l_idx = (l_idx + 1) % m
118+
if l_idx == 0:
119+
s_idx = (s_idx + 1) % n
120+
121+
attempts += 1
106122

107123
except Exception as e:
108124
print("\n[ERROR] Exception occurred in get_next_available_location:")
109125
print(traceback.format_exc())
110126
print(f"\n[ERROR] Failed to get_next_available_location: {e}")
111127
return None
128+
return None
112129

113130

114131
def collect_available_locations():
@@ -144,6 +161,7 @@ def collect_available_locations():
144161
except Exception as e:
145162
print(f"Failed to collect_available_locations, Error: {e}")
146163
return None
164+
return None
147165

148166
def clean_expired_over_limit_locations():
149167
'''

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_tmp = None
2122
region_map_and_instance_map_tmp = None
2223
subscriptions = None
2324
available_locations = None

0 commit comments

Comments
 (0)