SlideShare a Scribd company logo
Spark, and
Kafka timestamp offset
charsyam@naver.com
Common Way: Kafka and Spark Streaming
Server #1
Server #2
Server ...
Server ...
Log
Aggregation
Server
Syslogd
Kafka
Logstash
Spark
Streaming
Kafka Log
Kafka Log Segments
Segment is a file
If you set prealloc flag, segment
will be 1GB.
What is timestamp Index?
Fetching Kafka Logs by Timestamp.(From Kafka-0.10.0.2)
You can query from specific timestamp range of message.(very useful)
ex) From 2018-09-08 00:00:00 To 2018-09-08-23:59:59
Related KIPs
KIP-32 - Add timestamps to Kafka message : 0.10.0.0
KIP-33 - Add a time based log index : 0.10.1.0
Kafka Log Files
00000000000155593652.log Kafka Message Data File
00000000000155593652.index OffsetIndex File
00000000000155593652.timeindex TimeIndex File
There are many files for 1 segment.
.txnindex, .snapshot, .deleted, .cleaned, .swap, etc
Kafka Index Files
OffsetIndex.scala
TimeIndex.scala
There are different Index Files
When Kafka append Logs.
@nonthreadsafe
def append(largestOffset: Long,
largestTimestamp: Long,
shallowOffsetOfMaxTimestamp: Long,
records: MemoryRecords): Unit = {
……
val appendedBytes = log.append(records)
……
offsetIndex.append(largestOffset, physicalPosition)
timeIndex.maybeAppend(maxTimestampSoFar, offsetOfMaxTimestamp)
……
}
When Kafka writes Logs #1
● Using MMAP(so OS Page Cache is very important for performance)
● Offset is stored as relative offset
○ Current Offset - Base Offset
● Offset will be returned Absolute Offset
When Kafka writes Logs #2
OffsetIndex
Append
Int(4 bytes)
Offset
Int(4 bytes)
Position
When Kafka writes Logs #3
TimeIndex
Append
Long(8 bytes)
Timestamp
Int(4 bytes)
Position
How to fetch by timestamp #1
def fetchOffsetsByTimestamp(targetTimestamp: Long): Option[TimestampOffset] = {
……
val targetSeg = {
val earlierSegs = segmentsCopy.takeWhile(_.largestTimestamp < targetTimestamp)
if (earlierSegs.length < segmentsCopy.length)
Some(segmentsCopy(earlierSegs.length))
else
None
}
targetSeg.flatMap(_.findOffsetByTimestamp(targetTimestamp, logStartOffset))
}
}
How to fetch by timestamp #2
def findOffsetByTimestamp(timestamp: Long, startingOffset: Long = baseOffset): Option[TimestampOffset] = {
// Get the index entry with a timestamp less than or equal to the target timestamp
val timestampOffset = timeIndex.lookup(timestamp)
val position = offsetIndex.lookup(math.max(timestampOffset.offset, startingOffset)).position
// Search the timestamp
Option(log.searchForTimestamp(timestamp, position, startingOffset)).map { timestampAndOffset =>
TimestampOffset(timestampAndOffset.timestamp, timestampAndOffset.offset)
}
}
Using BinarySearch For Search
Simpe Question!!!
● Why offset index is needed?
● How to use binary search in Index File?
How to query by timestamp in spark
Convert timestamp to OffsetRange
Just Create KafkaRDD with OffsetRange
Create KafkaRDD with OffsetRange
KafkaUtils.createRDD[K, V](spark.sparkContext, kafkaParamsMap, offsetRanges, PreferConsistent)
Convert timestamp to OffsetRange
val consumer = createKafkaConsumer(props)
val startOffset = consumer.offsetsForTimes(topicMap)
val endOffset = consumer.offsetsForTimes(topicMap)
KafkaConsumer.offsetsForTimes
Client API
offsetsForTimes
KafkaConsumer.java
getOffsetsByTimes
Fetcher.java
retrieveOffsetsByTimes
Fetcher.java
Broker API
handleListOffsetRequest
KafkaApis.scala
Log Append Flows
Broker API
handleProduceRequest
KafkaApis.scala
appendRecords
ReplicaManager.scala
appendToLocalLog
ReplicaManager.scala
appendRecordsToLeade
r
Partition.scala
appendAsLeader
Log.scala
append
Log.scala
append
LogSegment.scala
When Segmnet is rolled?
def shouldRoll(messagesSize: Int, maxTimestampInMessages: Long, maxOffsetInMessages: Long, now: Long): Boolean = {
val reachedRollMs = timeWaitedForRoll(now, maxTimestampInMessages) > maxSegmentMs - rollJitterMs
size > maxSegmentBytes - messagesSize ||
(size > 0 && reachedRollMs) ||
offsetIndex.isFull || timeIndex.isFull || !canConvertToRelativeOffset(maxOffsetInMessages)
}
When Segmnet is rolled?
def shouldRoll(messagesSize: Int, maxTimestampInMessages: Long, maxOffsetInMessages: Long, now: Long): Boolean = {
val reachedRollMs = timeWaitedForRoll(now, maxTimestampInMessages) > maxSegmentMs - rollJitterMs
size > maxSegmentBytes - messagesSize ||
(size > 0 && reachedRollMs) ||
offsetIndex.isFull || timeIndex.isFull || !canConvertToRelativeOffset(maxOffsetInMessages)
}
1] size > maxSegmentBytes - messageSize
2] size > 0 && reachedRollMs
3] offsetIndex.isFull
4] timeIndex.isFull
5] canCovertToRelativeOffset is false
One Cent for Using Kafka Timestamp offset
● As a default, timestamp is set as sending time by client.
● So it is not a time that log is created.
○ You should specify to use timestamp as created time of log.
Thank you!
Quiz
● If timestamp is older than last timeIndex
○ How Kafka handles this?
Original
00000000317.log TimeIndex
Log1 Offset: 317
Timestamp: 10000
…...
Log100 Offset: 416
Timestamp: 20000
10000, 317
20000, 416
Append Log with old timestamp
00000000317.log TimeIndex
Log1 Offset: 317
Timestamp: 10000
…...
Log100 Offset: 416
Timestamp: 20000
10000, 317
20000, 416
Log101 Offset: 417
Timestamp: 20
Log200 Offset: 516
Timestamp: 30000
…...
30000, 516

More Related Content

PDF
Introduction to Kafka Streams
PPTX
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
PDF
Apache Auroraの始めかた
PDF
Simplify CDC Pipeline with Spark Streaming SQL and Delta Lake
PDF
End-to-End Spark/TensorFlow/PyTorch Pipelines with Databricks Delta
ODP
CKAN 技術介紹 (基礎篇)
PPTX
Apache Flink in the Cloud-Native Era
PDF
From HDFS to S3: Migrate Pinterest Apache Spark Clusters
Introduction to Kafka Streams
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
Apache Auroraの始めかた
Simplify CDC Pipeline with Spark Streaming SQL and Delta Lake
End-to-End Spark/TensorFlow/PyTorch Pipelines with Databricks Delta
CKAN 技術介紹 (基礎篇)
Apache Flink in the Cloud-Native Era
From HDFS to S3: Migrate Pinterest Apache Spark Clusters

What's hot (20)

PDF
Building a SIMD Supported Vectorized Native Engine for Spark SQL
PDF
Pyspark Tutorial | Introduction to Apache Spark with Python | PySpark Trainin...
PPTX
Apache Spark Core
PDF
Understanding and Improving Code Generation
PDF
Introduction to Apache Flink - Fast and reliable big data processing
PDF
Deep Dive into Spark SQL with Advanced Performance Tuning with Xiao Li & Wenc...
PDF
Introducing DataFrames in Spark for Large Scale Data Science
PDF
Apache kafka performance(latency)_benchmark_v0.3
PDF
Parquet performance tuning: the missing guide
PPTX
Spring boot
PDF
Fundamentals of Apache Kafka
PDF
Enable oracle database vault
PPTX
Scaling for Performance
PPTX
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
PDF
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
PDF
Getting Data In and Out of Flink - Understanding Flink and Its Connector Ecos...
PPTX
Spring Boot Tutorial
PDF
A Thorough Comparison of Delta Lake, Iceberg and Hudi
PDF
Kafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
PDF
elasticsearch-hadoopをつかってごにょごにょしてみる
Building a SIMD Supported Vectorized Native Engine for Spark SQL
Pyspark Tutorial | Introduction to Apache Spark with Python | PySpark Trainin...
Apache Spark Core
Understanding and Improving Code Generation
Introduction to Apache Flink - Fast and reliable big data processing
Deep Dive into Spark SQL with Advanced Performance Tuning with Xiao Li & Wenc...
Introducing DataFrames in Spark for Large Scale Data Science
Apache kafka performance(latency)_benchmark_v0.3
Parquet performance tuning: the missing guide
Spring boot
Fundamentals of Apache Kafka
Enable oracle database vault
Scaling for Performance
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Getting Data In and Out of Flink - Understanding Flink and Its Connector Ecos...
Spring Boot Tutorial
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Kafka Streams vs. KSQL for Stream Processing on top of Apache Kafka
elasticsearch-hadoopをつかってごにょごにょしてみる
Ad

Similar to Kafka timestamp offset (20)

PPTX
Kafka timestamp offset_final
PDF
Spark streaming + kafka 0.10
PDF
[Big Data Spain] Apache Spark Streaming + Kafka 0.10: an Integration Story
PDF
Apache Kafka - Scalable Message Processing and more!
PDF
Kafka Connect and Streams (Concepts, Architecture, Features)
PDF
Spark (Structured) Streaming vs. Kafka Streams - two stream processing platfo...
PPTX
London hug-samza
PPTX
Apache Samza: Reliable Stream Processing Atop Apache Kafka and Hadoop YARN
PPTX
Apache Spark Structured Streaming + Apache Kafka = ♡
PPTX
Westpac Bank Tech Talk 1: Dive into Apache Kafka
PDF
Apache Kafka - Scalable Message-Processing and more !
PDF
Machine Intelligence Guild_ Build ML Enhanced Event Streaming Applications wi...
PDF
Introduction to apache kafka, confluent and why they matter
PDF
Chti jug - 2018-06-26
PDF
Big Data LDN 2018: STREAMING DATA MICROSERVICES WITH AKKA STREAMS, KAFKA STRE...
PDF
Jug - ecosystem
PDF
Apache Kafka - Scalable Message Processing and more!
PPTX
Data Pipeline at Tapad
PDF
Spark (Structured) Streaming vs. Kafka Streams
PPTX
Running Presto and Spark on the Netflix Big Data Platform
Kafka timestamp offset_final
Spark streaming + kafka 0.10
[Big Data Spain] Apache Spark Streaming + Kafka 0.10: an Integration Story
Apache Kafka - Scalable Message Processing and more!
Kafka Connect and Streams (Concepts, Architecture, Features)
Spark (Structured) Streaming vs. Kafka Streams - two stream processing platfo...
London hug-samza
Apache Samza: Reliable Stream Processing Atop Apache Kafka and Hadoop YARN
Apache Spark Structured Streaming + Apache Kafka = ♡
Westpac Bank Tech Talk 1: Dive into Apache Kafka
Apache Kafka - Scalable Message-Processing and more !
Machine Intelligence Guild_ Build ML Enhanced Event Streaming Applications wi...
Introduction to apache kafka, confluent and why they matter
Chti jug - 2018-06-26
Big Data LDN 2018: STREAMING DATA MICROSERVICES WITH AKKA STREAMS, KAFKA STRE...
Jug - ecosystem
Apache Kafka - Scalable Message Processing and more!
Data Pipeline at Tapad
Spark (Structured) Streaming vs. Kafka Streams
Running Presto and Spark on the Netflix Big Data Platform
Ad

More from DaeMyung Kang (20)

PPTX
Count min sketch
PDF
PDF
Ansible
PDF
Why GUID is needed
PDF
How to use redis well
PPTX
The easiest consistent hashing
PDF
How to name a cache key
PDF
Integration between Filebeat and logstash
PDF
How to build massive service for advance
PDF
Massive service basic
PDF
Data Engineering 101
PDF
How To Become Better Engineer
PPTX
Data pipeline and data lake
PDF
Redis acl
PDF
Coffee store
PDF
Scalable webservice
PDF
Number system
PDF
webservice scaling for newbie
PDF
Internet Scale Service Arichitecture
PDF
Bloomfilter
Count min sketch
Ansible
Why GUID is needed
How to use redis well
The easiest consistent hashing
How to name a cache key
Integration between Filebeat and logstash
How to build massive service for advance
Massive service basic
Data Engineering 101
How To Become Better Engineer
Data pipeline and data lake
Redis acl
Coffee store
Scalable webservice
Number system
webservice scaling for newbie
Internet Scale Service Arichitecture
Bloomfilter

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Electronic commerce courselecture one. Pdf
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Electronic commerce courselecture one. Pdf
Teaching material agriculture food technology
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
NewMind AI Monthly Chronicles - July 2025
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Kafka timestamp offset

  • 1. Spark, and Kafka timestamp offset charsyam@naver.com
  • 2. Common Way: Kafka and Spark Streaming Server #1 Server #2 Server ... Server ... Log Aggregation Server Syslogd Kafka Logstash Spark Streaming
  • 4. Kafka Log Segments Segment is a file If you set prealloc flag, segment will be 1GB.
  • 5. What is timestamp Index? Fetching Kafka Logs by Timestamp.(From Kafka-0.10.0.2) You can query from specific timestamp range of message.(very useful) ex) From 2018-09-08 00:00:00 To 2018-09-08-23:59:59
  • 6. Related KIPs KIP-32 - Add timestamps to Kafka message : 0.10.0.0 KIP-33 - Add a time based log index : 0.10.1.0
  • 7. Kafka Log Files 00000000000155593652.log Kafka Message Data File 00000000000155593652.index OffsetIndex File 00000000000155593652.timeindex TimeIndex File There are many files for 1 segment. .txnindex, .snapshot, .deleted, .cleaned, .swap, etc
  • 9. When Kafka append Logs. @nonthreadsafe def append(largestOffset: Long, largestTimestamp: Long, shallowOffsetOfMaxTimestamp: Long, records: MemoryRecords): Unit = { …… val appendedBytes = log.append(records) …… offsetIndex.append(largestOffset, physicalPosition) timeIndex.maybeAppend(maxTimestampSoFar, offsetOfMaxTimestamp) …… }
  • 10. When Kafka writes Logs #1 ● Using MMAP(so OS Page Cache is very important for performance) ● Offset is stored as relative offset ○ Current Offset - Base Offset ● Offset will be returned Absolute Offset
  • 11. When Kafka writes Logs #2 OffsetIndex Append Int(4 bytes) Offset Int(4 bytes) Position
  • 12. When Kafka writes Logs #3 TimeIndex Append Long(8 bytes) Timestamp Int(4 bytes) Position
  • 13. How to fetch by timestamp #1 def fetchOffsetsByTimestamp(targetTimestamp: Long): Option[TimestampOffset] = { …… val targetSeg = { val earlierSegs = segmentsCopy.takeWhile(_.largestTimestamp < targetTimestamp) if (earlierSegs.length < segmentsCopy.length) Some(segmentsCopy(earlierSegs.length)) else None } targetSeg.flatMap(_.findOffsetByTimestamp(targetTimestamp, logStartOffset)) } }
  • 14. How to fetch by timestamp #2 def findOffsetByTimestamp(timestamp: Long, startingOffset: Long = baseOffset): Option[TimestampOffset] = { // Get the index entry with a timestamp less than or equal to the target timestamp val timestampOffset = timeIndex.lookup(timestamp) val position = offsetIndex.lookup(math.max(timestampOffset.offset, startingOffset)).position // Search the timestamp Option(log.searchForTimestamp(timestamp, position, startingOffset)).map { timestampAndOffset => TimestampOffset(timestampAndOffset.timestamp, timestampAndOffset.offset) } } Using BinarySearch For Search
  • 15. Simpe Question!!! ● Why offset index is needed? ● How to use binary search in Index File?
  • 16. How to query by timestamp in spark Convert timestamp to OffsetRange Just Create KafkaRDD with OffsetRange
  • 17. Create KafkaRDD with OffsetRange KafkaUtils.createRDD[K, V](spark.sparkContext, kafkaParamsMap, offsetRanges, PreferConsistent)
  • 18. Convert timestamp to OffsetRange val consumer = createKafkaConsumer(props) val startOffset = consumer.offsetsForTimes(topicMap) val endOffset = consumer.offsetsForTimes(topicMap)
  • 20. Log Append Flows Broker API handleProduceRequest KafkaApis.scala appendRecords ReplicaManager.scala appendToLocalLog ReplicaManager.scala appendRecordsToLeade r Partition.scala appendAsLeader Log.scala append Log.scala append LogSegment.scala
  • 21. When Segmnet is rolled? def shouldRoll(messagesSize: Int, maxTimestampInMessages: Long, maxOffsetInMessages: Long, now: Long): Boolean = { val reachedRollMs = timeWaitedForRoll(now, maxTimestampInMessages) > maxSegmentMs - rollJitterMs size > maxSegmentBytes - messagesSize || (size > 0 && reachedRollMs) || offsetIndex.isFull || timeIndex.isFull || !canConvertToRelativeOffset(maxOffsetInMessages) }
  • 22. When Segmnet is rolled? def shouldRoll(messagesSize: Int, maxTimestampInMessages: Long, maxOffsetInMessages: Long, now: Long): Boolean = { val reachedRollMs = timeWaitedForRoll(now, maxTimestampInMessages) > maxSegmentMs - rollJitterMs size > maxSegmentBytes - messagesSize || (size > 0 && reachedRollMs) || offsetIndex.isFull || timeIndex.isFull || !canConvertToRelativeOffset(maxOffsetInMessages) } 1] size > maxSegmentBytes - messageSize 2] size > 0 && reachedRollMs 3] offsetIndex.isFull 4] timeIndex.isFull 5] canCovertToRelativeOffset is false
  • 23. One Cent for Using Kafka Timestamp offset ● As a default, timestamp is set as sending time by client. ● So it is not a time that log is created. ○ You should specify to use timestamp as created time of log.
  • 25. Quiz ● If timestamp is older than last timeIndex ○ How Kafka handles this?
  • 26. Original 00000000317.log TimeIndex Log1 Offset: 317 Timestamp: 10000 …... Log100 Offset: 416 Timestamp: 20000 10000, 317 20000, 416
  • 27. Append Log with old timestamp 00000000317.log TimeIndex Log1 Offset: 317 Timestamp: 10000 …... Log100 Offset: 416 Timestamp: 20000 10000, 317 20000, 416 Log101 Offset: 417 Timestamp: 20 Log200 Offset: 516 Timestamp: 30000 …... 30000, 516