common_meta/election/object_store/
probe.rs1use common_telemetry::warn;
16use object_store::util;
17use object_store::{ErrorKind, Metadata};
18use snafu::{IntoError, ResultExt};
19use uuid::Uuid;
20
21use crate::election::object_store::{AuthorityHead, AuthorityState, HeadStore};
22use crate::error::{self, Result};
23use crate::metrics::METRIC_AUTHORITY_STORAGE_EVENTS;
24
25impl HeadStore {
26 pub(crate) fn check_capabilities(&self) -> Result<()> {
27 let info = self.operator.info();
28 let scheme = info.scheme();
29 let reason = match scheme {
32 "s3" | "azblob" => None,
33 "gcs" => {
34 Some("GCS generation-based conditional writes require real-endpoint validation")
35 }
36 "oss" => Some("OSS PutObject rejects If-Match and If-None-Match conditions"),
37 "fs" | "memory" => {
38 Some("backend lacks the required atomic conditional replacement semantics")
39 }
40 _ => Some("backend has not been qualified for Authority Head conditional replacement"),
41 };
42 if let Some(reason) = reason {
43 return error::AuthorityCapabilitySnafu { scheme, reason }.fail();
44 }
45 let c = info.capability();
46 if !(c.read
47 && c.write
48 && c.list
49 && c.delete
50 && c.write_with_if_not_exists
51 && (c.write_with_if_version_match || c.write_with_if_match))
52 {
53 return error::AuthorityCapabilitySnafu {
54 scheme,
55 reason: "read/write/list/delete and atomic conditional create/replace are required",
56 }
57 .fail();
58 }
59 Ok(())
60 }
61
62 pub(crate) async fn probe_conditional_writes(&self) -> Result<()> {
63 let path = util::join_path(&self.probe_prefix, &format!("{}.json", Uuid::new_v4()));
64 let result = self.probe_at(&path).await;
65 let cleanup = self.cleanup_probe(&path).await;
66 if result.is_err() {
67 METRIC_AUTHORITY_STORAGE_EVENTS
68 .with_label_values(&["probe_failure"])
69 .inc();
70 if let Err(error) = cleanup {
71 warn!(error; "Authority probe cleanup also failed");
72 }
73 return result;
74 }
75 cleanup
76 }
77
78 async fn probe_at(&self, path: &str) -> Result<()> {
79 let initial = AuthorityHead::initial();
80 self.create_at(path, &initial).await?;
81 let first = self.read_at(path).await?;
82 self.require_probe(first.head() == &initial, "initial contents differ")?;
83 let next = initial.clone().with_next_write_seq()?;
84 match self.create_at(path, &next).await {
85 Err(error)
86 if matches!(
87 error.authority_storage_error().map(|e| e.kind()),
88 Some(ErrorKind::ConditionNotMatch | ErrorKind::AlreadyExists)
89 ) => {}
90 Err(error) => return Err(error),
91 Ok(()) => {
92 return error::AuthorityProbeSnafu {
93 reason: "duplicate create was accepted",
94 }
95 .fail();
96 }
97 }
98 let unchanged = self.read_at(path).await?;
99 self.verify_probe(&unchanged, &initial, first.metadata(), true)?;
100 self.replace_at(path, &first, &next).await?;
101 let current = self.read_at(path).await?;
102 self.verify_probe(¤t, &next, first.metadata(), false)?;
103 let stale = next.clone().with_next_write_seq()?;
104 match self.replace_at(path, &first, &stale).await {
105 Err(error)
106 if error.authority_storage_error().map(|e| e.kind())
107 == Some(ErrorKind::ConditionNotMatch) => {}
108 Err(error) => return Err(error),
109 Ok(()) => {
110 return error::AuthorityProbeSnafu {
111 reason: "stale replacement was accepted",
112 }
113 .fail();
114 }
115 }
116 let unchanged = self.read_at(path).await?;
117 self.verify_probe(&unchanged, &next, current.metadata(), true)?;
118 self.operator
120 .write(path, stale.encode()?)
121 .await
122 .context(error::AuthorityStorageSnafu {
123 operation: "probe plain write",
124 })?;
125 let plain = self.read_at(path).await?;
126 self.verify_probe(&plain, &stale, current.metadata(), false)?;
127 self.operator
128 .list(&self.probe_prefix)
129 .await
130 .context(error::AuthorityStorageSnafu {
131 operation: "probe list",
132 })?;
133 Ok(())
134 }
135
136 fn verify_probe(
137 &self,
138 state: &AuthorityState,
139 expected: &AuthorityHead,
140 metadata: &Metadata,
141 same: bool,
142 ) -> Result<()> {
143 self.require_probe(
144 state.head() == expected && self.same_version(state.metadata(), metadata)? == same,
145 "contents or condition identity differ from expected",
146 )
147 }
148
149 fn require_probe(&self, valid: bool, reason: &'static str) -> Result<()> {
150 if !valid {
151 return error::AuthorityProbeSnafu { reason }.fail();
152 }
153 Ok(())
154 }
155
156 pub(crate) async fn cleanup_probe(&self, path: &str) -> Result<()> {
157 self.operator.delete(path).await.map_err(|error| {
158 METRIC_AUTHORITY_STORAGE_EVENTS
159 .with_label_values(&["probe_cleanup_failure"])
160 .inc();
161 error::AuthorityStorageSnafu {
162 operation: "probe cleanup",
163 }
164 .into_error(error)
165 })
166 }
167}