Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenStack COS: Ignore openstack online runners #288

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/openstack_cloud/openstack_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def reconcile(self, quantity: int) -> int:

end_ts = time.time()
self._issue_reconciliation_metrics(
ssh_connection=conn, reconciliation_start_ts=start_ts, reconciliation_end_ts=end_ts
conn=conn, reconciliation_start_ts=start_ts, reconciliation_end_ts=end_ts
)

return delta
Expand Down Expand Up @@ -1482,7 +1482,7 @@ def _issue_runner_installed_metric(

def _issue_reconciliation_metrics(
self,
ssh_connection: SshConnection,
conn: OpenstackConnection,
reconciliation_start_ts: float,
reconciliation_end_ts: float,
) -> None:
Expand All @@ -1491,45 +1491,52 @@ def _issue_reconciliation_metrics(
This includes the metrics for the runners and the reconciliation metric itself.

Args:
ssh_connection: The SSH connection to the runner instance.
conn: The connection object to access OpenStack cloud.
reconciliation_start_ts: The timestamp of when reconciliation started.
reconciliation_end_ts: The timestamp of when reconciliation ended.
"""
runner_states = self._get_openstack_runner_status(ssh_connection)
runner_states = self._get_openstack_runner_status(conn)

metric_stats = self._issue_runner_metrics(runner_states)
metric_stats = self._issue_runner_metrics(conn)
self._issue_reconciliation_metric(
metric_stats=metric_stats,
reconciliation_start_ts=reconciliation_start_ts,
reconciliation_end_ts=reconciliation_end_ts,
runner_states=runner_states,
)

def _issue_runner_metrics(self, runner_states: RunnerByHealth) -> IssuedMetricEventsStats:
def _issue_runner_metrics(self, conn: OpenstackConnection) -> IssuedMetricEventsStats:
"""Issue runner metrics.

Args:
runner_states: The states of the runners.
conn: The connection object to access OpenStack cloud.

Returns:
The stats of issued metric events.
"""
total_stats: IssuedMetricEventsStats = {}

try:
online_runners = {
runner_info.runner_name
for runner_info in self.get_github_runner_info()
if runner_info.online
}
except GithubApiError:
openstack_instances = self._get_openstack_instances(conn)
except openstack.exceptions.SDKException:
logger.exception(
"Failed to retrieve set of online runners. Will not issue runner metrics"
"Failed to get openstack instances to ignore when extracting metrics."
" Will not issue runner metrics"
)
return total_stats

# Don't extract metrics for instances which are still there, as it might be
# the case that the metrics have not yet been pulled
# (they get pulled right before server termination).
os_online_runners = {
instance.name
for instance in openstack_instances
if instance.status == _INSTANCE_STATUS_ACTIVE
}

for extracted_metrics in runner_metrics.extract(
metrics_storage_manager=metrics_storage,
ignore_runners=set(runner_states.healthy) | online_runners,
ignore_runners=os_online_runners,
):
try:
job_metrics = github_metrics.job(
Expand Down
88 changes: 64 additions & 24 deletions tests/unit/test_openstack_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from metrics.runner import RUNNER_INSTALLED_TS_FILE_NAME
from metrics.storage import MetricsStorage
from openstack_cloud import openstack_manager
from openstack_cloud.openstack_manager import MAX_METRICS_FILE_SIZE
from openstack_cloud.openstack_manager import MAX_METRICS_FILE_SIZE, METRICS_EXCHANGE_PATH
from runner_type import RunnerByHealth, RunnerGithubInfo

CLOUD_NAME = "microstack"
Expand Down Expand Up @@ -639,19 +639,19 @@ def test_reconcile_pulls_metric_files(
monkeypatch.setattr(openstack_manager.metrics_storage, "create", MagicMock(return_value=ms))
monkeypatch.setattr(openstack_manager.metrics_storage, "get", MagicMock(return_value=ms))
openstack_manager_for_reconcile._get_openstack_runner_status = MagicMock(
return_value=RunnerByHealth(healthy=("test_runner",), unhealthy=())
)
test_file_content = secrets.token_hex(16)
ssh_connection_mock.get.side_effect = lambda remote, local: Path(local).write_text(
test_file_content
return_value=RunnerByHealth(healthy=(), unhealthy=("test_runner",))
)

ssh_connection_mock.get.side_effect = MagicMock()
openstack_manager_for_reconcile.reconcile(quantity=0)

assert (ms.path / "pre-job-metrics.json").exists()
assert (ms.path / "pre-job-metrics.json").read_text() == test_file_content
assert (ms.path / "post-job-metrics.json").exists()
assert (ms.path / "post-job-metrics.json").read_text() == test_file_content
ssh_connection_mock.get.assert_any_call(
remote=str(METRICS_EXCHANGE_PATH / "pre-job-metrics.json"),
local=str(ms.path / "pre-job-metrics.json"),
)
ssh_connection_mock.get.assert_any_call(
remote=str(METRICS_EXCHANGE_PATH / "post-job-metrics.json"),
local=str(ms.path / "post-job-metrics.json"),
)


def test_reconcile_does_not_pull_too_large_files(
Expand Down Expand Up @@ -735,47 +735,87 @@ def test_reconcile_issue_reconciliation_metrics(
)


def test_reconcile_ignores_metrics_for_healthy_and_online_runners(
openstack_manager_for_reconcile, monkeypatch, tmp_path
def test_reconcile_ignores_metrics_for_openstack_online_runners(
openstack_manager_for_reconcile,
monkeypatch,
tmp_path,
patched_create_connection_context: MagicMock,
):
"""
arrange: Combination of runner status and github online status.
arrange: Combination of runner status/github status and openstack status.
act: Call reconcile.
assert: The healthy and online runners are ignored.
assert: The runners returned with status ACTIVE are ignored for metrics extraction.
"""
runner_metrics_path = tmp_path / "runner_fs"
runner_metrics_path.mkdir()
ms = MetricsStorage(path=runner_metrics_path, runner_name="test_runner")
monkeypatch.setattr(openstack_manager.metrics_storage, "create", MagicMock(return_value=ms))
monkeypatch.setattr(openstack_manager.metrics_storage, "get", MagicMock(return_value=ms))
instance_name = openstack_manager_for_reconcile.instance_name
runner_names = {
k: f"{instance_name}-{k}"
for k in [
"healthy_online",
"healthy_offline",
"unhealthy_online",
"unhealthy_offline",
"openstack_online_no_github_status",
]
}
openstack_manager_for_reconcile._get_openstack_runner_status = MagicMock(
return_value=RunnerByHealth(
healthy=("healthy_online", "healthy_offline"),
unhealthy=("unhealthy_online", "unhealthy_offline"),
healthy=(runner_names["healthy_online"], runner_names["healthy_offline"]),
unhealthy=(runner_names["unhealthy_online"], runner_names["unhealthy_offline"]),
)
)
openstack_manager_for_reconcile.get_github_runner_info = MagicMock(
return_value=(
RunnerGithubInfo(runner_name="healthy_online", runner_id=0, online=True, busy=True),
RunnerGithubInfo(runner_name="unhealthy_online", runner_id=1, online=True, busy=False),
RunnerGithubInfo(runner_name="healthy_offline", runner_id=2, online=False, busy=False),
RunnerGithubInfo(
runner_name="unhealthy_offline", runner_id=3, online=False, busy=False
runner_name=runner_names["healthy_online"], runner_id=0, online=True, busy=True
),
RunnerGithubInfo(
runner_name=runner_names["unhealthy_online"], runner_id=1, online=True, busy=False
),
RunnerGithubInfo(
runner_name=runner_names["healthy_offline"], runner_id=2, online=False, busy=False
),
RunnerGithubInfo(
runner_name=runner_names["unhealthy_offline"],
runner_id=3,
online=False,
busy=False,
),
)
)

openstack_manager.runner_metrics.extract.return_value = (MagicMock() for _ in range(2))
openstack_online_runner_names = [
runner_names["healthy_online"],
runner_names["healthy_offline"],
runner_names["unhealthy_online"],
runner_names["openstack_online_no_github_status"],
]
openstack_online_runners = [
openstack_manager.openstack.compute.v2.server.Server(name=runner_name, status="ACTIVE")
for runner_name in openstack_online_runner_names
]
openstack_offline_runners = [
openstack_manager.openstack.compute.v2.server.Server(name=runner_name, status="SHUTOFF")
for runner_name in [runner_names["unhealthy_offline"]]
]
patched_create_connection_context.list_servers.return_value = (
openstack_online_runners + openstack_offline_runners
)

openstack_manager.runner_metrics.extract.return_value = (MagicMock() for _ in range(1))
openstack_manager.runner_metrics.issue_events.side_effect = [
{metric_events.RunnerStart, metric_events.RunnerStop},
{metric_events.RunnerStart},
]

openstack_manager_for_reconcile.reconcile(quantity=0)

openstack_manager.runner_metrics.extract.assert_called_once_with(
metrics_storage_manager=metrics.storage,
ignore_runners={"healthy_online", "healthy_offline", "unhealthy_online"},
ignore_runners=set(openstack_online_runner_names),
)


Expand Down
Loading