You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "jiangpengcheng (via GitHub)" <gi...@apache.org> on 2024/01/25 03:19:10 UTC

[PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

jiangpengcheng opened a new pull request, #21966:
URL: https://github.com/apache/pulsar/pull/21966

   <!--
   ### Contribution Checklist
     
     - PR title format should be *[type][component] summary*. For details, see *[Guideline - Pulsar PR Naming Convention](https://pulsar.apache.org/contribute/develop-semantic-title/)*. 
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   -->
   
   <!-- Either this PR fixes an issue, -->
   
   Fixes #xyz
   
   <!-- or this PR is one task of an issue -->
   
   Main Issue: #xyz
   
   <!-- If the PR belongs to a PIP, please add the PIP link here -->
   
   PIP: #xyz 
   
   <!-- Details of when a PIP is required and how the PIP process work, please see: https://github.com/apache/pulsar/blob/master/pip/README.md -->
   
   ### Motivation
   
   https://github.com/apache/pulsar/pull/21597 introduce a change which removes the `version` field for the `querystate` API. This may break users' behavior, need to add it back
   
   ### Modifications
   
   1. Add a new class `org.apache.pulsar.functions.api.state.StateValue`, which contains `value`, `version`, and `isNumber` fields.
   2. Add two new methods to the `org.apache.pulsar.functions.api.state.ByteBufferStateStore` interface which return a `StateValue`:
       1. default StateValue getStateValue(String key)
       2. default StateValue getStateValueAsync(String key)
   3.  Use `DefaultStateStore#getStateValue` method instead of `DefaultStateStore#get` in `org.apache.pulsar.functions.worker.rest.api.ComponentImpl` to get the `version` field from the backend state store and return it to users.
   
   
   ### Verifying this change
   
   - [x] Make sure that the change passes the CI checks.
   
   - [x] This change added tests and can be verified as follows:
   
     - *Update integration tests for `PulsarStateTest#testPythonWordCountFunction`, now it will check the `version` field too.*
   
   ### Does this pull request potentially affect one of the following parts:
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   *If the box was checked, please highlight the changes*
   
   - [ ] Dependencies (add or upgrade a dependency)
   - [ ] The public API
   - [ ] The schema
   - [ ] The default values of configurations
   - [ ] The threading model
   - [ ] The binary protocol
   - [ ] The REST endpoints
   - [ ] The admin CLI options
   - [ ] The metrics
   - [ ] Anything that affects deployment
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: https://github.com/jiangpengcheng/pulsar/pull/24
   
   <!--
   After opening this PR, the build in apache/pulsar will fail and instructions will
   be provided for opening a PR in the PR author's forked repository.
   
   apache/pulsar pull requests should be first tested in your own fork since the 
   apache/pulsar CI based on GitHub Actions has constrained resources and quota.
   GitHub Actions provides separate quota for pull requests that are executed in 
   a forked repository.
   
   The tests will be run in the forked repository until all PR review comments have
   been handled, the tests pass and the PR is approved by a reviewer.
   -->
   


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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466188662


##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);

Review Comment:
   but the result is a new init `ByteBuffer`, I think its position was 0 before reading?



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466187778


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -18,21 +18,19 @@
  */
 package org.apache.pulsar.functions.api.state;
 
-import java.nio.ByteBuffer;
-
 public class StateValue {
-    private final ByteBuffer value;
+    private final byte[] value;
     private final Long version;
     private final Boolean isNumber;
 
-    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
-        this.value = value == null ? null : ByteBuffer.wrap(value.array());
+    public StateValue(byte[] value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : value.clone();

Review Comment:
   is there a need to make a copy of the input array?



##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);
+                            return new StateValue(result, data.version(), data.isNumber());
+                        }
+                        return null;
+                    } finally {
+                        if (data != null) {
+                            ReferenceCountUtil.safeRelease(data);

Review Comment:
   Oh yes, it's a Netty ByteBuf



##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -18,21 +18,19 @@
  */
 package org.apache.pulsar.functions.api.state;
 
-import java.nio.ByteBuffer;
-
 public class StateValue {
-    private final ByteBuffer value;
+    private final byte[] value;
     private final Long version;
     private final Boolean isNumber;
 
-    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
-        this.value = value == null ? null : ByteBuffer.wrap(value.array());
+    public StateValue(byte[] value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : value.clone();
         this.version = version;
         this.isNumber = isNumber;
     }
 
-    public ByteBuffer getValue() {
-        return value == null ? null : ByteBuffer.wrap(value.array());
+    public byte[] getValue() {
+        return this.value == null ? null : this.value.clone();

Review Comment:
   is it necessary to clone the returned value? I understand that this could make sense in some cases, but is there a real need to have defensive copies?



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466181704


##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);

Review Comment:
   got it now, I just used the same code in line: https://github.com/apache/pulsar/blob/b56efff3f125b107d39eb7a88c040badc721f692/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java#L179



##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);

Review Comment:
   got it now, I just used the same code with line: https://github.com/apache/pulsar/blob/b56efff3f125b107d39eb7a88c040badc721f692/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java#L179



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "eolivelli (via GitHub)" <gi...@apache.org>.
eolivelli commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466181381


##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,33 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null && data.value().readableBytes() > 0) {

Review Comment:
   `data.value().readableBytes() > 0`
   
   are we handling an empty value as a null ?



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466197192


##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,33 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null && data.value().readableBytes() > 0) {

Review Comment:
   updated



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466181704


##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);

Review Comment:
   got it now, I just used the same code with line: https://github.com/apache/pulsar/blob/b56efff3f125b107d39eb7a88c040badc721f692/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java#L179



##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);
+                            return new StateValue(result, data.version(), data.isNumber());
+                        }
+                        return null;
+                    } finally {
+                        if (data != null) {
+                            ReferenceCountUtil.safeRelease(data);

Review Comment:
   got it now, I just used the same code with line: https://github.com/apache/pulsar/blob/b56efff3f125b107d39eb7a88c040badc721f692/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java#L179



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466023980


##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/PulsarMetadataStateStoreImpl.java:
##########
@@ -111,6 +112,20 @@ public CompletableFuture<ByteBuffer> getAsync(String key) {
                                 .orElse(null));
     }
 
+    @Override
+    public StateValue getStateValue(String key) {
+        return getStateValueAsync(key).join();
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return store.get(getPath(key))
+                .thenApply(optRes ->
+                        optRes.map(x ->
+                            new StateValue(ByteBuffer.wrap(x.getValue()), x.getStat().getVersion(), null))

Review Comment:
   Is there a benefit of `ByteBuffer.wrap` when it's immediately accessed directly in `StateValue` constructor (in the questionable way)? Perhaps it would be better to pass what `byte[]` directly?



##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.pulsar.functions.api.state;
+
+import java.nio.ByteBuffer;
+
+public class StateValue {
+    private final ByteBuffer value;
+    private final Long version;
+    private final Boolean isNumber;
+
+    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : ByteBuffer.wrap(value.array());

Review Comment:
   The javadoc of `ByteBuffer.array` method says that the method should only be called if `hasArray()` returns true.
   The underlying array might have a specific position and limit. I think that `ByteBuffer.wrap(value.array())` is not a very good approach for duplicating a ByteBuffer.



##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);
+                            return new StateValue(result, data.version(), data.isNumber());
+                        }
+                        return null;
+                    } finally {
+                        if (data != null) {
+                            ReferenceCountUtil.safeRelease(data);

Review Comment:
   This doesn't make sense since `ReferenceCountUtil` is used for Netty's `ByteBuf`. In this case, we are using `java.nio.ByteBuffer`.



##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/BKStateStoreImpl.java:
##########
@@ -190,4 +191,39 @@ public ByteBuffer get(String key) {
             throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
         }
     }
+
+    @Override
+    public StateValue getStateValue(String key) {
+        try {
+            return result(getStateValueAsync(key));
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to retrieve the state value for key '" + key + "'", e);
+        }
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))).thenApply(
+                data -> {
+                    try {
+                        if (data != null && data.value() != null) {
+                            ByteBuffer result = ByteBuffer.allocate(data.value().readableBytes());
+                            data.value().readBytes(result);
+                            // Set position to off the buffer to the beginning, since the position after the
+                            // read is going to be end of the buffer
+                            // If we do not rewind to the beginning here, users will have to explicitly do
+                            // this in their function code
+                            // in order to use any of the ByteBuffer operations
+                            result.position(0);

Review Comment:
   isn't this making the assumption that position was 0 before reading? Aren't ByteBuffer's `mark` and `reset` meant to be used for this type of use cases?



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466201328


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -18,21 +18,19 @@
  */
 package org.apache.pulsar.functions.api.state;
 
-import java.nio.ByteBuffer;
-
 public class StateValue {
-    private final ByteBuffer value;
+    private final byte[] value;
     private final Long version;
     private final Boolean isNumber;
 
-    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
-        this.value = value == null ? null : ByteBuffer.wrap(value.array());
+    public StateValue(byte[] value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : value.clone();
         this.version = version;
         this.isNumber = isNumber;
     }
 
-    public ByteBuffer getValue() {
-        return value == null ? null : ByteBuffer.wrap(value.array());
+    public byte[] getValue() {
+        return this.value == null ? null : this.value.clone();

Review Comment:
   Just to pass the `EI_EXPOSE_REP` check, is it ok to ignore it?



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466178046


##########
tests/integration/src/test/java/org/apache/pulsar/tests/integration/functions/PulsarStateTest.java:
##########
@@ -480,6 +480,7 @@ private void queryState(String functionName, String key, int amount)
             "--key", key
         );
         assertTrue(result.getStdout().contains("\"numberValue\": " + amount));
+        assertTrue(result.getStdout().contains("\"version\": " + version));

Review Comment:
   added below:
   
   ```
           assertFalse(result.getStdout().contains("stringValue"));
           assertFalse(result.getStdout().contains("byteValue"));
   ```



##########
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/state/PulsarMetadataStateStoreImpl.java:
##########
@@ -111,6 +112,20 @@ public CompletableFuture<ByteBuffer> getAsync(String key) {
                                 .orElse(null));
     }
 
+    @Override
+    public StateValue getStateValue(String key) {
+        return getStateValueAsync(key).join();
+    }
+
+    @Override
+    public CompletableFuture<StateValue> getStateValueAsync(String key) {
+        return store.get(getPath(key))
+                .thenApply(optRes ->
+                        optRes.map(x ->
+                            new StateValue(ByteBuffer.wrap(x.getValue()), x.getStat().getVersion(), null))

Review Comment:
   Great, I will use `byte[]` 



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#issuecomment-1910594908

   > It's partly resolved, if the backend state store can specify whether a state value is a number(BK supports this), it won't return `stringValue` and `byteValue`
   
   This happened with BK and 3.2.0 rc4. What does it mean that it's partly resolved?


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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466179388


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.pulsar.functions.api.state;
+
+import java.nio.ByteBuffer;
+
+public class StateValue {
+    private final ByteBuffer value;
+    private final Long version;
+    private final Boolean isNumber;
+
+    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : ByteBuffer.wrap(value.array());

Review Comment:
   updated using `byte[]` instead of `ByteBuffer`



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466277615


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -18,21 +18,19 @@
  */
 package org.apache.pulsar.functions.api.state;
 
-import java.nio.ByteBuffer;
-
 public class StateValue {
-    private final ByteBuffer value;
+    private final byte[] value;
     private final Long version;
     private final Boolean isNumber;
 
-    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
-        this.value = value == null ? null : ByteBuffer.wrap(value.array());
+    public StateValue(byte[] value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : value.clone();
         this.version = version;
         this.isNumber = isNumber;
     }
 
-    public ByteBuffer getValue() {
-        return value == null ? null : ByteBuffer.wrap(value.array());
+    public byte[] getValue() {
+        return this.value == null ? null : this.value.clone();

Review Comment:
   updated



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466662553


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.pulsar.functions.api.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class StateValue {
+    private final byte[] value;
+    private final Long version;
+    private final Boolean isNumber;

Review Comment:
   Should these use primitive types? `Long` -> `long` and `Boolean` -> `boolean`?
   what does it mean that `isNumber` is null? wouldn't it be more explicity with primitive types? Isn't the `version` always known?



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1467139078


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.pulsar.functions.api.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class StateValue {
+    private final byte[] value;
+    private final Long version;
+    private final Boolean isNumber;

Review Comment:
   Some `StateStoreImpl` may not be able to judge whether a value is a number or not, then it will use `null` for the `isNumber` field.
   
   So is the `version` field, using `Long` makes it more "extendable", it will allow developers to implement and use `StateStoreImpl` which doesn't have "version" concept.



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1467139078


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.pulsar.functions.api.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class StateValue {
+    private final byte[] value;
+    private final Long version;
+    private final Boolean isNumber;

Review Comment:
   Some `StateStoreImpl` may not be able to judge whether a value is a number or not, then it will use `null` for the `isNumber` field.
   
   So is the `version` field, using `Long` makes it more "extendable", it allows to implement `StateStoreImpl` which doesn't have "version" concept



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#issuecomment-1911255173

   > > It's partly resolved, if the backend state store can specify whether a state value is a number(BK supports this), it won't return `stringValue` and `byteValue`
   > 
   > This happened with BK and 3.2.0 rc4. What does it mean that it's partly resolved?
   
   When using the BK state store, it won't return `stringValue` or `byteValue` if the value is a number, so it's fixed for BK.
   
   But if users use another state store, this state store may not be able to specify whether a state value is a number or a string(such as the `PulsarMetadataStateStoreImpl`),  then when the length of a state value's `byte[]` is equal to `Long.BYTES`, we can only return `numberValue` along with `stringValue` or `byteValue` because this state value can either be a number or string.
   
   This is also the reason why the type of `isNumber` is `Boolean`, because not all state store has the ability to know whether a state value is a number or 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.

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

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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#issuecomment-1912014655

   /pulsarbot rerun-failure-checks


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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "Technoboy- (via GitHub)" <gi...@apache.org>.
Technoboy- merged PR #21966:
URL: https://github.com/apache/pulsar/pull/21966


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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466052609


##########
tests/integration/src/test/java/org/apache/pulsar/tests/integration/functions/PulsarStateTest.java:
##########
@@ -480,6 +480,7 @@ private void queryState(String functionName, String key, int amount)
             "--key", key
         );
         assertTrue(result.getStdout().contains("\"numberValue\": " + amount));
+        assertTrue(result.getStdout().contains("\"version\": " + version));

Review Comment:
   It would be better to parse the JSON and validate that it doesn't contain extra keys such as `"stringValue": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n"` which was observed in the release candidate validation.



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#issuecomment-1909700310

   Regarding https://apache-pulsar.slack.com/archives/C5ZSVEN4E/p1706147824703029?thread_ts=1705826989.958419&cid=C5ZSVEN4E , 
   
   > now the display changed to
   > ```
   > {
   >   "key": "hello",
   >   "stringValue": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n",
   >   "numberValue": 10
   > }
   > ```
   > 
   
   What caused the `stringValue` key to appear? Is there also a fix for that?


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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#discussion_r1466208284


##########
pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/state/StateValue.java:
##########
@@ -18,21 +18,19 @@
  */
 package org.apache.pulsar.functions.api.state;
 
-import java.nio.ByteBuffer;
-
 public class StateValue {
-    private final ByteBuffer value;
+    private final byte[] value;
     private final Long version;
     private final Boolean isNumber;
 
-    public StateValue(ByteBuffer value, Long version, Boolean isNumber) {
-        this.value = value == null ? null : ByteBuffer.wrap(value.array());
+    public StateValue(byte[] value, Long version, Boolean isNumber) {
+        this.value = value == null ? null : value.clone();
         this.version = version;
         this.isNumber = isNumber;
     }
 
-    public ByteBuffer getValue() {
-        return value == null ? null : ByteBuffer.wrap(value.array());
+    public byte[] getValue() {
+        return this.value == null ? null : this.value.clone();

Review Comment:
   Yes, it's fine to omit the check. You can omit it with `@SuppressFBWarnings({"EI_EXPOSE_REP"})` or by adding an entry to the `src/main/resources/findbugsExclude.xml` file. I think that the latter approach is better so that `SuppressFBWarnings` annotation doesn't leak into client API classes.



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


Re: [PR] [fix][fn] Add missing `version` field back to `querystate` API [pulsar]

Posted by "jiangpengcheng (via GitHub)" <gi...@apache.org>.
jiangpengcheng commented on PR #21966:
URL: https://github.com/apache/pulsar/pull/21966#issuecomment-1909906536

   > Regarding https://apache-pulsar.slack.com/archives/C5ZSVEN4E/p1706147824703029?thread_ts=1705826989.958419&cid=C5ZSVEN4E ,
   > 
   > > now the display changed to
   > > ```
   > > {
   > >   "key": "hello",
   > >   "stringValue": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\n",
   > >   "numberValue": 10
   > > }
   > > ```
   > 
   > What caused the `stringValue` key to appear? Is there also a fix for that?
   
   It's partly resolved, if the backend state store can specify whether a state value is a number(BK supports this), it won't return `stringValue` and `byteValue`
   
   But if a backend state store doesn't have such ability, we can't tell whether a state value is a number even its length is `Long.BYTES`, in this case, a `stringValue` or `byteValue` is returned with the `numberValue`


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