Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Load progress percentage sometimes exceeding 100% #8

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 85 additions & 19 deletions src/qlever-petrimaps/GeomCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <algorithm>
#include <sstream>

#include <cassert>

#include "qlever-petrimaps/GeomCache.h"
#include "qlever-petrimaps/Misc.h"
#include "qlever-petrimaps/server/Requestor.h"
Expand Down Expand Up @@ -353,22 +355,29 @@ double GeomCache::getLoadStatusPercent(bool total) {
}

if (!total) {
return std::atomic<size_t>(_curRow) / static_cast<double>(_totalSize) *
100.0;
double percent = _curRow / static_cast<double>(_totalSize) * 100.0;
assert(percent <= 100.0);
return percent;
}

double parsePercent = 95.0;
double parseIdsPercent = 5.0;
double totalPercent = 0.0;
switch (_loadStatusStage) {
case _LoadStatusStages::Parse:
totalPercent = std::atomic<size_t>(_curRow) /
static_cast<double>(_totalSize) * parsePercent;
totalPercent = _curRow / static_cast<double>(_totalSize) * parsePercent;
assert(totalPercent <= 100.0);
break;

case _LoadStatusStages::ParseIds:
totalPercent = parsePercent;
totalPercent += std::atomic<size_t>(_curRow) /
static_cast<double>(_totalSize) * parseIdsPercent;
totalPercent += _curRow / static_cast<double>(_totalSize) * parseIdsPercent;
assert(totalPercent <= 100.0);
break;

case _LoadStatusStages::FromFile:
totalPercent = _curRow / static_cast<double>(_totalSize) * 100.0;
assert(totalPercent <= 100.0);
break;
}

Expand All @@ -380,6 +389,16 @@ int GeomCache::getLoadStatusStage() {
return _loadStatusStage;
}

// _____________________________________________________________________________
size_t GeomCache::getTotalProgress() {
return _totalSize;
}

// _____________________________________________________________________________
size_t GeomCache::getCurrentProgress() {
return _curRow;
}

// _____________________________________________________________________________
void GeomCache::parseIds(const char* c, size_t size) {
_loadStatusStage = _LoadStatusStages::ParseIds;
Expand Down Expand Up @@ -1001,6 +1020,8 @@ std::string GeomCache::indexHashFromDisk(const std::string& fname) {

// _____________________________________________________________________________
void GeomCache::fromDisk(const std::string& fname) {
_loadStatusStage = _LoadStatusStages::FromFile;

_points.clear();
_linePoints.clear();
_lines.clear();
Expand All @@ -1011,27 +1032,72 @@ void GeomCache::fromDisk(const std::string& fname) {
char tmp[100];
f.read(tmp, 100);
tmp[99] = 0;

_indexHash = util::trim(tmp);

size_t numPoints;
size_t numLinePoints;
size_t numLines;
size_t numQidToId;
std::streampos posPoints;
std::streampos posLinePoints;
std::streampos posLines;
std::streampos posQidToId;
// get total num points
// points
f.read(reinterpret_cast<char*>(&numPoints), sizeof(size_t));
_points.resize(numPoints);
f.read(reinterpret_cast<char*>(&_points[0]),
sizeof(util::geo::FPoint) * numPoints);
posPoints = f.tellg();
f.seekg(sizeof(util::geo::FPoint) * numPoints, f.cur);

// linePoints
f.read(reinterpret_cast<char*>(&numLinePoints), sizeof(size_t));
_linePoints.resize(numLinePoints);
posLinePoints = f.tellg();
f.seekg(sizeof(util::geo::Point<int16_t>) * numLinePoints, f.cur);

// lines
f.read(reinterpret_cast<char*>(&numLines), sizeof(size_t));
_lines.resize(numLines);
posLines = f.tellg();
f.seekg(sizeof(size_t) * numLines, f.cur);

// qidToId
f.read(reinterpret_cast<char*>(&numQidToId), sizeof(size_t));
_qidToId.resize(numQidToId);
posQidToId = f.tellg();
f.seekg(sizeof(IdMapping) * numQidToId, f.cur);

_totalSize = numPoints + numLinePoints + numLines + numQidToId;
_curRow = 0;

f.read(reinterpret_cast<char*>(&numPoints), sizeof(size_t));
_linePoints.resize(numPoints);
f.read(reinterpret_cast<char*>(&_linePoints[0]),
sizeof(util::geo::Point<int16_t>) * numPoints);
// read data from file
// points
f.seekg(posPoints);
for (size_t i = 0; i < numPoints; i++) {
f.read(reinterpret_cast<char*>(&_points[i]), sizeof(util::geo::FPoint));
_curRow += 1;
}

f.read(reinterpret_cast<char*>(&numPoints), sizeof(size_t));
_lines.resize(numPoints);
f.read(reinterpret_cast<char*>(&_lines[0]), sizeof(size_t) * numPoints);
// linePoints
f.seekg(posLinePoints);
for (size_t i = 0; i < numLinePoints; i++) {
f.read(reinterpret_cast<char*>(&_linePoints[i]), sizeof(util::geo::Point<int16_t>));
_curRow += 1;
}

f.read(reinterpret_cast<char*>(&numPoints), sizeof(size_t));
_qidToId.resize(numPoints);
f.read(reinterpret_cast<char*>(&_qidToId[0]), sizeof(IdMapping) * numPoints);
// lines
f.seekg(posLines);
for (size_t i = 0; i < numLines; i++) {
f.read(reinterpret_cast<char*>(&_lines[i]), sizeof(size_t));
_curRow += 1;
}

// qidToId
f.seekg(posQidToId);
for (size_t i = 0; i < numQidToId; i++) {
f.read(reinterpret_cast<char*>(&_qidToId[i]), sizeof(IdMapping));
_curRow += 1;
}

f.close();
}
Expand Down
23 changes: 11 additions & 12 deletions src/qlever-petrimaps/GeomCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>
#include <unordered_map>
#include <vector>
#include <atomic>

#include "qlever-petrimaps/Misc.h"
#include "util/geo/Geo.h"
Expand All @@ -24,8 +25,7 @@ class GeomCache {
GeomCache() : _backendUrl(""), _curl(0) {}
explicit GeomCache(const std::string& backendUrl)
: _backendUrl(backendUrl),
_curl(curl_easy_init()) {
}
_curl(curl_easy_init()) {}

GeomCache& operator=(GeomCache&& o) {
_backendUrl = o._backendUrl;
Expand Down Expand Up @@ -92,6 +92,8 @@ class GeomCache {
double getLoadStatusPercent(bool total);
double getLoadStatusPercent() { return getLoadStatusPercent(false); };
int getLoadStatusStage();
size_t getTotalProgress();
size_t getCurrentProgress();

private:
std::string _backendUrl;
Expand All @@ -100,18 +102,17 @@ class GeomCache {
uint8_t _curByte;
ID _curId;
QLEVER_ID_TYPE _maxQid;
size_t _curRow, _curUniqueGeom;
size_t _totalSize = 0;
std::atomic<size_t> _curRow;
size_t _curUniqueGeom;

enum _LoadStatusStages {Parse = 1, ParseIds};
enum _LoadStatusStages {Parse = 1, ParseIds, FromFile};
_LoadStatusStages _loadStatusStage = Parse;

static size_t writeCb(void* contents, size_t size, size_t nmemb, void* userp);
static size_t writeCbIds(void* contents, size_t size, size_t nmemb,
void* userp);
static size_t writeCbCount(void* contents, size_t size, size_t nmemb,
void* userp);
static size_t writeCbString(void* contents, size_t size, size_t nmemb,
void* userp);
static size_t writeCbIds(void* contents, size_t size, size_t nmemb, void* userp);
static size_t writeCbCount(void* contents, size_t size, size_t nmemb, void* userp);
static size_t writeCbString(void* contents, size_t size, size_t nmemb, void* userp);

// Get the right SPARQL query for the given backend.
const std::string& getQuery(const std::string& backendUrl) const;
Expand Down Expand Up @@ -145,8 +146,6 @@ class GeomCache {
std::fstream _linesF;
std::fstream _qidToIdF;

size_t _totalSize = 0;

IdMapping _lastQidToId;

std::vector<IdMapping> _qidToId;
Expand Down
54 changes: 44 additions & 10 deletions src/qlever-petrimaps/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ using util::geo::LineSegment;
using util::geo::webMercToLatLng;

const static double THRESHOLD = 200;
static std::atomic<size_t> _curRow;

// _____________________________________________________________________________
Server::Server(size_t maxMemory, const std::string& cacheDir, int cacheLifetime)
Expand Down Expand Up @@ -146,7 +147,6 @@ util::http::Answer Server::handleHeatMapReq(const Params& pars,
if (box.size() != 4) throw std::invalid_argument("Invalid request.");

std::shared_ptr<Requestor> r;

{
std::lock_guard<std::mutex> guard(_m);
bool has = _rs.count(id);
Expand Down Expand Up @@ -288,7 +288,6 @@ util::http::Answer Server::handleHeatMapReq(const Params& pars,
}

// LINES

const auto& lgrid = r->getLineGrid();

if (intersects(lgrid.getBBox(), fbbox)) {
Expand Down Expand Up @@ -655,7 +654,6 @@ util::http::Answer Server::handlePosReq(const Params& pars) const {
LOG(INFO) << "[SERVER] Click at " << x << ", " << y;

std::shared_ptr<Requestor> reqor;

{
std::lock_guard<std::mutex> guard(_m);
bool has = _rs.count(id);
Expand Down Expand Up @@ -863,6 +861,10 @@ std::string Server::parseUrl(std::string u, std::string pl,
return util::urlDecode(parts.front());
}

void Server::pngWriteRowCb(png_structp png_ptr, png_uint_32 row, int pass) {
_curRow = row;
}

// _____________________________________________________________________________
inline void pngWriteCb(png_structp png_ptr, png_bytep data, png_size_t length) {
int sock = *((int*)png_get_io_ptr(png_ptr));
Expand Down Expand Up @@ -891,7 +893,7 @@ inline void pngErrorCb(png_structp, png_const_charp error_msg) {
}

// _____________________________________________________________________________
void Server::writePNG(const unsigned char* data, size_t w, size_t h, int sock) {
void Server::writePNG(const unsigned char* data, size_t w, size_t h, int sock) const {
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr,
pngErrorCb, pngWarnCb);
if (!png_ptr) return;
Expand All @@ -907,8 +909,12 @@ void Server::writePNG(const unsigned char* data, size_t w, size_t h, int sock) {
return;
}

png_set_write_fn(png_ptr, &sock, pngWriteCb, 0);
// Handle Load Status
_totalSize = h;
_curRow = 0;

png_set_write_status_fn(png_ptr, pngWriteRowCb);
png_set_write_fn(png_ptr, &sock, pngWriteCb, 0);
png_set_filter(png_ptr, 0, PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
png_set_compression_level(png_ptr, 7);

Expand All @@ -918,8 +924,7 @@ void Server::writePNG(const unsigned char* data, size_t w, size_t h, int sock) {
png_set_IHDR(png_ptr, info_ptr, w, h, bit_depth, color_type, interlace_type,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

png_bytep* row_pointers =
(png_byte**)png_malloc(png_ptr, h * sizeof(png_bytep));
png_bytep* row_pointers = (png_byte**)png_malloc(png_ptr, h * sizeof(png_bytep));

for (size_t y = 0; y < h; ++y) {
row_pointers[y] = const_cast<png_bytep>(data + y * w * 4);
Expand Down Expand Up @@ -1126,12 +1131,30 @@ util::http::Answer Server::handleLoadStatusReq(const Params& pars) const {
auto backend = pars.find("backend")->second;
createCache(backend);
std::shared_ptr<GeomCache> cache = _caches[backend];
double loadStatusPercent = cache->getLoadStatusPercent(true);

// We have 3 loading stages:
// 1) Filling geometry cache / reading cache from disk
// 2) Fetching geometries
// 3) Rendering result
// 1) + 2) by GeomCache, 3) by Server
// => Merge load status
// 1) + 2) = 95%, 3) = 5%

double geomCachePercent = 0.95;
double serverPercent = 0.05;
double geomCacheLoadStatusPercent = cache->getLoadStatusPercent(true);
double serverLoadStatusPercent = getLoadStatusPercent();
double totalPercent = geomCachePercent * geomCacheLoadStatusPercent + serverPercent * serverLoadStatusPercent;

int loadStatusStage = cache->getLoadStatusStage();
size_t totalProgress = cache->getTotalProgress();
size_t currentProgress = cache->getCurrentProgress();

std::stringstream json;
json << "{\"percent\": " << loadStatusPercent
<< ", \"stage\": " << loadStatusStage << "}";
json << "{\"percent\": " << totalPercent
<< ", \"stage\": " << loadStatusStage
<< ", \"totalProgress\": " << totalProgress
<< ", \"currentProgress\": " << currentProgress << "}";
util::http::Answer ans = util::http::Answer("200 OK", json.str());

return ans;
Expand Down Expand Up @@ -1169,6 +1192,17 @@ std::string Server::getSessionId() const {
return std::to_string(d(rng));
}

double Server::getLoadStatusPercent() const {
if (_totalSize == 0) {
return 0.0;
}

double percent = _curRow / static_cast<double>(_totalSize) * 100.0;
assert(percent <= 100.0);

return percent;
}

void Server::createCache(const std::string& backend) const {
std::shared_ptr<GeomCache> cache;

Expand Down
9 changes: 8 additions & 1 deletion src/qlever-petrimaps/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <thread>

#include <png.h>
#include "qlever-petrimaps/GeomCache.h"
#include "qlever-petrimaps/server/Requestor.h"
#include "util/http/Server.h"
Expand Down Expand Up @@ -51,7 +52,10 @@ class Server : public util::http::Handler {

std::string getSessionId() const;

static void writePNG(const unsigned char* data, size_t w, size_t h, int sock);
double getLoadStatusPercent() const;

static void pngWriteRowCb(png_structp png_ptr, png_uint_32 row, int pass);
void writePNG(const unsigned char* data, size_t w, size_t h, int sock) const;

void drawPoint(std::vector<uint32_t>& points, std::vector<double>& points2,
int px, int py, int w, int h, MapStyle style) const;
Expand All @@ -64,6 +68,9 @@ class Server : public util::http::Handler {

int _cacheLifetime;

// Load Status
mutable size_t _totalSize = 0;

mutable std::mutex _m;

mutable std::map<std::string, std::shared_ptr<GeomCache>> _caches;
Expand Down
2 changes: 1 addition & 1 deletion src/util
12 changes: 8 additions & 4 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
<div id='stats'></div>
</main>
<div id='msg'>
<div id='msg-inner'>Loading results from QLever</div>
<div id='msg-inner-desc'></div>

<div id='msg-heading'>Loading results from QLever</div>
<div id='msg-info'>
<div id='msg-info-heading'>Filling the geometry cache</div>
<div id='msg-info-desc'>This needs to be done only once after the server has been started and does not have to be repeated for subsequent queries.</div>
</div>
<div id='msg-error'></div>

<div id="load">
<div id="load-stage">Parsing geometry... (1/2)</div>
<div id="load-stage"></div>
<div id="load-status">
<div id="load-bar"></div>
<div id="load-percent">0.00%</div>
Expand Down
Loading