Prince Harry reflects on 'amazing' Disney World trip with Meghan and their kids... 32 years after same trip with mom Princess Diana
Why won't I get my state pension on my 66th birthday? STEVE WEBB replies
Why Sydney Sweeney and Scooter Braun's hot new romance could get them CANCELLED - if they don't turn on each other first
Trump to warn parents everyday drug may trigger AUTISM... as officials hail breakthrough reversal treatment
Charlie Kirk's widow reveals Usha Vance's role in coping with death of her husband: 'Exactly what I needed to hear'
Nigel Farage vows to save taxpayers £230BILLION by sending back hundreds of thousands of migrants if Reform wins next election
NIGEL FARAGE: No one voted to let migrants in, so we will put our people first
Hamas claims 'victory' after Starmer hands out enormous 'prize for terrorism': Sir Keir is branded 'modern-day appeaser' after recognising Palestinian state despite Israeli and US fury
CodeSOD: Identify a Nap
Guy picked up a bug ticket. There was a Hiesenbug; sometimes, saving a new entry in the application resulted in a duplicate primary key error, which should never happen.
The error was in the message-bus implementation someone else at the company had inner-platformed together, and it didn't take long to understand why it failed.
/** * This generator is used to generate message ids. * This implementation merely returns the current timestamp as long. * * We are, thus, limited to insert 1000 new messages per second. * That throughput seems reasonable in regard with the overall * processing of a ticket. * * Might have to re-consider that if needed. * */ public class IdGenerator implements IdentifierGenerator { long previousId; @Override public synchronized Long generate (SessionImplementor session, Object parent) throws HibernateException { long newId = new Date().getTime(); if (newId == previousId) { try { Thread.sleep(1); } catch (InterruptedException ignore) {} newId = new Date().getTime(); } return newId; } }This generates IDs based off of the current timestamp. If too many requests come in and we start seeing repeating IDs, we sleep for a second and then try again.
This… this is just an autoincrementing counter with extra steps. Which most, but I suppose not all databases supply natively. It does save you the trouble of storing the current counter value outside of a running program, I guess, but at the cost of having your application take a break when it's under heavier than average load.
One thing you might note is absent here: generate doesn't update previousId. Which does, at least, mean we won't ever sleep for a second. But it also means we're not doing anything to avoid collisions here. But that, as it turns out, isn't really that much of a problem. Why?
Because this application doesn't just run on a single server. It's distributed across a handful of nodes, both for load balancing and resiliency. Which means even if the code properly updated previousId, this still wouldn't prevent collisions across multiple nodes, unless they suddenly start syncing previousId amongst each other.
I guess the fix might be to combine a timestamp with something unique to each machine, like… I don't know… hmmm… maybe the MAC address on one of their network interfaces? Oh! Or maybe you could use a sufficiently large random number, like really large. 128-bits or something. Or, if you're getting really fancy, combine the timestamp with some randomness. I dunno, something like that really sounds like it could get you to some kind of universally unique value.
Then again, since the throughput is well under 1,000 messages per second, you could probably also just let your database handle it, and maybe not generate the IDs in code.
.comment { border: none; }Elderly British couple who were held by the Taliban for nearly eight months feared they would be executed
Stacey Dooley tearfully reveals she suffered a devastating ectopic pregnancy with partner Kevin Clifton that left her needing emergency surgery
Tom Holland 'is rushed to hospital after a Spider-Man stunt goes horribly wrong - forcing the £150m blockbuster to halt filming'
I want to buy a house with my daughter and grandson - will they be stung by inheritance tax when I die?
James Van Der Beek drops out of Dawson's Creek reunion due to illness amid cancer fight
Trump reveals Charlie Kirk's last message to him before his assassination
Married At First Sight UK groom was left with fractured cheekbone and shattered teeth in brutal attack by thugs
Tech troubles create aviation chaos on both sides of the Atlantic
Technology problems hit the commercial aviation industry hard over the weekend, leading to hundreds of cancelled flights and myriad delays on both sides of the Atlantic.…