BewareMyPower commented on code in PR #984:
URL: https://github.com/apache/pulsar-client-go/pull/984#discussion_r1137115106


##########
pulsar/transaction_impl.go:
##########
@@ -0,0 +1,235 @@
+// 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 pulsar
+
+import (
+       "context"
+       "sync"
+       "sync/atomic"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       uAtomic "go.uber.org/atomic"
+)
+
+type TxnState int32
+type void struct{}
+type subscription struct {
+       topic        string
+       subscription string
+}
+
+type transaction struct {
+       sync.Mutex
+       txnID                    TxnID
+       state                    TxnState
+       tcClient                 *transactionCoordinatorClient
+       registerPartitions       map[string]void
+       registerAckSubscriptions map[subscription]void
+       /**
+       * opsFlow Wait all the operations of sending and acking messages with 
the transaction complete
+       * by reading msg from the chan.
+       * opsCount is record the number of the uncompleted operations.
+       * opsFlow
+       *        Write:
+       *                     1. When the transaction is created, a new empty 
struct{} will be written to opsFlow chan.
+       *                         2. When the opsCount decrement from 1 to 0, a 
new empty struct{} will be written to opsFlow chan.
+       *                         3. When get a retryable error after 
committing or aborting the transaction,
+       *                    a new empty struct{} will be written to opsFlow 
chan.
+       *        Read:
+       *                         1. When the transaction is committed or 
aborted, an empty struct{} will be read from opsFlow chan.
+       *                         2. When the opsCount increment from 0 to 1, 
an empty struct{} will be read from opsFlow chan.
+        */

Review Comment:
   ```suggestion
           // opsFlow Wait all the operations of sending and acking messages 
with the transaction complete
           // by reading msg from the chan.
           // opsCount is record the number of the uncompleted operations.
           // opsFlow
           //   Write:
           //     1. When the transaction is created, a new empty struct{} will 
be written to opsFlow chan.
           //     2. When the opsCount decrement from 1 to 0, a new empty 
struct{} will be written to opsFlow chan.
           //     3. When get a retryable error after committing or aborting 
the transaction,
           //        a new empty struct{} will be written to opsFlow chan.
           //   Read:
           //     1. When the transaction is committed or aborted, an empty 
struct{} will be read from opsFlow chan.
           //     2. When the opsCount increment from 0 to 1, an empty struct{} 
will be read from opsFlow chan.
   ```
   
   It's better to use `//` for multi-lines comments. And please don't mix 
spaces and tabs in the comments.



##########
pulsar/transaction_impl.go:
##########
@@ -0,0 +1,235 @@
+// 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 pulsar
+
+import (
+       "context"
+       "sync"
+       "sync/atomic"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       uAtomic "go.uber.org/atomic"
+)
+
+type TxnState int32
+type void struct{}
+type subscription struct {
+       topic        string
+       subscription string
+}
+
+type transaction struct {
+       sync.Mutex
+       txnID                    TxnID
+       state                    TxnState
+       tcClient                 *transactionCoordinatorClient
+       registerPartitions       map[string]void
+       registerAckSubscriptions map[subscription]void
+       /**
+       * opsFlow Wait all the operations of sending and acking messages with 
the transaction complete
+       * by reading msg from the chan.
+       * opsCount is record the number of the uncompleted operations.
+       * opsFlow
+       *        Write:
+       *                     1. When the transaction is created, a new empty 
struct{} will be written to opsFlow chan.
+       *                         2. When the opsCount decrement from 1 to 0, a 
new empty struct{} will be written to opsFlow chan.
+       *                         3. When get a retryable error after 
committing or aborting the transaction,
+       *                    a new empty struct{} will be written to opsFlow 
chan.
+       *        Read:
+       *                         1. When the transaction is committed or 
aborted, an empty struct{} will be read from opsFlow chan.
+       *                         2. When the opsCount increment from 0 to 1, 
an empty struct{} will be read from opsFlow chan.
+        */
+       opsFlow   chan void
+       opsCount  uAtomic.Int32

Review Comment:
   Why do you use `uAtomic` instead of the `sync.atomic` in the standard 
library? We can use `atomic.AddInt32(&txn.opsCount, -1)` and 
`atomic.AddInt32(&txn.opsCount, 1)` instead.



##########
pulsar/transaction_impl.go:
##########
@@ -0,0 +1,235 @@
+// 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 pulsar
+
+import (
+       "context"
+       "sync"
+       "sync/atomic"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       uAtomic "go.uber.org/atomic"
+)
+
+type TxnState int32
+type void struct{}
+type subscription struct {
+       topic        string
+       subscription string
+}
+
+type transaction struct {
+       sync.Mutex
+       txnID                    TxnID
+       state                    TxnState
+       tcClient                 *transactionCoordinatorClient
+       registerPartitions       map[string]void
+       registerAckSubscriptions map[subscription]void
+       /**
+       * opsFlow Wait all the operations of sending and acking messages with 
the transaction complete
+       * by reading msg from the chan.
+       * opsCount is record the number of the uncompleted operations.
+       * opsFlow
+       *        Write:
+       *                     1. When the transaction is created, a new empty 
struct{} will be written to opsFlow chan.
+       *                         2. When the opsCount decrement from 1 to 0, a 
new empty struct{} will be written to opsFlow chan.
+       *                         3. When get a retryable error after 
committing or aborting the transaction,
+       *                    a new empty struct{} will be written to opsFlow 
chan.
+       *        Read:
+       *                         1. When the transaction is committed or 
aborted, an empty struct{} will be read from opsFlow chan.
+       *                         2. When the opsCount increment from 0 to 1, 
an empty struct{} will be read from opsFlow chan.
+        */
+       opsFlow   chan void

Review Comment:
   I see you sometimes pass `void{}` to this channel, sometimes pass 
`struct{}{}`. Could you unify the value? BTW, you can just use a `chan bool` or 
`chan int` for it, `ch <- 0` or `ch <- false` is more simple than `ch <- 
void{}` or `ch <- struct{}{}`.



##########
pulsar/error.go:
##########
@@ -225,3 +226,14 @@ func getResultStr(r Result) string {
                return fmt.Sprintf("Result(%d)", r)
        }
 }
+
+func getErrorFromServerError(serverError *proto.ServerError) error {

Review Comment:
   Maybe we can replace this function with a simple call like:
   
   ```go
   newError(Result(*serverError), serverError.String())
   ```
   
   Because `Result` and `proto.ServerError` should be just the same.



##########
pulsar/transaction_impl.go:
##########
@@ -0,0 +1,235 @@
+// 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 pulsar
+
+import (
+       "context"
+       "sync"
+       "sync/atomic"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       uAtomic "go.uber.org/atomic"
+)
+
+type TxnState int32
+type void struct{}
+type subscription struct {
+       topic        string
+       subscription string
+}
+
+type transaction struct {
+       sync.Mutex
+       txnID                    TxnID
+       state                    TxnState
+       tcClient                 *transactionCoordinatorClient
+       registerPartitions       map[string]void
+       registerAckSubscriptions map[subscription]void
+       /**
+       * opsFlow Wait all the operations of sending and acking messages with 
the transaction complete
+       * by reading msg from the chan.
+       * opsCount is record the number of the uncompleted operations.
+       * opsFlow
+       *        Write:
+       *                     1. When the transaction is created, a new empty 
struct{} will be written to opsFlow chan.
+       *                         2. When the opsCount decrement from 1 to 0, a 
new empty struct{} will be written to opsFlow chan.
+       *                         3. When get a retryable error after 
committing or aborting the transaction,
+       *                    a new empty struct{} will be written to opsFlow 
chan.
+       *        Read:
+       *                         1. When the transaction is committed or 
aborted, an empty struct{} will be read from opsFlow chan.
+       *                         2. When the opsCount increment from 0 to 1, 
an empty struct{} will be read from opsFlow chan.
+        */
+       opsFlow   chan void

Review Comment:
   I agree with @shibd. The `opsFlow` is hard to understand. We usually create 
a channel to handle all events in a separated goroutine, and send events to 
this channel from various goroutines.
   
   See https://go.dev/blog/codelab-share



##########
pulsar/transaction.go:
##########
@@ -17,7 +17,32 @@
 
 package pulsar
 
+import (
+       "context"
+)
+
+const (
+       Open = iota //The init state is open
+       Committing
+       Aborting
+       Committed
+       Aborted
+       Errored
+       TimeOut
+)

Review Comment:
   It's better to add a type name for it. See `MetricsCardinality` in 
`client.go` for example.



##########
pulsar/transaction_impl.go:
##########
@@ -0,0 +1,235 @@
+// 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 pulsar
+
+import (
+       "context"
+       "sync"
+       "sync/atomic"
+       "time"
+
+       pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
+       "github.com/apache/pulsar-client-go/pulsar/log"
+       uAtomic "go.uber.org/atomic"
+)
+
+type TxnState int32

Review Comment:
   I think `TxnState` should be moved to `transaction.go` because it's a public 
type.
   
   ```go
   type TxnState int32
   
   const (
       Open TxnState = iota
       // ...
   )



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to