-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathservice_ro_segments.cpp
258 lines (200 loc) · 9.76 KB
/
service_ro_segments.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
#include "service_common.h"
bool ro_segment::prepare_access(const topic_partition *const partition) {
enum {
trace = false,
};
assert(partition);
// TODO:
// if we were unable to prepare_access() earlier, we need to fail explicitly for sometime and retry e.g 1 hour later
// otherwise we 'll be retrying to access(open, mmmap, etc) the files/VMAs for every other request to this ro segment
if (fdh) {
// already initialised
if (trace) {
SLog(ansifmt::color_green, "Already Initialised", ansifmt::reset, "\n");
}
assert(fdh->fd != -1);
assert(fdh->fd > 2);
return true;
}
// lazily initialise RO Segments for faster startup
// and reduced resources pressure
int fd, indexFd{-1};
char path[PATH_MAX];
const auto cleanup = [&]() {
fdh.reset();
return false;
};
const auto topic = partition->owner;
const auto absSeqNum = this->baseSeqNum;
const auto lastAbsSeqNum = this->lastAvailSeqNum;
const auto creationTS = this->createdTS;
struct stat64 st;
TANK_EXPECT(topic);
const auto path_base_len = sprintf(path, "%.*s/%s%.*s/%u/",
basePath_.size(), basePath_.data(),
(topic->flags & unsigned(topic::Flags::under_construction)) ? "." : "",
topic->name_.size(), topic->name_.data(),
partition->idx);
if (createdTS) {
sprintf(path + path_base_len, "%" PRIu64 "-%" PRIu64 "_%" PRIu32 ".ilog", absSeqNum, lastAbsSeqNum, createdTS);
} else {
sprintf(path + path_base_len, "%" PRIu64 "-%" PRIu64 ".ilog", absSeqNum, lastAbsSeqNum);
}
if (trace) {
SLog("Will access [", path, "\n");
}
fd = this_service->safe_open(path, O_RDONLY | O_LARGEFILE | O_NOATIME);
if (fd == -1) {
const auto saved = errno;
if (EPOLLIN == saved) {
// provide some guidance because this may result in a lot of wasted time
Print(ansifmt::color_red, ansifmt::bold, "Unable to access ", ansifmt::reset, path,
": The effective UID of the caller does not match the owner of the file, and the caller is not privilleged\n");
}
Print("Failed to access log file ", path, ":", strerror(saved));
return false;
}
fdh.reset(new fd_handle(fd));
assert(fdh);
assert(fdh->fd > 2);
assert(fdh->fd == fd);
if (fstat64(fd, &st) == -1) [[unlikely]] {
Print("Failed to fstat():", strerror(errno));
return cleanup();
}
auto size = st.st_size;
if (size == (off64_t)-1) [[unlikely]] {
Print("lseek64() failed: ", strerror(errno));
return cleanup();
}
TANK_EXPECT(size < std::numeric_limits<std::remove_reference<decltype(fileSize)>::type>::max());
fileSize = size;
if (trace) {
SLog(ansifmt::bold, "fileSize = ", fileSize,
", createdTS = ", Date::ts_repr(creationTS), ansifmt::reset, "\n");
}
if (haveWideEntries) {
indexFd = this_service->safe_open(Buffer::build(str_view32(path, path_base_len), "/", absSeqNum, "_64.index").data(), O_RDONLY | O_LARGEFILE | O_NOATIME);
} else {
indexFd = this_service->safe_open(Buffer::build(str_view32(path, path_base_len), "/", absSeqNum, ".index").data(), O_RDONLY | O_LARGEFILE | O_NOATIME);
}
const auto saved_errno = errno;
DEFER({
if (indexFd != -1) {
TANKUtil::safe_close(indexFd);
}
});
if (indexFd == -1) {
if (saved_errno == ENFILE || saved_errno == EMFILE) {
Print("open() failed: too many open files\n");
return cleanup();
}
if (haveWideEntries) {
indexFd = this_service->safe_open(Buffer::build(str_view32(path, path_base_len), "/", absSeqNum, "_64.index").data(),
read_only ? O_RDONLY : (O_RDWR | O_LARGEFILE | O_CREAT | O_NOATIME), 0775);
} else {
indexFd = this_service->safe_open(Buffer::build(str_view32(path, path_base_len), "/", absSeqNum, ".index").data(),
read_only ? O_RDONLY : (O_RDWR | O_LARGEFILE | O_CREAT | O_NOATIME), 0775);
}
if (indexFd == -1) {
Print("Failed to access index file: ", strerror(errno));
return cleanup();
}
if (haveWideEntries) {
IMPLEMENT_ME();
}
Service::rebuild_index(fdh->fd, indexFd, absSeqNum);
if (trace) {
SLog("Had to Rebuild the Index\n");
}
} else if (trace) {
SLog("open() index OK\n");
}
TANK_EXPECT(indexFd > 2);
size = lseek64(indexFd, 0, SEEK_END);
if (unlikely(size == (off64_t)-1)) {
Print("lseek64() failed: ", strerror(errno));
return cleanup();
}
TANK_EXPECT(size < std::numeric_limits<std::remove_reference<decltype(index.fileSize)>::type>::max());
// TODO(markp): if (haveWideEntries), index.lastRecorded.relSeqNum should be a union, and we should
// properly set index.lastRecorded here
assert(haveWideEntries == false); // not implemented yet
index.fileSize = size;
index.lastRecorded.relSeqNum = index.lastRecorded.absPhysical = 0;
if (size) {
auto data = mmap(nullptr, index.fileSize, PROT_READ, MAP_SHARED, indexFd, 0);
if (unlikely(data == MAP_FAILED)) {
Print("Failed to access the index file. mmap() failed:", strerror(errno),
" for ", size_repr(index.fileSize), " ", path);
return cleanup();
}
madvise(data, index.fileSize, MADV_DONTDUMP);
index.data = static_cast<const uint8_t *>(data);
if (likely(index.fileSize >= sizeof(uint32_t) + sizeof(uint32_t))) {
// last entry in the index; very handy
const auto *const p = (uint32_t *)(index.data + index.fileSize - sizeof(uint32_t) - sizeof(uint32_t));
index.lastRecorded.relSeqNum = p[0];
index.lastRecorded.absPhysical = p[1];
if (trace) {
SLog("lastRecorded = ", index.lastRecorded.relSeqNum,
"(", index.lastRecorded.relSeqNum + baseSeqNum,
"), ", index.lastRecorded.absPhysical, "\n");
}
}
} else {
index.data = nullptr;
}
assert(fdh);
assert(fdh->fd > 2);
return true;
}
ro_segment::ro_segment(const uint64_t absSeqNum,
uint64_t lastAbsSeqNum,
const strwlen32_t base,
const uint32_t creationTS,
const bool wideEntries)
: baseSeqNum{absSeqNum}
, createdTS{creationTS}
, haveWideEntries{wideEntries} {
static constexpr const bool trace{false};
if (trace) {
SLog("New ro_segment(this = ", ptr_repr(this),
", baseSeqNum = ", baseSeqNum,
", lastAbsSeqNum = ", lastAbsSeqNum,
", createdTS = ", createdTS,
", haveWideEntries = ", haveWideEntries, " ", base, "/", absSeqNum, "\n");
}
TANK_EXPECT(lastAbsSeqNum >= baseSeqNum);
index.data = nullptr;
index.fileSize = 0;
fileSize = 0;
// This is no longer enabled during ro_segment::ro_segment()
// TODO: come up with a new CLI option for verifying all RO segments
#if 0
bool _verified{false};
if (std::exchange(_verified, true) == false && []() {
static const bool _verify = getenv("TANK_VERIFY_ROLOGS");
return _verify;
}()) {
const auto l = Service::segment_lastmsg_seqnum(fd, absSeqNum);
if (l != lastAbsSeqNum) {
Buffer new_path;
Print(ansifmt::bold, ansifmt::color_red, "Unexpected last message abs.seqnum (", lastAbsSeqNum, ") for ", path, ", expected ", l, ansifmt::reset, "\n");
lastAbsSeqNum = l;
if (createdTS) {
new_path.append(base, "/", absSeqNum, "-", lastAbsSeqNum, "_", createdTS, ".ilog");
} else {
new_path.append(base, "/", absSeqNum, "-", lastAbsSeqNum, ".ilog");
}
TANKUtil::safe_close(fd);
if (-1 == rename(path.c_str(), new_path.c_str())) {
throw Switch::system_error("Failed to rename()");
}
Print(ansifmt::bold, ansifmt::color_green, "Renamed ", path, " to ", new_path, ansifmt::reset, "\n");
}
}
#endif
assert(not fdh);
this->lastAvailSeqNum = lastAbsSeqNum;
}