Skip to content

Commit

Permalink
Storage service write improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-signal committed Oct 12, 2020
1 parent 459eebc commit b879c73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
49 changes: 33 additions & 16 deletions ts/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ async function generateManifest(
isNewManifest = false
): Promise<GeneratedManifestType> {
window.log.info(
`storageService.generateManifest: generating manifest for version ${version}. Is new? ${isNewManifest}`
'storageService.generateManifest: generating manifest',
version,
isNewManifest
);

const ITEM_TYPE = window.textsecure.protobuf.ManifestRecord.Identifier.Type;
Expand Down Expand Up @@ -211,20 +213,18 @@ async function generateManifest(
}
}

const unknownRecordsArray =
window.storage.get('storage-service-unknown-records') || [];
const unknownRecordsArray = (
window.storage.get('storage-service-unknown-records') || []
).filter((record: UnknownRecord) => !validRecordTypes.has(record.itemType));

window.log.info(
`storageService.generateManifest: adding ${unknownRecordsArray.length} unknown records`
'storageService.generateManifest: adding unknown records:',
unknownRecordsArray.length
);

// When updating the manifest, ensure all "unknown" keys are added to the
// new manifest, so we don't inadvertently delete something we don't understand
unknownRecordsArray.forEach((record: UnknownRecord) => {
if (validRecordTypes.has(record.itemType)) {
return;
}

const identifier = new window.textsecure.protobuf.ManifestRecord.Identifier();
identifier.type = record.itemType;
identifier.raw = base64ToArrayBuffer(record.storageID);
Expand All @@ -236,7 +236,8 @@ async function generateManifest(
window.storage.get('storage-service-error-records') || [];

window.log.info(
`storageService.generateManifest: adding ${recordsWithErrors.length} records that had errors in the previous merge`
'storageService.generateManifest: adding records that had errors in the previous merge',
recordsWithErrors.length
);

// These records failed to merge in the previous fetchManifest, but we still
Expand Down Expand Up @@ -367,7 +368,9 @@ async function uploadManifest(
const credentials = window.storage.get('storageCredentials');
try {
window.log.info(
`storageService.uploadManifest: inserting ${newItems.size} items, deleting ${deleteKeys.length} keys`
'storageService.uploadManifest: keys inserting, deleting:',
newItems.size,
deleteKeys.length
);

const writeOperation = new window.textsecure.protobuf.WriteOperation();
Expand All @@ -384,7 +387,8 @@ async function uploadManifest(
);

window.log.info(
`storageService.uploadManifest: upload done, updating ${conversationsToUpdate.length} conversation(s) with new storageIDs`
'storageService.uploadManifest: upload done, updating conversation(s) with new storageIDs:',
conversationsToUpdate.length
);

// update conversations with the new storageID
Expand Down Expand Up @@ -647,6 +651,14 @@ async function processManifest(
}
});

const recordsWithErrors =
window.storage.get('storage-service-error-records') || [];

// Do not fetch any records that we failed to merge in the previous fetch
recordsWithErrors.forEach((record: UnknownRecord) => {
localKeys.push(record.storageID);
});

window.log.info(
`storageService.processManifest: localKeys.length ${localKeys.length}`
);
Expand Down Expand Up @@ -763,7 +775,7 @@ async function processManifest(
unknownRecords.set(record.storageID, record);
});

const recordsWithErrors: Array<UnknownRecord> = [];
const newRecordsWithErrors: Array<UnknownRecord> = [];

let hasConflict = false;

Expand All @@ -774,7 +786,7 @@ async function processManifest(
storageID: mergedRecord.storageID,
});
} else if (mergedRecord.hasError) {
recordsWithErrors.push({
newRecordsWithErrors.push({
itemType: mergedRecord.itemType,
storageID: mergedRecord.storageID,
});
Expand All @@ -789,14 +801,19 @@ async function processManifest(
);

window.log.info(
`storageService.processManifest: Found ${newUnknownRecords.length} unknown records`
'storageService.processManifest: Unknown records found:',
newUnknownRecords.length
);
window.storage.put('storage-service-unknown-records', newUnknownRecords);

window.log.info(
`storageService.processManifest: Found ${recordsWithErrors.length} records with errors`
'storageService.processManifest: Records with errors:',
newRecordsWithErrors.length
);
window.storage.put('storage-service-error-records', recordsWithErrors);
// Refresh the list of records that had errors with every push, that way
// this list doesn't grow unbounded and we keep the list of storage keys
// fresh.
window.storage.put('storage-service-error-records', newRecordsWithErrors);

const now = Date.now();

Expand Down
13 changes: 10 additions & 3 deletions ts/services/storageRecordOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ function addUnknownFields(
): void {
if (record.__unknownFields) {
window.log.info(
`storageService.addUnknownFields: Unknown fields found for ${conversation.get(
'id'
)}`
'storageService.addUnknownFields: Unknown fields found for',
conversation.debugID()
);
conversation.set({
storageUnknownFields: arrayBufferToBase64(record.__unknownFields),
});
} else if (conversation.get('storageUnknownFields')) {
// If the record doesn't have unknown fields attached but we have them
// saved locally then we need to clear it out
window.log.info(
'storageService.addUnknownFields: Clearing unknown fields for',
conversation.debugID()
);
conversation.unset('storageUnknownFields');
}
}

Expand Down

0 comments on commit b879c73

Please sign in to comment.