threaddata.cpp
16.4 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#include "threaddata.h"
#include <string>
#include <sstream>
#include <cassert>
#include "globalstats.h"
KeepAliveCheck::KeepAliveCheck(const std::shared_ptr<Client> client) :
client(client)
{
}
ThreadData::ThreadData(int threadnr, std::shared_ptr<Settings> settings) :
settingsLocalCopy(*settings.get()),
authentication(settingsLocalCopy),
threadnr(threadnr)
{
logger = Logger::getInstance();
epollfd = check<std::runtime_error>(epoll_create(999));
taskEventFd = eventfd(0, EFD_NONBLOCK);
if (taskEventFd < 0)
throw std::runtime_error("Can't create eventfd.");
struct epoll_event ev;
memset(&ev, 0, sizeof (struct epoll_event));
ev.data.fd = taskEventFd;
ev.events = EPOLLIN;
check<std::runtime_error>(epoll_ctl(this->epollfd, EPOLL_CTL_ADD, taskEventFd, &ev));
}
void ThreadData::start(thread_f f)
{
this->thread = std::thread(f, this);
pthread_t native = this->thread.native_handle();
std::ostringstream threadName;
threadName << "FlashMQ T " << threadnr;
threadName.flush();
std::string name = threadName.str();
const char *c_str = name.c_str();
pthread_setname_np(native, c_str);
/*
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(threadnr, &cpuset);
check<std::runtime_error>(pthread_setaffinity_np(native, sizeof(cpuset), &cpuset));
// It's not really necessary to get affinity again, but now I'm logging truth instead assumption.
check<std::runtime_error>(pthread_getaffinity_np(native, sizeof(cpuset), &cpuset));
int pinned_cpu = -1;
for (int j = 0; j < CPU_SETSIZE; j++)
if (CPU_ISSET(j, &cpuset))
pinned_cpu = j;
logger->logf(LOG_NOTICE, "Thread '%s' pinned to CPU %d", c_str, pinned_cpu);
*/
}
void ThreadData::quit()
{
running = false;
}
/**
* @brief ThreadData::queuePublishStatsOnDollarTopic makes this thread publish the $SYS topics.
* @param threads
*
* We want to do that in a thread because all authentication state is thread local.
*/
void ThreadData::queuePublishStatsOnDollarTopic(std::vector<std::shared_ptr<ThreadData>> &threads)
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::publishStatsOnDollarTopic, this, threads);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::queueSendingQueuedWills()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::sendQueuedWills, this);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::queueRemoveExpiredSessions()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::removeExpiredSessions, this);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::queueClientNextKeepAliveCheck(std::shared_ptr<Client> &client, bool keepRechecking)
{
const std::chrono::seconds k = client->getSecondsTillKillTime();
if (k == std::chrono::seconds(0))
return;
const std::chrono::seconds when = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch() + k);
KeepAliveCheck check(client);
check.recheck = keepRechecking;
queuedKeepAliveChecks[when].push_back(check);
}
void ThreadData::queueClientNextKeepAliveCheckLocked(std::shared_ptr<Client> &client, bool keepRechecking)
{
std::lock_guard<std::mutex> locker(this->queuedKeepAliveMutex);
queueClientNextKeepAliveCheck(client, keepRechecking);
}
void ThreadData::publishStatsOnDollarTopic(std::vector<std::shared_ptr<ThreadData>> &threads)
{
uint nrOfClients = 0;
uint64_t receivedMessageCountPerSecond = 0;
uint64_t receivedMessageCount = 0;
uint64_t sentMessageCountPerSecond = 0;
uint64_t sentMessageCount = 0;
uint64_t mqttConnectCountPerSecond = 0;
uint64_t mqttConnectCount = 0;
for (const std::shared_ptr<ThreadData> &thread : threads)
{
nrOfClients += thread->getNrOfClients();
receivedMessageCountPerSecond += thread->receivedMessageCounter.getPerSecond();
receivedMessageCount += thread->receivedMessageCounter.get();
sentMessageCountPerSecond += thread->sentMessageCounter.getPerSecond();
sentMessageCount += thread->sentMessageCounter.get();
mqttConnectCountPerSecond += thread->mqttConnectCounter.getPerSecond();
mqttConnectCount += thread->mqttConnectCounter.get();
}
GlobalStats *globalStats = GlobalStats::getInstance();
publishStat("$SYS/broker/network/socketconnects/total", globalStats->socketConnects.get());
publishStat("$SYS/broker/network/socketconnects/persecond", globalStats->socketConnects.getPerSecond());
publishStat("$SYS/broker/clients/mqttconnects/total", mqttConnectCount);
publishStat("$SYS/broker/clients/mqttconnects/persecond", mqttConnectCountPerSecond);
publishStat("$SYS/broker/clients/total", nrOfClients);
publishStat("$SYS/broker/load/messages/received/total", receivedMessageCount);
publishStat("$SYS/broker/load/messages/received/persecond", receivedMessageCountPerSecond);
publishStat("$SYS/broker/load/messages/sent/total", sentMessageCount);
publishStat("$SYS/broker/load/messages/sent/persecond", sentMessageCountPerSecond);
std::shared_ptr<SubscriptionStore> subscriptionStore = MainApp::getMainApp()->getSubscriptionStore();
publishStat("$SYS/broker/retained messages/count", subscriptionStore->getRetainedMessageCount());
publishStat("$SYS/broker/sessions/total", subscriptionStore->getSessionCount());
publishStat("$SYS/broker/subscriptions/count", subscriptionStore->getSubscriptionCount());
}
void ThreadData::publishStat(const std::string &topic, uint64_t n)
{
const std::string payload = std::to_string(n);
Publish p(topic, payload, 0);
PublishCopyFactory factory(&p);
std::shared_ptr<SubscriptionStore> subscriptionStore = MainApp::getMainApp()->getSubscriptionStore();
subscriptionStore->queuePacketAtSubscribers(factory, true);
subscriptionStore->setRetainedMessage(p, factory.getSubtopics());
}
void ThreadData::sendQueuedWills()
{
std::shared_ptr<SubscriptionStore> subscriptionStore = MainApp::getMainApp()->getSubscriptionStore();
subscriptionStore->sendQueuedWillMessages();
}
void ThreadData::removeExpiredSessions()
{
std::shared_ptr<SubscriptionStore> subscriptionStore = MainApp::getMainApp()->getSubscriptionStore();
subscriptionStore->removeExpiredSessionsClients();
}
void ThreadData::sendAllWills()
{
std::lock_guard<std::mutex> lck(clients_by_fd_mutex);
for(auto &pair : clients_by_fd)
{
std::shared_ptr<Client> &c = pair.second;
c->sendOrQueueWill();
}
allWillsQueued = true;
}
void ThreadData::sendAllDisconnects()
{
std::vector<std::shared_ptr<Client>> clientsFound;
{
std::lock_guard<std::mutex> lck(clients_by_fd_mutex);
clientsFound.reserve(clients_by_fd.size());
for(auto &pair : clients_by_fd)
{
clientsFound.push_back(pair.second);
}
}
for (std::shared_ptr<Client> &c : clientsFound)
{
c->serverInitiatedDisconnect(ReasonCodes::ServerShuttingDown);
}
allDisconnectsSent = true;
}
void ThreadData::removeQueuedClients()
{
// Using shared pointers to have a claiming reference in case we lose the clients between the two locks.
std::vector<std::shared_ptr<Client>> clients;
{
std::lock_guard<std::mutex> lck2(clientsToRemoveMutex);
for (const std::weak_ptr<Client> &c : clientsQueuedForRemoving)
{
std::shared_ptr<Client> client = c.lock();
if (client)
{
clients.push_back(client);
}
}
clientsQueuedForRemoving.clear();
}
{
std::lock_guard<std::mutex> lck(clients_by_fd_mutex);
for(const std::shared_ptr<Client> &client : clients)
{
int fd = client->getFd();
clients_by_fd.erase(fd);
}
}
}
void ThreadData::giveClient(std::shared_ptr<Client> client)
{
const int fd = client->getFd();
{
std::lock_guard<std::mutex> locker(clients_by_fd_mutex);
clients_by_fd[fd] = client;
}
queueClientNextKeepAliveCheckLocked(client, false);
struct epoll_event ev;
memset(&ev, 0, sizeof (struct epoll_event));
ev.data.fd = fd;
ev.events = EPOLLIN;
check<std::runtime_error>(epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev));
}
std::shared_ptr<Client> ThreadData::getClient(int fd)
{
std::lock_guard<std::mutex> lck(clients_by_fd_mutex);
auto pos = clients_by_fd.find(fd);
if (pos == clients_by_fd.end())
return std::shared_ptr<Client>();
return pos->second;
}
void ThreadData::removeClientQueued(const std::shared_ptr<Client> &client)
{
bool wakeUpNeeded = true;
{
std::lock_guard<std::mutex> locker(clientsToRemoveMutex);
wakeUpNeeded = clientsQueuedForRemoving.empty();
clientsQueuedForRemoving.push_front(client);
}
if (wakeUpNeeded)
{
auto f = std::bind(&ThreadData::removeQueuedClients, this);
std::lock_guard<std::mutex> lockertaskQueue(taskQueueMutex);
taskQueue.push_front(f);
wakeUpThread();
}
}
void ThreadData::removeClientQueued(int fd)
{
bool wakeUpNeeded = true;
std::shared_ptr<Client> clientFound;
{
std::lock_guard<std::mutex> lck(clients_by_fd_mutex);
auto client_it = this->clients_by_fd.find(fd);
if (client_it != this->clients_by_fd.end())
{
clientFound = client_it->second;
}
}
if (clientFound)
{
{
std::lock_guard<std::mutex> locker(clientsToRemoveMutex);
wakeUpNeeded = clientsQueuedForRemoving.empty();
clientsQueuedForRemoving.push_front(clientFound);
}
if (wakeUpNeeded)
{
auto f = std::bind(&ThreadData::removeQueuedClients, this);
std::lock_guard<std::mutex> lockertaskQueue(taskQueueMutex);
taskQueue.push_front(f);
wakeUpThread();
}
}
}
void ThreadData::removeClient(std::shared_ptr<Client> client)
{
// This function is only for same-thread calling.
assert(pthread_self() == thread.native_handle());
client->markAsDisconnecting();
std::lock_guard<std::mutex> lck(clients_by_fd_mutex);
clients_by_fd.erase(client->getFd());
}
void ThreadData::queueDoKeepAliveCheck()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::doKeepAliveCheck, this);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::queueQuit()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::quit, this);
taskQueue.push_front(f);
authentication.setQuitting();
wakeUpThread();
}
void ThreadData::waitForQuit()
{
thread.join();
}
void ThreadData::queuePasswdFileReload()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&Authentication::loadMosquittoPasswordFile, &authentication);
taskQueue.push_front(f);
auto f2 = std::bind(&Authentication::loadMosquittoAclFile, &authentication);
taskQueue.push_front(f2);
wakeUpThread();
}
int ThreadData::getNrOfClients() const
{
return clients_by_fd.size();
}
void ThreadData::queueAuthPluginPeriodicEvent()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::authPluginPeriodicEvent, this);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::authPluginPeriodicEvent()
{
authentication.periodicEvent();
}
void ThreadData::queueSendWills()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::sendAllWills, this);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::queueSendDisconnects()
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::sendAllDisconnects, this);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::doKeepAliveCheck()
{
logger->logf(LOG_DEBUG, "doKeepAliveCheck in thread %d", threadnr);
const std::chrono::seconds now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch());
try
{
// Put clients to delete in here, to avoid holding two locks.
std::vector<std::shared_ptr<Client>> clientsToRemove;
std::vector<std::shared_ptr<Client>> clientsToRecheck;
const int slotsTotal = this->queuedKeepAliveChecks.size();
int slotsProcessed = 0;
int clientsChecked = 0;
{
logger->logf(LOG_DEBUG, "Checking clients with pending keep-alive checks in thread %d", threadnr);
std::lock_guard<std::mutex> locker(this->queuedKeepAliveMutex);
auto pos = this->queuedKeepAliveChecks.begin();
while (pos != this->queuedKeepAliveChecks.end())
{
const std::chrono::seconds &doCheckAt = pos->first;
if (doCheckAt > now)
break;
slotsProcessed++;
std::vector<KeepAliveCheck> &checks = pos->second;
for (KeepAliveCheck &k : checks)
{
std::shared_ptr<Client> client = k.client.lock();
if (client)
{
clientsChecked++;
if (client->keepAliveExpired())
{
clientsToRemove.push_back(client);
}
else if (k.recheck)
{
clientsToRecheck.push_back(client);
}
}
}
pos = this->queuedKeepAliveChecks.erase(pos);
}
for (std::shared_ptr<Client> &c : clientsToRecheck)
{
c->resetBuffersIfEligible();
queueClientNextKeepAliveCheck(c, true);
}
}
logger->logf(LOG_DEBUG, "Checked %d clients in %d of %d keep-alive slots in thread %d", clientsChecked, slotsProcessed, slotsTotal, threadnr);
{
std::unique_lock<std::mutex> lock(clients_by_fd_mutex);
for (std::shared_ptr<Client> c : clientsToRemove)
{
c->setDisconnectReason("Keep-alive expired: " + c->getKeepAliveInfoString());
clients_by_fd.erase(c->getFd());
}
}
}
catch (std::exception &ex)
{
logger->logf(LOG_ERR, "Error handling keep-alives: %s.", ex.what());
}
}
void ThreadData::initAuthPlugin()
{
authentication.loadMosquittoPasswordFile();
authentication.loadMosquittoAclFile();
authentication.loadPlugin(settingsLocalCopy.authPluginPath);
authentication.init();
authentication.securityInit(false);
}
void ThreadData::cleanupAuthPlugin()
{
authentication.cleanup();
}
void ThreadData::reload(std::shared_ptr<Settings> settings)
{
logger->logf(LOG_DEBUG, "Doing reload in thread %d", threadnr);
try
{
// Because the auth plugin has a reference to it, it will also be updated.
settingsLocalCopy = *settings.get();
authentication.securityCleanup(true);
authentication.securityInit(true);
}
catch (std::exception &ex)
{
logger->logf(LOG_ERR, "Error reloading auth plugin: %s. Security checks will now fail, because we don't know the status of the plugin anymore.", ex.what());
}
}
void ThreadData::queueReload(std::shared_ptr<Settings> settings)
{
std::lock_guard<std::mutex> locker(taskQueueMutex);
auto f = std::bind(&ThreadData::reload, this, settings);
taskQueue.push_front(f);
wakeUpThread();
}
void ThreadData::wakeUpThread()
{
uint64_t one = 1;
check<std::runtime_error>(write(taskEventFd, &one, sizeof(uint64_t)));
}