Skip to content

Commit

Permalink
impl new algo
Browse files Browse the repository at this point in the history
  • Loading branch information
CamJN committed Sep 27, 2024
1 parent 824166d commit 60d254b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/agent/Core/ApplicationPool/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ 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;

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

Process *
Group::findProcessWithStickySessionIdOrLowestBusyness(unsigned int id) const {
int leastBusyProcessIndex = -1;
int lowestBusyness = 0;
unsigned int i, size = enabledProcessBusynessLevels.size(), highestGeneration = 0;
const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];

for (i = 0; i < size; i++) {
Process *process = enabledProcesses[i].get();
Group::findBestProcessPreferringStickySessionId(unsigned int id) const {
Process *bestProcess = nullptr;
ProcessList::const_iterator it;
ProcessList::const_iterator end = enabledProcesses.end();
for (it = enabledProcesses.begin(); it != end; it++) {
Process *process = (*it).get();
if (process->getStickySessionId() == id) {
return process;
} else if (leastBusyProcessIndex == -1 ||
enabledProcessBusynessLevels[i] < lowestBusyness ||
(enabledProcessBusynessLevels[i] == lowestBusyness && process->generation > highestGeneration)) {
highestGeneration = std::max(highestGeneration, process->generation);
leastBusyProcessIndex = i;
lowestBusyness = enabledProcessBusynessLevels[i];
} else if (bestProcess == nullptr ||
process->generation > bestProcess->generation ||
(process->generation == bestProcess->generation && process->spawnerCreationTime < bestProcess->spawnerCreationTime) ||
(process->generation == bestProcess->generation && process->spawnerCreationTime == bestProcess->spawnerCreationTime && process->busyness() < bestProcess->busyness())
) {
bestProcess = process;
}
}

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

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

int lowestBusyness = -1;
unsigned int highestGeneration = 0;
Process *leastBusyProcess = NULL;
Process *bestProcess = nullptr;
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 ||
(busyness == lowestBusyness && process->generation > highestGeneration)) {
highestGeneration = std::max(highestGeneration, process->generation);
lowestBusyness = busyness;
leastBusyProcess = process;

if (bestProcess == nullptr ||
process->generation > bestProcess->generation ||
(process->generation == bestProcess->generation && process->spawnerCreationTime < bestProcess->spawnerCreationTime) ||
(process->generation == bestProcess->generation && process->spawnerCreationTime == bestProcess->spawnerCreationTime && process->busyness() < bestProcess->busyness())
) {
bestProcess = process;
}
}
return leastBusyProcess;
return bestProcess;
}

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

int leastBusyProcessIndex = -1;
int lowestBusyness = 0;
unsigned int i, size = enabledProcessBusynessLevels.size(), highestGeneration = 0;
Process* bestProcess = nullptr;
unsigned long long bestProcessStartTime = 0;
unsigned int bestProcessGen = 0;
int bestProcessBusyness = 0;
unsigned int size = enabledProcessBusynessLevels.size();
const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];

for (i = 0; i < size; i++) {
if (leastBusyProcessIndex == -1 ||
enabledProcessBusynessLevels[i] < lowestBusyness ||
(enabledProcessBusynessLevels[i] == lowestBusyness && enabledProcesses[i]->generation > highestGeneration)) {
highestGeneration = std::max(highestGeneration, enabledProcesses[i]->generation);
leastBusyProcessIndex = i;
lowestBusyness = enabledProcessBusynessLevels[i];
for (unsigned int i = 0; i < size; i++) {
Process *process = enabledProcesses.at(i).get();
unsigned int gen = process->generation;
unsigned long long startTime = process->spawnerCreationTime;
int busyness = enabledProcessBusynessLevels[i];
if (bestProcess == nullptr ||
gen > bestProcess->generation ||
(gen == bestProcessGen && startTime < bestProcessStartTime) ||
(gen == bestProcessGen && startTime == bestProcessStartTime && busyness < bestProcessBusyness)
) {
bestProcess = process;
bestProcessGen = gen;
bestProcessBusyness = busyness;
bestProcessStartTime = startTime;
}
}
return enabledProcesses[leastBusyProcessIndex].get();
return bestProcess;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/agent/Core/ApplicationPool/Group/SessionManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ Group::RouteResult
Group::route(const Options &options) const {
if (OXT_LIKELY(enabledCount > 0)) {
if (options.stickySessionId == 0) {
Process *process = findEnabledProcessWithLowestBusyness();
Process *process = findBestEnabledProcess();
if (process->canBeRoutedTo()) {
return RouteResult(process);
} else {
return RouteResult(NULL, true);
}
} else {
Process *process = findProcessWithStickySessionIdOrLowestBusyness(
Process *process = findBestProcessPreferringStickySessionId(
options.stickySessionId);
if (process != NULL) {
if (process->canBeRoutedTo()) {
Expand All @@ -78,7 +78,7 @@ Group::route(const Options &options) const {
}
}
} else {
Process *process = findProcessWithLowestBusyness(disablingProcesses);
Process *process = findBestProcess(disablingProcesses);
if (process->canBeRoutedTo()) {
return RouteResult(process);
} else {
Expand Down Expand Up @@ -304,7 +304,7 @@ Group::get(const Options &newOptions, const GetCallback &callback,
assert(m_spawning || restarting() || poolAtFullCapacity());

if (disablingCount > 0 && !restarting()) {
Process *process = findProcessWithLowestBusyness(disablingProcesses);
Process *process = findBestProcess(disablingProcesses);
assert(process != NULL);
if (!process->isTotallyBusy()) {
return newSession(process, newOptions.currentTime);
Expand Down
21 changes: 10 additions & 11 deletions src/agent/Core/ApplicationPool/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include <string>
#include <vector>
#include <algorithm>
#include <boost/intrusive_ptr.hpp>
#include <boost/move/core.hpp>
#include <boost/container/vector.hpp>
Expand Down Expand Up @@ -102,6 +101,12 @@ class Process {
public:
static const unsigned int MAX_SOCKETS_ACCEPTING_HTTP_REQUESTS = 3;

/**
* Time at which the Spawner that created this process was created.
* Microseconds resolution.
*/
unsigned long long spawnerCreationTime;

private:
/*************************************************************
* Read-only fields, set once during initialization and never
Expand Down Expand Up @@ -141,12 +146,6 @@ class Process {
*/
StaticString codeRevision;

/**
* Time at which the Spawner that created this process was created.
* Microseconds resolution.
*/
unsigned long long spawnerCreationTime;

/** Time at which we started spawning this process. Microseconds resolution. */
unsigned long long spawnStartTime;

Expand Down Expand Up @@ -451,9 +450,9 @@ class Process {
ProcessMetrics metrics;

Process(const BasicGroupInfo *groupInfo, const unsigned int gen, const Json::Value &args)
: info(this, groupInfo, args),
: spawnerCreationTime(getJsonUint64Field(args, "spawner_creation_time")),
info(this, groupInfo, args),
socketsAcceptingHttpRequestsCount(0),
spawnerCreationTime(getJsonUint64Field(args, "spawner_creation_time")),
spawnStartTime(getJsonUint64Field(args, "spawn_start_time")),
spawnEndTime(SystemTime::getUsec()),
type(args["type"] == "dummy" ? SpawningKit::Result::DUMMY : SpawningKit::Result::UNKNOWN),
Expand All @@ -477,9 +476,9 @@ class Process {

Process(const BasicGroupInfo *groupInfo, const unsigned int gen, const SpawningKit::Result &skResult,
const Json::Value &args)
: info(this, groupInfo, skResult),
: spawnerCreationTime(getJsonUint64Field(args, "spawner_creation_time")),
info(this, groupInfo, skResult),
socketsAcceptingHttpRequestsCount(0),
spawnerCreationTime(getJsonUint64Field(args, "spawner_creation_time")),
spawnStartTime(skResult.spawnStartTime),
spawnEndTime(skResult.spawnEndTime),
type(skResult.type),
Expand Down

0 comments on commit 60d254b

Please sign in to comment.