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
208
209
210
211
212
|
#include "common.hh"
#include <unordered_map>
#include "fake_monitor.hh"
#include "looper.hh"
#include "observers.hh"
namespace {
class FakeMonitorImpl : public Monitor {
public:
explicit FakeMonitorImpl(std::shared_ptr<Looper> const& looper)
: looper_(looper), timer_(0), host_id_(0), job_id_(0), max_jobs_(0) {
}
~FakeMonitorImpl() override {
disconnect();
}
void connect(std::string const&, std::string const&,
uint16_t) override {
disconnect();
timer_ = looper_->schedule(
(rand() % 500) / 1000.0,
std::bind(&FakeMonitorImpl::fake_connect, this,
std::placeholders::_1,
std::placeholders::_2));
}
void disconnect() override {
if (timer_) {
looper_->cancel(timer_);
timer_ = 0;
}
machines_.clear();
auto observer = observers_.first();
while (observer) {
observer->state(this, SEARCHING);
observer = observers_.next();
}
}
size_t machines() const override {
return machines_.size();
}
uint32_t id(size_t index) const override {
auto it = machines_.begin();
while (index--) ++it;
if (it == machines_.end()) {
assert(false);
return static_cast<uint32_t>(-1);
}
return it->first;
}
Machine machine_at(size_t index) const override {
auto it = machines_.begin();
while (index--) ++it;
if (it == machines_.end()) {
assert(false);
return Machine();
}
return it->second.data;
}
Machine machine(uint32_t id) const override {
auto it = machines_.find(id);
if (it == machines_.end()) {
assert(false);
return Machine();
}
return it->second.data;
}
void add_observer(Observer* observer) override {
observers_.add(observer);
}
void remove_observer(Observer* observer) override {
observers_.remove(observer);
}
private:
struct Job {
uint32_t const host;
uint32_t const worker;
Job(uint32_t host, uint32_t worker)
: host(host), worker(worker) {
}
};
struct Entry {
Machine data;
unsigned active;
};
void fake_connect(Looper*, uint32_t) {
timer_ = 0;
auto observer = observers_.first();
while (observer) {
observer->state(this, CONNECTED);
observer = observers_.next();
}
add_machine("alice", 4);
add_machine("bob", 10);
add_machine("DeuX", 22);
add_machine("machine1", 4);
add_machine("machine2", 4);
add_machine("machine3", 4);
add_machine("machine4", 4);
add_machine("machine5", 4);
add_machine("machine6", 4);
add_machine("machine7", 4);
add_machine("machine8", 4);
schedule_jobs();
}
void add_machine(std::string const& name, unsigned max_jobs) {
auto id = ++host_id_;
auto& entry = machines_[id];
entry.data.name = name;
entry.data.max_jobs = max_jobs;
entry.active = 0;
max_jobs_ += max_jobs;
auto observer = observers_.first();
while (observer) {
observer->added_machine(this, id);
observer = observers_.next();
}
}
void schedule_jobs() {
assert(timer_ == 0);
timer_ = looper_->schedule(
(rand() % 2000) / 1000.0,
std::bind(&FakeMonitorImpl::fiddle_jobs, this,
std::placeholders::_1,
std::placeholders::_2));
}
void fiddle_jobs(Looper*, uint32_t) {
timer_ = 0;
auto remove = !jobs_.empty() ? rand() % jobs_.size() : 0;
while (remove--) {
auto it = jobs_.begin();
auto job = it->second;
jobs_.erase(it);
machines_[job.worker].active--;
auto observer = observers_.first();
while (observer) {
observer->removed_job(this, job.host, job.worker);
observer = observers_.next();
}
}
auto avail = (max_jobs_ * 7) / 8 - jobs_.size();
auto add = avail ? rand() % avail : 0;
while (add--) {
auto id = ++job_id_;
while (jobs_.count(id)) {
id = ++job_id_;
}
auto host = rand() % machines_.size();
auto target = rand() % machines_.size();
do {
auto tgtid = this->id(target);
auto& entry = machines_[tgtid];
if (entry.active < entry.data.max_jobs) {
entry.active++;
add_job(id, host != target ? this->id(host) : tgtid, tgtid);
break;
}
target = rand() % machines_.size();
} while (true);
}
schedule_jobs();
}
void add_job(uint32_t job_id, uint32_t host, uint32_t worker) {
jobs_.insert(std::make_pair(job_id, Job(host, worker)));
auto observer = observers_.first();
while (observer) {
observer->added_job(this, host, worker);
observer = observers_.next();
}
}
std::shared_ptr<Looper> looper_;
uint32_t timer_;
uint32_t host_id_;
uint32_t job_id_;
size_t max_jobs_;
std::unordered_map<uint32_t, Entry> machines_;
Observers<Observer> observers_;
std::unordered_map<uint32_t, Job> jobs_;
};
} // namespace
// static
Monitor* FakeMonitor::create(std::shared_ptr<Looper> const& looper) {
return new FakeMonitorImpl(looper);
}
|