forked from bersler/OpenLogReplicator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReaderFilesystem.cpp
More file actions
129 lines (102 loc) · 4.26 KB
/
Copy pathReaderFilesystem.cpp
File metadata and controls
129 lines (102 loc) · 4.26 KB
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
/* Base class for reading redo from file system
Copyright (C) 2018-2024 Adam Leszczynski ([email protected])
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 <cerrno>
#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../common/Clock.h"
#include "../common/Ctx.h"
#include "ReaderFilesystem.h"
namespace OpenLogReplicator {
ReaderFilesystem::ReaderFilesystem(Ctx* newCtx, const std::string& newAlias, const std::string& newDatabase, int64_t newGroup, bool newConfiguredBlockSum) :
Reader(newCtx, newAlias, newDatabase, newGroup, newConfiguredBlockSum),
fileDes(-1),
flags(0) {
}
ReaderFilesystem::~ReaderFilesystem() {
ReaderFilesystem::redoClose();
}
void ReaderFilesystem::redoClose() {
if (fileDes != -1) {
close(fileDes);
fileDes = -1;
}
}
uint64_t ReaderFilesystem::redoOpen() {
struct stat fileStat;
if (stat(fileName.c_str(), &fileStat) != 0) {
ctx->error(10003, "file: " + fileName + " - stat returned: " + strerror(errno));
return REDO_ERROR;
}
flags = O_RDONLY;
fileSize = fileStat.st_size;
#if __linux__
if (!ctx->flagsSet(Ctx::REDO_FLAGS_DIRECT_DISABLE))
flags |= O_DIRECT;
#endif
fileDes = open(fileName.c_str(), flags);
if (fileDes == -1) {
ctx->error(10001, "file: " + fileName + " - open returned: " + strerror(errno));
return REDO_ERROR;
}
#if __APPLE__
if (!ctx->flagsSet(Ctx::REDO_FLAGS_DIRECT_DISABLE)) {
if (fcntl(fileDes, F_GLOBAL_NOCACHE, 1) < 0)
ctx->error(10008, "file: " + fileName + " - set no cache returned: " + strerror(errno));
}
#endif
return REDO_OK;
}
int64_t ReaderFilesystem::redoRead(uint8_t* buf, uint64_t offset, uint64_t size) {
uint64_t startTime = 0;
if (ctx->trace & TRACE_PERFORMANCE)
startTime = ctx->clock->getTimeUt();
int64_t bytes = 0;
uint64_t tries = ctx->archReadTries;
while (tries > 0) {
if (ctx->hardShutdown)
break;
bytes = pread(fileDes, buf, size, static_cast<int64_t>(offset));
if (ctx->trace & TRACE_FILE)
ctx->logTrace(TRACE_FILE, "read " + fileName + ", " + std::to_string(offset) + ", " + std::to_string(size) +
" returns " + std::to_string(bytes));
if (bytes > 0)
break;
// Retry for SSHFS broken connection: Transport endpoint is not isConnected
if (bytes == -1 && errno != ENOTCONN)
break;
ctx->error(10005, "file: " + fileName + " - " + std::to_string(bytes) + " bytes read instead of " + std::to_string(size));
if (ctx->hardShutdown)
break;
ctx->info(0, "sleeping " + std::to_string(ctx->archReadSleepUs) + " us before retrying read");
usleep(ctx->archReadSleepUs);
--tries;
}
// Maybe direct IO does not work
if (bytes < 0 && !ctx->flagsSet(Ctx::REDO_FLAGS_DIRECT_DISABLE)) {
ctx->hint("if problem is related to Direct IO, try to restart with Direct IO mode disabled, set 'flags' to value: " +
std::to_string(Ctx::REDO_FLAGS_DIRECT_DISABLE));
}
if (ctx->trace & TRACE_PERFORMANCE) {
if (bytes > 0)
sumRead += bytes;
sumTime += ctx->clock->getTimeUt() - startTime;
}
return bytes;
}
}