2525import javax .inject .Inject ;
2626
2727import org .apache .log4j .Logger ;
28- import com .cloud .vm .SecondaryStorageVmVO ;
29- import com .cloud .vm .dao .SecondaryStorageVmDao ;
28+
3029import org .apache .cloudstack .engine .subsystem .api .storage .EndPoint ;
3130import org .apache .cloudstack .framework .async .AsyncCompletionCallback ;
3231import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
4342import com .cloud .exception .ConnectionException ;
4443import com .cloud .exception .OperationTimedoutException ;
4544import com .cloud .host .Host ;
45+ import com .cloud .host .HostVO ;
4646import com .cloud .host .Status ;
47+ import com .cloud .host .dao .HostDao ;
4748import com .cloud .hypervisor .HypervisorGuruManager ;
4849import com .cloud .utils .component .ComponentContext ;
4950import com .cloud .utils .concurrency .NamedThreadFactory ;
5051import com .cloud .utils .exception .CloudRuntimeException ;
52+ import com .cloud .vm .SecondaryStorageVmVO ;
53+ import com .cloud .vm .dao .SecondaryStorageVmDao ;
5154
5255public class RemoteHostEndPoint implements EndPoint {
5356 private static final Logger s_logger = Logger .getLogger (RemoteHostEndPoint .class );
@@ -60,21 +63,23 @@ public class RemoteHostEndPoint implements EndPoint {
6063 protected HypervisorGuruManager _hvGuruMgr ;
6164 @ Inject
6265 protected SecondaryStorageVmDao vmDao ;
66+ @ Inject
67+ protected HostDao _hostDao ;
6368 private ScheduledExecutorService executor ;
6469
6570 public RemoteHostEndPoint () {
6671 executor = Executors .newScheduledThreadPool (10 , new NamedThreadFactory ("RemoteHostEndPoint" ));
6772 }
6873
6974 private void configure (Host host ) {
70- this . hostId = host .getId ();
71- this . hostAddress = host .getPrivateIpAddress ();
72- this . publicAddress = host .getPublicIpAddress ();
75+ hostId = host .getId ();
76+ hostAddress = host .getPrivateIpAddress ();
77+ publicAddress = host .getPublicIpAddress ();
7378 if (Host .Type .SecondaryStorageVM == host .getType ()) {
7479 String vmName = host .getName ();
7580 SecondaryStorageVmVO ssvm = vmDao .findByInstanceName (vmName );
7681 if (ssvm != null ) {
77- this . publicAddress = ssvm .getPublicIpAddress ();
82+ publicAddress = ssvm .getPublicIpAddress ();
7883 }
7984 }
8085 }
@@ -87,24 +92,36 @@ public static RemoteHostEndPoint getHypervisorHostEndPoint(Host host) {
8792
8893 @ Override
8994 public String getHostAddr () {
90- return this . hostAddress ;
95+ return hostAddress ;
9196 }
9297
9398 @ Override
9499 public String getPublicAddr () {
95- return this . publicAddress ;
100+ return publicAddress ;
96101 }
97102
98103 @ Override
99104 public long getId () {
100- return this .hostId ;
105+ return hostId ;
106+ }
107+
108+ // used when HypervisorGuruManager choose a different host to send command
109+ private void setId (long id ) {
110+ HostVO host = _hostDao .findById (hostId );
111+ if (host != null ) {
112+ configure (host );
113+ }
101114 }
102115
103116 @ Override
104117 public Answer sendMessage (Command cmd ) {
105118 String errMsg = null ;
106119 try {
107120 long newHostId = _hvGuruMgr .getGuruProcessedCommandTargetHost (hostId , cmd );
121+ if (newHostId != hostId ) {
122+ // update endpoint with new host if changed
123+ setId (newHostId );
124+ }
108125 return agentMgr .send (newHostId , cmd );
109126 } catch (AgentUnavailableException e ) {
110127 errMsg = e .toString ();
@@ -126,7 +143,7 @@ public CmdRunner(AsyncCompletionCallback<Answer> callback) {
126143
127144 @ Override
128145 public boolean processAnswers (long agentId , long seq , Answer [] answers ) {
129- this . answer = answers [0 ];
146+ answer = answers [0 ];
130147 executor .schedule (this , 10 , TimeUnit .SECONDS );
131148 return true ;
132149 }
@@ -182,7 +199,11 @@ protected void runInContext() {
182199 @ Override
183200 public void sendMessageAsync (Command cmd , AsyncCompletionCallback <Answer > callback ) {
184201 try {
185- long newHostId = _hvGuruMgr .getGuruProcessedCommandTargetHost (this .hostId , cmd );
202+ long newHostId = _hvGuruMgr .getGuruProcessedCommandTargetHost (hostId , cmd );
203+ if (newHostId != hostId ) {
204+ // update endpoint with new host if changed
205+ setId (newHostId );
206+ }
186207 if (s_logger .isDebugEnabled ()) {
187208 s_logger .debug ("Sending command " + cmd .toString () + " to host: " + newHostId );
188209 }
0 commit comments