Fail the fio job instead of hanging when out of open files #207

Open
koolkhel wants to merge 2 commits from koolkhel/vitastor:fio-out-of-fds into master

View File

@@ -18,6 +18,9 @@
// fio -thread -ioengine=./libfio_cluster.so -name=test -bs=4k -direct=1 -iodepth=32 -rw=randread \
// -etcd=127.0.0.1:2379 [-etcd_prefix=/vitastor] -image=testimg
#include <fcntl.h>
#include <time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -38,6 +41,8 @@ struct sec_data
/* The list of completed io_u structs. */
std::vector<io_u*> completed;
uint64_t inflight = 0;
// last completion, or the first queued op after an idle period
time_t last_progress = 0;
int mirror_fd = -1;
bool trace = false;
};
@@ -225,6 +230,42 @@ static void opt_push(std::vector<char *> & options, const char *opt, const char
}
}
static time_t sec_now()
{
timespec ts;
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
return ts.tv_sec;
}
// A client out of fds retries its connections forever, so nothing it waits for completes:
// fail the job once nothing completed for this long and no fd can be opened
static const time_t sec_stall_seconds = 30;
static bool sec_out_of_fds(struct thread_data *td, time_t since)
{
if (sec_now() - since < sec_stall_seconds)
return false;
int fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
if (fd >= 0)
{
close(fd);
return false;
}
int err = errno;
if (err != EMFILE && err != ENFILE)
return false;
struct rlimit rl = {};
getrlimit(RLIMIT_NOFILE, &rl);
fprintf(stderr, "vitastor: nothing completed for %ld s and no file descriptor is left (%s), open files"
" limit: soft %ju, hard %ju. Each job keeps a connection to every OSD it writes to: raise ulimit -n.\n",
(long)sec_stall_seconds, strerror(err), (uintmax_t)rl.rlim_cur, (uintmax_t)rl.rlim_max);
// fio keeps 128 bytes of the whole error line
char msg[64];
snprintf(msg, sizeof(msg), "out of fds for %lds, ulimit -n %ju", (long)sec_stall_seconds, (uintmax_t)rl.rlim_cur);
td_verror(td, err, msg);
return true;
}
static int sec_setup(struct thread_data *td)
{
sec_options *o = (sec_options*)td->eo;
@@ -310,6 +351,7 @@ static int sec_setup(struct thread_data *td)
if (o->image)
{
bsd->watch = NULL;
time_t start = sec_now();
vitastor_c_watch_inode(bsd->cli, o->image, watch_callback, bsd);
if (!bsd->epoll_based)
{
@@ -318,6 +360,8 @@ static int sec_setup(struct thread_data *td)
vitastor_c_uring_handle_events(bsd->cli);
if (bsd->watch)
break;
if (sec_out_of_fds(td, start))
return 1;
vitastor_c_uring_wait_events(bsd->cli);
}
}
@@ -327,6 +371,8 @@ static int sec_setup(struct thread_data *td)
{
if (bsd->watch)
break;
if (sec_out_of_fds(td, start))
return 1;
vitastor_c_epoll_handle_events(bsd->cli, 1000);
}
}
@@ -374,6 +420,7 @@ static void io_callback(void *opaque, long retval)
io->error = retval < 0 ? -retval : 0;
sec_data *bsd = (sec_data*)io->engine_data;
bsd->inflight--;
bsd->last_progress = sec_now();
bsd->completed.push_back(io);
if (bsd->trace)
{
@@ -402,7 +449,8 @@ static enum fio_q_status sec_queue(struct thread_data *td, struct io_u *io)
io->engine_data = bsd;
io->error = 0;
bsd->inflight++;
if (!bsd->inflight++)
bsd->last_progress = sec_now();
uint64_t inode = opt->image ? vitastor_c_inode_get_num(bsd->watch) : opt->inode;
assert(io->xfer_buflen < (size_t)-1);
@@ -424,6 +472,7 @@ static enum fio_q_status sec_queue(struct thread_data *td, struct io_u *io)
{
fprintf(stderr, "Error writing mirror file: %s\n", strerror(errno));
io->error = errno;
bsd->inflight--;
return FIO_Q_COMPLETED;
}
if (r > 0)
@@ -435,6 +484,7 @@ static enum fio_q_status sec_queue(struct thread_data *td, struct io_u *io)
if (opt->image && vitastor_c_inode_get_readonly(bsd->watch))
{
io->error = EROFS;
bsd->inflight--;
return FIO_Q_COMPLETED;
}
iov = { .iov_base = io->xfer_buf, .iov_len = (size_t)io->xfer_buflen };
@@ -447,6 +497,7 @@ static enum fio_q_status sec_queue(struct thread_data *td, struct io_u *io)
break;
default:
io->error = EINVAL;
bsd->inflight--;
return FIO_Q_COMPLETED;
}
@@ -479,6 +530,8 @@ static int sec_getevents(struct thread_data *td, unsigned int min, unsigned int
vitastor_c_uring_handle_events(bsd->cli);
if (bsd->completed.size() >= min)
break;
if (bsd->inflight && sec_out_of_fds(td, bsd->last_progress))
return -td->error;
vitastor_c_uring_wait_events(bsd->cli);
}
}
@@ -488,6 +541,8 @@ static int sec_getevents(struct thread_data *td, unsigned int min, unsigned int
{
if (bsd->completed.size() >= min)
break;
if (bsd->inflight && sec_out_of_fds(td, bsd->last_progress))
return -td->error;
vitastor_c_epoll_handle_events(bsd->cli, 1000);
}
}