sijie closed pull request #3076: QueryState now spits out FunctionState
URL: https://github.com/apache/pulsar/pull/3076
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/Functions.java
 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/Functions.java
index f909557b8b..ca93c0241e 100644
--- 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/Functions.java
+++ 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/Functions.java
@@ -24,6 +24,7 @@
 import 
org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException;
 import org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException;
 import 
org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException;
+import org.apache.pulsar.common.functions.FunctionState;
 import org.apache.pulsar.common.io.ConnectorDefinition;
 import org.apache.pulsar.common.policies.data.FunctionStats;
 import org.apache.pulsar.functions.proto.InstanceCommunication.FunctionStatus;
@@ -398,6 +399,5 @@ FunctionStats getFunctionStats(String tenant, String 
namespace, String function)
      * @throws PulsarAdminException
      *             Unexpected error
      */
-    String getFunctionState(String tenant, String namespace, String function, 
String key) throws PulsarAdminException;
-
+    FunctionState getFunctionState(String tenant, String namespace, String 
function, String key) throws PulsarAdminException;
 }
diff --git 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/FunctionsImpl.java
 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/FunctionsImpl.java
index 753955e8de..a068a05d6b 100644
--- 
a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/FunctionsImpl.java
+++ 
b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/FunctionsImpl.java
@@ -19,8 +19,8 @@
 package org.apache.pulsar.client.admin.internal;
 
 import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
 import com.google.protobuf.AbstractMessage.Builder;
-import com.google.protobuf.MessageOrBuilder;
 import com.google.protobuf.util.JsonFormat;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -28,6 +28,7 @@
 import org.apache.pulsar.client.admin.PulsarAdminException;
 import org.apache.pulsar.client.api.Authentication;
 import org.apache.pulsar.common.functions.FunctionConfig;
+import org.apache.pulsar.common.functions.FunctionState;
 import org.apache.pulsar.common.functions.WorkerInfo;
 import org.apache.pulsar.common.io.ConnectorDefinition;
 import org.apache.pulsar.common.policies.data.ErrorData;
@@ -366,7 +367,7 @@ public void downloadFunction(String destinationPath, String 
path) throws PulsarA
         }
     }
 
-    public String getFunctionState(String tenant, String namespace, String 
function, String key)
+    public FunctionState getFunctionState(String tenant, String namespace, 
String function, String key)
         throws PulsarAdminException {
         try {
             Response response = request(functions.path(tenant)
@@ -374,7 +375,8 @@ public String getFunctionState(String tenant, String 
namespace, String function,
             if (!response.getStatusInfo().equals(Response.Status.OK)) {
                 throw new ClientErrorException(response);
             }
-            return response.readEntity(String.class);
+            String value = response.readEntity(String.class);
+            return new Gson().fromJson(value, new TypeToken<FunctionState>() 
{}.getType());
         } catch (Exception e) {
             throw getApiException(e);
         }
@@ -384,9 +386,4 @@ public String getFunctionState(String tenant, String 
namespace, String function,
     public static void mergeJson(String json, Builder builder) throws 
IOException {
         JsonFormat.parser().merge(json, builder);
     }
-
-    public static String printJson(MessageOrBuilder msg) throws IOException {
-        return JsonFormat.printer().print(msg);
-    }
-
 }
diff --git 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
index cfe709d489..fea0ffdc5a 100644
--- 
a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
+++ 
b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.java
@@ -53,6 +53,7 @@
 import org.apache.pulsar.common.functions.Resources;
 import org.apache.pulsar.common.functions.Utils;
 import org.apache.pulsar.common.functions.WindowConfig;
+import org.apache.pulsar.common.functions.FunctionState;
 
 @Slf4j
 @Parameters(commandDescription = "Interface for managing Pulsar Functions 
(lightweight, Lambda-style compute processes that work with Pulsar)")
@@ -756,8 +757,9 @@ void runCmd() throws Exception {
         @Override
         void runCmd() throws Exception {
             do {
-                String valueAndVersion = 
admin.functions().getFunctionState(tenant, namespace, functionName, key);
-                System.out.println(valueAndVersion);
+                FunctionState functionState = 
admin.functions().getFunctionState(tenant, namespace, functionName, key);
+                Gson gson = new GsonBuilder().setPrettyPrinting().create();
+                System.out.println(gson.toJson(functionState));
                 if (watch) {
                     Thread.sleep(1000);
                 }
diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/functions/FunctionState.java
 
b/pulsar-common/src/main/java/org/apache/pulsar/common/functions/FunctionState.java
new file mode 100644
index 0000000000..50622477ca
--- /dev/null
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/functions/FunctionState.java
@@ -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.
+ */
+package org.apache.pulsar.common.functions;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import lombok.*;
+
+@Getter
+@AllArgsConstructor
+@ToString
+@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class FunctionState {
+    private String key;
+    private String stringValue;
+    private Long numberValue;
+    private Long version;
+}
diff --git 
a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java
 
b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java
index 7f3ed44c07..6d6394f188 100644
--- 
a/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java
+++ 
b/pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsImpl.java
@@ -81,6 +81,7 @@
 import org.apache.pulsar.client.api.Reader;
 import org.apache.pulsar.client.api.Schema;
 import org.apache.pulsar.common.functions.FunctionConfig;
+import org.apache.pulsar.common.functions.FunctionState;
 import org.apache.pulsar.common.io.ConnectorDefinition;
 import org.apache.pulsar.common.io.SinkConfig;
 import org.apache.pulsar.common.io.SourceConfig;
@@ -1094,15 +1095,14 @@ public Response getFunctionState(final String tenant, 
final String namespace,
                         .entity(new String("key '" + key + "' doesn't exist."))
                         .build();
                 } else {
-                    String value;
+                    FunctionState value;
                     if (kv.isNumber()) {
-                        value = "value : " + kv.numberValue() + ", version : " 
+ kv.version();
+                        value = new FunctionState(key, null, kv.numberValue(), 
kv.version());
                     } else {
-                        value = "value : " + new 
String(ByteBufUtil.getBytes(kv.value()), UTF_8)
-                            + ", version : " + kv.version();
+                        value = new FunctionState(key, new 
String(ByteBufUtil.getBytes(kv.value()), UTF_8), null, kv.version());
                     }
                     return Response.status(Status.OK)
-                        .entity(new String(value))
+                        .entity(new Gson().toJson(value))
                         .build();
                 }
             }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to