const std = @import("std"); const config = @import("../config.zig"); const grpc_executor = @import("../protocol/grpc_executor.zig"); const http_executor = @import("../protocol/http_executor.zig"); const runner_control = @import("../runner/control.zig"); const runner_executor = @import("../runner/executor.zig"); const runner_log = @import("../runner/log.zig"); const runner_metrics = @import("../runner/metrics.zig"); const template = @import("../template.zig"); const workflow_iteration = @import("iteration.zig"); const workflow_vu = @import("vu.zig"); const http = std.http; const TlsContext = http_executor.TlsContext; const WorkflowVu = workflow_vu.WorkflowVu; const WorkflowExecutor = struct { allocator: std.mem.Allocator, client: *http.Client, grpc_client: *grpc_executor.Client, shared_store: *template.VarStore, tls_context: *TlsContext, scenario: *const config.ScenarioConfig, aggregator: *runner_metrics.MetricsAggregator, warmup_end: i128, logger: *runner_log.Logger, vus: []WorkflowVu, initialized: usize, next_spare_vu_id: *u32, max_vus: usize, healthy_vus: *usize, poisoned_workers: *u32, pub fn preConstantVus(self: *WorkflowExecutor) bool { return self.healthy_vus.* == 0; } pub fn constantVusSlots(self: *WorkflowExecutor) usize { return self.initialized; } pub fn dispatchConstantVus(self: *WorkflowExecutor, slot: usize) !bool { const vu = &self.vus[slot]; if (vu.poisoned) return false; return try self.executeOnVu(vu); } pub fn preArrivalDispatch(self: *WorkflowExecutor) bool { return self.healthy_vus.* == 0; } pub fn dispatchArrival(self: *WorkflowExecutor, rr_index: *usize) !bool { const target_index = workflow_vu.findNextHealthyIndex(self.vus[0..self.initialized], rr_index.*) orelse { return true; }; rr_index.* = target_index + 1; return try self.executeOnVu(&self.vus[target_index]); } pub fn preRampingVus(self: *WorkflowExecutor) bool { return self.healthy_vus.* == 0; } pub fn dispatchRampingVus(self: *WorkflowExecutor, slot: usize, active_vus: usize) !bool { const target_index = workflow_vu.findNextHealthyIndex(self.vus[0..active_vus], slot) orelse { return false; }; return try self.executeOnVu(&self.vus[target_index]); } fn executeOnVu(self: *WorkflowExecutor, vu: *WorkflowVu) !bool { const outcome = try workflow_iteration.execute( self.allocator, self.client, self.grpc_client, self.tls_context, self.scenario, vu, self.aggregator, self.warmup_end, &runner_control.stop_requested, ); return try self.handleOutcome(vu, outcome); } fn handleOutcome(self: *WorkflowExecutor, vu: *WorkflowVu, outcome: workflow_iteration.Outcome) !bool { switch (outcome) { .completed => return false, .recreate => { const recreate_now = std.time.nanoTimestamp(); self.aggregator.recordVuRecreated(recreate_now, self.warmup_end); if (workflow_vu.noteRecreated(vu, recreate_now, self.scenario.recovery)) { self.aggregator.recordUnhealthyVu(recreate_now, self.warmup_end); if (!try replaceWorkflowVuWithSpare( self.allocator, self.shared_store, self.scenario, vu, self.next_spare_vu_id, self.max_vus, self.aggregator, self.warmup_end, recreate_now, self.logger, )) { return true; } return false; } if (!workflow_vu.recreate(self.allocator, self.scenario, vu, self.logger, runner_control.workflowHostCheckAbort)) { self.aggregator.recordUnhealthyVu(recreate_now, self.warmup_end); if (!try replaceWorkflowVuWithSpare( self.allocator, self.shared_store, self.scenario, vu, self.next_spare_vu_id, self.max_vus, self.aggregator, self.warmup_end, recreate_now, self.logger, )) { return true; } } return false; }, .poisoned => { return workflow_vu.poison(vu, self.healthy_vus, self.poisoned_workers, self.scenario.execution.max_poisoned_workers); }, } } }; pub fn run( allocator: std.mem.Allocator, client: *http.Client, grpc_client: *grpc_executor.Client, shared_store: *template.VarStore, tls_context: *TlsContext, scenario: *const config.ScenarioConfig, aggregator: *runner_metrics.MetricsAggregator, warmup_end: i128, measured_end: i128, next_progress: *i128, abort_on_fail_enabled: bool, logger: *runner_log.Logger, ) !bool { const vu_count = switch (scenario.execution.mode) { .constant_vus => scenario.execution.vus.?, .constant_arrival_rate => scenario.execution.pre_allocated_vus.?, .ramping_vus => scenario.execution.maxRampingVus(), }; const max_vus: usize = scenario.execution.max_vus orelse vu_count; var vus = try allocator.alloc(WorkflowVu, vu_count); defer allocator.free(vus); var initialized: usize = 0; var next_spare_vu_id: u32 = @intCast(vu_count + 1); var healthy_vus: usize = 0; var poisoned_workers: u32 = 0; defer { for (vus[0..initialized]) |*vu| vu.deinit(); } var index: usize = 0; while (index < vu_count) : (index += 1) { if (!try initializeWorkflowVuSlot( allocator, shared_store, scenario, &vus[index], @intCast(index + 1), &next_spare_vu_id, max_vus, aggregator, warmup_end, logger, )) { return true; } initialized += 1; } healthy_vus = initialized; var executor = WorkflowExecutor{ .allocator = allocator, .client = client, .grpc_client = grpc_client, .shared_store = shared_store, .tls_context = tls_context, .scenario = scenario, .aggregator = aggregator, .warmup_end = warmup_end, .logger = logger, .vus = vus, .initialized = initialized, .next_spare_vu_id = &next_spare_vu_id, .max_vus = max_vus, .healthy_vus = &healthy_vus, .poisoned_workers = &poisoned_workers, }; return try runner_executor.run(WorkflowExecutor, &executor, .{ .scenario = scenario, .aggregator = aggregator, .warmup_end = warmup_end, .measured_end = measured_end, .next_progress = next_progress, .stop_requested = &runner_control.stop_requested, .abort_on_fail_enabled = abort_on_fail_enabled, .logger = logger, }); } fn initializeWorkflowVuSlot( allocator: std.mem.Allocator, shared_store: *template.VarStore, scenario: *const config.ScenarioConfig, vu: *WorkflowVu, preferred_vu_id: u32, next_spare_vu_id: *u32, max_vus: usize, aggregator: *runner_metrics.MetricsAggregator, warmup_end: i128, logger: *runner_log.Logger, ) !bool { const previous_next_spare = next_spare_vu_id.*; var candidate_id = preferred_vu_id; while (true) { workflow_vu.initialize(allocator, shared_store, scenario, vu, candidate_id, logger, runner_control.workflowHostCheckAbort) catch { if (candidate_id == preferred_vu_id and candidate_id <= max_vus) { if (@as(usize, next_spare_vu_id.*) > max_vus) return false; candidate_id = next_spare_vu_id.*; next_spare_vu_id.* += 1; continue; } return false; }; if (next_spare_vu_id.* != previous_next_spare) { aggregator.recordSpareVuActivated(std.time.nanoTimestamp(), warmup_end); } return true; } } fn replaceWorkflowVuWithSpare( allocator: std.mem.Allocator, shared_store: *template.VarStore, scenario: *const config.ScenarioConfig, vu: *WorkflowVu, next_spare_vu_id: *u32, max_vus: usize, aggregator: *runner_metrics.MetricsAggregator, warmup_end: i128, now_nanos: i128, logger: *runner_log.Logger, ) !bool { if (@as(usize, next_spare_vu_id.*) > max_vus) return false; vu.deinit(); aggregator.recordSpareVuActivated(now_nanos, warmup_end); return try workflow_vu.replaceWithSpare( allocator, shared_store, scenario, vu, next_spare_vu_id, max_vus, logger, runner_control.workflowHostCheckAbort, ); }