mirror of https://github.com/longtai-cn/hippo4j
parent
4863f8ef47
commit
ef93ab435d
@ -0,0 +1,77 @@
|
|||||||
|
package cn.hippo4j.common.model.register;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool register parameter.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DynamicThreadPoolRegisterParameter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread-pool id
|
||||||
|
* Empty or empty strings are not allowed, and `+` signs are not allowed
|
||||||
|
*/
|
||||||
|
private String threadPoolId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Content
|
||||||
|
*/
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Core pool size
|
||||||
|
*/
|
||||||
|
private Integer corePoolSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum pool size
|
||||||
|
*/
|
||||||
|
private Integer maximumPoolSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queue type
|
||||||
|
*/
|
||||||
|
private Integer queueType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capacity
|
||||||
|
*/
|
||||||
|
private Integer capacity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep alive time
|
||||||
|
*/
|
||||||
|
private Integer keepAliveTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rejected type
|
||||||
|
*/
|
||||||
|
private Integer rejectedType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is alarm
|
||||||
|
*/
|
||||||
|
private Integer isAlarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capacity alarm
|
||||||
|
*/
|
||||||
|
private Integer capacityAlarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liveness alarm
|
||||||
|
*/
|
||||||
|
private Integer livenessAlarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow core thread timeout
|
||||||
|
*/
|
||||||
|
private Integer allowCoreThreadTimeOut;
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package cn.hippo4j.common.model.register;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool register wrapper.
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DynamicThreadPoolRegisterWrapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tenant id
|
||||||
|
*/
|
||||||
|
private String tenantId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Item id
|
||||||
|
*/
|
||||||
|
private String itemId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update if exists
|
||||||
|
*/
|
||||||
|
private Boolean updateIfExists = Boolean.TRUE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool executor
|
||||||
|
*/
|
||||||
|
@JsonIgnore
|
||||||
|
private ThreadPoolExecutor dynamicThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool register parameter
|
||||||
|
*/
|
||||||
|
private DynamicThreadPoolRegisterParameter dynamicThreadPoolRegisterParameter;
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package cn.hippo4j.core.executor.support;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool service.
|
||||||
|
*/
|
||||||
|
public interface DynamicThreadPoolService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registering dynamic thread pools at runtime.
|
||||||
|
*
|
||||||
|
* @param registerWrapper
|
||||||
|
*/
|
||||||
|
void registerDynamicThreadPool(DynamicThreadPoolRegisterWrapper registerWrapper);
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* 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 cn.hippo4j.springboot.starter.core;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
|
||||||
|
import cn.hippo4j.common.toolkit.Assert;
|
||||||
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
||||||
|
import cn.hippo4j.common.web.base.Result;
|
||||||
|
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
||||||
|
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
||||||
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter;
|
||||||
|
import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper;
|
||||||
|
import cn.hippo4j.core.executor.support.DynamicThreadPoolService;
|
||||||
|
import cn.hippo4j.springboot.starter.config.BootstrapProperties;
|
||||||
|
import cn.hippo4j.springboot.starter.event.ApplicationCompleteEvent;
|
||||||
|
import cn.hippo4j.springboot.starter.remote.HttpAgent;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
|
||||||
|
import static cn.hippo4j.common.constant.Constants.REGISTER_DYNAMIC_THREAD_POOL_PATH;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool config service.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DynamicThreadPoolConfigService implements DynamicThreadPoolService, ApplicationListener<ApplicationCompleteEvent> {
|
||||||
|
|
||||||
|
private final HttpAgent httpAgent;
|
||||||
|
|
||||||
|
private final ClientWorker clientWorker;
|
||||||
|
|
||||||
|
private final BootstrapProperties properties;
|
||||||
|
|
||||||
|
private final DynamicThreadPoolSubscribeConfig dynamicThreadPoolSubscribeConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationCompleteEvent event) {
|
||||||
|
clientWorker.notifyApplicationComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerDynamicThreadPool(DynamicThreadPoolRegisterWrapper registerWrapper) {
|
||||||
|
DynamicThreadPoolRegisterParameter registerParameter = registerWrapper.getDynamicThreadPoolRegisterParameter();
|
||||||
|
checkThreadPoolParameter(registerParameter);
|
||||||
|
ThreadPoolParameterInfo parameter = JSONUtil.parseObject(JSONUtil.toJSONString(registerParameter), ThreadPoolParameterInfo.class);
|
||||||
|
String threadPoolId = registerParameter.getThreadPoolId();
|
||||||
|
Result registerResult;
|
||||||
|
try {
|
||||||
|
failDynamicThreadPoolRegisterWrapper(registerWrapper);
|
||||||
|
registerResult = httpAgent.httpPost(REGISTER_DYNAMIC_THREAD_POOL_PATH, registerWrapper);
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
log.error("Failed to dynamically register thread pool: {}", threadPoolId, ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
if (registerResult.isSuccess()) {
|
||||||
|
DynamicThreadPoolWrapper dynamicThreadPoolWrapper = DynamicThreadPoolWrapper.builder()
|
||||||
|
.threadPoolId(threadPoolId)
|
||||||
|
.executor(registerWrapper.getDynamicThreadPoolExecutor())
|
||||||
|
.build();
|
||||||
|
GlobalThreadPoolManage.register(threadPoolId, parameter, dynamicThreadPoolWrapper);
|
||||||
|
dynamicThreadPoolSubscribeConfig.subscribeConfig(threadPoolId);
|
||||||
|
clientWorker.addCacheDataIfAbsent(properties.getNamespace(), properties.getItemId(), threadPoolId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkThreadPoolParameter(DynamicThreadPoolRegisterParameter registerParameter) {
|
||||||
|
Assert.isTrue(!registerParameter.getThreadPoolId().contains("+"), "The thread pool contains sensitive characters.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void failDynamicThreadPoolRegisterWrapper(DynamicThreadPoolRegisterWrapper registerWrapper) {
|
||||||
|
registerWrapper.setTenantId(properties.getNamespace());
|
||||||
|
registerWrapper.setItemId(properties.getItemId());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package cn.hippo4j.springboot.starter.core;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.api.ThreadPoolDynamicRefresh;
|
||||||
|
import cn.hippo4j.core.executor.support.QueueTypeEnum;
|
||||||
|
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||||
|
import cn.hippo4j.springboot.starter.config.BootstrapProperties;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool subscribe config.
|
||||||
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DynamicThreadPoolSubscribeConfig {
|
||||||
|
|
||||||
|
private final ThreadPoolDynamicRefresh threadPoolDynamicRefresh;
|
||||||
|
|
||||||
|
private final ClientWorker clientWorker;
|
||||||
|
|
||||||
|
private final BootstrapProperties properties;
|
||||||
|
|
||||||
|
private final ExecutorService configRefreshExecutorService = ThreadPoolBuilder.builder()
|
||||||
|
.corePoolSize(1)
|
||||||
|
.maxPoolNum(2)
|
||||||
|
.keepAliveTime(2000)
|
||||||
|
.timeUnit(TimeUnit.MILLISECONDS)
|
||||||
|
.workQueue(QueueTypeEnum.SYNCHRONOUS_QUEUE)
|
||||||
|
.allowCoreThreadTimeOut(true)
|
||||||
|
.threadFactory("client.dynamic.threadPool.change.config")
|
||||||
|
.rejected(new ThreadPoolExecutor.AbortPolicy())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
public void subscribeConfig(String threadPoolId) {
|
||||||
|
subscribeConfig(threadPoolId, config -> threadPoolDynamicRefresh.dynamicRefresh(config));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void subscribeConfig(String threadPoolId, ThreadPoolSubscribeCallback threadPoolSubscribeCallback) {
|
||||||
|
Listener configListener = new Listener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveConfigInfo(String config) {
|
||||||
|
threadPoolSubscribeCallback.callback(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Executor getExecutor() {
|
||||||
|
return configRefreshExecutorService;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
clientWorker.addTenantListeners(properties.getNamespace(), properties.getItemId(), threadPoolId, Arrays.asList(configListener));
|
||||||
|
}
|
||||||
|
}
|
@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 cn.hippo4j.springboot.starter.core;
|
|
||||||
|
|
||||||
import cn.hippo4j.springboot.starter.event.ApplicationCompleteEvent;
|
|
||||||
import cn.hippo4j.springboot.starter.remote.HttpAgent;
|
|
||||||
import cn.hippo4j.springboot.starter.remote.ServerHealthCheck;
|
|
||||||
import org.springframework.context.ApplicationListener;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thread-pool config service.
|
|
||||||
*/
|
|
||||||
public class ThreadPoolConfigService implements ConfigService, ApplicationListener<ApplicationCompleteEvent> {
|
|
||||||
|
|
||||||
private final ClientWorker clientWorker;
|
|
||||||
|
|
||||||
private final ServerHealthCheck serverHealthCheck;
|
|
||||||
|
|
||||||
public ThreadPoolConfigService(HttpAgent httpAgent, String identify, ServerHealthCheck serverHealthCheck) {
|
|
||||||
this.serverHealthCheck = serverHealthCheck;
|
|
||||||
this.clientWorker = new ClientWorker(httpAgent, identify, serverHealthCheck);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addListener(String tenantId, String itemId, String threadPoolId, Listener listener) {
|
|
||||||
clientWorker.addTenantListeners(tenantId, itemId, threadPoolId, Arrays.asList(listener));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getServerStatus() {
|
|
||||||
if (serverHealthCheck.isHealthStatus()) {
|
|
||||||
return "UP";
|
|
||||||
} else {
|
|
||||||
return "DOWN";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onApplicationEvent(ApplicationCompleteEvent event) {
|
|
||||||
clientWorker.notifyApplicationComplete();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 cn.hippo4j.springboot.starter.core;
|
|
||||||
|
|
||||||
import cn.hippo4j.springboot.starter.config.BootstrapProperties;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thread-pool operation.
|
|
||||||
*/
|
|
||||||
public class ThreadPoolOperation {
|
|
||||||
|
|
||||||
private final ConfigService configService;
|
|
||||||
|
|
||||||
private final BootstrapProperties properties;
|
|
||||||
|
|
||||||
public ThreadPoolOperation(BootstrapProperties properties, ConfigService configService) {
|
|
||||||
this.properties = properties;
|
|
||||||
this.configService = configService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Listener subscribeConfig(String threadPoolId, Executor executor, ThreadPoolSubscribeCallback threadPoolSubscribeCallback) {
|
|
||||||
Listener configListener = new Listener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void receiveConfigInfo(String config) {
|
|
||||||
threadPoolSubscribeCallback.callback(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Executor getExecutor() {
|
|
||||||
return executor;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
configService.addListener(properties.getNamespace(), properties.getItemId(), threadPoolId, configListener);
|
|
||||||
return configListener;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue