Reputation: 991
The next SP run over the entire collection, do some process on each document, which I discard here, then replace the document with the processed document.
You'll see the SP are calling again and again with the returned continuationToken, query for more docs.
copy past the SP and see the numbers in the results.. the documents from last query is not been replaced, they been rejected from queuing.
Why?
SP:
function sample(continuationToken) {
var continuations = [];
var pSize = 100000;
var filterQuery = "select * from w";
var documentsProcessed = 0;
var querysCount = 0;
var documentsReplaced = 0;
var documentsRejectFromQueue = 0;
var context = getContext(),
collection = context.getCollection(),
response = context.getResponse();
tryQuery(continuationToken);
function tryQuery(nextContinuationToken) {
var options = { continuation: nextContinuationToken, pageSize: pSize };
if (!query(options)) {
setBody(nextContinuationToken);
}
}
function query(options) {
return (filterQuery && filterQuery.length) ?
collection.queryDocuments(collection.getSelfLink(), filterQuery, options, processMultiUsers) :
collection.readDocuments(collection.getSelfLink(), options, processMultiUsers);
}
function processMultiUsers(err, docs, options) {
for (j = 0; j < docs.length; j++) {
documentsProcessed++;
processUser(docs[j]);
}
querysCount++;
if (options.continuation) {
tryQuery(options.continuation);
} else {
setBody(null);
}
}
function processUser(doc, items) {
// do something with items...
doc.WishList = items;
var accept4 = collection.replaceDocument(doc._self, doc, { indexAction: "default" }, function (err, feed, options) {
if (err) throw err;
});
if (!accept4) documentsRejectFromQueue++;
}
function setBody(continuationToken) {
var body = { continuationToken: continuationToken, documentsProcessed: documentsProcessed, QuerysCount: querysCount, DocumentsReplaced: documentsReplaced, DocumentsRejectFromQueue: documentsRejectFromQueue};
getContext().getResponse().setBody(body);
}}
Upvotes: 0
Views: 242
Reputation: 303
Stored Procedures, like any request in DocumentDB, are set to execute in a bounded time interval. For a request that needs more time, there is a continuation mechanism. In stored procedures, each collection operation returns the Boolean flag to indicate if the request can be queued or not. If it's false, the function should wrap the current request and come back, from the client, with an entirely new request with the continuation token that's been preserved so far.
In your code, if you are noticing documentsRejectFromQueue to be non-zero, it indicates that the request ran out of the single round trip execution time. However, you could send a new request from the client with the continuation token that you got back from the response, and loop till you get back no continuation token from the response.
More details can be found here - https://azure.microsoft.com/en-us/documentation/articles/documentdb-programming
Upvotes: 1