From 2a635488acf0c56ffb9b66c57f6bf97a95c53be6 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Thu, 19 Sep 2019 16:56:53 -0400 Subject: [PATCH 1/1] relayd: Add backward compatibility for --group-output-by-session MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The current implementation works for producer (consumerd/lttng-sessiond) using protocol version >= 2.11. For producer using protocol version >= 2.4 and < 2.11, we can use the session name passed on the RELAYD_CREATE_SESSION command to infer information (creation datetime, base path) from the stream path passed on the RELAYD_ADD_STREAM command. That information is then used to craft a stream path, in a best effort manner, that comply with the --group-output-by-session requirements: /-/trace Prior to protocol version 2.4, the session name is unknown on lttng-relayd side. In those cases, we do not perform any transformation and leave the provided stream path as is. A warning (lttng-relayd) is emitted on such occasions. Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau --- src/bin/lttng-relayd/Makefile.am | 3 +- .../backward-compatibility-group-by.c | 232 ++++++++++++++++++ .../backward-compatibility-group-by.h | 24 ++ src/bin/lttng-relayd/main.c | 66 ++++- 4 files changed, 314 insertions(+), 11 deletions(-) create mode 100644 src/bin/lttng-relayd/backward-compatibility-group-by.c create mode 100644 src/bin/lttng-relayd/backward-compatibility-group-by.h diff --git a/src/bin/lttng-relayd/Makefile.am b/src/bin/lttng-relayd/Makefile.am index c95359e74..8b903680a 100644 --- a/src/bin/lttng-relayd/Makefile.am +++ b/src/bin/lttng-relayd/Makefile.am @@ -23,7 +23,8 @@ lttng_relayd_SOURCES = main.c lttng-relayd.h utils.h utils.c cmd.h \ viewer-session.c viewer-session.h \ tracefile-array.c tracefile-array.h \ tcp_keep_alive.c tcp_keep_alive.h \ - sessiond-trace-chunks.c sessiond-trace-chunks.h + sessiond-trace-chunks.c sessiond-trace-chunks.h \ + backward-compatibility-group-by.c backward-compatibility-group-by.h # link on liblttngctl for check if relayd is already alive. lttng_relayd_LDADD = -lurcu-common -lurcu \ diff --git a/src/bin/lttng-relayd/backward-compatibility-group-by.c b/src/bin/lttng-relayd/backward-compatibility-group-by.c new file mode 100644 index 000000000..0127f63f5 --- /dev/null +++ b/src/bin/lttng-relayd/backward-compatibility-group-by.c @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2019 - Jonathan Rajotte + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "backward-compatibility-group-by.h" + +#define DATETIME_STRING_SIZE 16 +#define DATETIME_REGEX \ + ".*-[1-2][0-9][0-9][0-9][0-1][0-1][0-3][0-9]-[0-2][0-9][0-5][0-9][0-5][0-9]$" + +/* + * Provide support for --group-output-by-session for producer >= 2.4 and < 2.11. + * Take the stream path, extract all available information, craft a new path to + * the best of our ability enforcing the group by session. + * + * Return the allocated string containing the new stream path or else NULL. + */ +char *backward_compat_group_by_session( + const char *path, const char *local_session_name) +{ + int ret; + size_t len; + char *leftover_ptr; + char *local_copy = NULL; + char *datetime = NULL; + char *partial_base_path = NULL; + char *filepath_per_session = NULL; + const char *second_token_ptr; + const char *leftover_second_token_ptr; + const char *hostname_ptr; + regex_t regex; + + assert(path); + assert(local_session_name); + assert(local_session_name[0] != '\0'); + + DBG("Parsing path \"%s\" of session \"%s\" to create a new path that is grouped by session", + path, local_session_name); + + /* Get a local copy for strtok */ + local_copy = strdup(path); + if (!local_copy) { + PERROR("Failed to parse session path: couldn't copy input path"); + goto error; + } + + /* + * The use of strtok with '/' as delimiter is valid since we refuse '/' + * in session name and '/' is not a valid hostname character based on + * RFC-952 [1], RFC-921 [2] and refined in RFC-1123 [2]. + * [1] https://tools.ietf.org/html/rfc952 + * [2] https://tools.ietf.org/html/rfc921 + * [3] https://tools.ietf.org/html/rfc1123#page-13 + */ + + /* + * Get the hostname and possible session_name. + * Note that we can get the hostname and session name from the + * relay_session object we already have. Still, it is easier to + * tokenized the passed path to obtain the start of the path leftover. + */ + hostname_ptr = strtok_r(local_copy, "/", &leftover_ptr); + if (!hostname_ptr) { + ERR("Failed to parse session path \"%s\": couldn't identify hostname", + path); + goto error; + } + + second_token_ptr = strtok_r(NULL, "/", &leftover_ptr); + if (!second_token_ptr) { + ERR("Failed to parse session path \"%s\": couldn't identify session name", + path); + goto error; + } + + /* + * Check if the second token is a base path set at url level. This is + * legal in streaming, live and snapshot [1]. Otherwise it is the + * session name with possibly a datetime attached [2]. Note that when + * "adding" snapshot output (lttng snapshot add-output), no session name + * is present in the path by default. The handling for "base path" take + * care of this case as well. + * [1] e.g --set-url net://localhost/my_marvellous_path + * [2] Can be: + * + * When using --snapshot on session create. + * --