예제 #1
0
        public bool OnLogReplication(AppendEntriesEvent appendEntriesEvent)
        {
            if (appendEntriesEvent.Entries != null && appendEntriesEvent.Entries.Count > 0)
            {
                foreach (var item in appendEntriesEvent.Entries)
                {
                    if (item.ObjectType == typeof(ServiceHealth).FullName)
                    {
                        var serviceHealth = JsonConvert.DeserializeObject <ServiceHealth>(item.Value.ToString());
                        _serviceHealthService.Create(serviceHealth.ServiceId, serviceHealth);
                    }
                    else if (item.ObjectType == typeof(Service).FullName)
                    {
                        var serviceData = JsonConvert.DeserializeObject <Service>(item.Value.ToString());
                        switch (item.Method)
                        {
                        case MethodType.Create:
                            _servicesService.Create(serviceData);
                            break;

                        case MethodType.Update:
                            _servicesService.Update(serviceData);
                            break;

                        case MethodType.Delete:
                            _servicesService.Delete(serviceData.Id);
                            break;
                        }
                    }
                    else if (item.ObjectType == typeof(KvProperty).FullName)
                    {
                        var kvProperty = JsonConvert.DeserializeObject <KvProperty>(item.Value.ToString());
                        switch (item.Method)
                        {
                        case MethodType.Create:
                            _kvPropertyService.Create(kvProperty);
                            break;

                        case MethodType.Update:
                            _kvPropertyService.Update(kvProperty);
                            break;

                        case MethodType.Delete:
                            _kvPropertyService.Delete(kvProperty.Key);
                            break;
                        }
                    }
                }
            }

            return(true);
        }
예제 #2
0
        private static void AppendItems(AppendEntriesEvent appendEntriesEvent, IEnumerable <dynamic> services)
        {
            foreach (var service in services)
            {
                var entry = new Entry
                {
                    Method     = MethodType.Create,
                    Value      = service,
                    ObjectType = service.GetType().FullName
                };

                appendEntriesEvent.Entries.Add(entry);
            }
        }
예제 #3
0
        public AppendEntriesEvent OnGetFullLog()
        {
            var appendEntriesEvent = new AppendEntriesEvent();

            appendEntriesEvent.Entries = new List <Entry>();

            var services      = _servicesService.Get();
            var kvProperties  = _kvPropertyService.Get();
            var serviceHealth = _serviceHealthService.Get();

            AppendItems(appendEntriesEvent, services);
            AppendItems(appendEntriesEvent, kvProperties);
            AppendItems(appendEntriesEvent, serviceHealth);

            return(appendEntriesEvent);
        }
예제 #4
0
        private Task <HttpResponseMessage> SendHeartbeat(string nodeAddress)
        {
            var url     = nodeAddress.AppendPathSegment("api/raft/append-entries");
            var request = new HttpRequestMessage(HttpMethod.Post, url);

            request.Headers.Authorization = new AuthenticationHeaderValue(
                _consensusContext.ConsensusOptions.AuthenticationScheme, _consensusContext.ConsensusOptions.AuthenticationParameter);

            var appendEntriesEvent = new AppendEntriesEvent(_consensusContext.CurrentTerm, _consensusContext.CurrentNode);
            var jsonContent        = JsonConvert.SerializeObject(appendEntriesEvent);
            var content            = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            request.Content = content;

            var task = _consensusContext.HttpClient.SendAsync(request);

            return(task);
        }
예제 #5
0
        private Task <HttpResponseMessage> SendAppendEntries(string nodeAddress, IList <Entry> entries)
        {
            try
            {
                var url     = nodeAddress.AppendPathSegment("api/raft/append-entries");
                var request = new HttpRequestMessage(HttpMethod.Post, url);
                request.Headers.Authorization = new AuthenticationHeaderValue(
                    _consensusContext.ConsensusOptions.AuthenticationScheme, _consensusContext.ConsensusOptions.AuthenticationParameter);

                var appendEntriesEvent = new AppendEntriesEvent(_consensusContext.CurrentTerm, _consensusContext.CurrentNode, entries);
                var jsonContent        = JsonConvert.SerializeObject(appendEntriesEvent);
                var content            = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                request.Content = content;

                var task = _httpClient.SendAsync(request);
                return(task);
            }
            catch (Exception exception)
            {
                _logger.LogDebug("Exception during sending entries.", exception);
                return(null);
            }
        }
예제 #6
0
        public ActionResult AppendEntries([FromBody] AppendEntriesEvent appendEntriesEvent)
        {
            IResponse response = null;

            if (_consensusContext.State == null)
            {
                response = new AppendEntriesResponse(_consensusContext.CurrentTerm, true);
                return(new ObjectResult(response));
            }

            _consensusContext.State.TriggerEvent(appendEntriesEvent);
            if (appendEntriesEvent.Entries != null && appendEntriesEvent.Entries.Count > 0)
            {
                var isSuccessfull = _consensusContext.LogReplicable.OnLogReplication(appendEntriesEvent);
                response = new AppendEntriesResponse(_consensusContext.CurrentTerm, isSuccessfull);
            }
            else
            {
                response = new AppendEntriesResponse(_consensusContext.CurrentTerm, true);
            }

            return(new ObjectResult(response));
        }