-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathservice_write.cpp
504 lines (394 loc) · 21.7 KB
/
service_write.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include "service_common.h"
int Rename(const char *oldpath, const char *newpath);
int Unlink(const char *pathname);
bool topic_partition_log::should_roll(const uint32_t now) const {
enum {
trace = false,
trace_yes = false,
};
cur.sanity_checks();
if (cur.fileSize == UINT32_MAX) {
return true;
}
if (0 == cur.fileSize) {
return false;
}
TANK_EXPECT(this_service);
if (now < this_service->no_roll_until) {
// if we run out of FDs, we don't want to keep trying in every produce request
return false;
}
if (trace) {
SLog(ansifmt::color_green, " Consider roll:cur.fileSize(", cur.fileSize,
"), config.maxSegmentSize(", config.maxSegmentSize,
"), skipList.size(", cur.index.skipList.size(),
"), config.curSegmentMaxAge (", config.curSegmentMaxAge,
"), ", Timings::Seconds::SysTime() - cur.createdTS,
" old, cur.rollJitterSecs = ", cur.rollJitterSecs, ansifmt::reset, "\n");
}
#if 1
const std::size_t max_seg_size_ceil = config.maxSegmentSize;
#else
const std::size_t max_seg_size_ceil = 10;
#endif
if (cur.fileSize > max_seg_size_ceil) [[unlikely]] {
if (trace == true || trace_yes == true) {
SLog(ansifmt::bold, "Should roll: cur.fileSize(", cur.fileSize, ") > config.maxSegmentSize(", config.maxSegmentSize, ")", ansifmt::reset, "\n");
}
return true;
}
const size_t curIndexSizeBytes = cur.index.ondisk.span + (cur.index.skipList.size() * (sizeof(uint32_t) + sizeof(uint32_t)));
if (curIndexSizeBytes > config.maxIndexSize) [[unlikely]] {
// index is full
if (trace == true || trace_yes == true) {
SLog(ansifmt::bold, "curIndexSizeBytes(", curIndexSizeBytes, ") > config.maxIndexSize(", config.maxIndexSize, ")", ansifmt::reset, "\n");
}
return true;
}
cur.sanity_checks();
if (const auto v = cur.rollJitterSecs) {
// XXX: it is important to use time32_delta() to guard against the race that stems from _now updates semantics
if (const auto since_seconds = TANKUtil::time32_delta(cur.createdTS, now); since_seconds > cur.rollJitterSecs) {
// Soft limit
if (trace == true or trace_yes == true) {
SLog(ansifmt::bold, "now - cur.createdTS(",
since_seconds, "s ago) > (", v - cur.rollJitterSecs,
") cur.rollJitterSecs = ", cur.rollJitterSecs,
", cur.createdTS = ", cur.createdTS,
"(", Date::ts_repr(cur.createdTS),
"), now = ", now,
" (", Date::ts_repr(now), ")", ansifmt::reset, "\n");
}
return true;
}
}
return false;
}
bool topic_partition_log::may_switch_index_wide(const uint64_t lastMsgSeqNum) {
// This is required for proper support for sparse segments
// maybe instead of transforming the index we should instead roll?
static constexpr bool trace{false};
if (trace) {
SLog("considering switch to wide index, lastMsgSeqNum = ", lastMsgSeqNum, ", cur.baseSeqNum = ", cur.baseSeqNum, "\n");
}
if (unlikely(lastMsgSeqNum - cur.baseSeqNum > INT32_MAX)) {
// arbitrary
IMPLEMENT_ME();
cur.index.haveWideEntries = true;
}
return false;
}
void topic_partition_log::roll(const uint64_t absSeqNum, const uint64_t saved_last_assigned_seqnum) {
static constexpr bool trace{false};
Buffer basePath;
int fd;
basePath.append(basePath_, "/", partition->owner->name(), "/", partition->idx, "/");
const auto basePathLen = basePath.size();
const auto _now = time(nullptr);
if (_now == ((time_t)-1)) {
// Yes, this _can_ fail
Print("Failed ", strerror(errno), "\n");
std::abort();
}
if (trace) {
SLog("Need to switch to another commit log (cur.fileSize = ", cur.fileSize,
"> config.maxSegmentSize = ", config.maxSegmentSize,
") cur.index.skipList.size = ", cur.index.skipList.size(),
", lastAssignedSeqNum = ", lastAssignedSeqNum,
" basePath = ", basePath, "\n");
}
cur.sanity_checks();
if (cur.fileSize != UINT32_MAX) {
// See ro_segment::createdTS declaration comments
struct stat st;
if (fstat(cur.fdh->fd, &st) == -1) {
if (errno == ENFILE || errno == EMFILE || errno == ENOSPC || errno == EDQUOT) {
TANK_EXPECT(this_service);
this_service->no_roll_until = time(nullptr) + 60;
}
throw Switch::system_error("fstat() failed:", strerror(errno));
}
const uint64_t freezeTs = st.st_mtime;
auto newROFiles = std::make_unique<std::vector<ro_segment *>>();
auto newROFile = std::make_unique<ro_segment>(cur.baseSeqNum, saved_last_assigned_seqnum, freezeTs);
const auto n = cur.fdh.use_count();
TANK_EXPECT(n >= 1);
newROFile->fdh = cur.fdh;
TANK_EXPECT(cur.fdh.use_count() == n + 1);
newROFile->fileSize = cur.fileSize;
newROFile->index.fileSize = lseek64(cur.index.fd, 0, SEEK_END);
newROFile->index.lastRecorded.relSeqNum = newROFile->index.lastRecorded.absPhysical = 0;
if (cur.index.haveWideEntries) {
IMPLEMENT_ME();
}
// We now encode the [first,last] range into the filename for simplicity and future-proofing; we 'd like to
// support sparse sequence numbers space
if (cur.nameEncodesTS) {
if (Rename(Buffer::build(basePath, "/", cur.baseSeqNum, "_", cur.createdTS, ".log").data(),
Buffer::build(basePath, "/", cur.baseSeqNum, "-", saved_last_assigned_seqnum, "_", freezeTs, ".ilog").data()) == -1) {
throw Switch::system_error("Failed to Rename():", strerror(errno));
}
} else if (Rename(Buffer::build(basePath, "/", cur.baseSeqNum, ".log").data(),
Buffer::build(basePath, "/", cur.baseSeqNum, "-", saved_last_assigned_seqnum, "_", freezeTs, ".ilog").data()) == -1) {
throw Switch::system_error("Failed to Rename():", strerror(errno));
}
if (newROFile->index.fileSize) {
newROFile->index.data = static_cast<uint8_t *>(mmap(nullptr, newROFile->index.fileSize, PROT_READ, MAP_SHARED, cur.index.fd, 0));
if (unlikely(newROFile->index.data == MAP_FAILED)) {
throw Switch::system_error("mmap() failed:", strerror(errno));
}
madvise((void *)newROFile->index.data, newROFile->index.fileSize, MADV_DONTDUMP);
if (newROFile->index.fileSize >= sizeof(uint32_t) + sizeof(uint32_t)) {
const auto *const p = (uint32_t *)(newROFile->index.data + newROFile->index.fileSize - sizeof(uint32_t) - sizeof(uint32_t));
newROFile->index.lastRecorded.relSeqNum = p[0];
newROFile->index.lastRecorded.absPhysical = p[1];
if (trace) {
SLog("set lastRecorded to ", newROFile->index.lastRecorded.relSeqNum, ", ", newROFile->index.lastRecorded.absPhysical, "\n");
}
}
} else {
newROFile->index.data = nullptr;
}
const auto prevSize = newROFiles->size();
newROFiles->insert(newROFiles->end(), roSegments->begin(), roSegments->end());
TANK_EXPECT(newROFiles->size() == prevSize + roSegments->size());
newROFiles->push_back(newROFile.release());
roSegments.reset(newROFiles.release());
consider_ro_segments();
} else {
// first segment for this partition
firstAvailableSeqNum = absSeqNum;
}
cur.baseSeqNum = absSeqNum;
cur.fileSize = 0;
// It is very important than the first bundle's recorded immediately in the index
// so we set cur.sinceLastUpdate = UINT32_MAX to force that
cur.sinceLastUpdate = UINT32_MAX;
cur.createdTS = time(nullptr); // will not assign to _now, to reduce likelihood of race conditions
cur.nameEncodesTS = true;
cur.index.haveWideEntries = false;
cur.index.skipList.clear();
if (cur.index.fd != -1) {
fsync(cur.index.fd);
TANKUtil::safe_close(cur.index.fd);
cur.index.fd = -1;
}
cur.reset_cache();
if (cur.index.ondisk.data != nullptr && cur.index.ondisk.data != MAP_FAILED) {
auto ptr = reinterpret_cast<void *>(const_cast<uint8_t *>(cur.index.ondisk.data));
madvise(ptr, cur.index.ondisk.span, MADV_DONTNEED);
munmap(ptr, cur.index.ondisk.span);
cur.index.ondisk.data = nullptr;
}
[[maybe_unused]] const auto saved = cur.createdTS;
basePath.append(cur.baseSeqNum, "_", cur.createdTS, ".log");
if (trace) {
SLog("New active:", basePath, " ", cur.createdTS, " ", Date::ts_repr(cur.createdTS), "\n");
}
cur.sanity_checks();
// XXX:
// Should open() .log.transient and .index.transient
// and if both are successful, rename and use
// as opposed to throwing an exception here and getting stuck in limbo
fd = this_service->safe_open(basePath.c_str(), read_only ? O_RDONLY : (O_RDWR | O_LARGEFILE | O_CREAT | O_NOATIME | O_APPEND), 0775);
if (-1 == fd) {
if (errno == ENFILE || errno == EMFILE || errno == ENOSPC || errno == EDQUOT) {
TANK_EXPECT(this_service);
this_service->no_roll_until = time(nullptr) + 60;
}
throw Switch::system_error("open(", basePath, ") failed:", strerror(errno), ". Cannot load segment log");
}
partition->flags &= ~unsigned(topic_partition::Flags::NoDataFiles);
cur.fdh.reset(new fd_handle(fd));
basePath.resize(basePathLen);
basePath.append(cur.baseSeqNum, ".index");
fd = this_service->safe_open(basePath.c_str(), read_only ? O_RDWR : (O_RDWR | O_LARGEFILE | O_CREAT | O_NOATIME | O_APPEND), 0775);
if (-1 == fd) {
const auto saved_errno = errno;
if (errno == ENFILE || errno == EMFILE || errno == ENOSPC || errno == EDQUOT) {
TANK_EXPECT(this_service);
this_service->no_roll_until = time(nullptr) + 60;
}
unlink(basePath.c_str());
basePath.resize(basePathLen);
basePath.append(cur.baseSeqNum, "_", cur.createdTS, ".log");
unlink(basePath.c_str());
throw Switch::system_error("open(", basePath, ") failed:", strerror(saved_errno), ". Cannot load segment index");
}
cur.index.fd = fd;
cur.ra_proxy.ra.reset_to(cur.fdh.get()->fd);
basePath.resize(basePathLen);
cur.sanity_checks();
if (const uint32_t max = config.maxRollJitterSecs) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<uint32_t> distr(0, max);
// make sure this is not low
cur.rollJitterSecs = std::max<uint32_t>(distr(rng),
Timings::Hours::ToSeconds(1));
} else {
// something sensible
cur.rollJitterSecs = std::max<uint32_t>(Timings::Hours::ToSeconds(1), Timings::Weeks::ToSeconds(1));
}
cur.flush_state.pendingFlushMsgs = 0;
cur.flush_state.nextFlushTS = config.flushIntervalSecs
? time(nullptr) + config.flushIntervalSecs
: UINT32_MAX;
if (trace) {
SLog("Switched\n");
}
cur.sanity_checks();
// it's important that we do this as soon as we roll
cur.triangulation_cache.clear();
cur.reset_cache();
cur.sanity_checks();
TANK_EXPECT(saved == cur.createdTS);
}
void topic_partition_log::flush_index_skiplist() {
// Implements https://github.com/phaistos-networks/TANK/issues/27
static constexpr bool trace{false};
if (trace) {
SLog(ansifmt::bold, ansifmt::color_red, "Emptying skiplist", ansifmt::reset, "\n");
}
if (cur.index.ondisk.data != nullptr && cur.index.ondisk.data != MAP_FAILED) {
auto ptr = reinterpret_cast<void *>(const_cast<uint8_t *>(cur.index.ondisk.data));
madvise(ptr, cur.index.ondisk.span, MADV_DONTNEED);
munmap(ptr, cur.index.ondisk.span);
}
cur.index.ondisk.span = lseek64(cur.index.fd, 0, SEEK_END);
cur.index.ondisk.data = static_cast<const uint8_t *>(mmap(nullptr, cur.index.ondisk.span, PROT_READ, MAP_SHARED, cur.index.fd, 0));
cur.index.haveWideEntries = false;
if (cur.index.ondisk.data == MAP_FAILED) {
throw Switch::system_error("mmap() failed:", strerror(errno));
}
madvise(reinterpret_cast<void *>(const_cast<uint8_t *>(cur.index.ondisk.data)), cur.index.ondisk.span, MADV_DONTDUMP);
cur.index.skipList.clear();
}
// if (firstMsgSeqNum != 0 && lastMsgSeqNum != 0), we have expicitly specified message sequence numbers for the bundle first/last message
append_res topic_partition_log::append_bundle(const time_t now,
const void *bundle,
const size_t bundleSize,
const uint32_t bundleMsgsCnt,
const uint64_t firstMsgSeqNum,
const uint64_t lastMsgSeqNum) {
enum {
trace = false,
};
const auto saved_last_assigned_seqnum = lastAssignedSeqNum;
const auto absSeqNum = firstMsgSeqNum ?: lastAssignedSeqNum + 1;
if (unlikely(absSeqNum < cur.baseSeqNum && cur.baseSeqNum != std::numeric_limits<uint64_t>::max())) {
// This is odd
throw Switch::data_error("Unexpected absSeqNum(", absSeqNum, ") < cur.baseSeqNum(", cur.baseSeqNum, ") for ", partition->owner->name(), "/", partition->idx);
}
TANK_EXPECT(bundleMsgsCnt);
cur.sanity_checks();
if (lastMsgSeqNum) {
// Sparse bundle; last message seqNum encoded in the bundle header
lastAssignedSeqNum = lastMsgSeqNum;
} else if (firstMsgSeqNum) {
// either sparse bundle(first message encoded in the bundle header), or bundle in
// a TankAPIMsgType::ProduceWithSeqnum request, where the bundle is encoded in the partition header
lastAssignedSeqNum = firstMsgSeqNum + bundleMsgsCnt - 1;
} else {
lastAssignedSeqNum += bundleMsgsCnt;
}
if (trace) {
SLog("firstMsgSeqNum(", firstMsgSeqNum,
"), lastMsgSeqNum(", lastMsgSeqNum,
"), bundleMsgsCnt(", bundleMsgsCnt,
") => lastAssignedSeqNum = ", lastAssignedSeqNum, "\n");
}
if (unlikely(should_roll(now))) {
roll(absSeqNum, saved_last_assigned_seqnum);
} else {
if (lastMsgSeqNum && !cur.index.haveWideEntries) {
// This may be necessary
may_switch_index_wide(lastMsgSeqNum);
}
if (unlikely(cur.index.skipList.size() > 65536)) {
flush_index_skiplist();
}
}
TANK_EXPECT(cur.fdh.use_count() >= 1);
uint8_t varint[8];
const uint8_t varintLen = Compression::PackUInt32(bundleSize, varint) - varint;
auto fd = cur.fdh->fd;
const struct iovec iov[] =
{
{(void *)varint, varintLen},
{const_cast<void *>(bundle), bundleSize},
};
const auto entryLen = iov[0].iov_len + iov[1].iov_len;
const range32_t fileRange(cur.fileSize, entryLen);
const auto before = cur.fdh.use_count();
std::shared_ptr<fd_handle> fdh(cur.fdh);
const auto b = trace ? Timings::Microseconds::Tick() : uint64_t(0);
TANK_EXPECT(cur.fdh.use_count() == before + 1);
// https://github.com/phaistos-networks/TANK/issues/14
if (unlikely(writev(fd, iov, sizeof_array(iov)) != entryLen)) {
if (EDQUOT == errno || ENOSPC == errno) {
TANK_EXPECT(this_service);
this_service->no_roll_until = this_service->curTime + 60;
}
lastAssignedSeqNum = saved_last_assigned_seqnum;
this_service->track_io_fail(partition);
return {nullptr, {}, {}};
} else {
if (trace) {
SLog("writev() took ", duration_repr(Timings::Microseconds::Since(b)),
", entryLen = ", entryLen,
", cur.sinceLastUpdate = ", cur.sinceLastUpdate,
", config.indexInterval = ", config.indexInterval, "\n");
}
// Even if we fail to update the index, that's not a big deal because
// 1. we can always rebuild the index 2. we use the index to locate the closest bundle to the target sequence number
if (cur.sinceLastUpdate > config.indexInterval) {
TANK_EXPECT(absSeqNum >= cur.baseSeqNum); // sanity check
const uint32_t out[] = {static_cast<uint32_t>(absSeqNum - cur.baseSeqNum),
cur.fileSize};
cur.index.skipList.push_back({out[0], out[1]});
if (trace) {
SLog(">> ", out[0], ", ", out[1], " ", cur.index.skipList.size(), "\n");
}
if (unlikely(write(cur.index.fd, out, sizeof(out)) != sizeof(out))) {
// don't restore neither lastAssignedSeqNum from saved_last_assigned_seqnum, nor fileSize
// because this has been accepted
if (EDQUOT == errno || ENOSPC == errno) {
TANK_EXPECT(this_service);
this_service->no_roll_until = this_service->curTime + 60;
}
this_service->track_io_fail(partition);
return {nullptr, {}, {}};
}
if (0 == cur.fileSize) {
// Make sure we get that first record synced
fsync(cur.index.fd);
}
cur.sinceLastUpdate = 0;
}
cur.fileSize += entryLen;
cur.sinceLastUpdate += entryLen;
cur.flush_state.pendingFlushMsgs += bundleMsgsCnt;
if (trace) {
SLog("cur.flush_state.pendingFlushMsgs = ", cur.flush_state.pendingFlushMsgs,
", config.flushIntervalMsgs = ", config.flushIntervalMsgs,
", config.flushIntervalMsgs = ", config.flushIntervalMsgs, "\n");
}
if (config.flushIntervalMsgs && cur.flush_state.pendingFlushMsgs >= config.flushIntervalMsgs) {
if (trace) {
SLog("Scheduling flush\n");
}
schedule_flush(now);
} else if (now >= cur.flush_state.nextFlushTS) {
if (trace) {
SLog("Scheduling flush\n");
}
schedule_flush(now);
}
this_service->track_io_success(partition);
return {fdh, fileRange, {absSeqNum, uint16_t(bundleMsgsCnt)}};
}
// return here and not in (writev() ! entryLen) check because some older compilers warn about
// a path with no return from a non-void function. Sigh
return {nullptr, {}, {}};
}