connector-runtime
GitHubApache Iggy Connectors 运行时修改指南,涵盖插件加载、FFI 通信、生命周期管理及配置提供等核心逻辑,旨在确保宿主进程稳定运行。
Trigger Scenarios
Install
npx skills add apache/iggy --skill connector-runtime -g -y
SKILL.md
Frontmatter
{
"name": "connector-runtime",
"description": "Modify the Apache Iggy Connectors runtime (`core\/connectors\/runtime\/`) - the host process that loads plugins, manages lifecycle, and bridges Apache Iggy streams to plugins via FFI. Use when changing plugin loading, the FFI dispatch, the sink\/source manager, state storage, config providers, the HTTP control API, metrics, or transforms wiring. NOT for plugin code."
}
Modifying the Apache Iggy Connectors Runtime
Runtime is the host. Loads .so/.dylib/.dll plugins, drives their
lifecycle, ferries messages between Apache Iggy and plugins, exposes
/stats, /metrics, control HTTP API. Bugs here affect every plugin.
Universal connector rules (benchmark, SecretString, verbose, drop accounting, exemplar patterns) live in connectors-overview. This skill covers runtime internals only.
Contents
- STOP and ask the user before
- File map
- Architecture invariants
- State storage (
state.rs) - Source forwarding loop
- Sink consumption loop
- Logging format
- The
verboseflag - Config provider rules
- Error categorization
- Metrics (
metrics.rs) - Drop accounting (wired sites)
- Hard rules
- Common pitfalls
- Before declaring done
STOP and ask the user before
- Changing FFI
#[repr(C)]layouts (SourceApi,SinkApi,RawMessages, headers) - existing plugin.sos misalign. - Modifying
state.rssave protocol (atomic tmp + rename + fsync ordering) - corruption risk. - Renaming the default consumer group
iggy-connect-sink-{key}- operators hold offsets there. - Hot-reloading or unloading a
.so- mid-FFI tasks would segfault. Architectural change required. - Changing the SerDe split (postcard for FFI payloads, serde_json for configs, rmp_serde for state) - downstream incompat.
File map
runtime/src/
├── main.rs Entry, plugin path resolution, SourceApi/SinkApi FFI structs
├── sink.rs Sink lifecycle, Iggy consumer wiring, FFI consume calls
├── source.rs Source lifecycle, flume forwarding, state save loop
├── stream.rs Stream + consumer/producer setup
├── transform.rs Loads transforms from config, applies them in chain
├── state.rs FileStateProvider, atomic ConnectorState file I/O
├── context.rs Per-instance context (Iggy client, metrics, config)
├── error.rs RuntimeError with as_code() for HTTP responses
├── log.rs init_logging (text/JSON x telemetry-on/off matrix) + LOG_CALLBACK
├── benchmark.rs emit_sink_event / emit_source_event + as_micros
├── metrics.rs Prometheus families + gauges + stage histograms + label caches
├── stats.rs /stats endpoint payload assembly
├── manager/{mod,sink,source}.rs SinkManager / SourceManager, status, restart_guard
├── configs/
│ ├── runtime.rs RuntimeConfig + LoggingConfig + LogFormat
│ ├── connectors.rs ConnectorConfig, SinkConfig, SourceConfig (verbose + benchmark)
│ └── connectors/{local,http}_provider.rs
└── api/ HTTP control + observability endpoints
Architecture invariants
Load-bearing assumptions. Breaking them silently corrupts data or hangs.
Plugin instance identity
- Each instance gets a globally unique
plugin_id: u32fromPLUGIN_ID: AtomicU32inmain.rs. Never reused. - Same
.somay load many instances. each gets a freshplugin_idbut shares theContainer(dlopen handle) viaArc(manager/{sink,source}.rs). - All FFI calls keyed by
plugin_id. Mixing IDs = data to the wrong plugin.
FFI pointer lifetimes
- Config and state are
(*const u8, usize). Valid for the call duration only. Plugin must copy if it needs the bytes past the call. SDK macros do this correctly. don't bypass. LogCallbacklifetime spans the whole process - kept static inlog.rs. Don't make it dynamic.
Container ownership
dlopen2::wrapper::Container<{Sink,Source}Api>(defined inmain.rs) is shared viaArcacross all instances of the same.so.- Arc must outlive every spawned task that calls into the plugin. Manager holds the Arc in
{Sink,Source}Detailsfor the connector lifetime. - Unloading a
.sowhile a task is mid-FFI-call would segfault. Runtime does not unload. Hot reload needs a rearchitect.
Plugin manager state
SinkManager/SourceManageruseDashMap<String, Arc<Mutex<Details>>>. Key = TOMLkeyfield.- Status transitions (
Starting → Running → Stopped/Error) update running counters only on transitions to/fromRunning. SeeSinkManager::update_status. restart_guard: Arc<Mutex<()>>prevents concurrent restart of the same connector.restart_connector()usestry_lock()- returns OK without restart if busy. Intentional anti-thundering-herd.
Serialization split
- FFI message payloads (
TopicMetadata,MessagesMetadata,RawMessages, headers):postcard(compact, stable wire). - Connector configs passed to plugin:
serde_json(TOML in, JSON over FFI - human-editable). ConnectorStatebytes inside plugins:rmp_serde(MessagePack - compact, opaque to runtime).
Don't mix.
State storage (state.rs)
- File path:
{state_path}/source_{connector_key}.state. Mode0o600on Unix. save()is crash-atomic.rename(2)is atomic in the namespace (no observer sees a half-renamed file). the precedingsync_dataon the tmp file plus the post-rename parent-dirsync_allare what make a crash leave either the old or new content - never truncated. The parent-dir fsync failure is propagated asCannotWriteStateFile(not swallowed) so a lost rename surfaces instead of silently restarting the source from scratch. AMutex<()>serializes concurrent saves on the same provider.- Save after every successful Apache Iggy send. Save failure logs + continues. next batch retries.
load()returnsOk(None)for missing or empty files. After aNotFoundread, re-stats the parent directory: missing parent ->Err(CannotOpenStateFile)so a broken state path fails at init rather than masquerading as "fresh start".- Sinks have no state - only sources.
Source forwarding loop
iggy_source_handle(id, send_callback)- plugin registers itself.- Plugin polls + invokes
send_callback(plugin_id, ptr, len). - Callback runs in the SDK macro's spawned async task. Pushes postcard
ProducedMessagesinto aflumechannel keyed byplugin_idinSOURCE_SENDERS: Lazy<DashMap<u32, SourceSenderEntry>>(pub(crate)).SourceSenderEntrywraps the sender + a pre-extracted ownedCounter(theerrorsseries,Arc<AtomicU64>inside). The FFI callback bumps errors on deserialize or channel-closed failure with one relaxed atomic - noFamilylookup, noArc<Metrics>handle. source_forwarding_looppulls from the channel, deserializes, applies transforms, encodes viaStreamEncoder, sends to Iggy producer.- On success, save returned
ConnectorStateviaFileStateProvider.
Shutdown ordering (manager/source.rs::stop_connector):
- Call
iggy_source_closeFIRST. It blocks until the plugin's polling task stops, so no new send callbacks fire after it returns. cleanup_sender(plugin_id)NEXT - dropping the channel sender makes the forwarding task'srecv_async()resolve withDisconnectedand exit cleanly, instead of blocking until the abort timeout.- Finally await spawned handlers with
tokio::time::timeout. On timeout,handle.abort()+ drain - prevents leaked tasks colliding with the nextstart_connector(a latefile.save()could otherwise race the new instance). The silent-drop branch inhandle_produced_messagesonly covers the window between close and cleanup.
Gotchas:
SOURCE_SENDERSmust be cleaned up on connector close or memory leaks (channel + task).spawn_source_handlerwraps the outeriggy_source_handle(id, callback)FFI call intokio::task::spawn_blocking(). That call returns quickly - the SDK macro internallyruntime.spawns an asynchandle_messagestask and returns.send_callbackinvocations come from that async task, not fromspawn_blocking. A long-running synchronous poll inside the plugin would block one Tokio worker. async polls don't.- No timeout on the registration call - a plugin whose
iggy_source_handlenever returns stalls one blocking worker for the process lifetime.
Sink consumption loop
Per [[streams]] entry per sink:
- Build / ensure consumer group
iggy-connect-sink-{key}(connectmatches the docker image nameapache/iggy-connect- seeruntime/src/sink.rs::default_consumer_group). Overridable via the stream config'sconsumer_group. - Spawn one task per topic (
spawn_consume_tasks). - Poll Iggy → batch messages (default
batch_length = 1000,poll_interval = 5ms). Aconsumer.next()Err(transport/decode at the Iggy client boundary) bumpserrorsandcontinues - offset already auto-committed viaAutoCommit::When(AutoCommitWhen::PollingMessages), message effectively dropped. - Decode via
Schema::decoder(). Decode failure logs + bumpserrors.messages_filteredis reserved for the transform filter contract (Ok(None)). - Apply transforms (chain). A transform
Erris logged, bumpserrors, and drops only that message (drop-and-continue, mirroring the source) - one bad payload never kills the batch.Ok(None)filters the message - bumpsmessages_filtered{connector_type="sink"}. - Postcard-encode batch as
RawMessages. Headers as a separate postcard blob. Per-message failures (missing field, payload conversion, header serialization) bumperrors+ skip. Batch-level failures (postcard serialization of metadata /RawMessages) propagateErr->spawn_consume_taskswrapper bumpserrors+set_error. The total histogram and benchmark emit still fire for the failed batch before theErrpropagates. - Call
iggy_sink_consume(id, topic_meta, messages_meta, messages). - Non-zero return → log +
errorsincrement.
Per-message counters (decode errors, transform errors, filters, field/serialize drops) are accumulated and flushed once per batch via inc_errors_by_with_labels / inc_messages_filtered_with_labels, not one Family lookup per message.
Stages (both labels snake_case): decode (schema decode loop), prepare (transform + serialize = total - decode - ffi), ffi (plugin consume call), total. Same decode / prepare meaning as the source side.
Gotchas:
- Batch flushes on
current_offset != message_offset(gap) OR batch full - higher latency for low-volume topics. - Auto-commit happens on poll - idempotency at the sink protects against re-delivery on crash before plugin write.
Logging format
LoggingConfig in configs/runtime.rs exposes format: LogFormat (Text default, Json), env-addressable via IGGY_CONNECTORS_LOGGING_FORMAT=json. log::init_logging matches on (telemetry.enabled, format) and installs the right fmt::layer()/fmt::layer().json() + OpenTelemetry layer. OTel pipeline untouched by format - only the stdout layer switches.
fmt::layer() and fmt::layer().json() are different concrete types, so the stdout layer is built once as a Box<dyn Layer<_>> (via .boxed()) by matching on format, then a single 2-arm branch on telemetry.enabled attaches the OTel layers. When extending, keep the boxed-layer + 2-arm shape rather than re-expanding into a 4-way match.
The verbose flag
SinkConfig and SourceConfig in configs/connectors.rs carry verbose: bool (default false). Runtime threads it to spawn_consume_tasks / source equivalent to gate per-batch log promotion:
if verbose {
info!("Processing {messages_count} messages for sink connector with ID: {plugin_id}");
} else {
debug!("Processing {messages_count} messages for sink connector with ID: {plugin_id}");
}
New per-batch logging follows the same pattern. Default to debug!, upgrade to info! when verbose. No third level.
Config provider rules
Local (configs/connectors/local_provider.rs)
- Discovery walk reads
*.tomlfromconfig_dir. Skips hidden files (.) andCargo.toml. - Filename:
{key}_{type}[_v{N}].toml(e.g.,postgres_sink_v2.toml). - One TOML deserialized into
SinkConfigorSourceConfig. Grouped bykey. Highestversionwins unless.active_versions.tomloverrides. .active_versions.tomlis NOT read via the discovery walk (hidden). Read via direct path throughLocalConnectorsConfigProvider::active_versions_file_path.
HTTP (configs/connectors/http_provider.rs)
reqwest_middleware+RetryTransientMiddleware. Retries 5xx + connection errors. Never 4xx (PermanentHttpErrorconvention).- Default URL templates documented in
runtime/README.md. Configurable via TOML.
Env-var overrides via ConfigEnv derive
SinkConfig, SourceConfig, and inner structs derive ConfigEnv (configs_derive::ConfigEnv). Generates env-var addressability as IGGY_CONNECTORS_<TYPE>_<KEY>_<FIELD> for primitive fields. Used heavily by integration tests to inject testcontainer ports - see core/integration/tests/connectors/fixtures/postgres/container.rs for env-var constants. Mark new compound fields #[config_env(skip)], leaf primitives #[config_env(leaf)].
Versioning
- Configs carry
version: u64. Local provider auto-increments if absent. HTTP provider trusts the server. - Restart on version change uses
restart_guard.
Error categorization
RuntimeError (error.rs) carries as_code() for HTTP API responses.
| Class | Fatal? | Example |
|---|---|---|
| Config load | yes (process exit) | Bad TOML, missing required field |
| Iggy client init | yes | Auth failure at startup |
| State dir create | yes | Permission denied |
| Plugin .so resolve | per-plugin (FailedPlugin) |
Missing file |
| Plugin open FFI | per-plugin (status = Error) | Plugin returned non-zero |
| Message decode | per-message (skip) | Bad protobuf bytes |
| Iggy send | per-batch (metric) | Network blip |
| State save | per-batch (log) | Disk full |
Fatal errors propagate to main and exit. Per-connector / per-message errors are isolated.
Metrics (metrics.rs)
All families labeled by connector_key + connector_type (histogram adds stage):
- Counters:
iggy_connector_messages_{produced,sent,consumed,processed,filtered}_totalandiggy_connector_errors_total. These are the rendered names; each is registered without the_total, which the OpenMetrics encoder appends.messages_filtered_total- intentional drops via transformOk(None).errors_total- unexpected drops (decode/encode/build failure, missing field, ...) + batch-level failures.
- Histograms:
iggy_connector_stage_duration_seconds{stage}(snake_case stage labels -prepare,ffi,decode,iggy_send,state_save,total). BucketsSTAGE_BUCKETS_SECONDS. Always populated regardless of any flag. Scraped at/metricswhen[http.metrics] enabled = true. - Gauges:
iggy_connectors_{sources,sinks}_{total,running}.
/stats JSON surface mirrors counters per-connector via ConnectorStats (sdk/api.rs): messages_filtered, errors, kind-specific counters.
When adding a metric:
- Add family to
Metricsstruct +init, register with name + help text. Never end aCounterfamily's registered name in_total: the encoder appends it and the series renders_total_total. Gauges get no suffix, so a gauge name may end in_totalliterally. - New label sets define
EncodeLabelSetstruct + label enum (hand-implEncodeLabelValuefor snake_case values - the derive emits PascalCase). - Histograms: pass
fn() -> HistogramtoFamily::new_with_constructor. - Add unit tests under
mod testswithgiven_*_when_*_should_*BDD names.
Drop accounting (wired sites)
Per-message drops in the batch loops are counted into a local u64 and flushed once after the loop via inc_errors_by_with_labels / inc_messages_filtered_with_labels - one Family lookup per batch, not per message. One-shot drops outside a loop (e.g. consumer.next() Err) still call inc_errors_with_labels directly. Hot path uses pre-built SinkLabels/SourceLabels. &str-based wrappers are #[cfg(test)]-only.
sink.rs::consume_messages-consumer.next()Errsink.rs::process_messages- decode, transform Err (drop-and-continue), missing required fields, payload conversion, header serialization (accumulated, flushed once per batch)sink.rs::spawn_consume_taskstask wrapper - bumps once onconsume_messagesErrsource.rs::source_forwarding_loop- payload decode, prepare (transform/encode) failure, Iggy send Err, state save Errsource.rs::process_messages- transform Err (logs + bumpserrors+ continue. does NOT propagate, so one bad payload doesn't flip the connector to permanent ERROR), transform encode failure,build_iggy_messagefailuresource.rs::handle_produced_messages- postcard deserialize failure,sender.sendchannel-closed
Filter case bumps messages_filtered via inc_messages_filtered_with_labels. Adding a new drop path: mirror this pattern.
Hard rules
- Never block the executor. All I/O async.
spawn_blockingonly for FFI registration (already in place). - Plugin ID counter is monotonic. No reset, no reuse.
- FFI return codes:
0success, non-zero failure. - Don't add static mutable state beyond
LOG_CALLBACK,PLUGIN_ID,SOURCE_SENDERS. - Pair
cleanup_sender(id)with shutdown for sources (avoid flume leak). Order: close FFI -> cleanup sender -> drain/abort tasks. - Restart uses
restart_guard.try_lock()- no thundering-herd regression. - No timeouts on plugin FFI calls without a kill-task strategy. A timeout that returns from the runtime but leaves the plugin running has the worst of both worlds.
Common pitfalls
- Mutating
INSTANCES(in SDK macro) from runtime side - impossible by design. only the macro touches it. - Calling
iggy_*_openwith a duplicate ID - returns-1. Means runtime forgot toiggy_*_closefirst. - Holding a
DashMapshard guard across.await- lockups. Pattern:.get().clone(), drop guard, await. - Adding fields to
#[repr(C)]types without an SDK version bump - existing plugins misalign onpostcard::from_bytes. - Changing the default consumer group name without coordinating with operators - they have offsets stored there.
Before declaring done
cargo fmt --all
cargo sort --no-format --workspace
cargo clippy -p iggy-connectors --all-targets -- -D warnings
cargo test -p iggy-connectors
# Sanity-build in-tree plugins:
cargo build -p iggy_connector_stdout_sink -p iggy_connector_random_source
# Runtime-focused integration tests:
cargo test -p integration -- connectors::runtime::
# Smoke-test with example config:
IGGY_CONNECTORS_CONFIG_PATH=core/connectors/runtime/example_config/config.toml \
cargo run --bin iggy-connectors
Update runtime/README.md if endpoints, env vars, or config schema change.
Discussion / help: see AGENTS.md.
Version History
- 3cd0860 Current 2026-08-20 14:49


