Skip to content

Commit 0a11528

Browse files
committed
env var based routing selection
1 parent 6a2330a commit 0a11528

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

src/agent/Core/ApplicationPool/Group.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ class Group: public boost::enable_shared_from_this<Group> {
233233
/****** Process list management ******/
234234

235235
Process *findProcessWithStickySessionId(unsigned int id) const;
236+
Process *findProcessWithStickySessionIdOrLowestBusyness(unsigned int id) const;
237+
Process *findProcessWithLowestBusyness(const ProcessList &processes) const;
238+
Process *findEnabledProcessWithLowestBusyness() const;
236239
Process *findBestProcessPreferringStickySessionId(unsigned int id) const;
237240
Process *findBestProcess(const ProcessList &processes) const;
238241
Process *findBestEnabledProcess() const;
242+
bool useNewRouting() const;
239243

240244
void addProcessToList(const ProcessPtr &process, ProcessList &destination);
241245
void removeProcessFromList(const ProcessPtr &process, ProcessList &source);

src/agent/Core/ApplicationPool/Group/ProcessListManagement.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,73 @@ Group::findProcessWithStickySessionId(unsigned int id) const {
6363
return NULL;
6464
}
6565

66+
Process *
67+
Group::findProcessWithStickySessionIdOrLowestBusyness(unsigned int id) const {
68+
int leastBusyProcessIndex = -1;
69+
int lowestBusyness = 0;
70+
unsigned int i, size = enabledProcessBusynessLevels.size();
71+
const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];
72+
for (i = 0; i < size; i++) {
73+
Process *process = enabledProcesses[i].get();
74+
if (process->getStickySessionId() == id) {
75+
return process;
76+
} else if (leastBusyProcessIndex == -1 || enabledProcessBusynessLevels[i] < lowestBusyness) {
77+
leastBusyProcessIndex = i;
78+
lowestBusyness = enabledProcessBusynessLevels[i];
79+
}
80+
}
81+
82+
if (leastBusyProcessIndex == -1) {
83+
return NULL;
84+
} else {
85+
return enabledProcesses[leastBusyProcessIndex].get();
86+
}
87+
}
88+
89+
Process *
90+
Group::findProcessWithLowestBusyness(const ProcessList &processes) const {
91+
if (processes.empty()) {
92+
return NULL;
93+
}
94+
95+
int lowestBusyness = -1;
96+
Process *leastBusyProcess = NULL;
97+
ProcessList::const_iterator it;
98+
ProcessList::const_iterator end = processes.end();
99+
for (it = processes.begin(); it != end; it++) {
100+
Process *process = (*it).get();
101+
int busyness = process->busyness();
102+
if (lowestBusyness == -1 || lowestBusyness > busyness) {
103+
lowestBusyness = busyness;
104+
leastBusyProcess = process;
105+
}
106+
}
107+
return leastBusyProcess;
108+
}
109+
110+
/**
111+
* Cache-optimized version of findProcessWithLowestBusyness() for the common case.
112+
*/
113+
Process *
114+
Group::findEnabledProcessWithLowestBusyness() const {
115+
if (enabledProcesses.empty()) {
116+
return NULL;
117+
}
118+
119+
int leastBusyProcessIndex = -1;
120+
int lowestBusyness = 0;
121+
unsigned int i, size = enabledProcessBusynessLevels.size();
122+
const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];
123+
124+
for (i = 0; i < size; i++) {
125+
if (leastBusyProcessIndex == -1 || enabledProcessBusynessLevels[i] < lowestBusyness) {
126+
leastBusyProcessIndex = i;
127+
lowestBusyness = enabledProcessBusynessLevels[i];
128+
}
129+
}
130+
return enabledProcesses[leastBusyProcessIndex].get();
131+
}
132+
66133
/**
67134
* Return the process with the given sticky session ID if it exists.
68135
* If not, then find the "best" enabled process to route a request to,

src/agent/Core/ApplicationPool/Group/SessionManagement.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifdef INTELLISENSE
3030
#include <Core/ApplicationPool/Pool.h>
3131
#endif
32+
#include <Shared/Fundamentals/Utils.h>
3233
#include <Core/ApplicationPool/Group.h>
3334
#include <cassert>
3435

@@ -63,18 +64,26 @@ using namespace boost;
6364
*/
6465
Group::RouteResult
6566
Group::route(const Options &options) const {
67+
Process *process = nullptr;
6668
if (OXT_LIKELY(enabledCount > 0)) {
6769
if (options.stickySessionId == 0) {
68-
Process *process = findBestProcess(enabledProcesses);
70+
if (OXT_LIKELY(useNewRouting())) {
71+
process = findBestProcess(enabledProcesses);
72+
} else {
73+
process = findEnabledProcessWithLowestBusyness();
74+
}
6975
if (process != nullptr) {
7076
assert(process->canBeRoutedTo());
7177
return RouteResult(process);
7278
} else {
7379
return RouteResult(NULL, true);
7480
}
7581
} else {
76-
Process *process = findBestProcessPreferringStickySessionId(
77-
options.stickySessionId);
82+
if (OXT_LIKELY(useNewRouting())) {
83+
process = findBestProcessPreferringStickySessionId(options.stickySessionId);
84+
}else{
85+
process = findProcessWithStickySessionIdOrLowestBusyness(options.stickySessionId);
86+
}
7887
if (process != nullptr) {
7988
if (process->canBeRoutedTo()) {
8089
return RouteResult(process);
@@ -86,7 +95,11 @@ Group::route(const Options &options) const {
8695
}
8796
}
8897
} else {
89-
Process *process = findBestProcess(disablingProcesses);
98+
if (OXT_LIKELY(useNewRouting())) {
99+
process = findBestProcess(disablingProcesses);
100+
} else {
101+
process = findProcessWithLowestBusyness(disablingProcesses);
102+
}
90103
if (process != nullptr) {
91104
assert(process->canBeRoutedTo());
92105
return RouteResult(process);
@@ -313,7 +326,12 @@ Group::get(const Options &newOptions, const GetCallback &callback,
313326
assert(m_spawning || restarting() || poolAtFullCapacity());
314327

315328
if (disablingCount > 0 && !restarting()) {
316-
Process *process = findBestProcess(disablingProcesses);
329+
Process *process = nullptr;
330+
if (OXT_LIKELY(useNewRouting())) {
331+
process = findBestProcess(disablingProcesses);
332+
} else {
333+
process = findProcessWithLowestBusyness(disablingProcesses);
334+
}
317335
if (process != nullptr && !process->isTotallyBusy()) {
318336
return newSession(process, newOptions.currentTime);
319337
}
@@ -341,6 +359,10 @@ Group::get(const Options &newOptions, const GetCallback &callback,
341359
}
342360
}
343361

362+
bool
363+
Group::useNewRouting() const {
364+
return !Agent::Fundamentals::getEnvBool("PASSENGER_OLD_ROUTING", false);
365+
}
344366

345367
} // namespace ApplicationPool2
346368
} // namespace Passenger

0 commit comments

Comments
 (0)