You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/02/25 02:59:40 UTC
[GitHub] [kafka] hachikuji opened a new pull request #10206: KAFKA-12369; Implement `ListTransactions` API
hachikuji opened a new pull request #10206:
URL: https://github.com/apache/kafka/pull/10206
This patch implements the `ListTransactions` API as documented in KIP-664: https://cwiki.apache.org/confluence/display/KAFKA/KIP-664%3A+Provide+tooling+to+detect+and+abort+hanging+transactions. This is only the server-side implementation and does not contain the `Admin` API.
### Committer Checklist (excluded from commit message)
- [ ] Verify design and implementation
- [ ] Verify test coverage and CI build status
- [ ] Verify documentation (including upgrade notes)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582586860
##########
File path: clients/src/main/resources/common/message/ListTransactionsRequest.json
##########
@@ -0,0 +1,31 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "request",
+ "listeners": ["zkBroker", "broker"],
+ "name": "ListTransactionsRequest",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "StatesFilter", "type": "[]string", "versions": "0+",
+ "about": "The transaction states to filter by: if empty, all transactions are returned; if non-empty, then only transactions matching one of the filtered states will be returned"
+ },
+ { "name": "ProducerIdFilter", "type": "[]int64", "versions": "0+",
+ "about": "The producerIds to filter by: if empty, no transactions will be returned; if non-empty, only transactions which match one of the filtered producerIds will be returned"
Review comment:
> On a side note, do you think it would be clearer if we used null to indicate the absence of the filter?
it should be fine in this case. However, the `empty` collection gets weird as it absolutely gets nothing :(
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582583372
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
Review comment:
Probably the response should have a field showing the invalid filters. The callers would know which filters are NOT working.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585784412
##########
File path: core/src/test/scala/integration/kafka/api/AuthorizerIntegrationTest.scala
##########
@@ -1813,17 +1813,25 @@ class AuthorizerIntegrationTest extends BaseRequestTest {
producer.beginTransaction()
producer.send(new ProducerRecord(tp.topic, tp.partition, "1".getBytes, "1".getBytes)).get
+ def assertListTransactionResult(
+ expectedTransactionalIds: Set[String]
+ ): Unit = {
+ val listTransactionsRequest = new ListTransactionsRequest.Builder(new ListTransactionsRequestData()).build()
+ val listTransactionsResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
+ assertEquals(Errors.NONE, Errors.forCode(listTransactionsResponse.data.errorCode))
+ assertEquals(expectedTransactionalIds, listTransactionsResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+ }
+
// First verify that we can list the transaction
- val listTransactionsRequest = new ListTransactionsRequest.Builder(new ListTransactionsRequestData()).build()
- val authorizedResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
- assertEquals(Errors.NONE, Errors.forCode(authorizedResponse.data.errorCode))
- assertEquals(Set(transactionalId), authorizedResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+ assertListTransactionResult(expectedTransactionalIds = Set(transactionalId))
// Now revoke authorization and verify that the transaction is no longer listable
removeAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), transactionalIdResource)
- val unauthorizedResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
- assertEquals(Errors.NONE, Errors.forCode(unauthorizedResponse.data.errorCode))
- assertEquals(Set(), unauthorizedResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+ assertListTransactionResult(expectedTransactionalIds = Set())
+
+ // The minimum permission needed is `Describe`
+ addAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), transactionalIdResource)
Review comment:
Should permission be describe rather than write?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585123307
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
Review comment:
You can see `listConsumerGroups` to see how this was handled in the analogous case for consumer groups.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585654890
##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3303,6 +3304,28 @@ class KafkaApis(val requestChannel: RequestChannel,
new DescribeTransactionsResponse(response.setThrottleTimeMs(requestThrottleMs)))
}
+ def handleListTransactionsRequest(request: RequestChannel.Request): Unit = {
+ val listTransactionsRequest = request.body[ListTransactionsRequest]
+ val filteredProducerIds = listTransactionsRequest.data.producerIdFilter.asScala.map(Long.unbox).toSet
+ val filteredStates = listTransactionsRequest.data.statesFilter.asScala.toSet
+ val response = txnCoordinator.handleListTransactions(filteredProducerIds, filteredStates)
+
+ // The response should contain only transactionalIds that the principal
+ // has `Describe` permission to access.
+ if (response.transactionStates != null) {
+ val transactionStateIter = response.transactionStates.iterator()
+ while (transactionStateIter.hasNext) {
Review comment:
We have not generally taken the view that cluster permission implies other permissions as far as I know. For example, cluster permission is not checked in the `Metadata` API to be able to list all topics.
We do have the following check in the `ListGroups` API, but the code notes that this was for compatibility.
```scala
if (authHelper.authorize(request.context, DESCRIBE, CLUSTER, CLUSTER_NAME))
// With describe cluster access all groups are returned. We keep this alternative for backward compatibility.
requestHelper.sendResponseMaybeThrottle(request, requestThrottleMs =>
createResponse(requestThrottleMs, groups, error))
else {
val filteredGroups = groups.filter(group => authHelper.authorize(request.context, DESCRIBE, GROUP, group.groupId))
requestHelper.sendResponseMaybeThrottle(request, requestThrottleMs =>
createResponse(requestThrottleMs, filteredGroups, error))
}
```
It's worth keeping in mind that the `ListTransactions` API should be seldom-used. I don't think we need to get too crazy with optimizations. We can always broaden the allowed ACLs in the future.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585660313
##########
File path: core/src/test/scala/integration/kafka/api/AuthorizerIntegrationTest.scala
##########
@@ -1801,6 +1801,31 @@ class AuthorizerIntegrationTest extends BaseRequestTest {
assertThrows(classOf[TransactionalIdAuthorizationException], () => producer.commitTransaction())
}
+ @Test
+ def testListTransactionsAuthorization(): Unit = {
+ createTopic(topic)
+ addAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), transactionalIdResource)
+ addAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), topicResource)
+
+ // Start a transaction and write to a topic.
+ val producer = buildTransactionalProducer()
+ producer.initTransactions()
+ producer.beginTransaction()
+ producer.send(new ProducerRecord(tp.topic, tp.partition, "1".getBytes, "1".getBytes)).get
+
+ // First verify that we can list the transaction
+ val listTransactionsRequest = new ListTransactionsRequest.Builder(new ListTransactionsRequestData()).build()
+ val authorizedResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
+ assertEquals(Errors.NONE, Errors.forCode(authorizedResponse.data.errorCode))
+ assertEquals(Set(transactionalId), authorizedResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+
+ // Now revoke authorization and verify that the transaction is no longer listable
+ removeAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), transactionalIdResource)
Review comment:
How about adding test for `DESCRIBE` permission?
##########
File path: core/src/test/scala/unit/kafka/coordinator/transaction/TransactionStateManagerTest.scala
##########
@@ -476,13 +476,78 @@ class TransactionStateManagerTest {
}
@Test
- def shouldReturnNotCooridnatorErrorIfTransactionIdPartitionNotOwned(): Unit = {
+ def shouldReturnNotCoordinatorErrorIfTransactionIdPartitionNotOwned(): Unit = {
transactionManager.getTransactionState(transactionalId1).fold(
err => assertEquals(Errors.NOT_COORDINATOR, err),
_ => fail(transactionalId1 + "'s transaction state is already in the cache")
)
}
+ @Test
+ def testListTransactionsWithCoordinatorLoadingInProgress(): Unit = {
+ transactionManager.addLoadingPartition(partitionId = 0, coordinatorEpoch = 15)
+ val listResponse = transactionManager.listTransactionStates(
+ filterProducerIds = Set.empty,
+ filterStateNames = Set.empty
+ )
+ assertEquals(Errors.COORDINATOR_LOAD_IN_PROGRESS, Errors.forCode(listResponse.errorCode))
+ }
+
+ @Test
+ def testListTransactionsFiltering(): Unit = {
+ for (partitionId <- 0 until numPartitions) {
+ transactionManager.addLoadedTransactionsToCache(partitionId, 0, new Pool[String, TransactionMetadata]())
+ }
+
+ def putTransaction(
+ transactionalId: String,
+ producerId: Long,
+ state: TransactionState
+ ): Unit = {
+ val txnMetadata = transactionMetadata(transactionalId, producerId, state)
+ transactionManager.putTransactionStateIfNotExists(txnMetadata).left.toOption.foreach { error =>
+ fail(s"Failed to insert transaction $txnMetadata due to error $error")
+ }
+ }
+
+ putTransaction(transactionalId = "t0", producerId = 0, state = Ongoing)
+ putTransaction(transactionalId = "t1", producerId = 1, state = Ongoing)
+ putTransaction(transactionalId = "t2", producerId = 2, state = PrepareCommit)
+ putTransaction(transactionalId = "t3", producerId = 3, state = PrepareAbort)
+ putTransaction(transactionalId = "t4", producerId = 4, state = CompleteCommit)
+ putTransaction(transactionalId = "t5", producerId = 5, state = CompleteAbort)
+ putTransaction(transactionalId = "t6", producerId = 6, state = CompleteAbort)
+ putTransaction(transactionalId = "t7", producerId = 7, state = PrepareEpochFence)
+ // Note that `Dead` transactions are never returned. This is a transient state
+ // which is used when the transaction state is in the process of being deleted
+ // (whether though expiration or coordinator unloading).
+ putTransaction(transactionalId = "t8", producerId = 8, state = Dead)
+
+ def assertListTransactions(
+ expectedTransactionalIds: Set[String],
+ filterProducerIds: Set[Long] = Set.empty,
+ filterStates: Set[String] = Set.empty
+ ): Unit = {
+ val listResponse = transactionManager.listTransactionStates(filterProducerIds, filterStates)
+ assertEquals(Errors.NONE, Errors.forCode(listResponse.errorCode))
+ assertEquals(expectedTransactionalIds, listResponse.transactionStates.asScala.map(_.transactionalId).toSet)
+ val expectedUnknownStates = filterStates.filter(state => TransactionState.fromName(state).isEmpty)
+ assertEquals(expectedUnknownStates, listResponse.unknownStateFilters.asScala.toSet)
+ }
+
+ assertListTransactions(Set("t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"))
+ assertListTransactions(Set("t0", "t1"), filterStates = Set("Ongoing"))
+ assertListTransactions(Set("t0", "t1"), filterStates = Set("Ongoing", "UnknownState"))
+ assertListTransactions(Set("t2", "t4"), filterStates = Set("PrepareCommit", "CompleteCommit"))
+ assertListTransactions(Set(), filterStates = Set("UnknownState"))
+ assertListTransactions(Set("t5"), filterProducerIds = Set(5L))
+ assertListTransactions(Set("t5", "t6"), filterProducerIds = Set(5L, 6L, 8L, 9L))
+ assertListTransactions(Set("t4"), filterProducerIds = Set(4L, 5L), filterStates = Set("CompleteCommit"))
+ assertListTransactions(Set("t4", "t5"), filterProducerIds = Set(4L, 5L), filterStates = Set("CompleteCommit", "CompleteAbort"))
+ assertListTransactions(Set(), filterProducerIds = Set(3L, 6L), filterStates = Set("UnknownState"))
+ assertListTransactions(Set(), filterProducerIds = Set(10L), filterStates = Set("CompleteCommit"))
+ }
Review comment:
How about adding test for `Dead` state? It should always return nothing.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582566067
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
Review comment:
The problem is that we don't have a way to map from the producerId to the partition.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#issuecomment-785640598
@chia7712 Thanks, I really appreciate your help reviewing these patches. I pushed a couple comments and left a few responses to your questions.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r583287853
##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3303,6 +3304,28 @@ class KafkaApis(val requestChannel: RequestChannel,
new DescribeTransactionsResponse(response.setThrottleTimeMs(requestThrottleMs)))
}
+ def handleListTransactionsRequest(request: RequestChannel.Request): Unit = {
+ val listTransactionsRequest = request.body[ListTransactionsRequest]
+ val response = new ListTransactionsResponseData()
+
+ val filteredProducerIds = listTransactionsRequest.data.producerIdFilter.asScala.map(Long.unbox).toSet
+ val filteredStates = listTransactionsRequest.data.statesFilter.asScala.toSet
+
+ txnCoordinator.handleListTransactions(filteredProducerIds, filteredStates) match {
+ case Left(error) =>
+ response.setErrorCode(error.code)
+ case Right(transactions) =>
+ val authorizedTransactions = transactions.filter { state =>
+ authHelper.authorize(request.context, DESCRIBE, TRANSACTIONAL_ID, state.transactionalId)
Review comment:
In the case of `DescribeTransactions`, the set of transaction states to include in the response are explicitly defined in the request. With `ListTransactions`, we only have filters. The only error state to convey is at the top level. For individual transaction states, they either match the filters or they do not.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582565318
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
Review comment:
I could be persuaded probably. Suppose we add a new state in the future. If a user tried to query that state on an old broker, definitely no transactions would exist in that state. From that perspective, the implementation would return an accurate result. On the other hand, I can see how that might be misleading. If we decide to return an error code, then I think we'll have to treat the state filter a little more carefully from a versioning perspective. A new state would probably demand a new version. I think we tried to view the states a little bit more loosely in the `ListGroups` API (which this is modeled after) because we considered the state machine more of an internal implementation detail. This, by the way, is why we went with the string representation rather than numeric ids. This became more of a grey area though when the state filter was added to the request...
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585622686
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,63 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): ListTransactionsResponseData = {
+ inReadLock(stateLock) {
+ val response = new ListTransactionsResponseData()
+ if (loadingPartitions.nonEmpty) {
+ response.setErrorCode(Errors.COORDINATOR_LOAD_IN_PROGRESS.code)
+ } else {
+ val filterStates = mutable.Set.empty[TransactionState]
+ val unknownStates = new java.util.ArrayList[String]
+ filterStateNames.foreach { stateName =>
+ TransactionState.fromName(stateName) match {
+ case Some(state) => filterStates += state
+ case None => unknownStates.add(stateName)
Review comment:
Yeah, I agree. It is simpler. I guess I don't have a strong reason to make this field nullable.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585252099
##########
File path: clients/src/main/resources/common/message/ListTransactionsResponse.json
##########
@@ -0,0 +1,35 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "response",
+ "name": "ListTransactionsResponse",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "ThrottleTimeMs", "type": "int32", "versions": "0+",
+ "about": "The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota." },
+ { "name": "ErrorCode", "type": "int16", "versions": "0+" },
+ { "name": "UnknownStateFilters", "type": "[]string", "default": "null", "versions": "0+", "nullableVersions": "0+",
Review comment:
the name in request is called `StatesFilter`. Could you make consistent naming?
##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3303,6 +3304,28 @@ class KafkaApis(val requestChannel: RequestChannel,
new DescribeTransactionsResponse(response.setThrottleTimeMs(requestThrottleMs)))
}
+ def handleListTransactionsRequest(request: RequestChannel.Request): Unit = {
+ val listTransactionsRequest = request.body[ListTransactionsRequest]
+ val filteredProducerIds = listTransactionsRequest.data.producerIdFilter.asScala.map(Long.unbox).toSet
+ val filteredStates = listTransactionsRequest.data.statesFilter.asScala.toSet
+ val response = txnCoordinator.handleListTransactions(filteredProducerIds, filteredStates)
+
+ // The response should contain only transactionalIds that the principal
+ // has `Describe` permission to access.
+ if (response.transactionStates != null) {
Review comment:
Is this null check necessary? the filed is NOT nullable. There appears to be a bug if we set null to it in production code.
##########
File path: clients/src/main/resources/common/message/ListTransactionsResponse.json
##########
@@ -0,0 +1,35 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "response",
+ "name": "ListTransactionsResponse",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "ThrottleTimeMs", "type": "int32", "versions": "0+",
+ "about": "The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota." },
+ { "name": "ErrorCode", "type": "int16", "versions": "0+" },
+ { "name": "UnknownStateFilters", "type": "[]string", "default": "null", "versions": "0+", "nullableVersions": "0+",
Review comment:
Why it requires `nullableVersions`?
##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3303,6 +3304,28 @@ class KafkaApis(val requestChannel: RequestChannel,
new DescribeTransactionsResponse(response.setThrottleTimeMs(requestThrottleMs)))
}
+ def handleListTransactionsRequest(request: RequestChannel.Request): Unit = {
+ val listTransactionsRequest = request.body[ListTransactionsRequest]
+ val filteredProducerIds = listTransactionsRequest.data.producerIdFilter.asScala.map(Long.unbox).toSet
+ val filteredStates = listTransactionsRequest.data.statesFilter.asScala.toSet
+ val response = txnCoordinator.handleListTransactions(filteredProducerIds, filteredStates)
+
+ // The response should contain only transactionalIds that the principal
+ // has `Describe` permission to access.
+ if (response.transactionStates != null) {
+ val transactionStateIter = response.transactionStates.iterator()
+ while (transactionStateIter.hasNext) {
Review comment:
How about checking permission of cluster first? It can avoiding redundant loops.
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,63 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): ListTransactionsResponseData = {
+ inReadLock(stateLock) {
+ val response = new ListTransactionsResponseData()
+ if (loadingPartitions.nonEmpty) {
+ response.setErrorCode(Errors.COORDINATOR_LOAD_IN_PROGRESS.code)
+ } else {
+ val filterStates = mutable.Set.empty[TransactionState]
+ val unknownStates = new java.util.ArrayList[String]
+ filterStateNames.foreach { stateName =>
+ TransactionState.fromName(stateName) match {
+ case Some(state) => filterStates += state
+ case None => unknownStates.add(stateName)
Review comment:
It response#unknownStates is NOT nullable, we can simplify it to `case None => response.unknownStateFilters().add(stateName)`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582558759
##########
File path: clients/src/main/java/org/apache/kafka/common/requests/ListTransactionsRequest.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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 org.apache.kafka.common.requests;
+
+import org.apache.kafka.common.message.ListTransactionsRequestData;
+import org.apache.kafka.common.message.ListTransactionsResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.ByteBufferAccessor;
+import org.apache.kafka.common.protocol.Errors;
+
+import java.nio.ByteBuffer;
+
+public class ListTransactionsRequest extends AbstractRequest {
+ public static class Builder extends AbstractRequest.Builder<ListTransactionsRequest> {
+ public final ListTransactionsRequestData data;
+
+ public Builder(ListTransactionsRequestData data) {
+ super(ApiKeys.LIST_TRANSACTIONS);
+ this.data = data;
+ }
+
+ @Override
+ public ListTransactionsRequest build(short version) {
+ return new ListTransactionsRequest(data, version);
+ }
+
+ @Override
+ public String toString() {
+ return data.toString();
+ }
+ }
+
+ private final ListTransactionsRequestData data;
+
+ private ListTransactionsRequest(ListTransactionsRequestData data, short version) {
+ super(ApiKeys.LIST_TRANSACTIONS, version);
+ this.data = data;
+ }
+
+ public ListTransactionsRequestData data() {
+ return data;
+ }
+
+ @Override
+ public ListTransactionsResponse getErrorResponse(int throttleTimeMs, Throwable e) {
+ Errors error = Errors.forException(e);
+ ListTransactionsResponseData response = new ListTransactionsResponseData()
Review comment:
Haha, I should really have checked for that after the last PR.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582556446
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
+ val states = mutable.ListBuffer.empty[ListTransactionsResponseData.TransactionState]
+
+ def shouldInclude(txnMetadata: TransactionMetadata): Boolean = {
+ if (txnMetadata.state == Dead) {
Review comment:
Could you add comment for this case? why it is excluded?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] tombentley commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
tombentley commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r584641483
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
Review comment:
How are the states going to be documented (e.g. in the Admin client?) It seems odd to me that on the one hand "they're an impl detail", but they're also used in the filter. How could someone write code to list all of the transaction not in a given state if they can't know the universe of states (given new states could be added in the future)?
##########
File path: clients/src/main/resources/common/message/ListTransactionsResponse.json
##########
@@ -0,0 +1,32 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "response",
+ "name": "ListTransactionsResponse",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "ThrottleTimeMs", "type": "int32", "versions": "0+",
+ "about": "The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota." },
+ { "name": "ErrorCode", "type": "int16", "versions": "0+" },
+ { "name": "TransactionStates", "type": "[]TransactionState", "versions": "0+", "fields": [
+ { "name": "TransactionalId", "type": "string", "versions": "0+", "entityType": "transactionalId" },
+ { "name": "ProducerId", "type": "int64", "versions": "0+", "entityType": "producerId" },
+ { "name": "TransactionState", "type": "string", "versions": "0+" }
Review comment:
Shouldn't these also have `about` properties?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582547180
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionMetadata.scala
##########
@@ -25,8 +25,50 @@ import org.apache.kafka.common.record.RecordBatch
import scala.collection.{immutable, mutable}
+
+object TransactionState {
+ val AllStates = Set(
+ Empty,
+ Ongoing,
+ PrepareCommit,
+ PrepareAbort,
+ CompleteCommit,
+ CompleteAbort,
+ Dead,
+ PrepareEpochFence
+ )
+
+ def fromName(name: String): Option[TransactionState] = {
Review comment:
How about `AllStates.find(_.name == name)`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582592473
##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3303,6 +3304,28 @@ class KafkaApis(val requestChannel: RequestChannel,
new DescribeTransactionsResponse(response.setThrottleTimeMs(requestThrottleMs)))
}
+ def handleListTransactionsRequest(request: RequestChannel.Request): Unit = {
+ val listTransactionsRequest = request.body[ListTransactionsRequest]
+ val response = new ListTransactionsResponseData()
+
+ val filteredProducerIds = listTransactionsRequest.data.producerIdFilter.asScala.map(Long.unbox).toSet
+ val filteredStates = listTransactionsRequest.data.statesFilter.asScala.toSet
+
+ txnCoordinator.handleListTransactions(filteredProducerIds, filteredStates) match {
+ case Left(error) =>
+ response.setErrorCode(error.code)
+ case Right(transactions) =>
+ val authorizedTransactions = transactions.filter { state =>
+ authHelper.authorize(request.context, DESCRIBE, TRANSACTIONAL_ID, state.transactionalId)
Review comment:
Why `TransactionState` does not include error field (similar to `DescribeTransactionsResponseData.TransactionState`)?
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,49 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
+ val states = mutable.ListBuffer.empty[ListTransactionsResponseData.TransactionState]
+
+ def shouldInclude(txnMetadata: TransactionMetadata): Boolean = {
+ if (txnMetadata.state == Dead) {
+ // We filter the `Dead` state since it is a transient state which
+ // indicates that the transactionalId and its metadata are in the
Review comment:
redundant "space"
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585656746
##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3303,6 +3304,28 @@ class KafkaApis(val requestChannel: RequestChannel,
new DescribeTransactionsResponse(response.setThrottleTimeMs(requestThrottleMs)))
}
+ def handleListTransactionsRequest(request: RequestChannel.Request): Unit = {
+ val listTransactionsRequest = request.body[ListTransactionsRequest]
+ val filteredProducerIds = listTransactionsRequest.data.producerIdFilter.asScala.map(Long.unbox).toSet
+ val filteredStates = listTransactionsRequest.data.statesFilter.asScala.toSet
+ val response = txnCoordinator.handleListTransactions(filteredProducerIds, filteredStates)
+
+ // The response should contain only transactionalIds that the principal
+ // has `Describe` permission to access.
+ if (response.transactionStates != null) {
+ val transactionStateIter = response.transactionStates.iterator()
+ while (transactionStateIter.hasNext) {
Review comment:
Thanks for this nice explanation :)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582558391
##########
File path: clients/src/main/resources/common/message/ListTransactionsRequest.json
##########
@@ -0,0 +1,31 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "request",
+ "listeners": ["zkBroker", "broker"],
+ "name": "ListTransactionsRequest",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "StatesFilter", "type": "[]string", "versions": "0+",
+ "about": "The transaction states to filter by: if empty, all transactions are returned; if non-empty, then only transactions matching one of the filtered states will be returned"
+ },
+ { "name": "ProducerIdFilter", "type": "[]int64", "versions": "0+",
+ "about": "The producerIds to filter by: if empty, no transactions will be returned; if non-empty, only transactions which match one of the filtered producerIds will be returned"
Review comment:
`no transactions will be returned`
^^^^^^^^^^^^^^^^^^^^^^^^^^ this document is a bit weird since the code shows that all transactions are returned if this filter is empty.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585128257
##########
File path: clients/src/main/resources/common/message/ListTransactionsResponse.json
##########
@@ -0,0 +1,32 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "response",
+ "name": "ListTransactionsResponse",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "ThrottleTimeMs", "type": "int32", "versions": "0+",
+ "about": "The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota." },
+ { "name": "ErrorCode", "type": "int16", "versions": "0+" },
+ { "name": "TransactionStates", "type": "[]TransactionState", "versions": "0+", "fields": [
+ { "name": "TransactionalId", "type": "string", "versions": "0+", "entityType": "transactionalId" },
+ { "name": "ProducerId", "type": "int64", "versions": "0+", "entityType": "producerId" },
+ { "name": "TransactionState", "type": "string", "versions": "0+" }
Review comment:
I added an `about` tag for `TransactionState`. I think the `entityType` tag is enough for the other two fields.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582567640
##########
File path: clients/src/main/resources/common/message/ListTransactionsRequest.json
##########
@@ -0,0 +1,31 @@
+// 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.
+
+{
+ "apiKey": 66,
+ "type": "request",
+ "listeners": ["zkBroker", "broker"],
+ "name": "ListTransactionsRequest",
+ "validVersions": "0",
+ "flexibleVersions": "0+",
+ "fields": [
+ { "name": "StatesFilter", "type": "[]string", "versions": "0+",
+ "about": "The transaction states to filter by: if empty, all transactions are returned; if non-empty, then only transactions matching one of the filtered states will be returned"
+ },
+ { "name": "ProducerIdFilter", "type": "[]int64", "versions": "0+",
+ "about": "The producerIds to filter by: if empty, no transactions will be returned; if non-empty, only transactions which match one of the filtered producerIds will be returned"
Review comment:
Yeah, my bad. On a side note, do you think it would be clearer if we used `null` to indicate the absence of the filter?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582547244
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionMetadata.scala
##########
@@ -25,8 +25,50 @@ import org.apache.kafka.common.record.RecordBatch
import scala.collection.{immutable, mutable}
+
+object TransactionState {
+ val AllStates = Set(
+ Empty,
+ Ongoing,
+ PrepareCommit,
+ PrepareAbort,
+ CompleteCommit,
+ CompleteAbort,
+ Dead,
+ PrepareEpochFence
+ )
+
+ def fromName(name: String): Option[TransactionState] = {
+ name match {
+ case "Empty" => Some(Empty)
+ case "Ongoing" => Some(Ongoing)
+ case "PrepareCommit" => Some(PrepareCommit)
+ case "PrepareAbort" => Some(PrepareAbort)
+ case "CompleteCommit" => Some(CompleteCommit)
+ case "CompleteAbort" => Some(CompleteAbort)
+ case "PrepareEpochFence" => Some(PrepareEpochFence)
+ case "Dead" => Some(Dead)
+ case _ => None
+ }
+ }
+
+ def fromId(id: Byte): TransactionState = {
Review comment:
How about `AllStates.find(_.id == id)`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582549509
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
+ val states = mutable.ListBuffer.empty[ListTransactionsResponseData.TransactionState]
+
+ def shouldInclude(txnMetadata: TransactionMetadata): Boolean = {
+ if (txnMetadata.state == Dead) {
+ false
+ } else if (filterProducerIds.nonEmpty && !filterProducerIds.contains(txnMetadata.producerId)) {
+ false
+ } else if (filterStateNames.nonEmpty && !filterStates.contains(txnMetadata.state)) {
+ false
+ } else {
+ true
+ }
+ }
+
+ transactionMetadataCache.foreach { case (_, cache) =>
Review comment:
Could you use `forKeyValue` instead of `foreach`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji merged pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji merged pull request #10206:
URL: https://github.com/apache/kafka/pull/10206
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582568344
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionMetadata.scala
##########
@@ -25,8 +25,50 @@ import org.apache.kafka.common.record.RecordBatch
import scala.collection.{immutable, mutable}
+
+object TransactionState {
+ val AllStates = Set(
+ Empty,
+ Ongoing,
+ PrepareCommit,
+ PrepareAbort,
+ CompleteCommit,
+ CompleteAbort,
+ Dead,
+ PrepareEpochFence
+ )
+
+ def fromName(name: String): Option[TransactionState] = {
+ name match {
+ case "Empty" => Some(Empty)
+ case "Ongoing" => Some(Ongoing)
+ case "PrepareCommit" => Some(PrepareCommit)
+ case "PrepareAbort" => Some(PrepareAbort)
+ case "CompleteCommit" => Some(CompleteCommit)
+ case "CompleteAbort" => Some(CompleteAbort)
+ case "PrepareEpochFence" => Some(PrepareEpochFence)
+ case "Dead" => Some(Dead)
+ case _ => None
+ }
+ }
+
+ def fromId(id: Byte): TransactionState = {
Review comment:
I left this one as it was since I am not 100% sure whether it has any impact on the performance of transaction loading.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r585800691
##########
File path: core/src/test/scala/integration/kafka/api/AuthorizerIntegrationTest.scala
##########
@@ -1813,17 +1813,25 @@ class AuthorizerIntegrationTest extends BaseRequestTest {
producer.beginTransaction()
producer.send(new ProducerRecord(tp.topic, tp.partition, "1".getBytes, "1".getBytes)).get
+ def assertListTransactionResult(
+ expectedTransactionalIds: Set[String]
+ ): Unit = {
+ val listTransactionsRequest = new ListTransactionsRequest.Builder(new ListTransactionsRequestData()).build()
+ val listTransactionsResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
+ assertEquals(Errors.NONE, Errors.forCode(listTransactionsResponse.data.errorCode))
+ assertEquals(expectedTransactionalIds, listTransactionsResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+ }
+
// First verify that we can list the transaction
- val listTransactionsRequest = new ListTransactionsRequest.Builder(new ListTransactionsRequestData()).build()
- val authorizedResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
- assertEquals(Errors.NONE, Errors.forCode(authorizedResponse.data.errorCode))
- assertEquals(Set(transactionalId), authorizedResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+ assertListTransactionResult(expectedTransactionalIds = Set(transactionalId))
// Now revoke authorization and verify that the transaction is no longer listable
removeAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), transactionalIdResource)
- val unauthorizedResponse = connectAndReceive[ListTransactionsResponse](listTransactionsRequest)
- assertEquals(Errors.NONE, Errors.forCode(unauthorizedResponse.data.errorCode))
- assertEquals(Set(), unauthorizedResponse.data.transactionStates.asScala.map(_.transactionalId).toSet)
+ assertListTransactionResult(expectedTransactionalIds = Set())
+
+ // The minimum permission needed is `Describe`
+ addAndVerifyAcls(Set(new AccessControlEntry(clientPrincipalString, WildcardHost, WRITE, ALLOW)), transactionalIdResource)
Review comment:
Ugh, yes.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#issuecomment-788392877
@chia7712 I implemented your suggestion to include the unknown states in the response. Let me know how this looks to you.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] hachikuji commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r583877084
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
Review comment:
It's a reasonable suggestion. Let me give it a try.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] chia7712 commented on a change in pull request #10206: KAFKA-12369; Implement `ListTransactions` API
Posted by GitBox <gi...@apache.org>.
chia7712 commented on a change in pull request #10206:
URL: https://github.com/apache/kafka/pull/10206#discussion_r582545612
##########
File path: clients/src/main/java/org/apache/kafka/common/requests/ListTransactionsRequest.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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 org.apache.kafka.common.requests;
+
+import org.apache.kafka.common.message.ListTransactionsRequestData;
+import org.apache.kafka.common.message.ListTransactionsResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.ByteBufferAccessor;
+import org.apache.kafka.common.protocol.Errors;
+
+import java.nio.ByteBuffer;
+
+public class ListTransactionsRequest extends AbstractRequest {
+ public static class Builder extends AbstractRequest.Builder<ListTransactionsRequest> {
+ public final ListTransactionsRequestData data;
+
+ public Builder(ListTransactionsRequestData data) {
+ super(ApiKeys.LIST_TRANSACTIONS);
+ this.data = data;
+ }
+
+ @Override
+ public ListTransactionsRequest build(short version) {
+ return new ListTransactionsRequest(data, version);
+ }
+
+ @Override
+ public String toString() {
+ return data.toString();
+ }
+ }
+
+ private final ListTransactionsRequestData data;
+
+ private ListTransactionsRequest(ListTransactionsRequestData data, short version) {
+ super(ApiKeys.LIST_TRANSACTIONS, version);
+ this.data = data;
+ }
+
+ public ListTransactionsRequestData data() {
+ return data;
+ }
+
+ @Override
+ public ListTransactionsResponse getErrorResponse(int throttleTimeMs, Throwable e) {
+ Errors error = Errors.forException(e);
+ ListTransactionsResponseData response = new ListTransactionsResponseData()
Review comment:
`throttleTimeMs` is neglected.
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
+ val states = mutable.ListBuffer.empty[ListTransactionsResponseData.TransactionState]
+
+ def shouldInclude(txnMetadata: TransactionMetadata): Boolean = {
+ if (txnMetadata.state == Dead) {
+ false
+ } else if (filterProducerIds.nonEmpty && !filterProducerIds.contains(txnMetadata.producerId)) {
+ false
+ } else if (filterStateNames.nonEmpty && !filterStates.contains(txnMetadata.state)) {
+ false
+ } else {
+ true
+ }
+ }
+
+ transactionMetadataCache.foreach { case (_, cache) =>
+ cache.metadataPerTransactionalId.values.foreach { txnMetadata =>
+ txnMetadata.inLock {
+ if (shouldInclude(txnMetadata)) {
+ states += new ListTransactionsResponseData.TransactionState()
+ .setTransactionalId(txnMetadata.transactionalId)
+ .setProducerId(txnMetadata.producerId)
+ .setTransactionState(txnMetadata.state.toString)
Review comment:
`txnMetadata.state.toString` -> `txnMetadata.state.name`
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionMetadata.scala
##########
@@ -25,8 +25,50 @@ import org.apache.kafka.common.record.RecordBatch
import scala.collection.{immutable, mutable}
+
+object TransactionState {
+ val AllStates = Set(
+ Empty,
+ Ongoing,
+ PrepareCommit,
+ PrepareAbort,
+ CompleteCommit,
+ CompleteAbort,
+ Dead,
+ PrepareEpochFence
+ )
+
+ def fromName(name: String): Option[TransactionState] = {
Review comment:
How about 'AllStates.find(_.name == name)'?
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
Review comment:
Maybe we can return error only if the expected partition ids are included by loading partitions?
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
+ val states = mutable.ListBuffer.empty[ListTransactionsResponseData.TransactionState]
+
+ def shouldInclude(txnMetadata: TransactionMetadata): Boolean = {
+ if (txnMetadata.state == Dead) {
+ false
+ } else if (filterProducerIds.nonEmpty && !filterProducerIds.contains(txnMetadata.producerId)) {
+ false
+ } else if (filterStateNames.nonEmpty && !filterStates.contains(txnMetadata.state)) {
+ false
+ } else {
+ true
+ }
+ }
+
+ transactionMetadataCache.foreach { case (_, cache) =>
Review comment:
Could you use 'forKeyValue' instead of 'foreach'?
##########
File path: core/src/test/scala/unit/kafka/coordinator/transaction/TransactionMetadataTest.scala
##########
@@ -460,6 +460,43 @@ class TransactionMetadataTest {
assertEquals(Left(Errors.PRODUCER_FENCED), result)
}
+ @Test
+ def testTransactionStateIdAndNameMapping(): Unit = {
+ for (state <- TransactionState.AllStates) {
+ assertEquals(state, TransactionState.fromId(state.id))
+ assertEquals(state, TransactionState.fromName(state.name))
Review comment:
assertEquals(state, TransactionState.fromName(state.name)*.get*)
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala
##########
@@ -223,6 +224,46 @@ class TransactionStateManager(brokerId: Int,
throw new IllegalStateException(s"Unexpected empty transaction metadata returned while putting $txnMetadata")))
}
+ def listTransactionStates(
+ filterProducerIds: Set[Long],
+ filterStateNames: Set[String]
+ ): Either[Errors, List[ListTransactionsResponseData.TransactionState]] = {
+ inReadLock(stateLock) {
+ if (loadingPartitions.nonEmpty) {
+ Left(Errors.COORDINATOR_LOAD_IN_PROGRESS)
+ } else {
+ val filterStates = filterStateNames.flatMap(TransactionState.fromName)
Review comment:
Should it produce exception if it include nonexistent state?
##########
File path: core/src/main/scala/kafka/coordinator/transaction/TransactionMetadata.scala
##########
@@ -25,8 +25,50 @@ import org.apache.kafka.common.record.RecordBatch
import scala.collection.{immutable, mutable}
+
+object TransactionState {
+ val AllStates = Set(
+ Empty,
+ Ongoing,
+ PrepareCommit,
+ PrepareAbort,
+ CompleteCommit,
+ CompleteAbort,
+ Dead,
+ PrepareEpochFence
+ )
+
+ def fromName(name: String): Option[TransactionState] = {
+ name match {
+ case "Empty" => Some(Empty)
+ case "Ongoing" => Some(Ongoing)
+ case "PrepareCommit" => Some(PrepareCommit)
+ case "PrepareAbort" => Some(PrepareAbort)
+ case "CompleteCommit" => Some(CompleteCommit)
+ case "CompleteAbort" => Some(CompleteAbort)
+ case "PrepareEpochFence" => Some(PrepareEpochFence)
+ case "Dead" => Some(Dead)
+ case _ => None
+ }
+ }
+
+ def fromId(id: Byte): TransactionState = {
Review comment:
How about 'AllStates.find(_.id == id)'?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org