You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "serrislew (via GitHub)" <gi...@apache.org> on 2023/08/07 17:27:11 UTC
[GitHub] [trafficserver] serrislew commented on a diff in pull request #10147: Wasm plugin: Upgrade to the latest proxy-wasm-cpp-host and remove reference to WAVM
serrislew commented on code in PR #10147:
URL: https://github.com/apache/trafficserver/pull/10147#discussion_r1286188921
##########
plugins/experimental/wasm/lib/src/wasm.cc:
##########
@@ -23,54 +23,75 @@
#include <limits>
#include <memory>
#include <mutex>
+#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
-#include <openssl/sha.h>
-
#include "include/proxy-wasm/bytecode_util.h"
#include "include/proxy-wasm/signature_util.h"
#include "include/proxy-wasm/vm_id_handle.h"
+#include "src/hash.h"
namespace proxy_wasm {
namespace {
-// Map from Wasm Key to the local Wasm instance.
+// Map from Wasm key to the thread-local Wasm instance.
thread_local std::unordered_map<std::string, std::weak_ptr<WasmHandleBase>> local_wasms;
+// Wasm key queue to track stale entries in `local_wasms`.
+thread_local std::queue<std::string> local_wasms_keys;
+
+// Map from plugin key to the thread-local plugin instance.
thread_local std::unordered_map<std::string, std::weak_ptr<PluginHandleBase>> local_plugins;
+// Plugin key queue to track stale entries in `local_plugins`.
+thread_local std::queue<std::string> local_plugins_keys;
+
+// Check no more than `MAX_LOCAL_CACHE_GC_CHUNK_SIZE` cache entries at a time during stale entries
+// cleanup.
+const size_t MAX_LOCAL_CACHE_GC_CHUNK_SIZE = 64;
+
// Map from Wasm Key to the base Wasm instance, using a pointer to avoid the initialization fiasco.
std::mutex base_wasms_mutex;
std::unordered_map<std::string, std::weak_ptr<WasmHandleBase>> *base_wasms = nullptr;
-std::vector<uint8_t> Sha256(const std::vector<std::string_view> &parts) {
- uint8_t sha256[SHA256_DIGEST_LENGTH];
- SHA256_CTX sha_ctx;
- SHA256_Init(&sha_ctx);
- for (auto part : parts) {
- SHA256_Update(&sha_ctx, part.data(), part.size());
- }
- SHA256_Final(sha256, &sha_ctx);
- return std::vector<uint8_t>(std::begin(sha256), std::end(sha256));
+void cacheLocalWasm(const std::string &key, const std::shared_ptr<WasmHandleBase> &wasm_handle) {
+ local_wasms[key] = wasm_handle;
+ local_wasms_keys.emplace(key);
+}
+
+void cacheLocalPlugin(const std::string &key,
+ const std::shared_ptr<PluginHandleBase> &plugin_handle) {
+ local_plugins[key] = plugin_handle;
+ local_plugins_keys.emplace(key);
}
-std::string BytesToHex(const std::vector<uint8_t> &bytes) {
- static const char *const hex = "0123456789ABCDEF";
- std::string result;
- result.reserve(bytes.size() * 2);
- for (auto byte : bytes) {
- result.push_back(hex[byte >> 4]);
- result.push_back(hex[byte & 0xf]);
+template <class T>
+void removeStaleLocalCacheEntries(std::unordered_map<std::string, std::weak_ptr<T>> &cache,
+ std::queue<std::string> &keys) {
+ auto num_keys_to_check = std::min(MAX_LOCAL_CACHE_GC_CHUNK_SIZE, keys.size());
+ for (size_t i = 0; i < num_keys_to_check; i++) {
+ std::string key(keys.front());
+ keys.pop();
+
+ const auto it = cache.find(key);
+ if (it == cache.end()) {
+ continue;
+ }
+
+ if (it->second.expired()) {
+ cache.erase(it);
+ } else {
+ keys.push(std::move(key));
+ }
}
- return result;
}
} // namespace
std::string makeVmKey(std::string_view vm_id, std::string_view vm_configuration,
std::string_view code) {
- return BytesToHex(Sha256({vm_id, vm_configuration, code}));
+ return Sha256String({vm_id, "||", vm_configuration, "||", code});
Review Comment:
Are these "| |" intentional to add to the hash key? Since it wasn't originally included?
--
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: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org