thrift

What is the purpose of Thrift parameter numbering?


What is the purpose of the numbers before each parameter (field identifiers)? Why does it jump from 5 to 16?

struct Tweet {
    1: required i32 userId;
    2: required string userName;
    3: required string text;
    4: optional Location loc;
    5: optional TweetType tweetType = TweetType.TWEET;
    16: optional string language = "english";
}

(Snippet from http://diwakergupta.github.io/thrift-missing-guide/)

I've been trying to find the answer to this for a while and haven't found anything in the docs.


Solution

  • That's the so-called field ID, a 16-bit (signed) integer. Except for the data itself, this is the only thing that goes over the wire to allow the other side correct identification of the field to which the data belong to.

    In earlier times the system automatically provided these IDs internally, which led to incompatibilities: if someone changed the order of fields, added a field between others or removed fields. For compatibility reasons, you can still omit the numbers and have the system allocate negative IDs automatically1):

    struct yeolde {
        i32 foo
        string bar
    }
    

    but now you get a nice warning about it:

    $ thrift -gen csharp test.thrift
    [WARNING:test.thrift:3] No field key specified for foo, resulting protocol may have conflicts or not be backwards compatible!
    [WARNING:test.thrift:4] No field key specified for bar, resulting protocol may have conflicts or not be backwards compatible!
    

    Why does it jump from 5 to 16?

    One may have decided that to be a good idea. There are not that many restrictions other than that the number has to be a positive 32-bit value > 0.

    Or there have been fields that have been removed in the meantime. Especially for the latter case, it is recommended to comment outdated fields but leave them in the IDL to prevent compatibility accidents because someone "reused" an already used and outdated old field numbers for new purposes.


    1)That's the reason why negative ID numbers are not allowed.