Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of file requests during worker restarts #2960

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/openneuro-server/src/datalad/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ export const getFiles = (datasetId, treeish): Promise<[DatasetFile?]> => {
])
return cache.get(
async (doNotCache): Promise<[DatasetFile?]> => {
const response = await fetch(`http://${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/tree/${treeish}`)
const response = await fetch(
`http://${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/tree/${treeish}`,
{
signal: AbortSignal.timeout(10000),
},
)
const body = await response.json()
const files = body?.files
if (files) {
Expand Down
26 changes: 21 additions & 5 deletions packages/openneuro-server/src/handlers/datalad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,27 @@ export const getFile = async (req, res) => {
let tree = snapshotId || "HEAD"
let file
for (const level of pathComponents) {
const files = await getFiles(datasetId, tree)
if (level == pathComponents.slice(-1)) {
file = files.find((f) => !f.directory && f.filename === level)
} else {
tree = files.find((f) => f.directory && f.filename === level).id
try {
const files = await getFiles(datasetId, tree)
if (level == pathComponents.slice(-1)) {
file = files.find((f) => !f.directory && f.filename === level)
} else {
tree = files.find((f) => f.directory && f.filename === level).id
}
} catch (err) {
// ConnectTimeoutError is Node/Undici and TimeoutError is the standard DOMException name
if (
err?.cause?.name === "ConnectTimeoutError" ||
err?.name === "TimeoutError"
) {
// Unreachable backend, forward this error
// Usually this is the service restarting due to node migrations or upgrades
res.status(503).send("Worker could not be reached")
return
} else {
// Unknown error should bubble up
throw err
}
}
}
// Get the file URL and redirect if external or serve if local
Expand Down