File: consistenthash_config.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 (260 lines) | stat: -rw-r--r-- 9,891 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
/*
 * 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 "time.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"

#include "consistenthash_config.h"

namespace
{
std::mutex strategies_cache_mutex;
std::map<std::string, strategies_map> strategies_cache;
} // namespace

void
clearStrategiesCache(void)
{
  std::lock_guard<std::mutex> guard(strategies_cache_mutex);
  strategies_cache.clear();
}

void loadConfigFile(const std::string &fileName, std::stringstream &doc, std::unordered_set<std::string> &include_once);

// createStrategy creates and initializes a Consistent Hash strategy from the given YAML node.
// Caller takes ownership of the returned pointer, and must call delete on it.
TSNextHopSelectionStrategy *
createStrategy(const std::string &name, const YAML::Node &node)
{
  TSDebug(PLUGIN_NAME, "createStrategy %s calling.", name.c_str());
  try {
    PLNextHopConsistentHash *st = new PLNextHopConsistentHash(name, node);
    TSDebug(PLUGIN_NAME, "createStrategy %s succeeded, returning object", name.c_str());
    return st;
  } catch (std::exception &ex) {
    TSError("[%s] creating strategies '%s' threw '%s', returning nullptr", PLUGIN_NAME, name.c_str(), ex.what());
    return nullptr;
  }
}

// Caller takes ownership of the returned pointers in the map, and must call delete on them.
// TODO change to only call createStrategy for the one we need, for efficiency.
strategies_map
createStrategiesFromFile(const char *file)
{
  TSDebug(PLUGIN_NAME, "createStrategiesFromFile plugin createStrategiesFromFile file '%s'", file);

  {
    std::lock_guard<std::mutex> guard(strategies_cache_mutex);
    auto it = strategies_cache.find(file);
    if (it != strategies_cache.end()) {
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile file '%s' in cache from previous remap, using cache", file);
      return it->second;
    }
  }
  TSDebug(PLUGIN_NAME, "createStrategiesFromFile file '%s' not in cache, loading file", file);

  YAML::Node config;
  YAML::Node strategies;
  std::stringstream doc;
  std::unordered_set<std::string> include_once;
  std::string_view fn = file;

  // strategy policy
  constexpr std::string_view consistent_hash = "consistent_hash";

  const char *basename = fn.substr(fn.find_last_of('/') + 1).data();

  try {
    TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s loading ...", basename);
    loadConfigFile(fn.data(), doc, include_once);
    TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s loaded.", basename);

    config = YAML::Load(doc);

    TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s yaml loaded.", basename);

    if (config.IsNull()) {
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile No NextHop strategy configs were loaded.");
      return strategies_map();
    }

    TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s checked null.", basename);

    strategies = config["strategies"];
    if (strategies.Type() != YAML::NodeType::Sequence) {
      TSError("[%s] malformed %s file, expected a 'strategies' sequence", PLUGIN_NAME, basename);
      return strategies_map();
    }

    TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s checked strategies member.", basename);

    // std::map<std::string, TSNextHopSelectionStrategy*, std::less<>>
    strategies_map strategiesMap;
    for (auto &&strategy : strategies) {
      auto name = strategy["strategy"].as<std::string>();
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s got strategy %s.", basename, name.c_str());
      auto policy = strategy["policy"];
      if (!policy) {
        TSError("[%s] no policy is defined for the strategy named '%s'.", PLUGIN_NAME, name.c_str());
        return strategies_map();
      }
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s got strategy %s checked policy.", basename, name.c_str());
      const auto &policy_value = policy.Scalar();
      if (policy_value != consistent_hash) {
        TSError("[%s] strategy named '%s' has unsupported policy '%s'.", PLUGIN_NAME, name.c_str(), policy_value.c_str());
        return strategies_map();
      }
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s got strategy %s creating strategy.", basename, name.c_str());
      TSNextHopSelectionStrategy *tsStrategy = createStrategy(name, strategy);
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s got strategy %s created strategy.", basename, name.c_str());
      if (tsStrategy == nullptr) {
        return strategies_map();
      }
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s got strategy %s checked strategy null.", basename, name.c_str());
      strategiesMap.emplace(name, std::unique_ptr<TSNextHopSelectionStrategy>(tsStrategy));
      TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s got strategy %s emplaced.", basename, name.c_str());
    }
    TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s returning strategies created.", basename);

    {
      std::lock_guard<std::mutex> guard(strategies_cache_mutex);
      strategies_cache[file] = strategiesMap;
    }

    return strategiesMap;
  } catch (std::exception &ex) {
    TSError("[%s] creating strategies from file %s threw '%s'.", PLUGIN_NAME, file, ex.what());
  }
  TSDebug(PLUGIN_NAME, "createStrategiesFromFile filename %s returning error.", basename);
  return strategies_map();
}

/*
 * loads the contents of a file into a std::stringstream document.  If the file has a '#include file'
 * directive, that 'file' is read into the document beginning at the point where the
 * '#include' was found. This allows the 'strategy' and 'hosts' yaml files to be separate.  The
 * 'strategy' yaml file would then normally have the '#include hosts.yml' in it's beginning.
 */
void
loadConfigFile(const std::string &fileName, std::stringstream &doc, std::unordered_set<std::string> &include_once)
{
  const char *sep = " \t";
  char *tok, *last;
  struct stat buf;
  std::string line;

  if (stat(fileName.c_str(), &buf) == -1) {
    std::string err_msg = strerror(errno);
    throw std::invalid_argument("Unable to stat '" + fileName + "': " + err_msg);
  }

  // if fileName is a directory, concatenate all '.yaml' files alphanumerically
  // into a single document stream.  No #include is supported.
  if (S_ISDIR(buf.st_mode)) {
    DIR *dir               = nullptr;
    struct dirent *dir_ent = nullptr;
    std::vector<std::string_view> files;

    TSDebug(PLUGIN_NAME, "loading strategy YAML files from the directory %s", fileName.c_str());
    if ((dir = opendir(fileName.c_str())) == nullptr) {
      std::string err_msg = strerror(errno);
      throw std::invalid_argument("Unable to open the directory '" + fileName + "': " + err_msg);
    } else {
      while ((dir_ent = readdir(dir)) != nullptr) {
        // filename should be greater that 6 characters to have a '.yaml' suffix.
        if (strlen(dir_ent->d_name) < 6) {
          continue;
        }
        std::string_view sv = dir_ent->d_name;
        if (sv.find(".yaml", sv.size() - 5) == sv.size() - 5) {
          files.push_back(sv);
        }
      }
      // sort the files alphanumerically
      std::sort(files.begin(), files.end(),
                [](const std::string_view lhs, const std::string_view rhs) { return lhs.compare(rhs) < 0; });

      for (auto &f : files) {
        std::ifstream file(fileName + "/" + f.data());
        if (file.is_open()) {
          while (std::getline(file, line)) {
            if (line[0] == '#') {
              // continue;
            }
            doc << line << "\n";
          }
          file.close();
        } else {
          throw std::invalid_argument("Unable to open and read '" + fileName + "/" + f.data() + "'");
        }
      }
    }
    closedir(dir);
  } else {
    std::ifstream file(fileName);
    if (file.is_open()) {
      while (std::getline(file, line)) {
        if (line[0] == '#') {
          tok = strtok_r(const_cast<char *>(line.c_str()), sep, &last);
          if (tok != nullptr && strcmp(tok, "#include") == 0) {
            std::string f = strtok_r(nullptr, sep, &last);
            if (include_once.find(f) == include_once.end()) {
              include_once.insert(f);
              // try to load included file.
              try {
                loadConfigFile(f, doc, include_once);
              } catch (std::exception &ex) {
                throw std::invalid_argument("Unable to open included file '" + f + "' from '" + fileName + "'");
              }
            }
          }
        } else {
          doc << line << "\n";
        }
      }
      file.close();
    } else {
      throw std::invalid_argument("Unable to open and read '" + fileName + "'");
    }
  }
}