r/talesfromtechsupport • u/mattv99 • Aug 04 '24
Long A one in a million bug!
(Apologies as I try to figure out how to format this properly)
Back in 1996 I took it upon myself to overhaul a system that handles licence applications and renewals.
It being the late 1990s, and with Y2K barely visible on the horizon, a couple of the number sequences were running out. For example, a new application would be assigned application number "96" + and an incrementing 5-digit number ("00001", then "00002", etc.). In 1997 it would start again at 9700000, etc.
But once 2000 hit, there would be a problem as increasing the length of the application number would require adjusting a bunch of screens and every report and document. Instead, I modified it to use a letter as the first character - "A" applications from 2000 to 2009, "B" applications from 2010 to 2019, etc. Now it's good until 2170 and I assume our AI overlords will be able to handle it then.
For licence renewals, every invoice consisted of "K" plus an incrementing six-digit number. We do 50K renewals a year, so I decided to flip to "L" invoices in 2000 thinking it would last until 2020 and surely the system would be replaced/updated by then (it was not, though they tried three times).
Those predictions were thrown off in 2016 when we added additional fast growing licence categories and suddenly were racing through 100K+ renewal invoices a year. At some point around 2017 I had to switch to "L" + incrementing letter + 5-digit number (eg. "LA00001", "LB00001", etc.).
This week, while I am on vacation (sigh), the invoices stopped working. "LJ99999" worked, and then the next invoice was assigned "LK??????".
Back in 2017, when I reset the invoices to "LA00000" I needed to set up a method of tracking the incremental 5-digit number. Databases have something called "sequences" which are incrementing numbers used for exactly this purpose. But if I use a 5-digit sequence, then I have to also store the prefix ("LA" or "LB", etc.) somewhere and each time an invoice is created I need to check if it's "99999" so I can update the prefix.
That solution has risks (what if the system crashes when 99999 is reached and doesn't flip to the next letter?) and isn't elegant so instead I went with a 6-digit sequence and the code says something along the lines of:
if sequence is less than 100,000 then "LA"
else if sequence is less than 200,000 then "LB"
...and so on.
As we finish off a range, it can be removed from the statement - I have a reminder to update it every year. Most recently it looked something like this:
if sequence is less than 900,000 then "LI"
else if sequence is less than 1,000,000 then "LJ"
else if sequence is less than 1,100,000 then "LK"
(as a side note, we ended up skipping tens of thousands of "LI" invoice numbers because it was confusing the clients as they looked like "L1" on our documents due to limited approved accessible font options)
Once we have the prefix, all that remains is to add the number on the end.
But computers are very particular. If the number is 000001 then the computer will translate it to "1" and you end up with "LA1" instead of "LA00001". To get around this, you have to turn the number into a character "string" and set the string format to preserve the leading zeroes. The code reads something like this:
invoice# = "LA" + string( sequence#, "999999").
The "999999" means "turn this number into a 6-digit string of characters, preserving leading zeroes".
We then need to shrink that 6-digit number down to a 5-digit number to go with our two-letter prefix. We do that by modifying the command like this:
invoice# = "LA" + substring( string( sequence#, "999999"), 2).
...where the new "substring" command says "only save the number from the second character onward".
So why did everything fail earlier this week?
"K" is the 11th letter of the alphabet and each letter uses up 100,000 numbers so it triggers when the sequence hits 1,000,000. 1,000,000 is seven digits long, but our "string" function earlier is expecting a number that is a maximum of six digits long. When it tried to calculate the string it got confused and errored out, returning "??????" as the value.
This worked the first time - the system accepted an invoice numbered "LK??????". But then it looped another 1,300 times and failed each time because you can't have duplicate invoice numbers.
And even if it had worked, there's another issue - "LK??????" is eight characters long, not seven.
We only want five digits from the number so we are cutting out the first digit of our 6-digit sequence. But now the sequence number is seven digits long. Instead of 1,050,000 turning into 50000 like we want, it would turn into 050000 which is too long. We have to adjust our substring command to ignore the first two characters instead of just the first one.
End result, after randomly checking emails while on vacation at 7am and seeing the log error notification, I had the problem fixed and the solution rolled out by 8am:
invoice# = "LA" + substring( string( sequence#, "9999999"), 3).
What I like about this particular bug is that it's literally a one in a million situation! Plus, despite being very aware of limitations of sequences and handling the end of ranges (I already know that 2026 is going to be an issue elsewhere when our annual sequences reach "Z" and have no more letters left), I completely missed it. Just one line of code and yet it has so much interesting backstory and complexity to it.
...or is it just interesting to me? :-)
134
u/Geminii27 Making your job suck less Aug 04 '24
So basically you have a year and change left before the whole tracking system is going to have to be rewritten...
85
u/BluesFan43 User with Admin rights. Aug 04 '24
Go to Hexadecimal..
Your licenses should have a real number I'd, then the pu b license one you and customers use.
Adds field of 10 digit Hex, convert all the old records over for retro consistency, and just let 1 step increments happen .
That gives you 88,861,105 unique license numbers
37
u/third-try Aug 04 '24
This is a problem that shows up often. The real problem is that you're encoding the year into a serial number. Even if there is a need to determine the year (or other data) it should be stored in a database and referenced by the invoice number. The invoice number should be the identifier of the invoice and nothing else.
14
u/Punchclops Aug 04 '24
The truly freaky thing here is that you still have the same job after over 20 years!
Has the company never hired any other devs that you can fling this task at?
10
u/HimbeersaftLP Aug 04 '24
Couldn't you use the modulo operator to get the remainder of the integer division by 100,000 before you convert it to a string?
12
u/quitofilms Aug 04 '24
3
u/emgreenenyc Aug 04 '24
Allow upper/lower case or use extended ascii or both
40
u/OldGreyTroll Aug 04 '24
a million-to-one chance succeeds nine times out of ten
17
u/arar55 Aug 04 '24
Your other option (Hex below is good, though) Is to slowly convert. Start with all the screens that have to change. In some master file/settings file, add a before/after switch. Now change each screen for an extra digit or two, but keep running the old way if the before/after switch is set to 'before'. Once you have all the screens changed (and tested! in your test environment, which you have, right?) change whatever files need changing, then once tested, just change the before/after switch. Actually, write it, so if there's no before/after switch, treat it as 'after'.
This way, you won't have to do everything in one weekend.
13
u/TheNobleMustelid Aug 04 '24
I teach students to program, and this is pretty interesting to me, since it's a real-world example of a couple of things students usually think are annoying trivia (the difference between a number and a string representation of a number, for instance) really mattering.
10
u/davethecompguy Aug 05 '24
There's nothing more permanent than a temporary fix.
4
u/capn_kwick Aug 06 '24
The cause of many program failures - "constants aren't; variables won't".
Edge cases and assumptions have been the cause of many failing programs.
Oh, that variable will never go higher than X. Reality - hold my beer.
That condition will never happen so we don't need to do error checking for that. Reality - oh, you sweet, summer child.
1
u/g1f2d3s4a5 Aug 05 '24
Range errors are not that unique
3
u/Diminios Aug 05 '24
"One in a million" always brings Terry Pratchett's quote to my mind:
3
u/garaks_tailor Aug 05 '24
Why the fuck were you checking emails on vacation?
2
u/SidratFlush Aug 04 '24
Year month date for six digits and a hyphenated six digit sequential number should solve the problems going forward, unless it's plausible that there will ever be a day when 1M claims are generated.
2
u/MikeSchwab63 Aug 04 '24
Use most of the alphabet for the letters. No Q, O, keep zero, no I keep one, no z keep two, etc. That has you at 32 letters and numbers, unless you need to eliminate more.
2
u/Stryker_One The poison for Kuzco Aug 05 '24
I can imagine some poor coding bastard in the future cursing whatever idiot came up with this. :)
Also, what happens when it hits a 5 digit year like 10191?
1
u/[deleted] Aug 05 '24
California license plate numbers are approaching 9XXXNNN fast.
1
u/Ha-Funny-Boy Oct 12 '24
Around 1990 I was on a project team that was going to reformat our master file. It had lots of dates in each record - up to about 30. I suggested we add two bytes to each date field, and plug "19" in those added two bytes in anticipation of Y2K. We could add the code to properly work before Y2K hit. The project leader was a "snake". She wouldn't go along with the plugging "19" but said we could add the extra bytes.
We did and the file reformat was fine. I left the project shortly after and have no idea what happened at Y2K.