Skip to content

read-only transactions for use on read-replicas - #7324

Merged
sushantdhiman merged 2 commits into
sequelize:masterfrom
xkjyeah:master
Mar 20, 2017
Merged

sushantdhiman merged 2 commits into
sequelize:masterfrom
xkjyeah:master

Conversation

@xkjyeah

@xkjyeah xkjyeah commented Mar 3, 2017

Copy link
Copy Markdown
Contributor

Closes #7323

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does your issue contain a link to existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Have you added an entry under Future in the changelog?

NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.

Description of change

transaction() accepts one more option, called readOnly. If true, the transaction is run on the read-replica.

@sushantdhiman sushantdhiman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few comments about tests, otherwise it will be a good feature to have

Comment thread test/integration/replication.test.js Outdated
return this.User.findAll();
});

function setupTransactionTestOptions() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rather than creating connection with master / salve once again, you can use already configured connection this.sequelize and this.User and test with them

Comment thread test/integration/replication.test.js Outdated
var connectStub = sandbox.stub(sequelize.connectionManager, '_connect').returns(resolvedPromise);

return sequelize.transaction({readOnly: true}, (transaction) => {})
.catch(() => { // Database doesn't exist, so query *will* fail

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use this.sequelize backed connection and assert with readOnly: true read replica gets utilized

Comment thread lib/transaction.js Outdated
* @param {String} options.type=true Sets the type of the transaction.
* @param {String} options.isolationLevel=true Sets the isolation level of the transaction.
* @param {String} options.deferrable Sets the constraints to be deferred or immediately checked.
* @param {String} options.readOnly=true Sets the read-only property of the transaction, allowing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

options.readOnly=false Sets read-only property of transaction, such transaction will use read replicas when available

@codecov-io

codecov-io commented Mar 4, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #7324 into master will increase coverage by <.01%.
The diff coverage is 100%.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2167194...3cf2322. Read the comment docs.

@xkjyeah

xkjyeah commented Mar 4, 2017

Copy link
Copy Markdown
Contributor Author

@sushantdhiman Thanks for the comments. The PR is amended as suggested


it('should run read-only transactions on the replica', () => {
return this.sequelize.transaction({readOnly: true}, (transaction) => {})
.then(expectReadCalls);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Read something here, just to be sure User.findAll call will do

Comment thread lib/transaction.js
let connectionPromise;

return Utils.Promise.resolve(this.parent ? this.parent.connection : this.sequelize.connectionManager.getConnection({ uuid: this.id }))
if (this.parent) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel this can be improved a whole lot

const acquireOptions = { uuid: this.id };

if (this.options.readOnly) {
  acquireOptions.type = 'SELECT';
}

return Utils.Promise.resolve(
  this.parent.connection
    ? this.parent.connection
    : this.sequelize.connectionManager.getConnection(acquireOptions)
)

Comment thread test/integration/replication.test.js Outdated
const DataTypes = require(__dirname + '/../../lib/data-types');
const dialect = Support.getTestDialect();
const sinon = require('sinon');
const _ = require('lodash');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No lodash needed in this code

@xkjyeah

xkjyeah commented Mar 5, 2017

Copy link
Copy Markdown
Contributor Author

Hi, changed as requested

@sushantdhiman
sushantdhiman merged commit a7e3104 into sequelize:master Mar 20, 2017
mkaufmaner pushed a commit to mkaufmaner/sequelize that referenced this pull request Apr 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants