You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pegasus.apache.org by wa...@apache.org on 2023/05/17 08:27:25 UTC

[incubator-pegasus] branch master updated: feat(java-client): add listApps interface (#1471)

This is an automated email from the ASF dual-hosted git repository.

wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new 48fd4e87f feat(java-client): add listApps interface (#1471)
48fd4e87f is described below

commit 48fd4e87fd4d067dd93309720954d7d0e230eafe
Author: WHBANG <38...@users.noreply.github.com>
AuthorDate: Wed May 17 16:27:18 2023 +0800

    feat(java-client): add listApps interface (#1471)
    
    https://github.com/apache/incubator-pegasus/issues/1470
    
    Add an interface for the java client to list all tables of the pegasus cluster,
    including or excluding the dropped but still reserved tables.
---
 .../org/apache/pegasus/client/ListAppInfoType.java | 34 +++++++++++
 .../apache/pegasus/client/PegasusAdminClient.java  | 31 ++++++++++
 .../client/PegasusAdminClientInterface.java        | 12 ++++
 .../pegasus/operator/list_apps_operator.java       | 68 ++++++++++++++++++++++
 .../org/apache/pegasus/rpc/async/MetaSession.java  |  3 +
 .../org/apache/pegasus/client/TestAdminClient.java | 31 ++++++++++
 6 files changed, 179 insertions(+)

diff --git a/java-client/src/main/java/org/apache/pegasus/client/ListAppInfoType.java b/java-client/src/main/java/org/apache/pegasus/client/ListAppInfoType.java
new file mode 100644
index 000000000..8fdd58791
--- /dev/null
+++ b/java-client/src/main/java/org/apache/pegasus/client/ListAppInfoType.java
@@ -0,0 +1,34 @@
+/*
+ * 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.pegasus.client;
+
+public enum ListAppInfoType {
+  LT_AVAILABLE_APPS(0),
+  LT_ALL_APPS(1);
+
+  private final int value;
+
+  private ListAppInfoType(int value) {
+    this.value = value;
+  }
+
+  public int getValue() {
+    return value;
+  }
+}
diff --git a/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClient.java b/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClient.java
index dc1636cf4..eb8268cd8 100644
--- a/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClient.java
+++ b/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClient.java
@@ -19,12 +19,14 @@
 
 package org.apache.pegasus.client;
 
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import org.apache.pegasus.base.error_code;
 import org.apache.pegasus.base.gpid;
 import org.apache.pegasus.operator.create_app_operator;
 import org.apache.pegasus.operator.drop_app_operator;
+import org.apache.pegasus.operator.list_apps_operator;
 import org.apache.pegasus.operator.query_cfg_operator;
 import org.apache.pegasus.replication.*;
 import org.apache.pegasus.rpc.Meta;
@@ -230,4 +232,33 @@ public class PegasusAdminClient extends PegasusAbstractClient
           String.format("Drop app:%s failed! error: %s.", appName, error.toString()));
     }
   }
+
+  @Override
+  public List<app_info> listApps(ListAppInfoType listAppInfoType) throws PException {
+    configuration_list_apps_request request = new configuration_list_apps_request();
+    if (listAppInfoType == ListAppInfoType.LT_AVAILABLE_APPS) {
+      // if set request.setStatus as 'AS_AVAILABLE', It will return 'app_info' of all available
+      // tables
+      request.setStatus(app_status.AS_AVAILABLE);
+    } else if (listAppInfoType == ListAppInfoType.LT_ALL_APPS) {
+      // if set request.setStatus as 'AS_INVALID', It will return app_info of all tables, including
+      // dropped but currently reserved tables
+      request.setStatus(app_status.AS_INVALID);
+    } else {
+      throw new PException(String.format("List apps failed, unknown ListAppInfoType."));
+    }
+
+    list_apps_operator app_operator = new list_apps_operator(request);
+    error_code.error_types error = this.meta.operate(app_operator, META_RETRY_MIN_COUNT);
+
+    if (error != error_code.error_types.ERR_OK) {
+      throw new PException(
+          String.format(
+              "List apps failed, query status: %s, error: %s.",
+              request.getStatus(), error.toString()));
+    }
+
+    configuration_list_apps_response response = app_operator.get_response();
+    return response.infos;
+  }
 }
diff --git a/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClientInterface.java b/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClientInterface.java
index bfa1e5bf1..d685a3d39 100644
--- a/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClientInterface.java
+++ b/java-client/src/main/java/org/apache/pegasus/client/PegasusAdminClientInterface.java
@@ -20,7 +20,9 @@
 package org.apache.pegasus.client;
 
 import java.io.Closeable;
+import java.util.List;
 import java.util.Map;
+import org.apache.pegasus.replication.app_info;
 
 public interface PegasusAdminClientInterface extends Closeable {
   /**
@@ -71,6 +73,16 @@ public interface PegasusAdminClientInterface extends Closeable {
 
   public void dropApp(String appName, int reserveSeconds) throws PException;
 
+  /**
+   * Get app infos in the Pegasus cluster
+   *
+   * @param listAppInfoType 'LT_AVAILABLE_APPS' means to return only available tables, 'LT_ALL_APPS'
+   *     means to return all tables, including dropped but currently reserved tables
+   * @return List of 'app_info' in the Pegasus cluster
+   * @throws PException
+   */
+  public List<app_info> listApps(ListAppInfoType listAppInfoType) throws PException;
+
   /** close the client */
   @Override
   public void close();
diff --git a/java-client/src/main/java/org/apache/pegasus/operator/list_apps_operator.java b/java-client/src/main/java/org/apache/pegasus/operator/list_apps_operator.java
new file mode 100644
index 000000000..c70a11b9a
--- /dev/null
+++ b/java-client/src/main/java/org/apache/pegasus/operator/list_apps_operator.java
@@ -0,0 +1,68 @@
+/*
+ * 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.pegasus.operator;
+
+import org.apache.pegasus.base.gpid;
+import org.apache.pegasus.replication.admin_client.list_apps_args;
+import org.apache.pegasus.replication.admin_client.list_apps_result;
+import org.apache.pegasus.replication.configuration_list_apps_request;
+import org.apache.pegasus.replication.configuration_list_apps_response;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TMessage;
+import org.apache.thrift.protocol.TMessageType;
+import org.apache.thrift.protocol.TProtocol;
+
+public class list_apps_operator extends client_operator {
+  public list_apps_operator(configuration_list_apps_request request) {
+    super(new gpid(), "", 0);
+    this.request = request;
+  }
+
+  @Override
+  public String name() {
+    return "list_apps_operator";
+  }
+
+  @Override
+  public void send_data(TProtocol oprot, int sequence_id) throws TException {
+    TMessage msg = new TMessage("RPC_CM_LIST_APPS", TMessageType.CALL, sequence_id);
+    oprot.writeMessageBegin(msg);
+    list_apps_args args = new list_apps_args(request);
+    args.write(oprot);
+    oprot.writeMessageEnd();
+  }
+
+  @Override
+  public void recv_data(TProtocol iprot) throws TException {
+    list_apps_result result = new list_apps_result();
+    result.read(iprot);
+    if (result.isSetSuccess()) response = result.success;
+    else
+      throw new org.apache.thrift.TApplicationException(
+          org.apache.thrift.TApplicationException.MISSING_RESULT,
+          "list apps failed: unknown result");
+  }
+
+  public configuration_list_apps_response get_response() {
+    return response;
+  }
+
+  private configuration_list_apps_request request;
+  private configuration_list_apps_response response;
+}
diff --git a/java-client/src/main/java/org/apache/pegasus/rpc/async/MetaSession.java b/java-client/src/main/java/org/apache/pegasus/rpc/async/MetaSession.java
index 5a0877f35..31bd9ac33 100644
--- a/java-client/src/main/java/org/apache/pegasus/rpc/async/MetaSession.java
+++ b/java-client/src/main/java/org/apache/pegasus/rpc/async/MetaSession.java
@@ -35,6 +35,7 @@ import org.apache.pegasus.base.rpc_address;
 import org.apache.pegasus.operator.client_operator;
 import org.apache.pegasus.operator.create_app_operator;
 import org.apache.pegasus.operator.drop_app_operator;
+import org.apache.pegasus.operator.list_apps_operator;
 import org.apache.pegasus.operator.query_cfg_operator;
 import org.apache.pegasus.replication.partition_configuration;
 
@@ -87,6 +88,8 @@ public class MetaSession extends HostNameResolver {
       return ((create_app_operator) (metaQueryOp)).get_response().getErr().errno;
     } else if (metaQueryOp instanceof drop_app_operator) {
       return ((drop_app_operator) (metaQueryOp)).get_response().getErr().errno;
+    } else if (metaQueryOp instanceof list_apps_operator) {
+      return ((list_apps_operator) (metaQueryOp)).get_response().getErr().errno;
     } else {
       assert (false);
       return null;
diff --git a/java-client/src/test/java/org/apache/pegasus/client/TestAdminClient.java b/java-client/src/test/java/org/apache/pegasus/client/TestAdminClient.java
index 4b76b2b1d..a9b1f4827 100644
--- a/java-client/src/test/java/org/apache/pegasus/client/TestAdminClient.java
+++ b/java-client/src/test/java/org/apache/pegasus/client/TestAdminClient.java
@@ -23,7 +23,9 @@ import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertThat;
 
 import java.util.HashMap;
+import java.util.List;
 import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.pegasus.replication.app_info;
 import org.apache.pegasus.rpc.async.MetaHandler;
 import org.apache.pegasus.rpc.async.MetaSession;
 import org.junit.After;
@@ -134,4 +136,33 @@ public class TestAdminClient {
     pClient.close();
     Assert.fail("expected PException for openTable");
   }
+
+  @Test
+  public void testListApps() throws PException {
+    String appName = "testListApps" + System.currentTimeMillis();
+    List<app_info> appInfoList = toolsClient.listApps(ListAppInfoType.LT_AVAILABLE_APPS);
+    int size1 = appInfoList.size();
+    toolsClient.createApp(
+        appName,
+        this.tablePartitionCount,
+        this.tableReplicaCount,
+        new HashMap<>(),
+        this.tableOpTimeoutMs);
+    boolean isAppHealthy = toolsClient.isAppHealthy(appName, this.tableReplicaCount);
+    Assert.assertTrue(isAppHealthy);
+
+    appInfoList.clear();
+    appInfoList = toolsClient.listApps(ListAppInfoType.LT_AVAILABLE_APPS);
+    Assert.assertEquals(size1 + 1, appInfoList.size());
+    appInfoList.clear();
+    appInfoList = toolsClient.listApps(ListAppInfoType.LT_ALL_APPS);
+    int size2 = appInfoList.size();
+    toolsClient.dropApp(appName, this.tableOpTimeoutMs);
+    appInfoList.clear();
+    appInfoList = toolsClient.listApps(ListAppInfoType.LT_AVAILABLE_APPS);
+    Assert.assertEquals(size1, appInfoList.size());
+    appInfoList.clear();
+    appInfoList = toolsClient.listApps(ListAppInfoType.LT_ALL_APPS);
+    Assert.assertEquals(size2, appInfoList.size());
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pegasus.apache.org
For additional commands, e-mail: commits-help@pegasus.apache.org