ClickHouse
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
8namespace DB
9{
10
11namespace ErrorCodes
12{
13extern const int CANNOT_CREATE_FILE;
14}
15
16void ServerUUID::load(const fs::path & server_uuid_file, Poco::Logger * log)
17{
18server_uuid = loadServerUUID(server_uuid_file, log);
19}
20
21UUID 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
25if (fs::exists(server_uuid_file))
26{
27try
28{
29UUID uuid;
30ReadBufferFromFile in(server_uuid_file);
31readUUIDText(uuid, in);
32assertEOF(in);
33return uuid;
34}
35catch (...)
36{
37/// As for now it's ok to just overwrite it, because persistency in not essential.
38LOG_ERROR(log, "Cannot read server UUID from file {}: {}. Will overwrite it",
39server_uuid_file.string(), getCurrentExceptionMessage(true));
40}
41}
42
43try
44{
45UUID new_uuid = UUIDHelpers::generateV4();
46auto uuid_str = toString(new_uuid);
47WriteBufferFromFile out(server_uuid_file);
48out.write(uuid_str.data(), uuid_str.size());
49out.sync();
50out.finalize();
51return new_uuid;
52}
53catch (...)
54{
55throw Exception(ErrorCodes::CANNOT_CREATE_FILE, "Caught Exception {} while writing the Server UUID file {}",
56getCurrentExceptionMessage(false), server_uuid_file.string());
57}
58}
59
60}
61