Re: [PATCH v4 4/5] ch: events: facilitate lifecycle events handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 12/2/2024 3:46 AM, Purna Pavan Chandra Aekkaladevi wrote:
Implement `virCHProcessEvent` that maps event string to corresponding
event type and take appropriate actions. As part of this, handle the
shutdown event by correctly updating the domain state. this change also
nit: Capitalize "this" as you are starting a new sentence.

facilitates the handling of other VM lifecycle events, such as booting,
rebooting, pause, resume, etc.

Signed-off-by: Purna Pavan Chandra Aekkaladevi <paekkaladevi@xxxxxxxxxxxxxxxxxxx>
---
  src/ch/ch_events.c | 87 +++++++++++++++++++++++++++++++++++++++++++++-
  src/ch/ch_events.h | 26 ++++++++++++++
  2 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c
index a4c2fc4130..45605a0826 100644
--- a/src/ch/ch_events.c
+++ b/src/ch/ch_events.c
@@ -30,6 +30,26 @@
VIR_LOG_INIT("ch.ch_events"); +VIR_ENUM_IMPL(virCHEvent,
+              VIR_CH_EVENT_LAST,
+              "vmm:starting",
+              "vmm:shutdown",
+              "vm:booting",
+              "vm:booted",
+              "vm:rebooting",
+              "vm:rebooted",
+              "vm:shutdown",
+              "vm:deleted",
+              "vm:pausing",
+              "vm:paused",
+              "vm:resuming",
+              "vm:resumed",
+              "vm:snapshotting",
+              "vm:snapshotted",
+              "vm:restoring",
+              "vm:restored",
+);
+
  static int virCHEventStopProcess(virDomainObj *vm,
                                   virDomainShutoffReason reason)
  {
@@ -45,6 +65,67 @@ static int virCHEventStopProcess(virDomainObj *vm,
      return 0;
  }
+static int virCHProcessEvent(virCHMonitor *mon,
+                             virJSONValue *eventJSON)
+{
+    const char *event;
+    const char *source;
+    virCHEvent ev;
+    g_autofree char *timestamp = NULL;
+    g_autofree char *full_event = NULL;
+    virDomainObj *vm = mon->vm;
+    int ret = 0;
+
+    if (virJSONValueObjectHasKey(eventJSON, "source") == 0) {
+        VIR_WARN("%s: Invalid JSON from monitor, no source key", vm->def->name);
+        return -1;
+    }
+    if (virJSONValueObjectHasKey(eventJSON, "event") == 0) {
+        VIR_WARN("%s: Invalid JSON from monitor, no event key", vm->def->name);
+        return -1;
+    }
+    source = virJSONValueObjectGetString(eventJSON, "source");
+    event = virJSONValueObjectGetString(eventJSON, "event");
+    full_event = g_strdup_printf("%s:%s", source, event);
+    ev = virCHEventTypeFromString(full_event);
+    VIR_DEBUG("%s: Source: %s, Event: %s, ev: %d", vm->def->name, source, event, ev);
+
+    switch (ev) {
+        case VIR_CH_EVENT_VMM_STARTING:
+        case VIR_CH_EVENT_VM_BOOTING:
+        case VIR_CH_EVENT_VM_BOOTED:
+        case VIR_CH_EVENT_VM_REBOOTING:
+        case VIR_CH_EVENT_VM_REBOOTED:
+        case VIR_CH_EVENT_VM_PAUSING:
+        case VIR_CH_EVENT_VM_PAUSED:
+        case VIR_CH_EVENT_VM_RESUMING:
+        case VIR_CH_EVENT_VM_RESUMED:
+        case VIR_CH_EVENT_VM_SNAPSHOTTING:
+        case VIR_CH_EVENT_VM_SNAPSHOTTED:
+        case VIR_CH_EVENT_VM_RESTORING:
+        case VIR_CH_EVENT_VM_RESTORED:
+        case VIR_CH_EVENT_VM_DELETED:
+            break;
+        case VIR_CH_EVENT_VMM_SHUTDOWN:
+            if (virCHEventStopProcess(vm, VIR_DOMAIN_SHUTOFF_SHUTDOWN)) {
+                VIR_WARN("Failed to mark the VM(%s) as SHUTDOWN!",
+                          vm->def->name);
+                ret = -1;
+            }
+            break;
+        case VIR_CH_EVENT_VM_SHUTDOWN:
+            virObjectLock(vm);
+            virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
+            virObjectUnlock(vm);
+            break;
+        case VIR_CH_EVENT_LAST:
+        default:
+            VIR_WARN("%s: Unknown event: %s", vm->def->name, full_event);
+    }
+
+    return ret;
+}
+
  /**
   * virCHProcessEvents:
   * @mon: the CH monitor object
@@ -94,7 +175,11 @@ static int virCHProcessEvents(virCHMonitor *mon)
                  json_start = buf + start_index;
if ((obj = virJSONValueFromString(json_start))) {
-                    /* Process the event string (obj) here */
+                    if (virCHProcessEvent(mon, obj) < 0) {
+                        VIR_WARN("%s: Failed to process JSON event doc: %s",
+                                 vm->def->name, json_start);
+                        ret = -1;
+                    }
                      virJSONValueFree(obj);
                  } else {
                      VIR_WARN("%s: Invalid JSON event doc: %s",
diff --git a/src/ch/ch_events.h b/src/ch/ch_events.h
index 2e9cdf03bb..3b360628f7 100644
--- a/src/ch/ch_events.h
+++ b/src/ch/ch_events.h
@@ -24,5 +24,31 @@
#define CH_EVENT_BUFFER_SZ PIPE_BUF +typedef enum {
+    /* source: vmm */
+    VIR_CH_EVENT_VMM_STARTING = 0,
+    VIR_CH_EVENT_VMM_SHUTDOWN,
+
+    /* source: vm */
+    VIR_CH_EVENT_VM_BOOTING,
+    VIR_CH_EVENT_VM_BOOTED,
+    VIR_CH_EVENT_VM_REBOOTING,
+    VIR_CH_EVENT_VM_REBOOTED,
+    VIR_CH_EVENT_VM_SHUTDOWN,
+    VIR_CH_EVENT_VM_DELETED,
+    VIR_CH_EVENT_VM_PAUSING,
+    VIR_CH_EVENT_VM_PAUSED,
+    VIR_CH_EVENT_VM_RESUMING,
+    VIR_CH_EVENT_VM_RESUMED,
+    VIR_CH_EVENT_VM_SNAPSHOTTING,
+    VIR_CH_EVENT_VM_SNAPSHOTTED,
+    VIR_CH_EVENT_VM_RESTORING,
+    VIR_CH_EVENT_VM_RESTORED,
+
+    VIR_CH_EVENT_LAST
+} virCHEvent;
+
+VIR_ENUM_DECL(virCHEvent);
+
  int virCHStartEventHandler(virCHMonitor *mon);
  void virCHStopEventHandler(virCHMonitor *mon);

--
Regards,
Praveen K Paladugu



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux