Skip to content

Commit

Permalink
env var based routing selection
Browse files Browse the repository at this point in the history
  • Loading branch information
CamJN committed Jan 20, 2025
1 parent 6a2330a commit 0a11528
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/agent/Core/ApplicationPool/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ class Group: public boost::enable_shared_from_this<Group> {
/****** Process list management ******/

Process *findProcessWithStickySessionId(unsigned int id) const;
Process *findProcessWithStickySessionIdOrLowestBusyness(unsigned int id) const;
Process *findProcessWithLowestBusyness(const ProcessList &processes) const;
Process *findEnabledProcessWithLowestBusyness() const;
Process *findBestProcessPreferringStickySessionId(unsigned int id) const;
Process *findBestProcess(const ProcessList &processes) const;
Process *findBestEnabledProcess() const;
bool useNewRouting() const;

void addProcessToList(const ProcessPtr &process, ProcessList &destination);
void removeProcessFromList(const ProcessPtr &process, ProcessList &source);
Expand Down
67 changes: 67 additions & 0 deletions src/agent/Core/ApplicationPool/Group/ProcessListManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,73 @@ Group::findProcessWithStickySessionId(unsigned int id) const {
return NULL;
}

Process *
Group::findProcessWithStickySessionIdOrLowestBusyness(unsigned int id) const {
int leastBusyProcessIndex = -1;
int lowestBusyness = 0;
unsigned int i, size = enabledProcessBusynessLevels.size();
const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];
for (i = 0; i < size; i++) {
Process *process = enabledProcesses[i].get();
if (process->getStickySessionId() == id) {
return process;
} else if (leastBusyProcessIndex == -1 || enabledProcessBusynessLevels[i] < lowestBusyness) {
leastBusyProcessIndex = i;
lowestBusyness = enabledProcessBusynessLevels[i];
}
}

if (leastBusyProcessIndex == -1) {
return NULL;
} else {
return enabledProcesses[leastBusyProcessIndex].get();
}
}

Process *
Group::findProcessWithLowestBusyness(const ProcessList &processes) const {
if (processes.empty()) {
return NULL;
}

int lowestBusyness = -1;
Process *leastBusyProcess = NULL;
ProcessList::const_iterator it;
ProcessList::const_iterator end = processes.end();
for (it = processes.begin(); it != end; it++) {
Process *process = (*it).get();
int busyness = process->busyness();
if (lowestBusyness == -1 || lowestBusyness > busyness) {
lowestBusyness = busyness;
leastBusyProcess = process;
}
}
return leastBusyProcess;
}

/**
* Cache-optimized version of findProcessWithLowestBusyness() for the common case.
*/
Process *
Group::findEnabledProcessWithLowestBusyness() const {
if (enabledProcesses.empty()) {
return NULL;
}

int leastBusyProcessIndex = -1;
int lowestBusyness = 0;
unsigned int i, size = enabledProcessBusynessLevels.size();
const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];

for (i = 0; i < size; i++) {
if (leastBusyProcessIndex == -1 || enabledProcessBusynessLevels[i] < lowestBusyness) {
leastBusyProcessIndex = i;
lowestBusyness = enabledProcessBusynessLevels[i];
}
}
return enabledProcesses[leastBusyProcessIndex].get();
}

/**
* Return the process with the given sticky session ID if it exists.
* If not, then find the "best" enabled process to route a request to,
Expand Down
32 changes: 27 additions & 5 deletions src/agent/Core/ApplicationPool/Group/SessionManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#ifdef INTELLISENSE
#include <Core/ApplicationPool/Pool.h>
#endif
#include <Shared/Fundamentals/Utils.h>
#include <Core/ApplicationPool/Group.h>
#include <cassert>

Expand Down Expand Up @@ -63,18 +64,26 @@ using namespace boost;
*/
Group::RouteResult
Group::route(const Options &options) const {
Process *process = nullptr;
if (OXT_LIKELY(enabledCount > 0)) {
if (options.stickySessionId == 0) {
Process *process = findBestProcess(enabledProcesses);
if (OXT_LIKELY(useNewRouting())) {
process = findBestProcess(enabledProcesses);
} else {
process = findEnabledProcessWithLowestBusyness();
}
if (process != nullptr) {
assert(process->canBeRoutedTo());
return RouteResult(process);
} else {
return RouteResult(NULL, true);
}
} else {
Process *process = findBestProcessPreferringStickySessionId(
options.stickySessionId);
if (OXT_LIKELY(useNewRouting())) {
process = findBestProcessPreferringStickySessionId(options.stickySessionId);
}else{
process = findProcessWithStickySessionIdOrLowestBusyness(options.stickySessionId);
}
if (process != nullptr) {
if (process->canBeRoutedTo()) {
return RouteResult(process);
Expand All @@ -86,7 +95,11 @@ Group::route(const Options &options) const {
}
}
} else {
Process *process = findBestProcess(disablingProcesses);
if (OXT_LIKELY(useNewRouting())) {
process = findBestProcess(disablingProcesses);
} else {
process = findProcessWithLowestBusyness(disablingProcesses);
}
if (process != nullptr) {
assert(process->canBeRoutedTo());
return RouteResult(process);
Expand Down Expand Up @@ -313,7 +326,12 @@ Group::get(const Options &newOptions, const GetCallback &callback,
assert(m_spawning || restarting() || poolAtFullCapacity());

if (disablingCount > 0 && !restarting()) {
Process *process = findBestProcess(disablingProcesses);
Process *process = nullptr;
if (OXT_LIKELY(useNewRouting())) {
process = findBestProcess(disablingProcesses);
} else {
process = findProcessWithLowestBusyness(disablingProcesses);
}
if (process != nullptr && !process->isTotallyBusy()) {
return newSession(process, newOptions.currentTime);
}
Expand Down Expand Up @@ -341,6 +359,10 @@ Group::get(const Options &newOptions, const GetCallback &callback,
}
}

bool
Group::useNewRouting() const {
return !Agent::Fundamentals::getEnvBool("PASSENGER_OLD_ROUTING", false);
}

} // namespace ApplicationPool2
} // namespace Passenger
Expand Down

0 comments on commit 0a11528

Please sign in to comment.