C++要点笔记(24)——类的静态成员

C++类中的静态数据成员(static data member)只是声明(declaration),不是定义(definition),需要在类定义外显示地进行定义。这样做是避免定义类的头文件被多个文件包含后,引起编译问题:

class A {
public:
    static int a;
};

int A::a;

除了静态数据成员(static data member),还有静态成员函数(static member function)。它们的共同点是属于整个类,而不属于类的某一个实例,可以通过“类名::成员”方式来访问。需要注意的是,由于静态成员函数没有指向实例的this指针,所以它只能访问类的静态数据成员,而不能访问每个实例的成员。

参考资料:
Static Data Member Initialization
Static data members in C++

 

Mesos笔记 (9)—— Containerizer类代码解析

Containerizer类(定义在src/slave/containerizer/containerizer.hpp )是所有Containerizer的抽象父类。除了默认的构造函数和一个什么都没做的析构函数,其只实现了createresources方法。create方法代码如下(v0.26版本):

Try<Containerizer*> Containerizer::create(
    const Flags& flags,
    bool local,
    Fetcher* fetcher)
{
  if (flags.isolation == "external") {
    LOG(WARNING) << "The 'external' isolation flag is deprecated, "
                 << "please update your flags to"
                 << " '--containerizers=external'.";

    Try<ExternalContainerizer*> containerizer =
      ExternalContainerizer::create(flags);
    if (containerizer.isError()) {
      return Error("Could not create ExternalContainerizer: " +
                   containerizer.error());
    }

    return containerizer.get();
  }

  // TODO(benh): We need to store which containerizer or
  // containerizers were being used. See MESOS-1663.

  // Create containerizer(s).
  vector<Containerizer*> containerizers;

  foreach (const string& type, strings::split(flags.containerizers, ",")) {
    if (type == "mesos") {
      Try<MesosContainerizer*> containerizer =
        MesosContainerizer::create(flags, local, fetcher);
      if (containerizer.isError()) {
        return Error("Could not create MesosContainerizer: " +
                     containerizer.error());
      } else {
        containerizers.push_back(containerizer.get());
      }
    } else if (type == "docker") {
      Try<DockerContainerizer*> containerizer =
        DockerContainerizer::create(flags, fetcher);
      if (containerizer.isError()) {
        return Error("Could not create DockerContainerizer: " +
                     containerizer.error());
      } else {
        containerizers.push_back(containerizer.get());
      }
    } else if (type == "external") {
      Try<ExternalContainerizer*> containerizer =
        ExternalContainerizer::create(flags);
      if (containerizer.isError()) {
        return Error("Could not create ExternalContainerizer: " +
                     containerizer.error());
      } else {
        containerizers.push_back(containerizer.get());
      }
    } else {
      return Error("Unknown or unsupported containerizer: " + type);
    }
  }

  if (containerizers.size() == 1) {
    return containerizers.front();
  }

  Try<ComposingContainerizer*> containerizer =
    ComposingContainerizer::create(containerizers);

  if (containerizer.isError()) {
    return Error(containerizer.error());
  }

  return containerizer.get();
}

默认情况下,containerizerstypemesos,所以会调用MesosContainerizer::create来生成containerizer。关于resources方法,参考Mesos笔记 (5)—— 资源

 

使用LXC初体验

我使用的OSCentOS 7.1,需要安装lxclxc-templates。安装后的模板在/usr/share/lxc/templates目录下:

# ls
lxc-alpine    lxc-archlinux  lxc-centos  lxc-debian    lxc-fedora  lxc-openmandriva  lxc-oracle  lxc-sshd    lxc-ubuntu-cloud
lxc-altlinux  lxc-busybox    lxc-cirros  lxc-download  lxc-gentoo  lxc-opensuse      lxc-plamo   lxc-ubuntu

接下来以CentOS为模板创建一个container

lxc-create -t centos --name cn-centos

临时的root密码存在/var/lib/lxc/cn-01/tmp_root_pass

# cat /var/lib/lxc/cn-centos/tmp_root_pass
Root-cn-centos-EXb6bB

启动container

# lxc-start -n cn-centos

停止container

# lxc-stop -n cn-centos

参考资料:
Setup Linux Containers Using LXC On Ubuntu 15.04

 

Mesos笔记 (8)—— message格式

MesosFrameworkMasterSlave之间使用http协议传递消息。参考下图:

Capture

由于Transfer-Encodingchunked格式,所以在http message body开始部分会包含data长度:0x44,也就是68个字节。另外,data的编码使用protobuf协议,也是key-value格式,具体编码请参考这里

参考资料:
《HTTP:The Definitive Guide》笔记(2)—— messages
HTTP – What does ‘Transfer-Encoding : Chunked’ mean?

LXC,cgroups和namespace简介

LXC is a userspace interface for the Linux kernel containment features. Through a powerful API and simple tools, it lets Linux users easily create and manage system or application containers.

The linux containers, lxc, aims to use these new functionalities to provide a userspace container object which provides full resource isolation and resource control for an application or a system.

Linux container技术的目标是为应用程序或系统提供完整的资源隔离和控制。LXC项目通过提供一组API接口和工具,可以让其他程序方便地使用Linux container技术。

The container technology is actively being pushed into the mainstream linux kernel. It provides the resource management through the control groups aka process containers and resource isolation through the namespaces.

Linux container技术cgroups(control groups)namespaces实现。两者的功能如下:

cgroups = limits how much you can use;
namespaces = limits what you can see (and therefore use)

Cgroups限制了你能够拥有的资源,而namespces限制了你能够看到的资源。

参考资料:
LXC
Anatomy of a Container: Namespaces, cgroups & Some Filesystem Magic

 

C++要点笔记(22)——多态(Polymorphism)

下面是对Polymorphism的解释:

Polymorphism means “many forms”. It is where one type of thing comes in many forms. So you can treat it according to its type, but it will behave differently according to which form of that type it happens to be.

Polymorphism含义为一个类型有多种形式:这些形式虽属于同一类型,但是具体的行为不同。

Polymorphism stems from inheritance. The whole idea is that you have a general base class and more specific derived classes. You can then write code that works with the base class… and polymorphims makes your code not only work with the base class, but all derived classes.

Polymorphism源于继承,它可以让代码对基类和衍生类都能工作。

One of the features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism takes advantage of this feature.

Polymorphism利用了衍生类的指针与基类指针兼容的特性。

A class that declares or inherits a virtual function is called a polymorphic class.

Polymorphism class是定义或继承了virtual functionclass

Pure virtual function定义:

virtual void f() = 0;

包含pure virtual function定义的类是abstract class

参考资料:
I don’t understand Polymorphism at all?
C++ polymorphism and abstract base class

 

C++要点笔记(21)——引用(reference)

Reference可以看做是一个变量的别名:

int x;
int& foo = x;

Reference的一个重要用途是用在函数传参中:

int workWithClass( const MyClass& a_class_object )
{
}

在上述函数中,因为参数是引用,所以省去了copy参数的工作;并且由于有const修饰,所以不会对参数所指向的对象进行修改。

参考资料:
C++ References

 

Haskell笔记 (9)—— typeclass

Typeclass顾名思义:typeclass,类型的分类,同Java中的interface类似。参看下例:

> :t (==)
(==) :: Eq a => a -> a -> Bool

=>之前的Eq a表示:atypeEqtypeclassaEq的一个实例。

参考资料:
Typeclasses 101
Why sum x y is of type (Num a) => a -> a -> a in Haskell?
Explain Type Classes in Haskell