summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/Database.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/IceGrid/Database.cpp')
-rw-r--r--cpp/src/IceGrid/Database.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/cpp/src/IceGrid/Database.cpp b/cpp/src/IceGrid/Database.cpp
index fa65046637c..3523a17614c 100644
--- a/cpp/src/IceGrid/Database.cpp
+++ b/cpp/src/IceGrid/Database.cpp
@@ -112,6 +112,14 @@ private:
const AdapterPtr _adapter;
};
+struct ObjectLoadCI : binary_function<pair<Ice::ObjectPrx, float>&, pair<Ice::ObjectPrx, float>&, bool>
+{
+ bool operator()(const pair<Ice::ObjectPrx, float>& lhs, const pair<Ice::ObjectPrx, float>& rhs)
+ {
+ return lhs.second < rhs.second;
+ }
+};
+
}
Database::Database(const Ice::ObjectAdapterPtr& adapter,
@@ -843,6 +851,32 @@ Database::getObjectByType(const string& type)
return objs[rand() % objs.size()];
}
+Ice::ObjectPrx
+Database::getObjectByTypeOnLeastLoadedNode(const string& type, LoadSample sample)
+{
+ Ice::ObjectProxySeq objs = getObjectsWithType(type);
+ random_shuffle(objs.begin(), objs.end());
+ vector<pair<Ice::ObjectPrx, float> > objectsWithLoad;
+ objectsWithLoad.reserve(objs.size());
+ for(Ice::ObjectProxySeq::const_iterator p = objs.begin(); p != objs.end(); ++p)
+ {
+ float load = 1.0f;
+ if(!(*p)->ice_getAdapterId().empty())
+ {
+ try
+ {
+ load = _adapterCache.get((*p)->ice_getAdapterId())->getLeastLoadedNodeLoad(sample);
+ }
+ catch(const AdapterNotExistException&)
+ {
+ }
+ }
+ objectsWithLoad.push_back(make_pair(*p, load));
+ }
+ return min_element(objectsWithLoad.begin(), objectsWithLoad.end(), ObjectLoadCI())->first;
+}
+
+
Ice::ObjectProxySeq
Database::getObjectsWithType(const string& type)
{