Skip to content

Commit f598354

Browse files
author
Marcus Sorensen
committed
Add License headers, refactor nested try
1 parent adf876d commit f598354

File tree

3 files changed

+83
-39
lines changed

3 files changed

+83
-39
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
117
package com.cloud.resource;
218

19+
/**
20+
* AgentStatusUpdater is an agent with triggerable update functionality
21+
*/
322
public interface AgentStatusUpdater {
23+
/**
24+
* Trigger the sending of an update (Ping).
25+
*/
426
void triggerUpdate();
527
}

core/src/main/java/com/cloud/resource/ResourceStatusUpdater.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
117
package com.cloud.resource;
218

319
/**
420
* ResourceStatusUpdater is a resource that can trigger out of band status updates
521
*/
622
public interface ResourceStatusUpdater {
723
/**
24+
* Register an AgentStatusUpdater to use for triggering out of band updates.
25+
*
826
* @param updater The object to call triggerUpdate() on
927
*/
1028
void registerStatusUpdater(AgentStatusUpdater updater);

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.libvirt.SchedUlongParameter;
9292
import org.libvirt.Secret;
9393
import org.libvirt.VcpuInfo;
94+
import org.libvirt.event.DomainEvent;
9495
import org.libvirt.event.DomainEventDetail;
9596
import org.libvirt.event.StoppedDetail;
9697
import org.w3c.dom.Document;
@@ -3608,55 +3609,58 @@ private StartupStorageCommand createLocalStoragePool(String localStoragePath, St
36083609
}
36093610

36103611
private void setupLibvirtEventListener() {
3612+
final Thread libvirtListenerThread = new Thread(() -> {
3613+
try {
3614+
Library.runEventLoop();
3615+
} catch (LibvirtException e) {
3616+
s_logger.error("LibvirtException was thrown in event loop: ", e);
3617+
} catch (InterruptedException e) {
3618+
s_logger.error("Libvirt event loop was interrupted: ", e);
3619+
}
3620+
});
3621+
36113622
try {
3612-
final Thread t = new Thread(() -> {
3613-
try {
3614-
Library.runEventLoop();
3615-
} catch (LibvirtException e) {
3616-
s_logger.error("LibvirtException was thrown in event loop: ", e);
3617-
} catch (InterruptedException e) {
3618-
s_logger.error("Libvirt event loop was interrupted: ", e);
3619-
}
3620-
});
3621-
t.setDaemon(true);
3622-
t.start();
3623+
libvirtListenerThread.setDaemon(true);
3624+
libvirtListenerThread.start();
36233625

36243626
Connect conn = LibvirtConnection.getConnection();
3625-
conn.addLifecycleListener((domain, domainEvent) -> {
3626-
try {
3627-
s_logger.debug(String.format("Got event lifecycle change on Domain %s, event %s", domain.getName(), domainEvent));
3628-
if (domainEvent != null) {
3629-
switch (domainEvent.getType()) {
3630-
case STOPPED:
3631-
/* libvirt-destroyed VMs have detail StoppedDetail.DESTROYED, self shutdown guests are StoppedDetail.SHUTDOWN
3632-
* Checking for this helps us differentiate between events where cloudstack or admin stopped the VM vs guest
3633-
* initiated, and avoid pushing extra updates for actions we are initiating without a need for extra tracking */
3634-
DomainEventDetail detail = domainEvent.getDetail();
3635-
if (StoppedDetail.SHUTDOWN.equals(detail) || StoppedDetail.CRASHED.equals(detail)) {
3636-
s_logger.info("Triggering out of band status update due to completed self-shutdown or crash of VM");
3637-
_agentStatusUpdater.triggerUpdate();
3638-
} else {
3639-
s_logger.debug("Event detail: " + detail);
3640-
}
3641-
break;
3642-
default:
3643-
s_logger.debug(String.format("No handling for event %s", domainEvent));
3644-
}
3645-
}
3646-
} catch (LibvirtException e) {
3647-
s_logger.error("Libvirt exception while processing lifecycle event", e);
3648-
} catch (Throwable e) {
3649-
s_logger.error("Error during lifecycle", e);
3650-
}
3651-
return 0;
3652-
});
3627+
conn.addLifecycleListener(this::onDomainLifecycleChange);
36533628

36543629
s_logger.debug("Set up the libvirt domain event lifecycle listener");
36553630
} catch (LibvirtException e) {
36563631
s_logger.error("Failed to get libvirt connection for domain event lifecycle", e);
36573632
}
36583633
}
36593634

3635+
private int onDomainLifecycleChange(Domain domain, DomainEvent domainEvent) {
3636+
try {
3637+
s_logger.debug(String.format("Got event lifecycle change on Domain %s, event %s", domain.getName(), domainEvent));
3638+
if (domainEvent != null) {
3639+
switch (domainEvent.getType()) {
3640+
case STOPPED:
3641+
/* libvirt-destroyed VMs have detail StoppedDetail.DESTROYED, self shutdown guests are StoppedDetail.SHUTDOWN
3642+
* Checking for this helps us differentiate between events where cloudstack or admin stopped the VM vs guest
3643+
* initiated, and avoid pushing extra updates for actions we are initiating without a need for extra tracking */
3644+
DomainEventDetail detail = domainEvent.getDetail();
3645+
if (StoppedDetail.SHUTDOWN.equals(detail) || StoppedDetail.CRASHED.equals(detail)) {
3646+
s_logger.info("Triggering out of band status update due to completed self-shutdown or crash of VM");
3647+
_agentStatusUpdater.triggerUpdate();
3648+
} else {
3649+
s_logger.debug("Event detail: " + detail);
3650+
}
3651+
break;
3652+
default:
3653+
s_logger.debug(String.format("No handling for event %s", domainEvent));
3654+
}
3655+
}
3656+
} catch (LibvirtException e) {
3657+
s_logger.error("Libvirt exception while processing lifecycle event", e);
3658+
} catch (Throwable e) {
3659+
s_logger.error("Error during lifecycle", e);
3660+
}
3661+
return 0;
3662+
}
3663+
36603664
public String diskUuidToSerial(String uuid) {
36613665
String uuidWithoutHyphen = uuid.replace("-","");
36623666
return uuidWithoutHyphen.substring(0, Math.min(uuidWithoutHyphen.length(), 20));

0 commit comments

Comments
 (0)