Skip to content

Commit d602021

Browse files
committed
MB-61458: Remove sendfile implementation for GetFileFragment
It does not give us a performance boost *AND*: 1. It isn't supported on all platforms 2. We can't use it on top of TLS We shouldn't keep the extra complexity around due to the above Change-Id: I8027ca52c7cb380ce0e33f7cf3c044a4cbdf44e7 Reviewed-on: https://review.couchbase.org/c/kv_engine/+/236622 Reviewed-by: Jim Walker <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent a3b496f commit d602021

File tree

5 files changed

+4
-84
lines changed

5 files changed

+4
-84
lines changed

daemon/connection.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,6 @@ class Connection : public ConnectionIface, public DcpMessageProducersIface {
426426
virtual void chainDataToOutputStream(
427427
std::unique_ptr<SendBuffer> buffer) = 0;
428428

429-
/// Do the underlying implementation used by this backed support using
430-
/// sendfile(2) to transfer data between two file descriptors without
431-
/// copying the data via userspace. Given that it is only our bufferevent
432-
/// backend supporting this (and for !TLS) we save ourself some typing
433-
/// by not making it a pure virtual
434-
virtual bool isSendfileSupported() const {
435-
return false;
436-
}
437-
438-
/// Send length bytes starting from the provided offset of the file
439-
/// descriptor fd to the other end. Given that it is only our bufferevent
440-
/// backend supporting this (and for !TLS) we save ourself some typing
441-
/// by not making it a pure virtual
442-
virtual cb::engine_errc sendFile(int fd, off_t offset, off_t length) {
443-
return cb::engine_errc::not_supported;
444-
}
445-
446429
/**
447430
* Enable the datatype which corresponds to the feature
448431
*

daemon/connection_libevent.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,6 @@ void LibeventConnection::chainDataToOutputStream(
410410
updateSendBytes(data.size());
411411
}
412412

413-
cb::engine_errc LibeventConnection::sendFile(int fd,
414-
off_t offset,
415-
off_t length) {
416-
if (evbuffer_add_file(
417-
bufferevent_get_output(bev.get()), fd, offset, length) == 0) {
418-
return cb::engine_errc::success;
419-
}
420-
throw std::bad_alloc();
421-
}
422-
423413
constexpr const char* invalidPacketHeaderMessage =
424414
"Invalid packet header detected";
425415

daemon/connection_libevent.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ class LibeventConnection : public Connection {
2626
void copyToOutputStream(std::string_view data) override;
2727
void copyToOutputStream(gsl::span<std::string_view> data) override;
2828
void chainDataToOutputStream(std::unique_ptr<SendBuffer> buffer) override;
29-
#ifndef WIN32
30-
bool isSendfileSupported() const override {
31-
return !isTlsEnabled();
32-
}
33-
#endif
34-
cb::engine_errc sendFile(int fd, off_t offset, off_t length) override;
3529
bool isPacketAvailable() const override;
3630
const cb::mcbp::Header& getPacket() const override;
3731
void nextPacket() override;

daemon/protocol/mcbp/get_file_fragment_context.cc

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
GetFileFragmentContext::GetFileFragmentContext(Cookie& cookie)
3030
: SteppableCommandContext(cookie),
3131
uuid(cookie.getRequest().getKeyString()),
32-
useSendfile(cookie.getConnection().isSendfileSupported()),
3332
max_read_size(Settings::instance().getFileFragmentMaxReadSize()),
3433
state(State::Initialize),
3534
chunk_size(Settings::instance().getFileFragmentMaxChunkSize()) {
@@ -41,9 +40,7 @@ GetFileFragmentContext::GetFileFragmentContext(Cookie& cookie)
4140
offset = stoll(json.value("offset", "0"));
4241
length = stoll(json["length"].get<std::string>());
4342
length = std::min(length, max_read_size);
44-
if (!useSendfile) {
45-
filestream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
46-
}
43+
filestream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
4744
}
4845

4946
GetFileFragmentContext::~GetFileFragmentContext() {
@@ -80,9 +77,6 @@ cb::engine_errc GetFileFragmentContext::step() {
8077
case State::ChainFileChunk:
8178
ret = chain_file_chunk();
8279
break;
83-
case State::TransferWithSendFile:
84-
ret = transfer_with_sendfile();
85-
break;
8680
case State::Done:
8781
cookie.clearEwouldblock();
8882
return cb::engine_errc::success;
@@ -123,32 +117,8 @@ cb::engine_errc GetFileFragmentContext::initialize() {
123117
return;
124118
}
125119

126-
if (useSendfile) {
127-
fd = ::open(filename.c_str(), O_RDONLY);
128-
if (fd == -1) {
129-
LOG_WARNING_CTX(
130-
"Failed to open file",
131-
{"conn_id", cookie.getConnectionId()},
132-
{"error", cb_strerror()},
133-
{"file", filename});
134-
if (errno == ENOENT) {
135-
cookie.notifyIoComplete(
136-
cb::engine_errc::no_such_key);
137-
return;
138-
}
139-
if (errno == EACCES) {
140-
cookie.notifyIoComplete(
141-
cb::engine_errc::no_access);
142-
return;
143-
}
144-
cookie.notifyIoComplete(cb::engine_errc::failed);
145-
return;
146-
}
147-
} else {
148-
filestream.open(filename,
149-
std::ios::binary | std::ios::in);
150-
filestream.seekg(offset);
151-
}
120+
filestream.open(filename, std::ios::binary | std::ios::in);
121+
filestream.seekg(offset);
152122

153123
length = std::min(length, max_read_size);
154124
state = State::SendResponseHeader;
@@ -172,11 +142,7 @@ cb::engine_errc GetFileFragmentContext::send_response_header() {
172142
{},
173143
length,
174144
PROTOCOL_BINARY_RAW_BYTES);
175-
if (connection.isSendfileSupported()) {
176-
state = State::TransferWithSendFile;
177-
} else {
178-
state = State::ReadFileChunk;
179-
}
145+
state = State::ReadFileChunk;
180146
return cb::engine_errc::success;
181147
}
182148

@@ -216,16 +182,6 @@ cb::engine_errc GetFileFragmentContext::read_file_chunk() {
216182
return cb::engine_errc::success;
217183
}
218184

219-
cb::engine_errc GetFileFragmentContext::transfer_with_sendfile() {
220-
const auto ret = connection.sendFile(fd, offset, length);
221-
if (ret == cb::engine_errc::success) {
222-
// evbuffer took the ownership..
223-
fd = -1;
224-
}
225-
state = State::Done;
226-
return ret;
227-
}
228-
229185
cb::engine_errc GetFileFragmentContext::chain_file_chunk() {
230186
std::unique_ptr<folly::IOBuf> iob;
231187
chunk.swap(iob);

daemon/protocol/mcbp/get_file_fragment_context.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class GetFileFragmentContext : public SteppableCommandContext {
4343
SendResponseHeader,
4444
ReadFileChunk,
4545
ChainFileChunk,
46-
TransferWithSendFile,
4746
Done
4847
};
4948

@@ -58,9 +57,7 @@ class GetFileFragmentContext : public SteppableCommandContext {
5857
cb::engine_errc send_response_header();
5958
cb::engine_errc read_file_chunk();
6059
cb::engine_errc chain_file_chunk();
61-
cb::engine_errc transfer_with_sendfile();
6260
const std::string uuid;
63-
const bool useSendfile;
6461
std::string filename;
6562
std::size_t id{0};
6663
std::size_t offset{0};

0 commit comments

Comments
 (0)