You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[SPARK-29223][SQL][SS] New option to specify timestamp on all subscribing topic-partitions in Kafka source
### What changes were proposed in this pull request?
This patch is a follow-up of SPARK-26848 (#23747). In SPARK-26848, we decided to open possibility to let end users set individual timestamp per partition. But in many cases, specifying timestamp represents the intention that we would want to go back to specific timestamp and reprocess records, which should be applied to all topics and partitions.
This patch proposes to provide a way to set a global timestamp across topic-partitions which the source is subscribing to, so that end users can set all offsets by specific timestamp easily. To provide the way to config the timestamp easier, the new options only receive "a" timestamp for start/end timestamp.
New options introduced in this PR:
* startingTimestamp
* endingTimestamp
All two options receive timestamp as string.
There're priorities for options regarding starting/ending offset as we will have three options for start offsets and another three options for end offsets. Priorities are following:
* starting offsets: startingTimestamp -> startingOffsetsByTimestamp -> startingOffsets
* ending offsets: startingTimestamp -> startingOffsetsByTimestamp -> startingOffsets
### Why are the changes needed?
Existing option to specify timestamp as offset is quite verbose if there're a lot of partitions across topics. Suppose there're 100s of partitions in a topic, the json should contain 100s of times of the same timestamp.
Also, the number of partitions can also change, which requires either:
* fixing the code if the json is statically created
* introducing the dependencies on Kafka client and deal with Kafka API on crafting json programmatically
Both approaches are even not "acceptable" if we're dealing with ad-hoc query; anyone doesn't want to write the code more complicated than the query itself. Flink [provides the option](https://ci.apache.org/projects/flink/flink-docs-release-1.13/docs/connectors/datastream/kafka/#kafka-consumers-start-position-configuration) to specify a timestamp for all topic-partitions like this PR, and even doesn't provide the option to specify the timestamp per topic-partition.
With this PR, end users are only required to provide a single timestamp value. No more complicated JSON format end users need to know about the structure.
### Does this PR introduce _any_ user-facing change?
Yes, this PR introduces two new options, described in above section.
Doc changes are following:




### How was this patch tested?
New UTs covering new functionalities. Also manually tested via simple batch & streaming queries.
Closes#32609 from HeartSaVioR/SPARK-29223-v2.
Authored-by: Jungtaek Lim <kabhwan.opensource@gmail.com>
Signed-off-by: Jungtaek Lim <kabhwan.opensource@gmail.com>
<td>none (next preference is <code>startingOffsetsByTimestamp</code>)</td>
369
+
<td>streaming and batch</td>
370
+
<td>The start point of timestamp when a query is started, a string specifying a starting timestamp for
371
+
all partitions in topics being subscribed. Please refer the details on timestamp offset options below. If Kafka doesn't return the matched offset,
372
+
the query will fail immediately to prevent unintended read from such partition. (This is a kind of limitation as of now, and will be addressed in near future.)<p/>
373
+
<p/>
374
+
Note1: <code>startingTimestamp</code> takes precedence over <code>startingOffsetsByTimestamp</code> and <code>startingOffsets</code>.<p/>
375
+
Note2: For streaming queries, this only applies when a new query is started, and that resuming will
376
+
always pick up from where the query left off. Newly discovered partitions during a query will start at
<td>none (the value of <code>startingOffsets</code> will apply)</td>
384
+
<td>none (next preference is <code>startingOffsets</code>)</td>
371
385
<td>streaming and batch</td>
372
386
<td>The start point of timestamp when a query is started, a json string specifying a starting timestamp for
373
-
each TopicPartition. The returned offset for each partition is the earliest offset whose timestamp is greater than or
374
-
equal to the given timestamp in the corresponding partition. If the matched offset doesn't exist,
387
+
each TopicPartition. Please refer the details on timestamp offset options below. If Kafka doesn't return the matched offset,
375
388
the query will fail immediately to prevent unintended read from such partition. (This is a kind of limitation as of now, and will be addressed in near future.)<p/>
376
389
<p/>
377
-
Spark simply passes the timestamp information to <code>KafkaConsumer.offsetsForTimes</code>, and doesn't interpret or reason about the value. <p/>
378
-
For more details on <code>KafkaConsumer.offsetsForTimes</code>, please refer <ahref="https://kafka.apache.org/21/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#offsetsForTimes-java.util.Map-">javadoc</a> for details.<p/>
379
-
Also the meaning of <code>timestamp</code> here can be vary according to Kafka configuration (<code>log.message.timestamp.type</code>): please refer <ahref="https://kafka.apache.org/documentation/">Kafka documentation</a> for further details.<p/>
380
-
Note: This option requires Kafka 0.10.1.0 or higher.<p/>
381
-
Note2: <code>startingOffsetsByTimestamp</code> takes precedence over <code>startingOffsets</code>.<p/>
382
-
Note3: For streaming queries, this only applies when a new query is started, and that resuming will
390
+
Note1: <code>startingOffsetsByTimestamp</code> takes precedence over <code>startingOffsets</code>.<p/>
391
+
Note2: For streaming queries, this only applies when a new query is started, and that resuming will
383
392
always pick up from where the query left off. Newly discovered partitions during a query will start at
384
393
earliest.</td>
385
394
</tr>
@@ -398,23 +407,28 @@ The following configurations are optional:
398
407
always pick up from where the query left off. Newly discovered partitions during a query will start at
399
408
earliest.</td>
400
409
</tr>
410
+
<tr>
411
+
<td>endingTimestamp</td>
412
+
<td>timestamp string e.g. "1000"</td>
413
+
<td>none (next preference is <code>endingOffsetsByTimestamp</code>)</td>
414
+
<td>batch query</td>
415
+
<td>The end point when a batch query is ended, a json string specifying an ending timestamp for
416
+
all partitions in topics being subscribed. Please refer the details on timestamp offset options below.
417
+
If Kafka doesn't return the matched offset, the offset will be set to latest.<p/>
418
+
Note: <code>endingTimestamp</code> takes precedence over <code>endingOffsetsByTimestamp</code> and <code>endingOffsets</code>.<p/>
<td>none (next preference is <code>endingOffsets</code>)</td>
407
427
<td>batch query</td>
408
428
<td>The end point when a batch query is ended, a json string specifying an ending timestamp for each TopicPartition.
409
-
The returned offset for each partition is the earliest offset whose timestamp is greater than or equal to
410
-
the given timestamp in the corresponding partition. If the matched offset doesn't exist, the offset will
411
-
be set to latest.<p/>
412
-
<p/>
413
-
Spark simply passes the timestamp information to <code>KafkaConsumer.offsetsForTimes</code>, and doesn't interpret or reason about the value. <p/>
414
-
For more details on <code>KafkaConsumer.offsetsForTimes</code>, please refer <ahref="https://kafka.apache.org/21/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#offsetsForTimes-java.util.Map-">javadoc</a> for details.<p/>
415
-
Also the meaning of <code>timestamp</code> here can be vary according to Kafka configuration (<code>log.message.timestamp.type</code>): please refer <ahref="https://kafka.apache.org/documentation/">Kafka documentation</a> for further details.<p/>
416
-
Note: This option requires Kafka 0.10.1.0 or higher.<p/>
417
-
Note2: <code>endingOffsetsByTimestamp</code> takes precedence over <code>endingOffsets</code>.
429
+
Please refer the details on timestamp offset options below. If Kafka doesn't return the matched offset,
430
+
the offset will be set to latest.<p/>
431
+
Note: <code>endingOffsetsByTimestamp</code> takes precedence over <code>endingOffsets</code>.
418
432
</td>
419
433
</tr>
420
434
<tr>
@@ -512,6 +526,17 @@ The following configurations are optional:
512
526
</tr>
513
527
</table>
514
528
529
+
### Details on timestamp offset options
530
+
531
+
The returned offset for each partition is the earliest offset whose timestamp is greater than or equal to the given timestamp in the corresponding partition.
532
+
The behavior varies across options if Kafka doesn't return the matched offset - check the description of each option.
533
+
534
+
Spark simply passes the timestamp information to <code>KafkaConsumer.offsetsForTimes</code>, and doesn't interpret or reason about the value.
535
+
For more details on <code>KafkaConsumer.offsetsForTimes</code>, please refer <ahref="http://kafka.apache.org/0101/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#offsetsForTimes(java.util.Map)">javadoc</a> for details.
536
+
Also, the meaning of <code>timestamp</code> here can be vary according to Kafka configuration (<code>log.message.timestamp.type</code>): please refer <ahref="https://kafka.apache.org/documentation/">Kafka documentation</a> for further details.
537
+
538
+
Timestamp offset options require Kafka 0.10.1.0 or higher.
539
+
515
540
### Offset fetching
516
541
517
542
In Spark 3.0 and before Spark uses <code>KafkaConsumer</code> for offset fetching which could cause infinite wait in the driver.
0 commit comments