Skip to content

Document JSON protocol #2225

Closed
Closed

Description

@mlucy
No description provided.

Activity

  1. added this to the 1.13 milestone on Apr 7, 2014
  2. self-assigned this
    on Apr 7, 2014
  3. mglukhovsky commented on Apr 8, 2014

    @mglukhovsky
    Member

    We should work with driver developers well in advance of the 1.13 release if possible so that they have a heads up on the new JSON API, once it's in a more stable form.

  4. coffeemug commented on Apr 8, 2014

    @coffeemug
    Contributor

    @mglukhovsky -- me, you, and @mlucy discussed this before. 1.13 will still ship with the protobuf API, so we won't break anything. It's better to ship a concrete implementation and then tell people, so they can migrate the drivers at their own pace.

  5. mglukhovsky commented on Apr 8, 2014

    @mglukhovsky
    Member

    @coffeemug, I remember, I'm fine with that. However, I think that it'd be nice to send out @mlucy's typical email to driver developers notifying them of changes sooner than we typically do (by a week or two, if possible).

    Obviously, this shouldn't get in the way of ensuring a good implementation of the JSON API.

  6. neumino commented on Apr 8, 2014

    @neumino
    Member

    We should write docs as soon as possible because

    • We could find edge cases/bugs while writing it.
    • We won't be in a rush to write this doc -- we are always in a rush for the docs before releasing.
    • We should give the chance to our drivers developers to release at the same time as we release the server.
  7. Tryneus commented on Apr 18, 2014

    @Tryneus
    Member

    Ok, I've done a good amount of work on the protocol, so I'm putting a partial writeup here in case it's useful. Could still use some more details and examples.

    Driver Handshake

    The client should send the following things to the server's driver port (default is 28015):

    • 4-byte version magic number, little-endian, corresponding to VersionDummy::Version::V0_3, for this version of the protocol
    • 4-byte length, little-endian, giving the length of the authorization key string (this may be 0 if no authorization key is given)
    • authorization key string, in ASCII
    • 4-byte wire protocol magic number, little-endian, corresponding to VersionDummy::Protocol::JSON, for this version of the protocol

    The server will then reply with a NULL-terminated ASCII string regarding the result of the connection attempt. If the string is "SUCCESS", the client may begin sending queries and receive responses, as detailed below. On an error, the string should contain some description of why the attempt failed, and the TCP connection will be closed.

    Example

    No authorization key

    Client to Server:

    Version Auth Length Wire Protocol
    3e e8 75 5f 00 00 00 00 c7 70 69 7e
    0x5f75e83e 0x00000000 0x7e6970c7

    Server to Client:

    Response String NULL
    53 55 43 43 45 53 53 00
    "SUCCESS"

    Incorrect Authorization Key

    Client to Server:

    Version Auth Length Auth Key Wire Protocol
    3e e8 75 5f 07 00 00 00 68 75 6e 74 65 72 32 c7 70 69 7e
    0x5f75e83e 0x00000007 "hunter2" 0x7e6970c7

    Server to Client:

    Response String NULL
    45 52 52 4f 52 3a 20 49 6e 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 6b 65 79 2e 00
    "ERROR: Incorrect authorization key."

    Queries

    The query format (client to server) is as follows:

    • 8-byte token, little-endian, providing a unique ID for the query
    • 4-byte length, little-endian, giving the length in bytes of the JSON string following this
    • JSON string request, as an array with the following elements:
      • The type of the query, an integer given by Query::QueryType in ql2.proto
      • The terms for the query, (see below). This should only be included for queries with Query::QueryType::START.
      • The global optargs for the query, as a dict (see below). This should only be included for queries with Query::QueryType::START, and may be omitted if the dict has no entries.

    Query terms:
    TODO

    Valid global optargs:

    • db - a string giving the default database to use if no r.db(...) is included in the query
    • use_outdated - a boolean indicating whether outdated reads may be performed
    • noreply - a boolean telling the server whether to send a response for the query
    • profile - a boolean telling the server whether to include profiler data in the response
    • durability - a string telling the server the type of write durability to use, possible values are "hard" and "soft"

    Examples

    TODO

    Responses

    The response format (server to client) is as follows:

    • 8-byte token, little-endian, corresponding to the token given in a previous query
    • 4-byte length, little-endian, giving the length in bytes of the JSON string following this
    • JSON string response as a dict with the following keys:
      • t - required, the type of the response, an integer given by Response::ResponseType in ql2.proto
      • r - required, the result data from the query, as an array
      • b - optional, the backtrace from the query if an error occurred
      • p - optional, the profile data from the query if profile=True was specified in the global optargs

    Examples

    TODO

    Atom

    Sequence

    Error

    Things to Be Careful About

    • Unicode string lengths
      • When passing length-prefixed strings to the server, make sure that the length given is the number of bytes, not the number of characters
    • NaN/Infinity
      • NaN and Infinity are not allowed in Datums on the server
      • These values are not valid in JSON, so some libraries might write them as strings
      • The driver should reject NaN and Infinity rather than attempt to send them to the server
  8. tomjakubowski commented on Apr 18, 2014

    @tomjakubowski

    @Tryneus thank you! I've been working on a driver for Rust and was able to piece together some of the new protocol from the new Ruby driver and json_shim.cc but a comprehensive document like this is worth its weight in gold. 🍺

  9. Tryneus commented on Apr 18, 2014

    @Tryneus
    Member

    @tomjakubowski, I should mention, some of this isn't in next yet, since it's still in code review. But everything including the updated python and javascript drivers are in my branch grey_issue_2224_js if that helps at all.

  10. srh commented on Apr 18, 2014

    @srh
    Contributor

    4-byte length, little-endian, giving the length in bytes of the JSON string following this

    Why is it a 4-byte length instead of 8 bytes or a varint?

  11. srh commented on Apr 18, 2014

    @srh
    Contributor

    The token for the query, a 64-bit integer which will be included in the response for this query, to uniquely identify it

    How can this possibly be a 64-bit integer, if it's encoded in a JSON string?

  12. srh commented on Apr 18, 2014

    @srh
    Contributor

    Wait, the requests send a token in JSON and the response receives it outside JSON??

  13. srh commented on Apr 18, 2014

    @srh
    Contributor

    Also, I'm told by somebody writing a driver that the JSON response contains the token inside of itself. Is this true? Is the response actually sending the token twice?

  14. tomjakubowski commented on Apr 18, 2014

    @tomjakubowski

    That appears to have been removed on the topic branch mentioned earlier f64981e. My mistake.

  15. Tryneus commented on Apr 18, 2014

    @Tryneus
    Member

    @srh:

    Why is it a 4-byte length instead of 8 bytes or a varint?

    I don't think we have any intention of allowing queries or responses greater than 4 GB in one message. I can change this, if you think otherwise.

    How can this possibly be a 64-bit integer, if it's encoded in a JSON string?

    Good point, it's treated on the server as a 64-bit integer, but I should have just said 'integer'

    Wait, the requests send a token in JSON and the response receives it outside JSON?

    We could send the token outside of JSON. That might actually be better for certain error cases in the server. We receive it outside of JSON so that the driver can know what query it is even if parsing fails.

    Is the response actually sending the token twice?

    No, I removed that because redundancy like that just adds more surface area for errors.

  16. mlucy commented on Apr 18, 2014

    @mlucy
    MemberAuthor

    I think we should pull the token out of requests as well as responses -- there's no reason we can't just send the token right before the size of the JSON string (in fact it's probably better, because if we fail to parse the JSON string we can tell the client what token we failed to parse on).

  17. Tryneus commented on Apr 18, 2014

    @Tryneus
    Member

    @mlucy, agreed, I'll change that.

  18. Tryneus commented on Apr 18, 2014

    @Tryneus
    Member

    Ok, opened #2283 for moving the token out, working on that now.

  19. Tryneus commented on Apr 23, 2014

    @Tryneus
    Member

    Updated the description for the changes from #2283. Also of note, the wire protocol is now a magic number rather than a string, values are in ql2.proto.

  20. mlucy commented on Apr 24, 2014

    @mlucy
    MemberAuthor

    @Tryneus -- since you've already started a writeup for this, could I re-assign to you?

  21. Tryneus commented on Apr 24, 2014

    @Tryneus
    Member

    @mlucy, sure, I'll go ahead and add some more details.

  22. assigned and unassigned on Apr 24, 2014
  23. neumino commented on May 2, 2014

    @neumino
    Member

    For those that may be interested, I just pushed some changes on rethinkdb-driver-development.
    Check out the branch v1.13 if you want to see how the JSON protocol works.

    https://github.com/neumino/rethinkdb-driver-development/tree/v1.13

  24. coffeemug commented on May 28, 2014

    @coffeemug
    Contributor

    @mlucy -- do you think the doc up in #2225 (comment) is good enough? Is there anything left to do here?

  25. mlucy commented on May 28, 2014

    @mlucy
    MemberAuthor

    @coffeemug -- several sections of that comment are still marked TODO.

  26. coffeemug commented on May 28, 2014

    @coffeemug
    Contributor

    Ok -- closing this in favor of rethinkdb/docs#320.

  27. modified the milestones: moved, 1.13 on May 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions