more logging for sync

This commit is contained in:
zadam 2023-09-21 18:13:14 +02:00
parent d0383f3044
commit 79e5e3b65f
3 changed files with 56 additions and 27 deletions

View file

@ -166,11 +166,7 @@ function update(req) {
const {entities, instanceId} = body; const {entities, instanceId} = body;
sql.transactional(() => { sql.transactional(() => syncUpdateService.updateEntities(entities, instanceId));
for (const {entityChange, entity} of entities) {
syncUpdateService.updateEntity(entityChange, entity, instanceId);
}
});
} }
setInterval(() => { setInterval(() => {

View file

@ -126,8 +126,6 @@ async function doLogin() {
} }
async function pullChanges(syncContext) { async function pullChanges(syncContext) {
let atLeastOnePullApplied = false;
while (true) { while (true) {
const lastSyncedPull = getLastSyncedPull(); const lastSyncedPull = getLastSyncedPull();
const logMarkerId = utils.randomString(10); // to easily pair sync events between client and server logs const logMarkerId = utils.randomString(10); // to easily pair sync events between client and server logs
@ -143,22 +141,7 @@ async function pullChanges(syncContext) {
const pulledDate = Date.now(); const pulledDate = Date.now();
sql.transactional(() => { sql.transactional(() => {
for (const {entityChange, entity} of entityChanges) { syncUpdateService.updateEntities(entityChanges, syncContext.instanceId);
const changeAppliedAlready = entityChange.changeId
&& !!sql.getValue("SELECT 1 FROM entity_changes WHERE changeId = ?", [entityChange.changeId]);
if (changeAppliedAlready) {
continue;
}
if (!atLeastOnePullApplied) { // send only for first
ws.syncPullInProgress();
atLeastOnePullApplied = true;
}
syncUpdateService.updateEntity(entityChange, entity, syncContext.instanceId);
}
if (lastSyncedPull !== lastEntityChangeId) { if (lastSyncedPull !== lastEntityChangeId) {
setLastSyncedPull(lastEntityChangeId); setLastSyncedPull(lastEntityChangeId);

View file

@ -3,15 +3,51 @@ const log = require('./log');
const entityChangesService = require('./entity_changes'); const entityChangesService = require('./entity_changes');
const eventService = require('./events'); const eventService = require('./events');
const entityConstructor = require("../becca/entity_constructor"); const entityConstructor = require("../becca/entity_constructor");
const ws = require("./ws");
function updateEntity(remoteEC, remoteEntityRow, instanceId) { function updateEntities(entityChanges, instanceId) {
if (entityChanges.length === 0) {
return;
}
let atLeastOnePullApplied = false;
const updateContext = {
updated: {},
alreadyUpdated: 0,
erased: 0,
alreadyErased: 0
};
for (const {entityChange, entity} of entityChanges) {
const changeAppliedAlready = entityChange.changeId
&& !!sql.getValue("SELECT 1 FROM entity_changes WHERE changeId = ?", [entityChange.changeId]);
if (changeAppliedAlready) {
updateContext.alreadyUpdated++;
continue;
}
if (!atLeastOnePullApplied) { // avoid spamming and send only for first
ws.syncPullInProgress();
atLeastOnePullApplied = true;
}
updateEntity(entityChange, entity, instanceId, updateContext);
}
logUpdateContext(updateContext);
}
function updateEntity(remoteEC, remoteEntityRow, instanceId, updateContext) {
if (!remoteEntityRow && remoteEC.entityName === 'options') { if (!remoteEntityRow && remoteEC.entityName === 'options') {
return; // can be undefined for options with isSynced=false return; // can be undefined for options with isSynced=false
} }
const updated = remoteEC.entityName === 'note_reordering' const updated = remoteEC.entityName === 'note_reordering'
? updateNoteReordering(remoteEC, remoteEntityRow, instanceId) ? updateNoteReordering(remoteEC, remoteEntityRow, instanceId)
: updateNormalEntity(remoteEC, remoteEntityRow, instanceId); : updateNormalEntity(remoteEC, remoteEntityRow, instanceId, updateContext);
if (updated) { if (updated) {
if (remoteEntityRow?.isDeleted) { if (remoteEntityRow?.isDeleted) {
@ -29,11 +65,12 @@ function updateEntity(remoteEC, remoteEntityRow, instanceId) {
} }
} }
function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) { function updateNormalEntity(remoteEC, remoteEntityRow, instanceId, updateContext) {
const localEC = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]); const localEC = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);
if (!localEC?.isErased && remoteEC.isErased) { if (!localEC?.isErased && remoteEC.isErased) {
eraseEntity(remoteEC, instanceId); eraseEntity(remoteEC, instanceId);
updateContext.erased++;
return true; return true;
} else if (localEC?.isErased && !remoteEC.isErased) { } else if (localEC?.isErased && !remoteEC.isErased) {
@ -42,6 +79,7 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
return false; return false;
} else if (localEC?.isErased && remoteEC.isErased) { } else if (localEC?.isErased && remoteEC.isErased) {
updateContext.alreadyErased++;
return false; return false;
} }
@ -65,6 +103,9 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
sql.replace(remoteEC.entityName, remoteEntityRow); sql.replace(remoteEC.entityName, remoteEntityRow);
updateContext.updated[remoteEC.entityName] = updateContext.updated[remoteEC.entityName] || [];
updateContext.updated[remoteEC.entityName].push(remoteEC.entityId);
if (!localEC || localEC.utcDateChanged < remoteEC.utcDateChanged) { if (!localEC || localEC.utcDateChanged < remoteEC.utcDateChanged) {
entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId); entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
} }
@ -118,6 +159,15 @@ function eraseEntity(entityChange, instanceId) {
entityChangesService.putEntityChangeWithInstanceId(entityChange, instanceId); entityChangesService.putEntityChangeWithInstanceId(entityChange, instanceId);
} }
function logUpdateContext(updateContext) {
const message = JSON.stringify(updateContext)
.replaceAll('"', '')
.replaceAll(":", ": ")
.replaceAll(",", ", ");
log.info(message.substr(1, message.length - 2));
}
module.exports = { module.exports = {
updateEntity updateEntities
}; };