relayd comm: add base path to create session
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 18 Aug 2019 19:12:02 +0000 (15:12 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 5 Sep 2019 20:39:15 +0000 (16:39 -0400)
Add base path and session_name_contains_creation_time flag to
session creation 2.11 message.

Use it to ensure the "auto-<ts>" session names don't get
duplicate timestamps for their output paths.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
14 files changed:
src/bin/lttng-relayd/cmd-2-11.c
src/bin/lttng-relayd/cmd-2-11.h
src/bin/lttng-relayd/cmd-2-4.c
src/bin/lttng-relayd/main.c
src/bin/lttng-relayd/session.c
src/bin/lttng-relayd/session.h
src/bin/lttng-sessiond/cmd.c
src/bin/lttng-sessiond/consumer.c
src/bin/lttng-sessiond/consumer.h
src/bin/lttng-sessiond/session.c
src/bin/lttng-sessiond/session.h
src/common/relayd/relayd.c
src/common/relayd/relayd.h
src/common/sessiond-comm/relayd.h

index c513130ab6fa40709a95151c885a9479cac80d87..0da8d2834dafe4c697c4d05024165fb0aa9da431 100644 (file)
@@ -34,7 +34,8 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload,
                uint32_t *live_timer, bool *snapshot,
                uint64_t *id_sessiond, lttng_uuid sessiond_uuid,
                bool *has_current_chunk, uint64_t *current_chunk_id,
-               time_t *creation_time)
+               time_t *creation_time,
+               bool *session_name_contains_creation_time)
 {
        int ret;
        struct lttcomm_relayd_create_session_2_11 header;
@@ -132,6 +133,8 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload,
        *current_chunk_id = header.current_chunk_id.value;
        *has_current_chunk = header.current_chunk_id.is_set;
        *creation_time = (time_t) header.creation_time;
+       *session_name_contains_creation_time =
+               header.session_name_contains_creation_time;
 
        ret = 0;
 
index 48841d26b53393be7c78b95725e90931a3cab8be..fc9021024515eb7a2b5f810adbde20cc2b308073 100644 (file)
@@ -26,7 +26,8 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload,
                uint32_t *live_timer, bool *snapshot,
                uint64_t *id_sessiond, lttng_uuid sessiond_uuid,
                bool *has_current_chunk, uint64_t *current_chunk_id,
-               time_t *creation_time);
+               time_t *creation_time,
+               bool *session_name_contains_creation_time);
 
 int cmd_recv_stream_2_11(const struct lttng_buffer_view *payload,
                char **ret_path_name, char **ret_channel_name,
index aeb760a043f773a86ef9d417f31fe36f16854fda..f420ad928ee0e4faf36c5c8f49f3df2bff734383 100644 (file)
@@ -64,6 +64,7 @@ int cmd_create_session_2_4(const struct lttng_buffer_view *payload,
        }
        strncpy(hostname, session_info.hostname,
                        sizeof(session_info.hostname));
+
        *live_timer = be32toh(session_info.live_timer);
        *snapshot = be32toh(session_info.snapshot);
 
index d164bbd8cde4c5c176859bb6728433eef455d479..a2904eb08ae044faa2109338f4af5fe1d0c764b2 100644 (file)
@@ -1073,6 +1073,7 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
        char hostname[LTTNG_HOST_NAME_MAX] = {};
        uint32_t live_timer = 0;
        bool snapshot = false;
+       bool session_name_contains_creation_timestamp = false;
        /* Left nil for peers < 2.11. */
        char base_path[LTTNG_PATH_MAX] = {};
        lttng_uuid sessiond_uuid = {};
@@ -1097,7 +1098,8 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
                ret = cmd_create_session_2_11(payload, session_name, hostname,
                                base_path, &live_timer, &snapshot, &id_sessiond_value,
                                sessiond_uuid, &has_current_chunk,
-                               &current_chunk_id_value, &creation_time_value);
+                               &current_chunk_id_value, &creation_time_value,
+                               &session_name_contains_creation_timestamp);
                if (lttng_uuid_is_nil(sessiond_uuid)) {
                        /* The nil UUID is reserved for pre-2.11 clients. */
                        ERR("Illegal nil UUID announced by peer in create session command");
@@ -1121,7 +1123,8 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
                        id_sessiond.is_set ? &id_sessiond.value : NULL,
                        current_chunk_id.is_set ? &current_chunk_id.value : NULL,
                        creation_time.is_set ? &creation_time.value : NULL,
-                       conn->major, conn->minor);
+                       conn->major, conn->minor,
+                       session_name_contains_creation_timestamp);
        if (!session) {
                ret = -1;
                goto send_reply;
@@ -2266,14 +2269,14 @@ static int init_session_output_directory_handle(struct relay_session *session,
        /*
         * session_directory:
         *
-        * if base_path is NULL
+        * if base_path is \0'
         *   hostname/session_name
         * else
         *   hostname/base_path
         */
        char *session_directory = NULL;
        /*
-        * base path + session_directory
+        * relayd_output_path/session_directory
         * e.g. /home/user/lttng-traces/hostname/session_name
         */
        char *full_session_path = NULL;
@@ -2282,28 +2285,60 @@ static int init_session_output_directory_handle(struct relay_session *session,
         * If base path is set, it overrides the session name for the
         * session relative base path. No timestamp is appended if the
         * base path is overridden.
+        *
+        * If the session name already contains the creation time (e.g.
+        * auto-<timestamp>, don't append yet another timestamp after
+        * the session name in the generated path.
+        *
+        * Otherwise, generate the path with session_name-<timestamp>.
         */
-       if (session->base_path[0] == '\0') {
-               char creation_time_str[16];
+       if (session->base_path[0] != '\0') {
+               pthread_mutex_lock(&session->lock);
+               ret = asprintf(&session_directory, "%s/%s", session->hostname,
+                               session->base_path);
+               pthread_mutex_unlock(&session->lock);
+       } else if (session->session_name_contains_creation_time) {
+               pthread_mutex_lock(&session->lock);
+               ret = asprintf(&session_directory, "%s/%s", session->hostname,
+                               session->session_name);
+               pthread_mutex_unlock(&session->lock);
+       } else {
+               char session_creation_datetime[16];
+               size_t strftime_ret;
                struct tm *timeinfo;
+               time_t creation_time;
 
-               assert(session->creation_time.is_set);
-               timeinfo = localtime(&session->creation_time.value);
-               if (!timeinfo) {
+               /*
+                * The 2.11+ protocol guarantees that a creation time
+                * is provided for a session. This would indicate a
+                * protocol error or an improper use of this util.
+                */
+               if (!session->creation_time.is_set) {
+                       ERR("Creation time missing for session \"%s\" (protocol error)",
+                                       session->session_name);
                        ret = -1;
                        goto end;
                }
-               strftime(creation_time_str, sizeof(creation_time_str), "%Y%m%d-%H%M%S",
-                               timeinfo);
+               creation_time = LTTNG_OPTIONAL_GET(session->creation_time);
 
+               timeinfo = localtime(&creation_time);
+               if (!timeinfo) {
+                       ERR("Failed to get timeinfo while initializing session output directory handle");
+                       ret = -1;
+                       goto end;
+               }
+               strftime_ret = strftime(session_creation_datetime,
+                               sizeof(session_creation_datetime),
+                               "%Y%m%d-%H%M%S", timeinfo);
+               if (strftime_ret == 0) {
+                       ERR("Failed to format session creation timestamp while initializing session output directory handle");
+                       ret = -1;
+                       goto end;
+               }
                pthread_mutex_lock(&session->lock);
-               ret = asprintf(&session_directory, "%s/%s-%s", session->hostname,
-                               session->session_name, creation_time_str);
-               pthread_mutex_unlock(&session->lock);
-       } else {
-               pthread_mutex_lock(&session->lock);
-               ret = asprintf(&session_directory, "%s/%s", session->hostname,
-                               session->base_path);
+               ret = asprintf(&session_directory, "%s/%s-%s",
+                               session->hostname, session->session_name,
+                               session_creation_datetime);
                pthread_mutex_unlock(&session->lock);
        }
        if (ret < 0) {
index de059aae203e2df17054f9bd1d415279e0bd23bf..605931d5bd82ed449143de9b6f9c6b870bb59a2b 100644 (file)
@@ -95,7 +95,8 @@ struct relay_session *session_create(const char *session_name,
                const uint64_t *current_chunk_id,
                const time_t *creation_time,
                uint32_t major,
-               uint32_t minor)
+               uint32_t minor,
+               bool session_name_contains_creation_time)
 {
        int ret;
        struct relay_session *session = NULL;
@@ -136,6 +137,11 @@ struct relay_session *session_create(const char *session_name,
                WARN("Base path exceeds maximal allowed length");
                goto error;
        }
+       if (creation_time) {
+               LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
+       }
+       session->session_name_contains_creation_time =
+                       session_name_contains_creation_time;
 
        session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
        if (!session->ctf_traces_ht) {
@@ -196,9 +202,6 @@ struct relay_session *session_create(const char *session_name,
        }
 
        lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
-       if (creation_time) {
-               LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
-       }
        return session;
 
 error:
index 228873300cab6f134465d8a6fa208b36dfc94d5d..b3d99012d2fa4369a583619858ba99ed3fc98afc 100644 (file)
@@ -87,6 +87,8 @@ struct relay_session {
         */
        bool aborted;
 
+       bool session_name_contains_creation_time;
+
        /* Contains ctf_trace object of that session indexed by path name. */
        struct lttng_ht *ctf_traces_ht;
 
@@ -134,7 +136,8 @@ struct relay_session *session_create(const char *session_name,
                const uint64_t *current_chunk_id,
                const time_t *creation_time,
                uint32_t major,
-               uint32_t minor);
+               uint32_t minor,
+               bool session_name_contains_creation_timestamp);
 struct relay_session *session_get_by_id(uint64_t id);
 bool session_get(struct relay_session *session);
 void session_put(struct relay_session *session);
index 0596033b1441fda9aaa3ca20e613be5923d4b714..577647a1c074597ecd7935e5e3d1e3a37e2d9b32 100644 (file)
@@ -1040,7 +1040,8 @@ static enum lttng_error_code send_consumer_relayd_socket(
                const char *session_name, const char *hostname,
                const char *base_path, int session_live_timer,
                const uint64_t *current_chunk_id,
-               time_t session_creation_time)
+               time_t session_creation_time,
+               bool session_name_contains_creation_time)
 {
        int ret;
        struct lttcomm_relayd_sock *rsock = NULL;
@@ -1070,7 +1071,7 @@ static enum lttng_error_code send_consumer_relayd_socket(
                        relayd_uri->stype, session_id,
                        session_name, hostname, base_path,
                        session_live_timer, current_chunk_id,
-                       session_creation_time);
+                       session_creation_time, session_name_contains_creation_time);
        if (ret < 0) {
                status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
                goto close_sock;
@@ -1118,7 +1119,8 @@ static enum lttng_error_code send_consumer_relayd_sockets(
                unsigned int session_id, struct consumer_output *consumer,
                struct consumer_socket *sock, const char *session_name,
                const char *hostname, const char *base_path, int session_live_timer,
-               const uint64_t *current_chunk_id, time_t session_creation_time)
+               const uint64_t *current_chunk_id, time_t session_creation_time,
+               bool session_name_contains_creation_time)
 {
        enum lttng_error_code status = LTTNG_OK;
 
@@ -1130,7 +1132,8 @@ static enum lttng_error_code send_consumer_relayd_sockets(
                status = send_consumer_relayd_socket(session_id,
                                &consumer->dst.net.control, consumer, sock,
                                session_name, hostname, base_path, session_live_timer,
-                               current_chunk_id, session_creation_time);
+                               current_chunk_id, session_creation_time,
+                               session_name_contains_creation_time);
                if (status != LTTNG_OK) {
                        goto error;
                }
@@ -1141,7 +1144,8 @@ static enum lttng_error_code send_consumer_relayd_sockets(
                status = send_consumer_relayd_socket(session_id,
                                &consumer->dst.net.data, consumer, sock,
                                session_name, hostname, base_path, session_live_timer,
-                               current_chunk_id, session_creation_time);
+                               current_chunk_id, session_creation_time,
+                               session_name_contains_creation_time);
                if (status != LTTNG_OK) {
                        goto error;
                }
@@ -1199,7 +1203,8 @@ int cmd_setup_relayd(struct ltt_session *session)
                                        session->base_path,
                                        session->live_timer,
                                        current_chunk_id.is_set ? &current_chunk_id.value : NULL,
-                                       session->creation_time);
+                                       session->creation_time,
+                                       session->name_contains_creation_time);
                        pthread_mutex_unlock(socket->lock);
                        if (ret != LTTNG_OK) {
                                goto error;
@@ -1224,7 +1229,8 @@ int cmd_setup_relayd(struct ltt_session *session)
                                        session->base_path,
                                        session->live_timer,
                                        current_chunk_id.is_set ? &current_chunk_id.value : NULL,
-                                       session->creation_time);
+                                       session->creation_time,
+                                       session->name_contains_creation_time);
                        pthread_mutex_unlock(socket->lock);
                        if (ret != LTTNG_OK) {
                                goto error;
@@ -4289,7 +4295,8 @@ static enum lttng_error_code set_relayd_for_snapshot(
                                base_path,
                                session->live_timer,
                                current_chunk_id.is_set ? &current_chunk_id.value : NULL,
-                               session->creation_time);
+                               session->creation_time,
+                               session->name_contains_creation_time);
                pthread_mutex_unlock(socket->lock);
                if (status != LTTNG_OK) {
                        rcu_read_unlock();
index 3e23d3255b3fa3334b51d789c1d7bf8a516ecd3d..4480da368d43b1df002506799f47e28767d492c1 100644 (file)
@@ -1076,7 +1076,8 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                enum lttng_stream_type type, uint64_t session_id,
                const char *session_name, const char *hostname,
                const char *base_path, int session_live_timer,
-               const uint64_t *current_chunk_id, time_t session_creation_time)
+               const uint64_t *current_chunk_id, time_t session_creation_time,
+               bool session_name_contains_creation_time)
 {
        int ret;
        struct lttcomm_consumer_msg msg;
@@ -1100,7 +1101,8 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                                session_live_timer,
                                consumer->snapshot, session_id,
                                sessiond_uuid, current_chunk_id,
-                               session_creation_time);
+                               session_creation_time,
+                               session_name_contains_creation_time);
                if (ret < 0) {
                        /* Close the control socket. */
                        (void) relayd_close(rsock);
index 37b522a91c153487708c1f1b6afeb45b29f268d4..af07f5ccfd7a0f17fc53d7dc1846c2f32c552420 100644 (file)
@@ -220,7 +220,8 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                enum lttng_stream_type type, uint64_t session_id,
                const char *session_name, const char *hostname,
                const char *base_path, int session_live_timer,
-               const uint64_t *current_chunk_id, time_t session_creation_time);
+               const uint64_t *current_chunk_id, time_t session_creation_time,
+               bool session_name_contains_creation_time);
 int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
                int pipe);
 int consumer_send_destroy_relayd(struct consumer_socket *sock,
index 2a7b8b83bb8d643ecdfcfdbd3c1364e69b562b10..602059424fd0d9fc8f54044111c5b2f02668e88c 100644 (file)
@@ -1065,6 +1065,7 @@ enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
                                                DEFAULT_SESSION_NAME, i,
                                                datetime);
                        }
+                       new_session->name_contains_creation_time = true;
                        if (ret == -1 || ret >= sizeof(new_session->name)) {
                                /*
                                 * Null-terminate in case the name is used
index cd0f99968a897df40c059669d8200a303003e52a..204701429ded69ba43c77b8917306bc347330172 100644 (file)
@@ -75,6 +75,7 @@ struct ltt_session_list {
 struct ltt_session {
        char name[NAME_MAX];
        bool has_auto_generated_name;
+       bool name_contains_creation_time;
        char hostname[HOST_NAME_MAX]; /* Local hostname. */
        time_t creation_time;
        struct ltt_kernel_session *kernel_session;
index 51de7b82f4a2216f780b15e13c2c9ec899a1b73e..ed5e45607e3d9bf897f47fdba0817b4cd45f0052 100644 (file)
@@ -132,7 +132,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock,
                const char *base_path, int session_live_timer,
                unsigned int snapshot, uint64_t sessiond_session_id,
                const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id,
-               time_t creation_time)
+               time_t creation_time, bool session_name_contains_creation_time)
 {
        int ret;
        struct lttcomm_relayd_create_session_2_11 *msg = NULL;
@@ -142,7 +142,10 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock,
        size_t msg_length;
        char *dst;
 
-       /* The two names are sent with a '\0' delimiter between them. */
+       if (!base_path) {
+               base_path = "";
+       }
+       /* The three names are sent with a '\0' delimiter between them. */
        session_name_len = strlen(session_name) + 1;
        hostname_len = strlen(hostname) + 1;
        base_path_len = base_path ? strlen(base_path) + 1 : 0;
@@ -185,7 +188,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock,
 
        lttng_uuid_copy(msg->sessiond_uuid, sessiond_uuid);
        msg->session_id = htobe64(sessiond_session_id);
-
+       msg->session_name_contains_creation_time = session_name_contains_creation_time;
        if (current_chunk_id) {
                LTTNG_OPTIONAL_SET(&msg->current_chunk_id,
                                htobe64(*current_chunk_id));
@@ -266,7 +269,7 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock,
                unsigned int snapshot, uint64_t sessiond_session_id,
                const lttng_uuid sessiond_uuid,
                const uint64_t *current_chunk_id,
-               time_t creation_time)
+               time_t creation_time, bool session_name_contains_creation_time)
 {
        int ret;
        struct lttcomm_relayd_status_session reply;
@@ -288,7 +291,8 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock,
                ret = relayd_create_session_2_11(rsock, session_name,
                                hostname, base_path, session_live_timer, snapshot,
                                sessiond_session_id, sessiond_uuid,
-                               current_chunk_id, creation_time);
+                               current_chunk_id, creation_time,
+                               session_name_contains_creation_time);
        }
 
        if (ret < 0) {
index 6dc5d0044aa35f38de03cd9caa112db19f013825..8d4c9da13edafd991d39d88a0b1b21b4369b316c 100644 (file)
@@ -46,7 +46,7 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock,
                unsigned int snapshot, uint64_t sessiond_session_id,
                const lttng_uuid sessiond_uuid,
                const uint64_t *current_chunk_id,
-               time_t creation_time);
+               time_t creation_time, bool session_name_contains_creation_time);
 int relayd_add_stream(struct lttcomm_relayd_sock *sock, const char *channel_name,
                const char *pathname, uint64_t *stream_id,
                uint64_t tracefile_size, uint64_t tracefile_count,
index 3bdff8705f88b4a6b0aafeaa13be64b7b16fe102..cc26a2c6bd84b0d636c265a66278376965714519 100644 (file)
@@ -210,6 +210,7 @@ struct lttcomm_relayd_create_session_2_11 {
        uint32_t base_path_len;
        uint32_t live_timer;
        uint8_t snapshot;
+       uint8_t session_name_contains_creation_time;
        /* Sessiond instance UUID */
        lttng_uuid sessiond_uuid;
        /* Sessiond session id */
This page took 0.035279 seconds and 4 git commands to generate.