1use std::fmt::Display;
16use std::path;
17use std::time::Duration;
18
19use common_telemetry::{debug, error, info, warn};
20use opendal::layers::{LoggingInterceptor, LoggingLayer, RetryInterceptor, TracingLayer};
21use opendal::raw::{AccessorInfo, HttpClient, Operation};
22use opendal::{Error, ErrorKind};
23use snafu::ResultExt;
24
25use crate::config::HttpClientConfig;
26use crate::{error, ObjectStore};
27
28pub fn join_dir(parent: &str, child: &str) -> String {
36 let output = format!("{parent}/{child}/");
38 normalize_dir(&output)
39}
40
41pub fn normalize_dir(v: &str) -> String {
61 let has_root = v.starts_with('/');
62 let mut v = v
63 .split('/')
64 .filter(|v| !v.is_empty())
65 .collect::<Vec<&str>>()
66 .join("/");
67 if has_root {
68 v.insert(0, '/');
69 }
70 if !v.ends_with('/') {
71 v.push('/')
72 }
73 v
74}
75
76pub fn join_path(parent: &str, child: &str) -> String {
81 let output = format!("{parent}/{child}");
82 normalize_path(&output)
83}
84
85pub fn normalize_path(path: &str) -> String {
97 let path = path.trim();
99
100 if path.is_empty() {
102 return "/".to_string();
103 }
104
105 let has_leading = path.starts_with('/');
106 let has_trailing = path.ends_with('/');
107
108 let mut p = path
109 .split('/')
110 .filter(|v| !v.is_empty())
111 .collect::<Vec<_>>()
112 .join("/");
113
114 if !p.starts_with('/') && has_leading {
116 p.insert(0, '/');
117 }
118
119 if !p.ends_with('/') && has_trailing {
121 p.push('/');
122 }
123
124 p
125}
126
127pub fn with_instrument_layers(object_store: ObjectStore, path_label: bool) -> ObjectStore {
129 object_store
130 .layer(LoggingLayer::new(DefaultLoggingInterceptor))
131 .layer(TracingLayer)
132 .layer(crate::layers::build_prometheus_metrics_layer(path_label))
133}
134
135static LOGGING_TARGET: &str = "opendal::services";
136
137struct LoggingContext<'a>(&'a [(&'a str, &'a str)]);
138
139impl Display for LoggingContext<'_> {
140 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141 for (i, (k, v)) in self.0.iter().enumerate() {
142 if i > 0 {
143 write!(f, " {}={}", k, v)?;
144 } else {
145 write!(f, "{}={}", k, v)?;
146 }
147 }
148 Ok(())
149 }
150}
151
152#[derive(Debug, Copy, Clone, Default)]
153pub struct DefaultLoggingInterceptor;
154
155impl LoggingInterceptor for DefaultLoggingInterceptor {
156 #[inline]
157 fn log(
158 &self,
159 info: &AccessorInfo,
160 operation: Operation,
161 context: &[(&str, &str)],
162 message: &str,
163 err: Option<&opendal::Error>,
164 ) {
165 if let Some(err) = err {
166 if err.kind() == ErrorKind::Unexpected {
168 error!(
169 target: LOGGING_TARGET,
170 "service={} name={} {}: {operation} {message} {err:#?}",
171 info.scheme(),
172 info.name(),
173 LoggingContext(context),
174 );
175 } else {
176 debug!(
177 target: LOGGING_TARGET,
178 "service={} name={} {}: {operation} {message} {err}",
179 info.scheme(),
180 info.name(),
181 LoggingContext(context),
182 );
183 };
184 }
185
186 debug!(
187 target: LOGGING_TARGET,
188 "service={} name={} {}: {operation} {message}",
189 info.scheme(),
190 info.name(),
191 LoggingContext(context),
192 );
193 }
194}
195
196pub(crate) fn build_http_client(config: &HttpClientConfig) -> error::Result<HttpClient> {
197 if config.skip_ssl_validation {
198 common_telemetry::warn!("Skipping SSL validation for object storage HTTP client. Please ensure the environment is trusted.");
199 }
200
201 let client = reqwest::ClientBuilder::new()
202 .pool_max_idle_per_host(config.pool_max_idle_per_host as usize)
203 .connect_timeout(config.connect_timeout)
204 .pool_idle_timeout(config.pool_idle_timeout)
205 .timeout(config.timeout)
206 .danger_accept_invalid_certs(config.skip_ssl_validation)
207 .build()
208 .context(error::BuildHttpClientSnafu)?;
209 Ok(HttpClient::with(client))
210}
211
212pub fn clean_temp_dir(dir: &str) -> error::Result<()> {
213 if path::Path::new(&dir).exists() {
214 info!("Begin to clean temp storage directory: {}", dir);
215 std::fs::remove_dir_all(dir).context(error::RemoveDirSnafu { dir })?;
216 info!("Cleaned temp storage directory: {}", dir);
217 }
218
219 Ok(())
220}
221
222pub struct PrintDetailedError;
224
225impl RetryInterceptor for PrintDetailedError {
227 fn intercept(&self, err: &Error, dur: Duration) {
228 warn!("Retry after {}s, error: {:#?}", dur.as_secs_f64(), err);
229 }
230}
231
232#[cfg(test)]
233mod tests {
234 use super::*;
235
236 #[test]
237 fn test_normalize_dir() {
238 assert_eq!("/", normalize_dir("/"));
239 assert_eq!("/", normalize_dir(""));
240 assert_eq!("/test/", normalize_dir("/test"));
241 }
242
243 #[test]
244 fn test_join_dir() {
245 assert_eq!("/", join_dir("", ""));
246 assert_eq!("/", join_dir("/", ""));
247 assert_eq!("/", join_dir("", "/"));
248 assert_eq!("/", join_dir("/", "/"));
249 assert_eq!("/a/", join_dir("/a", ""));
250 assert_eq!("a/b/c/", join_dir("a/b", "c"));
251 assert_eq!("/a/b/c/", join_dir("/a/b", "c"));
252 assert_eq!("/a/b/c/", join_dir("/a/b", "c/"));
253 assert_eq!("/a/b/c/", join_dir("/a/b", "/c/"));
254 assert_eq!("/a/b/c/", join_dir("/a/b", "//c"));
255 }
256
257 #[test]
258 fn test_join_path() {
259 assert_eq!("/", join_path("", ""));
260 assert_eq!("/", join_path("/", ""));
261 assert_eq!("/", join_path("", "/"));
262 assert_eq!("/", join_path("/", "/"));
263 assert_eq!("a/", join_path("a", ""));
264 assert_eq!("/a", join_path("/", "a"));
265 assert_eq!("a/b/c.txt", join_path("a/b", "c.txt"));
266 assert_eq!("/a/b/c.txt", join_path("/a/b", "c.txt"));
267 assert_eq!("/a/b/c/", join_path("/a/b", "c/"));
268 assert_eq!("/a/b/c/", join_path("/a/b", "/c/"));
269 assert_eq!("/a/b/c.txt", join_path("/a/b", "//c.txt"));
270 assert_eq!("abc/def", join_path(" abc", "/def "));
271 assert_eq!("/abc", join_path("//", "/abc"));
272 assert_eq!("abc/def", join_path("abc/", "//def"));
273 }
274}