Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports user defined consumer groups #906

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions rocketmq-spark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<rocketmq.version>4.9.4</rocketmq.version>
<spark.version>2.3.0</spark.version>
<scala.version>2.11.8</scala.version>
<scala.binary.version>2.11</scala.binary.version>
<spark.version>3.3.1</spark.version>
<scala.version>2.12.10</scala.version>
<scala.binary.version>2.12</scala.binary.version>
<commons-lang.version>2.5</commons-lang.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ private class RocketMQSource(
if (content(0) == 'v') {
val indexOfNewLine = content.indexOf("\n")
if (indexOfNewLine > 0) {
val version = parseVersion(content.substring(0, indexOfNewLine), VERSION)
RocketMQSourceOffset(SerializedOffset(content.substring(indexOfNewLine + 1)))
} else {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
package org.apache.spark.sql.rocketmq

import org.apache.rocketmq.common.message.MessageQueue
import org.apache.spark.sql.connector.read.streaming.PartitionOffset
import org.apache.spark.sql.execution.streaming.{Offset, SerializedOffset}
import org.apache.spark.sql.sources.v2.reader.streaming.{PartitionOffset, Offset => OffsetV2}

/**
* An [[Offset]] for the [[RocketMQSource]]. This one tracks all partitions of subscribed topics and
* their offsets.
*/
private[rocketmq]
case class RocketMQSourceOffset(queueToOffsets: Map[MessageQueue, Long]) extends OffsetV2 {
case class RocketMQSourceOffset(queueToOffsets: Map[MessageQueue, Long]) extends Offset {
override val json = JsonUtils.partitionOffsets(queueToOffsets)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ class RocketMQSourceProvider extends DataSourceRegister
// Each running query should use its own group id. Otherwise, the query may be only assigned
// partial data since RocketMQ will assign partitions to multiple consumers having the same group
// id. Hence, we should generate a unique id for each query.
val uniqueGroupId = s"spark-rocketmq-source-${UUID.randomUUID}-${metadataPath.hashCode.toHexString}"
var uniqueGroupId = s"spark-rocketmq-source-${UUID.randomUUID}-${metadataPath.hashCode.toHexString}"

val caseInsensitiveParams = parameters.map { case (k, v) => (k.toLowerCase(Locale.ROOT), v) }

val startingStreamOffsets = RocketMQSourceProvider.getRocketMQOffsetRangeLimit(caseInsensitiveParams,
RocketMQConf.CONSUMER_OFFSET, LatestOffsetRangeLimit)

if (caseInsensitiveParams.contains(RocketMQConf.CONSUMER_GROUP)) {
uniqueGroupId = caseInsensitiveParams.get(RocketMQConf.CONSUMER_GROUP).get
}
val offsetReader = new RocketMQOffsetReader(
paramsForDriver(caseInsensitiveParams),
parameters,
Expand Down Expand Up @@ -252,10 +255,10 @@ object RocketMQSourceProvider extends Logging {
}

def paramsForDriver(specifiedRocketMQParams: Map[String, String]): ju.Map[String, String] = {
if (specifiedRocketMQParams.contains(RocketMQConf.CONSUMER_GROUP)) {
throw new IllegalArgumentException(
s"Option '${RocketMQConf.CONSUMER_GROUP}' can not be specified")
}
// if (specifiedRocketMQParams.contains(RocketMQConf.CONSUMER_GROUP)) {
// throw new IllegalArgumentException(
// s"Option '${RocketMQConf.CONSUMER_GROUP}' can not be specified")
// }
ConfigUpdater("source", specifiedRocketMQParams)
// Set to "earliest" to avoid exceptions. However, RocketMQSource will fetch the initial
// offsets by itself instead of counting on RocketMQConsumer.
Expand All @@ -268,10 +271,10 @@ object RocketMQSourceProvider extends Logging {
def paramsForExecutors(
specifiedRocketMQParams: Map[String, String],
uniqueGroupId: String): ju.Map[String, String] = {
if (specifiedRocketMQParams.contains(RocketMQConf.CONSUMER_GROUP)) {
throw new IllegalArgumentException(
s"Option '${RocketMQConf.CONSUMER_GROUP}' can not be specified")
}
// if (specifiedRocketMQParams.contains(RocketMQConf.CONSUMER_GROUP)) {
// throw new IllegalArgumentException(
// s"Option '${RocketMQConf.CONSUMER_GROUP}' can not be specified")
// }
ConfigUpdater("executor", specifiedRocketMQParams)
// So that consumers in executors do not mess with any existing group id
.set(RocketMQConf.CONSUMER_GROUP, s"$uniqueGroupId-executor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private[rocketmq] class RocketMQSourceRDD(
}
}
// Release consumer, either by removing it or indicating we're no longer using it
context.addTaskCompletionListener { _ =>
context.addTaskCompletionListener[Unit] { _ =>
underlying.closeIfNeeded()
}
underlying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class RocketMqRDD (
logDebug(s"Computing topic ${part.topic}, queueId ${part.queueId} " +
s"offsets ${part.partitionOffsetRanges.mkString(",")}")

context.addTaskCompletionListener{ context => closeIfNeeded() }
context.addTaskCompletionListener[Unit]{ context => closeIfNeeded() }


val consumer = if (useConsumerCache) {
Expand Down