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.…
Leonardo DiCaprio is joined by braless co-star Teyana Taylor at One Battle After Another screening in NY
Close friend's sinister fears revealed over the REAL reason why Aussie pilot died in Brazilian plane crash surrounded by 200kg of SpaceX-branded cocaine
Essex care home owner hits back after receiving 'inadequate rating' in latest inspection
Margot Robbie suffers devastating career blow as latest film bombs at box office
Aussie woman told to 'button up' by Qantas staff to 'protect other cultures'
Mid Essex teen sprayed unknown substance leaving officers coughing for months
Could Wildfire Smoke Become America's Leading Climate Health Threat By 2050?
Read more of this story at Slashdot.
Huawei used its own silicon to re-educate DeepSeek so its output won’t bother Beijing
Asia In Brief Huawei last week revealed that China’s Zhejiang University used its Ascend 1000 accelerators to create a version of DeepSeek’s R1 model that improves on the original by producing fewer responses that China’s government would rather avoid.…