Apollo 11.0
自动驾驶开放平台
node_manager.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
19#include "cyber/common/log.h"
20#include "cyber/state.h"
21#include "cyber/time/time.h"
22
23namespace apollo {
24namespace cyber {
25namespace service_discovery {
26
28 allowed_role_ |= 1 << RoleType::ROLE_NODE;
29 change_type_ = ChangeType::CHANGE_NODE;
30 channel_name_ = "node_change_broadcast";
31}
32
34
35bool NodeManager::HasNode(const std::string& node_name) {
36 uint64_t key = common::GlobalData::RegisterNode(node_name);
37 return nodes_.Search(key);
38}
39
41 RETURN_IF_NULL(nodes);
42 nodes_.GetAllRoles(nodes);
43}
44
45bool NodeManager::Check(const RoleAttributes& attr) {
46 RETURN_VAL_IF(!attr.has_node_name(), false);
47 RETURN_VAL_IF(!attr.has_node_id(), false);
48 return true;
49}
50
51void NodeManager::Dispose(const ChangeMsg& msg) {
52 if (msg.operate_type() == OperateType::OPT_JOIN) {
53 DisposeJoin(msg);
54 } else {
55 DisposeLeave(msg);
56 }
57 Notify(msg);
58}
59
60void NodeManager::OnTopoModuleLeave(const std::string& host_name,
61 int process_id) {
63
64 RoleAttributes attr;
65 attr.set_host_name(host_name);
66 attr.set_process_id(process_id);
67 std::vector<RolePtr> nodes_to_remove;
68 nodes_.Search(attr, &nodes_to_remove);
69 for (auto& node : nodes_to_remove) {
70 nodes_.Remove(node->attributes().node_id());
71 }
72
73 ChangeMsg msg;
74 for (auto& node : nodes_to_remove) {
75 Convert(node->attributes(), RoleType::ROLE_NODE, OperateType::OPT_LEAVE,
76 &msg);
77 Notify(msg);
78 }
79}
80
81void NodeManager::DisposeJoin(const ChangeMsg& msg) {
82 auto node = std::make_shared<RoleNode>(msg.role_attr(), msg.timestamp());
83 uint64_t key = node->attributes().node_id();
84 // duplicated node
85 if (!nodes_.Add(key, node, false)) {
86 RolePtr existing_node;
87 if (!nodes_.Search(key, &existing_node)) {
88 nodes_.Add(key, node);
89 return;
90 }
91
92 RolePtr newer_node = existing_node;
93 if (node->IsEarlierThan(*newer_node)) {
94 nodes_.Add(key, node);
95 } else {
96 newer_node = node;
97 }
98
99 if (newer_node->attributes().process_id() == process_id_ &&
100 newer_node->attributes().host_name() == host_name_) {
101 AERROR << "this process will be terminated due to duplicated node["
102 << node->attributes().node_name()
103 << "], please ensure that each node has a unique name.";
105 }
106 }
107}
108
109void NodeManager::DisposeLeave(const ChangeMsg& msg) {
110 auto node = std::make_shared<RoleNode>(msg.role_attr(), msg.timestamp());
111 nodes_.Remove(node->attributes().node_id());
112}
113
114} // namespace service_discovery
115} // namespace cyber
116} // namespace apollo
static uint64_t RegisterNode(const std::string &node_name)
void Notify(const ChangeMsg &msg)
Definition manager.cc:186
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:170
std::atomic< bool > is_discovery_started_
Definition manager.h:155
virtual ~NodeManager()
Destroy the Node Manager object
void GetNodes(RoleAttrVec *nodes)
Get the Nodes object
std::vector< RoleAttributes > RoleAttrVec
bool HasNode(const std::string &node_name)
Checkout whether we have node_name in topology
NodeManager()
Construct a new Node Manager object
void GetAllRoles(std::vector< RolePtr > *roles) override
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define AERROR
Definition log.h:44
#define RETURN_IF(condition)
Definition log.h:106
std::shared_ptr< RoleBase > RolePtr
Definition role.h:31
void AsyncShutdown()
Definition state.h:56
class register implement
Definition arena_queue.h:37