|
19 | 19 | use self::error::{CreateStreamError, StreamError};
|
20 | 20 | use super::base_path_without_preceding_slash;
|
21 | 21 | use super::cluster::utils::{merge_quried_stats, IngestionStats, QueriedStats, StorageStats};
|
22 |
| -use super::cluster::{fetch_daily_stats_from_ingestors, fetch_stats_from_ingestors}; |
| 22 | +use super::cluster::{ |
| 23 | + fetch_daily_stats_from_ingestors, fetch_stats_from_ingestors, sync_streams_with_ingestors, |
| 24 | +}; |
23 | 25 | use crate::alerts::Alerts;
|
24 | 26 | use crate::handlers::{
|
25 | 27 | CUSTOM_PARTITION_KEY, STATIC_SCHEMA_FLAG, TIME_PARTITION_KEY, TIME_PARTITION_LIMIT_KEY,
|
@@ -169,89 +171,14 @@ pub async fn get_alert(req: HttpRequest) -> Result<impl Responder, StreamError>
|
169 | 171 |
|
170 | 172 | pub async fn put_stream(req: HttpRequest, body: Bytes) -> Result<impl Responder, StreamError> {
|
171 | 173 | let stream_name: String = req.match_info().get("logstream").unwrap().parse().unwrap();
|
172 |
| - let (time_partition, time_partition_limit, custom_partition, static_schema_flag, update_stream) = |
173 |
| - fetch_headers_from_put_stream_request(&req); |
174 |
| - |
175 |
| - if metadata::STREAM_INFO.stream_exists(&stream_name) && update_stream != "true" { |
176 |
| - // Error if the log stream already exists |
177 |
| - return Err(StreamError::Custom { |
178 |
| - msg: format!( |
179 |
| - "Logstream {stream_name} already exists, please create a new log stream with unique name" |
180 |
| - ), |
181 |
| - status: StatusCode::BAD_REQUEST, |
182 |
| - }); |
183 |
| - } |
184 |
| - |
185 |
| - if !time_partition.is_empty() && update_stream == "true" { |
186 |
| - return Err(StreamError::Custom { |
187 |
| - msg: "Altering the time partition of an existing stream is restricted.".to_string(), |
188 |
| - status: StatusCode::BAD_REQUEST, |
189 |
| - }); |
190 |
| - } |
191 |
| - let mut time_partition_in_days: &str = ""; |
192 |
| - if !time_partition_limit.is_empty() { |
193 |
| - let time_partition_days = validate_time_partition_limit(&time_partition_limit); |
194 |
| - if let Err(err) = time_partition_days { |
195 |
| - return Err(StreamError::CreateStream(err)); |
196 |
| - } else { |
197 |
| - time_partition_in_days = time_partition_days.unwrap(); |
198 |
| - if update_stream == "true" { |
199 |
| - if let Err(err) = update_time_partition_limit_in_stream( |
200 |
| - stream_name.clone(), |
201 |
| - time_partition_in_days, |
202 |
| - ) |
203 |
| - .await |
204 |
| - { |
205 |
| - return Err(StreamError::CreateStream(err)); |
206 |
| - } |
207 |
| - return Ok(("Log stream updated", StatusCode::OK)); |
208 |
| - } |
209 |
| - } |
210 |
| - } |
211 |
| - |
212 |
| - if !static_schema_flag.is_empty() && update_stream == "true" { |
213 |
| - return Err(StreamError::Custom { |
214 |
| - msg: "Altering the schema of an existing stream is restricted.".to_string(), |
215 |
| - status: StatusCode::BAD_REQUEST, |
216 |
| - }); |
217 |
| - } |
218 | 174 |
|
219 |
| - if !custom_partition.is_empty() { |
220 |
| - if let Err(err) = validate_custom_partition(&custom_partition) { |
221 |
| - return Err(StreamError::CreateStream(err)); |
222 |
| - } |
223 |
| - if update_stream == "true" { |
224 |
| - if let Err(err) = |
225 |
| - update_custom_partition_in_stream(stream_name.clone(), &custom_partition).await |
226 |
| - { |
227 |
| - return Err(StreamError::CreateStream(err)); |
228 |
| - } |
229 |
| - return Ok(("Log stream updated", StatusCode::OK)); |
230 |
| - } |
231 |
| - } |
232 |
| - |
233 |
| - let schema = validate_static_schema( |
234 |
| - &body, |
235 |
| - &stream_name, |
236 |
| - &time_partition, |
237 |
| - &custom_partition, |
238 |
| - &static_schema_flag, |
239 |
| - ); |
240 |
| - if let Err(err) = schema { |
241 |
| - return Err(StreamError::CreateStream(err)); |
| 175 | + if CONFIG.parseable.mode == Mode::Query { |
| 176 | + create_update_stream(&req, &body, &stream_name).await?; |
| 177 | + sync_streams_with_ingestors(req, body, &stream_name).await?; |
| 178 | + } else { |
| 179 | + create_update_stream(&req, &body, &stream_name).await?; |
242 | 180 | }
|
243 | 181 |
|
244 |
| - create_stream( |
245 |
| - stream_name, |
246 |
| - &time_partition, |
247 |
| - time_partition_in_days, |
248 |
| - &custom_partition, |
249 |
| - &static_schema_flag, |
250 |
| - schema.unwrap(), |
251 |
| - false, |
252 |
| - ) |
253 |
| - .await?; |
254 |
| - |
255 | 182 | Ok(("Log stream created", StatusCode::OK))
|
256 | 183 | }
|
257 | 184 |
|
@@ -355,6 +282,96 @@ fn validate_static_schema(
|
355 | 282 | Ok(schema)
|
356 | 283 | }
|
357 | 284 |
|
| 285 | +async fn create_update_stream( |
| 286 | + req: &HttpRequest, |
| 287 | + body: &Bytes, |
| 288 | + stream_name: &str, |
| 289 | +) -> Result<(), StreamError> { |
| 290 | + let (time_partition, time_partition_limit, custom_partition, static_schema_flag, update_stream) = |
| 291 | + fetch_headers_from_put_stream_request(req); |
| 292 | + |
| 293 | + if metadata::STREAM_INFO.stream_exists(stream_name) && update_stream != "true" { |
| 294 | + // Error if the log stream already exists |
| 295 | + return Err(StreamError::Custom { |
| 296 | + msg: format!( |
| 297 | + "Logstream {stream_name} already exists, please create a new log stream with unique name" |
| 298 | + ), |
| 299 | + status: StatusCode::BAD_REQUEST, |
| 300 | + }); |
| 301 | + } |
| 302 | + |
| 303 | + if !time_partition.is_empty() && update_stream == "true" { |
| 304 | + return Err(StreamError::Custom { |
| 305 | + msg: "Altering the time partition of an existing stream is restricted.".to_string(), |
| 306 | + status: StatusCode::BAD_REQUEST, |
| 307 | + }); |
| 308 | + } |
| 309 | + let mut time_partition_in_days: &str = ""; |
| 310 | + if !time_partition_limit.is_empty() { |
| 311 | + let time_partition_days = validate_time_partition_limit(&time_partition_limit); |
| 312 | + if let Err(err) = time_partition_days { |
| 313 | + return Err(StreamError::CreateStream(err)); |
| 314 | + } else { |
| 315 | + time_partition_in_days = time_partition_days.unwrap(); |
| 316 | + if update_stream == "true" { |
| 317 | + if let Err(err) = update_time_partition_limit_in_stream( |
| 318 | + stream_name.to_string(), |
| 319 | + time_partition_in_days, |
| 320 | + ) |
| 321 | + .await |
| 322 | + { |
| 323 | + return Err(StreamError::CreateStream(err)); |
| 324 | + } |
| 325 | + return Ok(()); |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + if !static_schema_flag.is_empty() && update_stream == "true" { |
| 331 | + return Err(StreamError::Custom { |
| 332 | + msg: "Altering the schema of an existing stream is restricted.".to_string(), |
| 333 | + status: StatusCode::BAD_REQUEST, |
| 334 | + }); |
| 335 | + } |
| 336 | + |
| 337 | + if !custom_partition.is_empty() { |
| 338 | + if let Err(err) = validate_custom_partition(&custom_partition) { |
| 339 | + return Err(StreamError::CreateStream(err)); |
| 340 | + } |
| 341 | + if update_stream == "true" { |
| 342 | + if let Err(err) = |
| 343 | + update_custom_partition_in_stream(stream_name.to_string(), &custom_partition).await |
| 344 | + { |
| 345 | + return Err(StreamError::CreateStream(err)); |
| 346 | + } |
| 347 | + return Ok(()); |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + let schema = validate_static_schema( |
| 352 | + body, |
| 353 | + stream_name, |
| 354 | + &time_partition, |
| 355 | + &custom_partition, |
| 356 | + &static_schema_flag, |
| 357 | + ); |
| 358 | + if let Err(err) = schema { |
| 359 | + return Err(StreamError::CreateStream(err)); |
| 360 | + } |
| 361 | + |
| 362 | + create_stream( |
| 363 | + stream_name.to_string(), |
| 364 | + &time_partition, |
| 365 | + time_partition_in_days, |
| 366 | + &custom_partition, |
| 367 | + &static_schema_flag, |
| 368 | + schema.unwrap(), |
| 369 | + false, |
| 370 | + ) |
| 371 | + .await?; |
| 372 | + |
| 373 | + Ok(()) |
| 374 | +} |
358 | 375 | pub async fn put_alert(
|
359 | 376 | req: HttpRequest,
|
360 | 377 | body: web::Json<serde_json::Value>,
|
@@ -471,7 +488,7 @@ pub async fn get_cache_enabled(req: HttpRequest) -> Result<impl Responder, Strea
|
471 | 488 | _ => {}
|
472 | 489 | }
|
473 | 490 |
|
474 |
| - let cache_enabled = STREAM_INFO.cache_enabled(&stream_name)?; |
| 491 | + let cache_enabled = STREAM_INFO.get_cache_enabled(&stream_name)?; |
475 | 492 | Ok((web::Json(cache_enabled), StatusCode::OK))
|
476 | 493 | }
|
477 | 494 |
|
@@ -545,7 +562,7 @@ pub async fn put_enable_cache(
|
545 | 562 | .put_stream_manifest(&stream_name, &stream_metadata)
|
546 | 563 | .await?;
|
547 | 564 |
|
548 |
| - STREAM_INFO.set_stream_cache(&stream_name, enable_cache)?; |
| 565 | + STREAM_INFO.set_cache_enabled(&stream_name, enable_cache)?; |
549 | 566 | Ok((
|
550 | 567 | format!("Cache set to {enable_cache} for log stream {stream_name}"),
|
551 | 568 | StatusCode::OK,
|
|
0 commit comments