@@ -101,8 +101,16 @@ Server::Server(engine::Storage *storage, Config *config)
101101 // init shard pub/sub channels
102102 pubsub_shard_channels_.resize (config->cluster_enabled ? HASH_SLOTS_SIZE : 1 );
103103
104+ std::vector<int > listen_fds;
104105 for (int i = 0 ; i < config->workers ; i++) {
105- auto worker = std::make_unique<Worker>(this , config);
106+ #ifdef __APPLE__
107+ auto worker = std::make_unique<Worker>(this , config, listen_fds);
108+ if (i == 0 ) {
109+ listen_fds = worker->GetTCPListenFDs ();
110+ }
111+ #else
112+ auto worker = std::make_unique<Worker>(this , config, std::vector<int >{});
113+ #endif
106114 // multiple workers can't listen to the same unix socket, so
107115 // listen unix socket only from a single worker - the first one
108116 if (!config->unixsocket .empty () && i == 0 ) {
@@ -255,6 +263,13 @@ void Server::Stop() {
255263 if (replication_thread_) replication_thread_->Stop ();
256264 slaveof_mu_.unlock ();
257265
266+ {
267+ std::lock_guard<std::mutex> guard (running_scripts_mu_);
268+ for (auto *rctx : running_scripts_) {
269+ rctx->is_killed = true ;
270+ }
271+ }
272+
258273 for (const auto &worker : worker_threads_) {
259274 worker->Stop (0 /* immediately terminate */ );
260275 }
@@ -1879,6 +1894,61 @@ StatusOr<std::unique_ptr<redis::Commander>> Server::LookupAndCreateCommand(const
18791894 return std::move (cmd);
18801895}
18811896
1897+ void Server::RegisterRunningScript (lua::ScriptRunCtx *rctx) {
1898+ std::lock_guard<std::mutex> guard (running_scripts_mu_);
1899+ running_scripts_.push_back (rctx);
1900+ running_script_count_.fetch_add (1 , std::memory_order_relaxed);
1901+ }
1902+
1903+ void Server::UnregisterRunningScript (lua::ScriptRunCtx *rctx) {
1904+ std::lock_guard<std::mutex> guard (running_scripts_mu_);
1905+ auto it = std::find (running_scripts_.begin (), running_scripts_.end (), rctx);
1906+ if (it != running_scripts_.end ()) {
1907+ running_scripts_.erase (it);
1908+ running_script_count_.fetch_sub (1 , std::memory_order_relaxed);
1909+ }
1910+ }
1911+
1912+ bool Server::IsScriptTimedOut () const {
1913+ int limit = config_->lua_time_limit ;
1914+ if (limit <= 0 ) return false ;
1915+ if (running_script_count_.load (std::memory_order_relaxed) == 0 ) return false ;
1916+
1917+ uint64_t now_ms = util::GetTimeStampMS ();
1918+ std::lock_guard<std::mutex> guard (running_scripts_mu_);
1919+ for (const auto *rctx : running_scripts_) {
1920+ if (now_ms - rctx->start_time_ms >= static_cast <uint64_t >(limit)) {
1921+ return true ;
1922+ }
1923+ }
1924+ return false ;
1925+ }
1926+
1927+ Status Server::ScriptKill () {
1928+ std::lock_guard<std::mutex> guard (running_scripts_mu_);
1929+ if (running_scripts_.empty ()) {
1930+ return {Status::NotOK, " NOTBUSY No scripts in execution right now." };
1931+ }
1932+
1933+ bool has_unkillable = false ;
1934+ for (auto *rctx : running_scripts_) {
1935+ if (rctx->is_write_dirty ) {
1936+ has_unkillable = true ;
1937+ } else {
1938+ rctx->is_killed = true ;
1939+ }
1940+ }
1941+
1942+ if (has_unkillable) {
1943+ return {Status::NotOK,
1944+ " UNKILLABLE Sorry the script already executed write commands against the dataset. "
1945+ " You can either wait the script termination or kill the server in a hard way using the SHUTDOWN NOSAVE "
1946+ " command." };
1947+ }
1948+
1949+ return Status::OK ();
1950+ }
1951+
18821952Status Server::ScriptExists (const std::string &sha) const {
18831953 std::string body;
18841954 return ScriptGet (sha, &body);
@@ -2068,8 +2138,14 @@ void Server::AdjustWorkerThreads() {
20682138}
20692139
20702140void Server::increaseWorkerThreads (size_t delta) {
2141+ std::vector<int > listen_fds;
2142+ #ifdef __APPLE__
2143+ if (!worker_threads_.empty ()) {
2144+ listen_fds = worker_threads_[0 ]->GetWorker ()->GetTCPListenFDs ();
2145+ }
2146+ #endif
20712147 for (size_t i = 0 ; i < delta; i++) {
2072- auto worker = std::make_unique<Worker>(this , config_);
2148+ auto worker = std::make_unique<Worker>(this , config_, listen_fds );
20732149 auto worker_thread = std::make_unique<WorkerThread>(std::move (worker));
20742150 worker_thread->Start ();
20752151 worker_threads_.emplace_back (std::move (worker_thread));
0 commit comments