void UpdateClusterInformation(ClusterInfo clusterInfo) { lock (_lock) { if (clusterInfo.Id == Guid.Empty || _clusterId == clusterInfo.Id || _previousClusterId == clusterInfo.Id) { return; } _previousClusterId = _clusterId; _throwOnNodeError = false; // Lock configuration since it's shared with all clients created by GetClient on the configuration. lock (Configuration) { Configuration.Host = clusterInfo.MasterHost; Configuration.Port = clusterInfo.MasterPort; Nodes.Clear(); MasterNode = (RemoteEngineClient <T>)Configuration.GetClient <T>(); foreach (var hostAndPort in clusterInfo.Slaves) { var host = hostAndPort.Key; var port = hostAndPort.Value; CreateNode(host, port); } } _clusterId = clusterInfo.Id; } }
object Execute <TMessage>(RemoteEngineClient <T> node, TMessage objectToExecute) { object result = null; var request = new ClusterExecuteRequest(_clusterId, objectToExecute); try { result = node.SendAndRecieve(request); } catch (WrongNodeException e) { lock (_lock) { node = GetNode(e.Host, e.Port); } return(Execute(node, objectToExecute)); } catch (Exception e) { if (e is SocketException || e is IOException) { lock (_lock) { RemoveNode(node); node = MasterNode; } return(Execute(node, objectToExecute)); } throw; } if (result is ClusterExecuteResponse) { var msg = result as ClusterExecuteResponse; if (msg.ClusterUpdated) { UpdateClusterInformation(msg.ClusterInfo); } return(msg.Payload); } throw new NotSupportedException("Format of returned data is unexpected."); }
void RemoveNode(RemoteEngineClient <T> node) { var nodeIndex = Nodes.IndexOf(node); if (nodeIndex >= 0) { Nodes.Remove(node); } if ((nodeIndex == 0 || Nodes.Count == 0)) { if (_throwOnNodeError) { throw new NotSupportedException("Lost connection to master."); } _throwOnNodeError = true; ResetConnection(); } }
public void ResetConnection() { _clusterId = Guid.Empty; _previousClusterId = Guid.Empty; MasterNode = (RemoteEngineClient <T>)Configuration.GetClient <T>(); }