summaryrefslogtreecommitdiffstats
path: root/modules-available/dnbd3/inc
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/dnbd3/inc')
-rw-r--r--modules-available/dnbd3/inc/dnbd3.inc.php39
-rw-r--r--modules-available/dnbd3/inc/dnbd3rpc.inc.php147
-rw-r--r--modules-available/dnbd3/inc/dnbd3util.inc.php52
3 files changed, 159 insertions, 79 deletions
diff --git a/modules-available/dnbd3/inc/dnbd3.inc.php b/modules-available/dnbd3/inc/dnbd3.inc.php
index ccd783d9..def4e062 100644
--- a/modules-available/dnbd3/inc/dnbd3.inc.php
+++ b/modules-available/dnbd3/inc/dnbd3.inc.php
@@ -4,10 +4,11 @@ class Dnbd3 {
const PROP_ENABLED = 'dnbd3.enabled';
const PROP_NFS_FALLBACK = 'dnbd3.nfs-fallback';
+ const PROP_PREFER_LOCAL = 'dnbd3.prefer-local';
- public static function isEnabled()
+ public static function isEnabled(): bool
{
- return Property::get(self::PROP_ENABLED, 0) ? true : false;
+ return (bool)Property::get(self::PROP_ENABLED, 0);
}
public static function setEnabled($bool)
@@ -16,14 +17,44 @@ class Dnbd3 {
Trigger::mount(false, true);
}
- public static function hasNfsFallback()
+ public static function hasNfsFallback(): bool
{
- return Property::get(self::PROP_NFS_FALLBACK, 0) ? true : false;
+ return (bool)Property::get(self::PROP_NFS_FALLBACK, 0);
}
public static function setNfsFallback($bool)
{
Property::set(self::PROP_NFS_FALLBACK, $bool ? 1 : 0);
}
+ public static function preferLocal(): bool
+ {
+ return (bool)Property::get(self::PROP_PREFER_LOCAL, 0);
+ }
+
+ public static function setPreferLocal($bool)
+ {
+ Property::set(self::PROP_PREFER_LOCAL, $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' => CONFIG_DEBUG ? 0 : time() - 310]);
+ $lookup = [];
+ foreach ($res as $row) {
+ $lookup[$row['fixedip'] ?? $row['clientip'] ?? ''] = $row['serverid'];
+ }
+ return $lookup;
+ }
+
+ public static function getServer(string $serverId)
+ {
+ return Database::queryFirst('SELECT s.serverid, IFNULL(s.fixedip, m.clientip) AS clientip
+ FROM dnbd3_server s
+ LEFT JOIN machine m ON (s.machineuuid = m.machineuuid)
+ WHERE s.serverid = :id', ['id' => $serverId]);
+ }
}
diff --git a/modules-available/dnbd3/inc/dnbd3rpc.inc.php b/modules-available/dnbd3/inc/dnbd3rpc.inc.php
index 6e7480c0..f6bbf0ca 100644
--- a/modules-available/dnbd3/inc/dnbd3rpc.inc.php
+++ b/modules-available/dnbd3/inc/dnbd3rpc.inc.php
@@ -2,26 +2,25 @@
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 === '<self>') {
$server = '127.0.0.1:5003';
- } elseif (($out = Dnbd3Util::matchAddress($server))) {
- if (isset($out['v4'])) {
- $server = $out['v4'];
- } else {
- $server = '[' . $out['v6'] . ']';
- }
- if (isset($out['port'])) {
- $server .= $out['port'];
- } else {
- $server .= ':5003';
- }
+ } elseif (($out = Dnbd3Util::matchAddress($server)) !== false) {
+ $server = $out['v4'] ?? '[' . $out['v6'] . ']';
+ $server .= $out['port'] ?? ':5003';
}
return $server;
}
@@ -30,44 +29,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&';
- }
- 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,10 +55,86 @@ 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;
}
+ /**
+ * Get statistics for multiple servers at once.
+ * @param string[] $servers
+ */
+ 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=version' . $extra;
+ $res = curl_init($url);
+ if ($res === false) {
+ error_log("curl_init($url) failed");
+ continue;
+ }
+ curl_setopt_array($res, [
+ CURLOPT_CONNECTTIMEOUT => $timeout,
+ CURLOPT_TIMEOUT => $timeout,
+ CURLOPT_FOLLOWLOCATION => 0,
+ CURLOPT_ACCEPT_ENCODING => '', // Use everything libcurl supports
+ CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
+ CURLOPT_RETURNTRANSFER => 1,
+ ]);
+ $err = curl_multi_add_handle($mh, $res);
+ if ($err !== 0) {
+ error_log("curl_multi_add_handle() failed with $err");
+ curl_close($res);
+ } else {
+ $active[(int)$res] = $server;
+ }
+ }
+ // Wait
+ $running = 1;
+ $result = [];
+ $startTime = microtime(true);
+ for (;;) {
+ $ret = curl_multi_exec($mh, $running);
+ while (($info = curl_multi_info_read($mh)) !== false) {
+ if ($info['msg'] === CURLMSG_DONE) {
+ if (isset($active[(int)$info['handle']])) {
+ $server = $active[(int)$info['handle']];
+ unset($active[(int)$info['handle']]);
+ if ($info['result'] === CURLE_OK) {
+ $data = json_decode(curl_multi_getcontent($info['handle']), true);
+ if (is_array($data)) {
+ $data['ts'] = microtime(true);
+ $result[$server] = $data;
+ }
+ }
+ }
+ curl_multi_remove_handle($mh, $info['handle']);
+ curl_close($info['handle']);
+ }
+ }
+ $delay = ($startTime + $timeout) - microtime(true);
+ if ($ret !== CURLM_OK || !$running || $delay <= 0)
+ break;
+ $sret = curl_multi_select($mh, $delay);
+ if ($sret < 0) {
+ error_log("curl_multi_select returned $sret");
+ break;
+ }
+ }
+ curl_multi_close($mh);
+ return $result;
+ }
+
}
diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php
index 8e355370..314c44fe 100644
--- a/modules-available/dnbd3/inc/dnbd3util.inc.php
+++ b/modules-available/dnbd3/inc/dnbd3util.inc.php
@@ -12,7 +12,7 @@ class Dnbd3Util {
$res = Database::simpleQuery('SELECT s.serverid, s.machineuuid, s.fixedip, s.lastup, s.lastdown, m.clientip
FROM dnbd3_server s
LEFT JOIN machine m USING (machineuuid)');
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ foreach ($res as $row) {
if (!empty($row['machineuuid'])) {
$allUuids[$row['machineuuid']] = true;
}
@@ -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'])) {
@@ -108,11 +108,9 @@ class Dnbd3Util {
/**
* A client is booting that has runmode dnbd3 proxy - set config vars accordingly.
*
- * @param string $machineUuid
* @param string $mode always 'proxy'
- * @param string $modeData
*/
- public static function runmodeConfigHook($machineUuid, $mode, $modeData)
+ public static function runmodeConfigHook(string $machineUuid, string $mode, ?string $modeData)
{
$self = Property::getServerIp();
// Get all directly assigned locations
@@ -121,11 +119,11 @@ class Dnbd3Util {
WHERE machineuuid = :uuid',
array('uuid' => $machineUuid));
$assignedLocs = array();
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ foreach ($res as $row) {
$assignedLocs[] = $row['locationid'];
}
- $modeData = (array)json_decode($modeData, true) + self::defaultRunmodeConfig();
- if (!empty($assignedLocs) && isset($modeData['firewall']) && $modeData['firewall']) {
+ $modeData = (array)json_decode($modeData ?? '{}', true) + self::defaultRunmodeConfig();
+ if (!empty($assignedLocs) && ($modeData['firewall'] ?? false)) {
// Get all sub-locations too
$recursiveLocs = $assignedLocs;
$locations = Location::getLocationsAssoc();
@@ -138,13 +136,10 @@ class Dnbd3Util {
array('locs' => array_values($recursiveLocs)));
// Coalesce overlapping ranges
$floatIp = ip2long($self); // Float for 32bit php :/
- if (PHP_INT_SIZE === 4) {
- $floatIp = (float)sprintf('%u', $floatIp); // Float for 32bit php :/
- }
$ranges = [['startaddr' => $floatIp, 'endaddr' => $floatIp]];
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- settype($row['startaddr'], PHP_INT_SIZE === 4 ? 'float' : 'int');
- settype($row['endaddr'], PHP_INT_SIZE === 4 ? 'float' : 'int');
+ foreach ($res as $row) {
+ settype($row['startaddr'], 'int');
+ settype($row['endaddr'], 'int');
self::mergeRanges($ranges, $row);
}
// Got subnets, build whitelist
@@ -164,8 +159,8 @@ class Dnbd3Util {
$public = array();
$private = array();
$public[$self] = $self;
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $ip = $row['fixedip'] ? $row['fixedip'] : $row['clientip'];
+ foreach ($res as $row) {
+ $ip = $row['fixedip'] ?: $row['clientip'];
if ($ip === '<self>') {
continue;
}
@@ -215,13 +210,9 @@ class Dnbd3Util {
* @param int $end end address
* @return string CIDR notation
*/
- private static function range2Cidr($start, $end)
+ private static function range2Cidr(int $start, int $end): string
{
- if (PHP_INT_SIZE > 4) {
- $bin = decbin((int)$start ^ (int)$end);
- } else {
- $bin = decbin((int)(float)$start ^ (int)(float)$end);
- }
+ $bin = decbin($start ^ $end);
if ($bin === '0')
return long2ip($start);
$mask = 32 - strlen($bin);
@@ -252,18 +243,21 @@ class Dnbd3Util {
// $row['startaddr'] must lie before range start, otherwise we'd have hit the case above
$row['endaddr'] = $ranges[$key]['endaddr'];
unset($ranges[$key]);
- continue;
+ //continue;
}
}
$ranges[] = $row;
}
- public static function defaultRunmodeConfig()
+ /**
+ * @return array{bgr: bool, firewall: bool}
+ */
+ public static function defaultRunmodeConfig(): array
{
- return array(
+ return [
'bgr' => true,
'firewall' => false
- );
+ ];
}
public static function matchAddress($server)