public PerformanceMonitor(TimeSpan updateInterval) { // schedule periodical updates this.cancelUpdates = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(updateInterval, updateInterval, Self, CollectMetrics.Instance, ActorRefs.NoSender); var cluster = Cluster.Get(Context.System); // create and start performance counters for a local node this.cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); this.memCounter = new PerformanceCounter("Memory", "Available MBytes"); cpuCounter.NextValue(); memCounter.NextValue(); // assign to a distributed store of performance metrics var perfCounterKey = new LWWDictionaryKey <UniqueAddress, PerformanceMetrics>("perf-counters"); var replicator = DistributedData.DistributedData.Get(Context.System).Replicator; replicator.Tell(Dsl.Subscribe(perfCounterKey, Self)); Receive <Replicator.Changed>(changed => { lastResult = lastResult.Merge(changed.Get(perfCounterKey)); }); Receive <CollectMetrics>(_ => { var cpuUsage = cpuCounter.NextValue(); var memUsage = memCounter.NextValue(); lastResult = lastResult.SetItem(cluster, cluster.SelfUniqueAddress, new PerformanceMetrics(cpuUsage, memUsage)); replicator.Tell(Dsl.Update(perfCounterKey, lastResult, WriteLocal.Instance, null, map => lastResult.Merge(map))); }); }
/// <summary> /// Merges data from provided <see cref="LWWDictionary{TKey,TValue}"/> into current CRDT, /// creating new immutable instance in a result. /// </summary> /// <param name="dictionary">TBD</param> /// <returns>TBD</returns> public LocalLWWDictionary <TKey, TVal> Merge(LWWDictionary <TKey, TVal> dictionary) => new LocalLWWDictionary <TKey, TVal>(_currentNode, _crdt.Merge(dictionary));