summaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/local_rpc_monitor_map.cpp
blob: 025103e4e3d38c8f2db9480d477fc00df742b36a (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "local_rpc_monitor_map.h"
#include "sbenv.h"
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/log/log.h>
LOG_SETUP(".slobrok.server.local_rpc_monitor_map");

namespace slobrok {

#pragma GCC diagnostic ignored "-Winline"

namespace {

struct ChainedCompletionHandler : CompletionHandler {
    std::unique_ptr<CompletionHandler> first;
    std::unique_ptr<CompletionHandler> second;

    ChainedCompletionHandler(std::unique_ptr<CompletionHandler> f,
                                     std::unique_ptr<CompletionHandler> s)
        : first(std::move(f)), second(std::move(s))
    {}

    void doneHandler(OkState result) override {
        first->doneHandler(result);
        second->doneHandler(result);
    }
    ~ChainedCompletionHandler() override {}
};

}

void LocalRpcMonitorMap::DelayedTasks::PerformTask() {
    std::vector<Event> todo;
    std::swap(todo, _queue);
    for (const auto & entry : todo) {
        switch (entry.type) {
        case EventType::ADD:
            _target.doAdd(entry.mapping);
            break;
        case EventType::REMOVE:
            _target.doRemove(entry.mapping);
            break;
        }
    }
}

LocalRpcMonitorMap::LocalRpcMonitorMap(FNET_Scheduler *scheduler,
                                       MappingMonitorFactory mappingMonitorFactory)
  : _delayedTasks(scheduler, *this),
    _map(),
    _dispatcher(),
    _history(),
    _mappingMonitor(mappingMonitorFactory(*this)),
    _subscription(MapSubscription::subscribe(_dispatcher, _history))
{
}

LocalRpcMonitorMap::~LocalRpcMonitorMap() = default;

LocalRpcMonitorMap::PerService &
LocalRpcMonitorMap::lookup(const ServiceMapping &mapping) {
    LOG(spam, "lookup %s->%s", mapping.name.c_str(), mapping.spec.c_str());
    auto iter = _map.find(mapping.name);
    if (iter == _map.end()) {
        LOG_ABORT("not in map");
    }
    PerService & psd = iter->second;
    if (psd.spec != mapping.spec) {
        LOG_ABORT("conflict in map: %s->%s");
    }
    LOG(spam, "found in map: %s->%s [%s,%s]",
        iter->first.c_str(), psd.spec.c_str(),
        psd.up ? "up" : "down",
        psd.localOnly ? "local" : "global");
    return psd;
}

void LocalRpcMonitorMap::addToMap(const ServiceMapping &mapping, PerService psd, bool hurry) {
    auto [ iter, was_inserted ] =
        _map.try_emplace(mapping.name, std::move(psd));
    LOG_ASSERT(was_inserted);
    _mappingMonitor->start(mapping, hurry);
}

LocalRpcMonitorMap::RemovedData
LocalRpcMonitorMap::removeFromMap(Map::iterator iter) {
    auto name = iter->first;
    PerService psd = std::move(iter->second);
    ServiceMapping mapping{iter->first, psd.spec};
    _mappingMonitor->stop(mapping);
    _map.erase(iter);
    return RemovedData {
        .mapping = mapping,
        .up = psd.up,
        .localOnly = psd.localOnly,
        .inflight = std::move(psd.inflight)
    };
}

ServiceMapHistory & LocalRpcMonitorMap::history() {
    return _history;
}

bool LocalRpcMonitorMap::wouldConflict(const ServiceMapping &mapping) const {
    auto iter = _map.find(mapping.name);
    if (iter == _map.end()) {
        return false; // no mapping, no conflict
    }
    return (iter->second.spec != mapping.spec);
}

void LocalRpcMonitorMap::addLocal(const ServiceMapping &mapping,
                                  std::unique_ptr<CompletionHandler> inflight)
{
    LOG(debug, "try local add: mapping %s->%s",
        mapping.name.c_str(), mapping.spec.c_str());
    auto old = _map.find(mapping.name);
    if (old != _map.end()) {
        PerService & exists = old->second;
        if (exists.spec == mapping.spec) {
            LOG(debug, "added mapping %s->%s was already present",
                mapping.name.c_str(), mapping.spec.c_str());
            if (exists.up) {
                inflight->doneHandler(OkState(0, "already registered"));
            } else if (exists.inflight) {
                auto newInflight = std::make_unique<ChainedCompletionHandler>(
                    std::move(exists.inflight),
                    std::move(inflight));
                exists.inflight = std::move(newInflight);
            } else {
                _mappingMonitor->stop(mapping);
                exists.inflight = std::move(inflight);
                _mappingMonitor->start(mapping, true);
            }
            return;
        }
        LOG(warning, "tried addLocal for mapping %s->%s, but already had conflicting mapping %s->%s",
            mapping.name.c_str(), mapping.spec.c_str(),
            mapping.name.c_str(), exists.spec.c_str());
        inflight->doneHandler(OkState(FRTE_RPC_METHOD_FAILED, "conflict"));
        return;
    }
    addToMap(mapping, localService(mapping, std::move(inflight)), true);
}

void LocalRpcMonitorMap::removeLocal(const ServiceMapping &mapping) {
    LOG(debug, "try local remove: mapping %s->%s",
        mapping.name.c_str(), mapping.spec.c_str());
    auto old = _map.find(mapping.name);
    if (old == _map.end()) {
        return; // already removed, OK
    }
    PerService & exists = old->second;
    if (exists.spec != mapping.spec) {
        LOG(warning, "tried removeLocal for mapping %s->%s, but already had conflicting mapping %s->%s",
            mapping.name.c_str(), mapping.spec.c_str(),
            mapping.name.c_str(), exists.spec.c_str());
        return; // unregister for old, conflicting mapping
    }
    if (exists.localOnly) {
        // we can just remove it
        auto removed = removeFromMap(old);
        if (removed.inflight) {
            auto target = std::move(removed.inflight);
            target->doneHandler(OkState(13, "removed during initialization"));
        }
        if (removed.up) {
            _dispatcher.remove(removed.mapping);            
        }
        return;
    }
    // also exists in consensus map, so we can't just remove it
    // instead, pretend it's down and delay next ping
    _mappingMonitor->stop(mapping);
    if (exists.up) {
        exists.up = false;
        _dispatcher.remove(mapping);            
    }
    _mappingMonitor->start(mapping, false);
    return;
}

void LocalRpcMonitorMap::add(const ServiceMapping &mapping) {
    _delayedTasks.handleLater(Event::add(mapping));
}

void LocalRpcMonitorMap::remove(const ServiceMapping &mapping) {
    _delayedTasks.handleLater(Event::remove(mapping));
}

void LocalRpcMonitorMap::doAdd(const ServiceMapping &mapping) {
    LOG(debug, "try add: mapping %s->%s",
        mapping.name.c_str(), mapping.spec.c_str());
    auto old = _map.find(mapping.name);
    if (old != _map.end()) {
        PerService & exists = old->second;
        if (exists.spec == mapping.spec) {
            LOG(debug, "added mapping %s->%s was already present",
                mapping.name.c_str(), mapping.spec.c_str());
            exists.localOnly = false;
            return;
        }
        auto removed = removeFromMap(old);
        LOG(warning, "added mapping %s->%s, but already had conflicting mapping %s->%s",
            mapping.name.c_str(), mapping.spec.c_str(),
            removed.mapping.name.c_str(), removed.mapping.spec.c_str());
        if (removed.inflight) {
            auto target = std::move(removed.inflight);
            target->doneHandler(OkState(13, "conflict during initialization"));
        }
        if (removed.up) {
            _dispatcher.remove(removed.mapping);
        }
    }
    addToMap(mapping, globalService(mapping), false);
}

void LocalRpcMonitorMap::doRemove(const ServiceMapping &mapping) {
    auto iter = _map.find(mapping.name);
    if (iter != _map.end()) {
        auto removed = removeFromMap(iter);
        LOG(debug, "remove: mapping %s->%s",
            removed.mapping.name.c_str(), removed.mapping.spec.c_str());
        if (mapping.spec != removed.mapping.spec) {
            LOG(warning, "inconsistent specs for name '%s': had '%s', but was asked to remove '%s'",
                mapping.name.c_str(),
                removed.mapping.spec.c_str(),
                mapping.spec.c_str());
        }
        if (removed.inflight) {
            auto target = std::move(removed.inflight);
            target->doneHandler(OkState(13, "removed during initialization"));
        }
        if (removed.up) {
            _dispatcher.remove(removed.mapping);
        }
    } else {
        LOG(debug, "tried to remove non-existing mapping %s->%s",
            mapping.name.c_str(), mapping.spec.c_str());
    }
}

void LocalRpcMonitorMap::down(const ServiceMapping& mapping) {
    PerService &psd = lookup(mapping);
    LOG(debug, "failed: %s->%s", mapping.name.c_str(), psd.spec.c_str());
    if (psd.inflight) {
        auto target = std::move(psd.inflight);
        target->doneHandler(OkState(13, "failed check using listNames callback"));
    }
    if (psd.localOnly) {
        auto iter = _map.find(mapping.name);
        auto removed = removeFromMap(iter);
        if (removed.up) {
            _dispatcher.remove(removed.mapping);
        }
    } else if (psd.up) {
        psd.up = false;
        _dispatcher.remove(mapping);
    }
}

void LocalRpcMonitorMap::up(const ServiceMapping& mapping) {
    PerService &psd = lookup(mapping);
    LOG(debug, "ok: %s->%s", mapping.name.c_str(), psd.spec.c_str());
    if (psd.inflight) {
        auto target = std::move(psd.inflight);
        target->doneHandler(OkState());
    }
    if (! psd.up) {
        psd.up = true;
        _dispatcher.add(mapping);
    }
}

} // namespace slobrok