Search code examples
thrift

What does 1: mean in thrift parameter list


An example from thrift website

int multiply(1:int n1, 2:int n2); 

Why do we have to use the labels 1: and 2: before variable names, what's the purpose of this label?


Solution

  • Those numbers ("1:" and "2:") are the ordinal values of the parameters. Ordinal is a fancy way to say "position in a series", however, the numbers need not be sequential, only unique.

    [TLDR]

    In Apache Thrift IDL, ordinals are assigned to each parameter in a parameter list and each field in a struct. When client programs call an Apache Thrift server they do not pass the parameter name, they pass the much smaller ordinal, a number representing the parameter type and the serialized parameter value.

    Not only does this save space on the wire, this also allows you to add parameters to a function over time and to send parameters in any order without breaking clients or servers. If an Apache Thrift program receives a parameter with an ordinal it does not recognize it ignores it.

    Also if a parameter is missing a default value can be used. Here's another example service with a default value assigned to the allowPartialMatch parameter:

    service SocialLookup {
        string GetSiteByRank( 1: i32 rank )
        i32 GetSiteRankByName(1: string name, 2: bool allowPartialMatch=false)
        list<string> GetSitesByUsers(1: i32 minUserCount, 2: i32 maxUserCount)
    }
    

    If a client calls the GetSiteRankByName method and does not pass param "2:", the default is used. Allowing interfaces to evolve like this is important in environments where you want to release new versions of individual services without having to update all of the users of the service at the same time, e.g. microservices and CI/CD.

    Importantly, once set, you should never change an ordinal because it could cause a given client and server to misunderstand each other. You can remove parameters but its best to comment them out so that everyone knows not to reuse the ordinal.

    [/TLDR]