Skip to content

Commit

Permalink
storage-node: drop idCache async-lock
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaamani committed Jan 4, 2024
1 parent c5c17db commit 030d0cb
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 27 deletions.
27 changes: 4 additions & 23 deletions storage-node/src/services/caching/localDataObjects.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import AwaitLock from 'await-lock'
import fs from 'fs'
import logger from '../logger'
const fsPromises = fs.promises

// Local in-memory cache for IDs.
const idCache = new Set<string>()

const lock = new AwaitLock()

/**
* Return the current ID cache.
*
* @returns ID array.
*
*/
export async function getDataObjectIDs(): Promise<string[]> {
await lock.acquireAsync()
const ids = Array.from(idCache)
lock.release()

return ids
export function getDataObjectIDs(): string[] {
return Array.from(idCache)
}

/**
Expand All @@ -31,8 +24,6 @@ export async function getDataObjectIDs(): Promise<string[]> {
* @param tempDirName - temp directory name
*/
export async function loadDataObjectIdCache(uploadDir: string): Promise<void> {
await lock.acquireAsync()

const names = await getLocalFileNames(uploadDir)

names
Expand All @@ -42,8 +33,6 @@ export async function loadDataObjectIdCache(uploadDir: string): Promise<void> {
.forEach((id) => idCache.add(id))

logger.debug(`Local ID cache loaded.`)

lock.release()
}

/**
Expand All @@ -53,25 +42,17 @@ export async function loadDataObjectIdCache(uploadDir: string): Promise<void> {
*
* @returns empty promise.
*/
export async function addDataObjectIdToCache(dataObjectId: string): Promise<void> {
await lock.acquireAsync()

export function addDataObjectIdToCache(dataObjectId: string): void {
idCache.add(dataObjectId)

lock.release()
}

/**
* Deletes data object ID from the local cache.
*
* @param dataObjectId - uploading directory
*/
export async function deleteDataObjectIdFromCache(dataObjectId: string): Promise<void> {
await lock.acquireAsync()

export function deleteDataObjectIdFromCache(dataObjectId: string): void {
idCache.delete(dataObjectId)

lock.release()
}

/**
Expand Down
4 changes: 2 additions & 2 deletions storage-node/src/services/sync/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class DeleteLocalFileTask implements SyncTask {
const fullPath = path.join(this.uploadsDirectory, this.filename)
await fsPromises.unlink(fullPath)

await deleteDataObjectIdFromCache(dataObjectId)
deleteDataObjectIdFromCache(dataObjectId)
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export class DownloadFileTask implements SyncTask {
await streamPipeline(request, fileStream)
await this.verifyDownloadedFile(tempFilePath)
await fsPromises.rename(tempFilePath, filepath)
await addDataObjectIdToCache(this.dataObjectId)
addDataObjectIdToCache(this.dataObjectId)
} catch (err) {
logger.error(`Sync - fetching data error for ${this.url}: ${err}`, { err })
try {
Expand Down
2 changes: 1 addition & 1 deletion storage-node/src/services/webApi/controllers/filesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function uploadFile(
const newPath = path.join(uploadsDir, dataObjectId)

registerNewDataObjectId(dataObjectId)
await addDataObjectIdToCache(dataObjectId)
addDataObjectIdToCache(dataObjectId)

// Overwrites existing file.
await fsPromises.rename(fileObj.path, newPath)
Expand Down
2 changes: 1 addition & 1 deletion storage-node/src/services/webApi/controllers/stateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getAllLocalDataObjects(
next: express.NextFunction
): Promise<void> {
try {
const ids = await getDataObjectIDs()
const ids = getDataObjectIDs()

res.status(200).json(ids)
} catch (err) {
Expand Down

0 comments on commit 030d0cb

Please sign in to comment.