Skip to content

Commit

Permalink
feat(connector): Add topic to mqtt additional columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Boudewijn26 committed Oct 18, 2024
1 parent c0d6af1 commit a85a7b8
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion integration_tests/mqtt/create_source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE
CREATE TABLE mqtt_source_table
(
id integer,
name varchar,
name varchar
)
WITH (
connector='mqtt',
Expand Down
4 changes: 4 additions & 0 deletions proto/plan_common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ message AdditionalCollectionName {}

message AdditionalColumnPayload {}

message AdditionalColumnTopic {}

// this type means we read all headers as a whole
message AdditionalColumnHeaders {}

Expand All @@ -281,6 +283,7 @@ message AdditionalColumn {
AdditionalTableName table_name = 10;
AdditionalCollectionName collection_name = 11;
AdditionalColumnPayload payload = 12;
AdditionalColumnTopic topic = 13;
}
}

Expand All @@ -294,4 +297,5 @@ enum AdditionalColumnType {
ADDITIONAL_COLUMN_TYPE_FILENAME = 6;
ADDITIONAL_COLUMN_TYPE_NORMAL = 7;
ADDITIONAL_COLUMN_TYPE_PAYLOAD = 8;
ADDITIONAL_COLUMN_TYPE_TOPIC = 9;
}
18 changes: 15 additions & 3 deletions src/connector/src/parser/additional_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use risingwave_pb::plan_common::{
AdditionalCollectionName, AdditionalColumn, AdditionalColumnFilename, AdditionalColumnHeader,
AdditionalColumnHeaders, AdditionalColumnKey, AdditionalColumnOffset,
AdditionalColumnPartition, AdditionalColumnPayload, AdditionalColumnTimestamp,
AdditionalDatabaseName, AdditionalSchemaName, AdditionalTableName,
AdditionalColumnTopic, AdditionalDatabaseName, AdditionalSchemaName, AdditionalTableName,
};

use crate::error::ConnectorResult;
use crate::source::cdc::MONGODB_CDC_CONNECTOR;
use crate::source::{
AZBLOB_CONNECTOR, GCS_CONNECTOR, KAFKA_CONNECTOR, KINESIS_CONNECTOR, NATS_CONNECTOR,
OPENDAL_S3_CONNECTOR, POSIX_FS_CONNECTOR, PULSAR_CONNECTOR,
AZBLOB_CONNECTOR, GCS_CONNECTOR, KAFKA_CONNECTOR, KINESIS_CONNECTOR, MQTT_CONNECTOR,
NATS_CONNECTOR, OPENDAL_S3_CONNECTOR, POSIX_FS_CONNECTOR, PULSAR_CONNECTOR,
};

// Hidden additional columns connectors which do not support `include` syntax.
Expand Down Expand Up @@ -87,6 +87,10 @@ pub static COMPATIBLE_ADDITIONAL_COLUMNS: LazyLock<HashMap<&'static str, HashSet
"collection_name",
]),
),
(
MQTT_CONNECTOR,
HashSet::from(["topic", "offset", "partition"]),
),
])
});

Expand Down Expand Up @@ -266,6 +270,14 @@ pub fn build_additional_column_desc(
)),
},
),
"topic" => ColumnDesc::named_with_additional_column(
column_name,
column_id,
DataType::Varchar,
AdditionalColumn {
column_type: Some(AdditionalColumnType::Topic(AdditionalColumnTopic {})),
},
),
_ => unreachable!(),
};

Expand Down
8 changes: 8 additions & 0 deletions src/connector/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ impl SourceStreamChunkRowWriter<'_> {
// do special logic in `KvEvent::access_field`
parse_field(desc)
}
(_, &Some(AdditionalColumnType::Topic(_))) => {
// topic is used as partition in mqtt connector
return Ok(A::output_for(
self.row_meta
.as_ref()
.map(|ele| ScalarRefImpl::Utf8(ele.split_id)),
));
}
(_, _) => {
// For normal columns, call the user provided closure.
parse_field(desc)
Expand Down
22 changes: 16 additions & 6 deletions src/connector/src/source/mqtt/enumerator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ use std::sync::atomic::AtomicBool;
use std::sync::Arc;

use async_trait::async_trait;
use risingwave_common::bail;
use rumqttc::v5::{ConnectionError, Event, Incoming};
use rumqttc::Outgoing;
use thiserror_ext::AsReport;
use tokio::sync::RwLock;

use super::source::MqttSplit;
use super::{MqttError, MqttProperties};
use crate::error::{ConnectorError, ConnectorResult};
use super::MqttProperties;
use crate::error::ConnectorResult;
use crate::source::{SourceEnumeratorContextRef, SplitEnumerator};

pub struct MqttSplitEnumerator {
#[expect(dead_code)]
topic: String,
#[expect(dead_code)]
client: rumqttc::v5::AsyncClient,
Expand Down Expand Up @@ -117,10 +119,18 @@ impl SplitEnumerator for MqttSplitEnumerator {

async fn list_splits(&mut self) -> ConnectorResult<Vec<MqttSplit>> {
if !self.connected.load(std::sync::atomic::Ordering::Relaxed) {
return Err(ConnectorError::from(MqttError(format!(
"Failed to connect to MQTT broker for topic {}",
self.topic
))));
let start = std::time::Instant::now();
loop {
if self.connected.load(std::sync::atomic::Ordering::Relaxed) {
break;
}

if start.elapsed().as_secs() > 10 {
bail!("Failed to connect to mqtt broker");
}

tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
}

let topics = self.topics.read().await;
Expand Down
3 changes: 2 additions & 1 deletion src/connector/src/source/mqtt/source/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl SplitReader for MqttSplitReader {
) -> Result<Self> {
let (client, eventloop) = properties
.common
.build_client(source_ctx.actor_id, source_ctx.fragment_id as u64)?;
.build_client(source_ctx.actor_id, source_ctx.fragment_id as u64)
.inspect_err(|e| tracing::error!("Failed to build mqtt client: {}", e.as_report()))?;

let qos = properties.common.qos();

Expand Down
1 change: 1 addition & 0 deletions src/prost/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"plan_common.AdditionalCollectionName",
"#[derive(Eq, Hash)]",
)
.type_attribute("plan_common.AdditionalColumnTopic", "#[derive(Eq, Hash)]")
.type_attribute("plan_common.AsOfJoinDesc", "#[derive(Eq, Hash)]")
.type_attribute("common.ColumnOrder", "#[derive(Eq, Hash)]")
.type_attribute("common.OrderType", "#[derive(Eq, Hash)]")
Expand Down
11 changes: 11 additions & 0 deletions src/sqlparser/tests/testdata/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@
topic = 'dummy')
FORMAT plain ENCODE bytes;
formatted_sql: CREATE TABLE t (raw BYTEA) INCLUDE header AS all_headers INCLUDE header 'foo' AS foo_bytea INCLUDE header 'foo' CHARACTER VARYING AS foo_str INCLUDE header INCLUDE header 'foo' INCLUDE header 'foo' CHARACTER VARYING INCLUDE header 'foo' BYTEA INCLUDE header 'bar' WITH (connector = 'kafka', kafka.brokers = '${RISEDEV_KAFKA_BOOTSTRAP_SERVERS}', topic = 'dummy') FORMAT PLAIN ENCODE BYTES
- input: |-
CREATE TABLE t
(raw BYTEA)
INCLUDE topic
WITH (
connector = 'mqtt',
url = 'tcp://mqtt-server',
topic = 'test',
qos = 'at_least_once')
FORMAT plain ENCODE bytes;
formatted_sql: CREATE TABLE t (raw BYTEA) INCLUDE topic WITH (connector = 'mqtt', url = 'tcp://mqtt-server', topic = 'test', qos = 'at_least_once') FORMAT PLAIN ENCODE BYTES
- input: CREATE TABLE T (v1 INT, v2 STRUCT<v1 INT, v2 INT>)
formatted_sql: CREATE TABLE T (v1 INT, v2 STRUCT<v1 INT, v2 INT>)
- input: CREATE TABLE T (v1 INT, v2 STRUCT<v1 INT, v2 INT, v3 STRUCT<v1 INT, v2 INT>>)
Expand Down

0 comments on commit a85a7b8

Please sign in to comment.