Skip to content

Commit ac5a3b2

Browse files
committed
Create ts API functions to change a transaction strategy.
This adds 'c' friendly api calls to get and set strategies, to get the name of a given strategy and to get a strategy from the strategy factory by name. Also includes additions to header_rewrite, regex_remap and lua plugins.
1 parent 1754d38 commit ac5a3b2

28 files changed

+956
-126
lines changed

doc/admin-guide/plugins/header_rewrite.en.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ are supported::
647647

648648
%{NEXT-HOP:HOST} Name of the current selected parent.
649649
%{NEXT-HOP:PORT} Port of the current selected parent.
650+
%{NEXT-HOP:STRATEGY} Name of the current strategy (can be "" if not using a strategy)
650651

651652
Note that the ``<part>`` of NEXT-HOP will likely not be available unless
652653
an origin server connection is attempted at which point it will available
@@ -1083,6 +1084,18 @@ if necessary.
10831084
The header's ``<value>`` may be a literal string, or take advantage of
10841085
`String concatenations`_ to calculate a dynamic value for the header.
10851086

1087+
set-next-hop-strategy
1088+
~~~~~~~~~~~~~~~~~~~~~
1089+
::
1090+
1091+
set-next-hop-strategy <name>
1092+
1093+
Replaces/Sets the current next hop parent selection strategy with
1094+
the matching strategy specified in `strategies.yaml`
1095+
1096+
Setting to "" removes the current strategy which will fall back
1097+
to other methods (ie: parent.config or remap to url).
1098+
10861099
set-redirect
10871100
~~~~~~~~~~~~
10881101
::

doc/admin-guide/plugins/lua.en.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,50 @@ Here is an example:
19301930

19311931
`TOP <#ts-lua-plugin>`_
19321932

1933+
ts.http.get_next_hop_strategy
1934+
-----------------------------
1935+
**syntax:** *ts.http.get_next_hop_strategy()*
1936+
1937+
**context:** function @ TS_LUA_HOOK_READ_REQUEST_HDR or do_remap()
1938+
1939+
**description** Returns the name of the current next hop selection strategy, or nil string if no strategy is in use.
1940+
1941+
Here is an example:
1942+
1943+
::
1944+
1945+
function do_remap()
1946+
local strategy = ts.http.get_next_hop_strategy()
1947+
ts.debug("Using strategy: " .. strategy)
1948+
end
1949+
1950+
`TOP <#ts-lua-plugin>`_
1951+
1952+
ts.http.set_next_hop_strategy
1953+
-----------------------------
1954+
**syntax:** *ts.http.set_next_hop_strategy(str)*
1955+
1956+
**context:** function @ TS_LUA_HOOK_READ_REQUEST_HDR or do_remap()
1957+
1958+
**description** Looks for the named strategy and sets the current
1959+
transaction to use that strategy.
1960+
1961+
Use empty string to clear the strategy and fall back to parent.config
1962+
or the remap to url.
1963+
1964+
Here is an example:
1965+
1966+
::
1967+
1968+
function do_remap()
1969+
local uri = ts.client_request.get_uri()
1970+
if uri == "otherhost" then
1971+
ts.http.set_next_hop_strategy("otherhost")
1972+
end
1973+
end
1974+
1975+
`TOP <#ts-lua-plugin>`_
1976+
19331977
ts.sha256
19341978
---------
19351979
**syntax:** *digest = ts.sha256(str)*

doc/admin-guide/plugins/regex_remap.en.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ remap.config. The following options are available ::
143143

144144
@caseless - Make regular expressions case insensitive
145145
@lowercase_substitutions - Turn on (enable) lower case substitutions
146+
@strategy - Specify a strategy from strategies.yaml
146147

147148

148149
This can be useful to force a particular response for some URLs, e.g. ::
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSHttpNextHopStrategyNameGet
22+
****************************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: char const* TSHttpNextHopStrategyNameGet(void const* strategy)
32+
33+
Description
34+
===========
35+
36+
Gets the name associated with the provided strategy.
37+
This may be nullptr indicating that parent.config is in use.
38+
39+
.. note::
40+
41+
This returned pointer must not be freed and the contents must not
42+
be changed.
43+
Strategy pointers held by plugins will become invalid when ATS
44+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
45+
46+
See Also
47+
========
48+
49+
:func:`TSHttpTxnNextHopStrategyGet`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSHttpTxnNextHopNamedStrategyGet
22+
********************************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: void const* TSHttpTxnNextHopNamedStrategyGet(TSHttpTxn txnp, const char *name)
32+
33+
Description
34+
===========
35+
36+
Gets a pointer to the specified :arg:`name` NextHopSelectionStrategy.
37+
This may be nullptr indicating that no strategy exists with the given name.
38+
39+
This function uses the transaction :arg:`txnp` to get access to the
40+
NextHopStrategyFactory associated with the current configuration.
41+
42+
.. note::
43+
44+
This returned pointer must not be freed and the contents must not
45+
be changed.
46+
Strategy pointers held by plugins will become invalid when ATS
47+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
48+
49+
See Also
50+
========
51+
52+
:func:`TSHttpTxnNextHopStrategySet`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSHttpTxnNextHopStrategyGet
22+
***************************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: void const* TSHttpTxnNextHopStrategyGet(TSHttpTxn txnp)
32+
33+
Description
34+
===========
35+
36+
Gets a pointer to the current transaction :arg:`txnp` NextHopSelectionStrategy.
37+
This may be nullptr indicating that parent.config is in use.
38+
39+
.. note::
40+
41+
This strategy pointer must not be freed and the contents must not
42+
be changed.
43+
Strategy pointers held by plugins will become invalid when ATS
44+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
45+
46+
See Also
47+
========
48+
49+
:func:`TSHttpTxnNextHopStrategySet`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed
3+
with this work for additional information regarding copyright
4+
ownership. The ASF licenses this file to you under the Apache
5+
License, Version 2.0 (the "License"); you may not use this file
6+
except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
implied. See the License for the specific language governing
15+
permissions and limitations under the License.
16+
17+
.. include:: ../../../common.defs
18+
19+
.. default-domain:: cpp
20+
21+
TSHttpTxnNextHopNameGet
22+
***********************
23+
24+
Synopsis
25+
========
26+
27+
.. code-block:: cpp
28+
29+
#include <ts/ts.h>
30+
31+
.. function:: void TSHttpTxnNextHopStrategySet(TSHttpTxn txnp, void const* strategy)
32+
33+
Description
34+
===========
35+
36+
Sets the next hop strategy for the transaction :arg:`txnp`
37+
This :arg:`strategy` pointer must be a valid strategy and can be
38+
nullptr to indicate that parent.config will be used instead.
39+
40+
Plugins can get a strategy by name by calling
41+
:func:`TSHttpTxnNextHopStrategyGet` to get the current transaction's
42+
active strategy or :func:`TSHttpTxnNextHopNamedStrategyGet` to
43+
look up a strategy by name using the transaction's pointer to the
44+
NextHopStrategyFactory strategy database.
45+
46+
.. note::
47+
48+
This strategy pointer must not be freed and the contents must not
49+
be changed.
50+
Strategy pointers held by plugins will become invalid when ATS
51+
configs are reloaded and should be reset with :func:`TSRemapNewInstance`
52+
53+
See Also
54+
========
55+
56+
:func:`TSHttpTxnNextHopStrategyGet`, :func:`TSHttpTxnNextHopNamedStrategyGet`.

include/proxy/http/HttpTransact.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,11 @@ class HttpTransact
724724
// able to defer some work in building the request
725725
TransactFunc_t pending_work = nullptr;
726726

727-
HttpRequestData request_data;
728-
ParentConfigParams *parent_params = nullptr;
729-
std::shared_ptr<NextHopSelectionStrategy> next_hop_strategy = nullptr;
730-
ParentResult parent_result;
731-
CacheControlResult cache_control;
727+
HttpRequestData request_data;
728+
ParentConfigParams *parent_params = nullptr;
729+
NextHopSelectionStrategy *next_hop_strategy = nullptr;
730+
ParentResult parent_result;
731+
CacheControlResult cache_control;
732732

733733
StateMachineAction_t next_action = StateMachineAction_t::UNDEFINED; // out
734734
StateMachineAction_t api_next_action = StateMachineAction_t::UNDEFINED; // out

include/proxy/http/remap/NextHopStrategyFactory.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ class NextHopStrategyFactory
4545
NextHopStrategyFactory() = delete;
4646
NextHopStrategyFactory(const char *file);
4747
~NextHopStrategyFactory();
48-
std::shared_ptr<NextHopSelectionStrategy> strategyInstance(const char *name);
48+
49+
// The lifetime of this is attached to UrlRewrite which is reference
50+
// counted and attached to an HttpSM.
51+
NextHopSelectionStrategy *strategyInstance(const char *name) const;
4952

5053
bool strategies_loaded;
5154

5255
private:
5356
std::string fn;
5457
void loadConfigFile(const std::string &file, std::stringstream &doc, std::unordered_set<std::string> &include_once);
5558
void createStrategy(const std::string &name, const NHPolicyType policy_type, ts::Yaml::Map &node);
56-
std::unordered_map<std::string, std::shared_ptr<NextHopSelectionStrategy>> _strategies;
59+
std::unordered_map<std::string, NextHopSelectionStrategy *> _strategies;
5760
};

include/proxy/http/remap/UrlMapping.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ class url_mapping
112112
bool ip_allow_check_enabled_p = false;
113113
acl_filter_rule *filter = nullptr; // acl filtering (linked list of rules)
114114
LINK(url_mapping, link); // For use with the main Queue linked list holding all the mapping
115-
std::shared_ptr<NextHopSelectionStrategy> strategy = nullptr;
116-
std::string remapKey;
117-
std::atomic<uint64_t> _hitCount = 0; // counter can overflow
115+
NextHopSelectionStrategy *strategy = nullptr;
116+
std::string remapKey;
117+
std::atomic<uint64_t> _hitCount = 0; // counter can overflow
118118

119119
int
120120
getRank() const

0 commit comments

Comments
 (0)