-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
350 additions
and
126 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
83 changes: 83 additions & 0 deletions
83
compute/src/main/java/org/zstack/compute/host/HostReconnectTask.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,83 @@ | ||
package org.zstack.compute.host; | ||
|
||
import org.springframework.beans.factory.annotation.Autowire; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Configurable; | ||
import org.zstack.core.cloudbus.CloudBus; | ||
import org.zstack.core.cloudbus.CloudBusCallBack; | ||
import org.zstack.core.thread.AsyncTimer; | ||
import org.zstack.header.core.Completion; | ||
import org.zstack.header.core.NoErrorCompletion; | ||
import org.zstack.header.errorcode.ErrorCode; | ||
import org.zstack.header.exception.CloudRuntimeException; | ||
import org.zstack.header.host.HostConstant; | ||
import org.zstack.header.host.ReconnectHostMsg; | ||
import org.zstack.header.message.MessageReply; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) | ||
public abstract class HostReconnectTask extends AsyncTimer { | ||
protected String uuid; | ||
private NoErrorCompletion completion; | ||
|
||
@Autowired | ||
protected CloudBus bus; | ||
|
||
protected enum CanDoAnswer { | ||
Ready, | ||
NotReady, | ||
NoReconnect | ||
} | ||
|
||
protected abstract CanDoAnswer canDoReconnect(); | ||
|
||
public HostReconnectTask(String uuid, NoErrorCompletion completion) { | ||
super(TimeUnit.SECONDS, HostGlobalConfig.PING_HOST_INTERVAL.value(Long.class)); | ||
this.uuid = uuid; | ||
this.completion = completion; | ||
} | ||
|
||
private void reconnectNow(String uuid, Completion completion) { | ||
ReconnectHostMsg msg = new ReconnectHostMsg(); | ||
msg.setHostUuid(uuid); | ||
msg.setSkipIfHostConnected(true); | ||
bus.makeTargetServiceIdByResourceUuid(msg, HostConstant.SERVICE_ID, uuid); | ||
bus.send(msg, new CloudBusCallBack(completion) { | ||
@Override | ||
public void run(MessageReply reply) { | ||
if (reply.isSuccess()) { | ||
completion.success(); | ||
} else { | ||
completion.fail(reply.getError()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void execute() { | ||
CanDoAnswer answer = canDoReconnect(); | ||
if (answer == CanDoAnswer.Ready) { | ||
reconnectNow(uuid, new Completion(completion) { | ||
@Override | ||
public void success() { | ||
completion.done(); | ||
} | ||
|
||
@Override | ||
public void fail(ErrorCode errorCode) { | ||
// still fail to reconnect the host, continue this reconnect task | ||
continueToRunThisTimer(); | ||
} | ||
}); | ||
} else if (answer == CanDoAnswer.NotReady) { | ||
// still not ready to reconnect the host, continue this reconnect task | ||
continueToRunThisTimer(); | ||
} else if (answer == CanDoAnswer.NoReconnect) { | ||
completion.done(); | ||
} else { | ||
throw new CloudRuntimeException(String.format("should not be here[%s]", answer)); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
compute/src/main/java/org/zstack/compute/host/HostReconnectTaskFactory.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,9 @@ | ||
package org.zstack.compute.host; | ||
|
||
import org.zstack.header.core.NoErrorCompletion; | ||
|
||
public interface HostReconnectTaskFactory { | ||
HostReconnectTask createTask(String uuid, NoErrorCompletion completion); | ||
|
||
String getHypervisorType(); | ||
} |
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
7 changes: 0 additions & 7 deletions
7
compute/src/main/java/org/zstack/compute/host/HostTrackerPreReconnectChecker.java
This file was deleted.
Oops, something went wrong.
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
18 changes: 18 additions & 0 deletions
18
plugin/kvm/src/main/java/org/zstack/kvm/KVMHostReconnectTaskFactory.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,18 @@ | ||
package org.zstack.kvm; | ||
|
||
import org.zstack.compute.host.HostReconnectTask; | ||
import org.zstack.compute.host.HostReconnectTaskFactory; | ||
import org.zstack.core.Platform; | ||
import org.zstack.header.core.NoErrorCompletion; | ||
|
||
public class KVMHostReconnectTaskFactory implements HostReconnectTaskFactory { | ||
@Override | ||
public HostReconnectTask createTask(String uuid, NoErrorCompletion completion) { | ||
return Platform.New(() -> new KVMReconnectHostTask(uuid, completion)); | ||
} | ||
|
||
@Override | ||
public String getHypervisorType() { | ||
return KVMConstant.KVM_HYPERVISOR_TYPE; | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
plugin/kvm/src/main/java/org/zstack/kvm/KVMHostTrackerPreReconnectChecker.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
plugin/kvm/src/main/java/org/zstack/kvm/KVMReconnectHostTask.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,29 @@ | ||
package org.zstack.kvm; | ||
|
||
import org.zstack.compute.host.HostReconnectTask; | ||
import org.zstack.core.db.Q; | ||
import org.zstack.header.core.NoErrorCompletion; | ||
import org.zstack.utils.network.NetworkUtils; | ||
|
||
import javax.persistence.Tuple; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class KVMReconnectHostTask extends HostReconnectTask { | ||
public KVMReconnectHostTask(String uuid, NoErrorCompletion completion) { | ||
super(uuid, completion); | ||
} | ||
|
||
@Override | ||
protected CanDoAnswer canDoReconnect() { | ||
Tuple t = Q.New(KVMHostVO.class).select(KVMHostVO_.managementIp, KVMHostVO_.port).eq(KVMHostVO_.uuid, uuid).findTuple(); | ||
if (t == null) { | ||
return CanDoAnswer.NoReconnect; | ||
} | ||
|
||
String ip = t.get(0, String.class); | ||
int port = t.get(1, Integer.class); | ||
|
||
return NetworkUtils.isRemotePortOpen(ip, port, (int) TimeUnit.SECONDS.toMillis(2)) ? | ||
CanDoAnswer.Ready : CanDoAnswer.NotReady; | ||
} | ||
} |
Oops, something went wrong.