CSE 331: Upgrading apps
Course motto: Your job is to deliver code that you know works.
When we saved the guestbook's book to a file, we made it a promise:
When the server responds to a post request, that message will be saved forever.
We also said what "forever" covers: every stop of the server, including the stops we choose, such as one "to install a new version of it". Then we built a new version, with a book for each user. That version kept its books in memory only, so it left the promise out.
This lecture's new version saves each user's book in a file, as the old version saved its one book. Then we install the new version in the old one's place, and hold that upgrade to the promise. This time the program we change is already running, with visitors using it and data on the disk.
In this lecture we:
- See that during an upgrade, the old version and the new one run at the same time.
- State the rule that keeps an upgrade safe: a stronger specification breaks no client, and its limit.
- Put the steps of an upgrade in an order the rule allows.
- Treat the data the old version wrote as a client from the past.
- Make a mismatch say what it is, with version numbers.
Two versions at once
Since we put JavaScript in the page,
the guestbook has been two programs that talk: the page's
program, app.js, run in each visitor's browser, and the server,
Guestbook.java. This lecture has two versions of both:
- v1 is the guestbook that saves its book:
one book for every visitor, kept in
book.txt. - v2 is the guestbook with a book for each user,
now saved as v1 saves its book: the user whose login is
LOGINhas their book kept inbooks/LOGIN.txt. Our first try at v2 isnew/Guestbook.java, with Lecture 19's page. We name it new/, for its directory, as we name each program below.
Stopping v1 and starting v2 in its place, in the same directory, while visitors go on using the site, is an upgrade.
Each program below defines a class named Guestbook. So
javac -d compiles each into a separate directory, and
java -cp with that directory's name starts it. In a new
directory beside the lectures' programs:
$ javac -d v1 ../lecture18-persistence/Guestbook.java
$ javac -d new ../lecture22-upgrading/new/Guestbook.java
$ javac -d expand ../lecture22-upgrading/Guestbook.java
$ javac -d contract ../lecture22-upgrading/contract/Guestbook.java
javac prints nothing. Every run in a terminal below, but
reorder/'s, is in this one directory, so whatever v1 leaves here,
the next finds, as when a
new version is installed over the old one. Installing a program
also puts its page, index.html and app.js, here. So before
starting a program, copy in the page it serves, with one of these
lines:
$ cp ../lecture18-persistence/index.html ../lecture18-persistence/app.js .
$ cp ../lecture19-accounts/index.html ../lecture19-accounts/app.js .
$ cp ../lecture22-upgrading/index.html ../lecture22-upgrading/app.js .
(In Windows PowerShell, type curl.exe for curl and type for
cat, and put a comma between the files you give cp or ls.)
The programs, by directory:
v1: Lecture 18's program, serving v1's page.new: new/, serving Lecture 19's page.expand: the finished program,Guestbook.javaat this lecture's root, the directorylecture22-upgradingitself, serving its own page.reorder: reorder/, the finished program with one line changed, serving the same page.contract: contract/, the finished program without v1's requests, serving the same page.
Start v1 with java -cp v1 Guestbook in the server's terminal,
and post two messages from a second terminal:
$ curl -d 'name=ada&message=hello' http://localhost:9331/api/messages
["ada: hello"]
$ curl -d 'name=bob&message=hi ada' http://localhost:9331/api/messages
["ada: hello", "bob: hi ada"]
$ cat book.txt
ada: hello
bob: hi ada
Now upgrade. Press Ctrl+C in the server's terminal, and start new/. The server's terminal now reads:
$ java -cp v1 Guestbook
Serving at http://localhost:9331/
POST /api/messages
POST /api/messages
^C
$ java -cp new Guestbook
Serving at http://localhost:9331/
Suppose ada opened v1's page in a tab before the upgrade. Now she
opens the guestbook in a second tab, and new/'s page for her book
asks for her messages: the first request below. In the first tab,
she types still here? and clicks Post. That tab still runs v1's
page, so it sends v1's post, the second request. Last, she posts
hello from the second tab. (curl -H sends the login in the
header Tailscale-User-Login, as
Tailscale
would.)
$ curl -H 'Tailscale-User-Login: ada@example.com' http://localhost:9331/api/books/ada@example.com/messages
[]
$ curl -H 'Tailscale-User-Login: ada@example.com' -d 'name=ada&message=still here?' http://localhost:9331/api/messages
not found
$ curl -H 'Tailscale-User-Login: ada@example.com' -d 'message=hello' http://localhost:9331/api/books/ada@example.com/messages
["ada@example.com: hello"]
$ ls book.txt books
book.txt
books:
ada@example.com.txt
new/'s page works. The old page's post gets a 404, Not Found.
new/'s manual has no /api/messages, so new/Guestbook.java
reaches its last case, the 404 on lines 94 to 96.
What does ada's first tab do with the 404? It runs the end of
v1's post:
30 31 32 33 34 35 36 | |
Line 34 reads the response's body as JSON. But the body is
not found, which is not JSON. So post stops at line 34 with an
error that nothing catches. The console shows it:
Uncaught (in promise) SyntaxError: Unexpected token 'o', "not found" is not valid JSON
(The promise here is JavaScript's, from Lecture 12, not a
specification's.)
Lines 35 and 36 never run. Ada's list stays as it was, and the
Message box still holds still here?. The page shows her no
error.
So during an upgrade, old code and new code run at the same
time. The browser fetched v1's app.js once, when the tab
loaded. From then on, the server cannot change the page in that
tab. So the new version has to work with what the old one left
behind. The old version
lives on in two places:
- the page in a tab opened before the upgrade, until the tab is reloaded or closed;
- the data the old version wrote, such as
book.txt, still on disk besidebooks/. A later section takes up the data.
Our upgrade also had a gap. Between Ctrl+C and new/'s first line, no server listened. A request sent then would get no answer at all. A site that must never go down instead runs old and new servers side by side for a while, both using one store of data. So old and new overlap on the servers too. How a site does that waits for the lecture on deploying and operating apps, on November 30.
Which changes to a server are safe for the pages already out there? The next section answers with specifications.
A stronger specification breaks no client
The old page, still open in its tab, is a client of the server. Lecture 12 said so: the page relies on the server's manual as a caller relies on a method's specification. Here is v1's manual:
| Guestbook.java (v1) | |
|---|---|
17 18 19 20 21 22 | |
The old page sends only the two lit requests, from lines 20 and 30
of v1's app.js. So the last section's question is about two
specifications: which new manuals keep every promise the old page
relies on?
Lecture 2 gave us the comparison. S1 is stronger than S2 "when S1's precondition is weaker than or the same as S2's, and, on every call where S2's precondition is true, S1's postcondition is stronger than or the same as S2's." Lecture 14 used it for substitution: a client argued correct from a type A's specification works with any B whose specification is stronger. An upgrade is the same substitution, made over time. The client was written for the old version, and the new version takes the old one's place.
Rule: a safe upgrade
An upgrade to a stronger specification is guaranteed not to break any clients.
In full: if the new version's specification is stronger than the old version's, then every client that relies only on the old specification still works with the new version. A client is any program that sends the old version requests, such as the old page in an open tab. A version's specification is its manual. What the manual asks of a request, such as a login, is the request's precondition. Stronger is Lecture 2's, request by request, as Lecture 14 compared types method by method: the new version asks no more of a request than the old one asked, and on every request the old precondition allows, it promises at least what the old one promised.
Look at relies only on. A client that relies only on the old manual was argued correct from that manual's promises. A stronger new version keeps every one of them. A client that relies on more is not covered, and we will meet one below.
Let's judge three candidate servers by the rule.
new/ drops v1's promises. Its manual, lines 27 to 41 of
new/Guestbook.java, does not list /api/messages. So new/ no
longer promises the messages for the old page's requests. It
also asks every request for a login (line 28), which v1 never
asked. So new/ is not stronger than v1, and the rule does not
apply. The old page breaks, as
its post to new/ showed.
The finished program keeps them. The finished program,
Guestbook.java at the lecture's root, keeps v1's two requests in
a paragraph of its manual:
| Guestbook.java (the finished program) | |
|---|---|
43 44 45 46 47 48 49 | |
Lines 47 to 49 are v1's lines 20 to 22, word for word. handle
answers them first. Where a listing uses Java you have not met,
read past it to the lit line:
| Guestbook.java (the finished program) | |
|---|---|
83 84 85 86 87 88 89 90 91 92 | |
shared is the book v1 kept in book.txt. Why must these lines
come ahead of the login check at line 94? v1's manual asks
nothing about a login. With the check
first, a request with no login would get 401 where v1 sent the
messages. That asks more of a request than v1 asked, so the new
specification would not be stronger.
We compiled the finished program into the directory expand (a
name the next section explains), and
start it with java -cp expand Guestbook where new/ ran, beside
v1's book.txt:
$ curl http://localhost:9331/api/messages
["ada: hello", "bob: hi ada"]
$ curl -H 'Tailscale-User-Login: ada@example.com' -d 'name=ada&message=still here?' http://localhost:9331/api/messages
["ada: hello", "bob: hi ada", "ada: still here?"]
$ curl -H 'Tailscale-User-Login: ada@example.com' http://localhost:9331/api/v2/books/ada@example.com/messages
["ada@example.com: hello"]
The first request sends no login, on purpose. The second is the
old page's post, which new/ answered with not found. The third
is the request the finished program's own page sends for ada's
book, which still holds the hello she posted under new/. Its
path begins /api/v2/, for a reason
Version numbers gives.
Is the finished program's whole manual stronger than v1's, though? No! v1's manual promises the page for GET /, and the finished program redirects the visitor to their own book instead, with a 303. Every request but /api/messages now needs a login, too. So at GET / the new manual is not stronger, and the box, read strictly, does not apply. Now compare the two manuals on what the old page sends, GET and POST /api/messages. There the finished program's promises are v1's, word for word, so they are stronger or the same. The next section refines the rule so that this comparison is the one that counts.
reorder/ keeps them too, by its manual. reorder/Guestbook.java
is the finished program with one line changed:
| reorder/Guestbook.java | |
|---|---|
89 90 | |
It sends the old book newest first. Its manual is unchanged, and
newest first meets "the messages posted so far, as JSON" as well
as oldest first does. Lecture 2's definition says "or the same as"
twice, so a specification is stronger than itself. So on the old
page's two requests, reorder/'s promises are v1's, as the finished
program's are. In a
fresh directory beside the first, we compile reorder/ into
reorder, as above, and start it with java -cp reorder Guestbook:
$ curl -d 'name=ada&message=hello' http://localhost:9331/api/messages
["ada: hello"]
$ curl -d 'name=bob&message=hi ada' http://localhost:9331/api/messages
["bob: hi ada", "ada: hello"]
Now the old page. In another fresh directory, we start v1, open
its page, and post hello signed ada, then hi ada signed bob.
The list reads "ada: hello", then "bob: hi ada". Then we stop v1
and start reorder/ in its place. On the old page, still open, ada
posts still here?. The response is 200. The console shows
nothing. The list now reads, top to bottom, "ada: still here?",
"bob: hi ada", "ada: hello".
show, at lines 8 to 16 of v1's app.js, lists the messages in
the order they arrive. So bob's "hi ada" now sits above the
message it answers, and the book reads from the bottom up. The
rule is not wrong here. The old page relied on the order, and no
line of v1's manual promised it.
Reading 4's chapter on software engineering calls this Hyrum's Law, the name most people at Google settled on, though Hyrum called it the Law of Implicit Dependencies:1
With a sufficient number of users of an API, it does not matter what you promise in the contract: all observable behaviors of your system will be depended on by somebody.
An API (application programming interface) is the requests or calls a program offers its clients, and its contract is its specification. So the rule protects only the clients that rely only on the specification. With enough clients, Hyrum's Law says, some client relies on more. Two lessons follow:
- Upgrade to a stronger specification. Every client that relies only on the old one still works.
- Write specifications that promise what clients rely on. v1's
manual should have said "oldest first", as the specification of
Book's
messagesdoes (line 148 of Lecture 18's Guestbook.java). With those words in the manual, reorder/ is not stronger than v1, and comparing the manuals finds the change before a visitor does.
The finished program now keeps two sets of promises: v1's, to the old page, and its own, to the page it serves. How long must it keep the old ones?
The order of an upgrade
The last section
found the finished program's whole manual not stronger than v1's,
because of GET /, and safe for the old page all the same. Why
safe? The old page never sends GET / again. It sends only
GET and POST /api/messages. So the old page relies on what
v1's manual promises about those two, on lines 20 to 22, and on
nothing else there.
Definition: requirement
A client's requirement is the part of a specification that the client relies on.
A requirement is a specification too. So Lecture 2's stronger compares it with a version's specification, and the rule can judge one client at a time:
An upgrade is safe for a client that relies only on the old specification if the new version's specification is stronger than that client's requirement.
Each client's requirement is part of the old specification. So the old specification is stronger than every requirement, and the old version worked for all its clients. A new specification stronger than the old one is then stronger than every requirement too: the earlier rule is a special case of this one.
The refined rule also allows a new specification that is not stronger than the old one, as long as no remaining client's requirement loses a promise. Removing a request needs that allowance. The removal takes three steps, in this order:
- Expand. Add the new promises, and keep the old ones that some client's requirement includes. The finished program is this step. It answers its own page's requests (its manual, lines 32 to 40), and v1's two as v1 answered them (lines 43 to 49). It is stronger than the old page's requirement and its own page's, so it is safe for those two pages, as long as each relies only on its requirement. It is not safe for a tab still open on new/'s page, which Exercise 3 takes up.
- Move the clients. Change nothing on the server, and wait.
Every page loaded from now on is the finished program's, and
sends nothing to
/api/messages. An old page relies on that path until its visitor reloads the tab, or closes it. The server cannot make its visitor reload, since v1's page has no code for it. - Contract. Remove the old promises.
contract/Guestbook.javais the finished program without v1's two requests: it answers/api/messageswith the status 410. So its specification is not stronger than the finished program's. By the rule, the step is safe once no remaining client's requirement includes/api/messages.
The three steps are known as expand and contract.2 Why 410, and not a bare 404, is a question for Version numbers.
The three steps take more work than one change. Until the
contract step, the server carries both books' code
(SharedBook, lines 453 to 506), and
every change has to keep both sets of promises. Within one
program we change the callers and the method at once. The steps
are for clients we cannot change, such as a page in a visitor's
browser.
When is it time to contract? Let's try the
three ways to know
on the claim no remaining client relies on /api/messages:
- A tool. Within one program,
javacfinds callers. When Lecture 19 changed the type of the fieldbook,javacpointed at the two lines of code that used the field. Across versions, no compiler sees the old page and the new server together. - Reasoning. We can search the clients we own, as Lecture 19
searched for every place the program said one. v1's
app.jssends/api/messages, on lines 20 and 30. The finished program's page,app.jsat the lecture's root, sends/api/v2/me, on line 72, and builds every other path fromurl, on line 18, which begins/api/v2/books/. So no page the finished program serves sends/api/messages. The argument says nothing about a tab opened before the upgrade. Nor does it cover a client we did not write, such as someone's script that posts withcurl. -
Testing. The server prints each request, on line 81. Here is its terminal from the finished program's run:
$ java -cp expand Guestbook Serving at http://localhost:9331/ GET /api/messages POST /api/messages GET /api/v2/books/ada@example.com/messagesHere
curlsent the two requests for/api/messages. On a real site, an open tab would. Each of those log lines refutes the claim that nobody uses/api/messages. A log with no such line confirms nothing, though: a tab left open over a weekend can still post on Monday.
So a site picks a date instead. It announces that the old requests stop working then, and contracts on that date. To announce that a request will be removed is to deprecate it. The date is a decision about which clients we are willing to break. It adds no knowledge: a tab never reloaded still breaks after it.
v1 left more behind than open tabs. It also left a file,
book.txt, and the new version has to read it.
Stored data is a client from the past
When we started new/
in v1's directory, ls showed
book.txt still there. Nothing had written to it since v1, so it
still held ada's and bob's messages. But
new/ never reads it: its main makes the books from books/
alone, on line 49 of new/Guestbook.java. No line of that file
names book.txt. So no request to new/ brings back "ada: hello".
That breaks
Lecture 18's promise,
that a message is saved through every stop, though book.txt is
still on disk.
Like the page in the open tab, book.txt was made by the old
version, and the new version has to take it as it is. So the
file is a client from the past. It sends no requests, so
the rule compares
two other specifications: v1's for reading the disk, and the new
version's.
v1's reader is its Book constructor, which "makes a book of the
messages saved in file, one per line" (lines 128 and 129 of
Lecture 18's
Guestbook.java). The file it reads is whatever v1's save left,
and save "writes the messages to file, one per line" (line 156).
So the comparison asks the new version's specification for reading
the disk to be stronger than v1's. With
Lecture 2's stronger,
that is two demands:
- A precondition no stronger. The new reader must accept every
file that v1's
savecould have left. - A postcondition no weaker. From each such file, the new version must give back the messages, as v1 did.
new/ has no reader for book.txt at all. So it gives back none of
the file's messages, and fails the second demand. Its
specification for reading the disk is not
stronger than v1's. The finished program meets both, with one
more line in main, line 61:
59 60 61 | |
SharedBook (lines 453 to 506) is v1's Book under a new name,
less the Crash class that Lecture 18 used to stop it in the
middle of a save, so it reads book.txt as v1 did.
But only an old page asks for those messages. The finished
program's page never does. And contract/ never reads
book.txt. The file's requirement is the part of v1's
specification that reads it back. So, by
the order of an upgrade, the contract
step has to wait until v1's messages are in the new version's
form.
Definition: migration
Migration is converting stored data from the old version's form to the new version's.
Here the conversion is not mechanical. In v2, a message is signed
with a login: Book.add "adds the message, signed with asker's
login" (line 391 of the finished program), and the login comes
from Tailscale's header, which the visitor cannot choose. A line
of book.txt is signed with a name typed into a field.
As Lecture 19 said,
"Nobody may choose who they are." So "ada: hello" says only that
someone typed ada. No program can turn that name into a login,
since v1 never learned one. Whose book gets v1's messages is a
decision, and each choice changes a specification or breaks a
promise:
- One shared book that nobody posts to. v2's specification
gains a kind of book no user owns, whose signatures are not
logins. The finished program is partway there: it keeps that
book in
book.txt, but shows it only to old pages, which can still post to it. - The book of the user whose login matches the name. "ada: hello" goes into ada@example.com's book, signed with that login. That breaks v2's promise that a signature is the login of the user who posted, since anyone could have typed ada.
- No book. Leave
book.txtunread, as new/ does. That breaks Lecture 18's promise.
The people who run the guestbook make that choice, and v2's specification has to say which they made.
Next, suppose we start contract/ in this directory, as the next section does, and it goes wrong. We stop it and start v1 again. That is a rollback, a substitution too: the old version for the new. Now the rule asks v1's specification to be stronger than contract/'s. It is not, since v1 has no books. The second terminal:
$ curl http://localhost:9331/api/messages
["ada: hello", "bob: hi ada", "ada: still here?"]
$ ls book.txt books
book.txt
books:
ada@example.com.txt
The server's terminal:
$ java -cp v1 Guestbook
Serving at http://localhost:9331/
GET /api/messages
The shared book came back whole, with "ada: still here?", the post
the finished program took through the old request. Ada's
"hello" in her own book did not: it is in books/, which v1 never
reads. The shared book came back only because the finished
program kept book.txt in v1's form. Had a migration deleted
book.txt, v1 would have started with an empty book. So keep the
old form readable until nobody wants a rollback.
Neither break was reported: the old page's post failed with no
word on the page, and new/ printed nothing about book.txt. When
a client does break, can it at least say why?
Version numbers
The contract step takes v1's requests out of the server. What does a page still open from v1 get back when it posts? Start contract/'s server in the directory where every version has run. From the second terminal, send three requests: the old page's post, the request that a tab still open from new/ sends for ada's book, and that request as the finished program's page sends it:
$ curl -H 'Tailscale-User-Login: ada@example.com' -d 'name=ada&message=still here?' http://localhost:9331/api/messages
this page is out of date; reload it
$ curl -H 'Tailscale-User-Login: ada@example.com' http://localhost:9331/api/books/ada@example.com/messages
this page is out of date; reload it
$ curl -H 'Tailscale-User-Login: ada@example.com' http://localhost:9331/api/v2/books/ada@example.com/messages
["ada@example.com: hello"]
The server's terminal:
$ java -cp contract Guestbook
Serving at http://localhost:9331/
POST /api/messages
GET /api/books/ada@example.com/messages
GET /api/v2/books/ada@example.com/messages
The finished program's page, which contract/ still serves, puts a
version, v2, right after /api/ in every request for data. So
any other path under /api/ comes from a page loaded from an
older version. Lines 78 to 84 of contract/Guestbook.java answer
such a path before the login check:
78 79 80 81 82 83 84 | |
The first two answers carry the status 410, Gone, which curl
does not print. HTTP's standard says that 410 means "access to
the target resource is no longer available at the origin server
and that this condition is likely to be permanent."3 That
fits: we took /api/messages out on purpose, for good.
A page the finished program served sends only /api/v2/ paths,
so contract/ breaks no such page. With such a page open at
/ada@example.com, in a directory where her book was still
empty, we stopped the finished program and started contract/.
Ada posted hello, and the response was 200. Her list
showed "ada@example.com: hello" with its Delete button. The
space under the title, where the page shows an error, stayed
empty, and the console showed nothing.
Who reads the server's words, though? On v1's page, nobody. Under
v1 again, ada loads v1's page and types still here?. Then we
stop v1 and start contract/ in its place, and she clicks Post.
The response is 410. Her list stays as it was, and the Message
box still holds still here?. The console shows:
Uncaught (in promise) SyntaxError: Unexpected token 'h', "this page i"... is not valid JSON
v1's post called response.json() on the server's words, at
line 34, as it did on new/'s 404 in
Two versions at once. (Chrome quotes
only the body's first 11 characters.) Lecture 19's page, which
new/ served, calls response.json() on its load and its post too
(its lines 61 and 72). Only the finished program's page shows
an error. It hands each answer about messages to receive:
60 61 62 63 64 65 66 67 | |
response.ok
is true for a status from 200 to 299. On any other status, line
65 puts the server's words at the top of the page. So when a
later version takes out v2's requests and answers them with
410, a visitor with the finished program's page open will read
why.
v1's page sends no version, and has no code to show an error, so no later server can make it show one. Lecture 19's page, which new/ served, lacks both too. Of the three pages, only the finished program's sends a version and shows the server's error. So the code that shows the server's error, like the version, has to be in a page from its first release.
The last section
said that this contract step has to wait for a migration, and
none has run in the directory where every version ran. So contract/
shows nobody the three
messages in book.txt. That is the choice No book, and it
breaks Lecture 18's promise.
Semantic versioning, a convention for release numbers, gives each release three numbers, MAJOR.MINOR.PATCH, as in 1.4.2, and says which to increment:4
- MAJOR version when you make incompatible API changes
- MINOR version when you add functionality in a backward compatible manner
- PATCH version when you make backward compatible bug fixes
A backward compatible change, in semver's phrase, breaks no client that relies only on the old version's API. In these notes' word, its specification is stronger. Read with the rule:
- MAJOR: the new specification is not stronger. It is weaker, or incomparable, so some client may break, as the old page broke on new/ and on contract/.
- MINOR: the new specification is stronger.
- PATCH: the specification is the same, and only the implementation is fixed. So the rule applies. Even so, by Hyrum's Law, some client may have relied on the bug. The rule never protected that client.
The number records a person's comparison of two specifications. No tool checks it.
A file can carry a version too. v1 reads every line of book.txt
as a message (Lecture 18's Guestbook.java, line 134). Suppose a
later version rewrote book.txt with a first line, version 2.
After a rollback, v1 would show version 2 as a message, and
take every line after it for a message too, whatever its form.
A v1 that checked the first line could have refused the file
instead. So a file's version, like a request's, has to be there
from the first release.
Three ways to know, with upgrades
An upgrade asks each of the three ways to know a new question: does the new version work with the clients and the data the old version left behind?
- Reasoning compared two specifications. Without any run, the rule, applied to the old page's requirement, says that new/ could break the old page, and that the finished program could not break a page that relies only on that requirement. Reasoning covers only what the specifications say, though. v1's manual promised no order, so the comparison passed reorder/, and the old page's list flipped.
- Tools helped inside one version. Delete a method, and javac refuses every caller. No tool did that across versions.
- Testing took old clients and old data as its inputs: the
old page's post, sent with curl, and the book.txt that v1
wrote. One run, with its 404, refuted the claim that new/
works with the old page. Likewise, one line in the server's
terminal refutes the claim that nobody sends
/api/messages. A terminal with no such line does not confirm it.
So the lecture comes to two practices:
- Upgrade to a stronger specification. An upgrade to a stronger specification breaks no client that relies only on the old specification.
- Put a version on every request and every file, and put code that shows the server's error on every page, from the first release. Then when a client does break, the server can answer with an error that says what is wrong, such as "this page is out of date; reload it", instead of a bare 404. And the page shows that error to its visitor.
Exercises
Each answer is folded under its question. Write yours first.
1. Bob's tab runs v1's page. He clicks Post during the
upgrade from v1 to new/, after ^C and before new/ starts. v1's
post sends it:
30 31 32 33 34 35 | |
Is his message saved? Does that break Lecture 18's promise? What if he clicks Post again once new/ is up?
Answer
- Not saved. No server is running to receive it.
- Lecture 18's promise is not broken. It says: "When the server
responds to a post request, that message will be saved
forever." No server responded. So
fetchthrows on line 30, andpoststops there. - Once new/ is up, a 404. new/ does not answer
/api/messages, so it sendsnot found, and line 34 throws on that text. No click on v1's page saves a message under new/. Bob has to reload.
2. v1's manual promises two requests:
| Guestbook.java (v1) | |
|---|---|
20 21 22 | |
Each change below is made alone to the finished program. Compared on what these lines promise, is the new specification stronger than v1's? Which client, if any, breaks?
- (a) A new request, GET /api/messages/count, answers the number of messages.
- (b) A POST with an empty message gets 400,
empty message. - (c) Each message ends with its time, as in
"ada: hello (10:02)".
Answer
- (a) is stronger. v1 promised nothing for that path, and every old promise is kept. No client that relies only on the manual breaks.
- (b) is not stronger. v1 accepted an empty message, so the
new precondition is stronger. The old page breaks when Post is
clicked with an empty Message box: its
postcallsresponse.json()on the text, which throws. - (c) is the same specification, "the messages posted so far,
as JSON", which, as
Lecture 12 noted,
leaves each string's form open. Yet a client that expects
exactly
"ada: hello"breaks: Hyrum's Law.
3. Suppose a tab still holds the page new/ served, Lecture
19's, which builds its paths from url:
15 | |
The finished program's manual lists book requests only under
/api/v2/books/. Under the finished program, the tab asks for
ada's book, and the server's terminal shows the request:
$ java -cp expand Guestbook
Serving at http://localhost:9331/
GET /api/books/ada@example.com/messages
What does the tab get? Was the expand step safe for it, by the rule? What would have made it safe?
Answer
- It gets 404,
not found. The finished program answers book requests only at paths that begin/api/v2/books/. Nor is this path the address of a book, such as/ada@example.com. So it gets the server's last answer, the 404. - Not safe. The tab's requirement includes new/'s
GET /api/books/OWNER/messages. The finished program does not promise it, so it is not stronger than that requirement. - Keeping new/'s requests too. Every version that served a page leaves clients, however briefly it ran. The expand step would keep new/'s paths beside v1's, and the contract step would remove both.
4. To shorten the gap in which no server listens, an operator upgrades in this order, while visitors post:
- With v1 still serving, run a migration program that reads
book.txtand writes its messages in v2's form. - Stop v1, and start a v2 that reads only v2's form.
(An upgrade with no gap has the same shape: old servers keep serving until the last is replaced.) Cy posts between steps 1 and 2, and v1 responds. Which promise breaks, for which message? What order of steps prevents it?
Answer
- Lecture 18's promise breaks, for cy's message. v1 responded
to cy's post, so the promise says cy's message is saved forever.
v1 saved it in
book.txtafter the migration had read the file, so the copy lacks it. v2 never readsbook.txt. Every post v1 saves after the migration's read is lost the same way. - Why. The migration is one more reader of v1's file, and it
read while v1's
savecould still write. - An order that prevents it. Let nothing write
book.txtafter the migration reads it: stop v1, run the migration, then start v2. The gap is longer, but no message that got a response is lost.
5. Version 1.4.2 of a server's manual promises two requests:
GET /api/messages, "the messages posted so far, oldest first,
as JSON", and POST /api/messages, which adds a message and
answers the same. Each change below is made alone to 1.4.2. By
the rule that a stronger specification breaks no client that
relies only on the old one, which number should it increment?
- A new request,
GET /api/messages/count, answers the number of messages. GET /api/messagesanswers only the 50 newest messages.- A message with a line break in it came back, after a restart, as two messages. Now it comes back as one.
Answer
- MINOR, to 1.5.0: stronger. The old requests are answered as before. A client of 1.4.2 never sent the new one.
- MAJOR, to 2.0.0: not stronger. With more than 50 messages, the answer leaves out messages that 1.4.2 promised.
- PATCH, to 1.4.3: the same specification. The implementation now satisfies it. A client that relied on the bug can still break (Hyrum's Law).
Further reading
- Hyrum's Law. Titus Winters, What Is Software Engineering?, chapter 1 of Software Engineering at Google (2020), one of Reading 4's articles: its section "Hyrum's Law".
- The three steps. Danilo Sato, ParallelChange (2014): expand, migrate, and contract, worked step by step on a small class.
- Version numbers. Tom Preston-Werner, Semantic Versioning 2.0.0 (2013): the rules, and answers to common questions about them.
- Status codes. Fielding, Nottingham, and Reschke, editors, RFC 9110, HTTP Semantics (2022), whose section 15 defines status codes, 404 and 410 among them.
-
Titus Winters, "What Is Software Engineering?", chapter 1 of Software Engineering at Google, 2020, section "Hyrum's Law". ↩
-
Danilo Sato, "ParallelChange", martinfowler.com bliki (2014): "Parallel change, also known as expand and contract, is a pattern to implement backward-incompatible changes to an interface in a safe manner, by breaking the change into three distinct phases: expand, migrate, and contract." These notes say move the clients for the middle step, and keep the word migration for converting stored data, in the next section. ↩
-
R. Fielding, M. Nottingham, and J. Reschke, editors, HTTP Semantics, RFC 9110 (2022), section 15.5.11, "410 Gone". ↩
-
Tom Preston-Werner, Semantic Versioning 2.0.0 (2013), the Summary. ↩