Skip to content

Commit 918dc52

Browse files
authored
refactor: remove extension group name parameter and add new function to retrieve it (#772)
1 parent 707e316 commit 918dc52

File tree

31 files changed

+216
-582
lines changed

31 files changed

+216
-582
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"request": "launch",
138138
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
139139
"args": [
140-
"--gtest_filter=ExtensionTest.FailedToConnectToRemote2"
140+
"--gtest_filter=ExtensionTest.CommandInvalidExtensionGroup"
141141
],
142142
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
143143
"env": {

core/include_internal/ten_runtime/extension_context/extension_context.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "include_internal/ten_runtime/common/loc.h"
1414
#include "ten_runtime/msg/msg.h"
1515
#include "ten_utils/container/list.h"
16-
#include "ten_utils/lib/atomic.h"
1716
#include "ten_utils/lib/signature.h"
1817
#include "ten_utils/lib/string.h"
1918
#include "ten_utils/sanitizer/thread_check.h"
@@ -71,9 +70,18 @@ TEN_RUNTIME_PRIVATE_API void ten_extension_context_on_close(
7170
ten_extension_context_t *self);
7271

7372
TEN_RUNTIME_PRIVATE_API ten_extension_info_t *
74-
ten_extension_context_get_extension_info_by_name(
75-
ten_extension_context_t *self, const char *app_uri, const char *graph_id,
76-
const char *extension_group_name, const char *extension_name);
73+
ten_extension_context_get_extension_info_by_name(ten_extension_context_t *self,
74+
const char *app_uri,
75+
const char *graph_id,
76+
const char *extension_name,
77+
bool check_thread);
7778

7879
TEN_RUNTIME_PRIVATE_API bool ten_extension_context_start_extension_group(
7980
ten_extension_context_t *self, ten_error_t *err);
81+
82+
TEN_RUNTIME_PRIVATE_API const char *
83+
ten_extension_context_get_extension_group_name(ten_extension_context_t *self,
84+
const char *app_uri,
85+
const char *graph_id,
86+
const char *extension_name,
87+
bool check_thread);

core/include_internal/ten_runtime/extension_group/extension_group.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "ten_utils/container/list.h"
1717
#include "ten_utils/io/runloop.h"
1818
#include "ten_utils/lib/signature.h"
19-
#include "ten_utils/lib/smart_ptr.h"
2019
#include "ten_utils/sanitizer/thread_check.h"
2120

2221
#define TEN_EXTENSION_GROUP_SIGNATURE 0x94F72EDA6137DF04U
@@ -124,10 +123,6 @@ TEN_RUNTIME_PRIVATE_API void ten_extension_group_destroy_extensions(
124123
TEN_RUNTIME_PRIVATE_API void ten_extension_group_set_addon(
125124
ten_extension_group_t *self, ten_addon_host_t *addon_host);
126125

127-
TEN_RUNTIME_PRIVATE_API ten_shared_ptr_t *
128-
ten_extension_group_create_cmd_result_for_invalid_dest(
129-
ten_shared_ptr_t *origin_cmd, ten_string_t *target_group_name);
130-
131126
TEN_RUNTIME_PRIVATE_API ten_runloop_t *ten_extension_group_get_attached_runloop(
132127
ten_extension_group_t *self);
133128

core/include_internal/ten_runtime/extension_group/msg_interface/common.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

core/src/ten_manager/src/designer/template_pkgs/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
use actix_web::{web, HttpResponse, Responder};
88
use anyhow::{anyhow, Result};
9+
use semver::Version;
910
use serde::{Deserialize, Serialize};
1011
use std::sync::Arc;
1112
use strum_macros::{Display, EnumString};
@@ -44,9 +45,15 @@ pub struct GetTemplateRequestPayload {
4445
pub language: TemplateLanguage,
4546
}
4647

48+
#[derive(Serialize, Deserialize, Debug)]
49+
pub struct TemplateInfo {
50+
pub pkg_name: String,
51+
pub pkg_version: Version,
52+
}
53+
4754
#[derive(Serialize, Deserialize, Debug)]
4855
pub struct GetTemplateResponseData {
49-
pub template_name: Vec<String>,
56+
pub templates: Vec<TemplateInfo>,
5057
}
5158

5259
pub async fn get_template_endpoint(
@@ -85,13 +92,16 @@ pub async fn get_template_endpoint(
8592
match result {
8693
Ok(packages) => {
8794
// Extract the package names from the PkgRegistryInfo structs.
88-
let template_names: Vec<String> = packages
95+
let templates: Vec<TemplateInfo> = packages
8996
.iter()
90-
.map(|pkg| pkg.basic_info.type_and_name.name.clone())
97+
.map(|pkg| TemplateInfo {
98+
pkg_name: pkg.basic_info.type_and_name.name.clone(),
99+
pkg_version: pkg.basic_info.version.clone(),
100+
})
91101
.collect();
92102

93103
// Handle case where no packages were found.
94-
if template_names.is_empty() {
104+
if templates.is_empty() {
95105
let error_message = format!(
96106
"Unsupported template combination: pkg_type={}, \
97107
language={}",
@@ -109,7 +119,7 @@ pub async fn get_template_endpoint(
109119

110120
let response = ApiResponse {
111121
status: Status::Ok,
112-
data: GetTemplateResponseData { template_name: template_names },
122+
data: GetTemplateResponseData { templates },
113123
meta: None,
114124
};
115125

core/src/ten_manager/tests/test_case/designer/template_pkgs/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use actix_web::{http::StatusCode, test, web, App};
1010

1111
use ten_manager::{
1212
config::{metadata::TmanMetadata, TmanConfig},
13-
constants::{DEFAULT_APP_NODEJS, DEFAULT_EXTENSION_CPP},
1413
designer::{
1514
response::{ApiResponse, Status},
1615
template_pkgs::{
@@ -58,7 +57,7 @@ async fn test_get_template_app_typescript() {
5857
test::call_and_read_body_json(&app, req).await;
5958

6059
assert_eq!(resp.status, Status::Ok);
61-
assert_eq!(resp.data.template_name, vec![DEFAULT_APP_NODEJS.to_string()]);
60+
println!("{:?}", resp.data.templates);
6261
}
6362

6463
#[actix_web::test]
@@ -96,10 +95,7 @@ async fn test_get_template_extension_cpp() {
9695
test::call_and_read_body_json(&app, req).await;
9796

9897
assert_eq!(resp.status, Status::Ok);
99-
assert_eq!(
100-
resp.data.template_name,
101-
vec![DEFAULT_EXTENSION_CPP.to_string()]
102-
);
98+
println!("{:?}", resp.data.templates);
10399
}
104100

105101
#[actix_web::test]

core/src/ten_runtime/app/msg_interface/common.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void ten_app_do_connection_migration_or_push_to_engine_queue(
5252
// TEN_NOLINTNEXTLINE(thread-check)
5353
// thread-check: We are in the app thread, and all the uses of the engine in
5454
// this function would not cause thread safety issues.
55-
TEN_ASSERT(engine && ten_engine_check_integrity(engine, false),
56-
"This function is called in the app thread.");
55+
TEN_ASSERT(engine, "Should not happen.");
56+
TEN_ASSERT(ten_engine_check_integrity(engine, false), "Should not happen.");
5757

5858
if (connection && ten_connection_needs_to_migrate(connection, engine)) {
5959
ten_connection_migrate(connection, engine, msg);
@@ -317,8 +317,8 @@ static bool ten_app_handle_stop_graph_cmd(ten_app_t *self,
317317
// to the engine.
318318
ten_list_foreach (ten_msg_get_dest(cmd), iter) {
319319
ten_loc_t *dest_loc = ten_ptr_listnode_get(iter.node);
320-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
321-
"Should not happen.");
320+
TEN_ASSERT(dest_loc, "Should not happen.");
321+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
322322

323323
ten_string_set_formatted(&dest_loc->graph_id, "%s",
324324
ten_string_get_raw_str(&dest_engine->graph_id));
@@ -354,8 +354,8 @@ static bool ten_app_handle_cmd_result(ten_app_t *self,
354354

355355
#if defined(_DEBUG)
356356
ten_loc_t *dest_loc = ten_msg_get_first_dest_loc(cmd_result);
357-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
358-
"Should not happen.");
357+
TEN_ASSERT(dest_loc, "Should not happen.");
358+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
359359

360360
ten_string_t loc_str;
361361
TEN_STRING_INIT(loc_str);
@@ -405,9 +405,9 @@ static bool ten_app_handle_cmd_result(ten_app_t *self,
405405
bool ten_app_dispatch_msg(ten_app_t *self, ten_shared_ptr_t *msg,
406406
ten_error_t *err) {
407407
ten_loc_t *dest_loc = ten_msg_get_first_dest_loc(msg);
408-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc) &&
409-
ten_msg_get_dest_cnt(msg) == 1,
410-
"Should not happen.");
408+
TEN_ASSERT(dest_loc, "Should not happen.");
409+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
410+
TEN_ASSERT(ten_msg_get_dest_cnt(msg) == 1, "Should not happen.");
411411
TEN_ASSERT(!ten_string_is_empty(&dest_loc->app_uri),
412412
"App URI should not be empty.");
413413

core/src/ten_runtime/connection/migration.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ void ten_connection_upgrade_migration_state_to_done(ten_connection_t *self,
134134
if (engine) {
135135
// The message is sent to the app, not an engine.
136136

137-
TEN_ASSERT(engine && ten_engine_check_integrity(engine, true),
138-
"Access across threads.");
137+
TEN_ASSERT(engine, "Invalid argument.");
138+
TEN_ASSERT(ten_engine_check_integrity(engine, true),
139+
"Invalid use of engine %p.", engine);
139140

140141
// @{
141142
// Attach to engine.

core/src/ten_runtime/engine/engine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ static void ten_engine_set_graph_id(ten_engine_t *self, ten_shared_ptr_t *cmd) {
139139
// Set the newly created graph_id to the 'start_graph' command.
140140
ten_list_foreach (ten_msg_get_dest(cmd), iter) {
141141
ten_loc_t *dest_loc = ten_ptr_listnode_get(iter.node);
142-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
143-
"Should not happen.");
142+
TEN_ASSERT(dest_loc, "Should not happen.");
143+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
144144

145145
ten_string_set_formatted(&dest_loc->graph_id, "%s",
146146
ten_string_get_raw_str(&graph_id_str));

core/src/ten_runtime/engine/internal/extension_interface.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ static void ten_engine_on_all_extension_threads_are_ready(
181181

182182
if (error_occurred) {
183183
ten_app_t *app = self->app;
184-
TEN_ASSERT(app && ten_app_check_integrity(app, false),
185-
"Invalid argument.");
184+
TEN_ASSERT(app, "Should not happen.");
185+
TEN_ASSERT(ten_app_check_integrity(app, false), "Should not happen.");
186186

187187
// This graph/engine will not be functioning properly, so it will be shut
188188
// down directly.
@@ -236,9 +236,7 @@ void ten_engine_find_extension_info_for_all_extensions_of_extension_thread_task(
236236
ten_extension_context_get_extension_info_by_name(
237237
extension_context, ten_app_get_uri(extension_context->engine->app),
238238
ten_engine_get_id(extension_context->engine, true),
239-
ten_extension_group_get_name(extension_thread->extension_group,
240-
false),
241-
ten_extension_get_name(extension, false));
239+
ten_extension_get_name(extension, false), true);
242240
}
243241

244242
if (extension_thread->is_close_triggered) {

core/src/ten_runtime/engine/msg_interface/common.c

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "include_internal/ten_runtime/msg/msg.h"
2525
#include "include_internal/ten_runtime/msg/msg_info.h"
2626
#include "include_internal/ten_runtime/remote/remote.h"
27+
#include "include_internal/ten_utils/value/value.h"
2728
#include "ten_runtime/app/app.h"
2829
#include "ten_runtime/msg/cmd_result/cmd_result.h"
2930
#include "ten_runtime/msg/msg.h"
@@ -363,18 +364,37 @@ static void ten_engine_post_msg_to_extension_thread(
363364
}
364365
}
365366

367+
ten_shared_ptr_t *ten_engine_create_cmd_result_for_invalid_dest(
368+
ten_shared_ptr_t *origin_cmd) {
369+
TEN_ASSERT(origin_cmd, "Should not happen.");
370+
371+
if (!ten_msg_is_cmd_and_result(origin_cmd)) {
372+
ten_msg_dump(origin_cmd, NULL, "Unexpected message: ^m");
373+
TEN_ASSERT(0, "Should not happen.");
374+
}
375+
376+
ten_shared_ptr_t *cmd_result =
377+
ten_cmd_result_create_from_cmd(TEN_STATUS_CODE_ERROR, origin_cmd);
378+
ten_msg_set_property(cmd_result, TEN_STR_DETAIL,
379+
ten_value_create_vstring("Failed to find destination."),
380+
NULL);
381+
382+
return cmd_result;
383+
}
384+
366385
bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
367386
TEN_ASSERT(self, "Should not happen.");
368387
TEN_ASSERT(ten_engine_check_integrity(self, true), "Should not happen.");
388+
369389
TEN_ASSERT(msg, "Should not happen.");
370390
TEN_ASSERT(ten_msg_check_integrity(msg), "Should not happen.");
371391
TEN_ASSERT(ten_msg_get_dest_cnt(msg) == 1,
372392
"When this function is executed, there should be only one "
373393
"destination remaining in the message's dest.");
374394

375395
ten_loc_t *dest_loc = ten_msg_get_first_dest_loc(msg);
376-
TEN_ASSERT(dest_loc && ten_loc_check_integrity(dest_loc),
377-
"Should not happen.");
396+
TEN_ASSERT(dest_loc, "Should not happen.");
397+
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
378398

379399
ten_app_t *app = self->app;
380400
TEN_ASSERT(app, "Invalid argument.");
@@ -409,7 +429,7 @@ bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
409429
// current TEN app.
410430
ten_app_push_to_in_msgs_queue(app, msg);
411431
} else {
412-
if (ten_string_is_empty(&dest_loc->extension_group_name)) {
432+
if (ten_string_is_empty(&dest_loc->extension_name)) {
413433
// It means the destination is the current engine, so ask the current
414434
// engine to handle this message.
415435
ten_engine_handle_msg(self, msg);
@@ -419,34 +439,45 @@ bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
419439
if (self->extension_context) {
420440
bool found = false;
421441

422-
ten_list_foreach (&self->extension_context->extension_threads, iter) {
423-
ten_extension_thread_t *extension_thread =
424-
ten_ptr_listnode_get(iter.node);
425-
TEN_ASSERT(
426-
extension_thread &&
427-
// TEN_NOLINTNEXTLINE(thread-check)
428-
// thread-check: We are in the engine thread, _not_ in the
429-
// extension thread. However, before the engine is closed,
430-
// the pointer of the extension group and the pointer of the
431-
// extension thread will not be changed, and the closing of
432-
// the entire engine must start from the engine, so the
433-
// execution to this position means that the engine has not
434-
// been closed, so there will be no thread safety issue.
435-
ten_extension_thread_check_integrity(extension_thread,
436-
false),
437-
"Should not happen.");
438-
439-
ten_extension_group_t *extension_group =
440-
extension_thread->extension_group;
441-
442-
if (ten_string_is_equal(&extension_group->name,
443-
&dest_loc->extension_group_name)) {
444-
// Find the correct extension thread, ask it to handle the
445-
// message.
446-
found = true;
447-
ten_engine_post_msg_to_extension_thread(self, extension_thread,
448-
msg);
449-
break;
442+
const char *extension_group_name =
443+
ten_extension_context_get_extension_group_name(
444+
self->extension_context,
445+
ten_string_get_raw_str(&dest_loc->app_uri),
446+
ten_string_get_raw_str(&dest_loc->graph_id),
447+
ten_string_get_raw_str(&dest_loc->extension_name), true);
448+
449+
if (extension_group_name) {
450+
ten_list_foreach (&self->extension_context->extension_threads,
451+
iter) {
452+
ten_extension_thread_t *extension_thread =
453+
ten_ptr_listnode_get(iter.node);
454+
TEN_ASSERT(
455+
extension_thread &&
456+
// TEN_NOLINTNEXTLINE(thread-check)
457+
// thread-check: We are in the engine thread, _not_ in the
458+
// extension thread. However, before the engine is closed,
459+
// the pointer of the extension group and the pointer of
460+
// the extension thread will not be changed, and the
461+
// closing of the entire engine must start from the
462+
// engine, so the execution to this position means that
463+
// the engine has not been closed, so there will be no
464+
// thread safety issue.
465+
ten_extension_thread_check_integrity(extension_thread,
466+
false),
467+
"Should not happen.");
468+
469+
ten_extension_group_t *extension_group =
470+
extension_thread->extension_group;
471+
472+
if (ten_string_is_equal_c_str(&extension_group->name,
473+
extension_group_name)) {
474+
// Find the correct extension thread, ask it to handle the
475+
// message.
476+
found = true;
477+
ten_engine_post_msg_to_extension_thread(self, extension_thread,
478+
msg);
479+
break;
480+
}
450481
}
451482
}
452483

@@ -457,8 +488,7 @@ bool ten_engine_dispatch_msg(ten_engine_t *self, ten_shared_ptr_t *msg) {
457488

458489
if (ten_msg_is_cmd(msg)) {
459490
ten_shared_ptr_t *cmd_result =
460-
ten_extension_group_create_cmd_result_for_invalid_dest(
461-
msg, &dest_loc->extension_group_name);
491+
ten_engine_create_cmd_result_for_invalid_dest(msg);
462492

463493
ten_engine_dispatch_msg(self, cmd_result);
464494

0 commit comments

Comments
 (0)