Search in sources :

Example 1 with UserThreadPool

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());
}
Also used : UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) Test(org.junit.Test)

Example 2 with UserThreadPool

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());
        }
    }
}
Also used : RpcThreadPoolMonitor(com.alipay.sofa.rpc.boot.common.RpcThreadPoolMonitor) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) HashSet(java.util.HashSet)

Example 3 with UserThreadPool

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);
}
Also used : RpcThreadPoolMonitor(com.alipay.sofa.rpc.boot.common.RpcThreadPoolMonitor) Field(java.lang.reflect.Field) List(java.util.List) Method(java.lang.reflect.Method) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) ActivelyDestroyTest(com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 4 with UserThreadPool

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());
        }
    }
}
Also used : RpcThreadPoolMonitor(com.alipay.sofa.rpc.boot.common.RpcThreadPoolMonitor) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) HashSet(java.util.HashSet)

Example 5 with UserThreadPool

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;
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig)

Aggregations

UserThreadPool (com.alipay.sofa.rpc.server.UserThreadPool)9 RpcThreadPoolMonitor (com.alipay.sofa.rpc.boot.common.RpcThreadPoolMonitor)4 Filter (com.alipay.sofa.rpc.filter.Filter)4 Test (org.junit.Test)3 SofaBootRpcRuntimeException (com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException)2 RpcBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam)2 ActivelyDestroyTest (com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)2 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)2 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)2 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 ExcludeFilter (com.alipay.sofa.rpc.filter.ExcludeFilter)2 SofaMethod (com.alipay.sofa.runtime.api.annotation.SofaMethod)2 SofaParameter (com.alipay.sofa.runtime.api.annotation.SofaParameter)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2