Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a required thrift field optional?

Tags:

thrift

What is the best process for making a required field optional in thrift. For example, I have a struct...

struct Message {
    1: required double userID;
    2: required string content;
    ...
} 

... but I want to make content optional.

EDIT: To clarify, I already have consumers that use this struct, so I would need to update it without breaking those consumers. A staged upgrade is fine (ie - add a new optional field, update the downstream clients, then remove--or stop using--the old required field).

like image 205
AJH Avatar asked Jun 01 '16 02:06

AJH


People also ask

How do you make a field optional?

In some languages it's quite elegant and simple wherein we just append a question mark after the field name. There is no concept for this in Java. Most people would just assign null to it and return Optional<String> on methods that would return it.

What is a struct in thrift?

Thrift structs define a common object – they are essentially equivalent to classes in OOP languages, but without inheritance. A struct has a set of strongly typed fields, each with a unique name identifier. Fields may have various annotations (numeric field IDs, optional default values, etc.)


1 Answers

It is possible to do this if you control all the readers and writers of the struct in the following steps :

  1. Change the field in the thrift schema from required to optional. Sync this new schema in all the systems that refer to or use this struct. Do not stop writing this field. This is a safe change as the field will still be always present.
  2. Only after the schema change has been deployed in all the systems using this struct, you can stop writing this field in some cases. This is now safe to do as this field is marked as optional in every system, there are no "old readers" as referred to in the other answer.
like image 196
Hitesh Gupta Avatar answered Oct 21 '22 05:10

Hitesh Gupta