Skip to content

Commit

Permalink
Merge "Migrate lb query to use non batch API requests fixes: 383171958"
Browse files Browse the repository at this point in the history
-- Branch commit log --
commit d091fa4618845e87151df162970bb78ac23bc4f1
Author:  Joel Jacob <joeljacob@google.com>
Date:    2024-12-09T18:06:51-05:00

    Migrate lb query to use non batch API requests
fixes: 383171958

Change-Id: I669c752ccd83999fa1b677807eb7a8b3153a2a7f
GitOrigin-RevId: e4843bafe5d943953e3e469f829731ece20d562e
  • Loading branch information
Y0Q authored and copybara-github committed Dec 10, 2024
1 parent 6337eb9 commit 80ca5ea
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions gcpdiag/queries/lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,6 @@ def health_state(self) -> str:
return self._resource_data.get('healthState', 'UNHEALTHY')


def _generate_health_response_callback(
backend_heath_statuses: List[BackendHealth], group: str):

def health_response_callback(request_id, response, exception):
del request_id, exception

# None is returned when backend type doesn't support health check
if response is not None:
for health_status in response.get('healthStatus', []):
backend_heath_statuses.append(BackendHealth(health_status, group))

return health_response_callback


@caching.cached_api_call(in_memory=True)
def get_backend_service_health(
project_id: str,
Expand All @@ -340,34 +326,33 @@ def get_backend_service_health(
backend_heath_statuses: List[BackendHealth] = []

compute = apis.get_api('compute', 'v1', project_id)
batch = compute.new_batch_http_request()

for i, backend in enumerate(backend_service.backends):
for backend in backend_service.backends:
group = backend['group']
if not backend_service.region:
batch.add(
compute.backendServices().getHealth(
project=project_id,
backendService=backend_service.name,
body={'group': group},
),
request_id=str(i),
callback=_generate_health_response_callback(backend_heath_statuses,
group),
)
response = compute.backendServices().getHealth(
project=project_id,
backendService=backend_service.name,
body={
'group': group
},
).execute(num_retries=config.API_RETRIES)
# None is returned when backend type doesn't support health check
if response is not None:
for health_status in response.get('healthStatus', []):
backend_heath_statuses.append(BackendHealth(health_status, group))
else:
batch.add(
compute.regionBackendServices().getHealth(
project=project_id,
region=backend_service.region,
backendService=backend_service.name,
body={'group': group},
),
request_id=str(i),
callback=_generate_health_response_callback(backend_heath_statuses,
group),
)
batch.execute()
response = compute.regionBackendServices().getHealth(
project=project_id,
region=backend_service.region,
backendService=backend_service.name,
body={
'group': group
},
).execute(num_retries=config.API_RETRIES)
if response is not None:
for health_status in response.get('healthStatus', []):
backend_heath_statuses.append(BackendHealth(health_status, group))

return backend_heath_statuses

Expand Down

0 comments on commit 80ca5ea

Please # to comment.