Skip to content

Commit 6774954

Browse files
committed
all test pass
1 parent d3da6ce commit 6774954

File tree

3 files changed

+30
-42
lines changed

3 files changed

+30
-42
lines changed

storage/src/test/java/org/apache/kafka/storage/internals/log/LogTestUtils.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import org.apache.kafka.common.Uuid;
2020
import org.apache.kafka.common.config.TopicConfig;
21+
import org.apache.kafka.common.record.ControlRecordType;
22+
import org.apache.kafka.common.record.EndTransactionMarker;
2123
import org.apache.kafka.common.record.FileRecords;
24+
import org.apache.kafka.common.record.MemoryRecords;
2225
import org.apache.kafka.common.utils.Time;
23-
import org.apache.kafka.coordinator.transaction.TransactionLogConfig;
26+
import org.apache.kafka.server.common.RequestLocal;
2427
import org.apache.kafka.server.config.ServerLogConfigs;
2528
import org.apache.kafka.server.util.Scheduler;
2629
import org.apache.kafka.storage.log.metrics.BrokerTopicStats;
@@ -29,7 +32,6 @@
2932
import java.io.IOException;
3033
import java.util.Optional;
3134
import java.util.Properties;
32-
import java.util.concurrent.ConcurrentHashMap;
3335
import java.util.concurrent.ConcurrentMap;
3436

3537
public class LogTestUtils {
@@ -44,27 +46,27 @@ public static LogSegment createSegment(long offset, File logDir, int indexInterv
4446
return new LogSegment(ms, idx, timeIdx, txnIndex, offset, indexIntervalBytes, 0, time);
4547
}
4648

47-
public static UnifiedLog createLog(File dir,
48-
LogConfig config,
49-
BrokerTopicStats brokerTopicStats,
50-
Scheduler scheduler,
51-
Time time) throws IOException {
52-
return createLog(
53-
dir,
54-
config,
55-
brokerTopicStats,
56-
scheduler,
57-
time,
58-
0L, // logStartOffset
59-
0L, // recoveryPoint
60-
5 * 60 * 1000, // maxTransactionTimeoutMs
61-
new ProducerStateManagerConfig(TransactionLogConfig.PRODUCER_ID_EXPIRATION_MS_DEFAULT, false),
62-
TransactionLogConfig.PRODUCER_ID_EXPIRATION_CHECK_INTERVAL_MS_DEFAULT, true, // lastShutdownClean
63-
Optional.empty(), // topicId
64-
new ConcurrentHashMap<>(), // numRemainingSegments
65-
false, // remoteStorageSystemEnable
66-
LogOffsetsListener.NO_OP_OFFSETS_LISTENER
67-
);
49+
public static LogAppendInfo appendEndTxnMarkerAsLeader(UnifiedLog log,
50+
long producerId,
51+
short producerEpoch,
52+
ControlRecordType controlType,
53+
long timestamp,
54+
int coordinatorEpoch,
55+
int leaderEpoch) {
56+
MemoryRecords records = endTxnRecords(controlType, producerId, producerEpoch, 0L, coordinatorEpoch, leaderEpoch, timestamp);
57+
58+
return log.appendAsLeader(records, leaderEpoch, AppendOrigin.COORDINATOR, RequestLocal.noCaching(), VerificationGuard.SENTINEL);
59+
}
60+
61+
public static MemoryRecords endTxnRecords(ControlRecordType controlRecordType,
62+
long producerId,
63+
short epoch,
64+
long offset,
65+
int coordinatorEpoch,
66+
int partitionLeaderEpoch,
67+
long timestamp) {
68+
EndTransactionMarker marker = new EndTransactionMarker(controlRecordType, coordinatorEpoch);
69+
return MemoryRecords.withEndTransactionMarker(offset, timestamp, partitionLeaderEpoch, producerId, epoch, marker);
6870
}
6971

7072
@SuppressWarnings("ParameterNumber")
@@ -195,7 +197,6 @@ public LogConfigBuilder withRemoteLogDeleteOnDisable(boolean remoteLogDeleteOnDi
195197
return this;
196198
}
197199

198-
// 3. 建立一個 build() 方法,它使用 builder 中設定的值來建立最終的 LogConfig 物件
199200
public LogConfig build() {
200201
Properties logProps = new Properties();
201202
logProps.put(TopicConfig.SEGMENT_MS_CONFIG, String.valueOf(segmentMs));

storage/src/test/java/org/apache/kafka/storage/internals/log/UnifiedLogTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class UnifiedLogTest {
5757
private final BrokerTopicStats brokerTopicStats = new BrokerTopicStats(false);
5858
private final MockTime mockTime = new MockTime();
5959
private final int maxTransactionTimeoutMs = 60 * 60 * 1000;
60-
private final ProducerStateManagerConfig producerStateManagerConfig = new ProducerStateManagerConfig(maxTransactionTimeoutMs, true);
60+
private final ProducerStateManagerConfig producerStateManagerConfig = new ProducerStateManagerConfig(maxTransactionTimeoutMs, false);
6161
private final List<UnifiedLog> logsToClose = new ArrayList<>();
6262

6363
private UnifiedLog log;
@@ -564,7 +564,8 @@ public void testFirstUnstableOffsetNoTransactionalData() throws IOException {
564564
@Test
565565
public void testFirstUnstableOffsetWithTransactionalData() throws IOException {
566566
LogConfig logConfig = new LogTestUtils.LogConfigBuilder()
567-
.withSegmentBytes(1024 * 1024 * 5).build();
567+
.withSegmentBytes(1024 * 1024 * 5)
568+
.build();
568569
log = createLog(logDir, logConfig);
569570

570571
long pid = 137L;
@@ -588,7 +589,8 @@ public void testFirstUnstableOffsetWithTransactionalData() throws IOException {
588589
assertEquals(Optional.of(firstAppendInfo.firstOffset()), log.firstUnstableOffset());
589590

590591
// now transaction is committed
591-
LogAppendInfo commitAppendInfo = appendEndTxnMarkerAsLeader(log, pid, epoch, ControlRecordType.COMMIT, mockTime.milliseconds());
592+
LogAppendInfo commitAppendInfo = LogTestUtils.appendEndTxnMarkerAsLeader(log, pid, epoch,
593+
ControlRecordType.COMMIT, mockTime.milliseconds(), 0, 0);
592594

593595
// first unstable offset is not updated until the high watermark is advanced
594596
assertEquals(Optional.of(firstAppendInfo.firstOffset()), log.firstUnstableOffset());

test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/TestUtils.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,8 @@
2424
import org.apache.kafka.common.record.RecordBatch;
2525
import org.apache.kafka.common.record.SimpleRecord;
2626
import org.apache.kafka.common.record.TimestampType;
27-
import org.apache.kafka.common.security.auth.SecurityProtocol;
2827
import org.apache.kafka.common.utils.Exit;
2928
import org.apache.kafka.common.utils.Utils;
30-
import org.apache.kafka.network.SocketServerConfigs;
31-
import org.apache.kafka.raft.QuorumConfig;
32-
import org.apache.kafka.server.config.KRaftConfigs;
33-
import org.apache.kafka.server.config.ReplicationConfigs;
34-
import org.apache.kafka.server.config.ServerConfigs;
35-
import org.apache.kafka.server.config.ServerLogConfigs;
3629

3730
import org.slf4j.Logger;
3831
import org.slf4j.LoggerFactory;
@@ -41,17 +34,9 @@
4134
import java.io.IOException;
4235
import java.nio.ByteBuffer;
4336
import java.nio.file.Files;
44-
import java.util.AbstractMap;
45-
import java.util.ArrayList;
4637
import java.util.List;
47-
import java.util.Map;
48-
import java.util.Optional;
49-
import java.util.Properties;
5038
import java.util.Random;
51-
import java.util.concurrent.TimeUnit;
5239
import java.util.function.Supplier;
53-
import java.util.stream.Collectors;
54-
import java.util.stream.IntStream;
5540

5641
import static java.lang.String.format;
5742

0 commit comments

Comments
 (0)