-
Notifications
You must be signed in to change notification settings - Fork 1
/
ILoaderBalancer.h
207 lines (196 loc) · 7.13 KB
/
ILoaderBalancer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**The MIT License (MIT)
Copyright (c) 2018 by AleksanderSergeevich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef LOADERBALANCER_H
#define LOADERBALANCER_H
#pragma once
#include <QtCore>
#define DEFAULT_MEASUREMENT_QUEUE_SIZE 10
/*Interface of performance predictor*/
template<int state_size = 5, class T = double>
class IPredictor {
public:
virtual ~IPredictor() {}
virtual void set_state(const QVector<T>& state_) = 0;
virtual QVector<T> get_state() const = 0;
virtual bool update_state(const QList<T> measurements) = 0;
virtual T calc_score() = 0;
};
/*Base implementation of performance predictor*/
template<class T = double, int state_size = 5>
class BasePredictor : public IPredictor<state_size, T> {
struct Measurement {
virtual ~Measurement() {
clear();
}
virtual void clear() {
if(!measurement.isEmpty()) measurement.clear();
}
virtual void set_data(const QList<T>& measurement_) {
time = QDateTime::currentDateTime();
if(!measurement.isEmpty()) measurement.clear();
measurement.append(measurement_);
}
QDateTime time;
QVector<T> measurement;
};
public:
BasePredictor() {
state.resize(state_size);
}
virtual ~BasePredictor() {
reset();
}
virtual void reset() {
if(!state.isEmpty()) state.clear();
if(!coefficient_state_to_score.isEmpty()) coefficient_state_to_score.clear();
if(measurements.isEmpty()) return;
foreach(QSharedPointer<Measurement> measurement, measurements) {
measurement->clear();
}
measurements.clear();
}
virtual void set_state(const QVector<T>& state_) {
if(!state.isEmpty()) state.clear();
state.append(state_);
}
virtual QVector<T> get_state() const {
return state;
}
virtual void set_coefficient_state_to_score(const QVector<T>& coefficient_state_to_score_) {
if(!coefficient_state_to_score.isEmpty()) coefficient_state_to_score.clear();
coefficient_state_to_score.append(coefficient_state_to_score_);
}
virtual T calc_score() {
T score = 0;
for(int i = 0; i < state.size(); i++) {
score += coefficient_state_to_score.at(i) * state.at(i);
}
return score;
}
virtual bool update_state(const QList<T>& measurement) {
if(measurements.size() >= DEFAULT_MEASUREMENT_QUEUE_SIZE) {
measurements.dequeue().reset();
}
QSharedPointer<Measurement> new_measurement_ptr(new Measurement());
new_measurement_ptr->set_data(measurement);
measurements.enqueue(new_measurement_ptr);
if(!calc_new_state()) return false;
return true;
}
virtual bool calc_new_state() = 0;
protected:
QVector<T> state;
QQueue<QSharedPointer<Measurement> > measurements;
private:
QVector<T> coefficient_state_to_score;
};
/*Node performance descriptor*/
template<class T = double>
struct Node_performance_desc {
virtual ~Node_performance_desc() {}
virtual void reset() {
if(predictor_ptr.isNull()) return;
predictor_ptr->reset();
predictor_ptr.reset();
}
virtual void set_url(QUrl& address_) {
address.swap(address_);
}
virtual QUrl get_url() const {
return address;
}
virtual T update_performance_score(const QList<T>& measurement, const T& defVal = T()) {
if(!predictor_ptr->update_state(measurement)) return defVal;
return predictor_ptr->calc_score();
}
virtual T get_performance_score() {
return predictor_ptr->calc_score();
}
private:
QUrl address;
QSharedPointer<BasePredictor<T>> predictor_ptr;
};
template<class T = double>
struct NodeScore {
NodeScore(const QString node_id_ = "", const T value = T()) {
node_id = node_id_;
score = value;
}
static bool moreThen(const NodeScore& n1, const NodeScore& n2) {
return n1.score > n2.score;
}
QString node_id;
T score;
};
/*Load balancer implementation*/
class BaseLoadBalancer : public QObject {
Q_OBJECT
public:
BaseLoadBalancer(QObject* parent = NULL) : QObject(parent) {}
virtual ~BaseLoadBalancer() {
reset();
}
virtual void reset() {
if(descriptors.isEmpty()) return;
foreach(QSharedPointer<Node_performance_desc<> > descriptor, descriptors) {
descriptor->reset();
descriptor.reset();
}
descriptors.clear();
}
virtual void add_new_node_desc(const QString& node_id, QSharedPointer<Node_performance_desc<> >& descriptor) {
descriptors.insert(node_id, descriptor);
}
virtual bool delete_node_desc(const QString& node_id) {
if(!descriptors.contains(node_id)) return false;
descriptors[node_id].reset();
return true;
}
virtual bool get_node_desc(const QString& node_id, QSharedPointer<Node_performance_desc<> >& descriptor) {
if(!descriptors.contains(node_id)) return false;
descriptor = descriptors[node_id];
return true;
}
template<class T>
bool update_node_desc(const QString& node_id, const QList<T>& measurement) {
if(!descriptors.contains(node_id)) return false;
descriptors.value(node_id)->update_performance_score(measurement);
return true;
}
virtual bool get_best_nodes_id(const int number, QList<NodeScore<> >& best_nodes) {
if(descriptors.isEmpty()) return false;
if(!best_nodes.isEmpty()) best_nodes.clear();
best_nodes.reserve(number);
QHashIterator<QString, QSharedPointer<Node_performance_desc<> > > desc_iter(descriptors);
while(desc_iter.hasNext()) {
desc_iter.next();
NodeScore<> node(desc_iter.key(), desc_iter.value()->get_performance_score());
score_list.append(node);
}
qSort(score_list.begin(), score_list.end(), NodeScore<>::moreThen);
for(int i = 0; i < number; ++i) {
best_nodes.append(score_list[i]);
}
return true;
}
private:
QHash<QString, QSharedPointer<Node_performance_desc<> > > descriptors;
QList<NodeScore<> > score_list;
};
#endif // LOADERBALANCER_H