Trump says Michael Dell is part of the team buying TikTok, with Larry Ellison and maybe some Murdochs
Dell CEO Michael Dell is part of the consortium that intends to acquire TikTok’s US operations, according to US president Donald Trump.…
Christina Aguilera, 44, recreates her iconic Dirrty look as she takes to the stage in chaps and a leather bodysuit 23 years on from song's release
World conker competition could be axed after series of heatwaves threaten ancient playground tradition
Prince Harry will NEVER be allowed to return as a 'half-in, half-out' working royal - and claims he could help ease William's workload are 'wide of the mark'
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; }