问题
I have recently been doing some work using a stored procedure in Azure's CosmosDB to update some documents. The Documents are a bit chunky (5000+ lines documents and dealing with about a 1000 of them) and the sproc takes more than 5 seconds to run. The 5 second limit stops the sproc from running, throwing the time limit exceeded error. So, I am asking if this can be turned off or configured?
I have read this question about some possible work arounds, which I am looking at along with some other ideas, but, to be clear, the purpose of this question is to establish if this 5 second limit can be turned off or configured not if I can work around it.
I have been trying to find somewhere in the official docs that says this, so if anyone knows where that is that would be great.
I have also tried increasing the throughput on the collection to reduce the time it takes to run. This helps, but the sproc still hits the limit.
回答1:
So, I am asking if this can be turned off or configured?
Clearly, as @David Makogon said the limitation of cosmos db stored procedure runtime can't be turned off or configured.
So , I recommand you adopt workaround in the thread :What happens when 5 second execution time limit exceeds in Azure DocumentDb Stored Procedures. Please use continuation tokens to process data in batchs. You could refer to the pseudo-code
as below :
function updateArticlesDetailsX() {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var docCount = 0;
var counter = 0;
tryQueryAndUpdate();
function tryQueryAndUpdate(continuation) {
var query = {
query: "select * from root r"
};
var requestOptions = {
continuation: continuation
};
var isAccepted =
collection
.queryDocuments(collectionLink,
query,
requestOptions,
function queryCallback(err, documents, responseOptions) {
if (err) throw err;
if (documents.length > 0) {
// If at least one document is found, update it.
docCount = documents.length;
for (var i=0; i<docCount; i++){
tryUpdate(documents[i]);
}
response.setBody("Updated " + docCount + " documents");
}
else if (responseOptions.continuation) {
// Else if the query came back empty, but with a continuation token;
// repeat the query w/ the token.
tryQueryAndUpdate(responseOptions.continuation);
} else {
throw new Error("Document not found.");
}
});
if (!isAccepted) {
throw new Error("The stored procedure timed out");
}
}
function tryUpdate(document) {
//Optimistic concurrency control via HTTP ETag.
var requestOptions = { etag: document._etag };
//Update statement goes here:
document.x = "some new value";
var isAccepted = collection
.replaceDocument(document._self,
document,
requestOptions,
function replaceCallback(err, updatedDocument, responseOptions) {
if (err) throw err;
counter++;
});
// If we hit execution bounds - throw an exception.
if (!isAccepted) {
throw new Error("The stored procedure timed out");
}
}
}
In addition, if your document is too large so that you could just handle only a few per batch (usually not, because the size of document has a 2M
limit) , maybe you should optimize your data structure to make your document snappy.
Hope it help you.
来源:https://stackoverflow.com/questions/49158257/is-it-possible-to-disable-the-5-second-time-limit-for-azure-cosmosdb-stored-proc