move the rename code at the end of the rotation
authorJulien Desfossez <jdesfossez@efficios.com>
Tue, 22 Aug 2017 18:54:23 +0000 (14:54 -0400)
committerJulien Desfossez <jdesfossez@efficios.com>
Wed, 6 Sep 2017 17:59:58 +0000 (13:59 -0400)
Signed-off-by: Julien Desfossez <jdesfossez@efficios.com>
src/bin/lttng-relayd/main.c
src/bin/lttng-relayd/session.c
src/bin/lttng-sessiond/cmd.c
src/bin/lttng-sessiond/consumer.c
src/bin/lttng-sessiond/rotate.c
src/bin/lttng-sessiond/rotation-thread.c
src/bin/lttng-sessiond/session.h
src/common/consumer/consumer.c

index 0eb8e28251f2451ab229dc41cf0e62726fddc7c5..dca753ba218ed02f5254ad2a74b2b2ad5872277f 100644 (file)
@@ -1102,6 +1102,7 @@ static int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
                goto send_reply;
        }
 
+       fprintf(stderr, "name: %s\n", session_name);
        session = session_create(session_name, hostname, live_timer,
                        snapshot, conn->major, conn->minor);
        if (!session) {
index 3ea8e50d6f9aaf60d64789069636c9ea13736e50..69aba7b4c3591d6a3779b99212c42994c83f0ae3 100644 (file)
@@ -54,6 +54,7 @@ struct relay_session *session_create(const char *session_name,
                        sizeof(session->hostname))) {
                goto error;
        }
+       fprintf(stderr, "hostname; %s\n", hostname);
        session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
        if (!session->ctf_traces_ht) {
                goto error;
index 1ec3891b8d0244c3f66a1fb997146dd8f34bfa88..ed112489b5e5ee53b71cd196574299461fa57e15 100644 (file)
@@ -2402,8 +2402,8 @@ int cmd_start_trace(struct ltt_session *session)
         * an eventual session rotation call.
         */
        if (!session->has_been_started) {
-               session->session_start_ts = time(NULL);
-               if (session->session_start_ts == (time_t) -1) {
+               session->current_chunk_start_ts = time(NULL);
+               if (session->current_chunk_start_ts == (time_t) -1) {
                        PERROR("Get start time");
                        ret = LTTNG_ERR_FATAL;
                        goto error;
@@ -4132,51 +4132,6 @@ int cmd_set_session_shm_path(struct ltt_session *session,
        return 0;
 }
 
-static
-int rename_first_chunk(struct ltt_session *session,
-               struct consumer_output *consumer, char *datetime)
-{
-       int ret;
-       char *tmppath = NULL, *tmppath2 = NULL;
-
-       tmppath = zmalloc(PATH_MAX * sizeof(char));
-       if (!tmppath) {
-               ret = -LTTNG_ERR_NOMEM;
-               goto error;
-       }
-       tmppath2 = zmalloc(PATH_MAX * sizeof(char));
-       if (!tmppath2) {
-               ret = -LTTNG_ERR_NOMEM;
-               goto error;
-       }
-
-       /* Current domain path: <session>/kernel */
-       snprintf(tmppath, PATH_MAX, "%s/%s",
-                       consumer->dst.session_root_path, consumer->subdir);
-       /* New domain path: <session>/<start-date>-/kernel */
-       snprintf(tmppath2, PATH_MAX, "%s/%s-/%s",
-                       consumer->dst.session_root_path, datetime,
-                       consumer->subdir);
-       /*
-        * Move the per-domain folder inside the first rotation
-        * folder.
-        */
-       ret = session_rename_chunk(session, tmppath, tmppath2, 1);
-       if (ret < 0) {
-               PERROR("Rename first trace directory");
-               ret = -LTTNG_ERR_ROTATE_NO_DATA;
-               goto error;
-       }
-
-       ret = 0;
-
-error:
-       free(tmppath);
-       free(tmppath2);
-
-       return ret;
-}
-
 /*
  * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
  *
@@ -4213,52 +4168,18 @@ int cmd_rotate_session(struct ltt_session *session,
 
        /* Special case for the first rotation. */
        if (session->rotate_count == 0) {
-               timeinfo = localtime(&session->session_start_ts);
-               strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
                /* Either one of the two sessions is enough to get the root path. */
                if (session->kernel_session) {
                        snprintf(session->rotation_chunk.current_rotate_path,
-                                       PATH_MAX, "%s/%s-",
-                                       session->kernel_session->consumer->dst.session_root_path,
-                                       datetime);
+                                       PATH_MAX, "%s",
+                                       session->kernel_session->consumer->dst.session_root_path);
                } else if (session->ust_session) {
                        snprintf(session->rotation_chunk.current_rotate_path,
-                                       PATH_MAX, "%s/%s-",
-                                       session->ust_session->consumer->dst.session_root_path,
-                                       datetime);
+                                       PATH_MAX, "%s",
+                                       session->ust_session->consumer->dst.session_root_path);
                } else {
                        assert(0);
                }
-
-               /*
-                * Create the first rotation folder to move the existing
-                * kernel/ust folders into.
-                */
-               ret = run_as_mkdir_recursive(session->rotation_chunk.current_rotate_path,
-                               S_IRWXU | S_IRWXG, session->uid, session->gid);
-               if (ret < 0) {
-                       if (errno != EEXIST) {
-                               ERR("Trace directory creation error");
-                               ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
-                               goto error;
-                       }
-               }
-               if (session->kernel_session) {
-                       ret = rename_first_chunk(session,
-                                       session->kernel_session->consumer,
-                                       datetime);
-                       if (ret < 0) {
-                               goto error;
-                       }
-               }
-               if (session->ust_session) {
-                       ret = rename_first_chunk(session,
-                                       session->ust_session->consumer,
-                                       datetime);
-                       if (ret < 0) {
-                               goto error;
-                       }
-               }
        } else {
                /*
                 * The currently active tracing path is now the folder we
@@ -4280,6 +4201,8 @@ int cmd_rotate_session(struct ltt_session *session,
                ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
                goto error;
        }
+       session->last_chunk_start_ts = session->current_chunk_start_ts;
+       session->current_chunk_start_ts = now;
 
        timeinfo = localtime(&now);
        strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
index 13d96578d78681d79b34ec6aa63726e2d85b72cb..ff5a2944e0305ab1dffeaa4078788b07d3005852 100644 (file)
@@ -1608,22 +1608,13 @@ int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
        msg.u.rotate_channel.key = key;
        msg.u.rotate_channel.metadata = metadata;
 
+
        if (output->type == CONSUMER_DST_NET) {
-               ERR("TODO");
-               ret = -1;
-               goto error;
-               /*
-               msg.u.rotate_channel.relayd_id = output->consumer->net_seq_index;
-               ret = snprintf(msg.u.rotate_channel.pathname,
-                               sizeof(msg.u.rotate_channel.pathname),
-                               "%s/%s-%s-%" PRIu64 "%s", output->consumer->subdir,
-                               output->name, output->datetime, output->nb_rotate,
-                               session_path);
-               if (ret < 0) {
-                       ret = -LTTNG_ERR_NOMEM;
-                       goto error;
-               }
-               */
+               fprintf(stderr, "SUBDIR: %s\n", output->subdir);
+               msg.u.rotate_channel.relayd_id = output->net_seq_index;
+               snprintf(msg.u.rotate_channel.pathname, PATH_MAX, "%s/%s/%s",
+                               output->subdir,
+                               output->chunk_path, tmp);
        } else {
                msg.u.rotate_channel.relayd_id = (uint64_t) -1ULL;
                snprintf(msg.u.rotate_channel.pathname, PATH_MAX, "%s/%s/%s",
@@ -1632,16 +1623,6 @@ int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
                fprintf(stderr, "rotate to %s\n",
                                msg.u.rotate_channel.pathname);
 
-               /* Create directory. Ignore if exist. */
-               /* FIXME: not sure this is useful */
-               ret = run_as_mkdir_recursive(msg.u.rotate_channel.pathname,
-                               S_IRWXU | S_IRWXG, uid, gid);
-               if (ret < 0) {
-                       if (errno != EEXIST) {
-                               ERR("Trace directory creation error");
-                               goto error;
-                       }
-               }
        }
 
        health_code_update();
@@ -1675,6 +1656,8 @@ int consumer_rotate_rename(struct consumer_socket *socket, uint64_t session_id,
        msg.u.rotate_rename.gid = gid;
 
        if (output->type == CONSUMER_DST_NET) {
+               fprintf(stderr, "SUBDIR: %s\n", output->subdir);
+               fprintf(stderr, "SUBDIR: %s\n", output->dst.net.control.subdir);
                ERR("TODO");
                ret = -1;
                msg.u.rotate_rename.relayd_id = output->net_seq_index;
index 2a861bc7b968410bb4522ce065f9279760d3c78f..bcd0b828dcedc715712dbf06b22767ffef19fc2d 100644 (file)
@@ -139,37 +139,138 @@ end:
        return ret;
 }
 
+static
+int rename_first_chunk(struct ltt_session *session,
+               struct consumer_output *consumer, char *new_path)
+{
+       int ret;
+       char *tmppath = NULL, *tmppath2 = NULL;
+
+       tmppath = zmalloc(PATH_MAX * sizeof(char));
+       if (!tmppath) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto error;
+       }
+       tmppath2 = zmalloc(PATH_MAX * sizeof(char));
+       if (!tmppath2) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto error;
+       }
+
+       /* Current domain path: <session>/kernel */
+       snprintf(tmppath, PATH_MAX, "%s/%s",
+                       consumer->dst.session_root_path, consumer->subdir);
+       /* New domain path: <session>/<start-date>-<end-date>-<rotate-count>/kernel */
+       snprintf(tmppath2, PATH_MAX, "%s/%s",
+                       new_path, consumer->subdir);
+       /*
+        * Move the per-domain folder inside the first rotation
+        * folder.
+        */
+       ret = session_rename_chunk(session, tmppath, tmppath2, 1);
+       if (ret < 0) {
+               PERROR("Rename first trace directory");
+               ret = -LTTNG_ERR_ROTATE_NO_DATA;
+               goto error;
+       }
+
+       ret = 0;
+
+error:
+       free(tmppath);
+       free(tmppath2);
+
+       return ret;
+}
+
 int rename_complete_chunk(struct ltt_session *session, time_t ts)
 {
        struct tm *timeinfo;
        char datetime[16];
-       char *tmppath = NULL;
+       char *new_path = NULL;
        int ret;
 
+       /*
+        * TODO 2: on first rotate, the current_rotate_path is the session root
+        * path, so move the kernel/ and ust/ folders inside the
+        * "session->last_chunk_start_ts-now()"
+        */
+
        timeinfo = localtime(&ts);
        strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
 
-       tmppath = zmalloc(PATH_MAX * sizeof(char));
-       if (!tmppath) {
-               ERR("Alloc tmppath");
+       new_path = zmalloc(PATH_MAX * sizeof(char));
+       if (!new_path) {
+               ERR("Alloc new_path");
                ret = -1;
                goto end;
        }
 
-       snprintf(tmppath, PATH_MAX, "%s%s-%" PRIu64,
-                       session->rotation_chunk.current_rotate_path,
-                       datetime, session->rotate_count);
+       fprintf(stderr, "COUNT: %d\n", session->rotate_count);
 
-       fprintf(stderr, "rename %s to %s\n", session->rotation_chunk.current_rotate_path,
-                       tmppath);
+       if (session->rotate_count == 1) {
+               char start_time[16];
 
-       ret = session_rename_chunk(session,
-                       session->rotation_chunk.current_rotate_path,
-                       tmppath, 0);
-       if (ret) {
-               ERR("Session rename");
-               ret = -1;
-               goto end;
+               timeinfo = localtime(&session->last_chunk_start_ts);
+               strftime(start_time, sizeof(start_time), "%Y%m%d-%H%M%S", timeinfo);
+
+               /*
+                * On the first rotation, the current_rotate_path is the
+                * session_root_path, so we need to create the chunk folder
+                * and move the domain-specific folders inside it.
+                */
+               snprintf(new_path, PATH_MAX, "%s/%s-%s-%" PRIu64,
+                               session->rotation_chunk.current_rotate_path,
+                               start_time,
+                               datetime, session->rotate_count);
+
+               if (session->kernel_session) {
+                       fprintf(stderr, "rename %s/kernel to %s\n",
+                                       session->rotation_chunk.current_rotate_path,
+                                       new_path);
+                       ret = rename_first_chunk(session,
+                                       session->kernel_session->consumer,
+                                       new_path);
+                       if (ret) {
+                               ERR("Rename kernel session");
+                               ret = -1;
+                               goto end;
+                       }
+               }
+               if (session->ust_session) {
+                       fprintf(stderr, "rename %s/kernel to %s\n",
+                                       session->rotation_chunk.current_rotate_path,
+                                       new_path);
+                       ret = rename_first_chunk(session,
+                                       session->ust_session->consumer,
+                                       new_path);
+                       if (ret) {
+                               ERR("Rename ust session");
+                               ret = -1;
+                               goto end;
+                       }
+               }
+       } else {
+               /*
+                * After the first rotation, all the trace data is already in
+                * its own chunk folder, we just need to append the suffix.
+                */
+               snprintf(new_path, PATH_MAX, "%s%s-%" PRIu64,
+                               session->rotation_chunk.current_rotate_path,
+                               datetime, session->rotate_count);
+
+               fprintf(stderr, "rename %s to %s\n",
+                               session->rotation_chunk.current_rotate_path,
+                               new_path);
+
+               ret = session_rename_chunk(session,
+                               session->rotation_chunk.current_rotate_path,
+                               new_path, 0);
+               if (ret) {
+                       ERR("Session rename");
+                       ret = -1;
+                       goto end;
+               }
        }
 
        /*
@@ -178,11 +279,11 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts)
         * rotation is started.
         */
        snprintf(session->rotation_chunk.current_rotate_path, PATH_MAX,
-                       "%s", tmppath);
+                       "%s", new_path);
        session->rotate_pending = 0;
 
 end:
-       free(tmppath);
+       free(new_path);
        return ret;
 }
 
index ded83a7a93006dd5c511d5d1d5b6fe9a8d391fe1..e50903f691405b213a70bc04993a0521c4c723ea 100644 (file)
@@ -282,7 +282,8 @@ int handle_channel_rotation_pipe(int fd, uint32_t revents,
        if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
                ret = lttng_poll_del(&state->events, fd);
                if (ret) {
-                       ERR("[rotation-thread] Failed to remove consumer rotation pipe from poll set");
+                       ERR("[rotation-thread] Failed to remove consumer "
+                                       "rotation pipe from poll set");
                }
                goto end;
        }
index ebcf89df26219cd1c093c87ce0896e4a5b3efbcb..9e5c3151bb1c26a947e970301355effdd23db364 100644 (file)
@@ -66,8 +66,6 @@ struct ltt_session_chunk {
         * session rotation.
         */
        char active_tracing_path[PATH_MAX];
-       time_t rotate_start_time;
-       time_t rotate_end_time;
 };
 
 /*
@@ -148,10 +146,19 @@ struct ltt_session {
        unsigned int nr_chan_rotate_pending;
        struct ltt_session_chunk rotation_chunk;
        /*
-        * Store the timestamp when the session started for an eventual
-        * session rotation call.
+        * The timestamp of the beginning of the previous chunk. For the
+        * first chunk, this is the "lttng start" timestamp. For the
+        * subsequent ones, this copies the current_chunk_start_ts value when
+        * a new rotation starts. This value is used to set the name of a
+        * complete chunk directory, ex: "last_chunk_start_ts-now()".
         */
-       time_t session_start_ts;
+       time_t last_chunk_start_ts;
+       /*
+        * This is the timestamp when a new chunk starts. When a new rotation
+        * starts, we copy this value to last_chunk_start_ts and override it
+        * with the current timestamp.
+        */
+       time_t current_chunk_start_ts;
        time_t session_last_stop_ts;
        time_t last_begin_rotation_ts;
 };
index 54491b89f37d44aa4f47123ef5bb08f454061f2d..ef69d65a4892a29b40b82954b46d55935c06020c 100644 (file)
@@ -3961,37 +3961,46 @@ int lttng_consumer_rotate_channel(uint64_t key, char *path,
                        ht->hash_fct(&channel->key, lttng_ht_seed),
                        ht->match_fct, &channel->key, &iter.iter,
                        stream, node_channel_id.node) {
+               uint64_t consumed_pos;
+
                health_code_update();
 
                /*
                 * Lock stream because we are about to change its state.
                 */
                pthread_mutex_lock(&stream->lock);
+               ret = utils_mkdir_recursive(channel->pathname, S_IRWXU | S_IRWXG,
+                               stream->uid, stream->gid);
+               if (ret < 0) {
+                       if (errno != EEXIST) {
+                               ERR("Trace directory creation error");
+                               goto end_unlock;
+                       }
+               }
+
                memcpy(stream->channel_ro_pathname, channel->pathname, PATH_MAX);
                ret = lttng_consumer_sample_snapshot_positions(stream);
                if (ret < 0) {
                        ERR("Taking kernel snapshot positions");
                        goto end_unlock;
-               } else {
-                       uint64_t consumed_pos;
+               }
 
-                       ret = lttng_consumer_get_produced_snapshot(stream,
-                                       &stream->rotate_position);
-                       if (ret < 0) {
-                               ERR("Produced kernel snapshot position");
-                               goto end_unlock;
-                       }
-                       fprintf(stderr, "Stream %lu should rotate after %lu to %s\n",
-                                       stream->key, stream->rotate_position,
-                                       channel->pathname);
-                       lttng_consumer_get_consumed_snapshot(stream,
-                                       &consumed_pos);
-                       fprintf(stderr, "consumed %lu\n", consumed_pos);
-                       if (consumed_pos == stream->rotate_position) {
-                               stream->rotate_ready = 1;
-                               fprintf(stderr, "Stream %lu ready to rotate to %s\n",
-                                               stream->key, channel->pathname);
-                       }
+               ret = lttng_consumer_get_produced_snapshot(stream,
+                               &stream->rotate_position);
+               if (ret < 0) {
+                       ERR("Produced kernel snapshot position");
+                       goto end_unlock;
+               }
+               fprintf(stderr, "Stream %lu should rotate after %lu to %s\n",
+                               stream->key, stream->rotate_position,
+                               channel->pathname);
+               lttng_consumer_get_consumed_snapshot(stream,
+                               &consumed_pos);
+               fprintf(stderr, "consumed %lu\n", consumed_pos);
+               if (consumed_pos == stream->rotate_position) {
+                       stream->rotate_ready = 1;
+                       fprintf(stderr, "Stream %lu ready to rotate to %s\n",
+                                       stream->key, channel->pathname);
                }
                channel->nr_stream_rotate_pending++;
 
This page took 0.038292 seconds and 5 git commands to generate.