|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.rocketmq.dashboard.admin; |
| 18 | + |
| 19 | +import lombok.extern.slf4j.Slf4j; |
| 20 | +import org.apache.commons.pool2.impl.GenericObjectPool; |
| 21 | +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| 22 | +import org.apache.rocketmq.client.ClientConfig; |
| 23 | +import org.apache.rocketmq.dashboard.config.RMQConfigure; |
| 24 | +import org.apache.rocketmq.tools.admin.MQAdminExt; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | + |
| 28 | +import javax.annotation.PreDestroy; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | +import java.util.concurrent.ConcurrentMap; |
| 31 | + |
| 32 | +@Component |
| 33 | +@Slf4j |
| 34 | +public class UserMQAdminPoolManager { |
| 35 | + |
| 36 | + |
| 37 | + private final ConcurrentMap<String/* userAk */, GenericObjectPool<MQAdminExt>> userPools = new ConcurrentHashMap<>(); |
| 38 | + |
| 39 | + private final ClientConfig baseClientConfig; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + public UserMQAdminPoolManager(RMQConfigure rmqConfigure) { |
| 43 | + this.baseClientConfig = new ClientConfig(); |
| 44 | + this.baseClientConfig.setNamesrvAddr(rmqConfigure.getNamesrvAddr()); |
| 45 | + this.baseClientConfig.setClientCallbackExecutorThreads(rmqConfigure.getClientCallbackExecutorThreads()); |
| 46 | + this.baseClientConfig.setVipChannelEnabled(Boolean.parseBoolean(rmqConfigure.getIsVIPChannel())); |
| 47 | + this.baseClientConfig.setUseTLS(rmqConfigure.isUseTLS()); |
| 48 | + log.info("UserMQAdminPoolManager initialized with baseClientConfig for NameServer: {}", rmqConfigure.getNamesrvAddr()); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public MQAdminExt borrowMQAdminExt(String userAk, String userSk) throws Exception { |
| 53 | + GenericObjectPool<MQAdminExt> userPool = userPools.get(userAk); |
| 54 | + |
| 55 | + if (userPool == null) { |
| 56 | + log.info("Creating new MQAdminExt pool for user: {}", userAk); |
| 57 | + GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); |
| 58 | + poolConfig.setMaxTotal(1); |
| 59 | + poolConfig.setMaxIdle(1); |
| 60 | + poolConfig.setMinIdle(0); |
| 61 | + poolConfig.setTestWhileIdle(true); |
| 62 | + poolConfig.setTimeBetweenEvictionRunsMillis(20000); |
| 63 | + poolConfig.setMaxWaitMillis(10000); |
| 64 | + |
| 65 | + UserSpecificMQAdminPooledObjectFactory factory = |
| 66 | + new UserSpecificMQAdminPooledObjectFactory(baseClientConfig, userAk, userSk); |
| 67 | + |
| 68 | + GenericObjectPool<MQAdminExt> newUserPool = new GenericObjectPool<>(factory, poolConfig); |
| 69 | + |
| 70 | + GenericObjectPool<MQAdminExt> existingPool = userPools.putIfAbsent(userAk, newUserPool); |
| 71 | + if (existingPool != null) { |
| 72 | + log.warn("Another thread concurrently created MQAdminExt pool for user {}. Shutting down redundant pool.", userAk); |
| 73 | + newUserPool.close(); |
| 74 | + userPool = existingPool; |
| 75 | + } else { |
| 76 | + userPool = newUserPool; |
| 77 | + log.info("Successfully created and registered MQAdminExt pool for user: {}", userAk); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return userPool.borrowObject(); |
| 82 | + } |
| 83 | + |
| 84 | + public void returnMQAdminExt(String userAk, MQAdminExt mqAdminExt) { |
| 85 | + GenericObjectPool<MQAdminExt> userPool = userPools.get(userAk); |
| 86 | + if (userPool != null) { |
| 87 | + try { |
| 88 | + userPool.returnObject(mqAdminExt); |
| 89 | + log.debug("Returned MQAdminExt object ({}) to pool for user: {}", mqAdminExt, userAk); |
| 90 | + } catch (Exception e) { |
| 91 | + log.error("Failed to return MQAdminExt object ({}) for user {}: {}", mqAdminExt, userAk, e.getMessage(), e); |
| 92 | + if (mqAdminExt != null) { |
| 93 | + try { |
| 94 | + mqAdminExt.shutdown(); |
| 95 | + } catch (Exception se) { |
| 96 | + log.warn("Error shutting down MQAdminExt after failed return: {}", se.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } else { |
| 101 | + log.warn("Attempted to return MQAdminExt for non-existent user pool: {}. Shutting down the object directly.", userAk); |
| 102 | + if (mqAdminExt != null) { |
| 103 | + try { |
| 104 | + mqAdminExt.shutdown(); |
| 105 | + } catch (Exception se) { |
| 106 | + log.warn("Error shutting down MQAdminExt for non-existent pool: {}", se.getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void shutdownUserPool(String userAk) { |
| 113 | + GenericObjectPool<MQAdminExt> userPool = userPools.remove(userAk); |
| 114 | + if (userPool != null) { |
| 115 | + userPool.close(); |
| 116 | + log.info("Shutdown and removed MQAdminExt pool for user: {}", userAk); |
| 117 | + } else { |
| 118 | + log.warn("Attempted to shut down non-existent user pool: {}", userAk); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @PreDestroy |
| 123 | + public void shutdownAllPools() { |
| 124 | + log.info("Shutting down all MQAdminExt user pools..."); |
| 125 | + userPools.forEach((userAk, pool) -> { |
| 126 | + pool.close(); |
| 127 | + log.info("Shutdown MQAdminExt pool for user: {}", userAk); |
| 128 | + }); |
| 129 | + userPools.clear(); |
| 130 | + log.info("All MQAdminExt user pools have been shut down."); |
| 131 | + } |
| 132 | +} |
0 commit comments