搜索

首页  >  问答  >  正文

c++-构造函数-虚函数 - Mesos源码中,有关C++几个类的设计问题

节选了有关调度算法代码中的三个类,不太明白为什么要这样设计,希望可以和大家探讨一下。
代码如下:

class Allocator
{
public:
  Allocator() {}

  virtual ~Allocator() {}

  virtual void addFramework(
      const FrameworkID& frameworkId,
      const FrameworkInfo& frameworkInfo,
      const Resources& used) = 0;

  virtual void removeFramework(
      const FrameworkID& frameworkId) = 0;
  /*省略了其他函数,其他函数均为纯虚函数*/
};


// A wrapper for Process-based allocators. It redirects all function
// invocations to the underlying AllocatorProcess and manages its
// lifetime. We ensure the template parameter AllocatorProcess
// implements MesosAllocatorProcess by storing a pointer to it.
template <typename AllocatorProcess>
class MesosAllocator : public Allocator
{
public:
  MesosAllocator();

  ~MesosAllocator();

  void addFramework(
      const FrameworkID& frameworkId,
      const FrameworkInfo& frameworkInfo,
      const Resources& used);

  void removeFramework(
      const FrameworkID& frameworkId);
  /*省略了其他函数,函数实现在最下面*/
private:
  MesosAllocator(const MesosAllocator&); // Not copyable.
  MesosAllocator& operator=(const MesosAllocator&); // Not assignable.

  MesosAllocatorProcess* process;
};

// The basic interface for all Process-based allocators.
class MesosAllocatorProcess : public process::Process<MesosAllocatorProcess>
{
public:
  MesosAllocatorProcess() {}

  virtual ~MesosAllocatorProcess() {}

  virtual void addFramework(
      const FrameworkID& frameworkId,
      const FrameworkInfo& frameworkInfo,
      const Resources& used) = 0;

  virtual void removeFramework(
      const FrameworkID& frameworkId) = 0;
  /*同样省略了其他函数*/
};

/*以下是MesosAllocator中的函数实现*/
template <typename AllocatorProcess>
MesosAllocator<AllocatorProcess>::MesosAllocator()
{
  process = new AllocatorProcess();
  process::spawn(process);
}


template <typename AllocatorProcess>
MesosAllocator<AllocatorProcess>::~MesosAllocator()
{
  process::terminate(process);
  process::wait(process);
  delete process;
}

template <typename AllocatorProcess>
inline void MesosAllocator<AllocatorProcess>::addFramework(
    const FrameworkID& frameworkId,
    const FrameworkInfo& frameworkInfo,
    const Resources& used)
{
  process::dispatch(
      process,
      &MesosAllocatorProcess::addFramework,
      frameworkId,
      frameworkInfo,
      used);
}


template <typename AllocatorProcess>
inline void MesosAllocator<AllocatorProcess>::removeFramework(
    const FrameworkID& frameworkId)
{
  process::dispatch(
      process,
      &MesosAllocatorProcess::removeFramework,
      frameworkId);
}

以上就是Mesos源码中关于调度算法的三个类的设计,其中英文注释是源码中自带的。个人理解第一个类Alloctor作用是提供一些调度过程中所涉及的接口,第二个类模版MesosAlloctor和第三个类MesosAl-loctorProcess目前想不通为什么这样设计,个人感觉使用一个类继承Alloctor进行实现即可,希望大家可以指点一二。

伊谢尔伦伊谢尔伦2804 天前696

全部回复(0)我来回复

暂无回复
  • 取消回复