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

[COMMON] rewrite admin.Transaction by java 17 record #1732

Merged
merged 1 commit into from
May 11, 2023
Merged
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
67 changes: 16 additions & 51 deletions common/src/main/java/org/astraea/common/admin/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,24 @@
import java.util.Set;
import java.util.stream.Collectors;

public interface Transaction {
public record Transaction(
String transactionId,
int coordinatorId,
TransactionState state,
long producerId,
int producerEpoch,
long transactionTimeoutMs,
Set<TopicPartition> topicPartitions) {

static Transaction of(
String transactionId, org.apache.kafka.clients.admin.TransactionDescription td) {
return new Transaction() {
@Override
public String transactionId() {
return transactionId;
}

@Override
public int coordinatorId() {
return td.coordinatorId();
}

@Override
public TransactionState state() {
return TransactionState.of(td.state());
}

@Override
public long producerId() {
return td.producerId();
}

@Override
public int producerEpoch() {
return td.producerEpoch();
}

@Override
public long transactionTimeoutMs() {
return 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這是 bug ... 已經補上測試

}

@Override
public Set<TopicPartition> topicPartitions() {
return td.topicPartitions().stream().map(TopicPartition::from).collect(Collectors.toSet());
}
};
return new Transaction(
transactionId,
td.coordinatorId(),
TransactionState.of(td.state()),
td.producerId(),
td.producerEpoch(),
td.transactionTimeoutMs(),
td.topicPartitions().stream().map(TopicPartition::from).collect(Collectors.toSet()));
}

String transactionId();

int coordinatorId();

TransactionState state();

long producerId();

int producerEpoch();

long transactionTimeoutMs();

Set<TopicPartition> topicPartitions();
}
28 changes: 28 additions & 0 deletions common/src/test/java/org/astraea/common/admin/AdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2192,4 +2192,32 @@ void testClusterId() {
.clusterId());
}
}

@Test
void testTransaction() {
var records =
IntStream.range(0, 100)
.mapToObj(
index -> Record.builder().topic(Utils.randomString()).key(new byte[100]).build())
.toList();
try (var admin = Admin.of(SERVICE.bootstrapServers());
var producer =
Producer.builder().bootstrapServers(SERVICE.bootstrapServers()).buildTransactional()) {
producer.send(records);
Assertions.assertTrue(
admin
.transactionIds()
.toCompletableFuture()
.join()
.contains(producer.transactionId().get()));
var transactions =
admin.transactions(Set.of(producer.transactionId().get())).toCompletableFuture().join();
Assertions.assertNotEquals(0, transactions.size());
transactions.forEach(
t -> {
Assertions.assertEquals(producer.transactionId().get(), t.transactionId());
Assertions.assertNotEquals(0, t.transactionTimeoutMs());
});
}
}
}