nodece commented on code in PR #999:
URL: https://github.com/apache/pulsar-client-go/pull/999#discussion_r1241028093


##########
pulsar/single_partition_router.go:
##########
@@ -0,0 +1,50 @@
+// 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 "sync"
+
+var (
+       singlePartition *int

Review Comment:
   DO NOT use the global variable.
   
   Please move these variables to line 28.



##########
pulsar/producer_test.go:
##########
@@ -560,6 +560,54 @@ func TestMessageRouter(t *testing.T) {
        assert.NotNil(t, msg)
        assert.Equal(t, string(msg.Payload()), "hello")
 }
+func TestMessageSingleRouter(t *testing.T) {
+       // Create topic with 5 partitions
+       topicAdminURL := 
"admin/v2/persistent/public/default/my-single-partitioned-topic/partitions"
+       err := httpPut(topicAdminURL, 5)
+       defer httpDelete(topicAdminURL)
+       if err != nil {
+               t.Fatal(err)
+       }
+       client, err := NewClient(ClientOptions{
+               URL: serviceURL,
+       })
+
+       assert.Nil(t, err)
+       defer client.Close()
+
+       // Only subscribe on the specific partition
+       consumer, err := client.Subscribe(ConsumerOptions{

Review Comment:
   I suggest you remove this logic, because it is not related to 
`SinglePartitionRouter`.
   



##########
pulsar/single_partition_route_bench_test.go:
##########
@@ -0,0 +1,39 @@
+// 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 (
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func BenchmarkSinglePartitionRouter(b *testing.B) {

Review Comment:
   Perhaps there is no need for performance testing here.



##########
pulsar/single_partition_router.go:
##########
@@ -0,0 +1,50 @@
+// 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 "sync"
+
+var (
+       singlePartition *int
+       once            sync.Once
+)
+
+func NewSinglePartitionRouter() func(*ProducerMessage, TopicMetadata) int {
+       return func(message *ProducerMessage, metadata TopicMetadata) int {
+               numPartitions := metadata.NumPartitions()
+               if len(message.OrderingKey) != 0 {
+                       // When an OrderingKey is specified, use the hash of 
that key
+                       return 
int(getHashingFunction(JavaStringHash)(message.OrderingKey) % numPartitions)
+               }
+
+               if len(message.Key) != 0 {
+                       // When a key is specified, use the hash of that key
+                       return 
int(getHashingFunction(JavaStringHash)(message.Key) % numPartitions)
+               }
+               once.Do(func() {
+                       if singlePartition == nil {

Review Comment:
   ```go
   once.Do(func() {
       partition := r.R.Intn(int(numPartitions))
       singlePartition = &partition
   })
   ```
   
   `f` is only called once.



##########
pulsar/single_partition_router.go:
##########
@@ -0,0 +1,50 @@
+// 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 "sync"
+
+var (
+       singlePartition *int
+       once            sync.Once
+)
+
+func NewSinglePartitionRouter() func(*ProducerMessage, TopicMetadata) int {
+       return func(message *ProducerMessage, metadata TopicMetadata) int {

Review Comment:
   ```suggestion
           singlePartition *int
           once            sync.Once
        return func(message *ProducerMessage, metadata TopicMetadata) int {
   ```



##########
pulsar/producer_test.go:
##########
@@ -560,6 +560,54 @@ func TestMessageRouter(t *testing.T) {
        assert.NotNil(t, msg)
        assert.Equal(t, string(msg.Payload()), "hello")
 }
+func TestMessageSingleRouter(t *testing.T) {
+       // Create topic with 5 partitions
+       topicAdminURL := 
"admin/v2/persistent/public/default/my-single-partitioned-topic/partitions"
+       err := httpPut(topicAdminURL, 5)
+       defer httpDelete(topicAdminURL)
+       if err != nil {
+               t.Fatal(err)
+       }
+       client, err := NewClient(ClientOptions{
+               URL: serviceURL,
+       })
+
+       assert.Nil(t, err)
+       defer client.Close()
+
+       // Only subscribe on the specific partition
+       consumer, err := client.Subscribe(ConsumerOptions{
+               Topic:            "my-single-partitioned-topic",
+               SubscriptionName: "my-sub",
+       })
+
+       assert.Nil(t, err)
+       defer consumer.Close()
+
+       producer, err := client.CreateProducer(ProducerOptions{
+               Topic:         "my-single-partitioned-topic",
+               MessageRouter: NewSinglePartitionRouter(),
+       })
+
+       assert.Nil(t, err)
+       defer producer.Close()
+
+       ctx := context.Background()
+
+       ID, err := producer.Send(ctx, &ProducerMessage{
+               Payload: []byte("hello"),
+       })
+       assert.Nil(t, err)
+       assert.NotNil(t, ID)
+
+       fmt.Println("PUBLISHED")

Review Comment:
   ```suggestion
   ```



##########
pulsar/single_partition_router.go:
##########
@@ -0,0 +1,50 @@
+// 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 "sync"
+
+var (
+       singlePartition *int
+       once            sync.Once
+)
+
+func NewSinglePartitionRouter() func(*ProducerMessage, TopicMetadata) int {
+       return func(message *ProducerMessage, metadata TopicMetadata) int {
+               numPartitions := metadata.NumPartitions()
+               if len(message.OrderingKey) != 0 {

Review Comment:
   I notice that Java only checks the `message.key`, without 
`message.orderingKey`, so we should keep the same implementation.
   



##########
pulsar/single_partition_router.go:
##########
@@ -0,0 +1,50 @@
+// 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 "sync"
+
+var (
+       singlePartition *int
+       once            sync.Once
+)
+
+func NewSinglePartitionRouter() func(*ProducerMessage, TopicMetadata) int {
+       return func(message *ProducerMessage, metadata TopicMetadata) int {

Review Comment:
    And then you need to add a test to verify this.



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