1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#include <Interpreters/Cache/FileCacheSettings.h>
#include <Interpreters/Cache/FileCacheFactory.h>
#include <Interpreters/Cache/FileCache.h>
#include <Common/logger_useful.h>
#include <Common/assert_cast.h>
#include <Common/filesystemHelpers.h>
#include <Disks/DiskFactory.h>
#include <Disks/ObjectStorages/Cached/CachedObjectStorage.h>
#include <Disks/ObjectStorages/DiskObjectStorage.h>
#include <Interpreters/Context.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
void registerDiskCache(DiskFactory & factory, bool /* global_skip_access_check */)
{
auto creator = [](const String & name,
const Poco::Util::AbstractConfiguration & config,
const String & config_prefix,
ContextPtr context,
const DisksMap & map) -> DiskPtr
{
auto disk_name = config.getString(config_prefix + ".disk", "");
if (disk_name.empty())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Disk Cache requires `disk` field in config");
auto disk_it = map.find(disk_name);
if (disk_it == map.end())
{
throw Exception(
ErrorCodes::BAD_ARGUMENTS,
"Cannot wrap disk `{}` with cache layer `{}`: there is no such disk (it should be initialized before cache disk)",
disk_name, name);
}
FileCacheSettings file_cache_settings;
file_cache_settings.loadFromConfig(config, config_prefix);
auto config_fs_caches_dir = context->getFilesystemCachesPath();
if (config_fs_caches_dir.empty())
{
if (fs::path(file_cache_settings.base_path).is_relative())
file_cache_settings.base_path = fs::path(context->getPath()) / "caches" / file_cache_settings.base_path;
}
else
{
if (fs::path(file_cache_settings.base_path).is_relative())
file_cache_settings.base_path = fs::path(config_fs_caches_dir) / file_cache_settings.base_path;
if (!pathStartsWith(file_cache_settings.base_path, config_fs_caches_dir))
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Filesystem cache path {} must lie inside default filesystem cache path `{}`",
file_cache_settings.base_path, config_fs_caches_dir);
}
}
auto cache = FileCacheFactory::instance().getOrCreate(name, file_cache_settings);
auto disk = disk_it->second;
if (!dynamic_cast<const DiskObjectStorage *>(disk.get()))
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Cannot wrap disk `{}` with cache layer `{}`: cached disk is allowed only on top of object storage",
disk_name, name);
auto disk_object_storage = disk->createDiskObjectStorage();
disk_object_storage->wrapWithCache(cache, file_cache_settings, name);
LOG_INFO(
&Poco::Logger::get("DiskCache"),
"Registered cached disk (`{}`) with structure: {}",
name, assert_cast<DiskObjectStorage *>(disk_object_storage.get())->getStructure());
return disk_object_storage;
};
factory.registerDiskType("cache", creator);
}
}
|