relayd: Retrieve a relay_session's trace chunk on creation
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 4 Jul 2019 13:04:53 +0000 (09:04 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 25 Jul 2019 19:51:47 +0000 (15:51 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-relayd/cmd-2-11.c
src/bin/lttng-relayd/cmd-2-11.h
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/common/relayd/relayd.h

index 857f3594299ebe7f17ae90ecb703d73321484202..269eeb9b078a707d4f22e8995a89cc7aa73147d9 100644 (file)
@@ -32,7 +32,8 @@
 int cmd_create_session_2_11(const struct lttng_buffer_view *payload,
                char *session_name, char *hostname,
                uint32_t *live_timer, bool *snapshot,
-               lttng_uuid sessiond_uuid)
+               uint64_t *id_sessiond, lttng_uuid sessiond_uuid,
+               uint64_t *current_chunk_id)
 {
        int ret;
        struct lttcomm_relayd_create_session_2_11 header;
index 54f6f409f1fe9308448dd1b3403e66f4a53c8126..eedf541873a865f1ef4c69f0cbc3873fceb5a554 100644 (file)
@@ -24,7 +24,8 @@
 int cmd_create_session_2_11(const struct lttng_buffer_view *payload,
                char *session_name, char *hostname,
                uint32_t *live_timer, bool *snapshot,
-               lttng_uuid sessiond_uuid);
+               uint64_t *id_sessiond, lttng_uuid sessiond_uuid,
+               uint64_t *current_chunk_id);
 
 int cmd_recv_stream_2_11(const struct lttng_buffer_view *payload,
                char **ret_path_name, char **ret_channel_name,
index db021f43fc5b28a9ac0de322752880a3c9a171c9..8f0767af373cf10c28dbdcdf078758c22c889941 100644 (file)
@@ -1095,18 +1095,15 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
        int ret = 0;
        ssize_t send_ret;
        struct relay_session *session = NULL;
-       struct lttcomm_relayd_status_session reply;
-       char session_name[LTTNG_NAME_MAX];
-       char hostname[LTTNG_HOST_NAME_MAX];
+       struct lttcomm_relayd_status_session reply = {};
+       char session_name[LTTNG_NAME_MAX] = {};
+       char hostname[LTTNG_HOST_NAME_MAX] = {};
        uint32_t live_timer = 0;
        bool snapshot = false;
        /* Left nil for peers < 2.11. */
        lttng_uuid sessiond_uuid = {};
-
-       memset(session_name, 0, LTTNG_NAME_MAX);
-       memset(hostname, 0, LTTNG_HOST_NAME_MAX);
-
-       memset(&reply, 0, sizeof(reply));
+       LTTNG_OPTIONAL(uint64_t) id_sessiond = {};
+       LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
 
        if (conn->minor < 4) {
                /* From 2.1 to 2.3 */
@@ -1119,13 +1116,16 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
                /* From 2.11 to ... */
                ret = cmd_create_session_2_11(payload, session_name,
                                hostname, &live_timer, &snapshot,
-                               sessiond_uuid);
+                               &id_sessiond.value, sessiond_uuid,
+                               &current_chunk_id.value);
                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");
                        ret = -1;
                        goto send_reply;
                }
+               id_sessiond.is_set = true;
+               current_chunk_id.is_set = true;
        }
 
        if (ret < 0) {
@@ -1133,7 +1133,10 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
        }
 
        session = session_create(session_name, hostname, live_timer,
-                       snapshot, sessiond_uuid, conn->major, conn->minor);
+                       snapshot, sessiond_uuid,
+                       id_sessiond.is_set ? &id_sessiond.value : NULL,
+                       current_chunk_id.is_set ? &current_chunk_id.value : NULL,
+                       conn->major, conn->minor);
        if (!session) {
                ret = -1;
                goto send_reply;
@@ -1147,8 +1150,7 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
        session->current_trace_chunk =
                        sessiond_trace_chunk_registry_get_anonymous_chunk(
                                sessiond_trace_chunk_registry, sessiond_uuid,
-                               session->id,
-                               opt_output_path);
+                               session->id);
        if (!session->current_trace_chunk) {
                ret = -1;
        }
index 51b1a3497641549af8faab3775bc76fd02f795c9..e087b51c3b71ceece2d66d71faa6b3d62d905810 100644 (file)
@@ -19,6 +19,7 @@
 
 #define _LGPL_SOURCE
 #include <common/common.h>
+#include <common/compat/uuid.h>
 #include <urcu/rculist.h>
 
 #include "lttng-relayd.h"
 static uint64_t last_relay_session_id;
 static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
 
+static int session_set_anonymous_chunk(struct relay_session *session)
+{
+       int ret = 0;
+       struct lttng_trace_chunk *chunk = NULL;
+       enum lttng_trace_chunk_status status;
+       struct lttng_directory_handle output_directory;
+
+       ret = lttng_directory_handle_init(&output_directory, opt_output_path);
+       if (ret) {
+               goto end;
+       }
+
+       chunk = lttng_trace_chunk_create_anonymous();
+       if (!chunk) {
+               goto end;
+       }
+
+       status = lttng_trace_chunk_set_credentials_current_user(chunk);
+       if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+               ret = -1;
+               goto end;
+       }
+
+       status = lttng_trace_chunk_set_as_owner(chunk, &output_directory);
+       if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+               ret = -1;
+               goto end;
+       }
+       session->current_trace_chunk = chunk;
+       chunk = NULL;
+end:
+       lttng_trace_chunk_put(chunk);
+       lttng_directory_handle_fini(&output_directory);
+       return ret;
+}
+
 /*
  * Create a new session by assigning a new session ID.
  *
@@ -39,6 +76,7 @@ static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
 struct relay_session *session_create(const char *session_name,
                const char *hostname, uint32_t live_timer,
                bool snapshot, const lttng_uuid sessiond_uuid,
+               uint64_t *id_sessiond, uint64_t *current_chunk_id,
                uint32_t major, uint32_t minor)
 {
        int ret;
@@ -46,15 +84,17 @@ struct relay_session *session_create(const char *session_name,
 
        session = zmalloc(sizeof(*session));
        if (!session) {
-               PERROR("relay session zmalloc");
+               PERROR("Failed to allocate session");
                goto error;
        }
        if (lttng_strncpy(session->session_name, session_name,
                        sizeof(session->session_name))) {
+               WARN("Session name exceeds maximal allowed length");
                goto error;
        }
        if (lttng_strncpy(session->hostname, hostname,
                        sizeof(session->hostname))) {
+               WARN("Hostname exceeds maximal allowed length");
                goto error;
        }
        session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
@@ -78,17 +118,48 @@ struct relay_session *session_create(const char *session_name,
        session->snapshot = snapshot;
        lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
 
+       if (id_sessiond) {
+               LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
+       }
+
        ret = sessiond_trace_chunk_registry_session_created(
                        sessiond_trace_chunk_registry, sessiond_uuid);
        if (ret) {
                goto error;
        }
 
+       if (id_sessiond && current_chunk_id) {
+               session->current_trace_chunk =
+                               sessiond_trace_chunk_registry_get_chunk(
+                                       sessiond_trace_chunk_registry,
+                                       session->sessiond_uuid,
+                                       session->id_sessiond.value,
+                                       *current_chunk_id);
+               if (!session->current_trace_chunk) {
+                       char uuid_str[UUID_STR_LEN];
+
+                       lttng_uuid_to_str(sessiond_uuid, uuid_str);
+                       ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
+                                       uuid_str, *id_sessiond,
+                                       *current_chunk_id);
+                }
+       } else if (!id_sessiond) {
+               /*
+                * Pre-2.11 peers will not announce trace chunks. An
+                * anonymous trace chunk which will remain set for the
+                * duration of the session is created.
+                */
+               ret = session_set_anonymous_chunk(session);
+               if (ret) {
+                       goto error;
+               }
+       }
+
        lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
        return session;
 
 error:
-       free(session);
+       session_put(session);
        return NULL;
 }
 
index 426cd0317ab46785c06ae44c5ed99c8438b0cc9e..8c679da9c975c858e2bd863a73e9ccc9aae9480f 100644 (file)
@@ -30,6 +30,7 @@
 #include <common/hashtable/hashtable.h>
 #include <common/compat/uuid.h>
 #include <common/trace-chunk.h>
+#include <common/optional.h>
 
 /*
  * Represents a session for the relay point of view
@@ -41,6 +42,15 @@ struct relay_session {
         * It is used to match a set of streams to their session.
         */
        uint64_t id;
+       /*
+        * ID of the session in the session daemon's domain.
+        * This information is only provided by 2.11+ peers.
+        */
+       LTTNG_OPTIONAL(uint64_t) id_sessiond;
+       /*
+        * Only provided by 2.11+ peers. However, the UUID is set to 'nil' in
+        * the other cases.
+        */
        lttng_uuid sessiond_uuid;
        char session_name[LTTNG_NAME_MAX];
        char hostname[LTTNG_HOST_NAME_MAX];
@@ -115,6 +125,7 @@ struct relay_session {
 struct relay_session *session_create(const char *session_name,
                const char *hostname, uint32_t live_timer,
                bool snapshot, const lttng_uuid sessiond_uuid,
+               uint64_t *id_sessiond, uint64_t *current_chunk_id,
                uint32_t major, uint32_t minor);
 struct relay_session *session_get_by_id(uint64_t id);
 bool session_get(struct relay_session *session);
index 84d6fd9b389f7ada44caacbb5f7d3fd1ccb1589e..d26d8de0bf661b7714914645558edcb2922e2f1d 100644 (file)
@@ -1038,7 +1038,8 @@ static enum lttng_error_code send_consumer_relayd_socket(
                struct consumer_output *consumer,
                struct consumer_socket *consumer_sock,
                const char *session_name, const char *hostname,
-               int session_live_timer)
+               int session_live_timer,
+               const uint64_t *current_chunk_id)
 {
        int ret;
        struct lttcomm_relayd_sock *rsock = NULL;
@@ -1066,7 +1067,8 @@ static enum lttng_error_code send_consumer_relayd_socket(
        /* Send relayd socket to consumer. */
        ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
                        relayd_uri->stype, session_id,
-                       session_name, hostname, session_live_timer);
+                       session_name, hostname, session_live_timer,
+                       current_chunk_id);
        if (ret < 0) {
                status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
                goto close_sock;
@@ -1113,7 +1115,8 @@ static enum lttng_error_code send_consumer_relayd_sockets(
                enum lttng_domain_type domain,
                unsigned int session_id, struct consumer_output *consumer,
                struct consumer_socket *sock, const char *session_name,
-               const char *hostname, int session_live_timer)
+               const char *hostname, int session_live_timer,
+               const uint64_t *current_chunk_id)
 {
        enum lttng_error_code status = LTTNG_OK;
 
@@ -1124,7 +1127,8 @@ static enum lttng_error_code send_consumer_relayd_sockets(
        if (!sock->control_sock_sent) {
                status = send_consumer_relayd_socket(session_id,
                                &consumer->dst.net.control, consumer, sock,
-                               session_name, hostname, session_live_timer);
+                               session_name, hostname, session_live_timer,
+                               current_chunk_id);
                if (status != LTTNG_OK) {
                        goto error;
                }
@@ -1134,7 +1138,8 @@ static enum lttng_error_code send_consumer_relayd_sockets(
        if (!sock->data_sock_sent) {
                status = send_consumer_relayd_socket(session_id,
                                &consumer->dst.net.data, consumer, sock,
-                               session_name, hostname, session_live_timer);
+                               session_name, hostname, session_live_timer,
+                               current_chunk_id);
                if (status != LTTNG_OK) {
                        goto error;
                }
@@ -1156,14 +1161,28 @@ int cmd_setup_relayd(struct ltt_session *session)
        struct ltt_kernel_session *ksess;
        struct consumer_socket *socket;
        struct lttng_ht_iter iter;
+        LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
 
-       assert(session);
+        assert(session);
 
        usess = session->ust_session;
        ksess = session->kernel_session;
 
        DBG("Setting relayd for session %s", session->name);
 
+       if (session->current_trace_chunk) {
+               enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
+                               session->current_trace_chunk, &current_chunk_id.value);
+
+               if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
+                       current_chunk_id.is_set = true;
+               } else {
+                       ERR("Failed to get current trace chunk id");
+                       ret = LTTNG_ERR_UNK;
+                       goto error;
+               }
+       }
+
        rcu_read_lock();
 
        if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
@@ -1175,7 +1194,8 @@ int cmd_setup_relayd(struct ltt_session *session)
                        ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
                                        usess->consumer, socket,
                                        session->name, session->hostname,
-                                       session->live_timer);
+                                       session->live_timer,
+                                       current_chunk_id.is_set ? &current_chunk_id.value : NULL);
                        pthread_mutex_unlock(socket->lock);
                        if (ret != LTTNG_OK) {
                                goto error;
@@ -1197,7 +1217,8 @@ int cmd_setup_relayd(struct ltt_session *session)
                        ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
                                        ksess->consumer, socket,
                                        session->name, session->hostname,
-                                       session->live_timer);
+                                       session->live_timer,
+                                       current_chunk_id.is_set ? &current_chunk_id.value : NULL);
                        pthread_mutex_unlock(socket->lock);
                        if (ret != LTTNG_OK) {
                                goto error;
@@ -4184,6 +4205,7 @@ static enum lttng_error_code set_relayd_for_snapshot(
        enum lttng_error_code status = LTTNG_OK;
        struct lttng_ht_iter iter;
        struct consumer_socket *socket;
+       LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
 
        assert(consumer);
        assert(snap_output);
@@ -4191,6 +4213,19 @@ static enum lttng_error_code set_relayd_for_snapshot(
 
        DBG2("Set relayd object from snapshot output");
 
+       if (session->current_trace_chunk) {
+               enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
+                               session->current_trace_chunk, &current_chunk_id.value);
+
+               if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
+                       current_chunk_id.is_set = true;
+               } else {
+                       ERR("Failed to get current trace chunk id");
+                       status = LTTNG_ERR_UNK;
+                       goto error;
+               }
+       }
+
        /* Ignore if snapshot consumer output is not network. */
        if (snap_output->consumer->type != CONSUMER_DST_NET) {
                goto error;
@@ -4207,7 +4242,8 @@ static enum lttng_error_code set_relayd_for_snapshot(
                status = send_consumer_relayd_sockets(0, session->id,
                                snap_output->consumer, socket,
                                session->name, session->hostname,
-                               session->live_timer);
+                               session->live_timer,
+                               current_chunk_id.is_set ? &current_chunk_id.value : NULL);
                pthread_mutex_unlock(socket->lock);
                if (status != LTTNG_OK) {
                        rcu_read_unlock();
index 50d19b074cf09542eb1a757f68fa3aaaa135a1db..8ff002cea1598013d9c460c11e07ba0d3e32c598 100644 (file)
@@ -1085,7 +1085,7 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
                enum lttng_stream_type type, uint64_t session_id,
                const char *session_name, const char *hostname,
-               int session_live_timer)
+               int session_live_timer, const uint64_t *current_chunk_id)
 {
        int ret;
        struct lttcomm_consumer_msg msg;
@@ -1107,7 +1107,7 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                                &msg.u.relayd_sock.relayd_session_id,
                                session_name, hostname, session_live_timer,
                                consumer->snapshot, session_id,
-                               sessiond_uuid);
+                               sessiond_uuid, current_chunk_id);
                if (ret < 0) {
                        /* Close the control socket. */
                        (void) relayd_close(rsock);
index 64f95d026dd32c9df00f95d96ae6017adabaf7a1..cf6fe48c9e291bfc1caa62146461f5430098f428 100644 (file)
@@ -219,7 +219,7 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
                struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
                enum lttng_stream_type type, uint64_t session_id,
                const char *session_name, const char *hostname,
-               int session_live_timer);
+               int session_live_timer, const uint64_t *current_chunk_id);
 int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
                int pipe);
 int consumer_send_destroy_relayd(struct consumer_socket *sock,
index ec114020e57e50ad177d3a3e7d364e053c874fbb..c8a216c543a0a984d4d368a81f1204723b500f0a 100644 (file)
 
 int relayd_connect(struct lttcomm_relayd_sock *sock);
 int relayd_close(struct lttcomm_relayd_sock *sock);
-int relayd_create_session(struct lttcomm_relayd_sock *sock,
-               uint64_t *relayd_session_id, const char *session_name,
-               const char *hostname, int session_live_timer,
+int relayd_create_session(struct lttcomm_relayd_sock *rsock,
+               uint64_t *relayd_session_id,
+               const char *session_name, const char *hostname,
+               int session_live_timer,
                unsigned int snapshot, uint64_t sessiond_session_id,
-               const lttng_uuid sessiond_uuid);
+               const lttng_uuid sessiond_uuid,
+               const uint64_t *current_chunk_id);
 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,
This page took 0.04855 seconds and 4 git commands to generate.