Repository navigation
Expand file tree
/
Copy pathServerUUID.cpp
More file actions
87 lines (77 loc) · 2.36 KB
/
Copy pathServerUUID.cpp
File metadata and controls
87 lines (77 loc) · 2.36 KB
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
86
87
#include <Core/ServerUUID.h>
#include <Core/UUID.h>
#include <Interpreters/Context.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Common/logger_useful.h>
#include <Common/ErrnoException.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_CREATE_FILE;
extern const int LOGICAL_ERROR;
}
UUID ServerUUID::get()
{
if (server_uuid == UUIDHelpers::Nil &&
(Context::getGlobalContextInstance()->getApplicationType() == Context::ApplicationType::SERVER ||
Context::getGlobalContextInstance()->getApplicationType() == Context::ApplicationType::KEEPER))
throw Exception(ErrorCodes::LOGICAL_ERROR, "ServerUUID is not initialized yet");
return server_uuid;
}
void ServerUUID::load(const fs::path & server_uuid_file, Poco::Logger * log)
{
server_uuid = loadServerUUID(server_uuid_file, log);
}
UUID loadServerUUID(const fs::path & server_uuid_file, Poco::Logger * log)
{
/// Write a uuid file containing a unique uuid if the file doesn't already exist during server start.
if (fs::exists(server_uuid_file))
{
try
{
UUID uuid;
ReadBufferFromFile in(server_uuid_file);
readUUIDText(uuid, in);
assertEOF(in);
return uuid;
}
catch (...)
{
/// As for now it's ok to just overwrite it, because persistency in not essential.
LOG_ERROR(log, "Cannot read server UUID from file {}: {}. Will overwrite it",
server_uuid_file.string(), getCurrentExceptionMessage(true));
}
}
try
{
UUID new_uuid = UUIDHelpers::generateV4();
auto uuid_str = toString(new_uuid);
WriteBufferFromFile out(server_uuid_file);
out.write(uuid_str.data(), uuid_str.size());
out.sync();
out.finalize();
return new_uuid;
}
catch (ErrnoException &)
{
throw;
}
catch (...)
{
throw Exception(ErrorCodes::CANNOT_CREATE_FILE, "Caught Exception {} while writing the Server UUID file {}",
getCurrentExceptionMessage(false), server_uuid_file.string());
}
}
void ServerUUID::set(UUID & uuid)
{
server_uuid = uuid;
}
void ServerUUID::setRandomForUnitTests()
{
server_uuid = UUIDHelpers::generateV4();
}
}