Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions core/connectors/runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use iggy_connector_sdk::{
StreamDecoder, StreamEncoder,
api::ConnectorStatus,
sink::ConsumeCallback,
source::{HandleCallback, SendCallback},
source::{BatchResultCallback, HandleCallback, SendCallback},
transforms::Transform,
};
use mimalloc::MiMalloc;
Expand Down Expand Up @@ -80,7 +80,8 @@ pub(crate) struct SourceApi {
state_len: usize,
log_callback: iggy_connector_sdk::LogCallback,
) -> i32,
iggy_source_handle: extern "C" fn(id: u32, callback: SendCallback) -> i32,
iggy_source_handle_v2: extern "C" fn(id: u32, callback: SendCallback) -> i32,
iggy_source_batch_result: extern "C" fn(plugin_id: u32, batch_id: u64, result: u8) -> i32,
iggy_source_close: extern "C" fn(id: u32) -> i32,
iggy_source_version: extern "C" fn() -> *const std::ffi::c_char,
}
Expand Down Expand Up @@ -185,12 +186,14 @@ async fn main() -> Result<(), RuntimeError> {
let mut source_containers_by_key: HashMap<String, Arc<Container<SourceApi>>> = HashMap::new();
for (_path, source) in sources {
let container = Arc::new(source.container);
let callback = container.iggy_source_handle;
let handle_callback = container.iggy_source_handle_v2;
let batch_result_callback = container.iggy_source_batch_result;
for plugin in &source.plugins {
source_containers_by_key.insert(plugin.key.clone(), container.clone());
}
source_wrappers.push(SourceConnectorWrapper {
callback,
handle_callback,
batch_result_callback,
plugins: source.plugins,
});
}
Expand Down Expand Up @@ -460,7 +463,8 @@ struct SourceConnectorProducer {
}

struct SourceConnectorWrapper {
callback: HandleCallback,
handle_callback: HandleCallback,
batch_result_callback: BatchResultCallback,
plugins: Vec<SourceConnectorPlugin>,
}

Expand Down
28 changes: 20 additions & 8 deletions core/connectors/runtime/src/manager/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ impl SourceManager {
}
}

pub async fn is_stopping_or_stopped(&self, key: &str) -> bool {
let Some(source) = self.sources.get(key).map(|entry| entry.value().clone()) else {
return true;
};
let source = source.lock().await;
matches!(
source.info.status,
ConnectorStatus::Stopping | ConnectorStatus::Stopped
)
}

pub async fn stop_connector_with_guard(
&self,
key: &str,
Expand Down Expand Up @@ -138,6 +149,9 @@ impl SourceManager {
.map(|e| e.value().clone())
.ok_or_else(|| RuntimeError::SourceNotFound(key.to_string()))?;

self.update_status(key, ConnectorStatus::Stopping, Some(metrics))
.await;

let (task_handles, plugin_id, container) = {
let mut details = details.lock().await;
(
Expand All @@ -147,8 +161,8 @@ impl SourceManager {
)
};

// Order: close FFI (stops callbacks) -> drop sender (unblocks
// recv_async) -> await tasks. Reversing risks an abort mid-save.
// The result callback can race with close, so the forwarding loop
// treats callback failures during Stopping as expected shutdown.
if let Some(container) = &container {
info!("Closing source connector with ID: {plugin_id} for plugin: {key}");
(container.iggy_source_close)(plugin_id);
Expand All @@ -172,12 +186,8 @@ impl SourceManager {

{
let mut details = details.lock().await;
let old_status = details.info.status;
details.info.status = ConnectorStatus::Stopped;
details.info.last_error = None;
if old_status == ConnectorStatus::Running {
metrics.decrement_sources_running();
}
}

Ok(())
Expand Down Expand Up @@ -223,7 +233,8 @@ impl SourceManager {
let (producer, encoder, transforms) =
source::setup_source_producer(key, config, iggy_client).await?;

let callback = container.iggy_source_handle;
let handle_callback = container.iggy_source_handle_v2;
let batch_result_callback = container.iggy_source_batch_result;
let handler_tasks = source::spawn_source_handler(
plugin_id,
key,
Expand All @@ -233,7 +244,8 @@ impl SourceManager {
encoder,
transforms,
state_storage,
callback,
handle_callback,
batch_result_callback,
context.clone(),
);

Expand Down
Loading