// Package failpoint implements deterministic, environment-gated crash windows // for the lab5 fault-injection evidence. Nothing fires unless FAULTS_ENABLED=1 // and the action matches both FAULT_ and, for job-scoped faults, // FAULT_TARGET_JOB. Production and CI leave the variables unset. package failpoint import ( "log/slog" "os" "path/filepath" "time" "github.com/google/uuid" ) // Enabled reports whether fault injection is explicitly switched on. func Enabled() bool { return os.Getenv("FAULTS_ENABLED") == "1" } // Check exits the process if the action is armed for this job. It must be // called at the exact window the evidence describes; a marker line is written // before exit so the harness can prove the fault actually fired. func Check(action string, jobID uuid.UUID) { if !Enabled() { return } if os.Getenv("FAULT_"+action) != "exit" { return } target := os.Getenv("FAULT_TARGET_JOB") if target != "" && target != jobID.String() { return } // A one-shot marker survives the container restart performed by the // restart policy. Without it a targeted crash failpoint would loop forever. marker := filepath.Join(os.TempDir(), "messenger-failpoint-"+action+"-"+jobID.String()) if _, err := os.Stat(marker); err == nil { return } _ = os.WriteFile(marker, []byte("fired\n"), 0o600) slog.Default().Error("failpoint fired", slog.String("action", action), slog.String("job_id", jobID.String()), slog.Int("exit_code", 70), ) _ = os.Stdout.Sync() _ = os.Stderr.Sync() os.Exit(70) } // Mode returns the armed value for an action/job pair. Unlike Check it does not // exit; the worker uses it to inject a permanent or transient processing error. func Mode(action string, jobID uuid.UUID) string { if !Enabled() { return "" } target := os.Getenv("FAULT_TARGET_JOB") if target != "" && target != jobID.String() { return "" } return os.Getenv("FAULT_" + action) } // Sleep blocks for the configured duration when the action is armed for this // job. It is used for hang/slow-worker scenarios where the heartbeat must keep // running (the caller controls that separately). func Sleep(action string, jobID uuid.UUID) { if !Enabled() { return } raw := os.Getenv("FAULT_" + action) if raw == "" { return } target := os.Getenv("FAULT_TARGET_JOB") if target != "" && target != jobID.String() { return } delay, err := time.ParseDuration(raw) if err != nil { return } slog.Default().Warn("failpoint sleeping", slog.String("action", action), slog.String("job_id", jobID.String()), slog.Duration("duration", delay), ) time.Sleep(delay) }