use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-rpc by sofastack.
the class UserThreadPoolManagerTest method getUserThreadPoolMap.
@Test
public void getUserThreadPoolMap() {
Set<UserThreadPool> userThreadPoolSet = UserThreadPoolManager.getUserThreadPoolSet();
Assert.assertTrue(userThreadPoolSet.isEmpty());
UserThreadPool pool1 = new UserThreadPool();
Assert.assertTrue(pool1.getThreadPoolName().startsWith(UserThreadPool.DEFAUT_POOL_NAME + "-"));
UserThreadPoolManager.registerUserThread("service1", pool1);
userThreadPoolSet = UserThreadPoolManager.getUserThreadPoolSet();
Assert.assertEquals(1, userThreadPoolSet.size());
UserThreadPool pool2 = new UserThreadPool();
Assert.assertTrue(pool2.getThreadPoolName().startsWith(UserThreadPool.DEFAUT_POOL_NAME + "-"));
Assert.assertNotEquals(pool2.getThreadPoolName(), pool1.getThreadPoolName());
UserThreadPoolManager.registerUserThread("service2", pool2);
userThreadPoolSet = UserThreadPoolManager.getUserThreadPoolSet();
Assert.assertEquals(2, userThreadPoolSet.size());
UserThreadPool pool3 = new UserThreadPool("samePoolName");
UserThreadPoolManager.registerUserThread("service3", pool3);
UserThreadPool pool4 = new UserThreadPool("samePoolName");
UserThreadPoolManager.registerUserThread("service4", pool4);
userThreadPoolSet = UserThreadPoolManager.getUserThreadPoolSet();
Assert.assertEquals(4, userThreadPoolSet.size());
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by sofastack.
the class ServerConfigContainer method startCustomThreadPoolMonitor.
private void startCustomThreadPoolMonitor() {
Set<UserThreadPool> userThreadPoolSet = UserThreadPoolManager.getUserThreadPoolSet();
if (!userThreadPoolSet.isEmpty()) {
Set<String> poolNames = new HashSet<>();
for (UserThreadPool pool : userThreadPoolSet) {
RpcThreadPoolMonitor customThreadPoolMonitor = new RpcThreadPoolMonitor(LoggerConstant.CUSTOM_THREAD_LOGGER_NAME);
customThreadPoolMonitorList.add(customThreadPoolMonitor);
if (poolNames.contains(pool.getThreadPoolName())) {
// use to distinguish some UserThreadPools set same poolName
customThreadPoolMonitor.setPoolName(pool.getThreadPoolName() + "-" + pool.hashCode());
} else {
customThreadPoolMonitor.setPoolName(pool.getThreadPoolName());
}
customThreadPoolMonitor.setThreadPoolExecutor(pool.getExecutor());
customThreadPoolMonitor.start();
poolNames.add(pool.getThreadPoolName());
}
}
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by sofastack.
the class ServerConfigContainerTest method testStartCustomThreadPoolMonitor.
@Test
public void testStartCustomThreadPoolMonitor() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
UserThreadPoolManager.registerUserThread("service1", new UserThreadPool());
UserThreadPoolManager.registerUserThread("service2", new UserThreadPool());
UserThreadPoolManager.registerUserThread("service3", new UserThreadPool("same-name"));
UserThreadPoolManager.registerUserThread("service4", new UserThreadPool("same-name"));
Method privateStartMethod = serverConfigContainer.getClass().getDeclaredMethod("startCustomThreadPoolMonitor");
privateStartMethod.setAccessible(true);
privateStartMethod.invoke(serverConfigContainer);
Field privateField = serverConfigContainer.getClass().getDeclaredField("customThreadPoolMonitorList");
privateField.setAccessible(true);
Object value = privateField.get(serverConfigContainer);
List<RpcThreadPoolMonitor> customThreadPoolMonitorList = (List<RpcThreadPoolMonitor>) value;
Assert.assertEquals(customThreadPoolMonitorList.size(), 4);
boolean hasHashCode = false;
for (RpcThreadPoolMonitor monitor : customThreadPoolMonitorList) {
if (monitor.getPoolName().contains("same-name-")) {
hasHashCode = true;
}
}
Assert.assertTrue(hasHashCode);
Method privateStopMethod = serverConfigContainer.getClass().getDeclaredMethod("stopCustomThreadPoolMonitor");
privateStopMethod.setAccessible(true);
privateStopMethod.invoke(serverConfigContainer);
Assert.assertEquals(customThreadPoolMonitorList.size(), 0);
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by alipay.
the class ServerConfigContainer method startCustomThreadPoolMonitor.
private void startCustomThreadPoolMonitor() {
Set<UserThreadPool> userThreadPoolSet = UserThreadPoolManager.getUserThreadPoolSet();
if (!userThreadPoolSet.isEmpty()) {
Set<String> poolNames = new HashSet<>();
for (UserThreadPool pool : userThreadPoolSet) {
RpcThreadPoolMonitor customThreadPoolMonitor = new RpcThreadPoolMonitor(LoggerConstant.CUSTOM_THREAD_LOGGER_NAME);
customThreadPoolMonitorList.add(customThreadPoolMonitor);
if (poolNames.contains(pool.getThreadPoolName())) {
// use to distinguish some UserThreadPools set same poolName
customThreadPoolMonitor.setPoolName(pool.getThreadPoolName() + "-" + pool.hashCode());
} else {
customThreadPoolMonitor.setPoolName(pool.getThreadPoolName());
}
customThreadPoolMonitor.setThreadPoolExecutor(pool.getExecutor());
customThreadPoolMonitor.start();
poolNames.add(pool.getThreadPoolName());
}
}
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by alipay.
the class ProviderConfigHelper method getProviderConfig.
/**
* 获取 ProviderConfig
*
* @param contract the Contract
* @param binding the RpcBinding
* @param target 服务实例
* @return the ProviderConfig
* @throws SofaBootRpcRuntimeException
*/
public ProviderConfig getProviderConfig(Contract contract, RpcBinding binding, Object target) throws SofaBootRpcRuntimeException {
RpcBindingParam param = binding.getRpcBindingParam();
String id = binding.getBeanId();
String interfaceId = contract.getInterfaceType().getName();
Object ref = target;
String uniqueId = contract.getUniqueId();
Integer timeout = param.getTimeout();
Integer weight = param.getWeight();
Integer warmupTime = param.getWarmUpTime();
Integer warmupWeight = param.getWarmUpWeight();
UserThreadPool threadPool = param.getUserThreadPool();
String serialization = param.getSerialization();
List<Filter> filters = param.getFilters();
List<MethodConfig> methodConfigs = convertToMethodConfig(param.getMethodInfos());
ServerConfig serverConfig = serverConfigContainer.getServerConfig(binding.getBindingType().getType());
ProviderConfig providerConfig = new ProviderConfig();
if (StringUtils.hasText(appName)) {
providerConfig.setApplication(new ApplicationConfig().setAppName(appName));
}
if (StringUtils.hasText(id)) {
providerConfig.setId(id);
}
if (StringUtils.hasText(interfaceId)) {
providerConfig.setInterfaceId(interfaceId);
}
if (contract.getInterfaceType() != null) {
providerConfig.setProxyClass(contract.getInterfaceType());
}
if (ref != null) {
providerConfig.setRef(ref);
}
if (StringUtils.hasText(uniqueId)) {
providerConfig.setUniqueId(uniqueId);
}
if (timeout != null) {
providerConfig.setTimeout(timeout);
}
if (weight != null) {
providerConfig.setWeight(weight);
}
if (warmupTime != null) {
providerConfig.setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, String.valueOf(warmupTime));
}
if (warmupWeight != null) {
providerConfig.setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, String.valueOf(warmupWeight));
}
if (!CollectionUtils.isEmpty(filters)) {
providerConfig.setFilterRef(filters);
}
if (!CollectionUtils.isEmpty(methodConfigs)) {
providerConfig.setMethods(methodConfigs);
}
if (threadPool != null) {
UserThreadPoolManager.registerUserThread(ConfigUniqueNameGenerator.getUniqueName(providerConfig), threadPool);
}
providerConfig.setServer(serverConfig);
String protocol = binding.getBindingType().getType();
// http protocol use default protocol
if (!SofaBootRpcConfigConstants.RPC_PROTOCOL_HTTP.equals(protocol)) {
providerConfig.setBootstrap(protocol);
}
if (StringUtils.hasText(serialization)) {
providerConfig.setSerialization(serialization);
}
if (param.getParameters() != null) {
providerConfig.setParameters(param.getParameters());
}
if (param.getRegistrys() != null && param.getRegistrys().size() > 0) {
List<String> registrys = param.getRegistrys();
for (String registryAlias : registrys) {
RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(registryAlias);
providerConfig.setRegistry(registryConfig);
}
} else if (registryConfigContainer.isMeshEnabled(protocol)) {
RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
providerConfig.setRegistry(registryConfig);
} else {
RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig();
providerConfig.setRegistry(registryConfig);
}
providerConfig.setRegister(false);
return providerConfig;
}
Aggregations