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
// Create an offer for each slave and add it to the message.
ResourceOffersMessage message;
Framework* framework = CHECK_NOTNULL(frameworks.registered[frameworkId]);
foreachpair (const SlaveID& slaveId, const Resources& offered, resources) {
……
Slave* slave = slaves.registered.get(slaveId);
CHECK_NOTNULL(slave);
……
// TODO(bmahler): Set "https" if only "https" is supported.
mesos::URL url;
url.set_scheme("http");
url.mutable_address()->set_hostname(slave->info.hostname());
url.mutable_address()->set_ip(stringify(slave->pid.address.ip));
url.mutable_address()->set_port(slave->pid.address.port);
url.set_path("/" + slave->pid.id);
Offer* offer = new Offer();
offer->mutable_id()->MergeFrom(newOfferId());
offer->mutable_framework_id()->MergeFrom(framework->id());
offer->mutable_slave_id()->MergeFrom(slave->id);
offer->set_hostname(slave->info.hostname());
offer->mutable_url()->MergeFrom(url);
offer->mutable_resources()->MergeFrom(offered);
offer->mutable_attributes()->MergeFrom(slave->info.attributes());
// Add all framework's executors running on this slave.
if (slave->executors.contains(framework->id())) {
const hashmap<ExecutorID, ExecutorInfo>& executors =
slave->executors[framework->id()];
foreachkey (const ExecutorID& executorId, executors) {
offer->add_executor_ids()->MergeFrom(executorId);
}
}
……
offers[offer->id()] = offer;
framework->addOffer(offer);
slave->addOffer(offer);
if (flags.offer_timeout.isSome()) {
// Rescind the offer after the timeout elapses.
offerTimers[offer->id()] =
delay(flags.offer_timeout.get(),
self(),
&Self::offerTimeout,
offer->id());
}
// TODO(jieyu): For now, we strip 'ephemeral_ports' resource from
// offers so that frameworks do not see this resource. This is a
// short term workaround. Revisit this once we resolve MESOS-1654.
Offer offer_ = *offer;
offer_.clear_resources();
foreach (const Resource& resource, offered) {
if (resource.name() != "ephemeral_ports") {
offer_.add_resources()->CopyFrom(resource);
}
}
// Add the offer *AND* the corresponding slave's PID.
message.add_offers()->MergeFrom(offer_);
message.add_pids(slave->pid);
}
if (message.offers().size() == 0) {
return;
}
LOG(INFO) << "Sending " << message.offers().size()
<< " offers to framework " << *framework;
framework->send(message);
}
|
3. 监听消息,注册处理函数
最后注册一些处理函数,当收到相应的消息的时候,调用相应的函数。
下面仅仅列举比较重要的几个函数。
第一个是Framework注册的时候调用的函数。
第二个是收到Framework发送来的运行Task的消息。
第三个是Slave发送来的注册时候调用的函数。
第四个是更新Status时候的Message
4. 竞争成为Master中的Leader,或者检测当前的Leader