2016-02-17 09:27:27 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2016-02-24 10:58:05 -06:00
|
|
|
#include "AdminModel.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
#include "net/WebSocketHandler.hpp"
|
2021-11-18 06:08:14 -06:00
|
|
|
#include "COOLWSD.hpp"
|
2017-03-15 09:58:55 -05:00
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
class Admin;
|
|
|
|
|
2017-03-15 09:58:55 -05:00
|
|
|
/// Handle admin client's Websocket requests & replies.
|
2017-03-16 12:32:38 -05:00
|
|
|
class AdminSocketHandler : public WebSocketHandler
|
2016-03-20 05:59:32 -05:00
|
|
|
{
|
|
|
|
public:
|
2020-04-26 15:55:16 -05:00
|
|
|
/// Client connection to remote admin socket
|
2018-04-18 13:20:54 -05:00
|
|
|
AdminSocketHandler(Admin* adminManager);
|
|
|
|
|
2018-06-21 08:32:53 -05:00
|
|
|
/// Connection from remote admin socket
|
2017-03-16 12:32:38 -05:00
|
|
|
AdminSocketHandler(Admin* adminManager,
|
|
|
|
const std::weak_ptr<StreamSocket>& socket,
|
|
|
|
const Poco::Net::HTTPRequest& request);
|
2016-03-20 05:59:32 -05:00
|
|
|
|
2017-03-15 11:13:13 -05:00
|
|
|
/// Handle the initial Admin WS upgrade request.
|
|
|
|
/// @returns true if we should give this socket to the Admin poll.
|
|
|
|
static bool handleInitialRequest(const std::weak_ptr<StreamSocket> &socket,
|
2017-03-15 09:40:24 -05:00
|
|
|
const Poco::Net::HTTPRequest& request);
|
2016-03-20 05:59:32 -05:00
|
|
|
|
2018-07-09 02:11:20 -05:00
|
|
|
static void subscribeAsync(const std::shared_ptr<AdminSocketHandler>& handler);
|
2018-06-21 08:32:53 -05:00
|
|
|
|
2020-04-10 06:34:15 -05:00
|
|
|
/// Process incoming websocket messages
|
|
|
|
void handleMessage(const std::vector<char> &data) override;
|
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
private:
|
2017-03-16 12:43:33 -05:00
|
|
|
/// Sends text frames simply to authenticated clients.
|
2016-05-03 02:10:21 -05:00
|
|
|
void sendTextFrame(const std::string& message);
|
|
|
|
|
2016-03-20 05:59:32 -05:00
|
|
|
private:
|
|
|
|
Admin* _admin;
|
2016-05-03 02:10:21 -05:00
|
|
|
int _sessionId;
|
2016-07-28 07:28:40 -05:00
|
|
|
bool _isAuthenticated;
|
2016-03-20 05:59:32 -05:00
|
|
|
};
|
|
|
|
|
2022-12-21 06:18:58 -06:00
|
|
|
class MonitorSocketHandler : public AdminSocketHandler
|
2022-12-05 01:58:24 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
MonitorSocketHandler(Admin *admin, const std::string &uri);
|
|
|
|
|
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point now,
|
|
|
|
int64_t &timeoutMaxMicroS) override;
|
2022-12-21 06:18:58 -06:00
|
|
|
|
2022-12-05 01:58:24 -06:00
|
|
|
void performWrites(std::size_t capacity) override;
|
|
|
|
|
|
|
|
void onDisconnect() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _connecting;
|
|
|
|
std::string _uri;
|
|
|
|
};
|
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
class MemoryStatsTask;
|
2017-01-29 13:18:55 -06:00
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
/// An admin command processor.
|
2017-03-15 11:13:13 -05:00
|
|
|
class Admin : public SocketPoll
|
2016-02-17 09:27:27 -06:00
|
|
|
{
|
2020-04-25 01:45:43 -05:00
|
|
|
Admin(const Admin &) = delete;
|
|
|
|
Admin& operator = (const Admin &) = delete;
|
2017-03-15 09:58:55 -05:00
|
|
|
Admin();
|
2016-02-17 09:27:27 -06:00
|
|
|
public:
|
2016-04-06 07:43:44 -05:00
|
|
|
virtual ~Admin();
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
static Admin& instance()
|
|
|
|
{
|
|
|
|
static Admin admin;
|
|
|
|
return admin;
|
|
|
|
}
|
2016-02-17 09:27:27 -06:00
|
|
|
|
2018-04-17 14:47:17 -05:00
|
|
|
void start();
|
Admin: shutdown cleanly to avoid races during close.
Suspect cause for SEGV in:
std::_Rb_tree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<DocumentBroker> >, std::_Select1st<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<DocumentBroker> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<DocumentBroker> > > >::find(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
COOLWSD::closeDocument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
Admin::triggerMemoryCleanup(unsigned long)
Admin::pollingThread()
Change-Id: Id09e4db4b21cb7a11dddb867758aded4b28ff3ec
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2022-03-02 11:40:33 -06:00
|
|
|
void stop();
|
2017-03-15 11:13:13 -05:00
|
|
|
|
2022-12-05 01:58:24 -06:00
|
|
|
void startMonitors();
|
|
|
|
|
2023-06-13 05:49:46 -05:00
|
|
|
void updateMonitors(std::vector<std::pair<std::string, int>>& oldMonitors);
|
2022-12-05 01:58:24 -06:00
|
|
|
|
2023-06-13 05:49:46 -05:00
|
|
|
std::vector<std::pair<std::string, int>> getMonitorList();
|
2022-12-05 01:58:24 -06:00
|
|
|
|
2017-03-15 11:39:13 -05:00
|
|
|
/// Custom poll thread function
|
|
|
|
void pollingThread() override;
|
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
size_t getTotalMemoryUsage();
|
2017-08-29 00:47:39 -05:00
|
|
|
/// Takes into account the 'memproportion' property in config file to find the amount of memory
|
|
|
|
/// available to us.
|
2018-01-10 22:53:47 -06:00
|
|
|
size_t getTotalAvailableMemory() { return _totalAvailMemKb; }
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t getTotalCpuUsage();
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2019-11-26 07:15:38 -06:00
|
|
|
void modificationAlert(const std::string& dockey, pid_t pid, bool value);
|
2023-04-27 08:11:18 -05:00
|
|
|
|
|
|
|
void uploadedAlert(const std::string& dockey, pid_t pid, bool value);
|
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
/// Update the Admin Model.
|
|
|
|
void update(const std::string& message);
|
|
|
|
|
2016-04-14 15:29:31 -05:00
|
|
|
/// Calls with same pid will increment view count, if pid already exists
|
2019-11-26 07:15:38 -06:00
|
|
|
void addDoc(const std::string& docKey, pid_t pid, const std::string& filename,
|
2020-04-28 08:55:47 -05:00
|
|
|
const std::string& sessionId, const std::string& userName, const std::string& userId,
|
2023-05-02 08:29:40 -05:00
|
|
|
const int smapsFD, const Poco::URI& wopiSrc, bool readOnly);
|
2016-04-14 15:29:31 -05:00
|
|
|
|
|
|
|
/// Decrement view count till becomes zero after which doc is removed
|
2016-04-17 21:11:10 -05:00
|
|
|
void rmDoc(const std::string& docKey, const std::string& sessionId);
|
2016-04-14 15:29:31 -05:00
|
|
|
|
2016-04-17 21:32:58 -05:00
|
|
|
/// Remove the document with all views. Used on termination or catastrophic failure.
|
|
|
|
void rmDoc(const std::string& docKey);
|
|
|
|
|
2019-11-12 03:50:33 -06:00
|
|
|
void setForKitPid(const int forKitPid) { _forKitPid = forKitPid; _model.setForKitPid(forKitPid);}
|
2016-04-06 07:43:44 -05:00
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
/// Callers must ensure that modelMutex is acquired
|
2016-03-03 12:01:37 -06:00
|
|
|
AdminModel& getModel();
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
unsigned getMemStatsInterval();
|
|
|
|
|
|
|
|
unsigned getCpuStatsInterval();
|
|
|
|
|
2017-06-09 20:28:16 -05:00
|
|
|
unsigned getNetStatsInterval();
|
|
|
|
|
2020-11-23 05:07:44 -06:00
|
|
|
/// Returns the log levels of wsd and forkit & kits.
|
2020-05-13 08:40:05 -05:00
|
|
|
std::string getChannelLogLevels();
|
|
|
|
|
2020-11-23 05:07:44 -06:00
|
|
|
/// Sets the specified channel's log level (wsd or forkit and kits).
|
2020-12-07 04:24:06 -06:00
|
|
|
void setChannelLogLevel(const std::string& channelName, std::string level);
|
2020-05-13 08:40:05 -05:00
|
|
|
|
|
|
|
std::string getLogLines();
|
|
|
|
|
2016-03-05 14:39:35 -06:00
|
|
|
void rescheduleMemTimer(unsigned interval);
|
|
|
|
|
|
|
|
void rescheduleCpuTimer(unsigned interval);
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void updateLastActivityTime(const std::string& docKey);
|
2017-06-03 16:53:57 -05:00
|
|
|
void addBytes(const std::string& docKey, uint64_t sent, uint64_t recv);
|
2016-12-05 14:11:07 -06:00
|
|
|
|
2023-04-04 19:58:37 -05:00
|
|
|
void dumpState(std::ostream& os) const override;
|
2017-03-15 11:13:13 -05:00
|
|
|
|
2017-06-11 21:51:38 -05:00
|
|
|
const DocProcSettings& getDefDocProcSettings() const { return _defDocProcSettings; }
|
2017-08-21 09:31:42 -05:00
|
|
|
void setDefDocProcSettings(const DocProcSettings& docProcSettings, bool notifyKit)
|
2017-06-11 22:47:48 -05:00
|
|
|
{
|
|
|
|
_defDocProcSettings = docProcSettings;
|
2020-06-08 05:41:03 -05:00
|
|
|
_model.setDefDocProcSettings(docProcSettings);
|
|
|
|
_cleanupIntervalMs = _defDocProcSettings.getCleanupSettings().getCleanupInterval();
|
2017-08-21 09:31:42 -05:00
|
|
|
if (notifyKit)
|
|
|
|
notifyForkit();
|
2017-06-11 22:47:48 -05:00
|
|
|
}
|
|
|
|
|
2018-05-18 11:35:19 -05:00
|
|
|
/// Attempt a synchronous connection to a monitor with @uri @when that future comes
|
|
|
|
void scheduleMonitorConnect(const std::string &uri, std::chrono::steady_clock::time_point when);
|
|
|
|
|
2019-11-12 03:50:33 -06:00
|
|
|
void sendMetrics(const std::shared_ptr<StreamSocket>& socket, const std::shared_ptr<Poco::Net::HTTPResponse>& response);
|
|
|
|
|
|
|
|
void setViewLoadDuration(const std::string& docKey, const std::string& sessionId, std::chrono::milliseconds viewLoadDuration);
|
|
|
|
void setDocWopiDownloadDuration(const std::string& docKey, std::chrono::milliseconds wopiDownloadDuration);
|
|
|
|
void setDocWopiUploadDuration(const std::string& docKey, const std::chrono::milliseconds uploadDuration);
|
2020-04-07 16:21:17 -05:00
|
|
|
void addSegFaultCount(unsigned segFaultCount);
|
2021-03-26 11:57:08 -05:00
|
|
|
void addLostKitsTerminated(unsigned lostKitsTerminated);
|
2019-11-12 03:50:33 -06:00
|
|
|
|
|
|
|
void getMetrics(std::ostringstream &metrics);
|
|
|
|
|
2023-02-10 01:48:32 -06:00
|
|
|
// delete entry from _monitorSocket map
|
|
|
|
void deleteMonitorSocket(const std::string &uriWithoutParam);
|
|
|
|
|
2017-06-11 22:47:48 -05:00
|
|
|
private:
|
|
|
|
/// Notify Forkit of changed settings.
|
|
|
|
void notifyForkit();
|
2017-06-11 21:51:38 -05:00
|
|
|
|
2017-07-07 06:42:19 -05:00
|
|
|
/// Memory consumption has increased, start killing kits etc. till memory consumption gets back
|
|
|
|
/// under @hardModeLimit
|
|
|
|
void triggerMemoryCleanup(size_t hardModeLimit);
|
2020-05-29 06:32:04 -05:00
|
|
|
void notifyDocsMemDirtyChanged();
|
2020-06-08 05:41:03 -05:00
|
|
|
void cleanupResourceConsumingDocs();
|
2021-03-26 11:57:08 -05:00
|
|
|
void cleanupLostKits();
|
2017-07-07 06:42:19 -05:00
|
|
|
|
2018-02-11 14:28:19 -06:00
|
|
|
/// Round the interval up to multiples of MinStatsIntervalMs.
|
|
|
|
/// This is to avoid arbitrarily small intervals that hammer the server.
|
|
|
|
static int capAndRoundInterval(const unsigned interval)
|
|
|
|
{
|
|
|
|
const int value = std::max<int>(interval, MinStatsIntervalMs);
|
|
|
|
return ((value + MinStatsIntervalMs - 1) / MinStatsIntervalMs) * MinStatsIntervalMs;
|
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:54 -05:00
|
|
|
/// Synchronous connection setup to remote monitoring server
|
2018-05-18 11:35:19 -05:00
|
|
|
void connectToMonitorSync(const std::string &uri);
|
2018-04-18 13:20:54 -05:00
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
private:
|
2017-04-03 12:21:20 -05:00
|
|
|
/// The model is accessed only during startup & in
|
|
|
|
/// the Admin Poll thread.
|
2016-02-24 10:58:05 -06:00
|
|
|
AdminModel _model;
|
2016-04-07 03:27:43 -05:00
|
|
|
int _forKitPid;
|
2017-06-03 16:53:57 -05:00
|
|
|
size_t _lastTotalMemory;
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t _lastJiffies;
|
2017-06-09 20:28:16 -05:00
|
|
|
uint64_t _lastSentCount;
|
|
|
|
uint64_t _lastRecvCount;
|
2018-01-10 22:53:47 -06:00
|
|
|
size_t _totalSysMemKb;
|
|
|
|
size_t _totalAvailMemKb;
|
2020-11-23 05:07:44 -06:00
|
|
|
std::string _forkitLogLevel;
|
2016-03-05 14:39:35 -06:00
|
|
|
|
2019-01-09 01:45:46 -06:00
|
|
|
struct MonitorConnectRecord
|
|
|
|
{
|
|
|
|
void setWhen(std::chrono::steady_clock::time_point when) { _when = when; }
|
|
|
|
std::chrono::steady_clock::time_point getWhen() const { return _when; }
|
|
|
|
|
|
|
|
void setUri(const std::string& uri) { _uri = uri; }
|
|
|
|
std::string getUri() const { return _uri; }
|
|
|
|
|
2019-01-10 02:11:38 -06:00
|
|
|
private:
|
2018-05-18 11:35:19 -05:00
|
|
|
std::chrono::steady_clock::time_point _when;
|
|
|
|
std::string _uri;
|
|
|
|
};
|
|
|
|
std::vector<MonitorConnectRecord> _pendingConnects;
|
|
|
|
|
2020-05-13 08:40:05 -05:00
|
|
|
int _cpuStatsTaskIntervalMs;
|
|
|
|
int _memStatsTaskIntervalMs;
|
|
|
|
int _netStatsTaskIntervalMs;
|
2020-06-08 05:41:03 -05:00
|
|
|
size_t _cleanupIntervalMs;
|
2018-01-10 21:04:14 -06:00
|
|
|
DocProcSettings _defDocProcSettings;
|
|
|
|
|
|
|
|
// Don't update any more frequently than this since it's excessive.
|
2018-02-26 09:30:30 -06:00
|
|
|
static const int MinStatsIntervalMs;
|
|
|
|
static const int DefStatsIntervalMs;
|
2022-12-05 01:58:24 -06:00
|
|
|
|
|
|
|
// map to make sure only connection with unique monitor uri exists
|
|
|
|
std::map<std::string, std::shared_ptr<MonitorSocketHandler>> _monitorSockets;
|
2016-03-05 14:39:35 -06:00
|
|
|
};
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|