Apollo 10.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
22namespace apollo {
23namespace cyber {
24namespace service_discovery {
25
27 allowed_role_ |= 1 << RoleType::ROLE_NODE;
28 change_type_ = ChangeType::CHANGE_NODE;
29 channel_name_ = "node_change_broadcast";
30}
31
33
34bool NodeManager::HasNode(const std::string& node_name) {
35 uint64_t key = common::GlobalData::RegisterNode(node_name);
36 return nodes_.Search(key);
37}
38
40 RETURN_IF_NULL(nodes);
41 nodes_.GetAllRoles(nodes);
42}
43
44bool NodeManager::Check(const RoleAttributes& attr) {
45 RETURN_VAL_IF(!attr.has_node_name(), false);
46 RETURN_VAL_IF(!attr.has_node_id(), false);
47 return true;
48}
49
50void NodeManager::Dispose(const ChangeMsg& msg) {
51 if (msg.operate_type() == OperateType::OPT_JOIN) {
52 DisposeJoin(msg);
53 } else {
54 DisposeLeave(msg);
55 }
56 Notify(msg);
57}
58
59void NodeManager::OnTopoModuleLeave(const std::string& host_name,
60 int process_id) {
62
63 RoleAttributes attr;
64 attr.set_host_name(host_name);
65 attr.set_process_id(process_id);
66 std::vector<RolePtr> nodes_to_remove;
67 nodes_.Search(attr, &nodes_to_remove);
68 for (auto& node : nodes_to_remove) {
69 nodes_.Remove(node->attributes().node_id());
70 }
71
72 ChangeMsg msg;
73 for (auto& node : nodes_to_remove) {
74 Convert(node->attributes(), RoleType::ROLE_NODE, OperateType::OPT_LEAVE,
75 &msg);
76 Notify(msg);
77 }
78}
79
80void NodeManager::DisposeJoin(const ChangeMsg& msg) {
81 auto node = std::make_shared<RoleNode>(msg.role_attr(), msg.timestamp());
82 uint64_t key = node->attributes().node_id();
83 // duplicated node
84 if (!nodes_.Add(key, node, false)) {
85 RolePtr existing_node;
86 if (!nodes_.Search(key, &existing_node)) {
87 nodes_.Add(key, node);
88 return;
89 }
90
91 RolePtr newer_node = existing_node;
92 if (node->IsEarlierThan(*newer_node)) {
93 nodes_.Add(key, node);
94 } else {
95 newer_node = node;
96 }
97
98 if (newer_node->attributes().process_id() == process_id_ &&
99 newer_node->attributes().host_name() == host_name_) {
100 AERROR << "this process will be terminated due to duplicated node["
101 << node->attributes().node_name()
102 << "], please ensure that each node has a unique name.";
104 }
105 }
106}
107
108void NodeManager::DisposeLeave(const ChangeMsg& msg) {
109 auto node = std::make_shared<RoleNode>(msg.role_attr(), msg.timestamp());
110 nodes_.Remove(node->attributes().node_id());
111}
112
113} // namespace service_discovery
114} // namespace cyber
115} // namespace apollo
static uint64_t RegisterNode(const std::string &node_name)
void Notify(const ChangeMsg &msg)
Definition manager.cc:153
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:137
std::atomic< bool > is_discovery_started_
Definition manager.h:156
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