-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from FederatedAI/dev-2.1.7
Dev 2.1.7
Showing
62 changed files
with
1,108 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,7 @@ public void stop() { | |
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
tryNum++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...admin/src/main/java/com/webank/ai/fate/serving/admin/controller/DynamicLogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.webank.ai.fate.serving.admin.controller; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
@RequestMapping("/admin") | ||
@RestController | ||
public class DynamicLogController { | ||
private static final Logger logger = LoggerFactory.getLogger(DynamicLogController.class); | ||
|
||
@GetMapping("/alterSysLogLevel/{level}") | ||
public String alterSysLogLevel(@PathVariable String level){ | ||
try { | ||
LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
context.getLogger("ROOT").setLevel(Level.valueOf(level)); | ||
return "ok"; | ||
} catch (Exception ex) { | ||
logger.error("admin alterSysLogLevel failed : " + ex); | ||
return "failed"; | ||
} | ||
|
||
} | ||
|
||
@GetMapping("/alterPkgLogLevel") | ||
public String alterPkgLogLevel(@RequestParam String level, @RequestParam String pkgName){ | ||
try { | ||
LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
context.getLogger(pkgName).setLevel(Level.valueOf(level)); | ||
return "ok"; | ||
} catch (Exception ex) { | ||
logger.error("admin alterPkgLogLevel failed : " + ex); | ||
return "failed"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...serving-admin/src/main/java/com/webank/ai/fate/serving/admin/utils/NetAddressChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.webank.ai.fate.serving.admin.utils; | ||
|
||
import com.webank.ai.fate.serving.admin.services.ComponentService; | ||
import com.webank.ai.fate.serving.core.exceptions.RemoteRpcException; | ||
import com.webank.ai.fate.serving.core.exceptions.SysException; | ||
import com.webank.ai.fate.serving.core.utils.NetUtils; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
public class NetAddressChecker { | ||
|
||
private static final ComponentService componentService = new ComponentService(); | ||
|
||
public static void check(String host, Integer port) { | ||
if (!NetUtils.isValidAddress(host + ":" + port)) { | ||
throw new SysException("invalid address"); | ||
} | ||
|
||
if (!componentService.isAllowAccess(host, port)) { | ||
throw new RemoteRpcException("no allow access, target: " + host + ":" + port); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
fate-serving-common/src/main/java/com/webank/ai/fate/serving/common/bean/ThreadVO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package com.webank.ai.fate.serving.common.bean; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
public class ThreadVO implements Serializable { | ||
private static final long serialVersionUID = 0L; | ||
|
||
private long id; | ||
private String name; | ||
private String group; | ||
private int priority; | ||
private Thread.State state; | ||
private double cpu; | ||
private long deltaTime; | ||
private long time; | ||
private boolean interrupted; | ||
private boolean daemon; | ||
|
||
public ThreadVO() { | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getGroup() { | ||
return group; | ||
} | ||
|
||
public void setGroup(String group) { | ||
this.group = group; | ||
} | ||
|
||
public int getPriority() { | ||
return priority; | ||
} | ||
|
||
public void setPriority(int priority) { | ||
this.priority = priority; | ||
} | ||
|
||
public Thread.State getState() { | ||
return state; | ||
} | ||
|
||
public void setState(Thread.State state) { | ||
this.state = state; | ||
} | ||
|
||
public double getCpu() { | ||
return cpu; | ||
} | ||
|
||
public void setCpu(double cpu) { | ||
this.cpu = cpu; | ||
} | ||
|
||
public long getDeltaTime() { | ||
return deltaTime; | ||
} | ||
|
||
public void setDeltaTime(long deltaTime) { | ||
this.deltaTime = deltaTime; | ||
} | ||
|
||
public long getTime() { | ||
return time; | ||
} | ||
|
||
public void setTime(long time) { | ||
this.time = time; | ||
} | ||
|
||
public boolean isInterrupted() { | ||
return interrupted; | ||
} | ||
|
||
public void setInterrupted(boolean interrupted) { | ||
this.interrupted = interrupted; | ||
} | ||
|
||
public boolean isDaemon() { | ||
return daemon; | ||
} | ||
|
||
public void setDaemon(boolean daemon) { | ||
this.daemon = daemon; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
ThreadVO threadVO = (ThreadVO) o; | ||
|
||
if (id != threadVO.id) { | ||
return false; | ||
} | ||
return Objects.equals(name, threadVO.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = (int) (id ^ (id >>> 32)); | ||
result = 31 * result + (name != null ? name.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ThreadVO{" + | ||
"id=" + id + | ||
", name='" + name + '\'' + | ||
", group='" + group + '\'' + | ||
", priority=" + priority + | ||
", state=" + state + | ||
", cpu=" + cpu + | ||
", deltaTime=" + deltaTime + | ||
", time=" + time + | ||
", interrupted=" + interrupted + | ||
", daemon=" + daemon + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
...ving-common/src/main/java/com/webank/ai/fate/serving/common/health/HealthCheckStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
package com.webank.ai.fate.serving.common.health; | ||
|
||
public enum HealthCheckStatus { | ||
ok, | ||
warn, | ||
error | ||
/** | ||
* 健康 | ||
*/ | ||
ok("状态健康"), | ||
|
||
/** | ||
* 异常 | ||
*/ | ||
warn("状态异常"), | ||
|
||
/** | ||
* 错误 | ||
*/ | ||
error("状态错误"); | ||
|
||
private final String desc; | ||
|
||
HealthCheckStatus(String desc) { | ||
this.desc = desc; | ||
} | ||
|
||
public String getDesc() { | ||
return desc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
fate-serving-common/src/main/java/com/webank/ai/fate/serving/common/utils/JVMCPUUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.webank.ai.fate.serving.common.utils; | ||
|
||
import com.webank.ai.fate.serving.common.bean.ThreadVO; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
public class JVMCPUUtils { | ||
|
||
private static Set<String> states = null; | ||
|
||
static { | ||
states = new HashSet<>(Thread.State.values().length); | ||
for (Thread.State state : Thread.State.values()) { | ||
states.add(state.name()); | ||
} | ||
} | ||
|
||
public static List<ThreadVO> getThreadsState() { | ||
|
||
List<ThreadVO> threads = ThreadUtils.getThreads(); | ||
|
||
Collection<ThreadVO> resultThreads = new ArrayList<>(); | ||
for (ThreadVO thread : threads) { | ||
if (thread.getState() != null && states.contains(thread.getState().name())) { | ||
resultThreads.add(thread); | ||
} | ||
} | ||
|
||
|
||
ThreadSampler threadSampler = new ThreadSampler(); | ||
threadSampler.setIncludeInternalThreads(true); | ||
threadSampler.sample(resultThreads); | ||
threadSampler.pause(1000); | ||
return threadSampler.sample(resultThreads); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
fate-serving-common/src/main/java/com/webank/ai/fate/serving/common/utils/ThreadSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package com.webank.ai.fate.serving.common.utils; | ||
|
||
|
||
import com.webank.ai.fate.serving.common.bean.ThreadVO; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import sun.management.HotspotThreadMBean; | ||
import sun.management.ManagementFactoryHelper; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.ThreadMXBean; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
|
||
class ThreadSampler { | ||
private static Logger logger = LoggerFactory.getLogger(ThreadSampler.class); | ||
private static ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); | ||
private static HotspotThreadMBean hotspotThreadMBean; | ||
private static boolean hotspotThreadMBeanEnable = true; | ||
|
||
private Map<ThreadVO, Long> lastCpuTimes = new HashMap<ThreadVO, Long>(); | ||
|
||
private long lastSampleTimeNanos; | ||
private boolean includeInternalThreads = true; | ||
|
||
|
||
public List<ThreadVO> sample(Collection<ThreadVO> originThreads) { | ||
|
||
List<ThreadVO> threads = new ArrayList<ThreadVO>(originThreads); | ||
|
||
// Sample CPU | ||
if (lastCpuTimes.isEmpty()) { | ||
lastSampleTimeNanos = System.nanoTime(); | ||
for (ThreadVO thread : threads) { | ||
if (thread.getId() > 0) { | ||
long cpu = threadMXBean.getThreadCpuTime(thread.getId()); | ||
lastCpuTimes.put(thread, cpu); | ||
thread.setTime(cpu / 1000000); | ||
} | ||
} | ||
|
||
// add internal threads | ||
Map<String, Long> internalThreadCpuTimes = getInternalThreadCpuTimes(); | ||
if (internalThreadCpuTimes != null) { | ||
for (Map.Entry<String, Long> entry : internalThreadCpuTimes.entrySet()) { | ||
String key = entry.getKey(); | ||
ThreadVO thread = createThreadVO(key); | ||
thread.setTime(entry.getValue() / 1000000); | ||
threads.add(thread); | ||
lastCpuTimes.put(thread, entry.getValue()); | ||
} | ||
} | ||
|
||
//sort by time | ||
Collections.sort(threads, new Comparator<ThreadVO>() { | ||
@Override | ||
public int compare(ThreadVO o1, ThreadVO o2) { | ||
long l1 = o1.getTime(); | ||
long l2 = o2.getTime(); | ||
if (l1 < l2) { | ||
return 1; | ||
} else if (l1 > l2) { | ||
return -1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
}); | ||
return threads; | ||
} | ||
|
||
// Resample | ||
long newSampleTimeNanos = System.nanoTime(); | ||
Map<ThreadVO, Long> newCpuTimes = new HashMap<ThreadVO, Long>(threads.size()); | ||
for (ThreadVO thread : threads) { | ||
if (thread.getId() > 0) { | ||
long cpu = threadMXBean.getThreadCpuTime(thread.getId()); | ||
newCpuTimes.put(thread, cpu); | ||
} | ||
} | ||
// internal threads | ||
Map<String, Long> newInternalThreadCpuTimes = getInternalThreadCpuTimes(); | ||
if (newInternalThreadCpuTimes != null) { | ||
for (Map.Entry<String, Long> entry : newInternalThreadCpuTimes.entrySet()) { | ||
ThreadVO threadVO = createThreadVO(entry.getKey()); | ||
threads.add(threadVO); | ||
newCpuTimes.put(threadVO, entry.getValue()); | ||
} | ||
} | ||
|
||
// Compute delta time | ||
final Map<ThreadVO, Long> deltas = new HashMap<ThreadVO, Long>(threads.size()); | ||
for (ThreadVO thread : newCpuTimes.keySet()) { | ||
Long t = lastCpuTimes.get(thread); | ||
if (t == null) { | ||
t = 0L; | ||
} | ||
long time1 = t; | ||
long time2 = newCpuTimes.get(thread); | ||
if (time1 == -1) { | ||
time1 = time2; | ||
} else if (time2 == -1) { | ||
time2 = time1; | ||
} | ||
long delta = time2 - time1; | ||
deltas.put(thread, delta); | ||
} | ||
|
||
long sampleIntervalNanos = newSampleTimeNanos - lastSampleTimeNanos; | ||
|
||
// Compute cpu usage | ||
final HashMap<ThreadVO, Double> cpuUsages = new HashMap<ThreadVO, Double>(threads.size()); | ||
for (ThreadVO thread : threads) { | ||
double cpu = sampleIntervalNanos == 0 ? 0 : (Math.rint(deltas.get(thread) * 10000.0 / sampleIntervalNanos) / 100.0); | ||
cpuUsages.put(thread, cpu); | ||
} | ||
|
||
// Sort by CPU time : should be a rendering hint... | ||
Collections.sort(threads, new Comparator<ThreadVO>() { | ||
@Override | ||
public int compare(ThreadVO o1, ThreadVO o2) { | ||
long l1 = deltas.get(o1); | ||
long l2 = deltas.get(o2); | ||
if (l1 < l2) { | ||
return 1; | ||
} else if (l1 > l2) { | ||
return -1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
}); | ||
|
||
for (ThreadVO thread : threads) { | ||
//nanos to mills | ||
long timeMills = newCpuTimes.get(thread) / 1000000; | ||
long deltaTime = deltas.get(thread) / 1000000; | ||
double cpu = cpuUsages.get(thread); | ||
|
||
thread.setCpu(cpu); | ||
thread.setTime(timeMills); | ||
thread.setDeltaTime(deltaTime); | ||
} | ||
lastCpuTimes = newCpuTimes; | ||
lastSampleTimeNanos = newSampleTimeNanos; | ||
|
||
return threads; | ||
} | ||
|
||
private Map<String, Long> getInternalThreadCpuTimes() { | ||
if (hotspotThreadMBeanEnable && includeInternalThreads) { | ||
try { | ||
if (hotspotThreadMBean == null) { | ||
hotspotThreadMBean = ManagementFactoryHelper.getHotspotThreadMBean(); | ||
} | ||
return hotspotThreadMBean.getInternalThreadCpuTimes(); | ||
} catch (Exception ex) { | ||
logger.error("getInternalThreadCpuTimes failed Cause : " + ex); | ||
hotspotThreadMBeanEnable = false; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private ThreadVO createThreadVO(String name) { | ||
ThreadVO threadVO = new ThreadVO(); | ||
threadVO.setId(-1); | ||
threadVO.setName(name); | ||
threadVO.setPriority(-1); | ||
threadVO.setDaemon(true); | ||
threadVO.setInterrupted(false); | ||
return threadVO; | ||
} | ||
|
||
public void pause(long mills) { | ||
try { | ||
Thread.sleep(mills); | ||
} catch (InterruptedException e) { | ||
logger.error("pause failed Cause : " + e); | ||
} | ||
} | ||
|
||
public boolean isIncludeInternalThreads() { | ||
return includeInternalThreads; | ||
} | ||
|
||
public void setIncludeInternalThreads(boolean includeInternalThreads) { | ||
this.includeInternalThreads = includeInternalThreads; | ||
} | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
fate-serving-common/src/main/java/com/webank/ai/fate/serving/common/utils/ThreadUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.webank.ai.fate.serving.common.utils; | ||
|
||
import com.webank.ai.fate.serving.common.bean.ThreadVO; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
public class ThreadUtils { | ||
|
||
private static ThreadGroup getRoot() { | ||
ThreadGroup group = Thread.currentThread().getThreadGroup(); | ||
ThreadGroup parent; | ||
while ((parent = group.getParent()) != null) { | ||
group = parent; | ||
} | ||
return group; | ||
} | ||
|
||
public static List<ThreadVO> getThreads() { | ||
ThreadGroup root = getRoot(); | ||
Thread[] threads = new Thread[root.activeCount()]; | ||
while (root.enumerate(threads, true) == threads.length) { | ||
threads = new Thread[threads.length * 2]; | ||
} | ||
List<ThreadVO> list = new ArrayList<ThreadVO>(threads.length); | ||
for (Thread thread : threads) { | ||
if (thread != null) { | ||
ThreadVO threadVO = createThreadVO(thread); | ||
list.add(threadVO); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
private static ThreadVO createThreadVO(Thread thread) { | ||
ThreadGroup group = thread.getThreadGroup(); | ||
ThreadVO threadVO = new ThreadVO(); | ||
threadVO.setId(thread.getId()); | ||
threadVO.setName(thread.getName()); | ||
threadVO.setGroup(group == null ? "" : group.getName()); | ||
threadVO.setPriority(thread.getPriority()); | ||
threadVO.setState(thread.getState()); | ||
threadVO.setInterrupted(thread.isInterrupted()); | ||
threadVO.setDaemon(thread.isDaemon()); | ||
return threadVO; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
...-core/src/main/java/com/webank/ai/fate/serving/core/bean/HttpAdapterResponseCodeEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package com.webank.ai.fate.serving.core.bean; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
public class HttpAdapterResponseCodeEnum{ | ||
|
||
// 通用http响应成功码 | ||
public static final int COMMON_HTTP_SUCCESS_CODE = 0; | ||
|
||
//正常 | ||
public final static int SUCCESS_CODE = 200; | ||
public static final int SUCCESS_CODE = 200; | ||
|
||
//查询无果 | ||
public final static int ERROR_CODE = 404; | ||
public static final int ERROR_CODE = 404; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...proxy/src/main/java/com/webank/ai/fate/serving/proxy/controller/DynamicLogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.webank.ai.fate.serving.proxy.controller; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
@RequestMapping("/proxy") | ||
@RestController | ||
public class DynamicLogController { | ||
private static final Logger logger = LoggerFactory.getLogger(DynamicLogController.class); | ||
|
||
@GetMapping("/alterSysLogLevel/{level}") | ||
public String alterSysLogLevel(@PathVariable String level){ | ||
try { | ||
LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
context.getLogger("ROOT").setLevel(Level.valueOf(level)); | ||
return "ok"; | ||
} catch (Exception ex) { | ||
logger.error("proxy alterSysLogLevel failed : " + ex); | ||
return "failed"; | ||
} | ||
|
||
} | ||
|
||
@GetMapping("/alterPkgLogLevel") | ||
public String alterPkgLogLevel(@RequestParam String level, @RequestParam String pkgName){ | ||
try { | ||
LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
context.getLogger(pkgName).setLevel(Level.valueOf(level)); | ||
return "ok"; | ||
} catch (Exception ex) { | ||
logger.error("proxy alterPkgLogLevel failed : " + ex); | ||
return "failed"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ving-server/src/main/java/com/webank/ai/fate/serving/controller/DynamicLogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.webank.ai.fate.serving.controller; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
@RequestMapping("/server") | ||
@RestController | ||
public class DynamicLogController { | ||
private static final Logger logger = LoggerFactory.getLogger(DynamicLogController.class); | ||
|
||
@GetMapping("/alterSysLogLevel/{level}") | ||
public String alterSysLogLevel(@PathVariable String level){ | ||
try { | ||
LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
context.getLogger("ROOT").setLevel(Level.valueOf(level)); | ||
return "ok"; | ||
} catch (Exception ex) { | ||
logger.error("server alterSysLogLevel failed : " + ex); | ||
return "failed"; | ||
} | ||
|
||
} | ||
|
||
@GetMapping("/alterPkgLogLevel") | ||
public String alterPkgLogLevel(@RequestParam String level, @RequestParam String pkgName){ | ||
try { | ||
LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
context.getLogger(pkgName).setLevel(Level.valueOf(level)); | ||
return "ok"; | ||
} catch (Exception ex) { | ||
logger.error("server alterPkgLogLevel failed : " + ex); | ||
return "failed"; | ||
} | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...ing-server/src/main/java/com/webank/ai/fate/serving/controller/ServerModelController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.webank.ai.fate.serving.controller; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.webank.ai.fate.api.mlmodel.manager.ModelServiceGrpc; | ||
import com.webank.ai.fate.api.mlmodel.manager.ModelServiceProto; | ||
import com.webank.ai.fate.serving.core.bean.GrpcConnectionPool; | ||
import com.webank.ai.fate.serving.core.bean.MetaInfo; | ||
import com.webank.ai.fate.serving.core.bean.RequestParamWrapper; | ||
import com.webank.ai.fate.serving.core.bean.ReturnResult; | ||
import com.webank.ai.fate.serving.core.exceptions.SysException; | ||
import com.webank.ai.fate.serving.core.utils.NetUtils; | ||
import io.grpc.ManagedChannel; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
@RestController | ||
public class ServerModelController { | ||
|
||
Logger logger = LoggerFactory.getLogger(ServerModelController.class); | ||
|
||
GrpcConnectionPool grpcConnectionPool = GrpcConnectionPool.getPool(); | ||
|
||
@RequestMapping(value = "/server/model/unbind", method = RequestMethod.POST) | ||
@ResponseBody | ||
public Callable<ReturnResult> unbind(@RequestBody RequestParamWrapper requestParams) throws Exception { | ||
return () -> { | ||
String host = requestParams.getHost(); | ||
Integer port = requestParams.getPort(); | ||
String tableName = requestParams.getTableName(); | ||
String namespace = requestParams.getNamespace(); | ||
List<String> serviceIds = requestParams.getServiceIds(); | ||
|
||
Preconditions.checkArgument(StringUtils.isNotBlank(tableName), "parameter tableName is blank"); | ||
Preconditions.checkArgument(StringUtils.isNotBlank(namespace), "parameter namespace is blank"); | ||
Preconditions.checkArgument(serviceIds != null && serviceIds.size() != 0, "parameter serviceId is blank"); | ||
|
||
ReturnResult result = new ReturnResult(); | ||
|
||
logger.debug("unbind model by tableName and namespace, host: {}, port: {}, tableName: {}, namespace: {}", host, port, tableName, namespace); | ||
|
||
ModelServiceGrpc.ModelServiceFutureStub futureStub = getModelServiceFutureStub(host, port); | ||
|
||
ModelServiceProto.UnbindRequest unbindRequest = ModelServiceProto.UnbindRequest.newBuilder() | ||
.setTableName(tableName) | ||
.setNamespace(namespace) | ||
.addAllServiceIds(serviceIds) | ||
.build(); | ||
|
||
ListenableFuture<ModelServiceProto.UnbindResponse> future = futureStub.unbind(unbindRequest); | ||
|
||
ModelServiceProto.UnbindResponse response = future.get(MetaInfo.PROPERTY_GRPC_TIMEOUT, TimeUnit.MILLISECONDS); | ||
|
||
logger.debug("response: {}", response); | ||
|
||
result.setRetcode(response.getStatusCode()); | ||
result.setRetmsg(response.getMessage()); | ||
return result; | ||
}; | ||
} | ||
|
||
@RequestMapping(value = "/server/model/transfer", method = RequestMethod.POST) | ||
@ResponseBody | ||
public Callable<ReturnResult> transfer(@RequestBody RequestParamWrapper requestParams) { | ||
return () -> { | ||
String host = requestParams.getHost(); | ||
Integer port = requestParams.getPort(); | ||
String tableName = requestParams.getTableName(); | ||
String namespace = requestParams.getNamespace(); | ||
|
||
String targetHost = requestParams.getTargetHost(); | ||
Integer targetPort = requestParams.getTargetPort(); | ||
|
||
Preconditions.checkArgument(StringUtils.isNotBlank(tableName), "parameter tableName is blank"); | ||
Preconditions.checkArgument(StringUtils.isNotBlank(namespace), "parameter namespace is blank"); | ||
|
||
ReturnResult result = new ReturnResult(); | ||
|
||
logger.debug("transfer model by tableName and namespace, host: {}, port: {}, tableName: {}, namespace: {}, targetHost: {}, targetPort: {}" | ||
, host, port, tableName, namespace, targetHost, targetPort); | ||
|
||
ModelServiceGrpc.ModelServiceFutureStub futureStub = getModelServiceFutureStub(targetHost, targetPort); | ||
ModelServiceProto.FetchModelRequest fetchModelRequest = ModelServiceProto.FetchModelRequest.newBuilder() | ||
.setNamespace(namespace).setTableName(tableName).setSourceIp(host).setSourcePort(port).build(); | ||
|
||
ListenableFuture<ModelServiceProto.FetchModelResponse> future = futureStub.fetchModel(fetchModelRequest); | ||
ModelServiceProto.FetchModelResponse response = future.get(MetaInfo.PROPERTY_GRPC_TIMEOUT, TimeUnit.MILLISECONDS); | ||
|
||
logger.debug("response: {}", response); | ||
|
||
result.setRetcode(response.getStatusCode()); | ||
result.setRetmsg(response.getMessage()); | ||
return result; | ||
}; | ||
} | ||
|
||
private ModelServiceGrpc.ModelServiceFutureStub getModelServiceFutureStub(String host, Integer port) { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(host), "parameter host is blank"); | ||
Preconditions.checkArgument(port != null && port != 0, "parameter port was wrong"); | ||
|
||
if (!NetUtils.isValidAddress(host + ":" + port)) { | ||
throw new SysException("invalid address"); | ||
} | ||
|
||
ManagedChannel managedChannel = grpcConnectionPool.getManagedChannel(host, port); | ||
return ModelServiceGrpc.newFutureStub(managedChannel); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...g-server/src/main/java/com/webank/ai/fate/serving/controller/ServerServiceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.webank.ai.fate.serving.controller; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.webank.ai.fate.api.networking.common.CommonServiceGrpc; | ||
import com.webank.ai.fate.api.networking.common.CommonServiceProto; | ||
import com.webank.ai.fate.serving.core.bean.GrpcConnectionPool; | ||
import com.webank.ai.fate.serving.core.bean.MetaInfo; | ||
import com.webank.ai.fate.serving.core.bean.RequestParamWrapper; | ||
import com.webank.ai.fate.serving.core.bean.ReturnResult; | ||
import com.webank.ai.fate.serving.core.exceptions.SysException; | ||
import com.webank.ai.fate.serving.core.utils.NetUtils; | ||
import io.grpc.ManagedChannel; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author hcy | ||
*/ | ||
@RestController | ||
public class ServerServiceController { | ||
|
||
Logger logger = LoggerFactory.getLogger(ServerServiceController.class); | ||
|
||
GrpcConnectionPool grpcConnectionPool = GrpcConnectionPool.getPool(); | ||
|
||
@RequestMapping(value = "/server/service/weight/update", method = RequestMethod.POST) | ||
@ResponseBody | ||
public ReturnResult updateService(@RequestBody RequestParamWrapper requestParams) throws Exception { | ||
String host = requestParams.getHost(); | ||
int port = requestParams.getPort(); | ||
String url = requestParams.getUrl(); | ||
String routerMode = requestParams.getRouterMode(); | ||
Integer weight = requestParams.getWeight(); | ||
Long version = requestParams.getVersion(); | ||
|
||
if (logger.isDebugEnabled()) { | ||
logger.debug("try to update service"); | ||
} | ||
|
||
Preconditions.checkArgument(StringUtils.isNotBlank(url), "parameter url is blank"); | ||
|
||
logger.info("update url: {}, routerMode: {}, weight: {}, version: {}", url, routerMode, weight, version); | ||
|
||
CommonServiceGrpc.CommonServiceFutureStub commonServiceFutureStub = getCommonServiceFutureStub(host, port); | ||
CommonServiceProto.UpdateServiceRequest.Builder builder = CommonServiceProto.UpdateServiceRequest.newBuilder(); | ||
|
||
builder.setUrl(url); | ||
if (StringUtils.isNotBlank(routerMode)) { | ||
builder.setRouterMode(routerMode); | ||
} | ||
|
||
if (weight != null) { | ||
builder.setWeight(weight); | ||
} else { | ||
builder.setWeight(-1); | ||
} | ||
|
||
if (version != null) { | ||
builder.setVersion(version); | ||
} else { | ||
builder.setVersion(-1); | ||
} | ||
|
||
ListenableFuture<CommonServiceProto.CommonResponse> future = commonServiceFutureStub.updateService(builder.build()); | ||
|
||
CommonServiceProto.CommonResponse response = future.get(MetaInfo.PROPERTY_GRPC_TIMEOUT, TimeUnit.MILLISECONDS); | ||
|
||
ReturnResult result = new ReturnResult(); | ||
result.setRetcode(response.getStatusCode()); | ||
result.setRetmsg(response.getMessage()); | ||
return result; | ||
} | ||
|
||
private CommonServiceGrpc.CommonServiceFutureStub getCommonServiceFutureStub(String host, Integer port) { | ||
Preconditions.checkArgument(StringUtils.isNotBlank(host), "parameter host is blank"); | ||
Preconditions.checkArgument(port != null && port != 0, "parameter port was wrong"); | ||
|
||
if (!NetUtils.isValidAddress(host + ":" + port)) { | ||
throw new SysException("invalid address"); | ||
} | ||
|
||
ManagedChannel managedChannel = grpcConnectionPool.getManagedChannel(host, port); | ||
return CommonServiceGrpc.newFutureStub(managedChannel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters