From a12cff0605cc9da29ee8d266cefb28c0c7405179 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 24 Jun 2022 14:06:21 +0200 Subject: [dnbd3] Rename/refactor RPC methods/constants --- modules-available/dnbd3/inc/dnbd3.inc.php | 13 ++++++ modules-available/dnbd3/inc/dnbd3rpc.inc.php | 65 ++++++++++++--------------- modules-available/dnbd3/inc/dnbd3util.inc.php | 8 ++-- 3 files changed, 45 insertions(+), 41 deletions(-) (limited to 'modules-available/dnbd3/inc') diff --git a/modules-available/dnbd3/inc/dnbd3.inc.php b/modules-available/dnbd3/inc/dnbd3.inc.php index 83baffbf..0c640973 100644 --- a/modules-available/dnbd3/inc/dnbd3.inc.php +++ b/modules-available/dnbd3/inc/dnbd3.inc.php @@ -26,4 +26,17 @@ class Dnbd3 { Property::set(self::PROP_NFS_FALLBACK, $bool ? 1 : 0); } + public static function getActiveServers(): array + { + $res = Database::simpleQuery('SELECT s.serverid, m.clientip, s.fixedip + FROM dnbd3_server s + LEFT JOIN machine m ON (s.machineuuid = m.machineuuid) + WHERE s.lastseen > :cutoff', ['cutoff' => time() - 310]); + $lookup = []; + foreach ($res as $row) { + $lookup[$row['fixedip'] ?? $row['clientip'] ?? ''] = $row['serverid']; + } + return $lookup; + } + } diff --git a/modules-available/dnbd3/inc/dnbd3rpc.inc.php b/modules-available/dnbd3/inc/dnbd3rpc.inc.php index 9d7ba46c..a26ae4fd 100644 --- a/modules-available/dnbd3/inc/dnbd3rpc.inc.php +++ b/modules-available/dnbd3/inc/dnbd3rpc.inc.php @@ -2,11 +2,18 @@ class Dnbd3Rpc { - const QUERY_UNREACHABLE = 1; - const QUERY_NOT_200 = 2; - const QUERY_NOT_JSON = 3; + const ERROR_UNREACHABLE = 1; + const ERROR_NOT_200 = 2; + const ERROR_NOT_JSON = 3; - private static function translateServer($server) + const QUERY_STATS = 'stats'; + const QUERY_CLIENTS = 'clients'; + const QUERY_IMAGES = 'images'; + const QUERY_SPACE = 'space'; + const QUERY_CONFIG = 'config'; + const QUERY_ALTSERVERS = 'altservers'; + + private static function translateServer(string $server): string { // Special case - local server if ($server === '') { @@ -30,44 +37,24 @@ class Dnbd3Rpc { * Query given DNBD3 server for status information. * * @param string $server server address - * @param bool $stats include general stats - * @param bool $clients include client list - * @param bool $images include image list - * @param bool $diskSpace include disk space stats - * @param bool $config get config - * @param bool $altservers list of alt servers with status - * @return int|array the queried data as an array, or false on error + * @param array $queryOptions Options to query, self::QUERY_* + * @return int|array the queried data as an array, or error code (self::ERROR_*) on error */ - public static function query($server, $stats, $clients, $images, $diskSpace = false, $config = false, $altservers = false) + public static function query(string $server, array $queryOptions) { $server = self::translateServer($server); - $url = 'http://' . $server . '/query?'; - if ($stats) { - $url .= 'q=stats&q=version&'; - } - if ($clients) { - $url .= 'q=clients&'; - } - if ($images) { - $url .= 'q=images&'; - } - if ($diskSpace) { - $url .= 'q=space&'; - } - if ($config) { - $url .= 'q=config&'; - } - if ($altservers) { - $url .= 'q=altservers&'; + $url = 'http://' . $server . '/query?q=version'; + if (!empty($queryOptions)) { + $url .= '&q=' . implode('&q=', $queryOptions); } $str = Download::asString($url, 3, $code); if ($str === false) - return self::QUERY_UNREACHABLE; + return self::ERROR_UNREACHABLE; if ($code !== 200) - return self::QUERY_NOT_200; + return self::ERROR_NOT_200; $ret = json_decode($str, true); if (!is_array($ret)) - return self::QUERY_NOT_JSON; + return self::ERROR_NOT_JSON; return $ret; } @@ -76,9 +63,9 @@ class Dnbd3Rpc { $server = self::translateServer($server); $str = Download::asString('http://' . $server . '/cachemap?id=' . $imgId, 3, $code); if ($str === false) - return self::QUERY_UNREACHABLE; + return self::ERROR_UNREACHABLE; if ($code !== 200) - return self::QUERY_NOT_200; + return self::ERROR_NOT_200; return $str; } @@ -87,17 +74,21 @@ class Dnbd3Rpc { * @param string[] $servers * @return array */ - public static function getStatsMulti(array $servers, int $timeout = 2): array + public static function getStatsMulti(array $servers, array $queryOptions = [], int $timeout = 2): array { if (empty($servers)) return []; + $extra = ''; + if (!empty($queryOptions)) { + $extra = '&q=' . implode('&q=', $queryOptions); + } $active = []; $mh = curl_multi_init(); curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, 8); curl_multi_setopt($mh, CURLMOPT_MAX_HOST_CONNECTIONS, 2); curl_multi_setopt($mh, CURLMOPT_MAX_TOTAL_CONNECTIONS, 8); foreach ($servers as $server) { - $url = 'http://' . self::translateServer($server) . '/query?q=stats'; + $url = 'http://' . self::translateServer($server) . '/query?q=version' . $extra; $res = curl_init($url); if ($res === false) { error_log("curl_init($url) failed with $res"); diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php index d9bfaeee..9aaa5432 100644 --- a/modules-available/dnbd3/inc/dnbd3util.inc.php +++ b/modules-available/dnbd3/inc/dnbd3util.inc.php @@ -64,12 +64,12 @@ class Dnbd3Util { // Now query them all $NOW = time(); foreach ($servers as $server) { - $data = Dnbd3Rpc::query($server['addr'], true, false, false, true); - if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) { + $data = Dnbd3Rpc::query($server['addr'], [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_SPACE]); + if ($data === Dnbd3Rpc::ERROR_UNREACHABLE) { $error = 'No (HTTP) reply from ' . $server['addr']; - } elseif ($data === Dnbd3Rpc::QUERY_NOT_200) { + } elseif ($data === Dnbd3Rpc::ERROR_NOT_200) { $error = 'No HTTP 200 OK from ' . $server['addr']; - } elseif ($data === Dnbd3Rpc::QUERY_NOT_JSON) { + } elseif ($data === Dnbd3Rpc::ERROR_NOT_JSON) { $error = 'Reply to status query is not JSON'; } elseif (!is_array($data) || !isset($data['runId'])) { if (is_array($data) && isset($data['errorMsg'])) { -- cgit v1.2.3-55-g7522