public void WhenReferencesAreEquivalent_ThenGetHasCodeIsSame()
        {
            var ref1 = new NodeTypeLocator("proj", "zone", "c2-node-60-240");
            var ref2 = new NodeTypeLocator("proj", "zone", "c2-node-60-240");

            Assert.AreEqual(ref1.GetHashCode(), ref2.GetHashCode());
        }
        public void WhenCreatedFromPath_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240";

            Assert.AreEqual(
                path,
                NodeTypeLocator.FromString(path).ToString());
        }
 public void WhenPathInvalid_FromStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => NodeTypeLocator.FromString(
                                           "projects/project-1/zones/us-central1-a/nodeTypes/"));
     Assert.Throws <ArgumentException>(() => NodeTypeLocator.FromString(
                                           "/zones/us-central1-a/nodeTypes/c2-node-60-240 "));
     Assert.Throws <ArgumentException>(() => NodeTypeLocator.FromString(
                                           "/"));
 }
        public void WhenCreatedFromUrl_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240";

            Assert.AreEqual(
                path,
                NodeTypeLocator.FromString(
                    "https://www.googleapis.com/compute/v1/" + path).ToString());
        }
        public void WhenPathIsValid_FromStringReturnsObject()
        {
            var ref1 = NodeTypeLocator.FromString(
                "projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240");

            Assert.AreEqual("nodeTypes", ref1.ResourceType);
            Assert.AreEqual("c2-node-60-240", ref1.Name);
            Assert.AreEqual("us-central1-a", ref1.Zone);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
        public void WhenUsingBetaApi_FromStringReturnsObject()
        {
            var ref1 = NodeTypeLocator.FromString(
                "https://compute.googleapis.com/compute/beta/projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240");

            Assert.AreEqual("nodeTypes", ref1.ResourceType);
            Assert.AreEqual("c2-node-60-240", ref1.Name);
            Assert.AreEqual("us-central1-a", ref1.Zone);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
        public void WhenReferencesAreEquivalent_ThenEqualsReturnsTrue()
        {
            var ref1 = new NodeTypeLocator("proj", "zone", "c2-node-60-240");
            var ref2 = new NodeTypeLocator("proj", "zone", "c2-node-60-240");

            Assert.IsTrue(ref1.Equals(ref2));
            Assert.IsTrue(ref1.Equals((object)ref2));
            Assert.IsTrue(ref1 == ref2);
            Assert.IsFalse(ref1 != ref2);
        }
        public void AddPlacement(Tenancies tenancy, string serverId, NodeTypeLocator nodeType, DateTime date)
        {
            Debug.Assert(!tenancy.IsFlagCombination());
            Debug.Assert(date <= this.lastEventDate);
            Debug.Assert(tenancy == Tenancies.SoleTenant || (serverId == null && nodeType == null));
            this.lastEventDate = date;

            DateTime placedUntil;

            if (this.placements.Count == 0)
            {
                if (this.lastStoppedOn.HasValue)
                {
                    Debug.Assert(this.lastStoppedOn != null);
                    Debug.Assert(date <= this.lastStoppedOn);

                    placedUntil = this.lastStoppedOn.Value;
                }
                else
                {
                    ApplicationTraceSources.Default.TraceWarning(
                        "Instance {0} was placed, but never stopped, " +
                        "and yet is not running anymore. Flagging as defunct.",
                        this.InstanceId);
                    this.missingStopEvent = true;
                    return;
                }
            }
            else
            {
                if (this.lastStoppedOn.HasValue)
                {
                    Debug.Assert(date <= this.lastStoppedOn);
                    Debug.Assert(date <= this.placements.First().From);

                    placedUntil = DateTimeUtil.Min(
                        this.lastStoppedOn.Value,
                        this.placements.First().From);
                }
                else
                {
                    Debug.Assert(date <= this.placements.First().From);
                    placedUntil = this.placements.First().From;
                }
            }

            if (tenancy == Tenancies.SoleTenant)
            {
                AddPlacement(new InstancePlacement(serverId, nodeType, date, placedUntil));
            }
            else
            {
                AddPlacement(new InstancePlacement(date, placedUntil));
            }
        }
        public void TestEqualsNull()
        {
            var ref1 = new NodeTypeLocator("proj", "zone", "c2-node-60-240");

            Assert.IsFalse(ref1.Equals(null));
            Assert.IsFalse(ref1.Equals((object)null));
            Assert.IsFalse(ref1 == null);
            Assert.IsFalse(null == ref1);
            Assert.IsTrue(ref1 != null);
            Assert.IsTrue(null != ref1);
        }
        public void OnSetPlacement(string serverId, NodeTypeLocator nodeType, DateTime date)
        {
            Debug.Assert(date <= this.lastEventDate);
            Debug.Assert(serverId != null);
            this.lastEventDate = date;

            // NB. While the serverId is always populated, the nodeType
            // is null for events emitted before August 2020.

            AddPlacement(Tenancies.SoleTenant, serverId, nodeType, date);
        }
        internal InstancePlacement(
            [JsonProperty("tenancy")] Tenancies tenancy,
            [JsonProperty("server")] string serverId,
            [JsonProperty("nodeType")] NodeTypeLocator nodeType,
            [JsonProperty("from")] DateTime from,
            [JsonProperty("to")] DateTime to)
        {
            Debug.Assert(from <= to);
            Debug.Assert(tenancy != Tenancies.Unknown);
            Debug.Assert(!tenancy.IsFlagCombination());

            this.Tenancy  = tenancy;
            this.ServerId = serverId;
            this.NodeType = nodeType;
            this.From     = from;
            this.To       = to;
        }
Пример #12
0
        internal NodeHistory(
            string serverId,
            NodeTypeLocator nodeType,
            DateTime firstUse,
            DateTime lastUse,
            uint peakInstanceCount,
            IEnumerable <NodePlacement> placements)
        {
            Debug.Assert(placements != null);
            Debug.Assert(firstUse <= lastUse);

            this.ServerId = serverId;
            this.NodeType = nodeType;
            this.FirstUse = firstUse;
            this.LastUse  = lastUse;
            this.PeakConcurrentPlacements = peakInstanceCount;
            this.Placements = placements;
        }
Пример #13
0
        internal static NodeAnnotation FromNodeType(NodeTypeLocator nodeType)
        {
            if (nodeType == null)
            {
                return(knownTypes[defaultNodeType]);
            }
            else if (knownTypes.TryGetValue(nodeType.Name, out NodeAnnotation annotation))
            {
                return(annotation);
            }
            else
            {
                TraceSources.IapDesktop.TraceWarning(
                    "Unrecognized node type {0}, assuming defaults",
                    nodeType);

                return(knownTypes[defaultNodeType]);
            }
        }
 public void AddExistingInstance(
     ulong instanceId,
     InstanceLocator reference,
     ImageLocator image,
     InstanceState state,
     DateTime lastSeen,
     Tenancies tenancy,
     string serverId,
     NodeTypeLocator nodeType)
 {
     Debug.Assert(!tenancy.IsFlagCombination());
     this.instanceBuilders[instanceId] = InstanceHistoryBuilder.ForExistingInstance(
         instanceId,
         reference,
         image,
         state,
         lastSeen,
         tenancy,
         serverId,
         nodeType);
 }
        internal static InstanceHistoryBuilder ForExistingInstance(
            ulong instanceId,
            InstanceLocator reference,
            ImageLocator image,
            InstanceState state,
            DateTime lastSeen,
            Tenancies tenancy,
            string serverId,
            NodeTypeLocator nodeType)
        {
            Debug.Assert(!tenancy.IsFlagCombination());
            Debug.Assert(state != InstanceState.Deleted);

            return(new InstanceHistoryBuilder(
                       instanceId,
                       reference,
                       image,
                       state,
                       lastSeen,
                       tenancy,
                       serverId,
                       nodeType));
        }
        //---------------------------------------------------------------------
        // Ctor
        //---------------------------------------------------------------------

        private InstanceHistoryBuilder(
            ulong instanceId,
            InstanceLocator reference,
            ImageLocator image,
            InstanceState state,
            DateTime?lastSeen,
            Tenancies tenancy,
            string serverId,
            NodeTypeLocator nodeType)
        {
            Debug.Assert(!tenancy.IsFlagCombination());
            Debug.Assert(tenancy == Tenancies.SoleTenant || (serverId == null && nodeType == null));

            if (instanceId == 0)
            {
                throw new ArgumentException("Instance ID cannot be 0");
            }

            this.InstanceId    = instanceId;
            this.reference     = reference;
            this.image         = image;
            this.lastStoppedOn = lastSeen;

            if (state == InstanceState.Running)
            {
                Debug.Assert(tenancy != Tenancies.Unknown);
                Debug.Assert(lastSeen != null);

                AddPlacement(new InstancePlacement(
                                 tenancy,
                                 serverId,
                                 nodeType,
                                 lastSeen.Value,
                                 lastSeen.Value));
            }
        }
 public InstancePlacement(string serverId, NodeTypeLocator nodeType, DateTime from, DateTime to)
     : this(Tenancies.SoleTenant, serverId, nodeType, from, to)
 {
 }
        public void AddExistingInstances(
            IEnumerable <Instance> instances,
            IEnumerable <NodeGroupNode> nodes,
            IEnumerable <Disk> disks,
            string projectId)
        {
            using (ApplicationTraceSources.Default.TraceMethod().WithParameters(projectId))
            {
                //
                // NB. Instances.list returns the disks associated with each
                // instance, but lacks the information about the source image.
                // Therefore, we load disks first and then join the data.
                //
                var sourceImagesByDisk = disks
                                         .EnsureNotNull()
                                         .ToDictionary(d => d.SelfLink, d => d.SourceImage);

                ApplicationTraceSources.Default.TraceVerbose("Found {0} existing disks", sourceImagesByDisk.Count());
                ApplicationTraceSources.Default.TraceVerbose("Found {0} existing instances", instances.Count());

                foreach (var instance in instances)
                {
                    ApplicationTraceSources.Default.TraceVerbose("Adding {0}", instance.Id);

                    var bootDiskUrl = instance.Disks
                                      .EnsureNotNull()
                                      .Where(d => d.Boot != null && d.Boot.Value)
                                      .EnsureNotNull()
                                      .Select(d => d.Source)
                                      .EnsureNotNull()
                                      .FirstOrDefault();
                    ImageLocator image = null;
                    if (bootDiskUrl != null &&
                        sourceImagesByDisk.TryGetValue(bootDiskUrl, out string imageUrl) &&
                        imageUrl != null)
                    {
                        image = ImageLocator.FromString(imageUrl);
                    }

                    var instanceLocator = new InstanceLocator(
                        projectId,
                        ShortZoneIdFromUrl(instance.Zone),
                        instance.Name);

                    if (instance.Scheduling.NodeAffinities != null && instance.Scheduling.NodeAffinities.Any())
                    {
                        // This VM runs on a sole-tenant node.
                        var node = nodes.FirstOrDefault(n => n.Instances
                                                        .EnsureNotNull()
                                                        .Select(uri => InstanceLocator.FromString(uri))
                                                        .Any(locator => locator == instanceLocator));
                        if (node == null)
                        {
                            ApplicationTraceSources.Default.TraceWarning(
                                "Could not identify node {0} is scheduled on",
                                instanceLocator);
                        }

                        AddExistingInstance(
                            (ulong)instance.Id.Value,
                            instanceLocator,
                            image,
                            instance.Status == "RUNNING"
                                ? InstanceState.Running
                                : InstanceState.Terminated,
                            this.EndDate,
                            Tenancies.SoleTenant,
                            node?.ServerId,
                            node?.NodeType != null
                                ? NodeTypeLocator.FromString(node.NodeType)
                                : null);
                    }
                    else
                    {
                        // Fleet VM.
                        AddExistingInstance(
                            (ulong)instance.Id.Value,
                            instanceLocator,
                            image,
                            instance.Status == "RUNNING"
                                ? InstanceState.Running
                                : InstanceState.Terminated,
                            this.EndDate,
                            Tenancies.Fleet,
                            null,
                            null);
                    }
                }
            }
        }