You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/09/15 23:47:03 UTC

[GitHub] [pulsar] syhily opened a new issue, #17680: [Improve] Calculate the sticky key hash by using keyBytes instead of Base64 encoded key

syhily opened a new issue, #17680:
URL: https://github.com/apache/pulsar/issues/17680

   ### Search before asking
   
   - [X] I searched in the [issues](https://github.com/apache/pulsar/issues) and found nothing similar.
   
   
   ### Motivation
   
   The method `peekStickyKey` in `Commands.java` is used for acquiring sticky key bytes. But I think the implementation of this method could be wrong.
   
   ```
   public static byte[] peekStickyKey(ByteBuf metadataAndPayload, String topic, String subscription) {
       try {
           int readerIdx = metadataAndPayload.readerIndex();
           MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload);
           metadataAndPayload.readerIndex(readerIdx);
           if (metadata.hasOrderingKey()) {
               return metadata.getOrderingKey();
           } else if (metadata.hasPartitionKey()) {
               return metadata.getPartitionKey().getBytes(StandardCharsets.UTF_8);
           }
       } catch (Throwable t) {
           log.error("[{}] [{}] Failed to peek sticky key from the message metadata", topic, subscription, t);
       }
       return Commands.NONE_KEY;
   }
   ```
   
   The Partition Key in metadata could be defined by using `TypedMessageBuilder#setKeyBytes` or `TypedMessageBuilder#setKey`. The key bytes will be encoded into a Base64 string. So I think we can just decode the key bytes instead of `String#getBytes(StandardCharsets.UTF_8)`.
   
   ### Solution
   
   ```java
   public static byte[] peekStickyKey(ByteBuf metadataAndPayload, String topic, String subscription) {
       try {
           int readerIdx = metadataAndPayload.readerIndex();
           MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload);
           metadataAndPayload.readerIndex(readerIdx);
           if (metadata.hasOrderingKey()) {
               return metadata.getOrderingKey();
           } else if (metadata.hasPartitionKey()) {
               String partitionKey = metadata.getPartitionKey();
               if (metadata.isPartitionKeyB64Encoded()) {
                   return Base64.getDecoder().decode(partitionKey);
               } else {
                   return partitionKey.getBytes(StandardCharsets.UTF_8);
               }
           }
       } catch (Throwable t) {
           log.error("[{}] [{}] Failed to peek sticky key from the message metadata", topic, subscription, t);
       }
       return Commands.NONE_KEY;
   }
   ```
   
   ### Alternatives
   
   _No response_
   
   ### Anything else?
   
   _No response_
   
   ### Are you willing to submit a PR?
   
   - [ ] I'm willing to submit a 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.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] syhily commented on issue #17680: [Improve] Calculate the sticky key hash by using keyBytes instead of Base64 encoded key

Posted by GitBox <gi...@apache.org>.
syhily commented on issue #17680:
URL: https://github.com/apache/pulsar/issues/17680#issuecomment-1248761778

   @codelipenghui Can you help me check 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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] Technoboy- commented on issue #17680: [Improve] Calculate the sticky key hash by using keyBytes instead of Base64 encoded key

Posted by GitBox <gi...@apache.org>.
Technoboy- commented on issue #17680:
URL: https://github.com/apache/pulsar/issues/17680#issuecomment-1248827060

   I think you are right.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece closed issue #17680: [Improve] Calculate the sticky key hash by using keyBytes instead of Base64 encoded key

Posted by GitBox <gi...@apache.org>.
nodece closed issue #17680: [Improve] Calculate the sticky key hash by using keyBytes instead of Base64 encoded key
URL: https://github.com/apache/pulsar/issues/17680


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org