File: strategy.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (393 lines) | stat: -rw-r--r-- 14,112 bytes parent folder | download | duplicates (2)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * 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 "strategy.h"
#include "consistenthash.h"
#include "util.h"

#include <cinttypes>
#include <string>
#include <algorithm>
#include <mutex>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <cstring>

#include <sys/stat.h>
#include <dirent.h>

#include <yaml-cpp/yaml.h>

#include "tscore/HashSip.h"
#include "tscore/ConsistentHash.h"
#include "tscore/ink_assert.h"
#include "ts/ts.h"
#include "ts/remap.h"
#include "ts/parentselectdefs.h"

//
// NextHopSelectionStrategy.cc
//

// ring mode strings
const std::string_view alternate_rings = "alternate_ring";
const std::string_view exhaust_rings   = "exhaust_ring";
const std::string_view peering_rings   = "peering_ring";

// health check strings
const std::string_view active_health_check  = "active";
const std::string_view passive_health_check = "passive";

PLNextHopSelectionStrategy::PLNextHopSelectionStrategy(const std::string_view &name, const YAML::Node &n) : strategy_name(name)
{
  PL_NH_Debug(PL_NH_DEBUG_TAG, "PLNextHopSelectionStrategy constructor calling");
  std::string self_host;
  bool self_host_used = false;

  try {
    // scheme is optional, and strategies with no scheme will match hosts with no scheme
    if (n["scheme"]) {
      auto scheme_val = n["scheme"].Scalar();
      if (scheme_val == "http") {
        scheme = PL_NH_SCHEME_HTTP;
      } else if (scheme_val == "https") {
        scheme = PL_NH_SCHEME_HTTPS;
      } else {
        PL_NH_Note("Invalid scheme '%s' for strategy '%s', setting to NONE", scheme_val.c_str(), strategy_name.c_str());
      }
    }

    // go_direct config.
    if (n["go_direct"]) {
      go_direct = n["go_direct"].as<bool>();
    }

    // parent_is_proxy config.
    if (n["parent_is_proxy"]) {
      parent_is_proxy = n["parent_is_proxy"].as<bool>();
    }

    // ignore_self_detect
    if (n["ignore_self_detect"]) {
      ignore_self_detect = n["ignore_self_detect"].as<bool>();
    }

    if (n["cache_peer_result"]) {
      cache_peer_result = n["cache_peer_result"].as<bool>();
    }

    // failover node.
    YAML::Node failover_node;
    if (n["failover"]) {
      failover_node = n["failover"];
      if (failover_node["ring_mode"]) {
        auto ring_mode_val = failover_node["ring_mode"].Scalar();
        if (ring_mode_val == alternate_rings) {
          ring_mode = PL_NH_ALTERNATE_RING;
        } else if (ring_mode_val == exhaust_rings) {
          ring_mode = PL_NH_EXHAUST_RING;
        } else if (ring_mode_val == peering_rings) {
          ring_mode            = PL_NH_PEERING_RING;
          YAML::Node self_node = failover_node["self"];
          if (self_node) {
            self_host = self_node.Scalar();
            PL_NH_Debug(PL_NH_DEBUG_TAG, "%s is self", self_host.c_str());
          }
        } else {
          ring_mode = PL_NH_ALTERNATE_RING;
          PL_NH_Note("Invalid 'ring_mode' value, '%s', for the strategy named '%s', using default '%s'.", ring_mode_val.c_str(),
                     strategy_name.c_str(), alternate_rings.data());
        }
      }
      if (failover_node["max_simple_retries"]) {
        max_simple_retries = failover_node["max_simple_retries"].as<int>();
      }

      if (failover_node["max_unavailable_retries"]) {
        max_unavailable_retries = failover_node["max_unavailable_retries"].as<int>();
      }

      YAML::Node resp_codes_node;
      // connection failures are always failure and retryable (pending retries)
      resp_codes.add(STATUS_CONNECTION_FAILURE);
      if (failover_node["response_codes"]) {
        resp_codes_node = failover_node["response_codes"];
        if (resp_codes_node.Type() != YAML::NodeType::Sequence) {
          PL_NH_Error("Error in the response_codes definition for the strategy named '%s', skipping response_codes.",
                      strategy_name.c_str());
        } else {
          for (auto &&k : resp_codes_node) {
            auto code = k.as<int>();
            if (code > 300 && code < 599) {
              resp_codes.add(code);
            } else {
              PL_NH_Note("Skipping invalid response code '%d' for the strategy named '%s'.", code, strategy_name.c_str());
            }
          }
          resp_codes.sort();
        }
      }
      YAML::Node markdown_codes_node;
      if (failover_node["markdown_codes"]) {
        markdown_codes_node = failover_node["markdown_codes"];
        if (markdown_codes_node.Type() != YAML::NodeType::Sequence) {
          PL_NH_Error("Error in the markdown_codes definition for the strategy named '%s', skipping markdown_codes.",
                      strategy_name.c_str());
        } else {
          for (auto &&k : markdown_codes_node) {
            auto code = k.as<int>();
            if (code > 300 && code < 599) {
              markdown_codes.add(code);
            } else {
              PL_NH_Note("Skipping invalid markdown response code '%d' for the strategy named '%s'.", code, strategy_name.c_str());
            }
          }
          markdown_codes.sort();
        }
      }
      YAML::Node health_check_node;
      if (failover_node["health_check"]) {
        health_check_node = failover_node["health_check"];
        if (health_check_node.Type() != YAML::NodeType::Sequence) {
          PL_NH_Error("Error in the health_check definition for the strategy named '%s', skipping health_checks.",
                      strategy_name.c_str());
        } else {
          for (auto it = health_check_node.begin(); it != health_check_node.end(); ++it) {
            auto health_check = it->as<std::string>();
            if (health_check.compare(active_health_check) == 0) {
              health_checks.active = true;
            }
            if (health_check.compare(passive_health_check) == 0) {
              health_checks.passive = true;
            }
          }
        }
      }
    }

    // parse and load the host data
    YAML::Node groups_node;
    if (n["groups"]) {
      groups_node = n["groups"];
      // a groups list is required.
      if (groups_node.Type() != YAML::NodeType::Sequence) {
        throw std::invalid_argument("Invalid groups definition, expected a sequence, '" + strategy_name + "' cannot be loaded.");
      } else {
        uint32_t grp_size = groups_node.size();
        if (grp_size > PL_NH_MAX_GROUP_RINGS) {
          PL_NH_Note(
            "the groups list exceeds the maximum of %d for the strategy '%s'. Only the first %d groups will be configured.",
            PL_NH_MAX_GROUP_RINGS, strategy_name.c_str(), PL_NH_MAX_GROUP_RINGS);
          groups = PL_NH_MAX_GROUP_RINGS;
        } else {
          groups = groups_node.size();
        }
        // resize the hosts vector.
        host_groups.reserve(groups);
        // loop through the groups
        for (unsigned int grp = 0; grp < groups; ++grp) {
          YAML::Node hosts_list = groups_node[grp];

          // a list of hosts is required.
          if (hosts_list.Type() != YAML::NodeType::Sequence) {
            throw std::invalid_argument("Invalid hosts definition, expected a sequence, '" + strategy_name + "' cannot be loaded.");
          } else {
            // loop through the hosts list.
            std::vector<std::shared_ptr<PLHostRecord>> hosts_inner;

            for (unsigned int hst = 0; hst < hosts_list.size(); ++hst) {
              std::shared_ptr<PLHostRecord> host_rec = std::make_shared<PLHostRecord>(hosts_list[hst].as<PLHostRecord>());
              host_rec->group_index                  = grp;
              host_rec->host_index                   = hst;
              if (self_host == host_rec->hostname ||
                  TSHostnameIsSelf(host_rec->hostname.c_str(), host_rec->hostname.size()) == TS_SUCCESS) {
                if (ring_mode == PL_NH_PEERING_RING && grp != 0) {
                  throw std::invalid_argument("self host (" + self_host +
                                              ") can only appear in first host group for peering ring mode");
                }
                TSHostStatusSet(host_rec->hostname.c_str(), host_rec->hostname.size(), TSHostStatus::TS_HOST_STATUS_DOWN, 0,
                                static_cast<unsigned int>(TS_HOST_STATUS_SELF_DETECT));
                host_rec->self = true;
                self_host_used = true;
              }
              hosts_inner.push_back(std::move(host_rec));
              num_parents++;
            }
            passive_health.insert(hosts_inner);
            host_groups.push_back(std::move(hosts_inner));
          }
        }
      }
    }
    if (!self_host.empty() && !self_host_used) {
      throw std::invalid_argument("self host (" + self_host + ") does not appear in the first (peer) group");
    }
  } catch (std::exception &ex) {
    throw std::invalid_argument("Error parsing strategy named '" + strategy_name + "' due to '" + ex.what() +
                                "', this strategy will be ignored.");
  }
}

bool
PLNextHopSelectionStrategy::nextHopExists(TSHttpTxn txnp)
{
  PL_NH_Debug(PL_NH_DEBUG_TAG, "nhplugin nextHopExists calling");

  const int64_t sm_id = TSHttpTxnIdGet(txnp);

  for (uint32_t gg = 0; gg < groups; gg++) {
    for (auto &hh : host_groups[gg]) {
      PLHostRecord *p = hh.get();
      if (p->available) {
        PL_NH_Debug(PL_NH_DEBUG_TAG,
                    "[%" PRIu64 "] found available next hop %.*s (this is NOT necessarily the parent which will be selected, just "
                    "the first available parent found)",
                    sm_id, int(p->hostname.size()), p->hostname.c_str());
        return true;
      }
    }
  }
  return false;
}

bool
PLNextHopSelectionStrategy::codeIsFailure(TSHttpStatus response_code)
{
  return this->resp_codes.contains(response_code) || this->markdown_codes.contains(response_code);
}

bool
PLNextHopSelectionStrategy::responseIsRetryable(unsigned int current_retry_attempts, TSHttpStatus response_code)
{
  return (current_retry_attempts < this->num_parents) &&
         ((this->resp_codes.contains(response_code) && current_retry_attempts < this->max_simple_retries) ||
          (this->markdown_codes.contains(response_code) && current_retry_attempts < this->max_unavailable_retries));
}

bool
PLNextHopSelectionStrategy::onFailureMarkParentDown(TSHttpStatus response_code)
{
  return this->markdown_codes.contains(response_code);
}

bool
PLNextHopSelectionStrategy::goDirect()
{
  PL_NH_Debug(PL_NH_DEBUG_TAG, "nhplugin goDirect calling");
  return this->go_direct;
}

bool
PLNextHopSelectionStrategy::parentIsProxy()
{
  PL_NH_Debug(PL_NH_DEBUG_TAG, "nhplugin parentIsProxy calling");
  return this->parent_is_proxy;
}

namespace YAML
{
template <> struct convert<PLHostRecord> {
  static bool
  decode(const Node &node, PLHostRecord &nh)
  {
    YAML::Node nd;
    bool merge_tag_used = false;

    // check for YAML merge tag.
    if (node["<<"]) {
      nd             = node["<<"];
      merge_tag_used = true;
    } else {
      nd = node;
    }

    // lookup the hostname
    if (nd["host"]) {
      nh.hostname = nd["host"].Scalar();
    } else {
      throw std::invalid_argument("Invalid host definition, missing host name.");
    }

    // lookup the port numbers supported by this host.
    YAML::Node proto = nd["protocol"];

    if (proto.Type() != YAML::NodeType::Sequence) {
      throw std::invalid_argument("Invalid host protocol definition, expected a sequence.");
    } else {
      for (auto &&ii : proto) {
        const YAML::Node &protocol_node  = ii;
        std::shared_ptr<PLNHProtocol> pr = std::make_shared<PLNHProtocol>(protocol_node.as<PLNHProtocol>());
        nh.protocols.push_back(std::move(pr));
      }
    }

    // get the host's weight
    YAML::Node weight;
    if (merge_tag_used) {
      weight    = node["weight"];
      nh.weight = weight.as<float>();
    } else if ((weight = nd["weight"])) {
      nh.weight = weight.as<float>();
    } else {
      PL_NH_Note("No weight is defined for the host '%s', using default 1.0", nh.hostname.data());
      nh.weight = 1.0;
    }

    // get the host's optional hash_string
    YAML::Node hash;
    if ((hash = nd["hash_string"])) {
      nh.hash_string = hash.Scalar();
    }

    return true;
  }
};

template <> struct convert<PLNHProtocol> {
  static bool
  decode(const Node &node, PLNHProtocol &nh)
  {
    // scheme is optional, and strategies with no scheme will match hosts with no scheme
    if (node["scheme"]) {
      const auto scheme_val = node["scheme"].Scalar();
      if (scheme_val == "http") {
        nh.scheme = PL_NH_SCHEME_HTTP;
      } else if (scheme_val == "https") {
        nh.scheme = PL_NH_SCHEME_HTTPS;
      } else {
        PL_NH_Note("Invalid scheme '%s' for protocol, setting to NONE", scheme_val.c_str());
      }
    }
    if (node["port"]) {
      nh.port = node["port"].as<int>();
      if (nh.port <= 0 || nh.port > 65535) {
        throw YAML::ParserException(node["port"].Mark(), "port number must be in (inclusive) range 1 - 65,536");
      }
    } else {
      throw YAML::ParserException(node["port"].Mark(),
                                  "no port is defined, a port number must be defined within (inclusive) range 1 - 65,536");
    }
    if (node["health_check_url"]) {
      nh.health_check_url = node["health_check_url"].Scalar();
    }
    return true;
  }
};
}; // namespace YAML