Browse Source

collect route coordinates

yggdrasil-0.4.7
ghost 1 year ago
parent
commit
42f939ca8e
  1. BIN
      database/yggstate.mwb
  2. BIN
      media/db-prototype.png
  3. 47
      src/crontab/crawler.php
  4. 62
      src/library/mysql.php

BIN
database/yggstate.mwb

Binary file not shown.

BIN
media/db-prototype.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 61 KiB

47
src/crontab/crawler.php

@ -44,7 +44,18 @@ $debug = [ @@ -44,7 +44,18 @@ $debug = [
'insert' => 0,
'update' => 0,
]
]
],
'coordinate' => [
'total' => [
'insert' => 0,
],
'route' => [
'total' => [
'insert' => 0,
'delete' => 0,
]
]
],
]
]
];
@ -82,7 +93,7 @@ if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) { @@ -82,7 +93,7 @@ if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) {
$debug['yggdrasil']['peer']['total']['insert']++;
}
// Init peer data
// Init peer remote
if ($connectedPeerRemoteUrl = Yggverse\Parser\Url::parse($connectedPeerInfo->remote)) {
if ($dbPeerRemote = $db->findPeerRemote($dbPeerId,
@ -122,6 +133,38 @@ if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) { @@ -122,6 +133,38 @@ if ($connectedPeers = Yggverse\Yggdrasilctl\Yggdrasil::getPeers()) {
$debug['yggdrasil']['peer']['remote']['total']['insert']++;
}
// Init peer coordinate
if ($dbPeerCoordinate = $db->getLastCoordinate($dbPeerId)) {
$peerCoordinateId = $dbPeerCoordinate->peerCoordinateId;
} else {
$peerCoordinateId = $db->addPeerCoordinate($dbPeerId, $connectedPeerInfo->port, time());
$debug['yggdrasil']['peer']['coordinate']['total']['insert']++;
}
// Init peer coordinate route
$localPeerCoordinateRoute = [];
foreach ($db->getPeerCoordinateRoute($peerCoordinateId) as $dbPeerCoordinateRoute) {
$localPeerCoordinateRoute[$dbPeerCoordinateRoute->level] = $dbPeerCoordinateRoute->port;
}
// Compare remote and local routes to prevent write operations
if ($localPeerCoordinateRoute !== $connectedPeerInfo->coords) {
$debug['yggdrasil']['peer']['coordinate']['route']['total']['delete'] +=
$db->flushPeerCoordinateRoute($peerCoordinateId);
foreach ($connectedPeerInfo->coords as $level => $port) {
$debug['yggdrasil']['peer']['coordinate']['route']['total']['insert'] +=
$db->addPeerCoordinateRoute($peerCoordinateId, $level, $port);
}
}
}
$debug['yggdrasil']['peer']['total']['online']++;

62
src/library/mysql.php

@ -155,6 +155,68 @@ class MySQL { @@ -155,6 +155,68 @@ class MySQL {
return $query->fetch();
}
// Peer coordinate
// https://github.com/matrix-org/pinecone/wiki/2.-Spanning-Tree#coordinates
public function addPeerCoordinate(int $peerId, int $port, int $timeAdded) {
$this->_debug->query->insert->total++;
$query = $this->_db->prepare('INSERT INTO `peerCoordinate` SET `peerId` = ?,
`port` = ?,
`timeAdded` = ?');
$query->execute([$peerId, $port, $timeAdded]);
return $this->_db->lastInsertId();
}
public function addPeerCoordinateRoute(int $peerCoordinateId, int $level, int $port) {
$this->_debug->query->insert->total++;
$query = $this->_db->prepare('INSERT INTO `peerCoordinateRoute` SET `peerCoordinateId` = ?,
`level` = ?,
`port` = ?');
$query->execute([$peerCoordinateId, $level, $port]);
return $this->_db->lastInsertId();
}
public function flushPeerCoordinateRoute(int $peerCoordinateId) {
$this->_debug->query->delete->total++;
$query = $this->_db->prepare('DELETE FROM `peerCoordinateRoute` WHERE `peerCoordinateId` = ?');
$query->execute([$peerCoordinateId]);
return $query->rowCount();
}
public function getLastCoordinate(int $peerId) {
$this->_debug->query->select->total++;
$query = $this->_db->prepare('SELECT * FROM `peerCoordinate` WHERE `peerId` = ? ORDER BY `timeAdded` DESC LIMIT 1');
$query->execute([$peerId]);
return $query->fetch();
}
public function getPeerCoordinateRoute(int $peerCoordinateId) {
$this->_debug->query->select->total++;
$query = $this->_db->prepare('SELECT * FROM `peerCoordinateRoute` WHERE `peerCoordinateId` = ? ORDER BY `level` ASC');
$query->execute([$peerCoordinateId]);
return $query->fetchAll();
}
// Other
public function optimize() {

Loading…
Cancel
Save