forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogTestUtils.scala
295 lines (263 loc) · 13.8 KB
/
LogTestUtils.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kafka.log
import kafka.log.remote.RemoteLogManager
import java.io.File
import java.util.Properties
import kafka.server.BrokerTopicStats
import kafka.utils.TestUtils
import org.apache.kafka.common.Uuid
import org.apache.kafka.common.record.{CompressionType, ControlRecordType, EndTransactionMarker, FileRecords, MemoryRecords, RecordBatch, SimpleRecord}
import org.apache.kafka.common.utils.{Time, Utils}
import org.junit.jupiter.api.Assertions.{assertEquals, assertFalse}
import java.nio.file.Files
import java.util.concurrent.{ConcurrentHashMap, ConcurrentMap}
import org.apache.kafka.common.config.TopicConfig
import org.apache.kafka.server.util.Scheduler
import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile
import org.apache.kafka.storage.internals.log.{AbortedTxn, AppendOrigin, FetchDataInfo, FetchIsolation, LazyIndex, LogAppendInfo, LogConfig, LogDirFailureChannel, LogFileUtils, LogOffsetsListener, ProducerStateManager, ProducerStateManagerConfig, TransactionIndex}
import scala.jdk.CollectionConverters._
object LogTestUtils {
/**
* Create a segment with the given base offset
*/
def createSegment(offset: Long,
logDir: File,
indexIntervalBytes: Int = 10,
time: Time = Time.SYSTEM): LogSegment = {
val ms = FileRecords.open(UnifiedLog.logFile(logDir, offset))
val idx = LazyIndex.forOffset(UnifiedLog.offsetIndexFile(logDir, offset), offset, 1000)
val timeIdx = LazyIndex.forTime(UnifiedLog.timeIndexFile(logDir, offset), offset, 1500)
val txnIndex = new TransactionIndex(offset, UnifiedLog.transactionIndexFile(logDir, offset))
new LogSegment(ms, idx, timeIdx, txnIndex, offset, indexIntervalBytes, 0, time)
}
def createLogConfig(segmentMs: Long = LogConfig.DEFAULT_SEGMENT_MS,
segmentBytes: Int = LogConfig.DEFAULT_SEGMENT_BYTES,
retentionMs: Long = LogConfig.DEFAULT_RETENTION_MS,
retentionBytes: Long = LogConfig.DEFAULT_RETENTION_BYTES,
segmentJitterMs: Long = LogConfig.DEFAULT_SEGMENT_JITTER_MS,
cleanupPolicy: String = LogConfig.DEFAULT_CLEANUP_POLICY,
maxMessageBytes: Int = LogConfig.DEFAULT_MAX_MESSAGE_BYTES,
indexIntervalBytes: Int = LogConfig.DEFAULT_INDEX_INTERVAL_BYTES,
segmentIndexBytes: Int = LogConfig.DEFAULT_SEGMENT_INDEX_BYTES,
fileDeleteDelayMs: Long = LogConfig.DEFAULT_FILE_DELETE_DELAY_MS,
remoteLogStorageEnable: Boolean = LogConfig.DEFAULT_REMOTE_STORAGE_ENABLE): LogConfig = {
val logProps = new Properties()
logProps.put(TopicConfig.SEGMENT_MS_CONFIG, segmentMs: java.lang.Long)
logProps.put(TopicConfig.SEGMENT_BYTES_CONFIG, segmentBytes: Integer)
logProps.put(TopicConfig.RETENTION_MS_CONFIG, retentionMs: java.lang.Long)
logProps.put(TopicConfig.RETENTION_BYTES_CONFIG, retentionBytes: java.lang.Long)
logProps.put(TopicConfig.SEGMENT_JITTER_MS_CONFIG, segmentJitterMs: java.lang.Long)
logProps.put(TopicConfig.CLEANUP_POLICY_CONFIG, cleanupPolicy)
logProps.put(TopicConfig.MAX_MESSAGE_BYTES_CONFIG, maxMessageBytes: Integer)
logProps.put(TopicConfig.INDEX_INTERVAL_BYTES_CONFIG, indexIntervalBytes: Integer)
logProps.put(TopicConfig.SEGMENT_INDEX_BYTES_CONFIG, segmentIndexBytes: Integer)
logProps.put(TopicConfig.FILE_DELETE_DELAY_MS_CONFIG, fileDeleteDelayMs: java.lang.Long)
logProps.put(TopicConfig.REMOTE_LOG_STORAGE_ENABLE_CONFIG, remoteLogStorageEnable: java.lang.Boolean)
new LogConfig(logProps)
}
def createLog(dir: File,
config: LogConfig,
brokerTopicStats: BrokerTopicStats,
scheduler: Scheduler,
time: Time,
logStartOffset: Long = 0L,
recoveryPoint: Long = 0L,
maxTransactionTimeoutMs: Int = 5 * 60 * 1000,
producerStateManagerConfig: ProducerStateManagerConfig = new ProducerStateManagerConfig(kafka.server.Defaults.ProducerIdExpirationMs),
producerIdExpirationCheckIntervalMs: Int = kafka.server.Defaults.ProducerIdExpirationCheckIntervalMs,
lastShutdownClean: Boolean = true,
topicId: Option[Uuid] = None,
keepPartitionMetadataFile: Boolean = true,
numRemainingSegments: ConcurrentMap[String, Int] = new ConcurrentHashMap[String, Int],
remoteStorageSystemEnable: Boolean = false,
remoteLogManager: Option[RemoteLogManager] = None,
logOffsetsListener: LogOffsetsListener = LogOffsetsListener.NO_OP_OFFSETS_LISTENER): UnifiedLog = {
UnifiedLog(
dir = dir,
config = config,
logStartOffset = logStartOffset,
recoveryPoint = recoveryPoint,
scheduler = scheduler,
brokerTopicStats = brokerTopicStats,
time = time,
maxTransactionTimeoutMs = maxTransactionTimeoutMs,
producerStateManagerConfig = producerStateManagerConfig,
producerIdExpirationCheckIntervalMs = producerIdExpirationCheckIntervalMs,
logDirFailureChannel = new LogDirFailureChannel(10),
lastShutdownClean = lastShutdownClean,
topicId = topicId,
keepPartitionMetadataFile = keepPartitionMetadataFile,
numRemainingSegments = numRemainingSegments,
remoteStorageSystemEnable = remoteStorageSystemEnable,
logOffsetsListener = logOffsetsListener
)
}
/**
* Check if the given log contains any segment with records that cause offset overflow.
* @param log Log to check
* @return true if log contains at least one segment with offset overflow; false otherwise
*/
def hasOffsetOverflow(log: UnifiedLog): Boolean = firstOverflowSegment(log).isDefined
def firstOverflowSegment(log: UnifiedLog): Option[LogSegment] = {
def hasOverflow(baseOffset: Long, batch: RecordBatch): Boolean =
batch.lastOffset > baseOffset + Int.MaxValue || batch.baseOffset < baseOffset
for (segment <- log.logSegments) {
val overflowBatch = segment.log.batches.asScala.find(batch => hasOverflow(segment.baseOffset, batch))
if (overflowBatch.isDefined)
return Some(segment)
}
None
}
def rawSegment(logDir: File, baseOffset: Long): FileRecords =
FileRecords.open(UnifiedLog.logFile(logDir, baseOffset))
/**
* Initialize the given log directory with a set of segments, one of which will have an
* offset which overflows the segment
*/
def initializeLogDirWithOverflowedSegment(logDir: File): Unit = {
def writeSampleBatches(baseOffset: Long, segment: FileRecords): Long = {
def record(offset: Long) = {
val data = offset.toString.getBytes
new SimpleRecord(data, data)
}
segment.append(MemoryRecords.withRecords(baseOffset, CompressionType.NONE, 0,
record(baseOffset)))
segment.append(MemoryRecords.withRecords(baseOffset + 1, CompressionType.NONE, 0,
record(baseOffset + 1),
record(baseOffset + 2)))
segment.append(MemoryRecords.withRecords(baseOffset + Int.MaxValue - 1, CompressionType.NONE, 0,
record(baseOffset + Int.MaxValue - 1)))
// Need to create the offset files explicitly to avoid triggering segment recovery to truncate segment.
Files.createFile(UnifiedLog.offsetIndexFile(logDir, baseOffset).toPath)
Files.createFile(UnifiedLog.timeIndexFile(logDir, baseOffset).toPath)
baseOffset + Int.MaxValue
}
def writeNormalSegment(baseOffset: Long): Long = {
val segment = rawSegment(logDir, baseOffset)
try writeSampleBatches(baseOffset, segment)
finally segment.close()
}
def writeOverflowSegment(baseOffset: Long): Long = {
val segment = rawSegment(logDir, baseOffset)
try {
val nextOffset = writeSampleBatches(baseOffset, segment)
writeSampleBatches(nextOffset, segment)
} finally segment.close()
}
// We create three segments, the second of which contains offsets which overflow
var nextOffset = 0L
nextOffset = writeNormalSegment(nextOffset)
nextOffset = writeOverflowSegment(nextOffset)
writeNormalSegment(nextOffset)
}
/* extract all the keys from a log */
def keysInLog(log: UnifiedLog): Iterable[Long] = {
for (logSegment <- log.logSegments;
batch <- logSegment.log.batches.asScala if !batch.isControlBatch;
record <- batch.asScala if record.hasValue && record.hasKey)
yield TestUtils.readString(record.key).toLong
}
def recoverAndCheck(logDir: File, config: LogConfig, expectedKeys: Iterable[Long], brokerTopicStats: BrokerTopicStats, time: Time, scheduler: Scheduler): UnifiedLog = {
// Recover log file and check that after recovery, keys are as expected
// and all temporary files have been deleted
val recoveredLog = createLog(logDir, config, brokerTopicStats, scheduler, time, lastShutdownClean = false)
time.sleep(config.fileDeleteDelayMs + 1)
for (file <- logDir.listFiles) {
assertFalse(file.getName.endsWith(LogFileUtils.DELETED_FILE_SUFFIX), "Unexpected .deleted file after recovery")
assertFalse(file.getName.endsWith(UnifiedLog.CleanedFileSuffix), "Unexpected .cleaned file after recovery")
assertFalse(file.getName.endsWith(UnifiedLog.SwapFileSuffix), "Unexpected .swap file after recovery")
}
assertEquals(expectedKeys, keysInLog(recoveredLog))
assertFalse(hasOffsetOverflow(recoveredLog))
recoveredLog
}
def appendEndTxnMarkerAsLeader(log: UnifiedLog,
producerId: Long,
producerEpoch: Short,
controlType: ControlRecordType,
timestamp: Long,
coordinatorEpoch: Int = 0,
leaderEpoch: Int = 0): LogAppendInfo = {
val records = endTxnRecords(controlType, producerId, producerEpoch,
coordinatorEpoch = coordinatorEpoch, timestamp = timestamp)
log.appendAsLeader(records, origin = AppendOrigin.COORDINATOR, leaderEpoch = leaderEpoch)
}
private def endTxnRecords(controlRecordType: ControlRecordType,
producerId: Long,
epoch: Short,
offset: Long = 0L,
coordinatorEpoch: Int,
partitionLeaderEpoch: Int = 0,
timestamp: Long): MemoryRecords = {
val marker = new EndTransactionMarker(controlRecordType, coordinatorEpoch)
MemoryRecords.withEndTransactionMarker(offset, timestamp, partitionLeaderEpoch, producerId, epoch, marker)
}
def readLog(log: UnifiedLog,
startOffset: Long,
maxLength: Int,
isolation: FetchIsolation = FetchIsolation.LOG_END,
minOneMessage: Boolean = true): FetchDataInfo = {
log.read(startOffset, maxLength, isolation, minOneMessage)
}
def allAbortedTransactions(log: UnifiedLog): Iterable[AbortedTxn] = log.logSegments.flatMap(_.txnIndex.allAbortedTxns.asScala)
def deleteProducerSnapshotFiles(logDir: File): Unit = {
val files = logDir.listFiles.filter(f => f.isFile && f.getName.endsWith(LogFileUtils.PRODUCER_SNAPSHOT_FILE_SUFFIX))
files.foreach(Utils.delete)
}
def listProducerSnapshotOffsets(logDir: File): Seq[Long] =
ProducerStateManager.listSnapshotFiles(logDir).asScala.map(_.offset).sorted.toSeq
def assertLeaderEpochCacheEmpty(log: UnifiedLog): Unit = {
assertEquals(None, log.leaderEpochCache)
assertEquals(None, log.latestEpoch)
assertFalse(LeaderEpochCheckpointFile.newFile(log.dir).exists())
}
def appendNonTransactionalAsLeader(log: UnifiedLog, numRecords: Int): Unit = {
val simpleRecords = (0 until numRecords).map { seq =>
new SimpleRecord(s"$seq".getBytes)
}
val records = MemoryRecords.withRecords(CompressionType.NONE, simpleRecords: _*)
log.appendAsLeader(records, leaderEpoch = 0)
}
def appendTransactionalAsLeader(log: UnifiedLog,
producerId: Long,
producerEpoch: Short,
time: Time): Int => Unit = {
appendIdempotentAsLeader(log, producerId, producerEpoch, time, isTransactional = true)
}
def appendIdempotentAsLeader(log: UnifiedLog,
producerId: Long,
producerEpoch: Short,
time: Time,
isTransactional: Boolean = false): Int => Unit = {
var sequence = 0
numRecords: Int => {
val simpleRecords = (sequence until sequence + numRecords).map { seq =>
new SimpleRecord(time.milliseconds(), s"$seq".getBytes)
}
val records = if (isTransactional) {
MemoryRecords.withTransactionalRecords(CompressionType.NONE, producerId,
producerEpoch, sequence, simpleRecords: _*)
} else {
MemoryRecords.withIdempotentRecords(CompressionType.NONE, producerId,
producerEpoch, sequence, simpleRecords: _*)
}
log.appendAsLeader(records, leaderEpoch = 0)
sequence += numRecords
}
}
}