-
Notifications
You must be signed in to change notification settings - Fork 29
/
tapconnmap.hh
229 lines (192 loc) · 5.65 KB
/
tapconnmap.hh
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef TAPCONNMAP_HH
#define TAPCONNMAP_HH 1
#include <map>
#include <list>
#include <iterator>
#include "common.hh"
#include "queueditem.hh"
#include "locks.hh"
#include "syncobject.hh"
// Forward declaration
class TapConnection;
class Item;
class EventuallyPersistentEngine;
/**
* Base class for operations performed on tap connections.
*
* @see TapConnMap::performTapOp
*/
template <typename V>
class TapOperation {
public:
virtual ~TapOperation() {}
virtual void perform(TapConnection *tc, V arg) = 0;
};
/**
* Indicate the tap operation is complete.
*/
class CompleteBackfillTapOperation : public TapOperation<void*> {
public:
void perform(TapConnection *tc, void* arg);
};
/**
* Indicate that we are going to schedule a tap disk backfill for a given vbucket.
*/
class ScheduleDiskBackfillTapOperation : public TapOperation<void*> {
public:
void perform(TapConnection *tc, void* arg);
};
/**
* Indicate the tap backfill disk stream thing is complete for a given vbucket.
*/
class CompleteDiskBackfillTapOperation : public TapOperation<void*> {
public:
void perform(TapConnection *tc, void* arg);
};
/**
* Give the tap connection a new item.
*/
class ReceivedItemTapOperation : public TapOperation<Item*> {
public:
ReceivedItemTapOperation(bool ie=false) : implicitEnqueue(ie) {}
void perform(TapConnection *tc, Item* arg);
private:
bool implicitEnqueue;
};
/**
* Indicate a background fetch completed on a tap connection.
*/
class CompletedBGFetchTapOperation : public TapOperation<EventuallyPersistentEngine*> {
public:
void perform(TapConnection *tc, EventuallyPersistentEngine* arg);
};
/**
* Send a tap notify.
*/
class NotifyIOTapOperation : public TapOperation<EventuallyPersistentEngine*> {
public:
void perform(TapConnection *tc, EventuallyPersistentEngine* arg);
};
/**
* A collection of tap connections.
*/
class TapConnMap {
public:
/**
* Disconnect a tap connection by its cookie.
*/
void disconnect(const void *cookie, int tapKeepAlive);
/**
* Perform a TapOperation for a named tap connection while holding
* appropriate locks.
*
* @param name the name of the tap connection to run the op
* @param tapop the operation to perform
* @param arg argument for the tap operation
*
* @return true if the tap connection was valid and the operation
* was performed
*/
template <typename V>
bool performTapOp(const std::string &name, TapOperation<V> &tapop, V arg) {
bool shouldNotify(true);
bool clear(true);
bool ret(true);
LockHolder lh(notifySync);
TapConnection *tc = findByName_UNLOCKED(name);
if (tc) {
tapop.perform(tc, arg);
shouldNotify = isPaused(tc);
clear = shouldDisconnect(tc);
} else {
ret = false;
}
if (clear) {
clearValidity(name);
}
if (shouldNotify) {
notifySync.notify();
}
return ret;
}
void purgeSingleExpiredTapConnection(TapConnection *tc);
/**
* Clear the tap validity for the given named connection.
*/
void clearValidity(const std::string &name);
/**
* Set named tap validity for the given token.
*/
void setValidity(const std::string &name, const void* token);
/**
* Return true if the given name is valid for the given token.
*/
bool checkValidity(const std::string &name, const void* token);
/**
* Set some backfilled events for a named conn.
*/
bool setEvents(const std::string &name,
std::list<QueuedItem> *q);
/**
* Get the size of the named queue.
*
* @return the size, or -1 if we can't find the queue
*/
ssize_t queueDepth(const std::string &name);
/**
* Add an event to all tap connections telling them to flush their
* items.
*/
void addFlushEvent();
/**
* Notify anyone who's waiting for tap stuff.
*/
void notify() {
LockHolder lh(notifySync);
notifySync.notify();
}
void wait(double howlong) {
// Prevent the notify thread from busy-looping while
// holding locks when there's work to do.
LockHolder lh(notifySync);
notifySync.wait(howlong);
}
/**
* Find or build a tap connection for the given cookie and with
* the given name.
*/
TapConnection *newConn(EventuallyPersistentEngine* e,
const void* cookie,
const std::string &name,
uint32_t flags,
uint64_t backfillAge,
int tapKeepAlive);
/**
* Call a function on each tap connection.
*/
template <typename Fun>
void each(Fun f) {
LockHolder lh(notifySync);
each_UNLOCKED(f);
}
/**
* Call a function on each tap connection *without* a lock.
*/
template <typename Fun>
void each_UNLOCKED(Fun f) {
std::for_each(all.begin(), all.end(), f);
}
void notifyIOThreadMain(EventuallyPersistentEngine *engine);
private:
TapConnection *findByName_UNLOCKED(const std::string &name);
int purgeExpiredConnections_UNLOCKED();
bool mapped(TapConnection *tc);
bool isPaused(TapConnection *tc);
bool shouldDisconnect(TapConnection *tc);
SyncObject notifySync;
std::map<const void*, TapConnection*> map;
std::map<const std::string, const void*> validity;
std::list<TapConnection*> all;
};
#endif /* TAPCONNMAP_HH */