]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Further efforts to make a more consistent architecture
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 23 Apr 2022 19:02:25 +0000 (20:02 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 23 Apr 2022 19:02:25 +0000 (20:02 +0100)
src/libserver/rspamd_symcache.h
src/libserver/symcache/symcache_c.cxx
src/libserver/symcache/symcache_impl.cxx
src/libserver/symcache/symcache_internal.hxx
src/libserver/symcache/symcache_runtime.cxx
src/libserver/symcache/symcache_runtime.hxx

index 0ef76c832d2a1d1f3ded73e443ba6929c73330f4..e47ea3eddf8a07a69b9c909abefc3e20b6a09f57 100644 (file)
@@ -249,21 +249,6 @@ struct rspamd_abstract_callback_data *rspamd_symcache_get_cbdata (
 const gchar *rspamd_symcache_get_parent (struct rspamd_symcache *cache,
                                                                                 const gchar *symbol);
 
-/**
- * Adds flags to a symbol
- * @param cache
- * @param symbol
- * @param flags
- * @return
- */
-gboolean rspamd_symcache_add_symbol_flags (struct rspamd_symcache *cache,
-                                                                                  const gchar *symbol,
-                                                                                  guint flags);
-
-gboolean rspamd_symcache_set_symbol_flags (struct rspamd_symcache *cache,
-                                                                                  const gchar *symbol,
-                                                                                  guint flags);
-
 guint rspamd_symcache_get_symbol_flags (struct rspamd_symcache *cache,
                                                                                const gchar *symbol);
 
index abedba23f6d82e9e8c33f92d181f0d3537a27bda..cb8a8af144c523b4004fb8f6af4b9e9ccd7f74ae 100644 (file)
@@ -309,6 +309,17 @@ rspamd_symcache_enable_symbol (struct rspamd_task *task,
        return cache_runtime->enable_symbol(task, *real_cache, symbol);
 }
 
+gboolean
+rspamd_symcache_is_checked (struct rspamd_task *task,
+                                                       struct rspamd_symcache *cache,
+                                                       const gchar *symbol)
+{
+       auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
+       auto *real_cache = C_API_SYMCACHE(cache);
+
+       return cache_runtime->is_symbol_checked(*real_cache, symbol);
+}
+
 gboolean
 rspamd_symcache_process_settings (struct rspamd_task *task,
                                                                  struct rspamd_symcache *cache)
index 99aae3df8a1bc00429aee2aa025e4a0a2e5c0755..da5a6145e5d6c46bd53277d82588cc9e2c097c2c 100644 (file)
@@ -420,9 +420,11 @@ auto symcache::resort() -> void
        auto ord = std::make_shared<order_generation>(filters.size(), cur_order_gen);
 
        for (auto &it: filters) {
-               total_hits += it->st->total_hits;
-               it->order = 0;
-               ord->d.emplace_back(it);
+               if (it) {
+                       total_hits += it->st->total_hits;
+                       it->order = 0;
+                       ord->d.emplace_back(it);
+               }
        }
 
        enum class tsort_mask {
@@ -486,8 +488,9 @@ auto symcache::resort() -> void
         * Topological sort
         */
        total_hits = 0;
+       auto used_items = ord->d.size();
 
-       for (const auto &it: filters) {
+       for (const auto &it: ord->d) {
                if (it->order == 0) {
                        tsort_visit(it.get(), 0, tsort_visit);
                }
@@ -542,6 +545,36 @@ auto symcache::resort() -> void
        };
 
        std::stable_sort(std::begin(ord->d), std::end(ord->d), cache_order_cmp);
+       /*
+        * Here lives some ugly legacy!
+        * We have several filters classes, connfilters, prefilters, filters... etc
+        *
+        * Our order is meaningful merely for filters, but we have to add other classes
+        * to understand if those symbols are checked or disabled.
+        * We can disable symbols for almost everything but not for virtual symbols.
+        * The rule of thumb is that if a symbol has explicit parent, then it is a
+        * virtual symbol that follows it's special rules
+        */
+
+       /*
+        * We enrich ord with all other symbol types without any sorting,
+        * as it is done in another place
+        */
+       constexpr auto append_items_vec = [](const auto &vec, auto &out) {
+               for (const auto &it : vec) {
+                       if (it) {
+                               out.emplace_back(it);
+                       }
+               }
+       };
+
+       append_items_vec(connfilters, ord->d);
+       append_items_vec(prefilters, ord->d);
+       append_items_vec(postfilters, ord->d);
+       append_items_vec(idempotent, ord->d);
+       append_items_vec(composites, ord->d);
+       append_items_vec(classifiers, ord->d);
+
        /* After sorting is done, we can assign all elements in the by_symbol hash */
        for (auto i = 0; i < ord->size(); i ++) {
                const auto &it = ord->d[i];
@@ -602,8 +635,8 @@ auto symcache::add_symbol_with_callback(std::string_view name,
                        real_type_pair.first, real_type_pair.second);
 
        items_by_symbol[item->get_name()] = item;
+       get_item_specific_vector(*item).push_back(item);
        items_by_id.push_back(item);
-       used_items++;
 
        if (!(real_type_pair.second & SYMBOL_TYPE_NOSTAT)) {
                cksum = t1ha(name.data(), name.size(), cksum);
@@ -635,14 +668,15 @@ auto symcache::add_virtual_symbol(std::string_view name, int parent_id, enum rsp
                return -1;
        }
 
-       auto id = virtual_symbols.size();
+       auto id = items_by_id.size();
 
        auto item = cache_item::create_with_virtual(static_pool,
                        id,
                        std::string{name},
                        parent_id, real_type_pair.first, real_type_pair.second);
        items_by_symbol[item->get_name()] = item;
-       virtual_symbols.push_back(item);
+       get_item_specific_vector(*item).push_back(item);
+       items_by_id.push_back(item);
 
        return id;
 }
@@ -903,4 +937,29 @@ auto symcache::maybe_resort() -> bool
        return false;
 }
 
+auto
+symcache::get_item_specific_vector(const cache_item &it) -> symcache::items_ptr_vec &
+{
+       switch (it.get_type()) {
+       case symcache_item_type::CONNFILTER:
+               return connfilters;
+       case symcache_item_type::FILTER:
+               return filters;
+       case symcache_item_type::IDEMPOTENT:
+               return idempotent;
+       case symcache_item_type::PREFILTER:
+               return prefilters;
+       case symcache_item_type::POSTFILTER:
+               return postfilters;
+       case symcache_item_type::COMPOSITE:
+               return composites;
+       case symcache_item_type::CLASSIFIER:
+               return classifiers;
+       case symcache_item_type::VIRTUAL:
+               return virtual_symbols;
+       }
+
+       RSPAMD_UNREACHABLE;
+}
+
 }
\ No newline at end of file
index 8a7e44e6246fc75a8bae0f39f2505b9e6ccab2ff..41edec53ec3afb908ce7f81932ee75bef3ffb49e 100644 (file)
@@ -129,21 +129,24 @@ public:
 
 class symcache {
 private:
+       using items_ptr_vec = std::vector<cache_item_ptr>;
        /* Map indexed by symbol name: all symbols must have unique names, so this map holds ownership */
        robin_hood::unordered_flat_map<std::string_view, cache_item_ptr> items_by_symbol;
-       std::vector<cache_item_ptr> items_by_id;
+       items_ptr_vec items_by_id;
 
        /* Items sorted into some order */
        order_generation_ptr items_by_order;
        unsigned int cur_order_gen;
 
-       std::vector<cache_item_ptr> connfilters;
-       std::vector<cache_item_ptr> prefilters;
-       std::vector<cache_item_ptr> filters;
-       std::vector<cache_item_ptr> postfilters;
-       std::vector<cache_item_ptr> composites;
-       std::vector<cache_item_ptr> idempotent;
-       std::vector<cache_item_ptr> virtual_symbols;
+       /* Specific vectors for execution/iteration */
+       items_ptr_vec connfilters;
+       items_ptr_vec prefilters;
+       items_ptr_vec filters;
+       items_ptr_vec postfilters;
+       items_ptr_vec composites;
+       items_ptr_vec idempotent;
+       items_ptr_vec classifiers;
+       items_ptr_vec virtual_symbols;
 
        /* These are stored within pointer to clean up after init */
        std::unique_ptr<std::vector<delayed_cache_dependency>> delayed_deps;
@@ -152,7 +155,6 @@ private:
        rspamd_mempool_t *static_pool;
        std::uint64_t cksum;
        double total_weight;
-       std::size_t used_items;
        std::size_t stats_symbols_count;
 
 private:
@@ -171,6 +173,7 @@ private:
        /* Internal methods */
        auto load_items() -> bool;
        auto resort() -> void;
+       auto get_item_specific_vector(const cache_item &) -> items_ptr_vec&;
        /* Helper for g_hash_table_foreach */
        static auto metric_connect_cb(void *k, void *v, void *ud) -> void;
 
index 55a0a62c441ad16aed1af977ee8e8e7650bd12d0..c86f79087bc0d3543b5c889a2ec89c96cb1df31a 100644 (file)
@@ -220,5 +220,23 @@ symcache_runtime::enable_symbol(struct rspamd_task *task, const symcache &cache,
        return false;
 }
 
+auto
+symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name) -> bool
+{
+       const auto *item = cache.get_item_by_name(name, true);
+
+       if (item != nullptr) {
+
+               auto our_id_maybe = rspamd::find_map(order->by_cache_id, item->id);
+
+               if (our_id_maybe) {
+                       auto *dyn_item = &dynamic_items[our_id_maybe.value()];
+                       return dyn_item->started;
+               }
+       }
+
+       return false;
+}
+
 }
 
index 78fc3bf7cbd7ad1545631fac37ffd2a2579d6fc9..63eda08e2b8c8c1b706ae1df087d4c235264974b 100644 (file)
@@ -101,6 +101,14 @@ public:
         * @return
         */
        auto enable_symbol(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool;
+
+       /**
+        * Checks if an item has been checked/disabled
+        * @param cache
+        * @param name
+        * @return
+        */
+       auto is_symbol_checked(const symcache &cache, std::string_view name) -> bool;
 };