-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ets
: add ability to suppress heir/gift data
#7970
Conversation
CT Test Results 4 files 227 suites 1h 55m 0s ⏱️ Results for commit 74cf856. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
00e0d70
to
b64932b
Compare
Rebased because of merge conflicts. |
The failing test seems to be unrelated to the changes made in this PR. |
While I don't mind the change in general, I think the justification is questionable. I think it promotes an anti-pattern I observed several times in the past:
It stands out to me, that if you want the supervisor to own the ETS table, you should do that from the very beginning. That is, create the table in the One should think of an ETS table as a part of the (owner) process state. If your process crashes, but you want to save (some?) of its state, it sounds alarming. When the process restarts but it keeps the state (by giving it away to an unsuspecting supervisor), chances are, another crash is coming up. Until finally the supervisor decides to give up, and crashes too (wiping out the ETS table, and eventually restarting a larger tree). If that's the behaviour you're after, you should not need this PR, as you could just create the ETS table in the supervisor (and keep ownership by the supervisor). |
@max-au hm, it sounds like you grabbed the wrong end of the stick somehow 😅 I'll elaborate on it later when I get my hands on a proper computer, I'm on my mobile on a shaky train right now 😵💫 |
@max-au sorry for the delay in answering 😅 For one, this PR doesn't add anything really new. It does not enable (or disable, for that matter) anything that couldn't already be done now, bad and good alike. For another, don't pay too much attention to the introduction of That said, the idea is to let a supervisor create a table in its Instead of giving the table away, the supervisor could in fact also stay its owner, but then there would be no choice but to make the table You point out that this could be used in an attempt to save some of the owner process' state, and I agree that this is bad design, an anti-pattern. However, in modern applications, a supervision (sub-)tree often represents a work unit of several cooperating processes, instead of single worker processes (this is also what gave rise to EEP 56). Viewed from this angle, such a table could be seen as the state of the work unit, instead of as the state of a single process. Depending on the task and subsequently the structure and (in-)dependence of the constituting child processes, it might be possible to restart one or another of them if they crash, without the need to crash the entire work unit. I agree that misuse is possible, yes, but it is already possible right now, at the expense of some logging noise. |
The purpose of the 'ETS-TRANSFER' message was to avoid the risk of table leakage due to the receiver not being aware. Without it you must make sure to make the receiver aware some other way. For an |
@sverker ok, it is probably a bad idea to create a new button only to label it "Don't push!" 😅 So if this is your final verdict, I'll remove this part again. |
b64932b
to
ddfb954
Compare
@sverker last commit removes |
That I understand.
And this is exactly the kind of "soft friction" I like. One is not prohibited from leveraging this use-case. But there is a nagging indicator "you might be holding it wrong". :-) To give some more background, at a previous place there was quite an amount of code using Having said that, I'll repeat that I don't mind merging this PR. There is some value in it, even though it may introduce some "happy debugging, folks" episodes later. |
ddfb954
to
637da61
Compare
Last commit solves merge conflicts caused by the merge of #8026. |
Ping @sverker :) Is there still any interest in this? |
@Maria-12648430 @juhlig |
@sverker LGTM 👍🙂 |
@Maria-12648430 wrote
When I think about this; either I don't understand the use case, or I don't understand how supervisors work. What would the supervisor do with the inherited table? Or rather how can it do anything with the table? Is there a hook/callback that lets you run your own code within the supervisor when a child is restarted? |
In a sense, yes: The child start function (as given under the So if you have a supervisor init(_) ->
Tbl = ets:new(tab, [{heir, self()}]),
{ok,
#{},
[
#{id => my_child,
start => {my_child, start_link, [Tbl]},
restart => permanent}
]
} ... and a function start_link(Tbl) ->
{ok, Child} = gen_server:start_link(?MODULE, [], []),
ets:give_away(Tbl, Child, here_comes_the_table),
{ok, Child}. ... which, as I said, will be run inside the supervisor process when it (re-)starts a child, the table will be given to the child process when it starts, return to the supervisor process when it dies, and in turn be given to the new child process when it gets restarted. |
@Maria-12648430 Thanks for the OTP lesson 🙂. |
8c34478
to
ef659c9
Compare
from raw DbTerm pointer to boxed-tagged DbTerm pointer (or immediate as before).
Co-authored-by: Maria Scott <[email protected]>
ef659c9
to
74cf856
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I inserted a refactoring commit and rebased on a new master.
lib/stdlib/src/ets.erl
Outdated
- **`{heir,Pid,HeirData} | {heir,none}`** - Set a process as heir. The heir | ||
inherits the table if the owner terminates. Message | ||
`{'ETS-TRANSFER',tid(),FromPid,HeirData}` is sent to the heir when that | ||
occurs. The heir must be a local process. Default heir is `none`, which | ||
inherits the table if the owner terminates. If `HeirData` is given, a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{heir,Pid}
is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by me.
The current implementation of the
heir
option as given toets:new/2
andets:setopts/2
requires that a term is given asHeirData
in a{heir, HeirPid, HeirData}
3-tuple, which will be sent in an{'ETS-TRANSFER', ...}
message when the owner process dies and ownership passes to the heir process.This is not always feasible, for example when the heir process is a supervisor which passes the table to a child process on (re-)starts. If the child process dies, the supervisor will receive an
'ETS-TRANSFER'
message, which it can't do anything with, and will just log it as an unexpected message.This PR changes the behavior of
ets:new/2
andets:setopts/2
such that theheir
option to also accepts a 2-tuple of the form{heir, HeirPid}
, ie omitting theHeirData
. If the owner process dies, table ownership transfers to the heir process quietly.This PR also adds a new functiongive_away/2
, in order to do explicit transfer of table ownership quietly. This will probably be of somewhat lesser use, and is only added for the sake of symmetry/completeness.