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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#ifndef ICE_GRID_SERVER_I_H
#define ICE_GRID_SERVER_I_H
#include <IceUtil/Timer.h>
#include <IceGrid/Activator.h>
#include <IceGrid/Internal.h>
#include <optional>
#include <set>
#ifndef _WIN32
# include <sys/types.h> // for uid_t, gid_t
#endif
namespace IceGrid
{
class NodeI;
class ServerAdapterI;
class ServerCommand;
class DestroyCommand;
class StopCommand;
class StartCommand;
class LoadCommand;
class ServerI final : public Server, public std::enable_shared_from_this<ServerI>
{
public:
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wshadow"
#endif
enum InternalServerState
{
Loading,
Inactive,
Activating,
WaitForActivation,
ActivationTimeout,
Active,
Deactivating,
DeactivatingWaitForProcess,
Destroying,
Destroyed
};
enum ServerActivation
{
Always,
Session,
OnDemand,
Manual,
Disabled
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
ServerI(const std::shared_ptr<NodeI>&, const std::shared_ptr<ServerPrx>&,
const std::string&, const std::string&, int);
void waitForApplicationUpdateCompleted();
void startAsync(std::function<void()>, std::function<void(std::exception_ptr)>, const Ice::Current&) override;
void stopAsync(std::function<void()>, std::function<void(std::exception_ptr)>, const Ice::Current&) override;
void sendSignal(std::string, const Ice::Current&) override;
void writeMessage(std::string, int, const Ice::Current&) override;
ServerState getState(const Ice::Current&) const override;
int getPid(const Ice::Current&) const override;
void setEnabled(bool, const Ice::Current&) override;
bool isEnabled(const Ice::Current&) const override;
void setProcessAsync(std::shared_ptr<Ice::ProcessPrx>, std::function<void()>,
std::function<void(std::exception_ptr)>, const Ice::Current&) override;
long long getOffsetFromEnd(std::string, int, const Ice::Current&) const override;
bool read(std::string, long long, int, long long&, Ice::StringSeq&, const Ice::Current&) const override;
bool isAdapterActivatable(const std::string&) const;
const std::string& getId() const;
void start(ServerActivation, std::function<void()> = nullptr, std::function<void(std::exception_ptr)> = nullptr);
std::shared_ptr<ServerCommand> load(const std::shared_ptr<InternalServerDescriptor>&, const std::string&, bool,
std::function<void(const std::shared_ptr<ServerPrx> &, const AdapterPrxDict &, int, int)>,
std::function<void(std::exception_ptr)>);
bool checkUpdate(std::shared_ptr<InternalServerDescriptor>, bool, const Ice::Current&) override;
void checkRemove(bool, const Ice::Current&);
std::shared_ptr<ServerCommand> destroy(const std::string&, int, const std::string &, bool, std::function<void()>);
void adapterActivated(const std::string&);
void adapterDeactivated(const std::string&);
void activationTimedOut();
void activate();
void kill();
void deactivate();
void update();
void destroy();
void terminated(const std::string&, int);
void shutdown();
//
// A proxy to the Process facet of the real Admin object; called by the AdminFacade servant implementation
//
std::shared_ptr<Ice::ObjectPrx> getProcess() const;
PropertyDescriptorSeqDict getProperties(const std::shared_ptr<InternalServerDescriptor>&);
void updateRuntimePropertiesCallback(const std::shared_ptr<InternalServerDescriptor>&);
void updateRuntimePropertiesCallback(std::exception_ptr, const std::shared_ptr<InternalServerDescriptor>&);
private:
void updateImpl(const std::shared_ptr<InternalServerDescriptor>&);
void checkRevision(const std::string&, const std::string&, int) const;
void checkNoRestart(const std::shared_ptr<InternalServerDescriptor>&);
void checkAndUpdateUser(const std::shared_ptr<InternalServerDescriptor>&, bool);
void updateRevision(const std::string&, int);
bool checkActivation();
void checkDestroyed() const;
void disableOnFailure();
void setState(InternalServerState, const std::string& = std::string());
std::shared_ptr<ServerCommand> nextCommand();
void setStateNoSync(InternalServerState, const std::string& = std::string());
ServerState toServerState(InternalServerState) const;
ServerActivation toServerActivation(const std::string&) const;
ServerDynamicInfo getDynamicInfo() const;
std::string getFilePath(const std::string&) const;
const std::shared_ptr<NodeI> _node;
const std::shared_ptr<ServerPrx> _this;
const std::string _id;
const std::chrono::seconds _waitTime;
const std::string _serverDir;
const std::chrono::seconds _disableOnFailure;
std::shared_ptr<InternalServerDescriptor> _desc;
#ifndef _WIN32
uid_t _uid;
gid_t _gid;
#endif
InternalServerState _state;
ServerActivation _activation;
std::chrono::seconds _activationTimeout;
std::chrono::seconds _deactivationTimeout;
using ServerAdapterDict = std::map<std::string, std::shared_ptr<ServerAdapterI>> ;
ServerAdapterDict _adapters;
std::set<std::string> _serverLifetimeAdapters;
std::shared_ptr<Ice::ProcessPrx> _process;
std::set<std::string> _activatedAdapters;
std::optional<std::chrono::steady_clock::time_point> _failureTime;
ServerActivation _previousActivation;
std::shared_ptr<IceUtil::TimerTask>_timerTask;
bool _waitForReplication;
std::string _stdErrFile;
std::string _stdOutFile;
Ice::StringSeq _logs;
PropertyDescriptorSeq _properties;
std::shared_ptr<DestroyCommand> _destroy;
std::shared_ptr<StopCommand> _stop;
std::shared_ptr<LoadCommand> _load;
std::shared_ptr<StartCommand> _start;
int _pid;
mutable std::mutex _mutex;
std::condition_variable _condVar;
};
class ServerCommand
{
public:
ServerCommand(const std::shared_ptr<ServerI>&);
virtual ~ServerCommand() = default;
virtual bool canExecute(ServerI::InternalServerState) = 0;
virtual void execute() = 0;
virtual ServerI::InternalServerState nextState() = 0;
protected:
const std::shared_ptr<ServerI> _server;
};
class TimedServerCommand : public ServerCommand, public std::enable_shared_from_this<TimedServerCommand>
{
public:
TimedServerCommand(const std::shared_ptr<ServerI>&, const IceUtil::TimerPtr&, std::chrono::seconds);
virtual void timeout() = 0;
void startTimer();
void stopTimer();
private:
IceUtil::TimerPtr _timer;
std::shared_ptr<IceUtil::TimerTask> _timerTask;
std::chrono::seconds _timeout;
};
class DestroyCommand : public ServerCommand
{
public:
DestroyCommand(const std::shared_ptr<ServerI>&, bool, bool);
bool canExecute(ServerI::InternalServerState) override;
ServerI::InternalServerState nextState() override;
void execute() override;
void addCallback(std::function<void()>);
void finished();
bool loadFailure() const;
bool clearDir() const;
private:
const bool _loadFailure;
const bool _clearDir;
std::vector<std::function<void()>> _destroyCB;
};
class StopCommand : public TimedServerCommand
{
public:
StopCommand(const std::shared_ptr<ServerI>&, const IceUtil::TimerPtr&, std::chrono::seconds,
bool = true);
static bool isStopped(ServerI::InternalServerState);
bool canExecute(ServerI::InternalServerState) override;
ServerI::InternalServerState nextState() override;
void execute() override;
void timeout() override;
void addCallback(std::function<void()>, std::function<void(std::exception_ptr)>);
void failed(const std::string& reason);
void finished();
private:
std::vector<std::pair<std::function<void()>, std::function<void(std::exception_ptr)>>> _stopCB;
bool _deactivate;
};
class StartCommand : public TimedServerCommand
{
public:
using TimedServerCommand::TimedServerCommand;
bool canExecute(ServerI::InternalServerState) override;
ServerI::InternalServerState nextState() override;
void execute() override;
void timeout() override;
void addCallback(std::function<void()>, std::function<void(std::exception_ptr)>);
void failed(const std::string&);
void finished();
private:
std::vector<std::pair<std::function<void()>, std::function<void(std::exception_ptr)>>> _startCB;
};
class LoadCommand : public ServerCommand
{
public:
LoadCommand(const std::shared_ptr<ServerI>&, const std::shared_ptr<InternalServerDescriptor>&,
const std::shared_ptr<TraceLevels>&);
bool canExecute(ServerI::InternalServerState) override;
ServerI::InternalServerState nextState() override;
void execute() override;
void setUpdate(const std::shared_ptr<InternalServerDescriptor>&, bool);
bool clearDir() const;
std::shared_ptr<InternalServerDescriptor> getInternalServerDescriptor() const;
void addCallback(std::function<void(const std::shared_ptr<ServerPrx>&, const AdapterPrxDict &, int, int)>,
std::function<void(std::exception_ptr)>);
void startRuntimePropertiesUpdate(const std::shared_ptr<Ice::ObjectPrx>&);
bool finishRuntimePropertiesUpdate(const std::shared_ptr<InternalServerDescriptor>&,
const std::shared_ptr<Ice::ObjectPrx>&);
void failed(std::exception_ptr);
void finished(const std::shared_ptr<ServerPrx>&, const AdapterPrxDict&, std::chrono::seconds, std::chrono::seconds);
private:
std::vector<std::pair<std::function<void(const std::shared_ptr<ServerPrx> &, const AdapterPrxDict &, int, int)>,
std::function<void(std::exception_ptr)>>> _loadCB;
bool _clearDir;
std::shared_ptr<InternalServerDescriptor> _desc;
std::shared_ptr<InternalServerDescriptor> _runtime;
bool _updating;
std::shared_ptr<TraceLevels> _traceLevels;
};
}
#endif
|