Copilot commented on code in PR #13807:
URL: https://github.com/apache/trafficserver/pull/13807#discussion_r4476608749


##########
plugins/experimental/jax_fingerprint/config.h:
##########
@@ -52,21 +57,87 @@ struct StringHash {
   }
 };
 
+/** Settings that can be changed by reloading the configuration file at 
runtime. */
+struct RuntimeSettings {
+  Mode                                                         mode            
= Mode::OVERWRITE;
+  std::string                                                  header_name     
= "";
+  std::string                                                  via_header_name 
= "";
+  std::unordered_set<std::string, StringHash, std::equal_to<>> servernames;
+};
+
 struct PluginConfig {
   PluginConfig() { Dbg(dbg_ctl, "New config (%p) has been created", this); }
   ~PluginConfig() { Dbg(dbg_ctl, "Config for (%p) has been deleted", this); }
 
-  PluginType    plugin_type     = PluginType::GLOBAL;
-  Mode          mode            = Mode::OVERWRITE;
-  struct Method method          = {"uninitialized", 
Method::Type::CONNECTION_BASED, nullptr, nullptr};
-  std::string   header_name     = "";
-  std::string   via_header_name = "";
-  std::string   log_filename    = "";
-  std::string   log_symbol      = "";
-  std::string   export_name     = "";
-  int           user_arg_index  = -1;
-  TSCont        handler         = nullptr; // For remap plugin
-  bool          standalone      = false;
-  std::unordered_set<std::string, StringHash, std::equal_to<>> servernames;
-  TSTextLogObject                                              log_handle = 
nullptr;
+  PluginType      plugin_type    = PluginType::GLOBAL;
+  struct Method   method         = {"uninitialized", 
Method::Type::CONNECTION_BASED, nullptr, nullptr};
+  std::string     log_filename   = "";
+  std::string     log_symbol     = "";
+  std::string     export_name    = "";
+  int             user_arg_index = -1;
+  TSCont          handler        = nullptr; // For remap plugin
+  bool            standalone     = false;
+  TSTextLogObject log_handle     = nullptr;
+
+  /** Get a snapshot of the runtime settings that stays valid while it is 
held. */
+  std::shared_ptr<RuntimeSettings const>
+  get_settings() const
+  {
+    std::shared_lock lock(_settings_mutex);
+    return _settings;
+  }
+
+  void
+  set_settings(std::shared_ptr<RuntimeSettings const> settings)
+  {
+    std::unique_lock lock(_settings_mutex);
+    _settings = std::move(settings);
+  }
+
+private:
+  mutable std::shared_mutex              _settings_mutex;
+  std::shared_ptr<RuntimeSettings const> _settings = 
std::make_shared<RuntimeSettings const>();

Review Comment:
   get_settings() is on the hot path (per request / per hook) and takes a 
shared_mutex lock each time. Since reloads are rare and this is essentially an 
RCU pattern, consider making `_settings` an atomically-loadable shared_ptr 
(e.g., `std::atomic<std::shared_ptr<const RuntimeSettings>>` where available, 
or `std::atomic_load/std::atomic_store` free functions) to avoid lock 
contention and reduce per-transaction overhead.



##########
plugins/experimental/jax_fingerprint/plugin.cc:
##########
@@ -459,88 +435,80 @@ TSRemapInit(TSRemapInterface *api_info, char *errbuf, int 
errbuf_size)
 }
 
 TSReturnCode
-TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf 
ATS_UNUSED */, int /* errbuf_size ATS_UNUSED */)
+TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int 
errbuf_size)
 {
   Dbg(dbg_ctl, "New instance for client matching %s to %s", argv[0], argv[1]);
-  auto owned_config         = std::make_unique<PluginConfig>();
-  owned_config->plugin_type = PluginType::REMAP;
 
-  // Parse parameters
-  if (!read_config_option(argc - 1, const_cast<const char **>(argv + 1), 
*owned_config)) {
-    Dbg(dbg_ctl, "Bad arguments");
+  // Skip the "from" URL so that the arguments begin with the "to" URL in 
place of a program name.
+  auto        configs = std::make_unique<PluginConfigs>();
+  std::string config_filename;
+  if (!load_config(argc - 1, const_cast<char const **>(argv + 1), 
PluginType::REMAP, *configs, config_filename)) {
+    snprintf(errbuf, errbuf_size, "[%s] Failed to load configuration from %s", 
PLUGIN_NAME,
+             config_filename.empty() ? "the @pparam arguments" : 
config_filename.c_str());
     return TS_ERROR;
   }
 
-  if (!owned_config->log_symbol.empty()) {
-    TSError("[%s] --log-field is not supported in remap.config. Use it in 
plugin.config instead.", PLUGIN_NAME);
+  if (!prepare_configs(*configs)) {
     return TS_ERROR;
   }
 
-  // Create a log file
-  if (!owned_config->log_filename.empty()) {
-    if (!create_log_file(owned_config->log_filename, 
owned_config->log_handle)) {
-      TSError("[%s] Failed to create log.", PLUGIN_NAME);
-      return TS_ERROR;
-    } else {
-      Dbg(dbg_ctl, "Created log file.");
-    }
-  }
-
-  if (reserve_user_arg(*owned_config) == TS_ERROR) {
-    TSError("[%s] Failed to reserve user arg index.", PLUGIN_NAME);
-    return TS_ERROR;
+  if (!config_filename.empty()) {
+    register_remap_config_file(config_filename);
   }
 
-  // Past here the instance handle owns the configuration and 
TSRemapDeleteInstance releases it.
-  PluginConfig *config = owned_config.release();
-
-  // Create continuation
-  if (config->standalone) {
-    Dbg(dbg_ctl, "Standalone mode. Adding hooks.");
-    config->handler = TSContCreate(main_handler, nullptr);
-    if (config->method.on_client_hello) {
-      TSHttpHookAdd(TS_SSL_CLIENT_HELLO_HOOK, config->handler);
-    }
-    if (config->method.type == Method::Type::CONNECTION_BASED) {
-      TSHttpHookAdd(TS_VCONN_CLOSE_HOOK, config->handler);
-    } else {
-      TSHttpHookAdd(TS_HTTP_TXN_CLOSE_HOOK, config->handler);
+  for (auto const &config : *configs) {
+    if (config->standalone) {
+      Dbg(dbg_ctl, "Standalone mode. Adding hooks for %s.", 
config->method.name.data());
+      config->handler = TSContCreate(main_handler, nullptr);
+      if (config->method.on_client_hello) {
+        TSHttpHookAdd(TS_SSL_CLIENT_HELLO_HOOK, config->handler);
+      }
+      if (config->method.type == Method::Type::CONNECTION_BASED) {
+        TSHttpHookAdd(TS_VCONN_CLOSE_HOOK, config->handler);
+      } else {
+        TSHttpHookAdd(TS_HTTP_TXN_CLOSE_HOOK, config->handler);
+      }
+      TSContDataSet(config->handler, config.get());
     }
-    TSContDataSet(config->handler, config);
   }
 
-  *ih = static_cast<void *>(config);
+  // Past here the instance handle owns the configurations and 
TSRemapDeleteInstance releases them.
+  *ih = static_cast<void *>(configs.release());
 
   return TS_SUCCESS;
 }
 
 TSRemapStatus
 TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
 {
-  auto config = static_cast<PluginConfig *>(ih);
+  auto configs = static_cast<PluginConfigs *>(ih);
 
-  if (!config || !rri) {
+  if (!configs || !rri) {
     TSError("[%s] Invalid private data or RRI or handler.", PLUGIN_NAME);
     return TSREMAP_NO_REMAP;
   }
 
-  handle_read_request_hdr(rh, *config);
+  for (auto const &config : *configs) {
+    handle_read_request_hdr(rh, *config);
+  }
 
   return TSREMAP_NO_REMAP;
 }
 
 void
 TSRemapDeleteInstance(void *ih)
 {
-  auto config = static_cast<PluginConfig *>(ih);
-  if (config->handler) {
-    // Destroying the continuation here causes a crash after remap.config 
reload
-    // Instead of destroying, make it NOP.
-    // TSContDestroy(config->handler);
-    TSContDataSet(config->handler, nullptr);
-  }
-  if (config->log_handle) {
-    flush_log_file(config->log_handle);
+  auto configs = static_cast<PluginConfigs *>(ih);
+  for (auto const &config : *configs) {
+    if (config->handler) {
+      // Destroying the continuation here causes a crash after remap.config 
reload
+      // Instead of destroying, make it NOP.
+      // TSContDestroy(config->handler);
+      TSContDataSet(config->handler, nullptr);
+    }
+    if (config->log_handle) {
+      flush_log_file(config->log_handle);

Review Comment:
   Not destroying remap standalone continuations means every remap.config 
reload can leave behind additional hooks/continuations (even if NOP), and this 
PR’s multi-entry remap configs can multiply that growth. This can degrade 
performance over time due to longer hook chains and increased memory usage. A 
concrete mitigation is to avoid creating one TSCont per fingerprint entry for 
remap-standalone mode (use a single TSCont that dispatches across the entry 
list), and/or address the underlying crash so TSContDestroy becomes safe on 
reload.



##########
tests/uranium_tests/pluginTest/jax_fingerprint/test_jax_fingerprint_invalid_config.py:
##########
@@ -0,0 +1,176 @@
+#  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.
+
+import re
+
+import pytest
+
+from tools.uranium.services import ATSFactory, wait_for_file_lines
+
+CASES = [
+    (
+        'Reject unknown configuration keys',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA4H
+      standalone: true
+      via-header: x-jax-via
+""",
+        "Unknown key 'via-header' in fingerprint entry",
+    ),
+    (
+        'Reject duplicate fingerprint settings',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA4H
+      standalone: false
+      standalone: true
+""",
+        "Duplicate key 'standalone' in fingerprint entry",
+    ),
+    (
+        'Reject duplicate fingerprints lists',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA4H
+      standalone: true
+  fingerprints:
+    - method: JA3
+""",
+        "Duplicate key 'fingerprints' in jax_fingerprint",
+    ),
+    (
+        'Reject empty server names',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA4
+      servernames:
+        - ""
+""",
+        "Each 'servernames' entry must be a non-empty server name",
+    ),
+    (
+        'Reject servernames for request-based methods',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA4H
+      standalone: true
+      servernames:
+        - abc.example
+""",
+        "'servernames' is not supported for JA4H",
+    ),
+    (
+        'Reject a method listed twice for one registry',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA4
+      standalone: true
+      header: x-ja4-a
+    - method: JA4
+      standalone: true
+      header: x-ja4-b
+      export: jax_fingerprint
+""",
+        "method JA4 is listed more than once for registry 'jax_fingerprint'",
+    ),
+    (
+        'Reject a log_field listed twice',
+        """
+jax_fingerprint:
+  fingerprints:
+    - method: JA3
+      log_field: jaxfp
+    - method: JA4
+      log_field: jaxfp
+""",
+        "log_field 'jaxfp' is listed more than once",
+    ),
+]
+
+
[email protected]("name,config,expected_error", CASES, ids=[case[0] for 
case in CASES])
+def test_jax_fingerprint_invalid_config(ats_factory: ATSFactory, name: str, 
config: str, expected_error: str) -> None:
+    """Reject ineffective configuration without crashing the proxy.
+
+    :param ats_factory: Factory owning the proxy.
+    :param name: Description of the invalid input.
+    :param config: Invalid YAML content, including duplicate keys when 
relevant.
+    :param expected_error: Required diagnostic regular expression.
+    """
+    ats = ats_factory.create("ts", enable_cache=False)
+    if not ats.plugin_exists("jax_fingerprint.so"):
+        pytest.skip("jax_fingerprint.so is required")
+    ats.write_config_file("jax_fingerprint.yaml", config)
+    ats.plugin_config.add_line("jax_fingerprint.so jax_fingerprint.yaml")
+    failed = r"Failed to load configuration from .*jax_fingerprint\.yaml"
+    context_error = (
+        "'jax_fingerprint' must be a map containing 'fingerprints'"
+        if name == "Reject duplicate fingerprints lists" else "invalid 
fingerprint entry at line 4")
+    for expression in (expected_error, failed, 
re.escape(f"{ats.config_directory / 'jax_fingerprint.yaml'}: {context_error}")):

Review Comment:
   The test hard-codes an exact YAML line number (`line 4`) in the expected 
diagnostic context. YAML parser mark/line reporting can vary with leading 
newlines/indentation and across yaml-cpp versions, making this assertion 
brittle. Prefer matching a less specific regex (e.g., `invalid fingerprint 
entry at line \\d+`) or validating only the stable portion of the message (file 
path + 'invalid fingerprint entry').



##########
plugins/experimental/jax_fingerprint/plugin.cc:
##########
@@ -27,195 +27,187 @@
 #include "header.h"
 #include "log.h"
 
-#ifdef ENABLE_JAX_METHOD_JA4
-#include "ja4/method.h"
-#endif
-#ifdef ENABLE_JAX_METHOD_JA4H
-#include "ja4h/method.h"
-#endif
-#ifdef ENABLE_JAX_METHOD_JA3
-#include "ja3/method.h"
-#endif
-
 #include <ts/apidefs.h>
 #include <ts/ts.h>
 #include <ts/remap.h>
 #include <ts/remap_version.h>
 
-#include <getopt.h>
-
-#include <cstddef>
-#include <cstdint>
 #include <cstdio>
-#include <cstring>
 #include <memory>
 #include <string>
 #include <string_view>
+#include <vector>
 #include <version>
 
 DbgCtl dbg_ctl{PLUGIN_NAME};
 
 namespace
 {
-constexpr Method const *METHODS[] = {
-#ifdef ENABLE_JAX_METHOD_JA4
-  &ja4::method,
-#endif
-#ifdef ENABLE_JAX_METHOD_JA4H
-  &ja4h::method,
-#endif
-#ifdef ENABLE_JAX_METHOD_JA3
-  &ja3::method,
-#endif
-};
-} // namespace
+bool
+prepare_config(PluginConfig &config)
+{
+  if (!config.log_filename.empty()) {
+    if (!create_log_file(config.log_filename, config.log_handle)) {
+      TSError("[%s] Failed to create log.", PLUGIN_NAME);

Review Comment:
   The error message on log creation failure loses the actionable detail (which 
log file path was attempted). Including `config.log_filename` in the message 
would make diagnostics much faster (especially now that multiple fingerprint 
entries/config files can exist).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to