Loading…
Loading…
Manages the lifecycle, health, and configuration of cloud-agnostic relay VMs used for API discovery ingest, including auto-healing and monitoring.
The Discovery Ingest Health Service is a worker component responsible for the complete lifecycle management of Discovery Ingest Relay Virtual Machines (VMs). These relay VMs are dedicated instances that receive high-volume syslog data from API gateways and forward it to the platform's central ingest service.
This service automates the provisioning, configuration, health monitoring, and maintenance of these relays. It operates in a cloud-agnostic manner by interacting with the underlying cloud infrastructure through a generic RelayVmGateway interface. Its primary function is to periodically reconcile the state of all active relays against their desired configuration, ensuring they are healthy and operational.
Key responsibilities include:
The service runs as a background process within the worker, iterating through all configured relays to enforce their state without manual intervention.
The DiscoveryIngestHealthService executes its main logic within the reconcileActiveRelays method. This process runs periodically, performing a reconciliation loop for every active and enabled relay instance defined in the database.
sequenceDiagram
participant Worker as Health Service
participant DB as Database
participant Cloud as Cloud Provider Gateway
participant Blob as Object Store
participant VM as Relay VM
Worker->>DB: Load active relay targets
Worker->>DB: Read desired container image digest
loop For each relay target
Worker->>DB: Check if discovery is enabled for gateway type
alt Discovery is disabled
opt deprovisionOnDiscoveryDisabled is true
Worker->>Cloud: Delete VM instance
Worker->>DB: Mark relay as deprovisioned
else
Worker->>Worker: Log S2R-INGEST-ORPHAN (once)
end
continue
end
Worker->>Blob: Publish runtime config, get signed URL
Worker->>Cloud: Ensure firewall rule and internal IP exist
Worker->>Cloud: Get VM instance details
alt VM does not exist
Worker->>Cloud: Create new VM instance
end
Worker->>VM: Probe /healthz endpoint
alt VM is unhealthy
opt Outside startup grace period
Worker->>Worker: Dispatch async VM recreate task
Worker->>DB: Update status to 'recreating'
else
Worker->>DB: Update status to 'starting'
end
else VM is healthy
Worker->>Worker: Classify image, heartbeat, disk, & throughput
opt Image is stale
Worker->>DB: Update status to 'stale-image'
Worker->>Worker: Log S2R-RELAY-DIVERGENCE
end
opt Heartbeat is stale
Worker->>Worker: Log S2R-RELAY-NO-HEARTBEAT
end
opt Disk pressure > grow threshold
Worker->>Cloud: Resize VM boot disk
Worker->>Worker: Log S2R-RELAY-DISK-GROW
end
opt Throughput shedding > warn threshold
Worker->>Worker: Log S2R-RELAY-SHEDDING
end
Worker->>DB: Update status to 'ok' with latest metrics
end
endThe reconciliation process for a single relay follows these steps:
s2r_f5_instance and s2r_f5_sync_state tables for all relays that are marked as enabled.s2r_discovery_vendor_enabled table. If discovery for the corresponding gateway type is explicitly disabled, the service takes action based on the S2R_INGEST_DEPROVISION_ON_DISCOVERY_DISABLED setting:
false (default), it logs an S2R-INGEST-ORPHAN warning once and takes no further action.true, it deprovisions the relay VM via the RelayVmGateway and updates its status in the database.ensureRelayHealthy.
ObjectStore. It obtains a 24-hour signed URL for the VM to download this configuration.RelayVmGateway./healthz endpoint.relayStartupGrace window, the service dispatches an asynchronous task to recreate it. This task, managed by RelayRecreateDispatcher, deletes the old VM and creates a new one without blocking the main reconciliation loop. The recreation attempt is subject to a 15-minute timeout./healthz endpoint:
s2r_system_setting table. If the digests differ for longer than the relayDivergenceGrace period, the relay is marked as stale-image.relayHeartbeatStale duration, an S2R-RELAY-NO-HEARTBEAT warning is logged.relayDiskWarnPercent and relayDiskGrowPercent. If usage is high, it logs S2R-RELAY-DISK-PRESSURE. If it exceeds the growth threshold and relayDiskAutoExtendEnabled is true, it triggers a boot disk resize, up to relayDiskMaxGb.relayDropWarnPercent, an S2R-RELAY-SHEDDING warning is logged.s2r_f5_sync_state table.When creating or recreating a relay VM, the service generates a comprehensive startup script. This script is responsible for bootstrapping the VM's environment, installing necessary software, and configuring all required services. This ensures that each VM is provisioned identically and includes self-healing mechanisms.
Below is an example of the structure and key components of the generated script.
#!/bin/bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
# 1. Install self-healing and reconciliation scripts
mkdir -p /opt /usr/local/share/s2r
install -m 0755 ... /opt/update_relay.sh
install -m 0755 ... /opt/relay_reconcile.sh
# 2. Install systemd service to ensure scripts exist on boot
cat >/etc/systemd/system/s2r-update-relay-installer.service <<'...'
# ... service definition ...
systemctl enable s2r-update-relay-installer.service
# 3. Install dependencies
apt-get update
apt-get install -y python3 ca-certificates curl docker.io cloud-guest-utils e2fsprogs xfsprogs
# 4. Configure log rotation to cap disk usage
# Docker container logs
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <<'DOCKERLOGEOF'
{
"log-driver": "json-file",
"log-opts": { "max-size": "100m", "max-file": "3" }
}
DOCKERLOGEOF
# Systemd journal
# ... journald.conf.d cap ...
# /var/log/syslog via hourly cron
# ... logrotate config and cron job ...
# 5. Configure and start Docker
systemctl enable docker
systemctl restart docker
# 6. Authenticate with artifact registry
# ... obtains token and runs docker login ...
# 7. Write environment and config files for services
cat >/etc/s2r-discovery-ingest.env <<'ENVEOF'
# ... env vars for local ingest container ...
ENVEOF
cat >/etc/s2r-discovery-ingest-reconcile.env <<'RECENVEOF'
# ... env vars for self-heal script ...
RECENVEOF
cat >/etc/s2r-discovery-ingest-relay-bootstrap.json <<'CFGEOF'
# ... bootstrap config for python relay script ...
CFGEOF
# 8. Install systemd units for services
cat >/etc/systemd/system/s2r-discovery-ingest-local.service <<'...'
# ... unit for local ingest container ...
cat >/etc/systemd/system/s2r-discovery-ingest-relay.service <<'...'
# ... unit for python syslog relay ...
cat >/etc/systemd/system/s2r-discovery-ingest-reconcile.timer <<'...'
# ... timer for jittered self-heal ...
# 9. Retire legacy pre-rename services to prevent conflicts
for legacy_unit in s2r-relay-reconcile.timer ...; do
systemctl stop "$legacy_unit" >/dev/null 2>&1 || true
# ... disable, rm, docker rm ...
done
systemctl daemon-reload
# 10. Pull container image and start all services
docker pull <local_ingest_image>
systemctl enable s2r-discovery-ingest-local.service
systemctl restart s2r-discovery-ingest-local.service
# ... enable and restart other services and timers ...
This script configures the VM with:
s2r-discovery-ingest-local) running the worker image to act as a local ingest endpoint.s2r-discovery-ingest-relay.service) that listens for syslog and forwards to the local ingest container.s2r-discovery-ingest-reconcile.timer) that periodically runs a script to ensure the ingest container is running the desired image version.journald, and /var/log/syslog to prevent disk exhaustion.The service exposes one primary public method for initiating the reconciliation process.
reconcileActiveRelaysTriggers a full reconciliation of all active discovery ingest relays.
Map<String, Object> containing a summary of the actions taken during the run.Example Success Response:
{
"status": "DONE",
"targetCount": 5,
"healthy": 4,
"starting": 0,
"recreated": 1,
"staleImage": 0,
"noHeartbeat": 0,
"orphanLogged": 0,
"deprovisioned": 0,
"diskGrown": 1,
"shedding": 0,
"failed": 0,
"time": "2023-10-27T10:00:00.123456Z"
}
Response Fields:
| Field | Type | Description |
|---|---|---|
status | String | The final status of the reconciliation run. Always DONE on completion. |
targetCount | Integer | The total number of active relay targets processed. |
healthy | Integer | The number of relays found to be healthy and operational. |
starting | Integer | The number of relays within their startup grace period and not yet healthy. |
recreated | Integer | The number of relays that were newly created or recreated this run. |
staleImage | Integer | The number of relays running a divergent container image. |
noHeartbeat | Integer | The number of relays that have missed their database heartbeat. |
orphanLogged | Integer | The number of orphaned relays that were logged but not deprovisioned. |
deprovisioned | Integer | The number of orphaned relays that were successfully deprovisioned. |
diskGrown | Integer | The number of relays whose boot disks were automatically resized. |
shedding | Integer | The number of relays detected to be shedding syslog messages. |
failed | Integer | The number of relays that failed to reconcile due to an error. |
time | String | The UTC timestamp when the reconciliation summary was generated. |
The behavior of the DiscoveryIngestHealthService and the relay VMs it manages is controlled by environment variables. The service supports new variable names with a one-release fallback to legacy names for backward compatibility.
IMPORTANT
The service reads its configuration at startup. Changes to these environment variables require a restart of the worker process to take effect.
S2R_RELAY_VM_PROVIDER | String | gcp | Selects the cloud provider implementation for the RelayVmGateway.
| S2R_PROJECT_ID | String | "" | The default cloud project ID for relay VM resources.
| S2R_DISCOVERY_INGEST_RELAY_REGION | String | me-west1 | Default cloud region for relay VMs. Falls back to S2R_F5_RELAY_REGION, then S2R_REGION.S2R_DISCOVERY_INGEST_RELAY_ZONE | String | [region]-b | Default cloud zone for relay VMs. Falls back to S2R_F5_RELAY_ZONE.S2R_DISCOVERY_INGEST_RELAY_MACHINE_TYPE | String | e2-standard-4 | Machine type for relay VMs. Falls back to S2R_F5_RELAY_MACHINE_TYPE.S2R_DISCOVERY_INGEST_RELAY_IMAGE_FAMILY | String | debian-12 | Base OS image family for relay VMs. Falls back to S2R_F5_RELAY_IMAGE_FAMILY.S2R_DISCOVERY_INGEST_RELAY_IMAGE_PROJECT | String | debian-cloud | Cloud project containing the base OS image. Falls back to S2R_F5_RELAY_IMAGE_PROJECT.S2R_DISCOVERY_INGEST_VM_INGEST_IMAGE | String | [region]-docker.pkg.dev/[proj]/[repo]/s2r-worker:dev | The full reference to the Docker image for the local ingest container on the relay VM. Falls back to S2R_F5_VM_INGEST_IMAGE.S2R_DISCOVERY_INGEST_RELAY_VM_SERVICE_ACCOUNT | String | s2r-worker-sa@[proj].iam.gserviceaccount.com | The email of the service account assigned to the relay VM. Falls back to S2R_F5_RELAY_VM_SERVICE_ACCOUNT.S2R_INGEST_DEPROVISION_ON_DISCOVERY_DISABLED | Boolean | false | If true, the service will deprovision relays whose discovery source is disabled. Otherwise, it only logs a warning.S2R_DISCOVERY_INGEST_RELAY_SYSLOG_PORT | Integer | 514 | TCP/UDP port on the relay VM for receiving syslog data. Falls back to S2R_F5_RELAY_SYSLOG_PORT.S2R_DISCOVERY_INGEST_RELAY_HEALTH_PORT | Integer | 18080 | Port on the relay VM for the health check endpoint (/healthz). Falls back to S2R_F5_RELAY_HEALTH_PORT.| S2R_DISCOVERY_INGEST_RELAY_LOCAL_INGEST_PORT | Integer | 18081 | Internal port used by the syslog forwarder to talk to the local ingest container. Falls back to S2R_F5_RELAY_LOCAL_INGEST_PORT.