forked from yandex/porto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.cpp
141 lines (115 loc) · 4.1 KB
/
context.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "context.hpp"
#include "epoll.hpp"
#include "config.hpp"
#include "cgroup.hpp"
#include "subsystem.hpp"
#include "event.hpp"
#include "holder.hpp"
#include "qdisc.hpp"
#include "volume.hpp"
#include "util/log.hpp"
#include "util/unix.hpp"
#include "util/mount.hpp"
TContext::TContext() {
Storage = std::make_shared<TKeyValueStorage>(TMount("tmpfs", config().keyval().file().path(), "tmpfs", { config().keyval().size() }));
VolumeStorage = std::make_shared<TKeyValueStorage>(TMount("tmpfs", config().volumes().keyval().file().path(), "tmpfs", { config().volumes().keyval().size() }));
Net = std::make_shared<TNetwork>();
EpollLoop = std::make_shared<TEpollLoop>();
Cholder = std::make_shared<TContainerHolder>(EpollLoop, Net, Storage);
Queue = std::make_shared<TEventQueue>(Cholder);
Cholder->Queue = Queue;
Vholder = std::make_shared<TVolumeHolder>(VolumeStorage);
}
TError TContext::CreateDaemonCgs() {
DaemonCgs[memorySubsystem] = memorySubsystem->GetRootCgroup()->GetChild(PORTO_DAEMON_CGROUP);
DaemonCgs[cpuacctSubsystem] = cpuacctSubsystem->GetRootCgroup()->GetChild(PORTO_DAEMON_CGROUP);
for (auto cg : DaemonCgs) {
TError error = cg.second->Create();
if (error)
return error;
// portod-slave
error = cg.second->Attach(GetPid());
if (error)
return error;
// portod master
error = cg.second->Attach(GetPPid());
if (error)
return error;
}
if (!config().daemon().debug()) {
TError error = memorySubsystem->SetLimit(DaemonCgs[memorySubsystem], config().daemon().memory_limit());
if (error)
return error;
}
return TError::Success();
}
TError TContext::Initialize() {
TError error;
error = EpollLoop->Create();
if (error)
return error;
// don't fail, try to recover anyway
error = Storage->MountTmpfs();
if (error)
L_ERR() << "Can't create key-value storage, skipping recovery: " << error << std::endl;
error = VolumeStorage->MountTmpfs();
if (error)
L_ERR() << "Can't create key-value storage, skipping recovery: " << error << std::endl;
if (config().network().enabled()) {
if (config().network().dynamic_ifaces()) {
NetEvt = std::make_shared<TNl>();
error = NetEvt->Connect();
if (error) {
L_ERR() << "Can't connect netlink events socket: " << error << std::endl;
return error;
}
error = NetEvt->SubscribeToLinkUpdates();
if (error) {
L_ERR() << "Can't subscribe netlink socket to events: " << error << std::endl;
return error;
}
}
TError error = Net->Prepare();
if (error) {
L_ERR() << "Can't prepare network: " << error << std::endl;
return error;
}
for (auto &link : Net->GetLinks())
L() << "Using " << link->GetAlias() << " interface" << std::endl;
}
auto holder_lock = Cholder->ScopedLock();
error = Cholder->CreateRoot(holder_lock);
if (error) {
L_ERR() << "Can't create root container: " << error << std::endl;
return error;
}
error = Cholder->CreatePortoRoot(holder_lock);
if (error) {
L_ERR() << "Can't create porto root container: " << error << std::endl;
return error;
}
error = CreateDaemonCgs();
if (error)
return error;
return TError::Success();
}
TError TContext::Destroy() {
TError error;
if (NetEvt)
NetEvt->Disconnect();
{
auto holder_lock = Cholder->ScopedLock();
Cholder->DestroyRoot(holder_lock);
Vholder->Destroy();
}
error = Storage->Destroy();
if (error)
L_ERR() << "Can't destroy key-value storage: " << error << std::endl;
error = VolumeStorage->Destroy();
if (error)
L_ERR() << "Can't destroy volume key-value storage: " << error << std::endl;
error = Net->Destroy();
if (error)
L_ERR() << "Can't destroy network: " << error << std::endl;
return TError::Success();
}