Mesos源码分析(6): Mesos Master的初始化

 

Mesos Master的初始化在src/master/master.cpp中

 

 

在Mesos Master的log中,是能看到这一行的。

 

1.初始化role,并设置weight权重

 

 

 

2. 初始化Allocator

 

 

注意,Allocator的initialize函数中,传入的OfferCallback是Master::offer。

 

如果前面所述,没过allocation_interval,Allocator都会计算每个framework的offer,然后依次调用Master::offer,将资源offer给相应的framework。

 

 

在Master::offer函数中,生成如下的ResourceOffersMessage,并且发送给Framework

 

  1.   // Create an offer for each slave and add it to the message.
  2.   ResourceOffersMessage message;
  3.  
  4.   Framework* framework = CHECK_NOTNULL(frameworks.registered[frameworkId]);
  5.   foreachpair (const SlaveID& slaveId, const Resources& offered, resources) {
  6. ……
  7.     Slave* slave = slaves.registered.get(slaveId);
  8.     CHECK_NOTNULL(slave);
  9. ……
  10.     // TODO(bmahler): Set "https" if only "https" is supported.
  11.     mesos::URL url;
  12.     url.set_scheme("http");
  13.     url.mutable_address()->set_hostname(slave->info.hostname());
  14.     url.mutable_address()->set_ip(stringify(slave->pid.address.ip));
  15.     url.mutable_address()->set_port(slave->pid.address.port);
  16.     url.set_path("/" + slave->pid.id);
  17.  
  18.     Offer* offer = new Offer();
  19.     offer->mutable_id()->MergeFrom(newOfferId());
  20.     offer->mutable_framework_id()->MergeFrom(framework->id());
  21.     offer->mutable_slave_id()->MergeFrom(slave->id);
  22.     offer->set_hostname(slave->info.hostname());
  23.     offer->mutable_url()->MergeFrom(url);
  24.     offer->mutable_resources()->MergeFrom(offered);
  25.     offer->mutable_attributes()->MergeFrom(slave->info.attributes());
  26.  
  27.     // Add all framework's executors running on this slave.
  28.     if (slave->executors.contains(framework->id())) {
  29.       const hashmap<ExecutorID, ExecutorInfo>& executors =
  30.         slave->executors[framework->id()];
  31.       foreachkey (const ExecutorID& executorId, executors) {
  32.         offer->add_executor_ids()->MergeFrom(executorId);
  33.       }
  34.     }
  35. ……
  36.     offers[offer->id()] = offer;
  37.  
  38.     framework->addOffer(offer);
  39.     slave->addOffer(offer);
  40.  
  41.     if (flags.offer_timeout.isSome()) {
  42.       // Rescind the offer after the timeout elapses.
  43.       offerTimers[offer->id()] =
  44.         delay(flags.offer_timeout.get(),
  45.               self(),
  46.               &Self::offerTimeout,
  47.               offer->id());
  48.     }
  49.  
  50.     // TODO(jieyu): For now, we strip 'ephemeral_ports' resource from
  51.     // offers so that frameworks do not see this resource. This is a
  52.     // short term workaround. Revisit this once we resolve MESOS-1654.
  53.     Offer offer_ = *offer;
  54.     offer_.clear_resources();
  55.  
  56.     foreach (const Resource& resource, offered) {
  57.       if (resource.name() != "ephemeral_ports") {
  58.         offer_.add_resources()->CopyFrom(resource);
  59.       }
  60.     }
  61.  
  62.     // Add the offer *AND* the corresponding slave's PID.
  63.     message.add_offers()->MergeFrom(offer_);
  64.     message.add_pids(slave->pid);
  65.   }
  66.  
  67.   if (message.offers().size() == 0) {
  68.     return;
  69.   }
  70.  
  71.   LOG(INFO) << "Sending " << message.offers().size()
  72.             << " offers to framework " << *framework;
  73.  
  74.   framework->send(message);
  75. }

 

3. 监听消息,注册处理函数

 

最后注册一些处理函数,当收到相应的消息的时候,调用相应的函数。

 

下面仅仅列举比较重要的几个函数。

 

第一个是Framework注册的时候调用的函数。

 

第二个是收到Framework发送来的运行Task的消息。

 

第三个是Slave发送来的注册时候调用的函数。

 

第四个是更新Status时候的Message

 

4. 竞争成为Master中的Leader,或者检测当前的Leader

 

posted @ 2016-07-25 23:10  popsuper1982  阅读(802)  评论(0编辑  收藏  举报