public bool CanSelect() { if (IsCompanyUser && ApplicationList.Count() == 1) return false; if (IsSchoolUser && ApplicationList.Count() == 1 && SchoolList.Count() == 1) return false; return true; }
/// <summary> /// This function gathers the state of the cluster on startup and caches the information /// Changes to cluster state are handled through notifications. /// /// Capture information for each replica for every service running in the cluster. /// </summary> /// <returns></returns> public static async Task InitializePartitionData() { EnvoyDefaults.LogMessage("InitializePartitionData started"); // Populate data locally Dictionary <Guid, SF_Partition> partitionData = new Dictionary <Guid, SF_Partition>(); var queryManager = client.QueryManager; ApplicationList applications = null; try { applications = await queryManager.GetApplicationListAsync(); } catch (Exception e) { EnvoyDefaults.LogMessage("GetApplicationListAsync: Failed"); EnvoyDefaults.LogMessage(String.Format("Error={0}", e.Message)); EnvoyDefaults.LogMessage(String.Format("Error={0}", e.StackTrace)); Environment.FailFast("GetApplicationListAsync failed"); } EnvoyDefaults.LogMessage("GetApplicationListAsync: Succeeded"); EnvoyDefaults.LogMessage(String.Format("GetApplicationListAsync: Application Count = {0}", applications.Count())); EnvoyDefaults.LogMessage("Start Enumerating Applications", EnvoyDefaults.IndentOperation.BeginLevel); foreach (var application in applications) { EnvoyDefaults.LogMessage(String.Format("{0}: Start Enumerating Services", application.ApplicationName), EnvoyDefaults.IndentOperation.BeginLevel); var services = await queryManager.GetServiceListAsync(application.ApplicationName); EnvoyDefaults.LogMessage(String.Format("{0}: Service Count = {1}", application.ApplicationName, services.Count())); foreach (var service in services) { EnvoyDefaults.LogMessage(String.Format("{0}: Start Enumerating Partitions", service.ServiceName), EnvoyDefaults.IndentOperation.BeginLevel); var partitions = await queryManager.GetPartitionListAsync(service.ServiceName); EnvoyDefaults.LogMessage(String.Format("{0}: Partition Count = {1}", service.ServiceName, partitions.Count())); foreach (var partition in partitions) { List <SF_Endpoint> listeners = new List <SF_Endpoint>(); EnvoyDefaults.LogMessage(String.Format("{0}: Start Enumerating Replicas", partition.PartitionInformation.Id), EnvoyDefaults.IndentOperation.BeginLevel); var replicas = await queryManager.GetReplicaListAsync(partition.PartitionInformation.Id); EnvoyDefaults.LogMessage(String.Format("{0}: Replica Count = {1}", partition.PartitionInformation.Id, replicas.Count())); foreach (var replica in replicas) { if (replica.ReplicaAddress.Length == 0) { EnvoyDefaults.LogMessage(String.Format("{0} = <Empty>", replica.NodeName)); continue; } EnvoyDefaults.LogMessage(String.Format("{0} = {1}", replica.NodeName, replica.ReplicaAddress)); JObject addresses; try { addresses = JObject.Parse(replica.ReplicaAddress); } catch { continue; } var replicaListeners = addresses["Endpoints"].Value <JObject>(); foreach (var replicaListener in replicaListeners) { var role = ServiceEndpointRole.Stateless; if (partition.ServiceKind == ServiceKind.Stateful) { var statefulRole = ((StatefulServiceReplica)replica).ReplicaRole; switch (statefulRole) { case ReplicaRole.Primary: role = ServiceEndpointRole.StatefulPrimary; break; case ReplicaRole.ActiveSecondary: role = ServiceEndpointRole.StatefulSecondary; break; default: role = ServiceEndpointRole.Invalid; break; } } int listenerIndex = listeners.FindIndex(x => x.Name == replicaListener.Key); if (listenerIndex == -1) { listeners.Add(new SF_Endpoint(replicaListener.Key)); listenerIndex = listeners.Count - 1; } try { var listenerAddressString = replicaListener.Value.ToString(); EnvoyDefaults.LogMessage(String.Format("AddressString = {0}", listenerAddressString)); var listenerAddress = new UriBuilder(listenerAddressString).Uri; if (listenerAddress.HostNameType == UriHostNameType.Dns) { var ipaddrs = Dns.GetHostAddresses(listenerAddress.Host); foreach (var ipaddr in ipaddrs) { if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { var saddrstring = ipaddr.ToString(); if (saddrstring.StartsWith("172")) { listenerAddress = new Uri(listenerAddress.Scheme + "://" + saddrstring + ":" + listenerAddress.Port + listenerAddress.PathAndQuery); break; } } } } listeners[listenerIndex].AddInstance(role, listenerAddress); } catch (System.Exception e) { EnvoyDefaults.LogMessage(String.Format("Error={0}", e)); } } EnvoyDefaults.LogMessage(String.Format("{0}: End Enumerating Replicas", partition.PartitionInformation.Id), EnvoyDefaults.IndentOperation.EndLevel); } // Remove any listeners without active endpoints listeners.RemoveAll(x => x.InstanceCount() == 0); if (listeners.Count != 0) { var partitionInfo = new SF_Partition(service.ServiceName, service.ServiceKind, partition.PartitionInformation, null, listeners); partitionData[partition.PartitionInformation.Id] = partitionInfo; } EnvoyDefaults.LogMessage(String.Format("{0}: End Enumerating Partitions", service.ServiceName), EnvoyDefaults.IndentOperation.EndLevel); } EnvoyDefaults.LogMessage(String.Format("{0}: End Enumerating Services", application.ApplicationName), EnvoyDefaults.IndentOperation.EndLevel); } EnvoyDefaults.LogMessage("End Enumerating Applications", EnvoyDefaults.IndentOperation.EndLevel); } // Process changes received through notifications lock (lock_) { foreach (var partition in partitionsAdd_) { partitionData[partition.Key] = partition.Value; } foreach (var partition in partitionsRemove_) { partitionData.Remove(partition.Key); } // Finally update global state, populate Service List and log details partitions_ = partitionData; services_ = new Dictionary <string, ServicePartitions>(); foreach (var partition in partitionData) { AddPartitionToService(partition.Key, partition.Value); EnvoyDefaults.LogMessage(String.Format("Added: {0}={1}", partition.Key, JsonConvert.SerializeObject(partition.Value))); } partitionsRemove_ = null; partitionsAdd_ = null; } }