ClickHouse/programs/library-bridge/ExternalDictionaryLibraryHandler.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

56 lines
1.4 KiB
C++
Raw Permalink Normal View History

2021-03-05 09:38:00 +00:00
#pragma once
2022-09-17 01:02:34 +00:00
#include "SharedLibrary.h"
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
#include "ExternalDictionaryLibraryUtils.h"
2021-03-05 09:38:00 +00:00
namespace DB
{
2021-03-22 14:39:17 +00:00
/// A class that manages all operations with library dictionary.
/// Every library dictionary source has its own object of this class, accessed by UUID.
class ExternalDictionaryLibraryHandler
2021-03-05 09:38:00 +00:00
{
public:
ExternalDictionaryLibraryHandler(
2021-03-24 09:23:29 +00:00
const std::string & library_path_,
const std::vector<std::string> & library_settings,
2021-03-24 19:32:31 +00:00
const Block & sample_block_,
const std::vector<std::string> & attributes_names_);
2021-03-05 09:38:00 +00:00
ExternalDictionaryLibraryHandler(const ExternalDictionaryLibraryHandler & other);
2021-03-05 09:38:00 +00:00
ExternalDictionaryLibraryHandler & operator=(const ExternalDictionaryLibraryHandler & other) = delete;
2021-05-08 15:20:40 +00:00
~ExternalDictionaryLibraryHandler();
2021-03-05 09:38:00 +00:00
2021-10-11 16:11:50 +00:00
Block loadAll();
2021-03-05 09:38:00 +00:00
2021-10-11 16:11:50 +00:00
Block loadIds(const std::vector<uint64_t> & ids);
2021-03-05 09:38:00 +00:00
2021-10-11 16:11:50 +00:00
Block loadKeys(const Columns & key_columns);
2021-03-07 15:23:20 +00:00
2021-03-05 10:43:47 +00:00
bool isModified();
bool supportsSelectiveLoad();
2021-03-24 08:41:42 +00:00
const Block & getSampleBlock() { return sample_block; }
2021-03-05 09:38:00 +00:00
private:
Block dataToBlock(ExternalDictionaryLibraryAPI::RawClickHouseLibraryTable data);
2021-03-05 09:38:00 +00:00
std::string library_path;
2021-03-24 08:41:42 +00:00
const Block sample_block;
2021-03-24 09:23:29 +00:00
std::vector<std::string> attributes_names;
SharedLibraryPtr library;
2021-03-05 09:38:00 +00:00
std::shared_ptr<CStringsHolder> settings_holder;
void * lib_data;
};
feat: implement catboost in library-bridge This commit moves the catboost model evaluation out of the server process into the library-bridge binary. This serves two goals: On the one hand, crashes / memory corruptions of the catboost library no longer affect the server. On the other hand, we can forbid loading dynamic libraries in the server (catboost was the last consumer of this functionality), thus improving security. SQL syntax: SELECT catboostEvaluate('/path/to/model.bin', FEAT_1, ..., FEAT_N) > 0 AS prediction, ACTION AS target FROM amazon_train LIMIT 10 Required configuration: <catboost_lib_path>/path/to/libcatboostmodel.so</catboost_lib_path> *** Implementation Details *** The internal protocol between the server and the library-bridge is simple: - HTTP GET on path "/extdict_ping": A ping, used during the handshake to check if the library-bridge runs. - HTTP POST on path "extdict_request" (1) Send a "catboost_GetTreeCount" request from the server to the bridge, containing a library path (e.g /home/user/libcatboost.so) and a model path (e.g. /home/user/model.bin). Rirst, this unloads the catboost library handler associated to the model path (if it was loaded), then loads the catboost library handler associated to the model path, then executes GetTreeCount() on the library handler and finally sends the result back to the server. Step (1) is called once by the server from FunctionCatBoostEvaluate::getReturnTypeImpl(). The library path handler is unloaded in the beginning because it contains state which may no longer be valid if the user runs catboost("/path/to/model.bin", ...) more than once and if "model.bin" was updated in between. (2) Send "catboost_Evaluate" from the server to the bridge, containing the model path and the features to run the interference on. Step (2) is called multiple times (once per chunk) by the server from function FunctionCatBoostEvaluate::executeImpl(). The library handler for the given model path is expected to be already loaded by Step (1). Fixes #27870
2022-08-05 07:53:06 +00:00
using ExternalDictionaryLibraryHandlerPtr = std::shared_ptr<ExternalDictionaryLibraryHandler>;
2021-03-05 09:38:00 +00:00
}