ClickHouse

Форк
0
/
ServerUUID.cpp 
60 строк · 1.6 Кб
1
#include <Core/ServerUUID.h>
2
#include <IO/ReadBufferFromFile.h>
3
#include <IO/WriteBufferFromFile.h>
4
#include <IO/ReadHelpers.h>
5
#include <IO/WriteHelpers.h>
6
#include <Common/logger_useful.h>
7

8
namespace DB
9
{
10

11
namespace ErrorCodes
12
{
13
    extern const int CANNOT_CREATE_FILE;
14
}
15

16
void ServerUUID::load(const fs::path & server_uuid_file, Poco::Logger * log)
17
{
18
    server_uuid = loadServerUUID(server_uuid_file, log);
19
}
20

21
UUID loadServerUUID(const fs::path & server_uuid_file, Poco::Logger * log)
22
{
23
    /// Write a uuid file containing a unique uuid if the file doesn't already exist during server start.
24

25
    if (fs::exists(server_uuid_file))
26
    {
27
        try
28
        {
29
            UUID uuid;
30
            ReadBufferFromFile in(server_uuid_file);
31
            readUUIDText(uuid, in);
32
            assertEOF(in);
33
            return uuid;
34
        }
35
        catch (...)
36
        {
37
            /// As for now it's ok to just overwrite it, because persistency in not essential.
38
            LOG_ERROR(log, "Cannot read server UUID from file {}: {}. Will overwrite it",
39
                      server_uuid_file.string(), getCurrentExceptionMessage(true));
40
        }
41
    }
42

43
    try
44
    {
45
        UUID new_uuid = UUIDHelpers::generateV4();
46
        auto uuid_str = toString(new_uuid);
47
        WriteBufferFromFile out(server_uuid_file);
48
        out.write(uuid_str.data(), uuid_str.size());
49
        out.sync();
50
        out.finalize();
51
        return new_uuid;
52
    }
53
    catch (...)
54
    {
55
        throw Exception(ErrorCodes::CANNOT_CREATE_FILE, "Caught Exception {} while writing the Server UUID file {}",
56
                        getCurrentExceptionMessage(false), server_uuid_file.string());
57
    }
58
}
59

60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.