Repository navigation
Expand file tree
/
Copy pathfault.rs
More file actions
133 lines (132 loc) · 4.67 KB
/
Copy pathfault.rs
File metadata and controls
133 lines (132 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Process-external file latches; entirely absent from release control flow.
use anyhow::{Result, ensure};
use postgres::Client;
pub const STAGES: &[&str] = &[
"after_source_selection",
"after_input_checkpoint",
"before_worker_apply",
"after_worker_compute",
"after_lease",
"before_ticks",
"after_ticks",
"before_bidtopid",
"after_bidtopid",
"before_ptptstats",
"after_ptptstats",
"before_main",
"after_main",
"before_commit",
"after_commit",
"after_ack",
"before_restore",
"after_restore",
"before_cursor",
"after_cursor",
"before_sweep",
"after_sweep",
"bundle_pinned",
"before_companion_join",
"cache_eviction_contends_with_same_zid_update",
];
#[derive(Default)]
pub struct Fault {
#[cfg(feature = "fault-injection")]
directory: Option<std::path::PathBuf>,
}
impl Fault {
pub fn reject_release_control() -> Result<()> {
#[cfg(not(feature = "fault-injection"))]
ensure!(
std::env::var_os("P026_FAULT_DIR").is_none(),
"FAULT-CONTROL-REFUSED"
);
Ok(())
}
pub fn new(client: &mut Client, namespace: &str) -> Result<Self> {
Self::reject_release_control()?;
#[cfg(feature = "fault-injection")]
{
let directory = std::env::var_os("P026_FAULT_DIR").map(std::path::PathBuf::from);
if directory.is_some() {
ensure!(
!matches!(namespace, "prod" | "preprod" | "dev"),
"test namespace refused"
);
let marker: bool = client
.query_one(
"SELECT EXISTS(SELECT 1 FROM p026_test_marker WHERE namespace=$1)",
&[&namespace],
)?
.get(0);
ensure!(marker, "public-fixture database/namespace marker required");
}
Ok(Self { directory })
}
#[cfg(not(feature = "fault-injection"))]
{
let _ = (client, namespace);
Ok(Self {})
}
}
/// Only the fault build may enable Python's public-fixture-database latches.
pub fn bridge_control(&self) -> Result<serde_json::Value> {
#[cfg(feature = "fault-injection")]
if let Some(dir) = &self.directory {
let arm = dir.join("arm.json");
if arm.exists() {
return Ok(serde_json::from_slice(&std::fs::read(arm)?)?);
}
}
Ok(serde_json::Value::Null)
}
pub fn bridge_resumed(&self, stage: &str, worker_pid: u32) -> Result<()> {
#[cfg(feature = "fault-injection")]
if let Some(dir) = &self.directory {
std::fs::write(
dir.join("bridge-resumed.tmp"),
serde_json::to_vec(&serde_json::json!({"stage":stage,"worker_pid":worker_pid}))?,
)?;
std::fs::rename(
dir.join("bridge-resumed.tmp"),
dir.join("bridge-resumed.json"),
)?;
}
let _ = (stage, worker_pid);
Ok(())
}
pub fn hit(&self, stage: &str, context: &serde_json::Value) -> Result<()> {
#[cfg(feature = "fault-injection")]
if let Some(dir) = &self.directory {
use std::{
fs, thread,
time::{Duration, Instant},
};
let arm = dir.join("arm.json");
if arm.exists() {
let request: serde_json::Value = serde_json::from_slice(&fs::read(&arm)?)?;
if request["stage"] == stage {
ensure!(
request["protocol"] == "polis-fault-control/1",
"invalid fault protocol"
);
let ack = serde_json::json!({"protocol":"polis-fault-control/1", "stage":stage,
"run_id":request["run_id"], "operation_id":request["operation_id"],
"pid":std::process::id(), "state":"reached-and-blocked", "context":context});
fs::write(dir.join("ack.tmp"), serde_json::to_vec(&ack)?)?;
fs::rename(dir.join("ack.tmp"), dir.join("ack.json"))?;
let start = Instant::now();
while !dir.join("release").exists() {
ensure!(
start.elapsed() < Duration::from_secs(180),
"fault latch timeout"
);
thread::sleep(Duration::from_millis(10));
}
fs::remove_file(arm)?;
}
}
}
let _ = (stage, context);
Ok(())
}
}