-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable maximum connections to be configured manually (#182)
* Enable maximum connections to be configured * Add arbiter for handling server startup * Add handling of maxcon for command-line args * Add changelog entry
- Loading branch information
Showing
7 changed files
with
207 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Created on Sat Jun 26 2021 | ||
* | ||
* This file is a part of Skytable | ||
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source | ||
* NoSQL database written by Sayan Nandan ("the Author") with the | ||
* vision to provide flexibility in data modelling without compromising | ||
* on performance, queryability or scalability. | ||
* | ||
* Copyright (c) 2021, Sayan Nandan <ohsayan@outlook.com> | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
use crate::config::BGSave; | ||
use crate::config::SnapshotConfig; | ||
use crate::coredb::CoreDB; | ||
use crate::dbnet::{self, Terminator}; | ||
use crate::diskstore::snapshot::DIR_REMOTE_SNAPSHOT; | ||
use crate::services; | ||
use crate::PortConfig; | ||
use std::fs; | ||
use tokio::sync::broadcast; | ||
|
||
#[cfg(unix)] | ||
use core::{future::Future, pin::Pin, task::Context, task::Poll}; | ||
#[cfg(unix)] | ||
use tokio::signal::unix::{signal as fnsignal, Signal, SignalKind}; | ||
#[cfg(unix)] | ||
/// Object to bind to unix-specific signals | ||
pub struct UnixTerminationSignal { | ||
sigterm: Signal, | ||
} | ||
|
||
#[cfg(unix)] | ||
impl UnixTerminationSignal { | ||
pub fn init() -> Result<Self, String> { | ||
let sigterm = fnsignal(SignalKind::terminate()) | ||
.map_err(|e| format!("Failed to bind to signal with: {}", e))?; | ||
Ok(Self { sigterm }) | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
impl Future for UnixTerminationSignal { | ||
type Output = Option<()>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.sigterm.poll_recv(ctx) | ||
} | ||
} | ||
|
||
/// Start the server waiting for incoming connections or a termsig | ||
pub async fn run( | ||
ports: PortConfig, | ||
bgsave_cfg: BGSave, | ||
snapshot_cfg: SnapshotConfig, | ||
restore_filepath: Option<String>, | ||
maxcon: usize, | ||
) -> Result<CoreDB, String> { | ||
// Intialize the broadcast channel | ||
let (signal, _) = broadcast::channel(1); | ||
|
||
// create the data directories and intialize the coredb | ||
fs::create_dir_all(&*DIR_REMOTE_SNAPSHOT) | ||
.map_err(|e| format!("Failed to create data directories: '{}'", e))?; | ||
let db = CoreDB::new(&snapshot_cfg, restore_filepath) | ||
.map_err(|e| format!("Error while initializing database: {}", e))?; | ||
|
||
// initialize the background services | ||
let bgsave_handle = tokio::spawn(services::bgsave::bgsave_scheduler( | ||
db.clone(), | ||
bgsave_cfg, | ||
Terminator::new(signal.subscribe()), | ||
)); | ||
let snapshot_handle = tokio::spawn(services::snapshot::snapshot_service( | ||
db.clone(), | ||
snapshot_cfg, | ||
Terminator::new(signal.subscribe()), | ||
)); | ||
|
||
// bind the ctrlc handler | ||
let sig = tokio::signal::ctrl_c(); | ||
|
||
// start the server (single or multiple listeners) | ||
let mut server = dbnet::connect(ports, maxcon, db.clone(), signal.clone()).await?; | ||
|
||
#[cfg(not(unix))] | ||
{ | ||
// Non-unix, usually Windows specific signal handling. | ||
// FIXME(@ohsayan): For now, let's just | ||
// bother with ctrl+c, we'll move ahead as users require them | ||
tokio::select! { | ||
_ = server.run_server() => {} | ||
_ = sig => {} | ||
} | ||
} | ||
#[cfg(unix)] | ||
{ | ||
let sigterm = UnixTerminationSignal::init()?; | ||
// apart from CTRLC, the only other thing we care about is SIGTERM | ||
// FIXME(@ohsayan): Maybe we should respond to SIGHUP too? | ||
tokio::select! { | ||
_ = server.run_server() => {}, | ||
_ = sig => {}, | ||
_ = sigterm => {} | ||
} | ||
} | ||
|
||
log::info!("Signalling all workers to shut down"); | ||
// drop the signal and let others exit | ||
drop(signal); | ||
server.finish_with_termsig().await; | ||
|
||
// wait for the background services to terminate | ||
let _ = snapshot_handle.await; | ||
let _ = bgsave_handle.await; | ||
Ok(db) | ||
} |
This file contains 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 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
Oops, something went wrong.