From 7de94c8ca10e4e92b080c359819fc8ccec2a0fd6 Mon Sep 17 00:00:00 2001 From: Alex Rousskov Date: Sat, 5 Jun 2010 13:08:44 -0600 Subject: [PATCH] Only one process (called the "primary" process) should handle WCCP. In SMP mode, this is the Coordinator process. Identified several commonly use process kinds and added the corresponding Iam*Process() tests to avoid duplicating complex conditions throughout the code. Note that kinds are not mutually exclusive. For example, a primary process is also the master process and the worker process when running in a no-daemon mode. --- src/main.cc | 68 +++++++++++++++++++++++++++++----------------------- src/protos.h | 14 ++++++++++- src/tools.cc | 62 +++++++++++++++++++++++++++++++---------------- 3 files changed, 93 insertions(+), 51 deletions(-) diff --git a/src/main.cc b/src/main.cc index 7eced83f58..058b4d2c03 100644 --- a/src/main.cc +++ b/src/main.cc @@ -621,11 +621,19 @@ shut_down(int sig) static void serverConnectionsOpen(void) { - // Coordinator does not start proxying services - if (!opt_no_daemon && Config.main_processes > 1 && - KidIdentifier == Config.main_processes + 1) - return; + if (IamPrimaryProcess()) { +#if USE_WCCP + + wccpConnectionOpen(); +#endif +#if USE_WCCPv2 + + wccp2ConnectionOpen(); +#endif + } + // Coordinator does not start proxying services + if (!IamCoordinatorProcess()) { clientOpenListenSockets(); icpConnectionsOpen(); #if USE_HTCP @@ -636,15 +644,6 @@ serverConnectionsOpen(void) snmpConnectionOpen(); #endif -#if USE_WCCP - - wccpConnectionOpen(); -#endif - -#if USE_WCCPv2 - - wccp2ConnectionOpen(); -#endif clientdbInit(); icmpEngine.Open(); @@ -656,12 +655,25 @@ serverConnectionsOpen(void) carpInit(); peerUserHashInit(); peerSourceHashInit(); + } } static void serverConnectionsClose(void) { assert(shutting_down || reconfiguring); + + if (IamPrimaryProcess()) { +#if USE_WCCP + + wccpConnectionClose(); +#endif +#if USE_WCCPv2 + + wccp2ConnectionClose(); +#endif + } + if (!IamCoordinatorProcess()) { clientHttpConnectionsClose(); icpConnectionShutdown(); #if USE_HTCP @@ -674,16 +686,9 @@ serverConnectionsClose(void) snmpConnectionShutdown(); #endif -#if USE_WCCP - - wccpConnectionClose(); -#endif -#if USE_WCCPv2 - - wccp2ConnectionClose(); -#endif asnFreeMemory(); + } } static void @@ -779,6 +784,8 @@ mainReconfigureFinish(void *) redirectInit(); authenticateInit(&Config.authConfiguration); externalAclInit(); + + if (IamPrimaryProcess()) { #if USE_WCCP wccpInit(); @@ -787,6 +794,7 @@ mainReconfigureFinish(void *) wccp2Init(); #endif + } serverConnectionsOpen(); @@ -1041,6 +1049,7 @@ mainInitialize(void) // moved to PconnModule::PconnModule() } + if (IamPrimaryProcess()) { #if USE_WCCP wccpInit(); @@ -1050,6 +1059,7 @@ mainInitialize(void) wccp2Init(); #endif + } serverConnectionsOpen(); @@ -1421,12 +1431,10 @@ SquidMain(int argc, char **argv) mainLoop.setTimeService(&time_engine); - if (!opt_no_daemon && Config.main_processes > 1) { - if (KidIdentifier == Config.main_processes + 1) - AsyncJob::AsyncStart(Ipc::Coordinator::Instance()); - else if (KidIdentifier != 0) - AsyncJob::AsyncStart(new Ipc::Strand); - } + if (IamCoordinatorProcess()) + AsyncJob::AsyncStart(Ipc::Coordinator::Instance()); + else if (UsingSmp() && IamWorkerProcess()) + AsyncJob::AsyncStart(new Ipc::Strand); /* at this point we are finished the synchronous startup. */ starting_up = 0; @@ -1546,7 +1554,7 @@ static int checkRunningPid(void) { // master process must start alone, but its kids processes may co-exist - if (KidIdentifier != 0) + if (!IamMasterProcess()) return 0; pid_t pid; @@ -1588,7 +1596,7 @@ watch_child(char *argv[]) int nullfd; - if (KidIdentifier != 0) + if (!IamMasterProcess()) return; openlog(APP_SHORTNAME, LOG_PID | LOG_NDELAY | LOG_CONS, LOG_LOCAL4); @@ -1853,7 +1861,7 @@ SquidShutdown() #endif - if (IsPidFileMaintainer()) { + if (IamPrimaryProcess()) { if (Config.pidFilename && strcmp(Config.pidFilename, "none") != 0) { enter_suid(); safeunlink(Config.pidFilename, 0); diff --git a/src/protos.h b/src/protos.h index b60f915e53..37960375bc 100644 --- a/src/protos.h +++ b/src/protos.h @@ -576,7 +576,19 @@ SQUIDCEXTERN pid_t readPidFile(void); SQUIDCEXTERN void keepCapabilities(void); SQUIDCEXTERN void BroadcastSignalIfAny(int& sig); -SQUIDCEXTERN bool IsPidFileMaintainer(); +/// whether the current process is the parent of all other Squid processes +SQUIDCEXTERN bool IamMasterProcess(); +/** + whether the current process is dedicated to doing things that only + a single process should do, such as PID file maintenance and WCCP +*/ +SQUIDCEXTERN bool IamPrimaryProcess(); +/// whether the current process coordinates worker processes +SQUIDCEXTERN bool IamCoordinatorProcess(); +/// whether the current process handles HTTP transactions and such +SQUIDCEXTERN bool IamWorkerProcess(); +/// Whether there should be more than one worker process running +SQUIDCEXTERN bool UsingSmp(); // try using specific Iam*() checks above first SQUIDCEXTERN int DebugSignal; /* AYJ debugs function to show locations being reset with memset() */ diff --git a/src/tools.cc b/src/tools.cc index df4eeeac01..8b2e45f43f 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -404,10 +404,8 @@ void BroadcastSignalIfAny(int& sig) { if (sig > 0) { - if (!opt_no_daemon && Config.main_processes > 1) { - if (KidIdentifier == Config.main_processes + 1) - Ipc::Coordinator::Instance()->broadcastSignal(sig); - } + if (IamCoordinatorProcess()) + Ipc::Coordinator::Instance()->broadcastSignal(sig); sig = -1; } } @@ -807,23 +805,47 @@ no_suid(void) } bool -IsPidFileMaintainer() +IamMasterProcess() { - if (!opt_no_daemon && Config.main_processes > 0) { - if (Config.main_processes > 1) { - // multiple kids delegate PID file maintanence to the coordinator - if (KidIdentifier <= Config.main_processes) - return false; - } else { - // master process does not maintain PID file when - // Config.main_processes == 1 - if (KidIdentifier == 0) - return false; - } - } + return KidIdentifier == 0; +} + +bool +IamWorkerProcess() +{ + // when there is only one process, it has to be the worker + if (opt_no_daemon || Config.main_processes == 0) + return true; + + return 0 < KidIdentifier && KidIdentifier <= Config.main_processes; +} + +bool +UsingSmp() +{ + return !opt_no_daemon && Config.main_processes > 1; +} + +bool +IamCoordinatorProcess() +{ + return UsingSmp() && KidIdentifier == Config.main_processes + 1; +} + +bool +IamPrimaryProcess() +{ + // when there is only one process, it has to be primary + if (opt_no_daemon || Config.main_processes == 0) + return true; + + // when there is a master and worker process, the master delegates + // primary functions to its only kid + if (Config.main_processes == 1) + return IamWorkerProcess(); - // if there are no kids, then the master process maintains the PID file - return true; + // in SMP mode, multiple kids delegate primary functions to the coordinator + return IamCoordinatorProcess(); } void @@ -834,7 +856,7 @@ writePidFile(void) mode_t old_umask; char buf[32]; - if (!IsPidFileMaintainer()) + if (!IamPrimaryProcess()) return; if ((f = Config.pidFilename) == NULL) -- 2.39.5