Skip to content

Commit a5cdbb7

Browse files
committed
initial working code for set/get strategies during TSRemapNewInstance
1 parent 6da2339 commit a5cdbb7

File tree

9 files changed

+108
-58
lines changed

9 files changed

+108
-58
lines changed

include/proxy/http/remap/RemapConfig.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ struct BUILD_TABLE_INFO {
7676
// noncopyable
7777
BUILD_TABLE_INFO(const BUILD_TABLE_INFO &) = delete; // disabled
7878
BUILD_TABLE_INFO &operator=(const BUILD_TABLE_INFO &) = delete; // disabled
79-
80-
// Exposes the main build table to the API for use during TSRemapNewInstance.
81-
inline static BUILD_TABLE_INFO *instance = nullptr;
8279
};
8380

8481
const char *remap_parse_directive(BUILD_TABLE_INFO *bti, char *errbuf, size_t errbufsize);

include/proxy/http/remap/UrlMapping.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "tscore/List.h"
3737

3838
class NextHopSelectionStrategy;
39+
class NextHopStrategyFactory;
3940

4041
/**
4142
* Used to store http referrer strings (and/or regexp)
@@ -112,9 +113,15 @@ class url_mapping
112113
bool ip_allow_check_enabled_p = false;
113114
acl_filter_rule *filter = nullptr; // acl filtering (linked list of rules)
114115
LINK(url_mapping, link); // For use with the main Queue linked list holding all the mapping
115-
NextHopSelectionStrategy *strategy = nullptr;
116-
std::string remapKey;
117-
std::atomic<uint64_t> _hitCount = 0; // counter can overflow
116+
117+
NextHopSelectionStrategy *strategy = nullptr;
118+
NextHopStrategyFactory *strategyFactory = nullptr;
119+
120+
std::string remapKey;
121+
std::atomic<uint64_t> _hitCount = 0; // counter can overflow
122+
123+
// For use with the strategies API to be called during TSRemapNewInstance.
124+
static inline thread_local url_mapping *instance = nullptr;
118125

119126
int
120127
getRank() const

include/ts/ts.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,10 +1579,22 @@ void TSHttpTxnErrorBodySet(TSHttpTxn txnp, char *buf, size_t buflength, char *mi
15791579
*/
15801580
char *TSHttpTxnErrorBodyGet(TSHttpTxn txnp, size_t *buflength, char **mimetype);
15811581

1582+
/**
1583+
Retrieves a handle to the named strategy in the strategy table.
1584+
Returns nullptr if no strategy is found.
1585+
This uses the current transaction's state machine to get
1586+
access to UrlRewrite's NextHopStrategyFactory.
1587+
It's preferable to retrive strategies during TSRemapNewInstance.
1588+
1589+
@param txnp HTTP transaction which holds the strategy table.
1590+
@param name of the strategy to look up.
1591+
1592+
*/
1593+
void const *TSHttpTxnNextHopStrategyFindByName(TSHttpTxn txnp, const char *name);
1594+
15821595
/**
15831596
Sets the Transaction's Next Hop Parent Strategy.
1584-
Calling this after TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK will
1585-
result in bad behavior.
1597+
Must be called before parent selection logic is required.
15861598
15871599
You can get this strategy pointer by calling TSHttpTxnParentStrategyGet().
15881600
@@ -1611,10 +1623,10 @@ void const *TSHttpTxnNextHopStrategyGet(TSHttpTxn txnp);
16111623
- parent proxying not enabled
16121624
- no parent selection strategy (using parent.config)
16131625
1614-
@param txnp HTTP transaction whose next hop strategy to get.
1626+
@param pointer to the NextHopStrategy.
16151627
16161628
*/
1617-
char const *TSHttpNextHopStrategyNameGet(void const *strategy);
1629+
char const *TSNextHopStrategyNameGet(void const *strategy);
16181630

16191631
/**
16201632
Retrieves a pointer to the named strategy in the strategy table.
@@ -1627,19 +1639,31 @@ char const *TSHttpNextHopStrategyNameGet(void const *strategy);
16271639
@param name of the strategy to look up.
16281640
16291641
*/
1630-
void const *TSHttpInitNextHopNamedStrategyGet(const char *name);
1642+
void const *TSRemapNextHopStrategyFindByName(const char *name);
1643+
1644+
/**
1645+
Retrieves a pointer to remap rule strategy pointer.
1646+
This can only be called during TSRemapNewInstance.
1647+
DO NOT FREE.
1648+
1649+
Returns nullptr if no strategy assigned.
1650+
@param name of the strategy to look up.
1651+
1652+
*/
1653+
void const *TSRemapNextHopStrategyGet();
16311654

16321655
/**
16331656
Retrieves a pointer to the named strategy in the strategy table.
1634-
Returns nullptr if no strategy is set.
1635-
This uses the current transaction's state machine to get
1636-
access to UrlRewrite's NextHopStrategyFactory.
1657+
This can only be called during TSRemapNewInstance.
1658+
DO NOT FREE.
1659+
1660+
Returns nullptr if no strategy found.
1661+
This uses the currently being loaded RemapConfig NextHopStrategyFactory.
16371662
1638-
@param txnp HTTP transaction which holds the strategy table.
16391663
@param name of the strategy to look up.
16401664
16411665
*/
1642-
void const *TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name);
1666+
void TSRemapNextHopStrategySet(void const *strategy);
16431667

16441668
/**
16451669
Sets the parent proxy name and port. The string hostname is copied

plugins/header_rewrite/conditions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ ConditionNextHop::append_value(std::string &s, const Resources &res)
15391539
s.append(std::to_string(port));
15401540
} break;
15411541
case NEXT_HOP_STRATEGY: {
1542-
char const *const name = TSHttpNextHopStrategyNameGet(res.state.txnp);
1542+
char const *const name = TSNextHopStrategyNameGet(res.state.txnp);
15431543
if (nullptr != name) {
15441544
Dbg(pi_dbg_ctl, "Appending '%s' to evaluation value", name);
15451545
s.append(name);

plugins/header_rewrite/operators.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ OperatorSetNextHopStrategy::initialize(Parser &p)
16411641
if (_stratname.empty() || "null" == _stratname) {
16421642
Dbg(pi_dbg_ctl, "OperatorSetNextHopStrategy::initialize: 'clear'");
16431643
} else {
1644-
_strategy = TSHttpInitNextHopNamedStrategyGet(_stratname.c_str());
1644+
_strategy = TSRemapNextHopStrategyFindByName(_stratname.c_str());
16451645
if (nullptr == _strategy) {
16461646
TSWarning("[%s] Failed to get strategy '%s'", PLUGIN_NAME, _stratname.c_str());
16471647
_apply = false;

plugins/lua/ts_lua_http.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ ts_lua_http_get_next_hop_strategy(lua_State *L)
537537

538538
void const *const stratptr = TSHttpTxnNextHopStrategyGet(http_ctx->txnp);
539539
if (nullptr != stratptr) {
540-
name = TSHttpNextHopStrategyNameGet(stratptr);
540+
name = TSNextHopStrategyNameGet(stratptr);
541541
}
542542

543543
if (name == nullptr) {
@@ -569,7 +569,7 @@ ts_lua_http_set_next_hop_strategy(lua_State *L)
569569
Dbg(dbg_ctl, "Clearning strategy (use parent.config)");
570570
TSHttpTxnNextHopStrategySet(http_ctx->txnp, nullptr);
571571
} else {
572-
void const *const stratptr = TSHttpTxnNextHopNamedStrategyGet(http_ctx->txnp, name);
572+
void const *const stratptr = TSHttpTxnNextHopStrategyFindByName(http_ctx->txnp, name);
573573
if (nullptr == stratptr) {
574574
TSError("[ts_lua][%s] Failed get next hop strategy name '%s'", __FUNCTION__, name);
575575
} else {

plugins/regex_remap/regex_remap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ RemapRegex::initialize(const std::string &reg, const std::string &sub, const std
332332
_has_strategy = true;
333333
_strategy_name = opt_val;
334334
if (!_strategy_name.empty() && "null" != _strategy_name) {
335-
_strategy = TSHttpInitNextHopNamedStrategyGet(_strategy_name.c_str());
335+
_strategy = TSRemapNextHopStrategyFindByName(_strategy_name.c_str());
336336
if (nullptr == _strategy) {
337337
TSError("[%s] Unable to resolve strategy: '%s'", PLUGIN_NAME, opt_val.c_str());
338338
_has_strategy = false; // disable the strategy action

src/api/InkAPI.cc

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5001,6 +5001,24 @@ TSHttpTxnNextHopStrategyGet(TSHttpTxn txnp)
50015001
return static_cast<void *>(sm->t_state.next_hop_strategy);
50025002
}
50035003

5004+
void const *
5005+
TSHttpTxnNextHopStrategyFindByName(TSHttpTxn txnp, const char *name)
5006+
{
5007+
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
5008+
sdk_assert(sdk_sanity_check_null_ptr((void *)name) == TS_SUCCESS);
5009+
5010+
auto sm = reinterpret_cast<HttpSM const *>(txnp);
5011+
5012+
sdk_assert(sdk_sanity_check_null_ptr((void *)sm->m_remap) == TS_SUCCESS);
5013+
sdk_assert(sdk_sanity_check_null_ptr((void *)sm->m_remap->strategyFactory) == TS_SUCCESS);
5014+
5015+
// HttpSM has a reference count handle to UrlRewrite which has a
5016+
// pointer to NextHopStrategyFactory
5017+
NextHopSelectionStrategy const *const strategy = sm->m_remap->strategyFactory->strategyInstance(name);
5018+
5019+
return static_cast<void const *>(strategy);
5020+
}
5021+
50045022
void
50055023
TSHttpTxnNextHopStrategySet(TSHttpTxn txnp, void const *stratptr)
50065024
{
@@ -5014,53 +5032,49 @@ TSHttpTxnNextHopStrategySet(TSHttpTxn txnp, void const *stratptr)
50145032
sm->t_state.next_hop_strategy = const_cast<NextHopSelectionStrategy *>(strategy);
50155033
}
50165034

5017-
char const *
5018-
TSHttpNextHopStrategyNameGet(void const *stratptr)
5019-
{
5020-
char const *name = nullptr;
5021-
if (nullptr != stratptr) {
5022-
auto strategy = reinterpret_cast<NextHopSelectionStrategy const *>(stratptr);
5023-
name = strategy->strategy_name.c_str();
5024-
}
5025-
5026-
return name;
5027-
}
5028-
50295035
void const *
5030-
TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name)
5036+
TSRemapNextHopStrategyFindByName(const char *name)
50315037
{
5032-
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
5033-
sdk_assert(sdk_sanity_check_null_ptr((void *)name) == TS_SUCCESS);
5038+
auto const um = url_mapping::instance;
5039+
sdk_assert(sdk_sanity_check_null_ptr((void *)um) == TS_SUCCESS);
50345040

5035-
auto sm = reinterpret_cast<HttpSM const *>(txnp);
5041+
NextHopSelectionStrategy const *strategy = nullptr;
50365042

5037-
sdk_assert(sdk_sanity_check_null_ptr((void *)sm->m_remap) == TS_SUCCESS);
5038-
sdk_assert(sdk_sanity_check_null_ptr((void *)sm->m_remap->strategyFactory) == TS_SUCCESS);
5043+
if (nullptr != um->strategyFactory) {
5044+
// HttpSM has a reference count handle to UrlRewrite which manages
5045+
// the NextHopStrategyFactory pointer.
5046+
strategy = um->strategyFactory->strategyInstance(name);
5047+
}
50395048

5040-
// HttpSM has a reference count handle to UrlRewrite which has a
5041-
// pointer to NextHopStrategyFactory
5042-
NextHopSelectionStrategy const *const strat = sm->m_remap->strategyFactory->strategyInstance(name);
5049+
return static_cast<void const *>(strategy);
5050+
}
50435051

5044-
return static_cast<void const *>(strat);
5052+
void
5053+
TSRemapNextHopStrategySet(void const *stratptr)
5054+
{
5055+
auto const um = url_mapping::instance;
5056+
sdk_assert(sdk_sanity_check_null_ptr((void *)um) == TS_SUCCESS);
5057+
um->strategy = static_cast<NextHopSelectionStrategy *>(const_cast<void *>(stratptr));
50455058
}
50465059

50475060
void const *
5048-
TSHttpInitNextHopNamedStrategyGet(const char *name)
5061+
TSRemapNextHopStrategyGet()
50495062
{
5050-
auto const bt = BUILD_TABLE_INFO::instance;
5051-
5052-
sdk_assert(sdk_sanity_check_null_ptr((void *)bt) == TS_SUCCESS);
5053-
sdk_assert(sdk_sanity_check_null_ptr((void *)bt->rewrite) == TS_SUCCESS);
5054-
5055-
NextHopSelectionStrategy const *strat = nullptr;
5063+
auto const um = url_mapping::instance;
5064+
sdk_assert(sdk_sanity_check_null_ptr((void *)um) == TS_SUCCESS);
5065+
return static_cast<void *>(um->strategy);
5066+
}
50565067

5057-
if (nullptr != bt->rewrite->strategyFactory) {
5058-
// HttpSM has a reference count handle to UrlRewrite which manages
5059-
// the NextHopStrategyFactory pointer.
5060-
strat = bt->rewrite->strategyFactory->strategyInstance(name);
5068+
char const *
5069+
TSNextHopStrategyNameGet(void const *stratptr)
5070+
{
5071+
char const *name = nullptr;
5072+
if (nullptr != stratptr) {
5073+
auto strategy = reinterpret_cast<NextHopSelectionStrategy const *>(stratptr);
5074+
name = strategy->strategy_name.c_str();
50615075
}
50625076

5063-
return static_cast<void const *>(strat);
5077+
return name;
50645078
}
50655079

50665080
TSReturnCode

src/proxy/http/remap/RemapConfig.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,9 @@ remap_parse_config_bti(const char *path, BUILD_TABLE_INFO *bti)
14291429
}
14301430
}
14311431

1432+
// Set up for ts API for strategies
1433+
new_mapping->strategyFactory = bti->rewrite->strategyFactory;
1434+
14321435
// Check "remap" plugin options and load .so object
14331436
if ((bti->remap_optflg & REMAP_OPTFLG_PLUGIN) != 0 &&
14341437
(maptype == mapping_type::FORWARD_MAP || maptype == mapping_type::FORWARD_MAP_REFERER ||
@@ -1437,13 +1440,17 @@ remap_parse_config_bti(const char *path, BUILD_TABLE_INFO *bti)
14371440
int plugin_found_at = 0;
14381441
int jump_to_argc = 0;
14391442

1443+
// Set up for ts API for strategies
1444+
url_mapping::instance = new_mapping;
1445+
14401446
// this loads the first plugin
14411447
if (!remap_load_plugin(bti->argv, bti->argc, new_mapping, errStrBuf, sizeof(errStrBuf), 0, &plugin_found_at,
14421448
bti->rewrite)) {
14431449
Dbg(dbg_ctl_remap_plugin, "Remap plugin load error - %s", errStrBuf[0] ? errStrBuf : "Unknown error");
14441450
errStr = errStrBuf;
14451451
goto MAP_ERROR;
14461452
}
1453+
14471454
// this loads any subsequent plugins (if present)
14481455
while (plugin_found_at) {
14491456
jump_to_argc += plugin_found_at;
@@ -1454,6 +1461,8 @@ remap_parse_config_bti(const char *path, BUILD_TABLE_INFO *bti)
14541461
goto MAP_ERROR;
14551462
}
14561463
}
1464+
1465+
url_mapping::instance = nullptr;
14571466
}
14581467
}
14591468

@@ -1475,6 +1484,8 @@ remap_parse_config_bti(const char *path, BUILD_TABLE_INFO *bti)
14751484
snprintf(errBuf, sizeof(errBuf), "%s failed to add remap rule at %s line %d: %s", modulePrefix, path, cln + 1, errStr);
14761485
Error("%s", errBuf);
14771486

1487+
url_mapping::instance = nullptr;
1488+
14781489
delete reg_map;
14791490
delete new_mapping;
14801491
return false;
@@ -1488,7 +1499,6 @@ bool
14881499
remap_parse_config(const char *path, UrlRewrite *rewrite)
14891500
{
14901501
BUILD_TABLE_INFO bti;
1491-
BUILD_TABLE_INFO::instance = &bti; // needed for plugin init api
14921502

14931503
/* If this happens to be a config reload, the list of loaded remap plugins is non-empty, and we
14941504
* can signal all these plugins that a reload has begun. */
@@ -1503,7 +1513,5 @@ remap_parse_config(const char *path, UrlRewrite *rewrite)
15031513

15041514
bti.clear_acl_rules_list();
15051515

1506-
BUILD_TABLE_INFO::instance = nullptr;
1507-
15081516
return status;
15091517
}

0 commit comments

Comments
 (0)