Merge pull request #25361 from overleaf/em-load-latest-raw

Rename loadLatestRaw() to getLatestChunkMetadata()

GitOrigin-RevId: e089dcfa57cbbc43df8666b51eca0d81d595a5a7
This commit is contained in:
Eric Mc Sween
2025-05-08 11:27:39 -04:00
committed by Copybot
parent a61afe06b3
commit a9cb0cc197
6 changed files with 31 additions and 29 deletions

View File

@@ -91,7 +91,7 @@ async function getLatestHistoryRaw(req, res, next) {
const readOnly = req.swagger.params.readOnly.value
try {
const { startVersion, endVersion, endTimestamp } =
await chunkStore.loadLatestRaw(projectId, { readOnly })
await chunkStore.getLatestChunkMetadata(projectId, { readOnly })
res.json({
startVersion,
endVersion,

View File

@@ -96,15 +96,15 @@ async function lazyLoadHistoryFiles(history, batchBlobStore) {
* @param {boolean} [opts.readOnly]
* @return {Promise<{id: string, startVersion: number, endVersion: number, endTimestamp: Date}>}
*/
async function loadLatestRaw(projectId, opts) {
async function getLatestChunkMetadata(projectId, opts) {
assert.projectId(projectId, 'bad projectId')
const backend = getBackend(projectId)
const chunkRecord = await backend.getLatestChunk(projectId, opts)
if (chunkRecord == null) {
const chunkMetadata = await backend.getLatestChunk(projectId, opts)
if (chunkMetadata == null) {
throw new Chunk.NotFoundError(projectId)
}
return chunkRecord
return chunkMetadata
}
/**
@@ -116,14 +116,14 @@ async function loadLatestRaw(projectId, opts) {
* @return {Promise<Chunk>}
*/
async function loadLatest(projectId, opts = {}) {
const chunkRecord = await loadLatestRaw(projectId)
const rawHistory = await historyStore.loadRaw(projectId, chunkRecord.id)
const chunkMetadata = await getLatestChunkMetadata(projectId)
const rawHistory = await historyStore.loadRaw(projectId, chunkMetadata.id)
const history = History.fromRaw(rawHistory)
if (!opts.persistedOnly) {
const nonPersistedChanges = await getChunkExtension(
projectId,
chunkRecord.endVersion
chunkMetadata.endVersion
)
history.pushChanges(nonPersistedChanges)
}
@@ -131,7 +131,7 @@ async function loadLatest(projectId, opts = {}) {
const blobStore = new BlobStore(projectId)
const batchBlobStore = new BatchBlobStore(blobStore)
await lazyLoadHistoryFiles(history, batchBlobStore)
return new Chunk(history, chunkRecord.startVersion)
return new Chunk(history, chunkMetadata.startVersion)
}
/**
@@ -354,7 +354,7 @@ async function loadByChunkRecord(projectId, chunkRecord) {
*/
async function* getProjectChunksFromVersion(projectId, version) {
const backend = getBackend(projectId)
const latestChunkMetadata = await loadLatestRaw(projectId)
const latestChunkMetadata = await getLatestChunkMetadata(projectId)
if (!latestChunkMetadata || version > latestChunkMetadata.endVersion) {
return
}
@@ -500,7 +500,7 @@ module.exports = {
getBackend,
initializeProject,
loadLatest,
loadLatestRaw,
getLatestChunkMetadata,
loadAtVersion,
loadAtTimestamp,
loadByChunkRecord,

View File

@@ -5,7 +5,7 @@ import commandLineArgs from 'command-line-args'
import { Chunk, History, Snapshot } from 'overleaf-editor-core'
import {
getProjectChunks,
loadLatestRaw,
getLatestChunkMetadata,
create,
} from '../lib/chunk_store/index.js'
import { client } from '../lib/mongodb.js'
@@ -444,7 +444,7 @@ async function analyseBackupStatus(projectId) {
await getBackupStatus(projectId)
// TODO: when we have confidence that the latestChunkMetadata always matches
// the values from the backupStatus we can skip loading it here
const latestChunkMetadata = await loadLatestRaw(historyId, {
const latestChunkMetadata = await getLatestChunkMetadata(historyId, {
readOnly: Boolean(USE_SECONDARY),
})
if (
@@ -454,7 +454,7 @@ async function analyseBackupStatus(projectId) {
// compare the current end version with the latest chunk metadata to check that
// the updates to the project collection are reliable
// expect some failures due to the time window between getBackupStatus and
// loadLatestRaw where the project is being actively edited.
// getLatestChunkMetadata where the project is being actively edited.
logger.warn(
{
projectId,

View File

@@ -119,17 +119,17 @@ async function verifyBlobHTTP(historyId, hash) {
}
async function backupChunk(historyId) {
const newChunk = await chunkStore.loadLatestRaw(historyId)
const newChunkMetadata = await chunkStore.getLatestChunkMetadata(historyId)
const { buffer: chunkBuffer } = await historyStore.loadRawWithBuffer(
historyId,
newChunk.id
newChunkMetadata.id
)
const md5 = Crypto.createHash('md5').update(chunkBuffer)
await backupPersistor.sendStream(
chunksBucket,
path.join(
projectKey.format(historyId),
projectKey.pad(newChunk.startVersion)
projectKey.pad(newChunkMetadata.startVersion)
),
Stream.Readable.from([chunkBuffer]),
{

View File

@@ -169,8 +169,8 @@ describe('backup script', function () {
makeChunkKey(historyId, 0)
)
const chunkContent = await text(chunkStream.pipe(createGunzip()))
const chunk = await ChunkStore.loadLatestRaw(historyId)
const rawHistory = await historyStore.loadRaw(historyId, chunk.id)
const chunkMetadata = await ChunkStore.getLatestChunkMetadata(historyId)
const rawHistory = await historyStore.loadRaw(historyId, chunkMetadata.id)
expect(JSON.parse(chunkContent)).to.deep.equal(rawHistory)
// Unrelated entries from backedUpBlobs should be not cleared
@@ -299,8 +299,8 @@ describe('backup script', function () {
makeChunkKey(historyId, 0)
)
const chunkContent = await text(chunkStream.pipe(createGunzip()))
const chunk = await ChunkStore.loadLatestRaw(historyId)
const rawHistory = await historyStore.loadRaw(historyId, chunk.id)
const chunkMetadata = await ChunkStore.getLatestChunkMetadata(historyId)
const rawHistory = await historyStore.loadRaw(historyId, chunkMetadata.id)
expect(JSON.parse(chunkContent)).to.deep.equal(rawHistory)
// Unrelated entries from backedUpBlobs should be not cleared
@@ -399,8 +399,8 @@ describe('backup script', function () {
makeChunkKey(historyId, 0)
)
const chunkContent = await text(chunkStream.pipe(createGunzip()))
const chunk = await ChunkStore.loadLatestRaw(historyId)
const rawHistory = await historyStore.loadRaw(historyId, chunk.id)
const chunkMetadata = await ChunkStore.getLatestChunkMetadata(historyId)
const rawHistory = await historyStore.loadRaw(historyId, chunkMetadata.id)
expect(JSON.parse(chunkContent)).to.deep.equal(rawHistory)
// Verify that the demoted global blob was backed up

View File

@@ -120,8 +120,9 @@ describe('chunkStore', function () {
})
it('records the correct metadata in db readOnly=false', async function () {
const raw = await chunkStore.loadLatestRaw(projectId)
expect(raw).to.deep.include({
const chunkMetadata =
await chunkStore.getLatestChunkMetadata(projectId)
expect(chunkMetadata).to.deep.include({
startVersion: 0,
endVersion: 2,
endTimestamp: lastChangeTimestamp,
@@ -129,10 +130,11 @@ describe('chunkStore', function () {
})
it('records the correct metadata in db readOnly=true', async function () {
const raw = await chunkStore.loadLatestRaw(projectId, {
readOnly: true,
})
expect(raw).to.deep.include({
const chunkMetadata = await chunkStore.getLatestChunkMetadata(
projectId,
{ readOnly: true }
)
expect(chunkMetadata).to.deep.include({
startVersion: 0,
endVersion: 2,
endTimestamp: lastChangeTimestamp,