-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshipping.h
311 lines (267 loc) · 7.5 KB
/
shipping.h
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
#ifndef SHIPPING_H_
#define SHIPPING_H_
#include <atomic>
#include <mutex>
#include <list>
#include <climits>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include "gopp/channels.h"
#include "log.h"
#include "node_config.h"
namespace felis {
class Slice;
class SliceQueue;
class ShippingHandle : public util::ListNode {
/**
* Which scanning session was this handle born?
*
* If the scanning session X is still scanning, then only add to the shipment
* when born == X. Because any born < X will be taken care of by the scan.
*
* If the scanning session X has finished scanning, then always add to the
* shipment.
*/
uint64_t born;
std::atomic_ullong generation;
std::atomic_ullong sent_generation;
public:
ShippingHandle();
/**
* This is not MT-Safe. You are not suppose to write to the same object
* concurrently anyway.
*
* @return if the handle should be put into a queue for sending
*/
bool MarkDirty();
/**
* This is not MT-safe either. You are not suppose to sent the same object
* using multiple threads.
*/
void PrepareSend();
/**
*
* @return if the handle should be put into queue by the scanner
*/
bool CheckSession();
// for analysis in ScanShippingHandle()
unsigned long long GetGeneration() { return generation.load(std::memory_order_acquire); }
};
template <typename T>
struct ObjectShippingHandle : public ShippingHandle {
T *object;
ObjectShippingHandle(T *object) : ShippingHandle(), object(object) {}
};
// SliceScanner is per slice, responsible for getting Entities from SliceQueues
// then ObjectSliceScanner can add them into its Shipment
class SliceScanner {
protected:
Slice * slice;
SliceQueue * current_q;
util::ListNode * current_node;
SliceScanner(Slice * slice);
ShippingHandle *GetNextHandle();
void ResetCursor();
public:
static void ScannerBegin();
static void ScannerEnd();
static void MigrationApproachingEnd();
static void MigrationEnd();
static void StatAddObject();
static bool IsConverging();
};
// Class for sending IOVec.
class BaseShipment {
public:
static constexpr int kSendBatch = 32 * IOV_MAX;
protected:
sockaddr_in addr;
int fd;
bool connected;
bool finished;
std::mutex lock;
void SendIOVec(struct iovec *vec, int nr_vec);
void ReceiveACK();
bool has_finished() const { return finished; }
std::mutex &mutex() { return lock; }
public:
BaseShipment(int fd) : fd(fd), connected(true), finished(false) {}
BaseShipment(std::string host, unsigned int port, bool defer_connect = false);
private:
void Connect();
};
// g_objects_added includes shipped and skipped
static std::atomic_ulong g_objects_shipped = 0;
static std::atomic_ulong g_objects_skipped = 0;
static std::atomic_ullong g_bytes_sent = 0;
/**
* T is a concept:
*
* // returns how many iovec encoded, 0 means not enough
* int EncodeIOVec(struct iovec *vec, int max_nr_vec);
* void DecodeIOVec(struct iovec *vec);
*
* uint64_t encoded_len; // Stored the total data length of EncodeIOVec();
*
* Shipment is a queue of Entity, waiting to be sent.
* Calling it "Shipment Queue" would be easier to understand.
*/
template <typename T>
class Shipment : public BaseShipment {
protected:
std::list<T *> queue;
public:
using BaseShipment::BaseShipment;
void AddObject(T *object) {
std::lock_guard _(lock);
queue.push_front(object);
}
bool RunSend() {
T *obj[kSendBatch];
int nr_obj = 0;
struct iovec vec[IOV_MAX];
{
std::lock_guard _(lock);
while (nr_obj < kSendBatch && !queue.empty()) {
obj[nr_obj++] = queue.back();
queue.pop_back();
}
if (nr_obj == 0) {
finished = true;
close(fd);
SliceScanner::MigrationEnd();
return true;
}
}
int i = 0;
int cur_iov = 0;
int enabled = 1;
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &enabled, 4);
while (i < nr_obj) {
if (obj[i]->ShouldSkip()) {
g_objects_skipped.fetch_add(1);
i++;
continue;
}
int n = 0;
if (cur_iov == IOV_MAX
|| (n = obj[i]->EncodeIOVec(&vec[cur_iov + 1], IOV_MAX - cur_iov - 1)) == 0) {
SendIOVec(vec, cur_iov);
cur_iov = 0;
continue;
}
vec[cur_iov].iov_len = 8;
vec[cur_iov].iov_base = &obj[i]->encoded_len;
cur_iov += n + 1;
g_bytes_sent.fetch_add(obj[i]->encoded_len);
g_objects_shipped.fetch_add(1);
i++;
}
SendIOVec(vec, cur_iov);
ReceiveACK();
return false;
}
};
//
// Making the ShipmentReceiver a go-routine has a lot of benefits.
//
// 1. We have tons of very small read() in the process. The go-routine channel
// can help us buffer and issue less system calls.
//
// 2. We need to create ThreadInfo to use MassTree and the index will create
// that for you if you are a go-routine.
//
template <typename T>
class ShipmentReceiver : public go::Routine {
protected:
go::TcpSocket *sock;
public:
ShipmentReceiver(go::TcpSocket *sock) : sock(sock) {}
bool Receive(T *shipment) {
auto *in = sock->input_channel();
auto *out = sock->output_channel();
again:
uint64_t psz;
if (!in->Read(&psz, 8)) {
return false;
}
if (psz == 0) {
uint8_t done = 0;
out->Write(&done, 1);
out->Flush();
goto again;
}
auto buffer = (uint8_t *) malloc(psz);
if (!in->Read(buffer, psz)) {
logger->critical("Unexpected EOF while reading {} bytes", psz);
std::abort();
}
struct iovec vec = {
.iov_base = buffer,
.iov_len = psz,
};
shipment->DecodeIOVec(&vec);
free(buffer);
return true;
}
};
class RowEntity;
class RowShipmentReceiver : public ShipmentReceiver<RowEntity> {
public:
RowShipmentReceiver(go::TcpSocket *sock) : ShipmentReceiver<RowEntity>(sock) {}
~RowShipmentReceiver() { delete sock; }
void Run() override final;
};
// ObjectSliceScanner is per Slice, it contains one Shipment (which is a queue for entities)
// it will scan Entities from the SliceQueues (via SliceScanner) and add them to the Shipment
// then the SliceManager can grab all the Shipments, and Shipment::RunSend()
template <typename T>
class ObjectSliceScanner : public SliceScanner {
Shipment<T> *ship;
public:
ObjectSliceScanner(Slice * slice, Shipment<T> *shipment)
: SliceScanner(slice), ship(shipment) {}
Shipment<T> *shipment() { return ship; }
void AddObject(T *object) {
if (ship) {
ship->AddObject(object);
StatAddObject();
}
}
// only the slices which (shipment != nullptr) will be scanned
void Scan() {
if (!ship) return;
ShippingHandle *handle = nullptr;
size_t cnt = 0;
while ((handle = GetNextHandle())) {
AddObject(((ObjectShippingHandle<T> *) handle)->object);
cnt++;
}
logger->info("[mig]ObjectSliceScanner found {} objects", cnt);
}
// scan the slice for analyzing the max time a row got send
void ScanShippingHandle() {
if (!ship) return;
ResetCursor();
ShippingHandle *handle = nullptr;
unsigned long long max = 0;
size_t cnt = 0;
while ((handle = GetNextHandle())) {
auto cur = handle->GetGeneration();
max = cur > max ? cur : max;
cnt++;
}
logger->info("[mig]In all the {} objects in slice, most shipped row: {} times", cnt, max);
}
};
using RowSliceScanner = ObjectSliceScanner<RowEntity>;
using RowShipment = felis::Shipment<RowEntity>;
class RowScannerRoutine : public go::Routine {
public:
void Run() final override;
};
}
#endif