You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2021/09/07 01:43:18 UTC

[GitHub] [rocketmq-dashboard] vongosling commented on a change in pull request #17: [ISSUE #16]Use an object pool to manage DefaultMQAdminExt objects.

vongosling commented on a change in pull request #17:
URL: https://github.com/apache/rocketmq-dashboard/pull/17#discussion_r703119076



##########
File path: src/main/java/org/apache/rocketmq/dashboard/admin/MQAdminFactory.java
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.rocketmq.dashboard.admin;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.rocketmq.acl.common.AclClientRPCHook;
+import org.apache.rocketmq.acl.common.SessionCredentials;
+import org.apache.rocketmq.dashboard.config.RMQConfigure;
+import org.apache.rocketmq.remoting.RPCHook;
+import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
+import org.apache.rocketmq.tools.admin.MQAdminExt;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MQAdminFactory {
+    private Logger logger = LoggerFactory.getLogger(this.getClass());
+    private RMQConfigure rmqConfigure;
+
+    public MQAdminFactory(RMQConfigure rmqConfigure) {
+        this.rmqConfigure = rmqConfigure;
+    }
+
+    public MQAdminExt getInstance() throws Exception {
+        DefaultMQAdminExt mqAdminExt = null;
+        RPCHook rpcHook = null;
+        boolean isEnableAcl = !StringUtils.isEmpty(rmqConfigure.getAccessKey())

Review comment:
       rmqConfigure.getXXX call twice, does it necessary?

##########
File path: src/main/java/org/apache/rocketmq/dashboard/admin/MQAdminPooledObjectFactory.java
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.rocketmq.dashboard.admin;
+
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+import org.apache.rocketmq.common.protocol.body.ClusterInfo;
+import org.apache.rocketmq.tools.admin.MQAdminExt;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MQAdminPooledObjectFactory implements PooledObjectFactory<MQAdminExt> {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private MQAdminFactory mqAdminFactory;
+
+    @Override
+    public PooledObject<MQAdminExt> makeObject() throws Exception {
+        DefaultPooledObject<MQAdminExt> pooledObject = new DefaultPooledObject<>(
+            mqAdminFactory.getInstance());
+        return pooledObject;
+    }
+
+    @Override
+    public void destroyObject(PooledObject<MQAdminExt> p) {
+        MQAdminExt mqAdmin = p.getObject();
+        if (mqAdmin != null) {
+            try {
+                mqAdmin.shutdown();
+            } catch (Exception e) {
+                logger.warn("MQAdminExt shutdown err", e);
+            }
+        }
+        logger.info("destroy object {}", p.getObject());
+    }
+
+    @Override
+    public boolean validateObject(PooledObject<MQAdminExt> p) {
+        MQAdminExt mqAdmin = p.getObject();
+        ClusterInfo clusterInfo = null;
+        try {
+            clusterInfo = mqAdmin.examineBrokerClusterInfo();
+        } catch (Exception e) {
+            logger.warn("validate object {} err", p.getObject(), e);
+        }
+        if (clusterInfo == null) {
+            return false;
+        }
+        if (clusterInfo.getBrokerAddrTable() == null) {
+            return false;
+        }
+        if (clusterInfo.getBrokerAddrTable().size() <= 0) {
+            return false;

Review comment:
       So many false, how do we judge which branch error?

##########
File path: src/main/java/org/apache/rocketmq/dashboard/admin/MQAdminPooledObjectFactory.java
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.rocketmq.dashboard.admin;
+
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+import org.apache.rocketmq.common.protocol.body.ClusterInfo;
+import org.apache.rocketmq.tools.admin.MQAdminExt;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MQAdminPooledObjectFactory implements PooledObjectFactory<MQAdminExt> {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private MQAdminFactory mqAdminFactory;
+
+    @Override
+    public PooledObject<MQAdminExt> makeObject() throws Exception {
+        DefaultPooledObject<MQAdminExt> pooledObject = new DefaultPooledObject<>(
+            mqAdminFactory.getInstance());
+        return pooledObject;
+    }
+
+    @Override
+    public void destroyObject(PooledObject<MQAdminExt> p) {
+        MQAdminExt mqAdmin = p.getObject();
+        if (mqAdmin != null) {
+            try {
+                mqAdmin.shutdown();
+            } catch (Exception e) {
+                logger.warn("MQAdminExt shutdown err", e);
+            }
+        }
+        logger.info("destroy object {}", p.getObject());
+    }
+
+    @Override
+    public boolean validateObject(PooledObject<MQAdminExt> p) {
+        MQAdminExt mqAdmin = p.getObject();
+        ClusterInfo clusterInfo = null;
+        try {
+            clusterInfo = mqAdmin.examineBrokerClusterInfo();
+        } catch (Exception e) {
+            logger.warn("validate object {} err", p.getObject(), e);
+        }
+        if (clusterInfo == null) {
+            return false;
+        }
+        if (clusterInfo.getBrokerAddrTable() == null) {
+            return false;
+        }
+        if (clusterInfo.getBrokerAddrTable().size() <= 0) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public void activateObject(PooledObject<MQAdminExt> p) {
+

Review comment:
       If you NOOP, you could just log it.

##########
File path: src/main/java/org/apache/rocketmq/dashboard/admin/MQAdminPooledObjectFactory.java
##########
@@ -0,0 +1,86 @@
+/*
+ * 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

Review comment:
       Format confused...




-- 
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: dev-unsubscribe@rocketmq.apache.org

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