Exemplo n.º 1
0
        public async Task <string> GetMyUrl(IEnumerable <string> urls, TimeSpan latencyMs)
        {
            string myUrl = null;

            while (myUrl == null)
            {
                foreach (var url in urls)
                {
                    //Create a test controller temporarily
                    var testConnector = new HttpNodeConnector(url, latencyMs, TimeSpan.FromMilliseconds(-1));

                    Guid?nodeId = null;
                    try
                    {
                        nodeId = (await testConnector.GetNodeInfoAsync()).Id;
                        if (nodeId == _nodeStorage.Id)
                        {
                            myUrl = url;
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.LogWarning("Node at url " + url + " was unreachable...");
                    }
                }
                if (myUrl == null)
                {
                    Logger.LogWarning("Node is not discoverable from the given node urls!");
                }
            }
            return(myUrl);
        }
Exemplo n.º 2
0
        public async Task <ConcurrentDictionary <Guid, string> > CollectVotes(int currentTerm, Guid candidateid, int lastLogIndex, int lastLogTerm)
        {
            try
            {
                ConcurrentDictionary <Guid, string> nodes = new ConcurrentDictionary <Guid, string>();

                var tasks = _members.Select(async nodeUrl =>
                {
                    try
                    {
                        var testConnector = new HttpNodeConnector(nodeUrl, _latencyTolerance, TimeSpan.FromMilliseconds(-1));
                        var result        = await testConnector.Send(new RequestVote()
                        {
                            Term         = currentTerm,
                            CandidateId  = candidateid,
                            LastLogIndex = lastLogIndex,
                            LastLogTerm  = lastLogTerm
                        });

                        if (result.IsSuccessful)
                        {
                            var addResult = nodes.TryAdd(result.NodeId, nodeUrl);
                            if (!addResult)
                            {
                                Console.WriteLine("Failed to add " + result.NodeId + " url: " + nodeUrl);
                            }
                        }
                        else
                        {
                            Logger.LogDebug("Node at " + nodeUrl + " rejected vote request.");
                        }
                    }
                    catch (TaskCanceledException e)
                    {
                        Logger.LogWarning("Encountered error while getting vote from node at " + nodeUrl + ", request timed out...");
                    }
                    catch (Exception e)
                    {
                        Logger.LogWarning("Encountered error while getting vote from node at " + nodeUrl + ", request failed with error \"" + e.Message + "\"");
                    }
                });

                await Task.WhenAll(tasks);

                return(nodes);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to run election with error " + e.StackTrace);
            }
        }
        public static INodeConnector Create(NodeConnectionType type, string address)
        {
            INodeConnector connector;

            switch (type)
            {
            case NodeConnectionType.HTTP:
                connector = new HttpNodeConnector();
                break;

            case NodeConnectionType.IPC:
                connector = new IpcNodeConnector();
                break;

            default: throw new Exception("Unknown INodeConnectionType.");
            }

            connector.Address = address;

            return(connector);
        }