-
-
Notifications
You must be signed in to change notification settings - Fork 157
Handling SIGTERM for Distributed and Standalone Mode #907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d31e046
handle sigterm for readiness probe
AdheipSingh 6e67207
update
AdheipSingh 20dabd7
add sigterm to handler
AdheipSingh ae44336
actix to shutdown connections
AdheipSingh ddaf196
add license
AdheipSingh ca63c9e
120 seconds
AdheipSingh 245f52a
add log info
AdheipSingh fd5d1e0
add probes
AdheipSingh b68925c
Merge branch 'main' into main
nitisht acabaad
cli args and hostname
AdheipSingh 38f1d10
fix typo
AdheipSingh 19ff803
Merge branch 'main' into main
AdheipSingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,74 @@ | ||
| /* | ||
| * Parseable Server (C) 2022 - 2024 Parseable, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| use crate::option::CONFIG; | ||
AdheipSingh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| use actix_web::http::StatusCode; | ||
| use actix_web::HttpResponse; | ||
| use lazy_static::lazy_static; | ||
| use std::sync::Arc; | ||
| use tokio::signal::unix::{signal, SignalKind}; | ||
| use tokio::sync::{oneshot, Mutex}; | ||
| use tokio::time::{sleep, Duration}; | ||
|
|
||
| use crate::option::CONFIG; | ||
| // Create a global variable to store signal status | ||
| lazy_static! { | ||
| static ref SIGNAL_RECEIVED: Arc<Mutex<bool>> = Arc::new(Mutex::new(false)); | ||
| } | ||
|
|
||
| pub async fn liveness() -> HttpResponse { | ||
| HttpResponse::new(StatusCode::OK) | ||
| } | ||
|
|
||
| pub async fn handle_signals(shutdown_signal: Arc<Mutex<Option<oneshot::Sender<()>>>>) { | ||
| let signal_received = SIGNAL_RECEIVED.clone(); | ||
|
|
||
| let mut sigterm = | ||
| signal(SignalKind::terminate()).expect("Failed to set up SIGTERM signal handler"); | ||
| log::info!("Signal handler task started"); | ||
|
|
||
| // Block until SIGTERM is received | ||
| match sigterm.recv().await { | ||
| Some(_) => { | ||
| log::info!("Received SIGTERM signal at Readiness Probe Handler"); | ||
|
|
||
| // Set the shutdown flag to true | ||
| let mut shutdown_flag = signal_received.lock().await; | ||
| *shutdown_flag = true; | ||
|
|
||
| // Trigger graceful shutdown | ||
| if let Some(shutdown_sender) = shutdown_signal.lock().await.take() { | ||
| let _ = shutdown_sender.send(()); | ||
| } | ||
|
|
||
| // Delay to allow readiness probe to return SERVICE_UNAVAILABLE | ||
| let _ = sleep(Duration::from_secs(20)).await; | ||
|
|
||
| // Sync to local | ||
| crate::event::STREAM_WRITERS.unset_all(); | ||
|
|
||
| // Sync to S3 | ||
| if let Err(e) = CONFIG.storage().get_object_store().sync().await { | ||
| log::warn!("Failed to sync local data with object store. {:?}", e); | ||
| } | ||
|
|
||
| log::info!("Local and S3 Sync done, handler SIGTERM completed."); | ||
| } | ||
| None => { | ||
| println!("Signal handler received None, indicating an error or end of stream"); | ||
| } | ||
| } | ||
|
|
||
| eprintln!("Signal handler task completed"); | ||
AdheipSingh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| pub async fn readiness() -> HttpResponse { | ||
| if CONFIG.storage().get_object_store().check().await.is_ok() { | ||
| return HttpResponse::new(StatusCode::OK); | ||
| // Check if the application has received a shutdown signal | ||
| let shutdown_flag = SIGNAL_RECEIVED.lock().await; | ||
| if *shutdown_flag { | ||
| return HttpResponse::new(StatusCode::SERVICE_UNAVAILABLE); | ||
| } | ||
|
|
||
| HttpResponse::new(StatusCode::SERVICE_UNAVAILABLE) | ||
| // Check the object store connection | ||
| if CONFIG.storage().get_object_store().check().await.is_ok() { | ||
| HttpResponse::new(StatusCode::OK) | ||
| } else { | ||
| HttpResponse::new(StatusCode::SERVICE_UNAVAILABLE) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nitisht any specific reason for adding shell. Is it because image is distroless, i have been using Dockerfile.debug, so just want to confirm ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because distroless doesn't have any other shell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, but do we need a shell ? I mean wasn't shell for ${hostname}, container and app PID should be same.
I'lll check once if needed. If you see CMD will trigger the command itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can remove in that case