-
Notifications
You must be signed in to change notification settings - Fork 497
/
executor-group.cc
196 lines (177 loc) · 7.31 KB
/
executor-group.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "scheduling/executor-group.h"
namespace impala {
// Hand-testing shows that 25 replicas produces a reasonable balance between nodes
// across the hash ring. See HashRingTest::MaxMinRatio() for some empirical results
// at similar replication levels. There is nothing special about 25 (i.e. 24 or 26
// would be similar). Increasing this results in a more even distribution.
// TODO: This can be tuned further with real world tests
static const uint32_t NUM_HASH_RING_REPLICAS = 25;
ExecutorGroup::ExecutorGroup(string name) : ExecutorGroup(name, 1) {}
ExecutorGroup::ExecutorGroup(string name, int64_t min_size)
: name_(name), min_size_(min_size), executor_ip_hash_ring_(NUM_HASH_RING_REPLICAS) {
DCHECK_GT(min_size_, 0);
}
ExecutorGroup::ExecutorGroup(const ExecutorGroupDescPB& desc)
: ExecutorGroup(desc.name(), desc.min_size()) {}
ExecutorGroup* ExecutorGroup::GetFilteredExecutorGroup(const ExecutorGroup* group,
const std::unordered_set<NetworkAddressPB>& blacklisted_executor_addresses) {
ExecutorGroup* filtered_group = new ExecutorGroup(*group);
for (const NetworkAddressPB& be_address : blacklisted_executor_addresses) {
const BackendDescriptorPB* be_desc = filtered_group->LookUpBackendDesc(be_address);
if (be_desc != nullptr) {
filtered_group->RemoveExecutor(BackendDescriptorPB(*be_desc));
}
}
return filtered_group;
}
const ExecutorGroup::Executors& ExecutorGroup::GetExecutorsForHost(
const IpAddr& ip) const {
ExecutorMap::const_iterator it = executor_map_.find(ip);
DCHECK(it != executor_map_.end());
return it->second;
}
ExecutorGroup::IpAddrs ExecutorGroup::GetAllExecutorIps() const {
IpAddrs ips;
ips.reserve(NumHosts());
for (auto& it: executor_map_) ips.push_back(it.first);
return ips;
}
ExecutorGroup::Executors ExecutorGroup::GetAllExecutorDescriptors() const {
Executors executors;
for (const auto& executor_list: executor_map_) {
executors.insert(executors.end(), executor_list.second.begin(),
executor_list.second.end());
}
return executors;
}
void ExecutorGroup::AddExecutor(const BackendDescriptorPB& be_desc) {
// be_desc.is_executor can be false for the local backend when scheduling queries to run
// on the coordinator host.
DCHECK(!be_desc.ip_address().empty());
Executors& be_descs = executor_map_[be_desc.ip_address()];
auto eq = [&be_desc](const BackendDescriptorPB& existing) {
// The IP addresses must already match, so it is sufficient to check the port.
DCHECK_EQ(existing.ip_address(), be_desc.ip_address());
return existing.address().port() == be_desc.address().port();
};
if (find_if(be_descs.begin(), be_descs.end(), eq) != be_descs.end()) {
LOG(DFATAL) << "Tried to add existing backend to executor group: "
<< be_desc.krpc_address();
return;
}
if (!CheckConsistencyOrWarn(be_desc)) {
LOG(WARNING) << "Ignoring inconsistent backend for executor group: "
<< be_desc.krpc_address();
return;
}
if (be_descs.empty()) {
executor_ip_hash_ring_.AddNode(be_desc.ip_address());
}
be_descs.push_back(be_desc);
executor_ip_map_[be_desc.address().hostname()] = be_desc.ip_address();
if (be_desc.ip_address() == "127.0.0.1") {
// Include localhost as an alias for filesystems that don't translate it.
LOG(INFO) << "Adding executor localhost alias for "
<< be_desc.address().hostname() << " -> " << be_desc.ip_address();
executor_ip_map_["localhost"] = be_desc.ip_address();
}
}
void ExecutorGroup::RemoveExecutor(const BackendDescriptorPB& be_desc) {
auto be_descs_it = executor_map_.find(be_desc.ip_address());
if (be_descs_it == executor_map_.end()) {
LOG(DFATAL) << "Tried to remove a backend from non-existing host: "
<< be_desc.krpc_address();
return;
}
auto eq = [&be_desc](const BackendDescriptorPB& existing) {
// The IP addresses must already match, so it is sufficient to check the port.
DCHECK_EQ(existing.ip_address(), be_desc.ip_address());
return existing.address().port() == be_desc.address().port();
};
Executors& be_descs = be_descs_it->second;
auto remove_it = find_if(be_descs.begin(), be_descs.end(), eq);
if (remove_it == be_descs.end()) {
LOG(DFATAL) << "Tried to remove non-existing backend from per-host list: "
<< be_desc.krpc_address();
return;
}
be_descs.erase(remove_it);
if (be_descs.empty()) {
executor_map_.erase(be_descs_it);
executor_ip_map_.erase(be_desc.address().hostname());
executor_ip_hash_ring_.RemoveNode(be_desc.ip_address());
}
}
bool ExecutorGroup::LookUpExecutorIp(const Hostname& hostname, IpAddr* ip) const {
// Check if hostname is already a valid IP address.
if (executor_map_.find(hostname) != executor_map_.end()) {
if (ip != nullptr) *ip = hostname;
return true;
}
auto it = executor_ip_map_.find(hostname);
if (it != executor_ip_map_.end()) {
if (ip != nullptr) *ip = it->second;
return true;
}
return false;
}
const BackendDescriptorPB* ExecutorGroup::LookUpBackendDesc(
const NetworkAddressPB& host) const {
IpAddr ip;
if (LookUpExecutorIp(host.hostname(), &ip)) {
const ExecutorGroup::Executors& be_list = GetExecutorsForHost(ip);
for (const BackendDescriptorPB& desc : be_list) {
if (desc.address() == host) return &desc;
}
}
return nullptr;
}
int ExecutorGroup::NumExecutors() const {
int count = 0;
for (const auto& executor_list : executor_map_) count += executor_list.second.size();
return count;
}
bool ExecutorGroup::IsHealthy() const {
int num_executors = NumExecutors();
if (num_executors < min_size_) {
LOG(WARNING) << "Executor group " << name_ << " is unhealthy: " << num_executors
<< " out of " << min_size_ << " are available.";
return false;
}
return true;
}
bool ExecutorGroup::CheckConsistencyOrWarn(const BackendDescriptorPB& be_desc) const {
// Check if the executor's group configuration matches this group.
for (const ExecutorGroupDescPB& desc : be_desc.executor_groups()) {
if (desc.name() == name_) {
if (desc.min_size() == min_size_) {
return true;
} else {
LOG(WARNING) << "Backend " << be_desc.DebugString()
<< " is configured for executor group " << desc.DebugString()
<< " but group has minimum size " << min_size_;
return false;
}
}
}
// If the backend does not mention the group we consider it consistent to allow backends
// to be added to unrelated groups, e.g. for the coordinator-only scheuduling.
return true;
}
} // end ns impala