rotate pending working on the relay
[deliverable/lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
index 6f277df0ec5637d95f6f95defb31577a4b521faa..d2552cb63f451a8d7416ca4d0a696e0bc4904244 100644 (file)
@@ -183,7 +183,7 @@ static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
                }
                goto error;
        }
-
+       consumer_stream_copy_ro_channel_values(stream, channel);
        stream->chan = channel;
 
 error:
@@ -567,7 +567,8 @@ static int send_sessiond_channel(int sock,
                        health_code_update();
 
                        /* Try to send the stream to the relayd if one is available. */
-                       ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
+                       ret = consumer_send_relayd_stream(stream, stream->chan->pathname,
+                                       LTTNG_DOMAIN_UST);
                        if (ret < 0) {
                                /*
                                 * Flag that the relayd was the problem here probably due to a
@@ -905,7 +906,7 @@ static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
        /* Send metadata stream to relayd if needed. */
        if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) {
                ret = consumer_send_relayd_stream(metadata->metadata_stream,
-                               metadata->pathname);
+                               metadata->pathname, LTTNG_DOMAIN_UST);
                if (ret < 0) {
                        ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
                        goto error;
@@ -1004,7 +1005,8 @@ static int snapshot_metadata(uint64_t key, char *path, uint64_t relayd_id,
 
        if (relayd_id != (uint64_t) -1ULL) {
                metadata_stream->net_seq_idx = relayd_id;
-               ret = consumer_send_relayd_stream(metadata_stream, path);
+               ret = consumer_send_relayd_stream(metadata_stream, path,
+                               LTTNG_DOMAIN_UST);
                if (ret < 0) {
                        goto error_stream;
                }
@@ -1083,7 +1085,8 @@ static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id,
                stream->net_seq_idx = relayd_id;
 
                if (use_relayd) {
-                       ret = consumer_send_relayd_stream(stream, path);
+                       ret = consumer_send_relayd_stream(stream, path,
+                                       LTTNG_DOMAIN_UST);
                        if (ret < 0) {
                                goto error_unlock;
                        }
@@ -1101,12 +1104,6 @@ static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id,
                        DBG("UST consumer snapshot stream %s/%s (%" PRIu64 ")", path,
                                        stream->name, stream->key);
                }
-               if (relayd_id != -1ULL) {
-                       ret = consumer_send_relayd_streams_sent(relayd_id);
-                       if (ret < 0) {
-                               goto error_unlock;
-                       }
-               }
 
                /*
                 * If tracing is active, we want to perform a "full" buffer flush.
@@ -1332,7 +1329,7 @@ int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
        case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
        {
                /* Session daemon status message are handled in the following call. */
-               ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
+               consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
                                msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
                                &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
                                msg.u.relayd_sock.relayd_session_id);
@@ -1893,6 +1890,131 @@ int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
                }
                goto end_msg_sessiond;
        }
+       case LTTNG_CONSUMER_SET_CHANNEL_ROTATE_PIPE:
+       {
+               int channel_rotate_pipe;
+               int flags;
+
+               ret_code = LTTCOMM_CONSUMERD_SUCCESS;
+               /* Successfully received the command's type. */
+               ret = consumer_send_status_msg(sock, ret_code);
+               if (ret < 0) {
+                       goto error_fatal;
+               }
+
+               ret = lttcomm_recv_fds_unix_sock(sock, &channel_rotate_pipe,
+                               1);
+               if (ret != sizeof(channel_rotate_pipe)) {
+                       ERR("Failed to receive channel rotate pipe");
+                       goto error_fatal;
+               }
+
+               DBG("Received channel rotate pipe (%d)", channel_rotate_pipe);
+               ctx->channel_rotate_pipe = channel_rotate_pipe;
+               /* Set the pipe as non-blocking. */
+               ret = fcntl(channel_rotate_pipe, F_GETFL, 0);
+               if (ret == -1) {
+                       PERROR("fcntl get flags of the channel rotate pipe");
+                       goto error_fatal;
+               }
+               flags = ret;
+
+               ret = fcntl(channel_rotate_pipe, F_SETFL,
+                               flags | O_NONBLOCK);
+               if (ret == -1) {
+                       PERROR("fcntl set O_NONBLOCK flag of the channel rotate pipe");
+                       goto error_fatal;
+               }
+               DBG("Channel rotate pipe set as non-blocking");
+               ret_code = LTTCOMM_CONSUMERD_SUCCESS;
+               ret = consumer_send_status_msg(sock, ret_code);
+               if (ret < 0) {
+                       goto error_fatal;
+               }
+               break;
+       }
+       case LTTNG_CONSUMER_ROTATE_CHANNEL:
+       {
+               ret = lttng_consumer_rotate_channel(msg.u.rotate_channel.key,
+                               msg.u.rotate_channel.pathname,
+                               msg.u.rotate_channel.relayd_id,
+                               msg.u.rotate_channel.metadata,
+                               msg.u.rotate_channel.new_chunk_id,
+                               ctx);
+               if (ret < 0) {
+                       ERR("Rotate channel failed");
+                       ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
+               }
+
+               health_code_update();
+
+               ret = consumer_send_status_msg(sock, ret_code);
+               if (ret < 0) {
+                       /* Somehow, the session daemon is not responding anymore. */
+                       goto end_nosignal;
+               }
+
+               /*
+                * Rotate the streams that are ready right now.
+                * FIXME: this is a second consecutive iteration over the
+                * streams in a channel, there is probably a better way to
+                * handle this, but it needs to be after the
+                * consumer_send_status_msg() call.
+                */
+               ret = lttng_consumer_rotate_ready_streams(
+                               msg.u.rotate_channel.key, ctx);
+               if (ret < 0) {
+                       ERR("Rotate channel failed");
+                       ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
+               }
+               break;
+       }
+       case LTTNG_CONSUMER_ROTATE_RENAME:
+       {
+               DBG("Consumer rename session %" PRIu64 " after rotation",
+                               msg.u.rotate_rename.session_id);
+               ret = lttng_consumer_rotate_rename(msg.u.rotate_rename.current_path,
+                               msg.u.rotate_rename.new_path,
+                               msg.u.rotate_rename.uid,
+                               msg.u.rotate_rename.gid,
+                               msg.u.rotate_rename.relayd_id);
+               if (ret < 0) {
+                       ERR("Rotate rename failed");
+                       ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
+               }
+
+               health_code_update();
+
+               ret = consumer_send_status_msg(sock, ret_code);
+               if (ret < 0) {
+                       /* Somehow, the session daemon is not responding anymore. */
+                       goto end_nosignal;
+               }
+
+       }
+       case LTTNG_CONSUMER_ROTATE_PENDING_RELAY:
+       {
+               DBG("Consumer rotate pending on relay for session %" PRIu64,
+                               msg.u.rotate_pending_relay.session_id);
+               ret = lttng_consumer_rotate_pending_relay(
+                               msg.u.rotate_pending_relay.session_id,
+                               msg.u.rotate_pending_relay.relayd_id,
+                               msg.u.rotate_pending_relay.chunk_id);
+               if (ret < 0) {
+                       ERR("Rotate pending relay failed");
+                       ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
+               }
+
+               health_code_update();
+               ret_code = ret;
+
+               ret = consumer_send_status_msg(sock, ret_code);
+               if (ret < 0) {
+                       /* Somehow, the session daemon is not responding anymore. */
+                       goto end_nosignal;
+               }
+
+       }
        default:
                break;
        }
@@ -1973,6 +2095,15 @@ void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream *stream)
        return ustctl_get_mmap_base(stream->ustream);
 }
 
+void lttng_ustctl_flush_buffer(struct lttng_consumer_stream *stream,
+               int producer_active)
+{
+       assert(stream);
+       assert(stream->ustream);
+
+       ustctl_flush_buffer(stream->ustream, producer_active);
+}
+
 /*
  * Take a snapshot for a specific stream.
  *
@@ -2476,7 +2607,7 @@ int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
                struct lttng_consumer_local_data *ctx)
 {
        unsigned long len, subbuf_size, padding;
-       int err, write_index = 1;
+       int err, write_index = 1, rotation_ret, rotate_ready;
        long ret = 0;
        struct ustctl_consumer_stream *ustream;
        struct ctf_packet_index index;
@@ -2508,7 +2639,7 @@ int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
                readlen = lttng_read(stream->wait_fd, &dummy, 1);
                if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
                        ret = readlen;
-                       goto end;
+                       goto error;
                }
        }
 
@@ -2523,7 +2654,7 @@ retry:
                if (stream->metadata_flag) {
                        ret = commit_one_metadata_packet(stream);
                        if (ret <= 0) {
-                               goto end;
+                               goto error;
                        }
                        ustctl_flush_buffer(stream->ustream, 1);
                        goto retry;
@@ -2538,7 +2669,7 @@ retry:
                 */
                DBG("Reserving sub buffer failed (everything is normal, "
                                "it is due to concurrency) [ret: %d]", err);
-               goto end;
+               goto error;
        }
        assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
 
@@ -2548,7 +2679,7 @@ retry:
                if (ret < 0) {
                        err = ustctl_put_subbuf(ustream);
                        assert(err == 0);
-                       goto end;
+                       goto error;
                }
 
                /* Update the stream's sequence and discarded events count. */
@@ -2557,7 +2688,7 @@ retry:
                        PERROR("kernctl_get_events_discarded");
                        err = ustctl_put_subbuf(ustream);
                        assert(err == 0);
-                       goto end;
+                       goto error;
                }
        } else {
                write_index = 0;
@@ -2575,6 +2706,17 @@ retry:
        assert(len >= subbuf_size);
 
        padding = len - subbuf_size;
+
+       rotate_ready = lttng_consumer_stream_is_rotate_ready(stream, len);
+       if (rotate_ready < 0) {
+               ERR("Failed to check if stream is ready for rotation");
+               ret = -1;
+               err = ustctl_put_subbuf(ustream);
+               assert(err == 0);
+               goto error;
+       }
+       stream->rotate_ready = rotate_ready;
+
        /* write the subbuffer to the tracefile */
        ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding, &index);
        /*
@@ -2606,13 +2748,13 @@ retry:
        if (!stream->metadata_flag) {
                ret = notify_if_more_data(stream, ctx);
                if (ret < 0) {
-                       goto end;
+                       goto error;
                }
        }
 
        /* Write index if needed. */
        if (!write_index) {
-               goto end;
+               goto rotate;
        }
 
        if (stream->chan->live_timer_interval && !stream->metadata_flag) {
@@ -2637,17 +2779,26 @@ retry:
                }
 
                if (err < 0) {
-                       goto end;
+                       goto error;
                }
        }
 
        assert(!stream->metadata_flag);
        err = consumer_stream_write_index(stream, &index);
        if (err < 0) {
-               goto end;
+               goto error;
        }
 
-end:
+rotate:
+       if (stream->rotate_ready) {
+               rotation_ret = lttng_consumer_rotate_stream(ctx, stream);
+               if (rotation_ret < 0) {
+                       ret = -1;
+                       ERR("Stream rotation error");
+                       goto error;
+               }
+       }
+error:
        return ret;
 }
 
This page took 0.03157 seconds and 5 git commands to generate.