|
| 1 | +// Copyright 2020 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// +build go1.13 |
| 6 | +// +build linux darwin |
| 7 | + |
| 8 | +package pool |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + "log" |
| 14 | + "sort" |
| 15 | + "sync" |
| 16 | + "time" |
| 17 | + |
| 18 | + "golang.org/x/build/internal/cloud" |
| 19 | +) |
| 20 | + |
| 21 | +// entry contains the resource usage of an instance as well as |
| 22 | +// identifying information. |
| 23 | +type entry struct { |
| 24 | + createdAt time.Time |
| 25 | + instanceID string |
| 26 | + instanceName string |
| 27 | + vCPUCount int64 |
| 28 | +} |
| 29 | + |
| 30 | +// ledger contains a record of the instances and their resource |
| 31 | +// consumption. Before an instance is created, a call to the ledger |
| 32 | +// will ensure that there are available resources for the new instance. |
| 33 | +type ledger struct { |
| 34 | + mu sync.RWMutex |
| 35 | + // cpuLimit is the limit of how many on-demand vCPUs can be created on EC2. |
| 36 | + cpuLimit int64 |
| 37 | + // cpuUsed is the current count of vCPUs reserved for on-demand instances. |
| 38 | + cpuUsed int64 |
| 39 | + // entries contains a mapping of instance name to entries for each instance |
| 40 | + // that has resources allocated to it. |
| 41 | + entries map[string]*entry |
| 42 | + // types contains a mapping of instance type names to instance types for each |
| 43 | + // ARM64 EC2 instance. |
| 44 | + types map[string]*cloud.InstanceType |
| 45 | +} |
| 46 | + |
| 47 | +// newLedger creates a new ledger. |
| 48 | +func newLedger() *ledger { |
| 49 | + return &ledger{ |
| 50 | + entries: make(map[string]*entry), |
| 51 | + types: make(map[string]*cloud.InstanceType), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// ReserveResources attempts to reserve the resources required for an instance to be created. |
| 56 | +// It will attempt to reserve the resourses that an instance type would require. This will |
| 57 | +// attempt to reserve the resources until the context deadline is reached. |
| 58 | +func (l *ledger) ReserveResources(ctx context.Context, instName, vmType string) error { |
| 59 | + instType, err := l.PrepareReservationRequest(instName, vmType) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + t := time.NewTicker(2 * time.Second) |
| 65 | + defer t.Stop() |
| 66 | + for { |
| 67 | + if l.allocateCPU(instType.CPU, instName) { |
| 68 | + return nil |
| 69 | + } |
| 70 | + select { |
| 71 | + case <-t.C: |
| 72 | + case <-ctx.Done(): |
| 73 | + return ctx.Err() |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// PrepareReservationRequest ensures all the preconditions necessary for a reservation request are |
| 79 | +// met. If the conditions are met then an instance type for the requested VM type is returned. If |
| 80 | +// not an error is returned. |
| 81 | +func (l *ledger) PrepareReservationRequest(instName, vmType string) (*cloud.InstanceType, error) { |
| 82 | + l.mu.RLock() |
| 83 | + defer l.mu.RUnlock() |
| 84 | + |
| 85 | + instType, ok := l.types[vmType] |
| 86 | + if !ok { |
| 87 | + return nil, fmt.Errorf("unknown EC2 vm type: %s", vmType) |
| 88 | + } |
| 89 | + _, ok = l.entries[instName] |
| 90 | + if ok { |
| 91 | + return nil, fmt.Errorf("quota has already been allocated for %s of type %s", instName, vmType) |
| 92 | + } |
| 93 | + return instType, nil |
| 94 | +} |
| 95 | + |
| 96 | +// releaseResources deletes the entry associated with an instance. The resources associated to the |
| 97 | +// instance will also be released. An error is returned if the instance entry is not found. |
| 98 | +// Lock l.mu must be held by the caller. |
| 99 | +func (l *ledger) releaseResources(instName string) error { |
| 100 | + e, ok := l.entries[instName] |
| 101 | + if !ok { |
| 102 | + return fmt.Errorf("instance not found for releasing quota: %s", instName) |
| 103 | + } |
| 104 | + l.deallocateCPU(e.vCPUCount) |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +// allocateCPU ensures that there is enough CPU to allocate below the CPU Quota |
| 109 | +// for the caller to create a resouce with the numCPU passed in. If there is enough |
| 110 | +// then the ammount of used CPU will increase by the requested ammount. If there is |
| 111 | +// not enough CPU available, then a false is returned. In the event that CPU is allocated |
| 112 | +// an entry will be added in the entries map for the instance. |
| 113 | +func (l *ledger) allocateCPU(numCPU int64, instName string) bool { |
| 114 | + // should never happen |
| 115 | + if numCPU <= 0 { |
| 116 | + log.Printf("invalid allocation requested: %d", numCPU) |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + l.mu.Lock() |
| 121 | + defer l.mu.Unlock() |
| 122 | + |
| 123 | + if numCPU+l.cpuUsed > l.cpuLimit { |
| 124 | + return false |
| 125 | + } |
| 126 | + l.cpuUsed += numCPU |
| 127 | + e, ok := l.entries[instName] |
| 128 | + if ok { |
| 129 | + e.vCPUCount = numCPU |
| 130 | + } else { |
| 131 | + l.entries[instName] = &entry{ |
| 132 | + instanceName: instName, |
| 133 | + vCPUCount: numCPU, |
| 134 | + } |
| 135 | + } |
| 136 | + return true |
| 137 | +} |
| 138 | + |
| 139 | +// deallocateCPU releases the CPU allocated to an instance associated with an entry. When an instance |
| 140 | +// is deleted, the CPU allocated for the instance should not be counted against the CPU quota reserved |
| 141 | +// all on-demand instances. If an invalid CPU number is passed in the function will not lower the CPU count. |
| 142 | +// Lock l.mu must be held by the caller. |
| 143 | +func (l *ledger) deallocateCPU(numCPU int64) { |
| 144 | + if numCPU <= 0 { |
| 145 | + log.Printf("invalid deallocation requested: %d", numCPU) |
| 146 | + return |
| 147 | + } |
| 148 | + if l.cpuUsed-numCPU < 0 { |
| 149 | + log.Printf("attempting to deallocate more cpu than used: %d of %d", numCPU, l.cpuUsed) |
| 150 | + return |
| 151 | + } |
| 152 | + l.cpuUsed -= numCPU |
| 153 | +} |
| 154 | + |
| 155 | +// UpdateReservation updates the entry for an instance with the id value for that instance. If |
| 156 | +// an entry for the instance does not exist then an error will be returned. Another mechanism should |
| 157 | +// be used to manage untracked instances. Updating the reservation acts as a signal that the instance |
| 158 | +// has actually been created since the instance ID is known. |
| 159 | +func (l *ledger) UpdateReservation(instName, instID string) error { |
| 160 | + l.mu.Lock() |
| 161 | + defer l.mu.Unlock() |
| 162 | + |
| 163 | + e, ok := l.entries[instName] |
| 164 | + if !ok { |
| 165 | + return fmt.Errorf("unable to update reservation: instance not found %s", instName) |
| 166 | + } |
| 167 | + e.createdAt = time.Now() |
| 168 | + e.instanceID = instID |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +// Remove releases any reserved resources for an instance and deletes the associated entry. |
| 173 | +// An error is returned if and entry does not exist for the instance. |
| 174 | +func (l *ledger) Remove(instName string) error { |
| 175 | + l.mu.Lock() |
| 176 | + defer l.mu.Unlock() |
| 177 | + |
| 178 | + if err := l.releaseResources(instName); err != nil { |
| 179 | + return fmt.Errorf("unable to remove instance: %w", err) |
| 180 | + } |
| 181 | + delete(l.entries, instName) |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +// InstanceID retrieves the instance ID for an instance by looking up the instance name. |
| 186 | +// If an instance is not found, an empty string is returned. |
| 187 | +func (l *ledger) InstanceID(instName string) string { |
| 188 | + l.mu.RLock() |
| 189 | + defer l.mu.RUnlock() |
| 190 | + |
| 191 | + e, ok := l.entries[instName] |
| 192 | + if !ok { |
| 193 | + return "" |
| 194 | + } |
| 195 | + return e.instanceID |
| 196 | +} |
| 197 | + |
| 198 | +// SetCPULimit sets the vCPU limit used to determine if a CPU allocation would |
| 199 | +// cross the threshold for available CPU for on-demand instances. |
| 200 | +func (l *ledger) SetCPULimit(numCPU int64) { |
| 201 | + l.mu.Lock() |
| 202 | + defer l.mu.Unlock() |
| 203 | + |
| 204 | + l.cpuLimit = numCPU |
| 205 | +} |
| 206 | + |
| 207 | +// UpdateInstanceTypes updates the map of instance types used to map instance |
| 208 | +// type to the resources required for the instance. |
| 209 | +func (l *ledger) UpdateInstanceTypes(types []*cloud.InstanceType) { |
| 210 | + l.mu.Lock() |
| 211 | + defer l.mu.Unlock() |
| 212 | + |
| 213 | + for _, it := range types { |
| 214 | + l.types[it.Type] = it |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +// resources contains the current limit and usage of instance related resources. |
| 219 | +type resources struct { |
| 220 | + // InstCount is the count of how many on-demand instances are tracked in the ledger. |
| 221 | + InstCount int64 |
| 222 | + // CPUUsed is a count of the vCPU's for on-demand instances are currently allocated in the ledger. |
| 223 | + CPUUsed int64 |
| 224 | + // CPULimit is the limit of how many vCPU's for on-demand instances can be allocated. |
| 225 | + CPULimit int64 |
| 226 | +} |
| 227 | + |
| 228 | +// Resources retrives the resource usage and limits for instances in the |
| 229 | +// store. |
| 230 | +func (l *ledger) Resources() *resources { |
| 231 | + l.mu.RLock() |
| 232 | + defer l.mu.RUnlock() |
| 233 | + |
| 234 | + return &resources{ |
| 235 | + InstCount: int64(len(l.entries)), |
| 236 | + CPUUsed: l.cpuUsed, |
| 237 | + CPULimit: l.cpuLimit, |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +// ResourceTime give a ResourceTime entry for each active instance. |
| 242 | +// The resource time slice is storted by creation time. |
| 243 | +func (l *ledger) ResourceTime() []ResourceTime { |
| 244 | + l.mu.RLock() |
| 245 | + defer l.mu.RUnlock() |
| 246 | + |
| 247 | + ret := make([]ResourceTime, 0, len(l.entries)) |
| 248 | + for name, data := range l.entries { |
| 249 | + ret = append(ret, ResourceTime{ |
| 250 | + Name: name, |
| 251 | + Creation: data.createdAt, |
| 252 | + }) |
| 253 | + } |
| 254 | + sort.Sort(ByCreationTime(ret)) |
| 255 | + return ret |
| 256 | +} |
0 commit comments