Search in sources :

Example 1 with EventSubscriber

use of org.ovirt.vdsm.jsonrpc.client.events.EventSubscriber in project ovirt-engine by oVirt.

the class EventVmStatsRefresher method startMonitoring.

@Override
public void startMonitoring() {
    allVmStatsOnlyRefresher.startMonitoring();
    final String hostname = vdsManager.getVdsHostname();
    resourceManager.subscribe(new EventSubscriber(hostname + "|*|VM_status|*") {

        @Override
        public void onSubscribe(Subscription sub) {
            subscription = sub;
            subscription.request(1);
        }

        @Override
        public void onNext(Map<String, Object> map) {
            try {
                long fetchTime = System.nanoTime();
                printEventInDebug(map);
                List<Pair<VmDynamic, VdsmVm>> vms = convertEvent(map);
                if (!vms.isEmpty()) {
                    getVmsMonitoring().perform(vms, fetchTime, vdsManager, false);
                    processDevices(vms.stream().map(Pair::getSecond), fetchTime);
                }
            } finally {
                subscription.request(1);
            }
        }

        private void printEventInDebug(Map<String, Object> map) {
            if (!log.isDebugEnabled()) {
                return;
            }
            StringBuilder sb = new StringBuilder();
            ObjectDescriptor.toStringBuilder(map, sb);
            log.debug("processing event for host {} data:\n{}", vdsManager.getVdsName(), sb);
        }

        @SuppressWarnings("unchecked")
        private List<Pair<VmDynamic, VdsmVm>> convertEvent(Map<String, Object> map) {
            Double notifyTime = VdsBrokerObjectsBuilder.removeNotifyTimeFromVmStatusEvent(map);
            return map.entrySet().stream().map(idToMap -> toMonitoredVm(new Guid(idToMap.getKey()), (Map<String, Object>) idToMap.getValue(), notifyTime)).collect(Collectors.toList());
        }

        private Pair<VmDynamic, VdsmVm> toMonitoredVm(Guid vmId, Map<String, Object> vmMap, Double notifyTime) {
            VmDynamic dbVm = vmDynamicDao.get(vmId);
            VdsmVm vdsmVm = dbVm == null ? createVdsmVm(vmId, vmMap, notifyTime) : createVdsmVm(dbVm, vmMap, notifyTime);
            return new Pair<>(dbVm, vdsmVm);
        }

        private VdsmVm createVdsmVm(Guid vmId, Map<String, Object> struct, Double notifyTime) {
            VmDynamic fakeVm = new VmDynamic();
            fakeVm.setId(vmId);
            return createVdsmVm(fakeVm, struct, notifyTime);
        }

        private VdsmVm createVdsmVm(VmDynamic dbVmDynamic, Map<String, Object> struct, Double notifyTime) {
            // send a clone of vm dynamic to be overridden with new data
            VmDynamic clonedVmDynamic = new VmDynamic(dbVmDynamic);
            VdsBrokerObjectsBuilder.updateVMDynamicData(clonedVmDynamic, struct, vdsManager.getCopyVds());
            return new VdsmVm(notifyTime).setVmDynamic(clonedVmDynamic).setDevicesHash(VdsBrokerObjectsBuilder.getVmDevicesHash(struct));
        }

        @Override
        public void onError(Throwable t) {
            // communication issue is delivered as a message so we need to request for more
            subscription.request(1);
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : EventSubscriber(org.ovirt.vdsm.jsonrpc.client.events.EventSubscriber) Guid(org.ovirt.engine.core.compat.Guid) VmDynamic(org.ovirt.engine.core.common.businessentities.VmDynamic) List(java.util.List) Subscription(org.reactivestreams.Subscription) Map(java.util.Map) Pair(org.ovirt.engine.core.common.utils.Pair)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 VmDynamic (org.ovirt.engine.core.common.businessentities.VmDynamic)1 Pair (org.ovirt.engine.core.common.utils.Pair)1 Guid (org.ovirt.engine.core.compat.Guid)1 EventSubscriber (org.ovirt.vdsm.jsonrpc.client.events.EventSubscriber)1 Subscription (org.reactivestreams.Subscription)1