Refactor: action executor: fetch session based on execution context
[lttng-tools.git] / src / bin / lttng-sessiond / action-executor.cpp
index cc7834f2745ca6e58d8299dd7c649fe68ecc0aaa..53d831354d21f19942def858f334241b14ac37ab 100644 (file)
@@ -305,38 +305,30 @@ static int action_executor_start_session_handler(
                                session_name, get_action_name(action),
                                get_trigger_name(work_item->trigger));
                lttng_action_increase_execution_failure_count(action);
-               ret = 0;
                goto end;
        }
 
        session_lock_list();
-       session = session_find_by_name(session_name);
+       session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
        if (!session) {
                DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
                                session_name, get_action_name(action),
                                get_trigger_name(work_item->trigger));
+               lttng_action_increase_execution_failure_count(action);
                goto error_unlock_list;
        }
 
-       /*
-        * Check if the session id is the same as when the work item was
-        * enqueued.
-        */
-       if (session->id != LTTNG_OPTIONAL_GET(item->context.session_id)) {
-               DBG("Session id for session `%s` (id: %" PRIu64
-                               " is not the same that was sampled (id: %" PRIu64
-                               " at the moment the work item was enqueued for %s` action of trigger `%s`",
-                               session_name, session->id,
-                               LTTNG_OPTIONAL_GET(item->context.session_id),
+       session_lock(session);
+       if (session->destroyed) {
+               DBG("Session '%s' with id = %" PRIu64 " is flagged as destroyed. Skipping: action = '%s', trigger = '%s'",
+                               session->name, session->id,
                                get_action_name(action),
                                get_trigger_name(work_item->trigger));
-               ret = 0;
-               goto error_unlock_list;
+               goto error_unlock_session;
        }
 
-       session_lock(session);
        if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
-               goto error_dispose_session;
+               goto error_unlock_session;
        }
 
        cmd_ret = (lttng_error_code) cmd_start_trace(session);
@@ -357,7 +349,7 @@ static int action_executor_start_session_handler(
                break;
        }
 
-error_dispose_session:
+error_unlock_session:
        session_unlock(session);
        session_put(session);
 error_unlock_list:
@@ -396,12 +388,11 @@ static int action_executor_stop_session_handler(
                                session_name, get_action_name(action),
                                get_trigger_name(work_item->trigger));
                lttng_action_increase_execution_failure_count(action);
-               ret = 0;
                goto end;
        }
 
        session_lock_list();
-       session = session_find_by_name(session_name);
+       session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
        if (!session) {
                DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
                                session_name, get_action_name(action),
@@ -410,25 +401,17 @@ static int action_executor_stop_session_handler(
                goto error_unlock_list;
        }
 
-       /*
-        * Check if the session id is the same as when the work item was
-        * enqueued
-        */
-       if (session->id != LTTNG_OPTIONAL_GET(item->context.session_id)) {
-               DBG("Session id for session `%s` (id: %" PRIu64
-                               " is not the same that was sampled (id: %" PRIu64
-                               " at the moment the work item was enqueued for %s` action of trigger `%s`",
-                               session_name, session->id,
-                               LTTNG_OPTIONAL_GET(item->context.session_id),
+       session_lock(session);
+       if (session->destroyed) {
+               DBG("Session '%s' with id = %" PRIu64 " is flagged as destroyed. Skipping: action = '%s', trigger = '%s'",
+                               session->name, session->id,
                                get_action_name(action),
                                get_trigger_name(work_item->trigger));
-               ret = 0;
-               goto error_unlock_list;
+               goto error_unlock_session;
        }
 
-       session_lock(session);
        if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
-               goto error_dispose_session;
+               goto error_unlock_session;
        }
 
        cmd_ret = (lttng_error_code) cmd_stop_trace(session);
@@ -449,7 +432,7 @@ static int action_executor_stop_session_handler(
                break;
        }
 
-error_dispose_session:
+error_unlock_session:
        session_unlock(session);
        session_put(session);
 error_unlock_list:
@@ -488,12 +471,11 @@ static int action_executor_rotate_session_handler(
                                session_name, get_action_name(action),
                                get_trigger_name(work_item->trigger));
                lttng_action_increase_execution_failure_count(action);
-               ret = 0;
                goto end;
        }
 
        session_lock_list();
-       session = session_find_by_name(session_name);
+       session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
        if (!session) {
                DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
                                session_name, get_action_name(action),
@@ -502,25 +484,17 @@ static int action_executor_rotate_session_handler(
                goto error_unlock_list;
        }
 
-       /*
-        * Check if the session id is the same as when the work item was
-        * enqueued.
-        */
-       if (session->id != LTTNG_OPTIONAL_GET(item->context.session_id)) {
-               DBG("Session id for session `%s` (id: %" PRIu64
-                   " is not the same that was sampled (id: %" PRIu64
-                   " at the moment the work item was enqueued for %s` action of trigger `%s`",
-                               session_name, session->id,
-                               LTTNG_OPTIONAL_GET(item->context.session_id),
+       session_lock(session);
+       if (session->destroyed) {
+               DBG("Session '%s' with id = %" PRIu64 " is flagged as destroyed. Skipping: action = '%s', trigger = '%s'",
+                               session->name, session->id,
                                get_action_name(action),
                                get_trigger_name(work_item->trigger));
-               ret = 0;
-               goto error_unlock_list;
+               goto error_unlock_session;
        }
 
-       session_lock(session);
        if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
-               goto error_dispose_session;
+               goto error_unlock_session;
        }
 
        cmd_ret = (lttng_error_code) cmd_rotate_session(session, NULL, false,
@@ -548,7 +522,7 @@ static int action_executor_rotate_session_handler(
                break;
        }
 
-error_dispose_session:
+error_unlock_session:
        session_unlock(session);
        session_put(session);
 error_unlock_list:
@@ -583,7 +557,6 @@ static int action_executor_snapshot_session_handler(
                                get_action_name(action),
                                get_trigger_name(work_item->trigger));
                lttng_action_increase_execution_failure_count(action);
-               ret = 0;
                goto end;
        }
 
@@ -607,7 +580,7 @@ static int action_executor_snapshot_session_handler(
        }
 
        session_lock_list();
-       session = session_find_by_name(session_name);
+       session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
        if (!session) {
                DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
                                session_name, get_action_name(action),
@@ -616,25 +589,17 @@ static int action_executor_snapshot_session_handler(
                goto error_unlock_list;
        }
 
-       /*
-        * Check if the session id is the same as when the work item was
-        * enqueued.
-        */
-       if (session->id != LTTNG_OPTIONAL_GET(item->context.session_id)) {
-               DBG("Session id for session `%s` (id: %" PRIu64
-                   " is not the same that was sampled (id: %" PRIu64
-                   " at the moment the work item was enqueued for %s` action of trigger `%s`",
-                               session_name, session->id,
-                               LTTNG_OPTIONAL_GET(item->context.session_id),
+       session_lock(session);
+       if (session->destroyed) {
+               DBG("Session '%s' with id = %" PRIu64 " is flagged as destroyed. Skipping: action = '%s', trigger = '%s'",
+                               session->name, session->id,
                                get_action_name(action),
                                get_trigger_name(work_item->trigger));
-               ret = 0;
-               goto error_unlock_list;
+               goto error_unlock_session;
        }
 
-       session_lock(session);
        if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
-               goto error_dispose_session;
+               goto error_unlock_session;
        }
 
        cmd_ret = (lttng_error_code) cmd_snapshot_record(session, snapshot_output, 0);
@@ -651,7 +616,7 @@ static int action_executor_snapshot_session_handler(
                break;
        }
 
-error_dispose_session:
+error_unlock_session:
        session_unlock(session);
        session_put(session);
 error_unlock_list:
This page took 0.026587 seconds and 4 git commands to generate.