|
| 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 | + |
| 18 | +package org.dromara.dynamictp.adapter.thrift; |
| 19 | + |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | +import org.apache.commons.collections4.CollectionUtils; |
| 22 | +import org.apache.thrift.server.TThreadPoolServer; |
| 23 | +import org.apache.thrift.server.THsHaServer; |
| 24 | +import org.apache.thrift.server.TThreadedSelectorServer; |
| 25 | +import org.dromara.dynamictp.adapter.common.AbstractDtpAdapter; |
| 26 | +import org.dromara.dynamictp.common.properties.DtpProperties; |
| 27 | +import org.dromara.dynamictp.common.util.MethodUtil; |
| 28 | +import org.dromara.dynamictp.common.util.ReflectionUtil; |
| 29 | +import org.dromara.dynamictp.core.support.proxy.ThreadPoolExecutorProxy; |
| 30 | +import org.dromara.dynamictp.jvmti.JVMTI; |
| 31 | + |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.concurrent.ExecutorService; |
| 36 | +import java.util.concurrent.ThreadPoolExecutor; |
| 37 | + |
| 38 | +/** |
| 39 | + * ThriftDtpAdapter for managing Thrift server thread pools |
| 40 | + * |
| 41 | + * @author devin |
| 42 | + * @since 1.2.2 |
| 43 | + */ |
| 44 | +@Slf4j |
| 45 | +@SuppressWarnings("all") |
| 46 | +public class ThriftDtpAdapter extends AbstractDtpAdapter { |
| 47 | + |
| 48 | + private static final String TP_PREFIX = "thriftTp"; |
| 49 | + |
| 50 | + private static final String THREAD_POOL_SERVER_EXECUTOR_FIELD = "executorService_"; |
| 51 | + private static final String HSHASERVER_EXECUTOR_FIELD = "invoker"; |
| 52 | + private static final String THREADED_SELECTOR_WORKER_FIELD = "invoker"; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void refresh(DtpProperties dtpProperties) { |
| 56 | + refresh(dtpProperties.getThriftTp(), dtpProperties.getPlatforms()); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected String getTpPrefix() { |
| 61 | + return TP_PREFIX; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void initialize() { |
| 66 | + super.initialize(); |
| 67 | + List<TThreadPoolServer> tThreadPoolServers = JVMTI.getInstances(TThreadPoolServer.class); |
| 68 | + if (CollectionUtils.isEmpty(tThreadPoolServers)) { |
| 69 | + if (log.isDebugEnabled()) { |
| 70 | + log.debug("Cannot find instances of TThreadPoolServer."); |
| 71 | + } |
| 72 | + } |
| 73 | + tThreadPoolServers.forEach(this::initializeTThreadPoolServer); |
| 74 | + |
| 75 | + List<THsHaServer> tHsHaServers = JVMTI.getInstances(THsHaServer.class); |
| 76 | + if (CollectionUtils.isEmpty(tHsHaServers)) { |
| 77 | + if (log.isDebugEnabled()) { |
| 78 | + log.debug("Cannot find instances of THsHaServer."); |
| 79 | + } |
| 80 | + } |
| 81 | + tHsHaServers.forEach(this::initializeTHsHaServer); |
| 82 | + |
| 83 | + List<TThreadedSelectorServer> tThreadedSelectorServers = JVMTI.getInstances(TThreadedSelectorServer.class); |
| 84 | + if (CollectionUtils.isEmpty(tThreadedSelectorServers)) { |
| 85 | + if (log.isDebugEnabled()) { |
| 86 | + log.debug("Cannot find instances of TThreadedSelectorServer."); |
| 87 | + } |
| 88 | + } |
| 89 | + tThreadedSelectorServers.forEach(this::initializeTThreadedSelectorServer); |
| 90 | + } |
| 91 | + |
| 92 | + public void initializeTThreadPoolServer(TThreadPoolServer server) { |
| 93 | + ThreadPoolExecutor executor = (ThreadPoolExecutor) ReflectionUtil.getFieldValue( |
| 94 | + TThreadPoolServer.class, THREAD_POOL_SERVER_EXECUTOR_FIELD, server); |
| 95 | + if (Objects.nonNull(executor)) { |
| 96 | + int port = getServerPort(server); |
| 97 | + String tpName = genTpName("TThreadPoolServer", port); |
| 98 | + ThreadPoolExecutorProxy proxy = new ThreadPoolExecutorProxy(executor); |
| 99 | + enhanceOriginExecutor(tpName, proxy, THREAD_POOL_SERVER_EXECUTOR_FIELD, server); |
| 100 | + log.info("DynamicTp adapter, thrift TThreadPoolServer executorService_ enhanced, tpName: {}", tpName); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public void initializeTHsHaServer(THsHaServer server) { |
| 105 | + ExecutorService executor = (ExecutorService) ReflectionUtil.getFieldValue( |
| 106 | + THsHaServer.class, HSHASERVER_EXECUTOR_FIELD, server); |
| 107 | + if (Objects.nonNull(executor) && executor instanceof ThreadPoolExecutor) { |
| 108 | + int port = getServerPort(server); |
| 109 | + String tpName = genTpName("THsHaServer", port); |
| 110 | + ThreadPoolExecutorProxy proxy = new ThreadPoolExecutorProxy((ThreadPoolExecutor) executor); |
| 111 | + enhanceOriginExecutor(tpName, proxy, HSHASERVER_EXECUTOR_FIELD, server); |
| 112 | + log.info("DynamicTp adapter, thrift THsHaServer invoker enhanced, tpName: {}", tpName); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void initializeTThreadedSelectorServer(TThreadedSelectorServer server) { |
| 117 | + ExecutorService executor = (ExecutorService) ReflectionUtil.getFieldValue( |
| 118 | + TThreadedSelectorServer.class, THREADED_SELECTOR_WORKER_FIELD, server); |
| 119 | + if (Objects.nonNull(executor) && executor instanceof ThreadPoolExecutor) { |
| 120 | + int port = getServerPort(server); |
| 121 | + String tpName = genTpName("TThreadedSelectorServer", port); |
| 122 | + ThreadPoolExecutorProxy proxy = new ThreadPoolExecutorProxy((ThreadPoolExecutor) executor); |
| 123 | + enhanceOriginExecutor(tpName, proxy, THREADED_SELECTOR_WORKER_FIELD, server); |
| 124 | + log.info("DynamicTp adapter, thrift TThreadedSelectorServer invoker enhanced, tpName: {}", tpName); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private String genTpName(String serverType, int port) { |
| 129 | + return TP_PREFIX + "#" + serverType + "#" + (port > 0 ? port : ""); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Try to get the server port for better naming |
| 134 | + * |
| 135 | + * @param server Thrift server instance |
| 136 | + * @return port number or -1 if not available |
| 137 | + */ |
| 138 | + private int getServerPort(Object server) { |
| 139 | + try { |
| 140 | + Object serverTransport = ReflectionUtil.getFieldValue("serverTransport_", server); |
| 141 | + if (Objects.isNull(serverTransport)) { |
| 142 | + return -1; |
| 143 | + } |
| 144 | + Object serverSocket = ReflectionUtil.getFieldValue("serverSocket_", serverTransport); |
| 145 | + if (Objects.isNull(serverSocket)) { |
| 146 | + return -1; |
| 147 | + } |
| 148 | + Method getLocalPortMethod = ReflectionUtil.findMethod(serverSocket.getClass(), "getLocalPort"); |
| 149 | + if (getLocalPortMethod != null) { |
| 150 | + int port = MethodUtil.invokeAndReturnInt(getLocalPortMethod, serverSocket); |
| 151 | + if (port > 0) { |
| 152 | + return port; |
| 153 | + } |
| 154 | + } |
| 155 | + log.warn("Could not extract port from Thrift server: {}", server.getClass().getSimpleName()); |
| 156 | + } catch (Exception e) { |
| 157 | + log.warn("Error extracting port from Thrift server: {}", e.getMessage()); |
| 158 | + } |
| 159 | + return -1; |
| 160 | + } |
| 161 | +} |
0 commit comments