Skip to content

Commit f3e0257

Browse files
committed
out_gcs: Addressed review comments
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 018a263 commit f3e0257

1 file changed

Lines changed: 47 additions & 31 deletions

File tree

plugins/out_gcs/gcs.c

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,6 @@ static struct flb_aws_header *get_content_encoding_header(int compression_type)
5353
}
5454
}
5555

56-
static struct flb_aws_header content_type_header = {
57-
.key = "Content-Type",
58-
.key_len = 12,
59-
.val = "",
60-
.val_len = 0,
61-
};
62-
63-
static struct flb_aws_header canned_acl_header = {
64-
.key = "x-goog-acl",
65-
.key_len = 10,
66-
.val = "",
67-
.val_len = 0,
68-
};
69-
70-
static struct flb_aws_header content_md5_header = {
71-
.key = "Content-MD5",
72-
.key_len = 11,
73-
.val = "",
74-
.val_len = 0,
75-
};
76-
77-
static struct flb_aws_header storage_class_header = {
78-
.key = "x-goog-storage-class",
79-
.key_len = 20,
80-
.val = "",
81-
.val_len = 0,
82-
};
83-
8456
static inline int key_cmp(char *str, int len, char *cmp) {
8557
if (strlen(cmp) != len) {
8658
return -1;
@@ -590,7 +562,10 @@ static int add_to_queue(struct flb_gcs *ctx, struct gcs_file *chunk,
590562

591563
entry->upload_file = chunk;
592564
entry->tag_len = tag_len;
593-
entry->upload_time = time(NULL) + ctx->upload_timeout;
565+
entry->upload_time = chunk->create_time + ctx->upload_timeout;
566+
if (entry->upload_time < time(NULL)) {
567+
entry->upload_time = time(NULL);
568+
}
594569
mk_list_add(&entry->_head, &ctx->upload_queue);
595570
return 0;
596571
}
@@ -660,6 +635,22 @@ static int gcs_upload_object(struct flb_gcs *ctx,
660635
struct flb_connection *u_conn;
661636
struct flb_http_client *c;
662637
struct flb_aws_header *encoding_header;
638+
struct flb_aws_header content_type_header = {
639+
.key = "Content-Type",
640+
.key_len = 12
641+
};
642+
struct flb_aws_header canned_acl_header = {
643+
.key = "x-goog-acl",
644+
.key_len = 10
645+
};
646+
struct flb_aws_header content_md5_header = {
647+
.key = "Content-MD5",
648+
.key_len = 11
649+
};
650+
struct flb_aws_header storage_class_header = {
651+
.key = "x-goog-storage-class",
652+
.key_len = 20
653+
};
663654
char final_body_md5[25];
664655

665656
if (gcs_under_test_mode() == FLB_TRUE) {
@@ -719,6 +710,13 @@ static int gcs_upload_object(struct flb_gcs *ctx,
719710
}
720711

721712
ret = flb_http_do(c, &bytes);
713+
if (ret == 0 &&
714+
(c->resp.status < 200 || c->resp.status >= 300)) {
715+
flb_plg_error(ctx->ins,
716+
"gcs upload failed with status=%i",
717+
c->resp.status);
718+
ret = -1;
719+
}
722720
flb_http_client_destroy(c);
723721
flb_upstream_conn_release(u_conn);
724722

@@ -754,9 +752,16 @@ static int upload_data(struct flb_gcs *ctx,
754752
return -1;
755753
}
756754

755+
if (ctx->key_fmt_has_seq_index) {
756+
ctx->seq_index++;
757+
}
758+
757759
gcs_key = flb_get_s3_key(ctx->gcs_key_format, time(NULL),
758-
entry->tag, ctx->tag_delimiters, 0);
760+
entry->tag, ctx->tag_delimiters, ctx->seq_index);
759761
if (!gcs_key) {
762+
if (ctx->key_fmt_has_seq_index && ctx->seq_index > 0) {
763+
ctx->seq_index--;
764+
}
760765
flb_sds_destroy(auth);
761766
return -1;
762767
}
@@ -771,12 +776,18 @@ static int upload_data(struct flb_gcs *ctx,
771776
gcs_key_final = flb_sds_create_size(flb_sds_len(gcs_key) + 16);
772777
if (!gcs_key_final) {
773778
flb_errno();
779+
if (ctx->key_fmt_has_seq_index && ctx->seq_index > 0) {
780+
ctx->seq_index--;
781+
}
774782
flb_sds_destroy(auth);
775783
flb_sds_destroy(gcs_key);
776784
return -1;
777785
}
778786
flb_sds_printf(&gcs_key_final, "%s-object%s", gcs_key, random_hex);
779787
if (!gcs_key_final) {
788+
if (ctx->key_fmt_has_seq_index && ctx->seq_index > 0) {
789+
ctx->seq_index--;
790+
}
780791
flb_sds_destroy(auth);
781792
flb_sds_destroy(gcs_key);
782793
return -1;
@@ -786,7 +797,6 @@ static int upload_data(struct flb_gcs *ctx,
786797
}
787798

788799
if (ctx->key_fmt_has_seq_index) {
789-
ctx->seq_index++;
790800
ret_seq = write_seq_index(ctx->seq_index_file, ctx->seq_index);
791801
if (ret_seq == -1) {
792802
flb_sds_destroy(auth);
@@ -1082,6 +1092,9 @@ static int cb_gcs_init(struct flb_output_instance *ins, struct flb_config *confi
10821092
}
10831093

10841094
ctx->o = flb_oauth2_create(config, FLB_GCS_AUTH_URL, FLB_GCS_TOKEN_REFRESH);
1095+
if (!ctx->o) {
1096+
goto error;
1097+
}
10851098
if (pthread_mutex_init(&ctx->token_mutex, NULL) == 0) {
10861099
ctx->token_mutex_initialized = FLB_TRUE;
10871100
}
@@ -1090,6 +1103,9 @@ static int cb_gcs_init(struct flb_output_instance *ins, struct flb_config *confi
10901103
}
10911104
ctx->u = flb_upstream_create(config, FLB_GCS_DEFAULT_HOST, FLB_GCS_DEFAULT_PORT,
10921105
FLB_IO_TLS, ins->tls);
1106+
if (!ctx->u) {
1107+
goto error;
1108+
}
10931109
ctx->out_format = FLB_PACK_JSON_FORMAT_LINES;
10941110
ctx->json_date_format = FLB_PACK_JSON_DATE_DOUBLE;
10951111
if (ctx->content_type == NULL) {

0 commit comments

Comments
 (0)