Esempio n. 1
0
        /// <summary>
        /// Subtracts another resource spec from this one.
        /// </summary>
        /// <param name="other">The other resource spec to subtract.</param>
        /// <returns>The subtracted resource spec.</returns>
        public ResourceSpec Subtract(ResourceSpec other)
        {
            Preconditions.CheckNotNull(other, "Cannot subtract null resources");

            if (Equals(Unknown) || other.Equals(Unknown))
            {
                return(Unknown);
            }

            Preconditions.CheckArgument(other.LessThanOrEqual(this), "Cannot subtract a larger ResourceSpec from this one.");

            var target = new ResourceSpec(
                CpuCores.Merge(other.CpuCores),
                TaskHeapMemory.Subtract(other.TaskHeapMemory),
                TaskOffHeapMemory.Subtract(other.TaskOffHeapMemory),
                OnHeapManagedMemory.Subtract(other.OnHeapManagedMemory),
                OffHeapManagedMemory.Subtract(other.OffHeapManagedMemory));

            foreach (var(key, value) in ExtendedResources)
            {
                target.ExtendedResources.Add(key, value);
            }

            foreach (var resource in other.ExtendedResources.Values)
            {
                var temp       = target.ExtendedResources[resource.Name];
                var subtracted = temp.Subtract(resource);

                target.ExtendedResources.Add(temp.Name, subtracted);
            }

            return(target);
        }
Esempio n. 2
0
        /// <summary>
        /// Used by system internally to merge the other resources of chained operators when generating the job graph.
        /// </summary>
        /// <param name="other">Reference to resource to merge in.</param>
        /// <returns>The new resource with merged values.</returns>
        public ResourceSpec Merge(ResourceSpec other)
        {
            Preconditions.CheckNotNull(other, "Cannot merge with null resources");

            if (Equals(Unknown) || other.Equals(Unknown))
            {
                return(Unknown);
            }

            var target = new ResourceSpec(
                CpuCores.Merge(other.CpuCores),
                TaskHeapMemory.Add(other.TaskHeapMemory),
                TaskOffHeapMemory.Add(other.TaskOffHeapMemory),
                OnHeapManagedMemory.Add(other.OnHeapManagedMemory),
                OffHeapManagedMemory.Add(other.OffHeapManagedMemory));

            foreach (var(key, value) in ExtendedResources)
            {
                target.ExtendedResources.Add(key, value);
            }

            foreach (var resource in other.ExtendedResources.Values)
            {
                var temp   = target.ExtendedResources[resource.Name];
                var merged = temp.Merge(resource);

                target.ExtendedResources.Add(temp.Name, merged);
            }

            return(target);
        }
Esempio n. 3
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = (CpuCores != null ? CpuCores.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (TaskHeapMemory != null ? TaskHeapMemory.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (TaskOffHeapMemory != null ? TaskOffHeapMemory.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (OnHeapManagedMemory != null ? OnHeapManagedMemory.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (OffHeapManagedMemory != null ? OffHeapManagedMemory.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (ExtendedResources != null ? ExtendedResources.GetHashCode() : 0);
         return(hashCode);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Checks the current resource less than or equal with the other resource by comparing all the fields in the resource.
        /// </summary>
        /// <param name="other">The resource to compare</param>
        /// <returns>True if current resource is less than or equal with the other resource, otherwise return false.</returns>
        public bool LessThanOrEqual(ResourceSpec other)
        {
            Preconditions.CheckNotNull(other, "Cannot compare with null resources");

            if (Equals(Unknown) && other.Equals(Unknown))
            {
                return(true);
            }

            if (Equals(Unknown) || other.Equals(Unknown))
            {
                throw new IllegalArgumentException("Cannot compare specified resources with UNKNOWN resources.");
            }

            var cmp1 = CpuCores.Value < other.CpuCores.Value;
            var cmp2 = TaskHeapMemory.CompareTo(other.TaskHeapMemory);
            var cmp3 = TaskOffHeapMemory.CompareTo(other.TaskOffHeapMemory);
            var cmp4 = OnHeapManagedMemory.CompareTo(other.OnHeapManagedMemory);
            var cmp5 = OffHeapManagedMemory.CompareTo(other.OffHeapManagedMemory);

            if (cmp1 && cmp2 <= 0 && cmp3 <= 0 && cmp4 <= 0 && cmp5 <= 0)
            {
                foreach (var resource in ExtendedResources.Values)
                {
                    if (!other.ExtendedResources.ContainsKey(resource.Name) ||
                        (other.ExtendedResources.TryGetValue(resource.Name, out var it) && it.Value < resource.Value))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }