TaskUtils.java
7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.viontech.fanxing.task.utils;
import cn.hutool.core.collection.ListUtil;
import com.google.common.collect.ArrayListMultimap;
import com.viontech.fanxing.commons.constant.TaskStatus;
import com.viontech.fanxing.commons.exception.FanXingException;
import com.viontech.fanxing.commons.model.Task;
import com.viontech.fanxing.commons.model.TaskExample;
import com.viontech.fanxing.commons.model.main.VaServerInfo;
import com.viontech.fanxing.task.model.TaskData;
import com.viontech.fanxing.task.model.runtime.DailyRuntimeConfig;
import com.viontech.fanxing.task.model.runtime.RandomRuntimeConfig;
import com.viontech.fanxing.task.model.runtime.RuntimeConfig;
import com.viontech.fanxing.task.service.TaskDataService;
import com.viontech.fanxing.task.service.VAServerService;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RMap;
import java.time.LocalTime;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* .
*
* @author 谢明辉
* @date 2021/12/21
*/
@SuppressWarnings("ALL")
@Slf4j
public enum TaskUtils {
INSTANCE;
/**
* 判断任务是否可以执行
* 没有场景存储配置,状态是停止或未部署的都不能运行
*/
public boolean canRun(Task task) {
return StringUtils.isNotBlank(task.getScene()) && task.getStoreConfigId() != null && !TaskStatus.AWAIT.valEqual(task.getStatus()) && !TaskStatus.STOP.valEqual(task.getStatus());
}
/**
* 判断任务是否需要被重新构建
*
* @param original 之前的任务信息
* @param present 现在的任务信息
*/
public boolean needRebuild(Task original, Task present) {
return (!original.getRuntimeConf().equals(present.getRuntimeConf()))
|| (!original.getStoreConfigId().equals(present.getStoreConfigId()))
|| (!original.getResourceNeed().equals(present.getResourceNeed()))
|| (!original.getVaType().equals(present.getVaType()))
|| (!original.getScene().equals(present.getScene()));
}
public void checkRuntimeConf(TaskData taskData, VAServerService vaServerService, TaskDataService taskDataService) {
Integer runtimeType = taskData.getTask().getRuntimeType();
TaskExample taskExample = new TaskExample();
taskExample.createCriteria().andStatusIn(ListUtil.of(TaskStatus.STARTING.val, TaskStatus.STAY.val, TaskStatus.RUNNING.val, TaskStatus.CAN_NOT_RUN.val));
List<Task> tasks = taskDataService.getTaskService().selectByExample(taskExample);
RMap<String, VaServerInfo> map = vaServerService.getVaServerRedisRepository().getVaServerInfoMap();
// 初始化资源检查对象
final TaskChecking taskChecking = new TaskChecking();
map.values().stream().forEach(x -> taskChecking.addServer(x));
tasks.stream().filter(x -> x.getId() != taskData.getTask().getId()).map(x -> new TaskData(x)).forEach(x -> taskChecking.addTask(x));
taskChecking.addTask(taskData);
}
private @Getter
@Setter
static class TaskChecking {
private ArrayListMultimap<String, ResourceNode> serverResourceMap = ArrayListMultimap.create();
public void addTask(TaskData taskData) {
String vaType = taskData.getTask().getVaType();
Integer runtimeType = taskData.getTask().getRuntimeType();
RuntimeConfig runtimeConfig = taskData.getRuntimeConfig();
Float resourceNeed = taskData.getTask().getResourceNeed();
Collection<ResourceNode> nodes;
if (vaType == null) {
nodes = serverResourceMap.values();
} else {
nodes = serverResourceMap.get(vaType);
}
boolean success = false;
switch (runtimeType) {
case 4:
if (reduce(nodes, resourceNeed, 0, ResourceNode.MAX_SECOND)) {
return;
}
break;
case 0:
DailyRuntimeConfig dConfig = (DailyRuntimeConfig) runtimeConfig;
List<RuntimeConfig.Config> multiConfig = dConfig.getMultiConfig();
for (RuntimeConfig.Config config : multiConfig) {
LocalTime start = config.getStart();
LocalTime end = config.getEnd();
if (reduce(nodes, resourceNeed, start.toSecondOfDay(), end.toSecondOfDay())) {
success = true;
} else {
break;
}
}
if (success) {
return;
} else {
break;
}
case 3:
RandomRuntimeConfig rConfig = (RandomRuntimeConfig) runtimeConfig;
Long runningTime = rConfig.getRunningTime();
long runSecond = (int) TimeUnit.MINUTES.toSeconds(runningTime);
int nowSecond = LocalTime.now().toSecondOfDay();
if (runSecond > 86400) {
success = reduce(nodes, resourceNeed, 0, ResourceNode.MAX_SECOND);
} else if ((nowSecond + runSecond) > ResourceNode.MAX_SECOND) {
success = reduce(nodes, resourceNeed, nowSecond, ResourceNode.MAX_SECOND);
if (success) {
reduce(nodes, resourceNeed, 0, (int) ((nowSecond + runSecond) - ResourceNode.MAX_SECOND));
}
} else {
success = reduce(nodes, resourceNeed, nowSecond, (int) (nowSecond + runSecond));
}
if (success) {
return;
}
break;
}
log.error("任务无法启动,");
throw new FanXingException("任务资源信息异常");
}
private boolean reduce(Collection<ResourceNode> resourceNodes, Float resourceNeed, int start, int end) {
for (ResourceNode node : resourceNodes) {
if (node.reduce(resourceNeed, start, end)) {
return true;
}
}
return false;
}
public void addServer(VaServerInfo vaServerInfo) {
ResourceNode resourceNode = new ResourceNode(vaServerInfo);
serverResourceMap.put(vaServerInfo.getPlatType(), resourceNode);
}
}
private @Getter
@Setter
@NoArgsConstructor
static class ResourceNode {
public static final Integer MAX_SECOND = 24 * 60 * 60 - 1;
/** min index 0; max index 86399 */
private Float[] resource;
private Float max;
private String devId;
public ResourceNode(VaServerInfo vaServerInfo) {
Float availableResources = vaServerInfo.getVideoResource();
this.devId = vaServerInfo.getDevID();
resource = new Float[86400];
for (int i = 0; i < resource.length; i++) {
resource[i] = availableResources;
}
this.max = availableResources;
}
/**
* @param start include
* @param end exclude
*/
public boolean reduce(Float value, int start, int end) {
for (int i = start; i < end; i++) {
if (!(resource[i] >= value)) {
return false;
}
}
for (int i = start; i < end; i++) {
resource[i] = resource[i] - value;
}
return true;
}
}
}