Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
[Cherry-Pick]Fix aio read (#697)
Browse files Browse the repository at this point in the history
Signed-off-by: zh Wang <[email protected]>
  • Loading branch information
hhy3 authored Feb 20, 2023
1 parent 7f69477 commit bb5fc9b
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions thirdparty/DiskANN/src/linux_aligned_file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace {

void execute_io(io_context_t ctx, uint64_t maxnr, int fd,
const std::vector<AlignedRead> &read_reqs,
uint64_t n_retries = 0) {
uint64_t n_retries = 10) {
#ifdef DEBUG
for (auto &req : read_reqs) {
assert(IS_ALIGNED(req.len, 512));
Expand All @@ -28,14 +28,13 @@ namespace {
#endif

// break-up requests into chunks of size maxnr each
uint64_t n_iters = ROUND_UP(read_reqs.size(), maxnr) / maxnr;
for (uint64_t iter = 0; iter < n_iters; iter++) {
uint64_t n_ops = std::min((uint64_t) read_reqs.size() - (iter * maxnr),
(uint64_t) maxnr);
int64_t n_iters = ROUND_UP(read_reqs.size(), maxnr) / maxnr;
for (int64_t iter = 0; iter < n_iters; iter++) {
int64_t n_ops = std::min(read_reqs.size() - (iter * maxnr), maxnr);
std::vector<iocb_t *> cbs(n_ops, nullptr);
std::vector<io_event_t> evts(n_ops);
std::vector<struct iocb> cb(n_ops);
for (uint64_t j = 0; j < n_ops; j++) {
for (int64_t j = 0; j < n_ops; j++) {
io_prep_pread(cb.data() + j, fd, read_reqs[j + iter * maxnr].buf,
read_reqs[j + iter * maxnr].len,
read_reqs[j + iter * maxnr].offset);
Expand All @@ -48,33 +47,60 @@ namespace {
cbs[i] = cb.data() + i;
}

uint64_t n_tries = 0;
while (n_tries <= n_retries) {
// issue reads
int64_t ret = io_submit(ctx, (int64_t) n_ops, cbs.data());
// if requests didn't get accepted
if (ret != (int64_t) n_ops) {
std::stringstream err;
err << "io_submit() failed; returned " << ret
<< ", expected=" << n_ops << ", ernno=" << errno << "="
<< ::strerror(-ret) << ", try #" << n_tries + 1
<< ", ctx: " << ctx;
throw diskann::ANNException(err.str(), -1, __FUNCSIG__, __FILE__,
__LINE__);
} else {
// wait on io_getevents
ret = io_getevents(ctx, (int64_t) n_ops, (int64_t) n_ops, evts.data(),
nullptr);
// if requests didn't complete
if (ret != (int64_t) n_ops) {
int64_t ret;
int64_t num_submitted = 0, submit_retry = 0;
while (num_submitted < n_ops) {
while ((ret = io_submit(ctx, n_ops - num_submitted,
cbs.data() + num_submitted)) < 0) {
if (-ret != EINTR) {
std::stringstream err;
err << "io_getevents() failed; returned " << ret
<< ", expected=" << n_ops << ", ernno=" << errno << "="
<< ::strerror(-ret) << ", try #" << n_tries + 1;
err << "Unknown error occur in io_submit, errno: " << -ret << ", "
<< strerror(-ret);
throw diskann::ANNException(err.str(), -1, __FUNCSIG__, __FILE__,
__LINE__);
}
}
num_submitted += ret;
if (num_submitted < n_ops) {
submit_retry++;
if (submit_retry <= n_retries) {
LOG(WARNING) << "io_submit() failed; submit: " << num_submitted
<< ", expected: " << n_ops
<< ", retry: " << submit_retry;
} else {
std::stringstream err;
err << "io_submit failed after retried " << n_retries << " times";
throw diskann::ANNException(err.str(), -1, __FUNCSIG__, __FILE__,
__LINE__);
}
}
}

int64_t num_read = 0, read_retry = 0;
while (num_read < n_ops) {
while ((ret = io_getevents(ctx, n_ops - num_read, n_ops - num_read,
evts.data() + num_read, nullptr)) < 0) {
if (-ret != EINTR) {
std::stringstream err;
err << "Unknown error occur in io_getevents, errno: " << -ret
<< ", " << strerror(-ret);
throw diskann::ANNException(err.str(), -1, __FUNCSIG__, __FILE__,
__LINE__);
}
}
num_read += ret;
if (num_read < n_ops) {
read_retry++;
if (read_retry <= n_retries) {
LOG(WARNING) << "io_getevents() failed; read: " << num_read
<< ", expected: " << n_ops
<< ", retry: " << read_retry;
} else {
break;
std::stringstream err;
err << "io_getevents failed after retried " << n_retries
<< " times";
throw diskann::ANNException(err.str(), -1, __FUNCSIG__, __FILE__,
__LINE__);
}
}
}
Expand Down

0 comments on commit bb5fc9b

Please sign in to comment.