7676import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
7777import org .apache .cloudstack .management .ManagementServerHost ;
7878import org .apache .cloudstack .outofbandmanagement .dao .OutOfBandManagementDao ;
79+ import org .apache .cloudstack .threadcontext .ThreadContextCommandUtil ;
7980import org .apache .cloudstack .utils .identity .ManagementServerNode ;
8081import org .apache .commons .collections .MapUtils ;
8182import org .apache .commons .lang3 .BooleanUtils ;
8283import org .apache .commons .lang3 .ObjectUtils ;
83- import org .apache .logging .log4j .ThreadContext ;
8484
8585import com .cloud .agent .AgentManager ;
8686import com .cloud .agent .Listener ;
@@ -572,21 +572,6 @@ private void initAndScheduleAgentConnectionsMonitor() {
572572 newAgentConnectionsMonitor .scheduleAtFixedRate (new AgentNewConnectionsMonitorTask (), cleanupTimeInSecs , cleanupTimeInSecs , TimeUnit .SECONDS );
573573 }
574574
575- private AgentControlAnswer handleControlCommand (final AgentAttache attache , final AgentControlCommand cmd ) {
576- AgentControlAnswer answer ;
577-
578- for (final Pair <Integer , Listener > listener : _cmdMonitors ) {
579- answer = listener .second ().processControlCommand (attache .getId (), cmd );
580-
581- if (answer != null ) {
582- return answer ;
583- }
584- }
585-
586- logger .warn ("No handling of agent control command: {} sent from {}" , cmd , attache );
587- return new AgentControlAnswer (cmd );
588- }
589-
590575 private AgentConnectStatusAnswer handleAgentConnectStatusCommand (AgentAttache attache , AgentConnectStatusCommand cmd ) {
591576 HostVO hostVo = _hostDao .findById (attache .getId ());
592577 if (hostVo == null ) {
@@ -714,10 +699,7 @@ private static void tagCommand(final Command cmd) {
714699 cmd .setContextParam ("job" , "job-" + job .getId ());
715700 }
716701 }
717- String logcontextid = ThreadContext .get ("logcontextid" );
718- if (StringUtils .isNotEmpty (logcontextid )) {
719- cmd .setContextParam ("logid" , logcontextid );
720- }
702+ ThreadContextCommandUtil .setContextInCommand (cmd );
721703 }
722704
723705 /**
@@ -2180,12 +2162,9 @@ private void processStartupRoutingCommand(StartupRoutingCommand startup, long ho
21802162 * and request to reconnect (instead of throwing away).
21812163 */
21822164 private void processPingCommand (Link link , PingCommand cmd , Request request ) {
2183- request .logD ("Processing:" , true );
2184- if (cmd .getContextParam ("logid" ) != null ) {
2185- ThreadContext .put ("logcontextid" , cmd .getContextParam ("logid" ));
2186- }
2187- boolean requestStartupCommand = cmd instanceof PingRoutingCommand && ((PingRoutingCommand ) cmd ).isGatewayAccessible ();
2188- PingAnswer answer = new PingAnswer (cmd , getAvoidMsList (), requestStartupCommand );
2165+ ThreadContextCommandUtil .propagateContextFromCommand (cmd );
2166+ boolean sendStartup = cmd instanceof PingRoutingCommand && ((PingRoutingCommand ) cmd ).isGatewayAccessible ();
2167+ PingAnswer answer = new PingAnswer (cmd , getAvoidMsList (), sendStartup );
21892168 Response response = new Response (request , new Answer []{answer }, _nodeId , cmd .getHostId ());
21902169 response .setSequence (request .getSequence ());
21912170 response .logD ("Sending:" , true );
@@ -2211,9 +2190,7 @@ protected void processRequest(final Link link, final Request request) {
22112190 Command cmd = cmds [0 ];
22122191 boolean logD = true ;
22132192
2214- if (cmd !=null && cmd .getContextParam ("logid" ) != null ) {
2215- ThreadContext .put ("logcontextid" , cmd .getContextParam ("logid" ));
2216- }
2193+ ThreadContextCommandUtil .propagateContextFromCommand (cmd );
22172194
22182195 if (attache == null ) {
22192196 // FIXME: if there are more than one command and first is not startup command,
@@ -2281,13 +2258,14 @@ protected void processRequest(final Link link, final Request request) {
22812258 }
22822259 return ;
22832260 } else if (cmd instanceof AgentControlCommand ) {
2284- answer = handleControlCommand (attache , (AgentControlCommand ) cmd );
2261+ answer = handleAgentControlCommand (attache , (AgentControlCommand ) cmd );
22852262 } else if (cmd instanceof AgentConnectStatusCommand ) {
22862263 answer = handleAgentConnectStatusCommand (attache , (AgentConnectStatusCommand ) cmd );
22872264 } else {
22882265 handleCommands (attache , request .getSequence (), new Command [] {cmd });
22892266 if (cmd instanceof PingCommand ) {
22902267 final long cmdHostId = ((PingCommand )cmd ).getHostId ();
2268+ boolean requestStartupCommand = false ;
22912269
22922270 final HostVO host = _hostDao .findById (cmdHostId );
22932271 boolean gatewayAccessible = true ;
@@ -2315,7 +2293,6 @@ protected void processRequest(final Link link, final Request request) {
23152293 }
23162294 }
23172295
2318- boolean requestStartupCommand = false ;
23192296 if (host != null && gatewayAccessible ) {
23202297 requestStartupCommand = sendRequestStartupCommand (hostId , host );;
23212298 }
@@ -2397,6 +2374,20 @@ protected void processResponse(final Link link, final Response response) {
23972374 }
23982375 }
23992376
2377+ private AgentControlAnswer handleAgentControlCommand (final AgentAttache attache , final AgentControlCommand cmd ) {
2378+ AgentControlAnswer answer ;
2379+
2380+ for (final Pair <Integer , Listener > listener : _cmdMonitors ) {
2381+ answer = listener .second ().processControlCommand (attache .getId (), cmd );
2382+ if (answer != null ) {
2383+ return answer ;
2384+ }
2385+ }
2386+
2387+ logger .warn ("No handling of agent control command: {} sent from {}" , cmd , attache );
2388+ return new AgentControlAnswer (cmd );
2389+ }
2390+
24002391 private void processAgentConnectStatusCommand (Link link , AgentConnectStatusCommand cmd , Request request ) {
24012392 request .logD ("Processing:" , true );
24022393 Long hostId = cmd .getHostId ();
0 commit comments