X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Frotate.c;h=c039bf413381022d7a0a145585630dd660fe4652;hp=f4127c968b558bdbd8c41bb2367fb7f6a3f770c6;hb=d295668767ac8234e83984e1812d342d03293d88;hpb=fb9a95c4d6242bd8336b638c90a7d8f846125659 diff --git a/src/bin/lttng-sessiond/rotate.c b/src/bin/lttng-sessiond/rotate.c index f4127c968..c039bf413 100644 --- a/src/bin/lttng-sessiond/rotate.c +++ b/src/bin/lttng-sessiond/rotate.c @@ -49,305 +49,6 @@ #include #include -/* The session's lock must be held by the caller. */ -static -int session_rename_chunk(struct ltt_session *session, char *current_path, - char *new_path) -{ - int ret; - struct consumer_socket *socket; - struct consumer_output *output; - struct lttng_ht_iter iter; - uid_t uid; - gid_t gid; - - DBG("Renaming session chunk path of session \"%s\" from %s to %s", - session->name, current_path, new_path); - - /* - * Either one of the sessions is enough to find the consumer_output - * and uid/gid. - */ - if (session->kernel_session) { - output = session->kernel_session->consumer; - uid = session->kernel_session->uid; - gid = session->kernel_session->gid; - } else if (session->ust_session) { - output = session->ust_session->consumer; - uid = session->ust_session->uid; - gid = session->ust_session->gid; - } else { - assert(0); - } - - if (!output || !output->socks) { - ERR("No consumer output found for session \"%s\"", - session->name); - ret = -1; - goto end; - } - - rcu_read_lock(); - /* - * We have to iterate to find a socket, but we only need to send the - * rename command to one consumer, so we break after the first one. - */ - cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) { - pthread_mutex_lock(socket->lock); - ret = consumer_rotate_rename(socket, session->id, output, - current_path, new_path, uid, gid); - pthread_mutex_unlock(socket->lock); - if (ret) { - ret = -1; - goto end_unlock; - } - break; - } - - ret = 0; - -end_unlock: - rcu_read_unlock(); -end: - return ret; -} - -/* The session's lock must be held by the caller. */ -static -int rename_first_chunk(struct ltt_session *session, - struct consumer_output *consumer, char *new_path) -{ - int ret; - char current_full_path[LTTNG_PATH_MAX], new_full_path[LTTNG_PATH_MAX]; - - if (session->net_handle > 0) { - /* - * Current domain path: - * HOSTNAME/{SESSION-[TIMESTAMP], USER_DIRECTORY}/DOMAIN - */ - ret = snprintf(current_full_path, sizeof(current_full_path), - "%s%s", - consumer->dst.net.base_dir, - consumer->domain_subdir); - if (ret < 0 || ret >= sizeof(current_full_path)) { - ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"", - session->name); - ret = -LTTNG_ERR_UNK; - goto error; - } - } else { - /* - * Current domain path: - * SESSION_OUTPUT_PATH/DOMAIN - */ - ret = snprintf(current_full_path, sizeof(current_full_path), - "%s/%s", - consumer->dst.session_root_path, - consumer->domain_subdir); - if (ret < 0 || ret >= sizeof(current_full_path)) { - ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"", - session->name); - ret = -LTTNG_ERR_UNK; - goto error; - } - } - /* - * New domain path: - * SESSION_BASE_PATH/_-INDEX/DOMAIN - */ - ret = snprintf(new_full_path, sizeof(new_full_path), "%s/%s", - new_path, consumer->domain_subdir); - if (ret < 0 || ret >= sizeof(new_full_path)) { - ERR("Failed to initialize new full path while renaming first rotation chunk of session \"%s\"", - session->name); - ret = -LTTNG_ERR_UNK; - goto error; - } - /* Move the per-domain inside the first rotation chunk path. */ - ret = session_rename_chunk(session, current_full_path, new_full_path); - if (ret < 0) { - ret = -LTTNG_ERR_UNK; - goto error; - } - - ret = 0; - -error: - return ret; -} - -/* - * Rename a chunk folder after a rotation is complete. - * session_lock_list and session lock must be held. - * - * Returns 0 on success, a negative value on error. - */ -int rename_completed_chunk(struct ltt_session *session, time_t end_ts) -{ - int ret; - size_t strf_ret; - struct tm *timeinfo; - char new_path[LTTNG_PATH_MAX]; - char start_datetime[21], end_datetime[21]; - - DBG("Renaming completed chunk for session %s", session->name); - - /* Format chunk start time. */ - timeinfo = localtime(&session->last_chunk_start_ts); - if (!timeinfo) { - ERR("Failed to separate local time while renaming completed chunk"); - ret = -1; - goto end; - } - strf_ret = strftime(start_datetime, sizeof(start_datetime), - "%Y%m%dT%H%M%S%z", timeinfo); - if (strf_ret == 0) { - ERR("Failed to format timestamp while renaming completed session chunk"); - ret = -1; - goto end; - } - - /* Format chunk end time. */ - timeinfo = localtime(&end_ts); - if (!timeinfo) { - ERR("Failed to parse time while renaming completed chunk"); - ret = -1; - goto end; - } - strf_ret = strftime(end_datetime, sizeof(end_datetime), - "%Y%m%dT%H%M%S%z", timeinfo); - if (strf_ret == 0) { - ERR("Failed to format timestamp while renaming completed session chunk"); - ret = -1; - goto end; - } - - /* Format completed chunk's path. */ - ret = snprintf(new_path, sizeof(new_path), "%s/archives/%s-%s-%" PRIu64, - session_get_base_path(session), - start_datetime, end_datetime, - session->current_archive_id); - if (ret < 0 || ret >= sizeof(new_path)) { - ERR("Failed to format new chunk path while renaming chunk of session \"%s\"", - session->name); - ret = -1; - goto error; - } - - if (session->current_archive_id == 1) { - /* - * 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. - */ - if (session->kernel_session) { - ret = rename_first_chunk(session, - session->kernel_session->consumer, - new_path); - if (ret) { - ERR("Failed to rename kernel session trace folder to \"%s\"", new_path); - /* - * This is not a fatal error for the rotation - * thread, we just need to inform the client - * that a problem occurred with the rotation. - * Returning 0, same for the other errors - * below. - */ - ret = 0; - goto error; - } - } - if (session->ust_session) { - ret = rename_first_chunk(session, - session->ust_session->consumer, - new_path); - if (ret) { - ERR("Failed to rename userspace session trace folder to \"%s\"", new_path); - ret = 0; - goto error; - } - } - } else { - /* - * After the first rotation, all the trace data is already in - * its own chunk folder, we just need to append the suffix. - */ - ret = session_rename_chunk(session, - session->rotation_chunk.current_rotate_path, - new_path); - if (ret) { - ERR("Failed to rename session trace folder from \"%s\" to \"%s\"", - session->rotation_chunk.current_rotate_path, - new_path); - ret = 0; - goto error; - } - } - - /* - * Store the path where the readable chunk is. This path is valid - * and can be queried by the client with rotate_pending until the next - * rotation is started. - */ - ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, - new_path, - sizeof(session->rotation_chunk.current_rotate_path)); - if (ret) { - ERR("Failed the current chunk's path of session \"%s\"", - session->name); - ret = -1; - goto error; - } - - goto end; - -error: - session->rotation_state = LTTNG_ROTATION_STATE_ERROR; -end: - return ret; -} - -int rename_active_chunk(struct ltt_session *session) -{ - int ret; - - session->current_archive_id++; - - /* - * The currently active tracing path is now the folder we - * want to rename. - */ - ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, - session->rotation_chunk.active_tracing_path, - sizeof(session->rotation_chunk.current_rotate_path)); - if (ret) { - ERR("Failed to copy active tracing path"); - goto end; - } - - ret = rename_completed_chunk(session, time(NULL)); - if (ret < 0) { - ERR("Failed to rename current rotation's path"); - goto end; - } - - /* - * We just renamed, the folder, we didn't do an actual rotation, so - * the active tracing path is now the renamed folder and we have to - * restore the rotate count. - */ - ret = lttng_strncpy(session->rotation_chunk.active_tracing_path, - session->rotation_chunk.current_rotate_path, - sizeof(session->rotation_chunk.active_tracing_path)); - if (ret) { - ERR("Failed to rename active session chunk tracing path"); - goto end; - } -end: - session->current_archive_id--; - return ret; -} - int subscribe_session_consumed_size_rotation(struct ltt_session *session, uint64_t size, struct notification_thread_handle *notification_thread_handle) {