Skip to content

Commit

Permalink
Fix Parallel Download Issue by seperating the downloading with serial…
Browse files Browse the repository at this point in the history
…ization process

Co-authored-by: Charlie Ruan <53290280+CharlieFRuan@users.noreply.github.com>
  • Loading branch information
DiegoCao and CharlieFRuan committed Mar 2, 2024
1 parent c37b435 commit dbf8fd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions web/src/artifact_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export interface ArtifactCacheTemplate {
*/
fetchWithCache(url: string);

/**
* add ey url to cache
*/
addToCache(url: string);

/**
* check if cache has all keys in Cache
*/
Expand All @@ -16,4 +21,5 @@ export interface ArtifactCacheTemplate {
* Delete url in cache if url exists
*/
deleteInCache(url: string);

}
26 changes: 24 additions & 2 deletions web/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,17 @@ export class ArtifactCache implements ArtifactCacheTemplate {
return result;
}

async addToCache(url: string) {
const request = new Request(url);
if (this.cache === undefined) {
this.cache = await caches.open(this.scope);
}
const result = await this.cache.match(request);
if (result === undefined){
await this.cache.add(request);
}
}

async hasAllKeys(keys: string[]) {
if (this.cache === undefined) {
this.cache = await caches.open(this.scope);
Expand Down Expand Up @@ -1548,8 +1559,7 @@ export class Instance implements Disposable {
text: "Start to fetch params",
});
}

for (let i = 0; i < list.length; ++i) {
const downloadCache = async (i: number) => {
reportCallback(i);
const shard = list[i];
const dataUrl = new URL(shard.dataPath, ndarrayCacheUrl).href;
Expand All @@ -1560,6 +1570,18 @@ export class Instance implements Disposable {
this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err);
throw err;
}
}
await Promise.all(list.map((_, index) => downloadCache(index)));
for (let i = 0; i < list.length; ++i) {
const shard = list[i];
const dataUrl = new URL(shard.dataPath, ndarrayCacheUrl).href;
let buffer;
try {
buffer = await (await artifactCache.fetchWithCache(dataUrl)).arrayBuffer();
} catch (err) {
this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err);
throw err;
}
const shardRecords = shard.records;
for (let j = 0; j < shardRecords.length; ++j) {
const rec = shardRecords[j];
Expand Down

0 comments on commit dbf8fd4

Please sign in to comment.