Repository navigation
lib: Apply config changes sequentially (ref #5298) - #7188
Conversation
Actually I wanted to talk about that (the concept) mainly. I just started to explain what I had in mind without code, and thought that wouldn't be comprehensible. Thus I created an explanation plus code to compare if the explanation isn't fully comprehensible. |
|
I think the concept is sound. In general I think 1) starting a subscription, 2) getting the config, 3) continuing with life, should be a working sequence of operations for a subscriber even without this, as long as you're guaranteed to get notifications of all config changes that happen after the subscribe call returns. However, sequencing all config changes so that they are actually applied in the order they were asked for still seems worthwhile. |
|
I refactored (rewrote) as suggested with a buffered chan. There's still a bit of machinery as I didn't want to just spawn a goroutine on |
There was a problem hiding this comment.
So I understand what this tries to do, but I doubt it squashes a lot of bugs.
I think our bigger problem is things like
cfg = wcfg.Copy()
cfg.Fiddle = true
wcfg.Save(...)
which would not be solved by this, because someone else might have done SetFolders between Copy() and Save() which would now be lost.
The same problem applies at a smaller level too, whereby
devs := wcfg.Devices()
devs.forEach { it.Fiddle = true }
wcfg.SetDevices(devs)
Suddenly loses any devices added between Devices() and SetDevices().
The few ways I can think of solving this is turning config modification around.
All the types you read now like Devices() etc are immutable (probably return interfaces),
and the only way to mutate stuff is to do something like:
wcfg.Modify(func (cfg config.Configuration) {
cfg.Fiddle = true
})
or
leImmutableDevice.Modify(func (mutable) {
mutable.Fiddle = true
})
which is obviously serialized and potentially blocking, and probably PITA to use.
The other option would be remove ability to modify collections in bulk.
So no SetDevices, just AddDevice/RemoveDevice etc, then each struct gains a version, and on each read/modify/write we check if the version being written for this device matches the version currently in the config to catch outdated data writes, and prevent those.
Sprinkle retry logging everywhere.
Or idk, switch to some sort of CRDT library, plus counters?
| // Replace swaps the current configuration object for the given one. | ||
| func (w *wrapper) Replace(cfg Configuration) (Waiter, error) { | ||
| return w.replaceQueued(func() (Waiter, error) { | ||
| return w.replaceLocked(cfg.Copy()) |
There was a problem hiding this comment.
I know this doesn't affect this implementation, but in all of the functions, you probably want to copy the inputs
before you create the closure, just in case there is a reference to the same variable in some other routine that is not blocked by replaceQueued and is happily fiddling with the inputs.
| select { | ||
| case e = <-w.queue: | ||
| default: | ||
| w.queueMut.Unlock() |
There was a problem hiding this comment.
I don't think you need the lock for the queue, as it's only set on initialization, so the struct member is never written to, just read.
I agree. What you explained is a much bigger source of bugs. However solving it is also hard, while this is simple, and can happen now. I do believe the issue solved here might realistically cause bugs at startup, as it's quite realistic that a user changes stuff in the UI while folders are still rebuilding metadata. That immeadiately creates a race between handling folders in
Not that it changes much, but |
|
I see the thing with avoiding a serve() method, but it's a new design that we don't have anywhere else that's less easy to see is obviously correct. I think having an actual serve loop and combining that with Audrius' API suggestion would be better. That is, something like We can then be sure that things are serialized because there is only the one loop, and it can do things like set a timer to do a save one second after a config change, so multiple changes get aggregated, etc... We could still have methods like RemoveDevice and SetDevice. If SetDevice takes a device config it's not 100% safe against multiple mutators touching the same device at the same time, but really that's a design error regardless. If we really care then the Set... functions could of course take mutator functions like |
69f278e to
0c0998b
Compare
|
Now The end state is quite palatable, the diff... less so. Though a lot of it is boilerplate in tests. |
|
Sorry if this is not directly related, but I read about possibly aggregating config changes before calling // Ignored folder was removed, reconnect to retrigger the prompt.
if !toCfg.Paused && len(fromCfg.IgnoredFolders) > len(toCfg.IgnoredFolders) {
closeDevices = append(closeDevices, deviceID)
}If a folder is unignored and another one ignored in the same modification cycle, this check will not notice and neglect to replace the connection. I don't see an easy way except a deep comparison to catch this. |
|
I briefly considered aggregating config changes, but dropped that thought quickly because of various possible problems like the one you mentioned. I am not aware of ever mentioning that though. In this issue the only mentioning of aggregating is calmh's suggestion to defer saving the config (as in saving to file), which is independent of |
|
The new config wrapper service saves the config every 5 seconds if there are any changes now. Thus I removed all the The PR is ready from my side. |
|
|
||
| w.mut.Lock() | ||
| to := w.cfg.Copy() | ||
| if e.modifyFunc(&to) { |
There was a problem hiding this comment.
As an alternative to returning a boolean we could serialize the config before and after the function call and compare to see if they changed something relevant or not. Then perhaps the modifier could/should return an error, for cases where it starts modifying the config but then realizes something went wrong and needs to bail out... but if that is not a thing that can happen currently we could leave the modifier without return value?
There was a problem hiding this comment.
It seems a bit ugly to me to serialize or reflect to see if the config changed, when the caller knows whether they changed the config or not. Not that it matters performance wise. Well that's my reasoning, I am not attached to either solution though :)
There was a problem hiding this comment.
I was thinking it a bit ugly that I had to say whether I modified the config or not, when the config is there so the caller can plainly see if I did or not. :) It also wasn't 100% certain what the bool return meant at first glance.
Since it's just for saving we could even just remember the last serialized version and when going to save check if it's the same and then skip it.
But if there are cases where we start changing the config and then return false because we change our mind then this is legitimate. (I didn't check if we ever do that, some of the auto accept and introducer stuff is convoluted.)
(I'm also not heavily invested in this, it's fine as is, really.)
There was a problem hiding this comment.
There's a bit of ugliness everywhere :)
Since it's just for saving we could even just remember the last serialized version and when going to save check if it's the same and then skip it.
It's also to skip the subscriber verify and commit stages if unchanged.
But if there are cases where we start changing the config and then return false because we change our mind then this is legitimate. (I didn't check if we ever do that, some of the auto accept and introducer stuff is convoluted.)
We do. Previously it was a bunch of conditions based on reading the config to determine if we need to change something, then call some Wrapper.Set... method. Now all of this is a single call to Wrapper.Modify, the conditions check the config obtained as an argument from Wrapper.Modify. Still, we can use the same conditions to just return early without changes and let the wrapper realize that we didn't change anything on its own.
It also wasn't 100% certain what the bool return meant at first glance.
That tips it in favor of not having a bool to me: Less potential for confusion.
| done := make(chan struct{}) | ||
| go func() { | ||
| waiter.Wait() | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| if !saveTimerRunning { | ||
| saveTimer.Reset(minSaveInterval) | ||
| saveTimerRunning = true | ||
| } | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| } |
There was a problem hiding this comment.
Seems to me we could start the save timer directly when the modify returns and save some small complexity?
There was a problem hiding this comment.
Waiting for done happens to serialize modifications, i.e. only process a change once the previous one is fully in effect. Using that to restart the timer only afterward is just a side-effect. However regardless of complexity, starting it immediatly on the change feels like the better call: I don't see any reason to e.g. wait for a slow CommitConfiguration before saving.
| a.mainService.Add(connectionsService) | ||
|
|
||
| // Candidate builds always run with usage reporting. | ||
| a.cfg.Modify(func(cfg *config.Configuration) bool { |
There was a problem hiding this comment.
Are we sure mainService is running at this point? If not, this will hang, right?
There was a problem hiding this comment.
Yes and yes. Starting the api in this same function would already hang: It's waiting for the server to come up.
| t.Helper() | ||
| waiter, err := w.Modify(func(cfg *Configuration) bool { | ||
| waiter, err := w.Modify(func(cfg *Configuration) { | ||
| cfg = &to |
There was a problem hiding this comment.
This can't possibly work? Maybe *cfg = to but why doesn't anything fail due to this?
|
|
||
| _, err := w.Modify(func(cfg *Configuration) bool { | ||
| _, err := w.Modify(func(cfg *Configuration) { | ||
| cfg = &Configuration{Version: 3} |
| t.Helper() | ||
| waiter, err := w.Modify(func(cfg *config.Configuration) bool { | ||
| waiter, err := w.Modify(func(cfg *config.Configuration) { | ||
| *cfg = to |
calmh
left a comment
There was a problem hiding this comment.
LGTM apart from that which is impossible
|
The explanation for the impossibility: The entire test file with the "defect" is one large no-op. The only test within is skipped. I was unable to make the test fail (after fixing), so I'll unskip it to see what CI has to say. |
|
Aah it's |
|
And, do you want to give it a new chance? Or is it too 😬 |
|
Who knows, this change might even fix it?! Let's see. |
| // environment variable is set. | ||
|
|
||
| if build.IsCandidate && !upgrade.DisabledByCompilation && !runtimeOptions.NoUpgrade { | ||
| l.Infoln("Automatic upgrade is always enabled for candidate releases.") |
There was a problem hiding this comment.
This deadlocks because we're not running the service yet...
This is a proposal to make config changes in the current system less prone to races. It is thus related to #5298, however clearly does not address all the issues brought up there nor is it intending to.
It was prompted by the review comment about the order of initialising folders in the model and subscribing to config changes here: #7150 (comment)
That's inherently racy as it is, regardless of the order in which order it's done.
The proposed change here is that changes to the config only go ahead once all calls to
.CommitConfigurationdue to the previous change returned. If multiple config changes are outstanding, they will applied in the same order as the config change method (e.g.SetFolder) was called.In addition with
.Subscribereturning the currently active config, this allows a subscriber to subscribe to config changes, use the returned config for initialisation, and then start processing config changes. Thus starting with subscribing, they get all config changes sequentially in the correct order. Which I have done formodel.The obvious potential for problems here is locking. Previously anyone changing the config was only blocked until all previous config changes were dispatched asynchronously. Now blocking happens until all previous config changes took effect. If a subscriber is changing the config while holding a lock, that the subscriber also acquires on
CommitConfiguration, that can deadlock. However doing that is also problematic today (can cause concurrentCommitConfigurationcalls, which is also very bad) and we are aware of it, as e.g. evident in this comment:// Needs to happen outside of the fmut, as can cause CommitConfigurationI added an explicit clause to the comment on the
Committerinterface, which obviously doesn't guarantee anything, but might increase awareness.