summaryrefslogtreecommitdiff
path: root/src/mongo/db/free_mon/free_mon_processor.h
blob: 98875b7801d7206b6d433061dd9682420b2ca9da (plain)
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
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program 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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */
#pragma once

#include <boost/optional.hpp>
#include <cstdint>
#include <deque>
#include <memory>
#include <ratio>
#include <string>
#include <vector>

#include "mongo/db/client.h"
#include "mongo/db/free_mon/free_mon_message.h"
#include "mongo/db/free_mon/free_mon_network.h"
#include "mongo/db/free_mon/free_mon_processor.h"
#include "mongo/db/free_mon/free_mon_protocol_gen.h"
#include "mongo/db/free_mon/free_mon_queue.h"
#include "mongo/db/free_mon/free_mon_storage_gen.h"
#include "mongo/db/ftdc/collector.h"
#include "mongo/db/service_context.h"
#include "mongo/util/clock_source.h"
#include "mongo/util/duration.h"
#include "mongo/util/future.h"
#include "mongo/util/synchronized_value.h"
#include "mongo/util/time_support.h"

namespace mongo {
using FreeMonCollectorInterface = FTDCCollectorInterface;
using FreeMonCollectorCollection = FTDCCollectorCollection;


/**
 * Reponsible for tracking when to send the next retry after errors are encountered.
 */
class RetryCounter {
    const int64_t kMax = 60 * 60 * 24;

public:
    RetryCounter() : _min(1), _max(kMax) {}
    virtual ~RetryCounter() = default;

    /**
     * Set Minimum rety interval
     */
    void setMin(Seconds s) {
        _min = s;
        reset();
    }

    /**
     * Reset the retry interval, typically occurs after a succesfull message is sent.
     */
    virtual void reset() = 0;

    /**
     * Increment the error count and compute the next interval.
     */
    virtual bool incrementError() = 0;

    /**
     * Get the next retry duration.
     */
    Seconds getNextDuration() const {
        dassert(_current != Seconds(0));
        return _current;
    }

    /**
     * Get the next retry deadline
     */
    Date_t getNextDeadline(Client* client) const {
        return client->getServiceContext()->getPreciseClockSource()->now() + _current;
    }

protected:
    // Current retry interval
    Seconds _current;

    // Minimum retry interval
    Seconds _min;

    // Maximum retry interval
    Seconds _max;
};

/**
 * Manage retries for registrations
 */
class RegistrationRetryCounter : public RetryCounter {
public:
    explicit RegistrationRetryCounter(PseudoRandom& random) : _random(random) {}

    void reset() final;

    bool incrementError() final;

    size_t getCount() const {
        return _retryCount;
    }

private:
    // Random number generator for jitter
    PseudoRandom& _random;

    // Retry count for stage 1 retry
    size_t _retryCount{0};

    // Total Seconds we have retried for
    Seconds _total;

    // Last retry interval without jitter
    Seconds _base;

    // Max Retry count
    const size_t kStage1RetryCountMax{10};

    const size_t kStage1JitterMin{2};
    const size_t kStage1JitterMax{10};

    const Hours kStage2DurationMax{48};

    const size_t kStage2JitterMin{60};
    const size_t kStage2JitterMax{120};
};

/**
 * Manage retries for metrics
 */
class MetricsRetryCounter : public RetryCounter {
public:
    explicit MetricsRetryCounter(PseudoRandom& random) : _random(random) {}

    void reset() final;

    bool incrementError() final;

    size_t getCount() const {
        return _retryCount;
    }

private:
    // Random number generator for jitter
    PseudoRandom& _random;

    // Retry count for stage 1 retry
    size_t _retryCount{0};

    // Total Seconds we have retried for
    Seconds _total;

    // Last retry interval without jitter
    Seconds _base;

    // Max Duration
    const Hours kDurationMax{7 * 24};
};

/**
 * Simple bounded buffer of metrics to upload.
 */
class MetricsBuffer {
public:
    using container_type = std::deque<BSONObj>;

    /**
     * Add a metric to the buffer. Oldest metric will be discarded if buffer is at capacity.
     */
    void push(BSONObj obj) {
        if (_queue.size() == kMaxElements) {
            _queue.pop_front();
        }

        _queue.push_back(obj);
    }

    /**
     * Flush the buffer down to kMinElements entries. The last entries are held for cloud.
     */
    void reset() {
        while (_queue.size() > kMinElements) {
            _queue.pop_front();
        }
    }

    container_type::iterator begin() {
        return _queue.begin();
    }
    container_type::iterator end() {
        return _queue.end();
    }

private:
    // Bounded queue of metrics
    container_type _queue;

    const size_t kMinElements = 1;
    const size_t kMaxElements = 10;
};

/**
 * Countdown latch for test support in FreeMonProcessor so that a crank can be turned manually.
 */
class FreeMonCountdownLatch {
public:
    explicit FreeMonCountdownLatch() : _count(0) {}

    /**
     * Reset countdown latch wait for N events.
     */
    void reset(uint32_t count) {
        stdx::lock_guard<stdx::mutex> lock(_mutex);
        dassert(_count == 0);
        dassert(count > 0);
        _count = count;
    }

    /**
     * Count down an event.
     */
    void countDown() {
        stdx::lock_guard<stdx::mutex> lock(_mutex);

        if (_count > 0) {
            --_count;
            if (_count == 0) {
                _condvar.notify_one();
            }
        }
    }

    /**
     * Wait until the N events specified in reset have occured.
     */
    void wait() {
        stdx::unique_lock<stdx::mutex> lock(_mutex);
        _condvar.wait(lock, [&] { return _count == 0; });
    }

private:
    // mutex to break count and cond var
    stdx::mutex _mutex;

    // cond var to signal and wait on
    stdx::condition_variable _condvar;

    // count of events to wait for
    size_t _count;
};

/**
 * In-memory registration status
 *
 * Ensures primaries and secondaries register separately
 */
enum class FreeMonRegistrationStatus {
    /**
     * Free monitoring is not enabled - default state.
     */
    kDisabled,

    /**
     * Registration in progress.
     */
    kPending,

    /**
     * Free Monitoring is enabled.
     */
    kEnabled,
};

/**
 * Process in an Agent in a Agent/Message Passing model.
 *
 * Messages are given to it by enqueue, and the Processor processes messages with run().
 */
class FreeMonProcessor : public std::enable_shared_from_this<FreeMonProcessor> {
public:
    FreeMonProcessor(FreeMonCollectorCollection& registration,
                     FreeMonCollectorCollection& metrics,
                     FreeMonNetworkInterface* network,
                     bool useCrankForTest,
                     Seconds metricsGatherInterval);

    /**
     * Enqueue a message to process
     */
    void enqueue(std::shared_ptr<FreeMonMessage> msg);

    /**
     * Stop processing messages.
     */
    void stop();

    /**
     * Turn the crank of the message queue by ignoring deadlines for N messages.
     */
    void turnCrankForTest(size_t countMessagesToIgnore);

    /**
     * Processes messages forever
     */
    void run();

    /**
     * Validate the registration response. Public for unit testing.
     */
    static Status validateRegistrationResponse(const FreeMonRegistrationResponse& resp);

    /**
     * Validate the metrics response. Public for unit testing.
     */
    static Status validateMetricsResponse(const FreeMonMetricsResponse& resp);

private:
    /**
     * Read the state from the database.
     */
    void readState(OperationContext* opCtx);

    /**
     * Create a short-lived opCtx and read the state from the database.
     */
    void readState(Client* client);

    /**
     * Write the state to disk if there are any changes.
     */
    void writeState(Client* client);

    /**
     * Process a registration from a command.
     */
    void doCommandRegister(Client* client, std::shared_ptr<FreeMonMessage> sharedMsg);

    /**
     * Process a registration from configuration.
     */
    void doServerRegister(Client* client,
                          const FreeMonMessageWithPayload<FreeMonMessageType::RegisterServer>* msg);

    /**
     * Process unregistration from a command.
     */
    void doCommandUnregister(
        Client* client,
        FreeMonWaitableMessageWithPayload<FreeMonMessageType::UnregisterCommand>* msg);

    /**
     * Process a successful HTTP request.
     */
    void doAsyncRegisterComplete(
        Client* client,
        const FreeMonMessageWithPayload<FreeMonMessageType::AsyncRegisterComplete>* msg);

    /**
     * Process an unsuccessful HTTP request.
     */
    void doAsyncRegisterFail(
        Client* client,
        const FreeMonMessageWithPayload<FreeMonMessageType::AsyncRegisterFail>* msg);

    /**
     * Notify any command registers that are waiting.
     */
    void notifyPendingRegisters(const Status s);

    /**
     * Upload collected metrics.
     */
    void doMetricsCollect(Client* client);

    /**
     * Upload gathered metrics.
     */
    void doMetricsSend(Client* client);

    /**
     * Process a successful HTTP request.
     */
    void doAsyncMetricsComplete(
        Client* client,
        const FreeMonMessageWithPayload<FreeMonMessageType::AsyncMetricsComplete>* msg);

    /**
     * Process an unsuccessful HTTP request.
     */
    void doAsyncMetricsFail(
        Client* client, const FreeMonMessageWithPayload<FreeMonMessageType::AsyncMetricsFail>* msg);

    /**
     * Process a change to become a replica set primary
     */
    void doOnTransitionToPrimary(Client* client);

    /**
     * Process a notification that storage has received insert or update.
     */
    void doNotifyOnUpsert(Client* client,
                          const FreeMonMessageWithPayload<FreeMonMessageType::NotifyOnUpsert>* msg);

    /**
     * Process a notification that storage has received delete or drop collection.
     */
    void doNotifyOnDelete(Client* client);


    /**
     * Process a notification that storage has rolled back.
     */
    void doNotifyOnRollback(Client* client);

    /**
     * Process a in-memory state transition of state.
     */
    void processInMemoryStateChange(const FreeMonStorageState& originalState,
                                    const FreeMonStorageState& newState);

protected:
    friend class FreeMonController;

    enum FreeMonGetStatusEnum {
        kServerStatus,
        kCommandStatus,
    };

    /**
     *  Populate results for getFreeMonitoringStatus or serverStatus commands.
     */
    void getStatus(OperationContext* opCtx, BSONObjBuilder* status, FreeMonGetStatusEnum mode);

private:
    // Collection of collectors to send on registration
    FreeMonCollectorCollection& _registration;

    // Collection of collectors to send on each metrics call
    FreeMonCollectorCollection& _metrics;

    // HTTP Network interface
    FreeMonNetworkInterface* _network;

    // Random number generator for retries
    PseudoRandom _random;

    // Registration Retry logic
    synchronized_value<RegistrationRetryCounter> _registrationRetry;

    // Metrics Retry logic
    synchronized_value<MetricsRetryCounter> _metricsRetry;

    // Interval for gathering metrics
    Seconds _metricsGatherInterval;

    // Buffer of metrics to upload
    MetricsBuffer _metricsBuffer;

    // When did we last send a metrics batch?
    synchronized_value<boost::optional<Date_t>> _lastMetricsSend;

    // List of tags from server configuration registration
    std::vector<std::string> _tags;

    // In-flight registration response
    std::unique_ptr<Future<void>> _futureRegistrationResponse;

    // List of command registers waiting to be told about registration
    std::vector<std::shared_ptr<FreeMonMessage>> _pendingRegisters;

    // Last read storage state
    synchronized_value<boost::optional<FreeMonStorageState>> _lastReadState;

    // When we change to primary, do we register?
    RegistrationType _registerOnTransitionToPrimary{RegistrationType::DoNotRegister};

    // Pending update to disk
    synchronized_value<FreeMonStorageState> _state;

    // In-memory registration status
    FreeMonRegistrationStatus _registrationStatus{FreeMonRegistrationStatus::kDisabled};

    // Countdown launch to support manual cranking
    FreeMonCountdownLatch _countdown;

    // Message queue
    FreeMonMessageQueue _queue;
};

}  // namespace mongo