Skip to content
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

Fix for race conditions in presistence plugins #1543

Merged
merged 1 commit into from
Dec 31, 2015

Conversation

Horusiath
Copy link
Contributor

This PR should fix race problems with lazy-loaded persistent plugins (#1522). It turns out that lazy loading isn't actually needed. Also plugins cache has been refactored to work in safer manner.

[EDIT:30.12.2015] More description

Actual problem seemed to lay in the way the plugin handlers were registered in Persistence extension. Because of concurrent access, all parties (i.e. persistent actors) were trying to lazily initialize plugin handler and AddOrUpdate it to concurrent dictionary - this part was handled correctly. However when there are no entry in dictionary, new plugin was initialized and overriding concurrently updated entry, that may appeared in the meantime, as the default dictionary conflict resolution was to take latest plugin created. However one of the plugin handler actions was new journal actor creation - that means, the latest plugin handler would trigger actor creation failure as that actor was already created by earlier plugin handlers already registered.

I think there were two solutions for this problem:

  • Use atomic writes + recursive plugin handler retrieving - that is the way, JVM akka followed. It uses basically no locks and that one I've decided to use.
  • Use ConcurrentDictionary.GetOrAdd method - this also seems to solve an issue, but lack of good example to recreate the problem on the first place seems a more riskier to me. Also turning back from concurrent dictionary into combination of atomic + immutable collection seems to be better option.

@Aaronontheweb
Copy link
Member

@Horusiath test failures

MemoryEndToEndAdapterSpec.EventAdapters_in_end_to_end_scenarios_should_allow_using_an_adapter_when_write_was_performed_without_an_adapter

MemoryEventAdapterSpec.EventAdapter_should_store_events_after_applying_adapter

@Aaronontheweb
Copy link
Member

@Horusiath saw that the tests were re-run - is there a known issue with the MemoryEndToEndAdapterSpec and MemoryEventAdapterSpec?

@Horusiath
Copy link
Contributor Author

I'd like to know it too. I've re-runed them to check if this are racy tests or repeatable issue associated with changes I've applied.

@Horusiath
Copy link
Contributor Author

As the previous attempt was not 100% thread safe, I've changed it to implementation which is more in line with JVM version - now plugin holders are synced through atomic references on immutable dictionaries.

var plugin = new Lazy<PluginHolder>(() => CreatePlugin(configPath, _ => DefaultPluginDispatcherId), LazyThreadSafetyMode.ExecutionAndPublication);
pluginContainer = _snapshotPluginExtensionIds.AddOrUpdate(configPath, plugin, (key, old) => plugin);
pluginContainer = new Lazy<PluginHolder>(() => CreatePlugin(configPath, _ => DefaultPluginDispatcherId), LazyThreadSafetyMode.ExecutionAndPublication);
_snapshotPluginExtensionIds.CompareAndSet(extensionIdMap, extensionIdMap.AddOrUpdate(configPath, pluginContainer));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - immutable copy + CAS

@Aaronontheweb
Copy link
Member

I think this approach should work. Going to merge it in and give it a try.

Aaronontheweb added a commit that referenced this pull request Dec 31, 2015
Fix for race conditions in presistence plugins
@Aaronontheweb Aaronontheweb merged commit 2eacc39 into akkadotnet:dev Dec 31, 2015
@Horusiath Horusiath deleted the persistence-race-fix branch March 23, 2016 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants