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

Added request options for DataSource retry info #970

Merged
merged 2 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 0 additions & 10 deletions lib/HttpDataSource.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ Model.prototype._hasValidParentReference = require("./deref/hasValidParentRefere
* @param {Path} path - the path to retrieve
* @return {Observable.<*>} - the value for the path
* @example
var model = new falcor.Model({source: new falcor.HttpDataSource("/model.json") });
var model = new falcor.Model({source: new HttpDataSource("/model.json") });

model.
getValue('user.name').
Expand All @@ -373,7 +373,7 @@ Model.prototype.getValue = require("./get/getValue");
* @param {Object} value - the value to set
* @return {Observable.<*>} - the value for the path
* @example
var model = new falcor.Model({source: new falcor.HttpDataSource("/model.json") });
var model = new falcor.Model({source: new HttpDataSource("/model.json") });

model.
setValue('user.name', 'Jim').
Expand Down
9 changes: 8 additions & 1 deletion lib/request/GetRequestV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var $error = require("./../types/error");
var emptyArray = [];
var InvalidSourceError = require("./../errors/InvalidSourceError");

/**
* @typedef DataSourceRequestOptions
* @property {number} retryCount The retry count for this get request based on the request cycle.
*/

/**
* Creates a new GetRequest. This GetRequest takes a scheduler and
* the request queue. Once the scheduler fires, all batched requests
Expand All @@ -19,15 +24,17 @@ var InvalidSourceError = require("./../errors/InvalidSourceError");
*
* @param {Scheduler} scheduler -
* @param {RequestQueueV2} requestQueue -
* @param {DataSourceRequestOptions} dsRequestOpts
*/
var GetRequestV2 = function(scheduler, requestQueue) {
var GetRequestV2 = function(scheduler, requestQueue, dsRequestOpts) {
this.sent = false;
this.scheduled = false;
this.requestQueue = requestQueue;
this.id = ++REQUEST_ID;
this.type = GetRequestType;

this._scheduler = scheduler;
this._dataSourceRequestOptions = dsRequestOpts;
this._pathMap = {};
this._optimizedPaths = [];
this._requestedPaths = [];
Expand Down
28 changes: 23 additions & 5 deletions lib/request/RequestQueueV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ var sendSetRequest = require("./sendSetRequest");
var GetRequest = require("./GetRequestV2");
var falcorPathUtils = require("falcor-path-utils");

/**
* @typedef DataSourceRequestOptions
* @property {number} retryCount The retry count for this request based on the get/set request cycle.
*/

/**
* The request queue is responsible for queuing the operations to
* the model"s dataSource.
Expand All @@ -29,24 +34,32 @@ RequestQueueV2.prototype = {
* currently could be batched potentially in the future. Since no batching
* is required the setRequest action is simplified significantly.
*
* @param {JSONGraphEnvelope) jsonGraph -
* @param {JSONGraphEnvelope} jsonGraph -
* @param {DataSourceRequestOptions} dsRequestOpts
* @param {Function} cb
*/
set: function(jsonGraph, cb) {
set: function(jsonGraph, dsRequestOpts, cb) {
if (this.model._enablePathCollapse) {
jsonGraph.paths = falcorPathUtils.collapse(jsonGraph.paths);
}

return sendSetRequest(jsonGraph, this.model, cb);
if (cb === undefined) {
cb = dsRequestOpts;
dsRequestOpts = undefined;
}

return sendSetRequest(jsonGraph, this.model, dsRequestOpts, cb);
},

/**
* Creates a get request to the dataSource. Depending on the current
* scheduler is how the getRequest will be flushed.
* @param {Array} requestedPaths -
* @param {Array} optimizedPaths -
* @param {DataSourceRequestOptions} dsRequestOpts
* @param {Function} cb -
*/
get: function(requestedPaths, optimizedPaths, cb) {
get: function(requestedPaths, optimizedPaths, dsRequestOpts, cb) {
var self = this;
var disposables = [];
var count = 0;
Expand All @@ -57,6 +70,11 @@ RequestQueueV2.prototype = {
var disposed = false;
var request;

if (cb === undefined) {
cb = dsRequestOpts;
dsRequestOpts = undefined;
}

for (i = 0, len = requests.length; i < len; ++i) {
request = requests[i];
if (request.type !== RequestTypes.GetRequest) {
Expand Down Expand Up @@ -99,7 +117,7 @@ RequestQueueV2.prototype = {
// After going through all the available requests if there are more
// paths to process then a new request must be made.
if (oRemainingPaths && oRemainingPaths.length) {
request = new GetRequest(self.scheduler, self);
request = new GetRequest(self.scheduler, self, dsRequestOpts);
requests[requests.length] = request;
++count;
var disposable = request.batch(rRemainingPaths, oRemainingPaths, refCountCallback);
Expand Down
2 changes: 1 addition & 1 deletion lib/request/flushGetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = function flushGetRequest(request, pathSetArrayBatch, callback)
// we cancel at the callback above.
var getRequest;
try {
getRequest = model._source.get(requestPaths);
getRequest = model._source.get(requestPaths, request._dataSourceRequestOptions);
} catch (e) {
callback(new InvalidSourceError());
return null;
Expand Down
10 changes: 8 additions & 2 deletions lib/request/sendSetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ var InvalidSourceError = require("./../errors/InvalidSourceError");
var emptyArray = [];
var emptyDisposable = {dispose: function() {}};

/**
* @typedef DataSourceRequestOptions
* @property {number} retryCount The retry count for this set request based on the request cycle.
*/

/**
* A set request is not an object like GetRequest. It simply only needs to
* close over a couple values and its never batched together (at least not now).
*
* @private
* @param {JSONGraphEnvelope} jsonGraph -
* @param {Model} model -
* @param {DataSourceRequestOptions} dsRequestOpts
* @param {Function} callback -
*/
var sendSetRequest = function(originalJsonGraph, model, callback) {
var sendSetRequest = function(originalJsonGraph, model, dsRequestOpts, callback) {
var paths = originalJsonGraph.paths;
var modelRoot = model._root;
var errorSelector = modelRoot.errorSelector;
Expand All @@ -28,7 +34,7 @@ var sendSetRequest = function(originalJsonGraph, model, callback) {
var setObservable;
try {
setObservable = model._source.
set(originalJsonGraph);
set(originalJsonGraph, dsRequestOpts);
} catch (e) {
callback(new InvalidSourceError());
return emptyDisposable;
Expand Down
2 changes: 1 addition & 1 deletion lib/response/get/getRequestCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function getRequestCycle(getResponse, model, results, observer,
}

var currentRequestDisposable = requestQueue.
get(boundRequestedMissingPaths, optimizedMissingPaths, function(err, data, hasInvalidatedResult) {
get(boundRequestedMissingPaths, optimizedMissingPaths, { retryCount: count }, function(err, data, hasInvalidatedResult) {
if (model._treatDataSourceErrorsAsJSONGraphErrors ? err instanceof InvalidSourceError : !!err) {
if (results.hasValues) {
observer.onNext(results.values && results.values[0]);
Expand Down
2 changes: 1 addition & 1 deletion lib/response/set/SetResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SetResponse.prototype._subscribe = function _subscribe(observer) {

// Starts the async request cycle.
return setRequestCycle(
model, observer, groups, isJSONGraph, isProgressive, 0);
model, observer, groups, isJSONGraph, isProgressive, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tquetano-netflix The initial value was changed here, in coordination with the retry logic condition.

};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/response/set/setRequestCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function setRequestCycle(model, observer, groups,
var requestedPaths = requestedAndOptimizedPaths.requestedPaths;

// we have exceeded the maximum retry limit.
if (count === model._maxRetries) {
if (count > model._maxRetries) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking considering the original comparison logic, should this be >=?

Copy link
Contributor Author

@jcranendonk jcranendonk Jan 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the change to align the retry counts with getRequestCycle; one started at 0, the other at 1. Now they both start at 1, which lines up better with the Zuul retry header value.

observer.onError(new MaxRetryExceededError(optimizedPaths));
return {
dispose: function() {}
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports = function setRequestCycle(model, observer, groups,
// If disposed before this point then the sendSetRequest will not
// further any callbacks. Therefore, if we are at this spot, we are
// not disposed yet.
set(currentJSONGraph, function(error, jsonGraphEnv) {
set(currentJSONGraph, { retryCount: count }, function(error, jsonGraphEnv) {
if (error instanceof InvalidSourceError) {
observer.onError(error);
return;
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/data/LocalDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LocalSource.prototype = {
this.model = new falcor.Model({cache: modelOrCache});
}
},
get: function(paths) {
get: function(paths, dsRequestOpts) {
var self = this;
var options = this._options;
var miss = options.miss;
Expand All @@ -42,7 +42,7 @@ LocalSource.prototype = {
var results;
var values = [{}];
if (self._missCount >= miss) {
onGet(self, paths);
onGet(self, paths, dsRequestOpts);
self.model._getPathValuesAsJSONG(self.model, paths, values, errorSelector);
} else {
self._missCount++;
Expand All @@ -68,7 +68,7 @@ LocalSource.prototype = {
}
});
},
set: function(jsongEnv) {
set: function(jsongEnv, dsRequestOpts) {
var self = this;
var options = this._options;
var miss = options.miss;
Expand All @@ -82,7 +82,7 @@ LocalSource.prototype = {
var tempModel = new falcor.Model({
cache: jsongEnv.jsonGraph,
errorSelector: errorSelector});
jsongEnv = onSet(self, tempModel, jsongEnv);
jsongEnv = onSet(self, tempModel, jsongEnv, dsRequestOpts);

tempModel.set(jsongEnv).subscribe();
tempModel._getPathValuesAsJSONG(
Expand Down
Loading