Skip to content

Commit b289607

Browse files
authored
[torchagent] batch metadata (#6804)
Batching metadata requests from s3 history for perfomance reasons
1 parent f2c6cbe commit b289607

File tree

1 file changed

+30
-8
lines changed
  • torchci/pages/api/torchagent-get-history

1 file changed

+30
-8
lines changed

torchci/pages/api/torchagent-get-history/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,30 @@ export default async function handler(
6868
return res.status(200).json({ sessions: [] });
6969
}
7070

71-
// Get metadata for each object to fetch the title
72-
const objectsWithMetadata = await Promise.all(
73-
data.Contents.filter((obj) => obj.Key && obj.Key.endsWith(".json")).map(
74-
async (obj) => {
71+
// Helper function to chunk array for batch processing
72+
const chunkArray = <T>(array: T[], chunkSize: number): T[][] => {
73+
const chunks: T[][] = [];
74+
for (let i = 0; i < array.length; i += chunkSize) {
75+
chunks.push(array.slice(i, i + chunkSize));
76+
}
77+
return chunks;
78+
};
79+
80+
// Filter JSON files and prepare for batched metadata fetching
81+
const jsonFiles = data.Contents.filter(
82+
(obj) => obj.Key && obj.Key.endsWith(".json")
83+
);
84+
85+
// Process files in batches of 10 to avoid overwhelming S3 with concurrent requests
86+
const BATCH_SIZE = 10;
87+
const fileChunks = chunkArray(jsonFiles, BATCH_SIZE);
88+
89+
const allObjectsWithMetadata: (HistorySession | null)[] = [];
90+
91+
// Process each batch sequentially to control concurrency
92+
for (const chunk of fileChunks) {
93+
const chunkResults = await Promise.all(
94+
chunk.map(async (obj) => {
7595
try {
7696
const headResponse = await s3.send(
7797
new HeadObjectCommand({
@@ -128,11 +148,13 @@ export default async function handler(
128148
}
129149
return null;
130150
}
131-
}
132-
)
133-
);
151+
})
152+
);
153+
154+
allObjectsWithMetadata.push(...chunkResults);
155+
}
134156

135-
const sessions: HistorySession[] = objectsWithMetadata
157+
const sessions: HistorySession[] = allObjectsWithMetadata
136158
.filter(Boolean)
137159
.sort((a, b) =>
138160
b!.timestamp.localeCompare(a!.timestamp)

0 commit comments

Comments
 (0)