Skip to main content

CodeSOD: Identify a Nap

4 weeks 1 day ago

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; } [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
Remy Porter

Tech troubles create aviation chaos on both sides of the Atlantic

4 weeks 1 day ago
‘Cyber-attack’ on ticketing outfit Collins and cable cuts at Dallas ground hundreds of flights

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.…

Iain Thomson and Simon Sharwood

Could Wildfire Smoke Become America's Leading Climate Health Threat By 2050?

4 weeks 1 day ago
"New research suggests ash and soot from burning wildlands has caused more than 41,000 excess deaths annually from 2011 to 2020," reports the Los Angeles Times: By 2050, as global warming makes large swaths of North America hotter and drier, the annual death toll from smoke could reach between 68,000 and 71,000, without stronger preventive and public health measures... In the span studied, millions of people were exposed to unhealthful levels of air pollution. When inhaled, this microscopic pollution not only aggravates people's lungs, it also enters the bloodstream, provoking inflammation that can induce heart attacks and stroke. For years, researchers have struggled to quantify the danger the smoke poses. In the paper published in Nature, they report it's far greater than public health officials may have recognized. Yet most climate assessments "don't often include wildfire smoke as a part of the climate-related damages. And it turns out, by our calculation, this is one of the most important climate impacts in the U.S." The study also estimates a higher number of deaths than previous work in part because it projected mortality up to three years after a person has been exposed to wildfire smoke. It also illustrates the dangers of smoke drifting from fire-prone regions into wetter parts of the country, a recent phenomenon that has garnered more attention with large Canadian wildfires contributing to hazy skies in the Midwest and East Coast in the last several years. "Everybody is impacted across the U.S.," said Minghoa Qiu [lead author and assistant professor at Stony Brook University]. "Certainly the Western U.S. is more impacted. But the Eastern U.S. is by no means isolated from this problem."

Read more of this story at Slashdot.

EditorDavid

Huawei used its own silicon to re-educate DeepSeek so its output won’t bother Beijing

1 month ago
PLUS: India ponders tax breaks for datacenters; Samsung plans hiring spree; Taliban bans fiber internet; and more

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.…

Simon Sharwood