-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add smart retries to table.mutate #17
Merged
stephenplusplus
merged 5 commits into
googleapis:master
from
kolodny:smart-mutate-retries
Dec 13, 2017
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41be320
Add smart retries to table.mutate
kolodny 921d1ef
moved integration-test to system tests addressed PR comments.
kolodny 5fa4aa6
Use implicit setTimeout for retries from retry-request.
kolodny e622bcd
Add unit tests for retries
kolodny e4c03fa
Small code style / convention changes.
stephenplusplus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
env: | ||
mocha: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
{ | ||
"tests": [ | ||
{ | ||
"name": "valid mutation", | ||
"max_retries": 3, | ||
"mutations_request": [ | ||
{ "method": "insert", "key": "foo", "data": {} }, | ||
{ "method": "insert", "key": "bar", "data": {} }, | ||
{ "method": "insert", "key": "baz", "data": {} } | ||
], | ||
"responses": [ | ||
{ "code": 200, "entry_codes": [ 0, 0, 0 ] } | ||
], | ||
"mutation_batches_invoked": [ | ||
[ "foo", "bar", "baz" ] | ||
] | ||
}, { | ||
"name": "retries the failed mutations", | ||
"max_retries": 3, | ||
"mutations_request": [ | ||
{ "method": "insert", "key": "foo", "data": {} }, | ||
{ "method": "insert", "key": "bar", "data": {} }, | ||
{ "method": "insert", "key": "baz", "data": {} } | ||
], | ||
"responses": [ | ||
{ "code": 200, "entry_codes": [ 0, 4, 4 ] }, | ||
{ "code": 200, "entry_codes": [ 4, 0 ] }, | ||
{ "code": 200, "entry_codes": [ 4 ] }, | ||
{ "code": 200, "entry_codes": [ 0 ] } | ||
], | ||
"mutation_batches_invoked": [ | ||
[ "foo", "bar", "baz" ], | ||
[ "bar", "baz" ], | ||
[ "bar" ], | ||
[ "bar" ] | ||
] | ||
}, { | ||
"name": "has a `PartialFailureError` error when an entry fails after the retries", | ||
"max_retries": 3, | ||
"mutations_request": [ | ||
{ "method": "insert", "key": "foo", "data": {} }, | ||
{ "method": "insert", "key": "bar", "data": {} }, | ||
{ "method": "insert", "key": "baz", "data": {} } | ||
], | ||
"responses": [ | ||
{ "code": 200, "entry_codes": [ 0, 4, 0 ] }, | ||
{ "code": 200, "entry_codes": [ 4 ] }, | ||
{ "code": 200, "entry_codes": [ 4 ] }, | ||
{ "code": 200, "entry_codes": [ 4 ] } | ||
], | ||
"mutation_batches_invoked": [ | ||
[ "foo", "bar", "baz" ], | ||
[ "bar" ], | ||
[ "bar" ], | ||
[ "bar" ] | ||
], | ||
"errors": [ | ||
{ "index_in_mutations_request": 1 } | ||
] | ||
}, { | ||
"name": "does not retry unretryable mutations", | ||
"max_retries": 5, | ||
"mutations_request": [ | ||
{ "method": "insert", "key": "foo", "data": {} }, | ||
{ "method": "insert", "key": "bar", "data": {} }, | ||
{ "method": "insert", "key": "baz", "data": {} }, | ||
{ "method": "insert", "key": "qux", "data": {} }, | ||
{ "method": "insert", "key": "quux", "data": {} }, | ||
{ "method": "insert", "key": "corge", "data": {} } | ||
], | ||
"responses": [ | ||
{ "code": 200, "entry_codes": [ 4, 4, 4, 4, 4, 1 ] }, | ||
{ "code": 200, "entry_codes": [ 10, 14, 10, 14, 0 ] }, | ||
{ "code": 200, "entry_codes": [ 1, 4, 4, 0 ] }, | ||
{ "code": 200, "entry_codes": [ 0, 4 ] }, | ||
{ "code": 200, "entry_codes": [ 4 ] }, | ||
{ "code": 200, "entry_codes": [ 1 ] } | ||
], | ||
"mutation_batches_invoked": [ | ||
[ "foo", "bar", "baz", "qux", "quux", "corge" ], | ||
[ "foo", "bar", "baz", "qux", "quux" ], | ||
[ "foo", "bar", "baz", "qux" ], | ||
[ "bar", "baz" ], | ||
[ "baz" ], | ||
[ "baz" ] | ||
], | ||
"errors": [ | ||
{ "index_in_mutations_request": 0 }, | ||
{ "index_in_mutations_request": 2 }, | ||
{ "index_in_mutations_request": 5 } | ||
] | ||
}, { | ||
"name": "considers network errors towards the retry count", | ||
"max_retries": 3, | ||
"mutations_request": [ | ||
{ "method": "insert", "key": "foo", "data": {} }, | ||
{ "method": "insert", "key": "bar", "data": {} }, | ||
{ "method": "insert", "key": "baz", "data": {} } | ||
], | ||
"responses": [ | ||
{ "code": 200, "entry_codes": [ 0, 4, 0 ] }, | ||
{ "code": 429 }, | ||
{ "code": 200, "entry_codes": [ 4 ] }, | ||
{ "code": 200, "entry_codes": [ 4 ] } | ||
], | ||
"mutation_batches_invoked": [ | ||
[ "foo", "bar", "baz" ], | ||
[ "bar" ], | ||
[ "bar" ], | ||
[ "bar" ] | ||
], | ||
"errors": [ | ||
{ "index_in_mutations_request": 1 } | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
'use strict'; | ||
|
||
const Bigtable = require('../'); | ||
|
||
const tests = require('./mutate-rows-acceptance-test.json').tests; | ||
|
||
const assert = require('assert'); | ||
const grpc = require('grpc'); | ||
const sinon = require('sinon'); | ||
const through = require('through2'); | ||
|
||
function dispatch(emitter, response) { | ||
const emits = []; | ||
emits.push({name: 'response', arg: {code: response.code}}); | ||
if (response.entry_codes) { | ||
emits.push({name: 'data', arg: entryResponses(response.entry_codes)}); | ||
} | ||
emits.push({name: 'end'}); | ||
let index = 0; | ||
setImmediate(next); | ||
|
||
function next() { | ||
if (index < emits.length) { | ||
const emit = emits[index]; | ||
index++; | ||
emitter.emit(emit.name, emit.arg); | ||
setImmediate(next); | ||
} | ||
} | ||
} | ||
|
||
function entryResponses(statusCodes) { | ||
return { | ||
entries: statusCodes.map((code, index) => ({ | ||
index, | ||
status: {code}, | ||
})), | ||
}; | ||
} | ||
|
||
function getDeltas(array) { | ||
return array.reduce((acc, item, index) => { | ||
return index ? acc.concat(item - array[index - 1]) : [item]; | ||
}, []); | ||
} | ||
|
||
describe('Bigtable/Table', () => { | ||
const bigtable = new Bigtable(); | ||
bigtable.grpcCredentials = grpc.credentials.createInsecure(); | ||
const bigtableService = bigtable.getService_({service: 'Bigtable'}); | ||
|
||
const INSTANCE = bigtable.instance('instance'); | ||
const TABLE = INSTANCE.table('table'); | ||
|
||
describe('mutate()', () => { | ||
let clock; | ||
let mutationBatchesInvoked; | ||
let mutationCallTimes; | ||
let responses; | ||
let stub; | ||
|
||
beforeEach(() => { | ||
clock = sinon.useFakeTimers({ | ||
toFake: [ | ||
'setTimeout', | ||
'clearTimeout', | ||
'setImmediate', | ||
'clearImmediate', | ||
'setInterval', | ||
'clearInterval', | ||
'Date', | ||
'nextTick', | ||
], | ||
}); | ||
mutationBatchesInvoked = []; | ||
mutationCallTimes = []; | ||
responses = null; | ||
stub = sinon.stub(bigtableService, 'mutateRows').callsFake(grpcOpts => { | ||
mutationBatchesInvoked.push( | ||
grpcOpts.entries.map(entry => entry.rowKey.asciiSlice()) | ||
); | ||
mutationCallTimes.push(new Date().getTime()); | ||
const emitter = through.obj(); | ||
dispatch(emitter, responses.shift()); | ||
return emitter; | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
clock.uninstall(); | ||
stub.restore(); | ||
}); | ||
|
||
tests.forEach(test => { | ||
it(test.name, done => { | ||
responses = test.responses; | ||
TABLE.maxRetries = test.max_retries; | ||
TABLE.mutate(test.mutations_request, error => { | ||
assert.deepEqual( | ||
mutationBatchesInvoked, | ||
test.mutation_batches_invoked | ||
); | ||
getDeltas(mutationCallTimes).forEach((delta, index) => { | ||
if (index === 0) { | ||
const message = 'First request should happen Immediately'; | ||
assert.strictEqual(index, 0, message); | ||
return; | ||
} | ||
const minBackoff = 1000 * Math.pow(2, index); | ||
|
||
// Adjust for some flakiness with the fake timers. | ||
const maxBackoff = minBackoff + 1010; | ||
const message = | ||
`Backoff for retry #${index} should be between ` + | ||
`${minBackoff} and ${maxBackoff}, was ${delta}`; | ||
assert(delta > minBackoff, message); | ||
assert(delta < maxBackoff, message); | ||
}); | ||
if (test.errors) { | ||
const expectedIndices = test.errors.map(error => { | ||
return error.index_in_mutations_request; | ||
}); | ||
const actualIndices = error.errors.map(error => { | ||
return test.mutations_request.indexOf(error.entry); | ||
}); | ||
assert.deepEqual(expectedIndices, actualIndices); | ||
} else { | ||
assert.ifError(error); | ||
} | ||
done(); | ||
}); | ||
clock.runAll(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- a/node_modules/through2/node_modules/readable-stream/node_modules/process-nextick-args/index.js | ||
+++ b/node_modules/through2/node_modules/readable-stream/node_modules/process-nextick-args/index.js | ||
@@ -5,7 +5,9 @@ if (!process.version || | ||
process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { | ||
module.exports = nextTick; | ||
} else { | ||
- module.exports = process.nextTick; | ||
+ module.exports = function() { | ||
+ return process.nextTick.apply(this, arguments); | ||
+ }; | ||
} | ||
|
||
function nextTick(fn, arg1, arg2, arg3) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- a/node_modules/process-nextick-args/index.js | ||
+++ b/node_modules/process-nextick-args/index.js | ||
@@ -5,7 +5,9 @@ if (!process.version || | ||
process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { | ||
module.exports = nextTick; | ||
} else { | ||
- module.exports = process.nextTick; | ||
+ module.exports = function() { | ||
+ return process.nextTick.apply(this, arguments); | ||
+ }; | ||
} | ||
|
||
function nextTick(fn, arg1, arg2, arg3) { |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.