DagrA guide to binary formats, and to Dagrdraft

16 Chapter sixteen

Change: when the schema has to grow

Nearly every chapter so far had one schema, and the writer and the reader agreed on it. Where an older reader appeared, it was in passing. That holds on the day a program is first released and seldom after. Data stays in files for years, and programs are replaced one at a time. This chapter is about changing a schema while data and programs made with the old one are still around.

Two directions

When a schema changes there are, for a while, two versions of everything. Two questions follow, and they have names that are easy to mix up.

A change can be fine in one direction and fatal in the other. Most systems need both, because nobody can update every program and every stored byte in the same instant.

What a field is, to the bytes

The earlier chapters already contain the answer to most of this. A field's name is never in the data. What stands for it is its position in the schema: its entry in the vtable of a regular node (chapter 5), or the number in its tag in a packed node (chapter 6). Three rules follow from that alone.

Below is a small schema, a shop's product, and thirteen things one might do to it. For each, the panel shows what both versions write, what each makes of the other's bytes, and what Dagr's own check says about the change.

Version 1

Version 2

What each version writes

What each makes of the other's bytes

    What dagr check says

    
      

    Going through the list gives three groups.

    Removing a field is a case of its own. Choose it: both readings are fine, and the check refuses it all the same. The harm would come later, when someone adds a new field and it lands on the freed position, with old data still carrying the old meaning there. The answer is to deprecate the field and leave its position occupied for good.

    Required fields, without the regret

    Whether a field should be optional or required is an old argument, and both sides are right about something.

    Protocol Buffers had required fields and took them out again, because a required field could never be removed: every old reader refused data without it. Dagr keeps both kinds. Fields are optional unless the schema says otherwise, and a missing required field is an error the program can handle, not a crash. What lets a required field change after all is a default.

    What a default does

    "unit" >> t.utf8 >> required >> string_val("piece")

    Two consequences are worth saying out loud, because both are in the panel.

    Only for required fields

    An optional field can have a default too, but it is used only when a value is built. When data is read, a missing optional field stays missing. Otherwise a reader could not tell "nobody set this" from "somebody set it to the default", and for an optional field that difference is the whole point. Protocol Buffers version 3 merged the two, and later had to add a way to tell them apart again.

    Retiring a field

    A deprecated field keeps its position, and programs can still read it, since old data has to be migrated somehow. New code can no longer set it.

    For an optional field that is all. For a required field there is a catch: old readers insist on finding it. Choose "deprecate a required field" in the panel. The field is given a default at the same moment, and from then on the writer always stores that default, whatever the usual rule about leaving defaults out. Old readers get the value they demand. New code has one field less to think about.

    A default for a whole record

    Formats that have defaults at all usually have them for numbers and switches. In Dagr every kind of field can have one: text, a list, an enum case, a union. That leaves the hard case, a field that refers to another node. A node is not a value one can write into a schema line.

    For that a node type can declare named, ready-made instances of itself. Dagr calls them prefabs.

    Node("Color", fields=[
        "r" >> t.u8 >> required,
        "g" >> t.u8 >> required,
        "b" >> t.u8 >> required,
        "a" >> t.u8 >> required,
    ], frozen=True) >> {
        "black": {"r": int_val(0),   "g": int_val(0),   "b": int_val(0),   "a": int_val(255)},
        "white": {"r": int_val(255), "g": int_val(255), "b": int_val(255), "a": int_val(255)},
    }
    
    Node("Paint", fields=[
        "label"   >> t.utf8 >> required,
        "color"   >> t.ref("Color") >> required >> ref_val("black"),
        "opacity" >> t.f32 >> required >> float_val(1.0),
    ], packed=True)

    A paint's colour defaults to the prefab named black. So a required colour could be added to a schema that had none, and can later be deprecated, like any other field.

    What a reader gets

    framinglengthstagslabelcolouropacity

    Prefabs turned out to be useful for one more thing. An enum case can carry a value, which chapter 8 showed with numbers. The value can also be a prefab, so that a case stored in two bits is handed to the program as a whole record.

    Where change is not on offer

    The receipt

    All of these rules can be learned. The trouble is that they have to be remembered on an ordinary afternoon, by someone who only wants to tidy a schema, and that the worst mistakes produce no error. A rule that matters this much should be checked by a machine.

    To check a change, a tool needs to know what the schema was before. Asking people to keep old schema files around, and to name the right one, does not work for long. So Dagr writes it down itself. Every time code is generated, dagr build also writes a small file next to it, the generation receipt, and you commit that file with your code.

    
    
    

    That is the receipt of version 1 above, with empty entries left out. It has three parts.

    What it is used for

    Two kinds of breaking

    The check asks two separate questions, and the first panel shows both answers.

    Choose "rename a field": the bytes are identical and the check still asks for a major version, because product.name no longer exists. Choose "insert a field in the middle": every name is still there, and the bytes are ruined. Neither question can stand in for the other.

    The change you did not make: the library

    So far the schema was the thing that changed. In most formats a second thing changes underneath you, and you did not ask it to: the library.

    The usual arrangement has two parts. A compiler turns the schema into code, and that code calls a runtime library, which your project pulls in as a dependency like any other. The two have to match. Generated code from one version of the compiler can refuse to build, or to run, against another version of the runtime. And the runtime is shared: if two libraries you depend on each need a different version of it, you have a conflict that is not about your data at all. An update that somebody else made can stop code that was working yesterday.

    Dagr's generated code has no dependencies. What dagr build writes into your project is all of it: the code for your types and the small runtime it calls, as source files in your language, using nothing beyond the language's standard library.

    The price is the other side of the same coin. A correction to the runtime does not reach you through an update of a package. It reaches you when you generate again, and the copy in your repository is yours to keep current. One target is an exception: the library Dagr can generate for Python's data tools (chapter 18) is built on Apache Arrow and depends on it.

    Next to the others

    a field isrequired fieldsyour own defaultswho checks a change
    JSONits nameby conventionin codenobody
    Protocol Buffers 3a number you writenonenonea separate tool
    FlatBuffersits positionsome, cannot be retiredmainly numbersthe compiler, given the old file
    Cap'n Protoa number you writenoneyesnobody
    Avroits nameall, unless a union with nullyes, used when readinga schema registry
    Dagrits positionyes, with a way outevery type, nodes includedevery build, against the receipt

    What none of this catches

    What these panels are.

    Try it

    1. Choose "add a required field without a default". Which direction fails, and what one addition to the schema line would repair it?
    2. Choose "make a required field optional". The new program reads old data without trouble. Who is hurt, and when?
    3. Choose "change a field's type" and compare packed with regular. Why does the packed reader get as far as it does?
    4. Find the two changes where no error appears anywhere and a value is wrong.
    5. Choose "rename a field". Are the two rows of bytes the same? What does the check ask for, and for whose sake?
    6. In the second panel, choose black and 1.0, then tick the box. The reader's answer does not change. Which bytes disappeared, and which of them would the writer never have left out on its own?
    7. You want to replace price, a whole number of cents, with a record of amount and currency. Using only changes from the safe group, describe the steps.

    What comes next

    The format is now complete, and one subject has been left out of every comparison of sizes so far. Most data that travels is compressed on the way. Chapter 17 is about what a compressor does, what it costs, and whether Dagr needs one.