Пример #1
0
        public async Task CheckpointAsync()
        {
            _settings.LockExpirationTime = DateTime.UtcNow.AddSeconds(LockTime);
            await _workerRepository.CheckpointAsync(_settings).ConfigureAwait(false);

            var workers = await _workerRepository.GetWorkersAsync().ConfigureAwait(false);

            var removedWorkers = _hash.GetNodes().Where(w => workers.All(a => a.Id != w.Id));
            var changed        = false;

            foreach (var worker in removedWorkers)
            {
                if (_hash.RemoveNode(worker))
                {
                    changed = true;
                    Logger.LogWarning($"Remove worker from hash {worker} {worker.Description}");
                }
            }

            foreach (var worker in workers)
            {
                if (_hash.AddNode(worker))
                {
                    Logger.LogInformation($"Add worker to hash {worker} {worker.Description}");
                    changed = true;
                }
            }

            if (changed)
            {
                var nodeHashes = new List <int>();

                foreach (var i in Enumerable.Range(0, Universe))
                {
                    var node = _hash.GetShardForKey(i.ToString());

                    if (node.Id == _settings.Id)
                    {
                        nodeHashes.Add(i);
                    }
                }

                NodeHashes = nodeHashes;
            }


            Logger.LogInformation($"Lock {_settings.Id} {_settings.Description}");
        }