Skip to content

Commit a9b59ee

Browse files
committed
input_chunk: return errors as negative values with errors based on errno.
Not all errors will be the same es the errno values and this code does not set the actuall errno variable. Signed-off-by: Phillip Adair Stewart Whelan <[email protected]>
1 parent d6c4b3d commit a9b59ee

File tree

1 file changed

+77
-12
lines changed

1 file changed

+77
-12
lines changed

src/flb_input_chunk.c

+77-12
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,41 @@ static int input_chunk_write_header(struct cio_chunk *chunk, int event_type,
837837
return 0;
838838
}
839839

840-
struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, int event_type,
841-
const char *tag, int tag_len)
840+
static inline void errnum_set(int *errnum, int error)
841+
{
842+
if (errnum) {
843+
*errnum = error;
844+
}
845+
}
846+
847+
static inline void errnum_set_from_errno(int *errnum)
848+
{
849+
if (errno) {
850+
errnum_set(errnum, errno);
851+
}
852+
}
853+
854+
static inline void errnum_set_cio(int *errnum, int cio_err)
855+
{
856+
switch (cio_err) {
857+
case CIO_OK:
858+
errnum_set(errnum, 0);
859+
break;
860+
case CIO_CORRUPTED:
861+
errnum_set(errnum, EIO);
862+
break;
863+
case CIO_RETRY:
864+
errnum_set(errnum, EAGAIN);
865+
break;
866+
default:
867+
case CIO_ERROR:
868+
errnum_set(errnum, EINVAL);
869+
break;
870+
}
871+
}
872+
873+
static struct flb_input_chunk *input_chunk_create(struct flb_input_instance *in, int event_type,
874+
const char *tag, int tag_len, int *errnum)
842875
{
843876
int ret;
844877
int err;
@@ -860,6 +893,7 @@ struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, in
860893
if (!chunk) {
861894
flb_error("[input chunk] could not create chunk file: %s:%s",
862895
storage->stream->name, name);
896+
errnum_set_cio(errnum, err);
863897
return NULL;
864898
}
865899
/*
@@ -870,6 +904,7 @@ struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, in
870904
if (ret == CIO_FALSE) {
871905
ret = cio_chunk_up_force(chunk);
872906
if (ret == -1) {
907+
errnum_set(errnum, EIO);
873908
cio_chunk_close(chunk, CIO_TRUE);
874909
return NULL;
875910
}
@@ -879,6 +914,7 @@ struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, in
879914
/* Write chunk header */
880915
ret = input_chunk_write_header(chunk, event_type, (char *) tag, tag_len);
881916
if (ret == -1) {
917+
errnum_set(errnum, EIO);
882918
cio_chunk_close(chunk, CIO_TRUE);
883919
return NULL;
884920
}
@@ -887,6 +923,7 @@ struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, in
887923
ic = flb_calloc(1, sizeof(struct flb_input_chunk));
888924
if (!ic) {
889925
flb_errno();
926+
errnum_set_from_errno(errnum);
890927
cio_chunk_close(chunk, CIO_TRUE);
891928
return NULL;
892929
}
@@ -938,6 +975,12 @@ struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, in
938975
return ic;
939976
}
940977

978+
struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in, int event_type,
979+
const char *tag, int tag_len)
980+
{
981+
return input_chunk_create(in, event_type, tag, tag_len, NULL);
982+
}
983+
941984
int flb_input_chunk_destroy_corrupted(struct flb_input_chunk *ic,
942985
const char *tag_buf, int tag_len,
943986
int del)
@@ -1109,7 +1152,8 @@ int flb_input_chunk_destroy(struct flb_input_chunk *ic, int del)
11091152
static struct flb_input_chunk *input_chunk_get(struct flb_input_instance *in,
11101153
int event_type,
11111154
const char *tag, int tag_len,
1112-
size_t chunk_size, int *set_down)
1155+
size_t chunk_size, int *set_down,
1156+
int *errnum)
11131157
{
11141158
int id = -1;
11151159
int ret;
@@ -1174,7 +1218,7 @@ static struct flb_input_chunk *input_chunk_get(struct flb_input_instance *in,
11741218

11751219
/* No chunk was found, we need to create a new one */
11761220
if (!ic) {
1177-
ic = flb_input_chunk_create(in, event_type, (char *) tag, tag_len);
1221+
ic = input_chunk_create(in, event_type, (char *) tag, tag_len, errnum);
11781222
new_chunk = FLB_TRUE;
11791223
if (!ic) {
11801224
return NULL;
@@ -1198,6 +1242,8 @@ static struct flb_input_chunk *input_chunk_get(struct flb_input_instance *in,
11981242
if (new_chunk || flb_routes_mask_is_empty(ic->routes_mask) == FLB_TRUE) {
11991243
flb_input_chunk_destroy(ic, FLB_TRUE);
12001244
}
1245+
/* Set the error no ENOSPC so the caller knows we have hit a storage limit. */
1246+
errnum_set(errnum, ENOSPC);
12011247
return NULL;
12021248
}
12031249

@@ -1466,6 +1512,7 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
14661512
const void *buf, size_t buf_size)
14671513
{
14681514
int ret, total_records_start;
1515+
int err = 0;
14691516
int set_down = FLB_FALSE;
14701517
int min;
14711518
int new_chunk = FLB_FALSE;
@@ -1513,7 +1560,7 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
15131560

15141561
if (ret != 0) {
15151562
/* we could not allocate the required space, just return */
1516-
return -1;
1563+
return -ENOMEM;
15171564
}
15181565
}
15191566
}
@@ -1522,7 +1569,7 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
15221569
if (flb_input_buf_paused(in) == FLB_TRUE) {
15231570
flb_debug("[input chunk] %s is paused, cannot append records",
15241571
flb_input_name(in));
1525-
return -1;
1572+
return -EAGAIN;
15261573
}
15271574

15281575
if (buf_size == 0) {
@@ -1549,10 +1596,17 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
15491596
* Get a target input chunk, can be one with remaining space available
15501597
* or a new one.
15511598
*/
1552-
ic = input_chunk_get(in, event_type, tag, tag_len, buf_size, &set_down);
1599+
ic = input_chunk_get(in, event_type, tag, tag_len, buf_size, &set_down, &err);
15531600
if (!ic) {
15541601
flb_error("[input chunk] no available chunk");
1555-
return -1;
1602+
if (err != 0) {
1603+
return -err;
1604+
}
1605+
/* fallback on returning errno if it is set. */
1606+
else if (errno != 0) {
1607+
return -errno;
1608+
}
1609+
return -EIO;
15561610
}
15571611

15581612
/* newly created chunk */
@@ -1564,9 +1618,17 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
15641618
ret = flb_input_chunk_is_up(ic);
15651619
if (ret == FLB_FALSE) {
15661620
ret = cio_chunk_up_force(ic->chunk);
1567-
if (ret == -1) {
1621+
if (ret <= CIO_ERROR) {
15681622
flb_error("[input chunk] cannot retrieve temporary chunk");
1569-
return -1;
1623+
switch (ret) {
1624+
case CIO_CORRUPTED:
1625+
return -EIO;
1626+
case CIO_RETRY:
1627+
return -EAGAIN;
1628+
case CIO_ERROR:
1629+
return -ENOMEM;
1630+
}
1631+
return -EINVAL;
15701632
}
15711633
set_down = FLB_TRUE;
15721634
}
@@ -1638,6 +1700,7 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
16381700
ret = flb_input_chunk_write(ic,
16391701
final_data_buffer,
16401702
final_data_size);
1703+
err = errno;
16411704
}
16421705
else {
16431706
ret = 0;
@@ -1661,8 +1724,10 @@ static int input_chunk_append_raw(struct flb_input_instance *in,
16611724
flb_error("[input chunk] error writing data from %s instance",
16621725
flb_input_name(in));
16631726
cio_chunk_tx_rollback(ic->chunk);
1664-
1665-
return -1;
1727+
if (err) {
1728+
return -err;
1729+
}
1730+
return -EIO;
16661731
}
16671732

16681733
/* get the chunks content size */

0 commit comments

Comments
 (0)