blob: e1f20e0d8a45204b2d330664d68c00a9d999cb87 (
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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/gencnt.h>
#include <vespa/vespalib/util/arrayqueue.hpp>
#include <map>
#include <mutex>
#include "service_mapping.h"
#include "map_diff.h"
namespace slobrok {
/**
* @class ServiceMapHistory
* @brief API to generate incremental updates for a collection of name->spec mappings
**/
class ServiceMapHistory
{
public:
using Generation = vespalib::GenCnt;
/** implement this interface to receive the result of an async generation diff */
class DiffCompletionHandler
{
public:
/**
* Handle the result of asyncGenerationDiff()
*
* @param updateDiff changes from the generation requested
**/
virtual void handle(MapDiff updateDiff) = 0;
protected:
virtual ~DiffCompletionHandler() {}
};
private:
struct UpdateLog {
static constexpr uint32_t keep_items = 1000;
Generation startGeneration;
Generation currentGeneration;
vespalib::ArrayQueue<vespalib::string> updates;
UpdateLog();
~UpdateLog();
void add(const vespalib::string &name);
bool isInRange(const Generation &gen) const;
std::vector<vespalib::string> updatedSince(const Generation &gen) const;
};
using Map = std::map<vespalib::string, vespalib::string>;
using Waiter = std::pair<DiffCompletionHandler *, Generation>;
using WaitList = std::vector<Waiter>;
mutable std::mutex _lock;
Map _map;
WaitList _waitList;
UpdateLog _log;
void notify_updated();
const Generation &myGen() const { return _log.currentGeneration; }
public:
ServiceMapHistory();
~ServiceMapHistory();
/**
* Ask for notification when the history has changes newer than fromGen.
* Note that if there are any changes in the history already, the callback
* will happen immediately (inside asyncGenerationDiff).
**/
void asyncGenerationDiff(DiffCompletionHandler *handler, const Generation &fromGen);
/**
* Cancel pending notification.
* @return true if handler was canceled without calling handle() at all.
**/
bool cancel(DiffCompletionHandler *handler);
/** add or update name->spec mapping */
void update(const ServiceMapping &mapping);
/** remove mapping for name */
void remove(const vespalib::string &name);
/** For unit testing only: */
Generation currentGen() const { return myGen(); }
private:
MapDiff makeDiffFrom(const Generation &fromGen) const;
};
//-----------------------------------------------------------------------------
} // namespace slobrok
|