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

Re: [Ovirt-devel] [PATCH matahari] Refactored the Host agent.



On Thu, 2010-04-15 at 11:19 -0400, Darryl L. Pierce wrote:
> Replaced the previous HostWrapper class with the new HostAgent class,
> which more closely resembles the style of the ProcessorsAgent class.
> 
> Deleted the HostWrapper class entirely.
> 
> Refactored the daemon entry point to reduce the number of methods
> invoked in order to start or stop the HostAgent instance. Moved the
> NICWrapper's setupQMFObject method from private to public so that it can
> be invoked by the HostAgent. This is a temporary measure since the
> NICWrapper will be replaced by a future patch.

ACK

	Ian


> Signed-off-by: Darryl L. Pierce <dpierce redhat com>
> ---
>  src/host.cpp       |  263 +++++++++++++---------------------------------------
>  src/host.h         |   75 ++++-----------
>  src/main.cpp       |   57 ++++--------
>  src/nic.h          |    2 +-
>  src/processors.cpp |    6 +
>  src/processors.h   |    3 +
>  6 files changed, 111 insertions(+), 295 deletions(-)
> 
> diff --git a/src/host.cpp b/src/host.cpp
> index ac04f47..885068b 100644
> --- a/src/host.cpp
> +++ b/src/host.cpp
> @@ -37,213 +37,80 @@ using namespace std;
>  using qpid::management::Manageable;
>  namespace _qmf = qmf::com::redhat::matahari;
>  
> -ostream& operator<<(ostream &output, const HostWrapper& host)
> +void
> +HostAgent::setup(ManagementAgent* agent)
>  {
> -    output << "Node" << endl << endl;
> -    output << "UUID: " << host.uuid << endl;
> -    output << "Hostname: " << host.hostname << endl;
> -    output << "Memory: " << host.memory << endl;
> -    output << "Hypervisor: " << host.hypervisor << endl;
> -    output << "Arch: " << host.arch << endl << endl;
> -
> -    vector<CPUWrapper*> cpus = host.cpus;
> -    vector<NICWrapper*> nics = host.nics;
> -
> -    for (vector<CPUWrapper*>::iterator iter = cpus.begin();
> -         iter!= cpus.end();
> -         iter++) {
> -        output << **iter << endl;
> -    }
> -    for (vector<NICWrapper*>::iterator iter = nics.begin();
> -         iter!= nics.end();
> -         iter++) {
> -        output << **iter << endl;
> +  management_object = new _qmf::Host(agent, this);
> +  agent->addObject(management_object);
> +
> +  // discover the aspects of the host
> +  processors.setup(agent, this);
> +
> +  LibHalContext *hal_ctx;
> +  int ret;
> +
> +  // Get our HAL Context or die trying
> +  hal_ctx = get_hal_ctx();
> +  if (!hal_ctx)
> +    throw runtime_error("Unable to get HAL Context Structure.");
> +
> +  try {
> +    NICWrapper::fillNICInfo(this->nics, agent, hal_ctx);
> +
> +    // Host UUID
> +    char *uuid_c = get_uuid(hal_ctx);
> +    string uuid(uuid_c);
> +    management_object->set_uuid(uuid);
> +
> +    // Hostname
> +    char hostname_c[HOST_NAME_MAX];
> +    ret = gethostname(hostname_c, sizeof(hostname_c));
> +    if (ret != 0)
> +      throw runtime_error("Unable to get hostname");
> +    string hostname(hostname_c);
> +    management_object->set_hostname(hostname);
> +
> +    // Hypervisor, arch, memory
> +    management_object->set_memory(0);
> +    management_object->set_hypervisor("unknown");
> +    management_object->set_arch("unknown");
> +
> +    virConnectPtr connection;
> +    virNodeInfo info;
> +    connection = virConnectOpenReadOnly(NULL);
> +    if (connection) {
> +      const char *hv = virConnectGetType(connection);
> +      if (hv != NULL)
> +        management_object->set_hypervisor(hv);
> +      ret = virNodeGetInfo(connection, &info);
> +      if (ret == 0) {
> +        management_object->set_arch(info.model);
> +        management_object->set_memory(info.memory);
> +      }
>      }
> +    virConnectClose(connection);
>  
> -    output << "End Node" << endl;
> -    return output;
> -}
> +    management_object->set_beeping(false);
>  
> -void HostWrapper::doLoop(void)
> -{
> -    // Someday we might update statistics too!
> -    while(1)
> +    // setup the nic objects
> +    for(vector<NICWrapper*>::iterator iter = nics.begin();
> +        iter != nics.end();
> +        iter++)
>        {
> -        // update all statistics
> -        processors.update_load_averages();
> -        sleep(5);
> -      }
> -}
> -
> -void HostWrapper::setupQMFObjects(ManagementAgent *agent)
> -{
> -    // Set up Host object
> -    mgmt_object = new _qmf::Host(agent, this);
> -    agent->addObject(mgmt_object);
> -    syncQMFHostObject();
> -
> -    processors.setup(agent, this);
> -
> -    // Iterate over list and set up CPU objects
> -    for (vector<CPUWrapper*>::iterator iter = cpus.begin();
> -         iter!= cpus.end();
> -         iter++) {
> -        (*iter)->setupQMFObject(agent, this);
> -    }
> -    // Iterate over list and set up NIC objects
> -    for (vector<NICWrapper*>::iterator iter = nics.begin();
> -         iter!= nics.end();
> -         iter++) {
>          (*iter)->setupQMFObject(agent, this);
> -    }
> -}
> -
> -void HostWrapper::syncQMFHostObject(void)
> -{
> -    mgmt_object->set_uuid(uuid);
> -    mgmt_object->set_hostname(hostname);
> -    mgmt_object->set_memory(memory);
> -    mgmt_object->set_hypervisor(hypervisor);
> -    mgmt_object->set_arch(arch);
> -    mgmt_object->set_beeping(beeping);
> -}
> -
> -void HostWrapper::cleanupQMFObjects(void)
> -{
> -    // Clean up Host object
> -    mgmt_object->resourceDestroy();
> -
> -    // Iterate over list and clean up CPU objects
> -    for (vector<CPUWrapper*>::iterator iter = cpus.begin();
> -         iter!= cpus.end();
> -         iter++) {
> -        (*iter)->cleanupQMFObject();
> -    }
> -    // Iterate over list and clean up NIC objects
> -    for (vector<NICWrapper*>::iterator iter = nics.begin();
> -         iter!= nics.end();
> -         iter++) {
> -        (*iter)->cleanupQMFObject();
> -    }
> -}
> -
> -void HostWrapper::cleanupMemberObjects(void)
> -{
> -    // Get rid of the CPUWrapper objects for this host
> -    for (vector<CPUWrapper*>::iterator iter = cpus.begin(); iter != cpus.end();) {
> -        delete (*iter);
> -        iter = cpus.erase(iter);
> -    }
> -    // Get rid of the NICWrapper objects for this host
> -    for (vector<NICWrapper*>::iterator iter = nics.begin(); iter != nics.end();) {
> -        delete (*iter);
> -        iter = nics.erase(iter);
> -    }
> -}
> -
> -void HostWrapper::disposeHostWrapper()
> -{
> -    if (hostSingleton == NULL)
> -        return;
> -
> -    hostSingleton->cleanupQMFObjects();
> -    hostSingleton->cleanupMemberObjects();
> -
> -    delete hostSingleton;
> -    hostSingleton = NULL;
> -}
> -
> -HostWrapper* HostWrapper::setupHostWrapper(ManagementAgent *agent)
> -{
> -    if (hostSingleton != NULL)
> -        return hostSingleton;
> -
> -    LibHalContext *hal_ctx;
> -    int ret;
> -
> -    // Get our HAL Context or die trying
> -    hal_ctx = get_hal_ctx();
> -    if (!hal_ctx)
> -        throw runtime_error("Unable to get HAL Context Structure.");
> -
> -    HostWrapper *host = new HostWrapper();
> -
> -    try {
> -        CPUWrapper::fillCPUInfo(host->cpus, agent);
> -        NICWrapper::fillNICInfo(host->nics, agent, hal_ctx);
> -
> -        // Host UUID
> -        char *uuid_c = get_uuid(hal_ctx);
> -        string uuid(uuid_c);
> -        host->uuid = uuid;
> -
> -        // Hostname
> -        char hostname_c[HOST_NAME_MAX];
> -        ret = gethostname(hostname_c, sizeof(hostname_c));
> -        if (ret != 0)
> -            throw runtime_error("Unable to get hostname");
> -        string hostname(hostname_c);
> -        host->hostname = hostname;
> -
> -        // Hypervisor, arch, memory
> -        host->memory = 0;
> -        host->hypervisor = "unknown";
> -        host->arch = "unknown";
> -
> -        virConnectPtr connection;
> -        virNodeInfo info;
> -        connection = virConnectOpenReadOnly(NULL);
> -        if (connection) {
> -            const char *hv = virConnectGetType(connection);
> -            if (hv != NULL)
> -                host->hypervisor = hv;
> -            ret = virNodeGetInfo(connection, &info);
> -            if (ret == 0) {
> -                host->arch = info.model;
> -                host->memory = info.memory;
> -            }
> -        }
> -            virConnectClose(connection);
> -
> -        host->beeping = false;
> -    }
> -    catch (...) {
> -        host->cleanupMemberObjects();
> -        put_hal_ctx(hal_ctx);
> -        delete host;
> -        throw;
> -    }
> -
> -    // Close the Hal Context
> +      }
> +  }
> +  catch (...) {
>      put_hal_ctx(hal_ctx);
> +    throw;
> +  }
>  
> -    host->setupQMFObjects(agent);
> -
> -    // Setup singleton reference and return
> -    hostSingleton = host;
> -    return hostSingleton;
> +  // Close the Hal Context
> +  put_hal_ctx(hal_ctx);
>  }
>  
> -void HostWrapper::reboot()
> +void
> +HostAgent::update(void)
>  {
> -    system("shutdown -r now");
> -}
> -
> -void HostWrapper::shutdown()
> -{
> -    system("shutdown -h now");
> -}
> -
> -Manageable::status_t
> -HostWrapper::ManagementMethod(uint32_t methodId, Args& args, string& text)
> -{
> -    switch(methodId) {
> -        case _qmf::Host::METHOD_SHUTDOWN:
> -            shutdown();
> -            return Manageable::STATUS_OK;
> -        case _qmf::Host::METHOD_REBOOT:
> -            reboot();
> -            return Manageable::STATUS_OK;
> -    }
> -    return Manageable::STATUS_NOT_IMPLEMENTED;
> +  processors.update();
>  }
> diff --git a/src/host.h b/src/host.h
> index 148c91f..c19da3e 100644
> --- a/src/host.h
> +++ b/src/host.h
> @@ -1,3 +1,6 @@
> +#ifndef __HOST_H
> +#define __HOST_H
> +
>  /* host.h - Copyright (C) 2009 Red Hat, Inc.
>   * Written by Arjun Roy <arroy redhat com>
>   *
> @@ -32,67 +35,25 @@ using namespace std;
>  
>  using qpid::management::Manageable;
>  
> -class HostWrapper : public Manageable
> +class HostAgent : public Manageable
>  {
> -    static HostWrapper *hostSingleton;
> -
> -    friend ostream& operator<<(ostream &output, const HostWrapper& host);
> -
> -    // Host Parameters
> -    string uuid;
> -    string hostname;
> -    string hypervisor;
> -    string arch;
> -    bool beeping;
> -    int memory;
> -
> -    // Aggregated components
> -    vector<CPUWrapper*> cpus;
> -    vector<NICWrapper*> nics;
> -
> -    ProcessorsAgent processors;
> -
> -    // QMF related fields
> -    ManagementAgent *agent;
> -    qmf::com::redhat::matahari::Host *mgmt_object;
> -
> -    // Methods to put up / take down QMF objects
> -    void setupQMFObjects(ManagementAgent *agent);
> -    void cleanupQMFObjects(void);
> -
> -    // Housekeeping methods
> -    void syncQMFHostObject(void);
> -    void cleanupMemberObjects(void);
> -
> -    // Host functionality
> -    void reboot();
> -    void shutdown();
> -
> -    // Constructors and Destructor are private
> -    HostWrapper() {}
> -    HostWrapper(const HostWrapper &) {}
> -    ~HostWrapper() {}
> + private:
> +  qmf::com::redhat::matahari::Host* management_object;
> +  ProcessorsAgent processors;
> +  vector<NICWrapper*> nics;
>  
>   public:
> -    // Factory methods to create/dispose of the singleton
> -    static HostWrapper *setupHostWrapper(ManagementAgent *agent);
> -    static void disposeHostWrapper(void);
> -
> -    // QMF Methods
> -    ManagementObject* GetManagementObject(void) const { return mgmt_object; }
> -    status_t ManagementMethod(uint32_t methodId, Args& args, string& text);
> +  HostAgent() {}
> +  virtual ~HostAgent() {}
>  
> -    // Field Accessors
> -    const string &getUUID(void) { return uuid; }
> -    const string &getHostname(void) { return hostname; }
> -    const string &getHypervisor(void) { return hypervisor; }
> -    const string &getArch(void) { return arch; }
> -    bool isBeeping(void) { return beeping; }
> -    int getMemory(void) { return memory; }
> +  ManagementObject* GetManagementObject(void) const { return management_object; }
>  
> -    const vector<CPUWrapper*> &getCPUList(void) { return cpus; }
> -    const vector<NICWrapper*> &getNICList(void) { return nics; }
> +  void setup(ManagementAgent* agent);
> +  void update(void);
>  
> -    // Main Loop
> -    void doLoop(void);
> +  // agent methods
> +  void shutdown(void);
> +  void reboot(void);
>  };
> +
> +#endif // __HOST_H
> diff --git a/src/main.cpp b/src/main.cpp
> index 002a02e..8493864 100644
> --- a/src/main.cpp
> +++ b/src/main.cpp
> @@ -42,18 +42,11 @@ namespace _qmf = qmf::com::redhat::matahari;
>  
>  // Global Variables
>  ManagementAgent::Singleton* singleton;
> -HostWrapper* HostWrapper::hostSingleton = NULL;
>  
> -void cleanup(void)
> +void
> +shutdown(int /*signal*/)
>  {
> -    HostWrapper::disposeHostWrapper();
> -    delete singleton;
> -}
> -
> -void shutdown(int)
> -{
> -    cleanup();
> -    exit(0);
> +  exit(0);
>  }
>  
>  static void
> @@ -69,21 +62,22 @@ print_usage()
>      printf("\t-p | --port       specify broker port.\n");
>  }
>  
> -int do_main(int argc, char **argv)
> +int
> +main(int argc, char **argv)
>  {
>      int arg;
>      int idx = 0;
>      bool daemonize = false;
>      bool gssapi = false;
>      bool verbose = false;
> -    char *host = NULL;
> +    char *servername = NULL;
>      char *username = NULL;
>      char *service = NULL;
> -    int port = 5672;
> +    int serverport = 5672;
>  
>      ConnectionSettings settings;
>      ManagementAgent *agent;
> -    HostWrapper *hostWrapper;
> +    HostAgent host;
>  
>      struct option opt[] = {
>          {"help", no_argument, NULL, 'h'},
> @@ -131,7 +125,7 @@ int do_main(int argc, char **argv)
>                  break;
>              case 'p':
>                  if (optarg) {
> -                    port = atoi(optarg);
> +                    serverport = atoi(optarg);
>                  } else {
>                      print_usage();
>                      exit(1);
> @@ -139,7 +133,7 @@ int do_main(int argc, char **argv)
>                  break;
>              case 'b':
>                  if (optarg) {
> -                    host = strdup(optarg);
> +                    servername = strdup(optarg);
>                  } else {
>                      print_usage();
>                      exit(1);
> @@ -169,8 +163,8 @@ int do_main(int argc, char **argv)
>      signal(SIGINT, shutdown);
>  
>      // Connect to the broker
> -    settings.host = host ? host : "127.0.0.1";
> -    settings.port = port;
> +    settings.host = servername ? servername : "127.0.0.1";
> +    settings.port = serverport;
>  
>      if (username != NULL) {
>          settings.username = username;
> @@ -185,28 +179,13 @@ int do_main(int argc, char **argv)
>      agent->init(settings, 5, false, ".magentdata");
>  
>      // Get the info and post it to the broker
> -    try {
> -        hostWrapper = HostWrapper::setupHostWrapper(agent);
> -    }
> -    catch (...) {
> -        cleanup();
> -            throw;
> -    }
> +    host.setup(agent);
>  
> -    // Main loop
> -    hostWrapper->doLoop();
> +    while(1)
> +      {
> +        host.update();
> +        sleep(5);
> +      }
>  
> -    // And we are done
> -    cleanup();
>      return 0;
>  }
> -
> -int main(int argc, char** argv)
> -{
> -    try {
> -        return do_main(argc, argv);
> -    }
> -    catch(std::exception& e) {
> -        cout << "Top Level Exception: " << e.what() << endl;
> -    }
> -}
> diff --git a/src/nic.h b/src/nic.h
> index 15f31fb..c79ecf9 100644
> --- a/src/nic.h
> +++ b/src/nic.h
> @@ -47,7 +47,6 @@ class NICWrapper : public Manageable
>      qmf::com::redhat::matahari::NIC *mgmt_object;
>  
>      // Methods to put up / take down QMF Objects
> -    void setupQMFObject(ManagementAgent *agent, Manageable *parent);
>      void cleanupQMFObject(void);
>      void syncQMFObject(void);
>  
> @@ -77,6 +76,7 @@ class NICWrapper : public Manageable
>      int identifyNIC(int seconds);
>  public:
>  
> +    void setupQMFObject(ManagementAgent *agent, Manageable *parent);
>      // Factory like method
>      static void fillNICInfo(vector<NICWrapper*> &nics,
>                              ManagementAgent *agent,
> diff --git a/src/processors.cpp b/src/processors.cpp
> index 4317b1f..cff66d9 100644
> --- a/src/processors.cpp
> +++ b/src/processors.cpp
> @@ -57,6 +57,12 @@ ProcessorsAgent::setup(ManagementAgent* agent, Manageable* parent)
>  }
>  
>  void
> +ProcessorsAgent::update(void) const
> +{
> +  update_load_averages();
> +}
> +
> +void
>  ProcessorsAgent::update_load_averages(void) const
>  {
>    double load_average;
> diff --git a/src/processors.h b/src/processors.h
> index 3e46b16..17a6a6a 100644
> --- a/src/processors.h
> +++ b/src/processors.h
> @@ -42,6 +42,9 @@ class ProcessorsAgent : public Manageable
>  
>    void setup(ManagementAgent* agent, Manageable* parent);
>  
> +  void update(void) const;
> +
> + private:
>    // agent methods
>    void update_load_averages(void) const;
>  };



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