File: parent_select.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 (378 lines) | stat: -rw-r--r-- 14,063 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
/** @file

  This plugin counts the number of times every header has appeared.
  Maintains separate counts for client and origin headers.

  @section license License

  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 <iostream>
#include <map>
#include <memory>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <string>

#include "ts/ts.h"
#include "ts/remap.h"
#include "ts/parentselectdefs.h"

#include "consistenthash_config.h"
#include "strategy.h"

// TODO summary:
// - TSRemapInit version check

namespace
{
// The strategy and its transaction state.
struct StrategyTxn {
  TSNextHopSelectionStrategy *strategy;
  void *txn; // void* because the actual type will depend on the strategy.
  int request_count;
  TSResponseAction prev_ra;
};

// mark parents up or down, on failure or successful retry.
void
mark_response(TSHttpTxn txnp, StrategyTxn *strategyTxn, TSHttpStatus status)
{
  TSDebug(PLUGIN_NAME, "mark_response calling with code: %d", status);

  auto strategy = strategyTxn->strategy;

  const bool isFailure = strategy->codeIsFailure(status);

  TSResponseAction ra;
  // if the prev_host isn't null, then that was the actual host we tried which needs to be marked down.
  if (strategyTxn->prev_ra.hostname_len != 0) {
    ra = strategyTxn->prev_ra;
    TSDebug(PLUGIN_NAME, "mark_response using prev %.*s:%d", int(ra.hostname_len), ra.hostname, ra.port);
  } else {
    TSHttpTxnResponseActionGet(txnp, &ra);
    TSDebug(PLUGIN_NAME, "mark_response using response_action %.*s:%d", int(ra.hostname_len), ra.hostname, ra.port);
  }

  if (isFailure && strategy->onFailureMarkParentDown(status)) {
    if (ra.hostname == nullptr) {
      TSError(
        "[%s] mark_response got a failure, but response_action had no hostname! This shouldn't be possible! Not marking down!",
        PLUGIN_NAME);
    } else {
      TSDebug(PLUGIN_NAME, "mark_response marking %.*s:%d down", int(ra.hostname_len), ra.hostname, ra.port);
      strategy->mark(txnp, strategyTxn->txn, ra.hostname, ra.hostname_len, ra.port, PL_NH_MARK_DOWN);
    }
  } else if (!isFailure && ra.is_retry) {
    if (ra.hostname == nullptr) {
      TSError(
        "[%s] mark_response got a retry success, but response_action had no hostname! This shouldn't be possible! Not marking up!",
        PLUGIN_NAME);
    } else {
      TSDebug(PLUGIN_NAME, "mark_response marking %.*s:%d up", int(ra.hostname_len), ra.hostname, ra.port);
      strategy->mark(txnp, strategyTxn->txn, ra.hostname, ra.hostname_len, ra.port, PL_NH_MARK_UP);
    }
  }
}

int
handle_read_response(TSHttpTxn txnp, StrategyTxn *strategyTxn)
{
  TSDebug(PLUGIN_NAME, "handle_read_response calling");

  auto strategy = strategyTxn->strategy;

  TSDebug(PLUGIN_NAME, "handle_read_response got strategy '%s'", strategy->name());

  TSMBuffer resp;
  TSMLoc resp_hdr;
  if (TS_SUCCESS != TSHttpTxnServerRespGet(txnp, &resp, &resp_hdr)) {
    TSDebug(PLUGIN_NAME, "handle_read_response failed to get resp");
    TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
    return TS_SUCCESS;
  }

  TSHttpStatus status = TSHttpHdrStatusGet(resp, resp_hdr);
  TSDebug(PLUGIN_NAME, "handle_read_response got response code: %d", status);

  mark_response(txnp, strategyTxn, status);

  if (!strategy->codeIsFailure(status)) {
    // if it's a success, set the action to not retry
    TSResponseAction ra;
    // this sets failed=false, responseIsRetryable=false => don't retry, return the success
    memset(&ra, 0, sizeof(TSResponseAction)); // because {0} gives a C++ warning. Ugh.
    TSDebug(PLUGIN_NAME, "handle_read_response success, setting response_action to not retry");
    TSHttpTxnResponseActionSet(txnp, &ra);
  } else {
    // We already set the response_action for what to do on failure in send_request.
    // (because we don't always get here with responses, like DNS or connection failures)
    // But we need to get the action previously set, and update responseIsRetryable, which we couldn't determine before without the
    // Status.
    TSResponseAction ra;
    TSHttpTxnResponseActionGet(txnp, &ra);
    ra.responseIsRetryable = strategy->responseIsRetryable(strategyTxn->request_count - 1, status);

    TSHttpTxnResponseActionSet(txnp, &ra);
  }

  // un-set the "prev" hackery. That only exists for markdown, which we just did.
  // The response_action is now the next thing to try, if this was a failure,
  // and should now be considered authoritative for everything.

  memset(&strategyTxn->prev_ra, 0, sizeof(TSResponseAction));

  TSHandleMLocRelease(resp, TS_NULL_MLOC, resp_hdr);
  TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  return TS_SUCCESS;
}

int
handle_os_dns(TSHttpTxn txnp, StrategyTxn *strategyTxn)
{
  TSDebug(PLUGIN_NAME, "handle_os_dns calling");

  ++strategyTxn->request_count;

  auto strategy = strategyTxn->strategy;

  TSDebug(PLUGIN_NAME, "handle_os_dns got strategy '%s'", strategy->name());

  const TSServerState server_state = TSHttpTxnServerStateGet(txnp);
  if (server_state == TS_SRVSTATE_CONNECTION_ERROR || server_state == TS_SRVSTATE_INACTIVE_TIMEOUT) {
    mark_response(txnp, strategyTxn, STATUS_CONNECTION_FAILURE);
  }

  TSDebug(PLUGIN_NAME, "handle_os_dns had no prev, setting new response_action");

  {
    TSResponseAction ra;
    TSHttpTxnResponseActionGet(txnp, &ra);
    strategyTxn->prev_ra = ra;
  }

  TSResponseAction ra;
  memset(&ra, 0, sizeof(TSResponseAction));
  strategy->next(txnp, strategyTxn->txn, &ra.hostname, &ra.hostname_len, &ra.port, &ra.is_retry, &ra.no_cache);

  ra.fail = ra.hostname == nullptr; // failed is whether to immediately fail and return the client a 502. In this case: whether or
                                    // not we found another parent.
  ra.nextHopExists       = ra.hostname_len != 0;
  ra.responseIsRetryable = strategy->responseIsRetryable(strategyTxn->request_count - 1, STATUS_CONNECTION_FAILURE);
  ra.goDirect            = strategy->goDirect();
  ra.parentIsProxy       = strategy->parentIsProxy();
  TSDebug(PLUGIN_NAME, "handle_os_dns setting response_action hostname '%.*s' port %d direct %d proxy %d is_retry %d exists %d",
          int(ra.hostname_len), ra.hostname, ra.port, ra.goDirect, ra.parentIsProxy, ra.is_retry, ra.nextHopExists);
  TSHttpTxnResponseActionSet(txnp, &ra);

  TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  return TS_SUCCESS;
}

int
handle_txn_close(TSHttpTxn txnp, TSCont contp, StrategyTxn *strategyTxn)
{
  TSDebug(PLUGIN_NAME, "handle_txn_close calling");

  auto strategy = strategyTxn->strategy;

  if (strategy != nullptr) {
    TSContDataSet(contp, nullptr);
    strategy->deleteTxn(strategyTxn->txn);
    delete strategyTxn;
    // we delete the state, and the strategyAndState pointer at the end of the transaction,
    // but we DON'T delete the Strategy, which lives as long as the remap.
  }
  TSContDestroy(contp);
  TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  return TS_SUCCESS;
}

int
handle_hook(TSCont contp, TSEvent event, void *edata)
{
  TSDebug(PLUGIN_NAME, "handle_hook calling");

  TSHttpTxn txnp           = static_cast<TSHttpTxn>(edata);
  StrategyTxn *strategyTxn = static_cast<StrategyTxn *>(TSContDataGet(contp));

  TSDebug(PLUGIN_NAME, "handle_hook got strategy '%s'", strategyTxn->strategy->name());

  switch (event) {
  case TS_EVENT_HTTP_READ_RESPONSE_HDR:
    return handle_read_response(txnp, strategyTxn);
  case TS_EVENT_HTTP_OS_DNS:
    return handle_os_dns(txnp, strategyTxn);
  case TS_EVENT_HTTP_TXN_CLOSE:
    return handle_txn_close(txnp, contp, strategyTxn);
  default:
    TSError("[%s] handle_hook got unknown event %d - should never happen!", PLUGIN_NAME, event);
    return TS_ERROR;
  }
}

} // namespace

TSReturnCode
TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size)
{
  TSDebug(PLUGIN_NAME, "TSRemapInit calling");

  // TODO add ATS API Version check here, to bail if ATS doesn't support the version necessary for strategy plugins

  if (!api_info) {
    strncpy(errbuf, "[tsstrategy_init] - Invalid TSRemapInterface argument", errbuf_size - 1);
    return TS_ERROR;
  }

  if (api_info->tsremap_version < TSREMAP_VERSION) {
    snprintf(errbuf, errbuf_size, "[TSStrategyInit] - Incorrect API version %ld.%ld", api_info->tsremap_version >> 16,
             (api_info->tsremap_version & 0xffff));
    return TS_ERROR;
  }

  TSDebug(PLUGIN_NAME, "Remap successfully initialized");
  return TS_SUCCESS;
}

TSReturnCode
TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuff, int errbuff_size)
{
  TSDebug(PLUGIN_NAME, "TSRemapNewInstance calling");

  *ih = nullptr;

  for (int i = 0; i < argc; ++i) {
    TSDebug(PLUGIN_NAME, "TSRemapNewInstance arg %d '%s'", i, argv[i]);
  }

  if (argc < 4) {
    TSError("[%s] insufficient number of arguments, %d, expected file and strategy argument.", PLUGIN_NAME, argc);
    return TS_ERROR;
  }
  if (argc > 4) {
    TSError("[%s] too many arguments, %d, only expected file and strategy argument.", PLUGIN_NAME, argc);
    return TS_ERROR;
  }

  const char *remap_from       = argv[0];
  const char *remap_to         = argv[1];
  const char *config_file_path = argv[2];
  const char *strategy_name    = argv[3];

  TSDebug(PLUGIN_NAME, "%s %s Loading parent selection strategy file %s for strategy %s", remap_from, remap_to, config_file_path,
          strategy_name);
  auto file_strategies = createStrategiesFromFile(config_file_path);
  if (file_strategies.size() == 0) {
    TSError("[%s] %s %s Failed to parse configuration file %s", PLUGIN_NAME, remap_from, remap_to, config_file_path);
    return TS_ERROR;
  }

  TSDebug(PLUGIN_NAME, "'%s' '%s' successfully created strategies in file %s num %d", remap_from, remap_to, config_file_path,
          int(file_strategies.size()));

  auto new_strategy = file_strategies.find(strategy_name);
  if (new_strategy == file_strategies.end()) {
    TSDebug(PLUGIN_NAME, "'%s' '%s' TSRemapNewInstance strategy '%s' not found in file '%s'", remap_from, remap_to, strategy_name,
            config_file_path);
    return TS_ERROR;
  }

  TSDebug(PLUGIN_NAME, "'%s' '%s' TSRemapNewInstance successfully loaded strategy '%s' from '%s'.", remap_from, remap_to,
          strategy_name, config_file_path);

  // created a raw pointer _to_ a shared_ptr, because ih needs a raw pointer.
  // The raw pointer in ih will be deleted in TSRemapDeleteInstance,
  // which will destruct the shared_ptr,
  // destroying the strategy if this is the last remap rule using it.
  *ih = static_cast<void *>(new std::shared_ptr<TSNextHopSelectionStrategy>(new_strategy->second));

  // Associate our config file with remap.config to be able to initiate reloads
  TSMgmtString result;
  const char *var_name = "proxy.config.url_remap.filename";
  TSMgmtStringGet(var_name, &result);
  TSMgmtConfigFileAdd(result, config_file_path);

  return TS_SUCCESS;
}

extern "C" tsapi TSRemapStatus
TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
{
  TSDebug(PLUGIN_NAME, "TSRemapDoRemap calling");

  auto strategy_ptr = static_cast<std::shared_ptr<TSNextHopSelectionStrategy> *>(ih);
  auto strategy     = strategy_ptr->get();

  TSDebug(PLUGIN_NAME, "TSRemapDoRemap got strategy '%s'", strategy->name());

  TSCont cont = TSContCreate(handle_hook, TSMutexCreate());

  auto strategyTxn           = new StrategyTxn();
  strategyTxn->strategy      = strategy;
  strategyTxn->txn           = strategy->newTxn();
  strategyTxn->request_count = 0;
  memset(&strategyTxn->prev_ra, 0, sizeof(TSResponseAction));
  TSContDataSet(cont, (void *)strategyTxn);

  TSHttpTxnHookAdd(txnp, TS_HTTP_READ_RESPONSE_HDR_HOOK, cont);
  TSHttpTxnHookAdd(txnp, TS_HTTP_OS_DNS_HOOK, cont);
  TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, cont);

  TSResponseAction ra;
  memset(&ra, 0, sizeof(TSResponseAction)); // because {0} gives a C++ warning. Ugh.
  strategy->next(txnp, strategyTxn->txn, &ra.hostname, &ra.hostname_len, &ra.port, &ra.is_retry, &ra.no_cache);

  ra.nextHopExists = ra.hostname != nullptr;
  ra.fail          = !ra.nextHopExists;
  // The action here is used for the very first connection, not any retry. So of course we should try it.
  ra.responseIsRetryable = true;
  ra.goDirect            = strategy->goDirect();
  ra.parentIsProxy       = strategy->parentIsProxy();

  if (ra.fail && !ra.goDirect) {
    // TODO make configurable
    TSDebug(PLUGIN_NAME, "TSRemapDoRemap strategy '%s' next returned nil, returning 502!", strategy->name());
    TSHttpTxnStatusSet(txnp, TS_HTTP_STATUS_BAD_GATEWAY);
    // TODO verify TS_EVENT_HTTP_TXN_CLOSE fires, and if not, free the cont here.
    return TSREMAP_DID_REMAP;
  }

  TSDebug(PLUGIN_NAME, "TSRemapDoRemap setting response_action hostname '%.*s' port %d direct %d proxy %d", int(ra.hostname_len),
          ra.hostname, ra.port, ra.goDirect, ra.parentIsProxy);
  TSHttpTxnResponseActionSet(txnp, &ra);

  return TSREMAP_NO_REMAP;
}

extern "C" tsapi void
TSRemapDeleteInstance(void *ih)
{
  TSDebug(PLUGIN_NAME, "TSRemapDeleteInstance calling");
  auto strategy_ptr = static_cast<std::shared_ptr<TSNextHopSelectionStrategy> *>(ih);
  delete strategy_ptr;
  TSDebug(PLUGIN_NAME, "TSRemapDeleteInstance deleted strategy pointer");
}

void
TSRemapPreConfigReload(void)
{
  TSDebug(PLUGIN_NAME, "TSRemapPreConfigReload clearing strategies cache");
  clearStrategiesCache();
}