Skip to content

Commit

Permalink
regex_revalidate: add stats for miss/stale counts (#7950)
Browse files Browse the repository at this point in the history
  • Loading branch information
traeak committed Jun 18, 2021
1 parent c75c797 commit f3fb74e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/admin-guide/plugins/regex_revalidate.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ regular expression against your origin URLs permits. Thus, individual cache
objects may have rules created for them, or entire path prefixes, or even any
cache objects with a particular file extension.

Revalidate count stats for MISS and STALE are recorded under plugins

Installation
============

Expand Down
47 changes: 47 additions & 0 deletions plugins/regex_revalidate/regex_revalidate.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,50 @@ static char const *const RESULT_MISS = "MISS";
static char const *const RESULT_STALE = "STALE";
static char const *const RESULT_UNKNOWN = "UNKNOWN";

static int stat_id_stale = TS_ERROR;
static char const *const stat_name_stale = "plugin.regex_revalidate.stale";
static int stat_id_miss = TS_ERROR;
static char const *const stat_name_miss = "plugin.regex_revalidate.miss";

static void
create_stats()
{
if (TS_ERROR == stat_id_stale && TS_ERROR == TSStatFindName(stat_name_stale, &stat_id_stale)) {
stat_id_stale = TSStatCreate(stat_name_stale, TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
if (TS_ERROR != stat_id_stale) {
TSDebug(PLUGIN_NAME, "Created stat '%s'", stat_name_stale);
}
}

if (TS_ERROR == stat_id_miss && TS_ERROR == TSStatFindName(stat_name_miss, &stat_id_miss)) {
stat_id_miss = TSStatCreate(stat_name_miss, TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
if (TS_ERROR != stat_id_miss) {
TSDebug(PLUGIN_NAME, "Created stat '%s'", stat_name_miss);
}
}
}

static void
increment_stat(TSCacheLookupResult const result)
{
switch (result) {
case TS_CACHE_LOOKUP_MISS:
if (TS_ERROR != stat_id_miss) {
TSStatIntIncrement(stat_id_miss, 1);
TSDebug(PLUGIN_NAME, "Incrementing stat '%s'", stat_name_miss);
}
break;
case TS_CACHE_LOOKUP_HIT_STALE:
if (TS_ERROR != stat_id_stale) {
TSStatIntIncrement(stat_id_stale, 1);
TSDebug(PLUGIN_NAME, "Incrementing stat '%s'", stat_name_stale);
}
break;
default:
break;
}
}

static char const *const
strForResult(TSCacheLookupResult const result)
{
Expand Down Expand Up @@ -585,6 +629,7 @@ main_handler(TSCont cont, TSEvent event, void *edata)
}
if (pcre_exec(iptr->regex, iptr->regex_extra, url, url_len, 0, 0, NULL, 0) >= 0) {
TSHttpTxnCacheLookupStatusSet(txn, iptr->new_result);
increment_stat(iptr->new_result);
TSDebug(PLUGIN_NAME, "Forced revalidate - %.*s %s", url_len, url, strForResult(iptr->new_result));
iptr = NULL;
}
Expand Down Expand Up @@ -700,6 +745,8 @@ TSPluginInit(int argc, const char *argv[])
TSDebug(PLUGIN_NAME, "Plugin registration succeeded");
}

create_stats();

main_cont = TSContCreate(main_handler, NULL);
TSContDataSet(main_cont, (void *)pstate);
TSHttpHookAdd(TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, main_cont);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugin.regex_revalidate.stale 3
plugin.regex_revalidate.miss 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugin.regex_revalidate.stale 1
plugin.regex_revalidate.miss 2
30 changes: 30 additions & 0 deletions tests/gold_tests/pluginTest/regex_revalidate/metrics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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.

N=60
while (( N > 0 ))
do
rm -f metrics.out
traffic_ctl metric match regex_revalidate > metrics.out
sleep 1
if diff metrics.out ${AUTEST_TEST_DIR}/gold/metrics.gold
then
exit 0
fi
let N=N-1
done
echo TIMEOUT
exit 1
30 changes: 30 additions & 0 deletions tests/gold_tests/pluginTest/regex_revalidate/metrics_miss.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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.

N=60
while (( N > 0 ))
do
rm -f metrics.out
traffic_ctl metric match regex_revalidate > metrics.out
sleep 1
if diff metrics.out ${AUTEST_TEST_DIR}/gold/metrics_miss.gold
then
exit 0
fi
let N=N-1
done
echo TIMEOUT
exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
# Define ATS and configure
ts = Test.MakeATSProcess("ts", command="traffic_manager", select_ports=True)

Test.testName = "regex_revalidate"
Test.Setup.Copy("metrics.sh")

# default root
request_header_0 = {"headers":
"GET / HTTP/1.1\r\n" +
Expand Down Expand Up @@ -264,3 +267,11 @@
tr.Processes.Default.ReturnCode = 0
tr.Processes.Default.Streams.stdout = "gold/regex_reval-stale.gold"
tr.StillRunningAfter = ts

# 12 Stats check
tr = Test.AddTestRun("Check stats")
tr.DelayStart = 5
tr.Processes.Default.Command = "bash -c ./metrics.sh"
tr.Processes.Default.Env = ts.Env
tr.Processes.Default.ReturnCode = 0
tr.StillRunningAfter = ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
# Define ATS and configure
ts = Test.MakeATSProcess("ts", command="traffic_manager")

# **testname is required**
#testName = "regex_reval"
Test.testName = "regex_revalidate_miss"
Test.Setup.Copy("metrics_miss.sh")

# default root
request_header_0 = {"headers":
Expand Down Expand Up @@ -220,11 +220,19 @@
ps.TimeOut = 5
tr.TimeOut = 5

# 8 Test - Cache stale
# 10 Test - Cache stale
tr = Test.AddTestRun("Cache stale path1")
ps = tr.Processes.Default
tr.DelayStart = 5
ps.Command = curl_and_args + ' http://ats/path1'
ps.ReturnCode = 0
ps.Streams.stdout.Content = Testers.ContainsExpression("X-Cache: hit-fresh", "expected cache hit response")
tr.StillRunningAfter = ts

# 11 Stats check
tr = Test.AddTestRun("Check stats")
tr.DelayStart = 5
tr.Processes.Default.Command = "bash -c ./metrics_miss.sh"
tr.Processes.Default.Env = ts.Env
tr.Processes.Default.ReturnCode = 0
tr.StillRunningAfter = ts

0 comments on commit f3fb74e

Please sign in to comment.