rapidfsub

rapidfsub

Just one use of “manage_relationship” makes action not able to be atomic.
I think the default value of require_atomic? should be “false”.
Why is it “true”?

Showing Posts 14 to 5

rapidfsub

rapidfsub OP

I am also a race condition worrying guy.
But I think many folks would write a spark dsl for “automatically setting require_atomic? false”,
because require_atomic? true is rare case for most apps. :rofl:

zachdaniel

zachdaniel

Creator of Ash

So in a way, its supposed to be slightly annoying, and if it drives folks to understand what it means for an action to be atomic and why it matters then its doing its job :slight_smile:

Its worth highlighting a snippet from those docs though:

Not all actions can reasonably be made atomic, and not all non-atomic actions are problematic for concurrency. The goal is only to make sure that you are aware and have considered the implications.

zachdaniel

zachdaniel

Creator of Ash

Writing atomic code is harder, but here is an example of a case where it will pay off significantly:

update :add_ten_to_score do
  change fn changeset, _ -> 
    Ash.Changeset.change_attribute(changeset, :score, changeset.data.score + 10)
  end
end

The action above raises an error about it not being atomic. In this case, the effect here is that if you ran this same update concurrently on the same record, it’s possible to have inconsistent results.

i.e with a record that has a score of 0
Action 1: record is read, current score: 0
Action 2: record is read, current score: 0
Action 1: new score is determined, new score 10
Action 2: new score is determined, new score 10
Action 1: data is written, new score 10
Action 2: data is written, new score 10

So now even though the action was called twice, and you’d expect a new score of 20, you actually have a new score of 10.

By using an action like this:

update :add_ten_to_score do
  change atomic_update(:score, expr(score + 10))
  # or use the built in
  change increment(:score, amount: 10)
end

you now have a fully atomic update that does not have the same problems as described above

I’ve made some tweaks to the atomic update docs to potentially help with clarity. Update Actions — ash v3.29.3

rapidfsub

rapidfsub OP

Thanks for your explanation and advice. I really appreciate it.

zachdaniel

zachdaniel

Creator of Ash

require_atomic? is a safety guard. It’s like marking code as safe or unsafe in another language. It has little to no value if it defaults to false. It is expected that you will have to turn it off relatively often. That is still important though, as when you see that error you should consider if there is anything to be done for it.

This configuration:

config :ash, :require_atomic_by_default?, false, was made only to assist with the upgrade and will be removed in the future. I would not suggest using it at all.

rapidfsub

rapidfsub OP

“Keep It Simple” is not “Always be simple”.
In complex problems, you can’t stay in simple.

rapidfsub

rapidfsub OP

I think you are not an ash framework user.
You don’t get what “not that simple” means.

Just one use of manage_relationship makes action not able to be atomic.
And manage_relationship is a basic feature in ash,
which is for creating two related resources in one action.

D4no0

D4no0

When this argument is brought up, in a lot of cases it points to poor design. If you cobble code together OFC you will end up with actions that are not atomic because you are always tempted to take the easy way out and do side effects as much as possible.

Happily enough, elixir and the VM limits drastically how many shortcuts of this type you can have by making the values immutable and having a clear concurrency concept.

rapidfsub

rapidfsub OP

Hmm.. Then I still cannot understand why require_atomic? defaults to true..
I think this would be the last question.

config :ash, :require_atomic_by_default?, false
Is this option “not recommended” or just “up to my choice”? @zachdaniel

I think most apps are

  1. not that simple
  2. has many actions that can’t be atomic

so most apps would use config above.

zachdaniel

zachdaniel

Creator of Ash

It really just depends on the action. Being atomic is a property of the action. Some actions can be, some can’t be, it’s entirely based on what the action does.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews