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 file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
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 <mutex>
|
|
|
|
|
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"
|
2020-04-10 06:34:15 -05:00
|
|
|
#include "LOOLWSD.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:
|
2018-06-21 08:32:53 -05:00
|
|
|
/// Client connection to remote amdin 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
|
|
|
};
|
|
|
|
|
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();
|
2017-03-15 11:13:13 -05: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
|
|
|
|
2017-05-12 08:59:01 -05:00
|
|
|
void modificationAlert(const std::string& dockey, Poco::Process::PID 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
|
2017-06-23 07:32:44 -05:00
|
|
|
void addDoc(const std::string& docKey, Poco::Process::PID pid, const std::string& filename, const std::string& sessionId, const std::string& userName, const std::string& userId);
|
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();
|
|
|
|
|
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-02-07 08:39:56 -06:00
|
|
|
void updateMemoryDirty(const std::string& docKey, int dirty);
|
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
|
|
|
|
2017-03-15 11:13:13 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
|
|
|
|
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;
|
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 sendMetricsAsync(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);
|
2019-11-12 03:50:33 -06:00
|
|
|
|
|
|
|
void getMetrics(std::ostringstream &metrics);
|
|
|
|
|
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);
|
|
|
|
|
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;
|
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;
|
|
|
|
|
2017-03-15 11:39:13 -05:00
|
|
|
std::atomic<int> _cpuStatsTaskIntervalMs;
|
2018-02-11 14:28:19 -06:00
|
|
|
std::atomic<int> _memStatsTaskIntervalMs;
|
|
|
|
std::atomic<int> _netStatsTaskIntervalMs;
|
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;
|
2016-03-05 14:39:35 -06:00
|
|
|
};
|
|
|
|
|
2016-02-17 09:27:27 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|