Skip to content

Commit 76b7f24

Browse files
Merge pull request #326 from jembi/CU-86bzu8m1g_Increase-Lease-in-Dgraph-while-restoring-Backup-using-API
CU-86bzu8m1g - Increase Lease in Dgraph while restoring Backup using API
2 parents ad7083b + 66f0152 commit 76b7f24

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

devops/linux/docker/backup_restore/dgraph-restore-api.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,47 @@
99

1010
host = env_vars['NODE1_IP']
1111
port = "50010"
12+
endpoint = "http://" + host + ":6080"
13+
new_lease_value = 50000000000000
14+
15+
def get_current_lease(endpoint):
16+
"""Fetches the current maxLeasedUid from the Dgraph Zero /state endpoint."""
17+
try:
18+
response = requests.get(f"{endpoint}/state")
19+
response.raise_for_status() # Raise an exception for HTTP errors
20+
data = response.json()
21+
# Extracting the maxLeasedUid from the JSON response
22+
max_leased_uid = data.get('maxUID', None)
23+
if max_leased_uid is not None:
24+
print(f"Current maxLeasedUid: {max_leased_uid}")
25+
return max_leased_uid
26+
else:
27+
print("Error: maxLeasedUid not found in the response.")
28+
return None
29+
except requests.RequestException as e:
30+
print(f"Error fetching current lease: {e}")
31+
return None
32+
33+
def increase_lease(endpoint, new_value):
34+
"""Increases the lease value if it's below the new_value."""
35+
current_value = get_current_lease(endpoint)
36+
current_value = int(current_value)
37+
if current_value is None:
38+
print("Unable to fetch the current lease value. Exiting.")
39+
return
40+
41+
if current_value < new_value:
42+
try:
43+
# Assuming a POST request with form data to increase lease size
44+
data = {'what': 'uids', 'num': new_value}
45+
response = requests.get(f"{endpoint}/assign", params=data)
46+
response.raise_for_status() # Raise an exception for HTTP errors
47+
print(f"Lease increased to {new_value}.")
48+
except requests.RequestException as e:
49+
print(f"Error increasing lease: {e}")
50+
print(f"Response Content: {response.content}") # Debug print to see response content
51+
else:
52+
print(f"Current lease ({current_value}) is already greater than or equal to {new_value}. No action taken.")
1253

1354

1455
def main(json_file):
@@ -54,16 +95,15 @@ def convert_datetime_format(date_str):
5495
continue
5596
else:
5697
return date_str # If the format is not correct, return the original string
57-
98+
5899
output_format = "%Y-%m-%dT%H:%M:%S.%fZ"
59100
output_str = dt.strftime(output_format)
60101
output_str = output_str[:26] + 'Z' # Keep only the first 2 decimal places of the seconds part
61102
return output_str
62103

63104
def process_json_data(golden_records):
64-
105+
increase_lease(endpoint, new_lease_value)
65106
for golden_record in golden_records:
66-
67107
golden_record['goldenRecord']['uniqueGoldenRecordData']['auxDateCreated'] = convert_datetime_format(golden_record['goldenRecord']['uniqueGoldenRecordData']['auxDateCreated'])
68108
for interaction in golden_record['interactionsWithScore']:
69109
interaction['interaction']['uniqueInteractionData']['auxDateCreated'] = convert_datetime_format(
@@ -73,7 +113,7 @@ def process_json_data(golden_records):
73113
response = send_golden_record_to_api(golden_record)
74114
if response:
75115
print("After Restore Golden ID--"+ response.text)
76-
116+
77117
def send_golden_record_to_api(golden_record_payload):
78118
get_expanded_golden_record_url = f'http://{host}:{port}/JeMPI/restoreGoldenRecord'
79119
# Normalize date fields in the payload

0 commit comments

Comments
 (0)