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

Refactor Kinetic lib #24

Merged
merged 6 commits into from
Nov 3, 2015
Merged

Conversation

adrienverge
Copy link
Contributor

Refactor Kinetic lib for static constants and PDU class

Big refactor of the Kinetic library:

  • move constants out of instances (memory footprint)
  • give a proper name to instances (Kinetic.PDU)
  • modify tests consequently
  • document library usage in README.md:

Fixes: #18


Kinetic: Define PDU commands as classes

Currently to create a "PUT" Kinetic PDU, one must run:

const pdu = new Kinetic.PDU();
pdu.put();

This is not object-oriented programming, and is error-prone since we don't expect commands like put, flush etc. to create messages with a given type.

This patch creates classes that inherit from PDU, e.g. PutPDU, GetPDU, GetLogPDU etc. and that can be used the same way.


Sample from README:

import Kinetic from 'Kinetic';

const rawData = new Buffer('\x46\x00\x00\x00\x32\x00');
const kineticPDU = new Kinetic.PDU(rawData);

if (kineticPDU.getMessageType() === Kinetic.ops.GET) {
    const response = new Kinetic.PutPDU('key', 1, null, '1', 0);
    response.setChunk(new Buffer('value'));

    const sock = net.connect(1234, 'localhost');
    const ret = response.send(sock);
    if (ret !== Kinetic.errors.SUCCESS)
        throw new Error("argh: " + Kinetic.getErrorName(ret) + "!");
}

Old tests were buggy (HMAC problems) and we don't have guarantees on the
messages contents validity. Now that we have trustable tests with a
third-party simulator, we don't need them anymore.
@ghost
Copy link

ghost commented Nov 3, 2015

LGTM. Good work on refining that library.

@adrienverge adrienverge force-pushed the dev/CLEANUP/refactor-kinetic-lib branch from 2aed87e to 5b2769a Compare November 3, 2015 13:23
@adrienverge
Copy link
Contributor Author

Thanks @MichaelZapataScality. I'm currently adding some more changes to this PR so @AntoninCoulibaly and I can write unit tests on sane basis.

@adrienverge adrienverge changed the title Refactor Kinetic lib for static constants and PDU class Refactor Kinetic lib Nov 3, 2015
@adrienverge adrienverge force-pushed the dev/CLEANUP/refactor-kinetic-lib branch from 5c43b2a to 840eb58 Compare November 3, 2015 14:25
@koolfunky
Copy link

👍 Nice refactor ;)

@adrienverge
Copy link
Contributor Author

Ready for review from you @MichaelZapataScality 🔎 ;-)

response.setChunk(new Buffer('value'));

const sock = net.connect(1234, 'localhost');
const ret = response.send(sock);
Copy link

Choose a reason for hiding this comment

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

response.send(sock) ? I might be too used to the sock.send(response) architecture, but it looks strange to me, in OOP terms.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not change this. This PR is a refactor only.

Copy link

Choose a reason for hiding this comment

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

okay, might be for another round then :)

@ghost
Copy link

ghost commented Nov 3, 2015

Despite all my remarks that are not completely relevant to this PR (due to the fact that I've never reviewed any code from Kinetic PRs until now), I'm ok with this PR, though I think it will require a cleanup soon enough, for which I'm ready to make myself available to discuss the different details I commented here.
So in the meantime, for this PR 👍

@ghost ghost mentioned this pull request Nov 3, 2015
@adrienverge adrienverge force-pushed the dev/CLEANUP/refactor-kinetic-lib branch from 840eb58 to cbb6a28 Compare November 3, 2015 16:08
@adrienverge
Copy link
Contributor Author

@DavidPineauScality Thanks for your comments, I addressed them.

Everything should be fine now.

Big refactor of the Kinetic library:
- move constants out of instances (memory footprint)
- give a proper name to instances (`Kinetic.PDU`)
- modify tests consequently
- document library usage in README.md

    import Kinetic from 'Kinetic';

    const rawData = new Buffer('\x46\x00\x00\x00\x32\x00');
    const kineticPDU = new Kinetic.PDU(rawData);

    const sock = net.connect(1234, 'localhost');
    const ret = kineticPDU.send(sock);
    if (ret !== Kinetic.errors.SUCCESS)
        throw new Error("argh: " + Kinetic.getErrorName(ret) + "!");

Fixes: #18
The Node.js standard `Buffer.equals()` already exists for that.
- Add a `_computeHMAC` function to avoid code duplication.
- Have consistent return type in `hmacIntegrity` instead of returning
  either `true` or `errors.HMAC_FAILURE`.
Currently to create a "PUT" Kinetic PDU, one must run:

    const pdu = new Kinetic.PDU();
    pdu.put();

This is not object-oriented programming, and is error-prone since
we don't expect commands like `put`, `flush` etc. to create messages
with a given type.

This patch creates classes that inherit from `PDU`, e.g. `PutPDU`,
`GetPDU`, `GetLogPDU` etc. and that can be used the same way.
Remove an unneeded test that was provoking errors.HMAC when parsing
Kinetic PDUs.
@adrienverge adrienverge force-pushed the dev/CLEANUP/refactor-kinetic-lib branch from cbb6a28 to b8e799d Compare November 3, 2015 16:45
@ghost
Copy link

ghost commented Nov 3, 2015

👍

adrienverge added a commit that referenced this pull request Nov 3, 2015
@adrienverge adrienverge merged commit bab1d69 into master Nov 3, 2015
@adrienverge adrienverge deleted the dev/CLEANUP/refactor-kinetic-lib branch November 3, 2015 17:14
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.

2 participants