|
1 | 1 | package org.zstack.compute.cluster; |
2 | 2 |
|
| 3 | +import org.apache.logging.log4j.ThreadContext; |
3 | 4 | import org.springframework.beans.factory.annotation.Autowire; |
4 | 5 | import org.springframework.beans.factory.annotation.Autowired; |
5 | 6 | import org.springframework.beans.factory.annotation.Configurable; |
| 7 | +import org.zstack.core.asyncbatch.While; |
6 | 8 | import org.zstack.core.cascade.CascadeConstant; |
7 | 9 | import org.zstack.core.cascade.CascadeFacade; |
8 | 10 | import org.zstack.core.cloudbus.CloudBus; |
| 11 | +import org.zstack.core.cloudbus.CloudBusCallBack; |
9 | 12 | import org.zstack.core.db.DatabaseFacade; |
| 13 | +import org.zstack.core.db.Q; |
10 | 14 | import org.zstack.core.errorcode.ErrorFacade; |
11 | | -import org.zstack.header.core.NopeCompletion; |
12 | | -import org.zstack.header.core.workflow.*; |
13 | | -import org.zstack.header.errorcode.SysErrors; |
| 15 | +import org.zstack.core.progress.ProgressReportService; |
| 16 | +import org.zstack.core.thread.ChainTask; |
14 | 17 | import org.zstack.core.thread.SyncTask; |
| 18 | +import org.zstack.core.thread.SyncTaskChain; |
15 | 19 | import org.zstack.core.thread.ThreadFacade; |
16 | | -import org.zstack.core.workflow.*; |
| 20 | +import org.zstack.core.workflow.FlowChainBuilder; |
17 | 21 | import org.zstack.header.cluster.*; |
18 | 22 | import org.zstack.header.core.Completion; |
| 23 | +import org.zstack.header.core.NoErrorCompletion; |
| 24 | +import org.zstack.header.core.NopeCompletion; |
| 25 | +import org.zstack.header.core.workflow.*; |
19 | 26 | import org.zstack.header.errorcode.ErrorCode; |
| 27 | +import org.zstack.header.errorcode.SysErrors; |
| 28 | +import org.zstack.header.host.HostConstant; |
| 29 | +import org.zstack.header.host.HostVO; |
| 30 | +import org.zstack.header.host.HostVO_; |
| 31 | +import org.zstack.header.host.UpdateHostOSMsg; |
20 | 32 | import org.zstack.header.message.APIDeleteMessage; |
21 | 33 | import org.zstack.header.message.APIMessage; |
22 | 34 | import org.zstack.header.message.Message; |
| 35 | +import org.zstack.header.message.MessageReply; |
23 | 36 | import org.zstack.utils.Utils; |
24 | 37 | import org.zstack.utils.logging.CLogger; |
25 | 38 |
|
26 | 39 | import java.util.Arrays; |
27 | 40 | import java.util.List; |
28 | 41 | import java.util.Map; |
| 42 | +import java.util.concurrent.ConcurrentHashMap; |
| 43 | + |
| 44 | +import static org.zstack.header.Constants.THREAD_CONTEXT_API; |
| 45 | +import static org.zstack.header.Constants.THREAD_CONTEXT_TASK_NAME; |
29 | 46 |
|
30 | 47 | @Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) |
31 | 48 | public class ClusterBase extends AbstractCluster { |
@@ -71,11 +88,33 @@ protected void handleApiMessage(APIMessage msg) { |
71 | 88 | handle((APIDeleteClusterMsg) msg); |
72 | 89 | } else if (msg instanceof APIUpdateClusterMsg) { |
73 | 90 | handle((APIUpdateClusterMsg) msg); |
| 91 | + } else if (msg instanceof APIUpdateClusterOSMsg) { |
| 92 | + handle((APIUpdateClusterOSMsg) msg); |
74 | 93 | } else { |
75 | 94 | bus.dealWithUnknownMessage(msg); |
76 | 95 | } |
77 | 96 | } |
78 | 97 |
|
| 98 | + private void handle(APIUpdateClusterOSMsg msg) { |
| 99 | + APIUpdateClusterOSEvent evt = new APIUpdateClusterOSEvent(msg.getId()); |
| 100 | + |
| 101 | + UpdateClusterOSMsg umsg = new UpdateClusterOSMsg(); |
| 102 | + umsg.setUuid(msg.getUuid()); |
| 103 | + bus.makeTargetServiceIdByResourceUuid(umsg, ClusterConstant.SERVICE_ID, msg.getUuid()); |
| 104 | + bus.send(umsg, new CloudBusCallBack(msg) { |
| 105 | + @Override |
| 106 | + public void run(MessageReply reply) { |
| 107 | + if (reply.isSuccess()) { |
| 108 | + UpdateClusterOSReply rly = reply.castReply(); |
| 109 | + evt.setResults(rly.getResults()); |
| 110 | + } else { |
| 111 | + evt.setError(reply.getError()); |
| 112 | + } |
| 113 | + bus.publish(evt); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
79 | 118 | private void handle(APIUpdateClusterMsg msg) { |
80 | 119 | boolean update = false; |
81 | 120 | if (msg.getName() != null) { |
@@ -219,12 +258,84 @@ protected void handleLocalMessage(Message msg) { |
219 | 258 | handle((ChangeClusterStateMsg) msg); |
220 | 259 | } else if (msg instanceof ClusterDeletionMsg) { |
221 | 260 | handle((ClusterDeletionMsg) msg); |
| 261 | + } else if (msg instanceof UpdateClusterOSMsg) { |
| 262 | + handle((UpdateClusterOSMsg) msg); |
222 | 263 | } else { |
223 | 264 | bus.dealWithUnknownMessage(msg); |
224 | 265 | } |
225 | 266 | } |
226 | 267 |
|
227 | | - private void handle(ClusterDeletionMsg msg) { |
| 268 | + private void handle(UpdateClusterOSMsg msg) { |
| 269 | + UpdateClusterOSReply reply = new UpdateClusterOSReply(); |
| 270 | + reply.setResults(new ConcurrentHashMap<>()); |
| 271 | + |
| 272 | + ErrorCode error = extpEmitter.preUpdateOS(self); |
| 273 | + if (error != null) { |
| 274 | + reply.setError(error); |
| 275 | + bus.reply(msg, reply); |
| 276 | + return; |
| 277 | + } |
| 278 | + |
| 279 | + thdf.chainSubmit(new ChainTask(msg) { |
| 280 | + @Override |
| 281 | + public String getSyncSignature() { |
| 282 | + return "update-cluster-os"; |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public int getSyncLevel() { |
| 287 | + return ClusterGlobalConfig.CLUSTER_UPDATE_OS_PARALLELISM_DEGREE.value(Integer.class); |
| 288 | + } |
| 289 | + |
| 290 | + @Override |
| 291 | + public void run(SyncTaskChain chain) { |
| 292 | + String apiId = ThreadContext.get(THREAD_CONTEXT_API); |
| 293 | + String taskName = ThreadContext.get(THREAD_CONTEXT_TASK_NAME); |
| 294 | + extpEmitter.beforeUpdateOS(self); |
| 295 | + |
| 296 | + // update each hosts os in the cluster |
| 297 | + List<String> hostUuids = Q.New(HostVO.class) |
| 298 | + .select(HostVO_.uuid) |
| 299 | + .eq(HostVO_.clusterUuid, msg.getUuid()) |
| 300 | + .listValues(); |
| 301 | + new While<>(hostUuids).all((hostUuid, completion) -> { |
| 302 | + UpdateHostOSMsg umsg = new UpdateHostOSMsg(); |
| 303 | + umsg.setUuid(hostUuid); |
| 304 | + umsg.setClusterUuid(msg.getUuid()); |
| 305 | + bus.makeTargetServiceIdByResourceUuid(umsg, HostConstant.SERVICE_ID, hostUuid); |
| 306 | + bus.send(umsg, new CloudBusCallBack(completion) { |
| 307 | + @Override |
| 308 | + public void run(MessageReply rly) { |
| 309 | + if (rly.isSuccess()) { |
| 310 | + reply.getResults().put(hostUuid, "success"); |
| 311 | + } else { |
| 312 | + reply.getResults().put(hostUuid, rly.getError().getDetails()); |
| 313 | + } |
| 314 | + // progress info |
| 315 | + ThreadContext.put(THREAD_CONTEXT_API, apiId); |
| 316 | + ThreadContext.put(THREAD_CONTEXT_TASK_NAME, taskName); |
| 317 | + ProgressReportService.reportProgress(String.valueOf(100 * reply.getResults().size() / hostUuids.size())); |
| 318 | + completion.done(); |
| 319 | + } |
| 320 | + }); |
| 321 | + }).run(new NoErrorCompletion() { |
| 322 | + @Override |
| 323 | + public void done() { |
| 324 | + extpEmitter.afterUpdateOS(self); |
| 325 | + bus.reply(msg, reply); |
| 326 | + chain.next(); |
| 327 | + } |
| 328 | + }); |
| 329 | + } |
| 330 | + |
| 331 | + @Override |
| 332 | + public String getName() { |
| 333 | + return getSyncSignature(); |
| 334 | + } |
| 335 | + }); |
| 336 | + } |
| 337 | + |
| 338 | + private void handle(ClusterDeletionMsg msg) { |
228 | 339 | ClusterInventory inv = ClusterInventory.valueOf(self); |
229 | 340 | extpEmitter.beforeDelete(inv); |
230 | 341 | deleteHook(); |
|
0 commit comments