summaryrefslogtreecommitdiff
path: root/src/mongo/client/sdam/topology_listener.cpp
blob: 147fa7dcb264733d76df189846864a4ec1444de1 (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
/**
 *    Copyright (C) 2020-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.
 */
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kNetwork

#include "mongo/client/sdam/topology_listener.h"

#include "mongo/util/log.h"

namespace mongo::sdam {

void TopologyEventsPublisher::registerListener(TopologyListenerPtr listener) {
    stdx::lock_guard lock(_mutex);
    _listeners.push_back(listener);
}

void TopologyEventsPublisher::removeListener(TopologyListenerPtr listener) {
    stdx::lock_guard lock(_mutex);
    _listeners.erase(std::remove(_listeners.begin(), _listeners.end(), listener), _listeners.end());
}

void TopologyEventsPublisher::close() {
    stdx::lock_guard lock(_mutex);
    _listeners.clear();
    _isClosed = true;
}

void TopologyEventsPublisher::onTopologyDescriptionChangedEvent(
    UUID topologyId,
    TopologyDescriptionPtr previousDescription,
    TopologyDescriptionPtr newDescription) {
    {
        stdx::lock_guard lock(_eventQueueMutex);
        EventPtr event = std::make_unique<Event>();
        event->type = EventType::TOPOLOGY_DESCRIPTION_CHANGED;
        event->topologyId = std::move(topologyId);
        event->previousDescription = previousDescription;
        event->newDescription = newDescription;
        _eventQueue.push_back(std::move(event));
    }
    _scheduleNextDelivery();
}

void TopologyEventsPublisher::onServerHandshakeCompleteEvent(IsMasterRTT durationMs,
                                                             const sdam::ServerAddress& address,
                                                             const BSONObj reply) {
    {
        stdx::lock_guard<Mutex> lock(_eventQueueMutex);
        EventPtr event = std::make_unique<Event>();
        event->type = EventType::HANDSHAKE_COMPLETE;
        event->duration = duration_cast<IsMasterRTT>(durationMs);
        event->hostAndPort = address;
        event->reply = reply;
        _eventQueue.push_back(std::move(event));
    }
    _scheduleNextDelivery();
}

void TopologyEventsPublisher::onServerHeartbeatSucceededEvent(IsMasterRTT durationMs,
                                                              const ServerAddress& hostAndPort,
                                                              const BSONObj reply) {
    {
        stdx::lock_guard lock(_eventQueueMutex);
        EventPtr event = std::make_unique<Event>();
        event->type = EventType::HEARTBEAT_SUCCESS;
        event->duration = duration_cast<IsMasterRTT>(durationMs);
        event->hostAndPort = hostAndPort;
        event->reply = reply;
        _eventQueue.push_back(std::move(event));
    }
    _scheduleNextDelivery();
}

void TopologyEventsPublisher::onServerHeartbeatFailureEvent(IsMasterRTT durationMs,
                                                            Status errorStatus,
                                                            const ServerAddress& hostAndPort,
                                                            const BSONObj reply) {
    {
        stdx::lock_guard lock(_eventQueueMutex);
        EventPtr event = std::make_unique<Event>();
        event->type = EventType::HEARTBEAT_FAILURE;
        event->duration = duration_cast<IsMasterRTT>(durationMs);
        event->hostAndPort = hostAndPort;
        event->reply = reply;
        event->status = errorStatus;
        _eventQueue.push_back(std::move(event));
    }
    _scheduleNextDelivery();
}

void TopologyEventsPublisher::_scheduleNextDelivery() {
    // run nextDelivery async
    _executor->schedule(
        [self = shared_from_this()](const Status& status) { self->_nextDelivery(); });
}

void TopologyEventsPublisher::onServerPingFailedEvent(const ServerAddress& hostAndPort,
                                                      const Status& status) {}

void TopologyEventsPublisher::onServerPingSucceededEvent(IsMasterRTT durationMS,
                                                         const ServerAddress& hostAndPort) {}

// note that this could be done in batches if it is a bottleneck.
void TopologyEventsPublisher::_nextDelivery() {
    // get the next event to send
    EventPtr nextEvent;
    {
        stdx::lock_guard lock(_eventQueueMutex);
        if (!_eventQueue.size()) {
            return;
        }
        nextEvent = std::move(_eventQueue.front());
        _eventQueue.pop_front();
    }

    // release the lock before sending to avoid deadlock in the case there
    // are events generated by sending the current one.
    std::vector<TopologyListenerPtr> listeners;
    {
        stdx::lock_guard lock(_mutex);
        if (_isClosed) {
            return;
        }
        listeners = _listeners;
    }

    // send to the listeners outside of the lock.
    for (auto listener : listeners) {
        _sendEvent(listener, *nextEvent);
    }
}

void TopologyEventsPublisher::_sendEvent(TopologyListenerPtr listener, const Event& event) {
    switch (event.type) {
        case EventType::HEARTBEAT_SUCCESS:
            listener->onServerHeartbeatSucceededEvent(
                duration_cast<IsMasterRTT>(event.duration), event.hostAndPort, event.reply);
            break;
        case EventType::HEARTBEAT_FAILURE:
            listener->onServerHeartbeatFailureEvent(duration_cast<IsMasterRTT>(event.duration),
                                                    event.status,
                                                    event.hostAndPort,
                                                    event.reply);
            break;
        case EventType::TOPOLOGY_DESCRIPTION_CHANGED:
            // TODO SERVER-46497: fix uuid or just remove
            listener->onTopologyDescriptionChangedEvent(
                UUID::gen(), event.previousDescription, event.newDescription);
            break;
        case EventType::HANDSHAKE_COMPLETE:
            listener->onServerHandshakeCompleteEvent(
                sdam::IsMasterRTT(duration_cast<Milliseconds>(event.duration)),
                event.hostAndPort,
                event.reply);
            break;
        default:
            MONGO_UNREACHABLE;
    }
}
};  // namespace mongo::sdam