private void HandleMessage(CreateEventMarket message) { try { var cluster = Cluster.Get(Context.System); var replicator = DistributedData.Get(Context.System).Replicator; var key = new ORSetKey <string>($"Event-{message.EventId}"); var writeConsistency = new WriteMajority(TimeSpan.FromSeconds(2)); replicator.Tell(Dsl.Update(key, ORSet <string> .Empty, writeConsistency, existing => existing.Add(cluster, message.Market))); var localEvent = replicator.Ask <IGetResponse>(Dsl.Get(key, ReadLocal.Instance)); Sender.Tell(localEvent.Result); } catch (Exception e) { _log.Error(e, "Unable to process message CreateEventMarket for Event {0} Market {1}", message.EventId, message.Market); Sender.Tell( $"Unable to process message CreateEventMarket for Event {message.EventId} Market {message.Market}"); } }
//The way our data works, I opted to just removed all the key data, but leave the key intact as sometimes i need to use the key again in rare instances, but once deleted, ddata doesn't allow this. private void HandleMessage(CloseEvent message) { try { var replicator = DistributedData.Get(Context.System).Replicator; var cluster = Cluster.Get(Context.System); var key = new ORSetKey <string>($"Event-{message.EventId}"); var writeConsistency = WriteLocal.Instance; replicator.Tell(Dsl.Update(key, ORSet <string> .Empty, writeConsistency, $"Event-{message.EventId}", existing => { return(existing.Clear(cluster)); })); var finalResult = replicator.Ask <IGetResponse>(Dsl.Get(key, ReadLocal.Instance)); Sender.Tell(finalResult.Result); } catch (DataDeletedException e) { Sender.Tell($"Event {message.EventId} has been deleted"); } catch (Exception e) { _log.Error(e, "Unable to process message CloseEvent for Event {0}", message.EventId); Sender.Tell($"Unable to process message CloseEvent for Event {message.EventId}"); } }
public async Task DistributedData_Replicator_Defaults_to_NoSupervisor() { const string replicatorActorPath = "/user/ddataReplicator"; const string durableStoreActorPath = "/user/ddataReplicator/durableStore"; await InitCluster(); var replicator = DistributedData.Get(_sys1).Replicator; IActorRef durableStore = null; await AwaitAssertAsync(() => { durableStore = _sys1.ActorSelection(durableStoreActorPath).ResolveOne(TimeSpan.FromSeconds(3)) .ContinueWith( m => m.Result).Result; }, TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(100)); Watch(replicator); Watch(durableStore); durableStore.Tell(new InitFail()); // termination orders aren't guaranteed, so can't use ExpectTerminated here var terminated1 = ExpectMsg <Terminated>(TimeSpan.FromSeconds(10)); var terminated2 = ExpectMsg <Terminated>(TimeSpan.FromSeconds(10)); ImmutableHashSet.Create(terminated1.ActorRef, terminated2.ActorRef).Should().BeEquivalentTo(durableStore, replicator); // The replicator should not have been recreated, so expect ActorNotFound await Assert.ThrowsAsync <ActorNotFoundException>(() => _sys1.ActorSelection(replicatorActorPath).ResolveOne(TimeSpan.FromSeconds(5))); }
private static async Task Main() { Console.WriteLine("Put the ddata mdb file in a folder called cluster-data in the application root folder. Press a key when done"); Console.Read(); var cfg = ConfigurationFactory.ParseString(File.ReadAllText("HOCON")) .WithFallback(DistributedData.DefaultConfig()); var originalColour = Console.ForegroundColor; var sys = ActorSystem.Create("test", cfg); var dd = DistributedData.Get(sys); int emptyKeyCount = 0; var resp = await dd.Replicator.Ask <GetKeysIdsResult>(Dsl.GetKeyIds); foreach (var resultKey in resp.Keys) { var key = new ORSetKey <string>($"{resultKey}"); var keyResp = await dd.Replicator.Ask <IGetResponse>(Dsl.Get(key)); Console.ForegroundColor = ConsoleColor.Green; if (keyResp.Get(key).Elements.Count == 0) { emptyKeyCount++; } Console.WriteLine($"{key.Id}\t{string.Join<string>(",", keyResp.Get(key).Elements)}"); } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Finished loading {resp.Keys.Count} keys. There were {emptyKeyCount} empty keys"); Console.ForegroundColor = originalColour; }
public LocalConcurrencySpec(ITestOutputHelper output) : base(ConfigurationFactory.ParseString(@" akka.actor.provider = ""Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"" akka.remote.dot-netty.tcp.port = 0"), "LocalConcurrencySpec", output) { _replicator = DistributedData.Get(Sys).Replicator; }
public App(ActorSystem system) { this.system = system; this.cluster = Cluster.Get(system); this.ddata = DistributedData.Get(system); this.timeout = TimeSpan.FromSeconds(10); this.readConsistency = new ReadMajority(timeout); this.writeConsistency = new WriteMajority(timeout); }
public Updater() { var cluster = Cluster.Cluster.Get(Context.System); var replicator = DistributedData.Get(Context.System).Replicator; Receive <string>(s => { var update = Dsl.Update(Key, ORSet <string> .Empty, WriteLocal.Instance, old => old.Add(cluster.SelfUniqueAddress, s)); replicator.Tell(update); }); }
static void StartActors(ActorSystem sys) { var replicator = DistributedData.Get(sys).Replicator; catalogRegistries.Add(sys.ActorOf( Props.Create(() => new CatalogRegistryActor()), "CatalogRegistryActor")); genericActors.Add(sys.ActorOf(Props.Create(() => new SomeGenericActor(replicator)), "SomeActor")); }
public JepsenInspiredInsertSpec(JepsenInspiredInsertSpecConfig config) : base(config) { _cluster = Cluster.Cluster.Get(Sys); _replicator = DistributedData.Get(Sys).Replicator; _nodes = Roles.Remove(Controller); _nodeCount = _nodes.Count; _timeout = Dilated(TimeSpan.FromSeconds(3)); _expectedData = Enumerable.Range(0, _totalCount).ToArray(); var nodeindex = _nodes.Zip(Enumerable.Range(0, _nodes.Count - 1), (name, i) => new KeyValuePair <int, RoleName>(i, name)) .ToImmutableDictionary(); _data = Enumerable.Range(0, _totalCount).GroupBy(i => nodeindex[i % _nodeCount]) .ToImmutableDictionary(x => x.Key, x => (IEnumerable <int>)x.ToArray()); }
private void Join(RoleName from, RoleName to) { RunOn(() => { Cluster.Join(Node(to).Address); _replicator = DistributedData.Get(Sys).Replicator; _nodeConfig = new NodeConfig(from.Name) { GossipTimeFrameInSeconds = 5 }; _actorDirectory = new ActorDirectory <XmlMessage, XmlMessagePattern>(Sys); }, from); EnterBarrier(from.Name + "-joined"); }
public async Task <IEnumerable <MatchingActor> > GetMatchingActorsAsync(M message, IMessagePatternFactory <P> messagePatternFactory) { var replicator = DistributedData.Get(ActorSystem).Replicator; GetKeysIdsResult keys = await replicator.Ask <GetKeysIdsResult>(Dsl.GetKeyIds); var matchingActors = new List <MatchingActor>(); foreach (var node in keys.Keys) { var setKey = new LWWRegisterKey <ActorProps[]>(node); var getResponse = await replicator.Ask <IGetResponse>(Dsl.Get(setKey, ReadLocal.Instance)); if (!getResponse.IsSuccessful) { throw new Exception($"cannot get message patterns for node {node}"); } var actorPropsList = getResponse.Get(setKey).Value; foreach (var actorProps in actorPropsList) { var path = actorProps.Path; var matchingPatterns = actorProps.Patterns.Where(pattern => pattern.Match(message)); int matchingPatternsCount = matchingPatterns.Count(); if (matchingPatternsCount > 0) { var matchingPattern = matchingPatterns.First(); if (matchingPatternsCount > 1) { Logger.Warning("For actor {0}, found {1} patterns matching message {2}. Taking first one = {3}", path, matchingPatternsCount, message, matchingPattern); } matchingActors.Add( new MatchingActor { Path = path, IsSecondary = matchingPattern.IsSecondary, MatchingScore = matchingPattern.Conjuncts.Length, MistrustFactor = actorProps.MistrustFactor }); } } } return(matchingActors); }
public void Pruning_of_durable_CRDT_should_move_data_from_removed_node() { Join(first, first); Join(second, first); var sys2 = ActorSystem.Create(Sys.Name, Sys.Settings.Config); var cluster2 = Akka.Cluster.Cluster.Get(sys2); var distributedData2 = DistributedData.Get(sys2); var replicator2 = StartReplicator(sys2); var probe2 = new TestProbe(sys2, new XunitAssertions()); cluster2.Join(Node(first).Address); AwaitAssert(() => { cluster.State.Members.Count.ShouldBe(4); cluster.State.Members.All(m => m.Status == MemberStatus.Up).ShouldBe(true); cluster2.State.Members.Count.ShouldBe(4); cluster2.State.Members.All(m => m.Status == MemberStatus.Up).ShouldBe(true); }, TimeSpan.FromSeconds(10)); EnterBarrier("joined"); Within(TimeSpan.FromSeconds(5), () => AwaitAssert(() => { replicator.Tell(Dsl.GetReplicaCount); ExpectMsg(new ReplicaCount(4)); replicator2.Tell(Dsl.GetReplicaCount, probe2.Ref); probe2.ExpectMsg(new ReplicaCount(4)); })); replicator.Tell(Dsl.Update(keyA, GCounter.Empty, WriteLocal.Instance, c => c.Increment(cluster, 3))); ExpectMsg(new UpdateSuccess(keyA, null)); replicator2.Tell(Dsl.Update(keyA, GCounter.Empty, WriteLocal.Instance, c => c.Increment(cluster2.SelfUniqueAddress, 2)), probe2.Ref); probe2.ExpectMsg(new UpdateSuccess(keyA, null)); EnterBarrier("updates-done"); Within(TimeSpan.FromSeconds(10), () => AwaitAssert(() => { replicator.Tell(Dsl.Get(keyA, new ReadAll(TimeSpan.FromSeconds(1)))); var counter1 = ExpectMsg <GetSuccess>().Get(keyA); counter1.Value.ShouldBe(10UL); counter1.State.Count.ShouldBe(4); })); Within(TimeSpan.FromSeconds(10), () => AwaitAssert(() => { replicator2.Tell(Dsl.Get(keyA, new ReadAll(TimeSpan.FromSeconds(1))), probe2.Ref); var counter2 = probe2.ExpectMsg <GetSuccess>().Get(keyA); counter2.Value.ShouldBe(10UL); counter2.State.Count.ShouldBe(4); })); EnterBarrier("get1"); RunOn(() => cluster.Leave(cluster2.SelfAddress), first); Within(TimeSpan.FromSeconds(15), () => AwaitAssert(() => { replicator.Tell(Dsl.GetReplicaCount); ExpectMsg(new ReplicaCount(3)); })); EnterBarrier("removed"); RunOn(() => sys2.Terminate().Wait(TimeSpan.FromSeconds(5)), first); Within(TimeSpan.FromSeconds(15), () => { var values = ImmutableHashSet <int> .Empty; AwaitAssert(() => { replicator.Tell(Dsl.Get(keyA, ReadLocal.Instance)); var counter3 = ExpectMsg <GetSuccess>().Get(keyA); var value = counter3.Value; values = values.Add((int)value); value.ShouldBe(10UL); counter3.State.Count.ShouldBe(3); }); values.ShouldBe(ImmutableHashSet.Create(10)); }); EnterBarrier("prunned"); RunOn(() => { var address = cluster2.SelfAddress; var sys3 = ActorSystem.Create(Sys.Name, ConfigurationFactory.ParseString($@" akka.remote.dot-netty.tcp.port = {address.Port} ").WithFallback(Sys.Settings.Config)); var cluster3 = Akka.Cluster.Cluster.Get(sys3); var replicator3 = StartReplicator(sys3); var probe3 = new TestProbe(sys3, new XunitAssertions()); cluster3.Join(Node(first).Address); Within(TimeSpan.FromSeconds(10), () => { var values = ImmutableHashSet <int> .Empty; AwaitAssert(() => { replicator3.Tell(Dsl.Get(keyA, ReadLocal.Instance), probe3.Ref); var counter4 = probe3.ExpectMsg <GetSuccess>().Get(keyA); var value = counter4.Value; values = values.Add((int)value); value.ShouldBe(10UL); counter4.State.Count.ShouldBe(3); }); values.ShouldBe(ImmutableHashSet.Create(10)); }); // all must at least have seen it as joining AwaitAssert(() => { cluster3.State.Members.Count.ShouldBe(4); cluster3.State.Members.All(m => m.Status == MemberStatus.Up).ShouldBeTrue(); }, TimeSpan.FromSeconds(10)); // after merging with others replicator3.Tell(Dsl.Get(keyA, new ReadAll(RemainingOrDefault))); var counter5 = ExpectMsg <GetSuccess>().Get(keyA); counter5.Value.ShouldBe(10UL); counter5.State.Count.ShouldBe(3); }, first); EnterBarrier("sys3-started"); replicator.Tell(Dsl.Get(keyA, new ReadAll(RemainingOrDefault))); var counter6 = ExpectMsg <GetSuccess>().Get(keyA); counter6.Value.ShouldBe(10UL); counter6.State.Count.ShouldBe(3); EnterBarrier("after-1"); }
public Listener() { var ddata = DistributedData.Get(Context.System); }