-
Notifications
You must be signed in to change notification settings - Fork 124
/
WriterFile.cpp
320 lines (278 loc) · 13 KB
/
WriterFile.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
/* Thread writing to file (or stdout)
Copyright (C) 2018-2023 Adam Leszczynski (aleszczynski@bersler.com)
This file is part of OpenLogReplicator.
OpenLogReplicator is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your option)
any later version.
OpenLogReplicator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenLogReplicator; see the file LICENSE; If not see
<http://www.gnu.org/licenses/>. */
#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64
#include <cstring>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../builder/Builder.h"
#include "../common/ConfigurationException.h"
#include "../common/RuntimeException.h"
#include "WriterFile.h"
namespace OpenLogReplicator {
WriterFile::WriterFile(Ctx* newCtx, const std::string& newAlias, const std::string& newDatabase, Builder* newBuilder, Metadata* newMetadata,
const char* newOutput, const char* newTimestampFormat, uint64_t newMaxFileSize, uint64_t newNewLine, uint64_t newAppend) :
Writer(newCtx, newAlias, newDatabase, newBuilder, newMetadata),
prefixPos(0),
suffixPos(0),
mode(WRITER_FILE_MODE_STDOUT),
fill(0),
output(newOutput),
timestampFormat(newTimestampFormat),
fileNameNum(0),
fileSize(0),
maxFileSize(newMaxFileSize),
outputDes(-1),
newLine(newNewLine),
append(newAppend),
lastSequence(ZERO_SEQ),
newLineMsg(nullptr),
warningDisplayed(false) {
}
WriterFile::~WriterFile() {
closeFile();
}
void WriterFile::initialize() {
Writer::initialize();
if (newLine == 1) {
newLineMsg = "\n";
} else if (newLine == 2) {
newLineMsg = "\r\n";
}
if (this->output.length() == 0) {
outputDes = STDOUT_FILENO;
return;
}
auto outputIt = this->output.find_last_of('/');
if (outputIt != std::string::npos) {
pathName = this->output.substr(0, outputIt);
fileNameMask = this->output.substr(outputIt + 1);
} else {
pathName = ".";
fileNameMask = this->output;
}
if ((prefixPos = this->output.find("%i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
suffixPos = prefixPos + 2;
} else if ((prefixPos = this->output.find("%2i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 2;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%3i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 3;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%4i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 4;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%5i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 5;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%6i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 6;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%7i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 7;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%8i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 8;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%9i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 9;
suffixPos = prefixPos + 3;
} else if ((prefixPos = this->output.find("%10i")) != std::string::npos) {
mode = WRITER_FILE_MODE_NUM;
fill = 10;
suffixPos = prefixPos + 4;
} else if ((prefixPos = this->output.find("%t")) != std::string::npos) {
mode = WRITER_FILE_MODE_TIMESTAMP;
suffixPos = prefixPos + 2;
} else if ((prefixPos = this->output.find("%s")) != std::string::npos) {
mode = WRITER_FILE_MODE_SEQUENCE;
suffixPos = prefixPos + 2;
} else {
if ((prefixPos = this->output.find('%')) != std::string::npos)
throw ConfigurationException(30005, "invalid value for 'output': " + this->output);
if (append == 0)
throw ConfigurationException(30006, "output file is with no rotation: " + this->output + " - 'append' must be set to 1");
mode = WRITER_FILE_MODE_NO_ROTATE;
}
if ((mode == WRITER_FILE_MODE_TIMESTAMP || mode == WRITER_FILE_MODE_NUM) && maxFileSize == 0)
throw ConfigurationException(30007, "output file is with no max file size: " + this->output +
" - 'max-file-size' must be defined for output with rotation");
// Search for last used number
if (mode == WRITER_FILE_MODE_NUM) {
DIR* dir;
if ((dir = opendir(pathName.c_str())) == nullptr)
throw RuntimeException(10012, "directory: " + pathName + " - can't read");
struct dirent* ent;
while ((ent = readdir(dir)) != nullptr) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
struct stat fileStat;
std::string fileName(ent->d_name);
std::string fileNameFull(pathName + "/" + ent->d_name);
if (stat(fileNameFull.c_str(), &fileStat) != 0) {
ctx->warning(60034, "file: " + fileNameFull + " - stat returned: " + strerror(errno));
continue;
}
if (S_ISDIR(fileStat.st_mode))
continue;
std::string prefix(fileNameMask.substr(0, prefixPos));
if (fileName.length() < prefix.length() || fileName.substr(0, prefix.length()) != prefix)
continue;
std::string suffix(fileNameMask.substr(suffixPos));
if (fileName.length() < suffix.length() || fileName.substr(fileName.length() - suffix.length()) != suffix)
continue;
if (ctx->trace & TRACE_WRITER)
ctx->logTrace(TRACE_WRITER, "found previous output file: " + pathName + "/" + fileName);
std::string fileNameFoundNum(fileName.substr(prefix.length(), fileName.length() - suffix.length() - prefix.length()));
typeScn fileNum;
try {
fileNum = strtoull(fileNameFoundNum.c_str(), nullptr, 10);
} catch (std::exception& e) {
// Ignore other files
continue;
}
if (append > 0) {
if (fileNameNum < fileNum)
fileNameNum = fileNum;
} else {
if (fileNameNum <= fileNum)
fileNameNum = fileNum + 1;
}
}
closedir(dir);
ctx->info(0, "next number for " + this->output + " is: " + std::to_string(fileNameNum));
}
}
void WriterFile::closeFile() {
if (outputDes != -1) {
close(outputDes);
outputDes = -1;
}
}
void WriterFile::checkFile(typeScn scn __attribute__((unused)), typeSeq sequence, uint64_t length) {
if (mode == WRITER_FILE_MODE_STDOUT) {
return;
} else if (mode == WRITER_FILE_MODE_NO_ROTATE) {
fullFileName = pathName + "/" + fileNameMask;
} else if (mode == WRITER_FILE_MODE_NUM) {
if (fileSize + length > maxFileSize) {
closeFile();
++fileNameNum;
fileSize = 0;
}
if (length > maxFileSize)
ctx->warning(60029, "message size (" + std::to_string(length) + ") will exceed 'max-file' size (" +
std::to_string(maxFileSize) + ")");
if (outputDes == -1) {
std::string outputFileNumStr(std::to_string(fileNameNum));
uint64_t zeros = 0;
if (fill > outputFileNumStr.length())
zeros = fill - outputFileNumStr.length();
fullFileName = pathName + "/" + fileNameMask.substr(0, prefixPos) + std::string(zeros, '0') + outputFileNumStr +
fileNameMask.substr(suffixPos);
}
} else if (mode == WRITER_FILE_MODE_TIMESTAMP) {
bool shouldSwitch = false;
if (fileSize + length > maxFileSize)
shouldSwitch = true;
if (length > maxFileSize)
ctx->warning(60029, "message size (" + std::to_string(length) + ") will exceed 'max-file' size (" +
std::to_string(maxFileSize) + ")");
if (outputDes == -1 || shouldSwitch) {
time_t now = time(nullptr);
tm nowTm = *localtime(&now);
char str[50];
strftime(str, sizeof(str), timestampFormat.c_str(), &nowTm);
std::string newOutputFile = pathName + "/" + fileNameMask.substr(0, prefixPos) + str + fileNameMask.substr(suffixPos);
if (fullFileName == newOutputFile) {
if (!warningDisplayed) {
ctx->warning(60030, "rotation size is set too low (" + std::to_string(maxFileSize) +
"), increase it, should rotate but too early (" + fullFileName + ")");
warningDisplayed = true;
}
shouldSwitch = false;
} else
fullFileName = newOutputFile;
}
if (shouldSwitch) {
closeFile();
fileSize = 0;
}
} else if (mode == WRITER_FILE_MODE_SEQUENCE) {
if (sequence != lastSequence) {
closeFile();
}
lastSequence = sequence;
if (outputDes == -1)
fullFileName = pathName + "/" + fileNameMask.substr(0, prefixPos) + std::to_string(sequence) +
fileNameMask.substr(suffixPos);
}
// File is closed, open it
if (outputDes == -1) {
struct stat fileStat;
if (stat(fullFileName.c_str(), &fileStat) != 0) {
// File already exists, append?
if (append == 0)
throw RuntimeException(10003, "file: " + fullFileName + " - stat returned: " + strerror(errno));
fileSize = fileStat.st_size;
} else
fileSize = 0;
ctx->info(0, "opening output file: " + fullFileName);
outputDes = open(fullFileName.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (outputDes == -1)
throw RuntimeException(10006, "file: " + fullFileName + " - open for write returned: " + strerror(errno));
if (lseek(outputDes, 0, SEEK_END) == -1)
throw RuntimeException(10011, "file: " + fullFileName + " - seek returned: " + strerror(errno));
}
}
void WriterFile::sendMessage(BuilderMsg* msg) {
if (newLine > 0)
checkFile(msg->scn, msg->sequence, msg->length + 1);
else
checkFile(msg->scn, msg->sequence, msg->length);
int64_t bytesWritten = write(outputDes, (const char*)msg->data, msg->length);
if (static_cast<uint64_t>(bytesWritten) != msg->length)
throw RuntimeException(10007, "file: " + fullFileName + " - " + std::to_string(bytesWritten) + " bytes written instead of " +
std::to_string(msg->length) + ", code returned: " + strerror(errno));
fileSize += bytesWritten;
if (newLine > 0) {
bytesWritten = write(outputDes, newLineMsg, newLine);
if (static_cast<uint64_t>(bytesWritten) != newLine)
throw RuntimeException(10007, "file: " + fullFileName + " - " + std::to_string(bytesWritten) + " bytes written instead of " +
std::to_string(msg->length) + ", code returned: " + strerror(errno));
fileSize += bytesWritten;
}
confirmMessage(msg);
}
std::string WriterFile::getName() const {
if (outputDes == STDOUT_FILENO)
return "stdout";
else
return "file:" + pathName + "/" + fileNameMask;
}
void WriterFile::pollQueue() {
}
}