|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package subscribers |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "log/slog" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/apache/airavata-custos/connectors/SLURM/Rest-Client/pkg/client" |
| 28 | + "github.com/apache/airavata-custos/pkg/models" |
| 29 | +) |
| 30 | + |
| 31 | +// tresFor builds a SLURM TRES from a stored resource type and count. A GRES |
| 32 | +// resource is stored joined as "gres/gpu" but SLURM wants it split into type |
| 33 | +// and name; a plain resource like "cpu" has no name. |
| 34 | +func tresFor(resourceType string, count int64) client.TRES { |
| 35 | + if typ, name, ok := strings.Cut(resourceType, "/"); ok { |
| 36 | + return client.TRES{Type: typ, Name: name, Count: count} |
| 37 | + } |
| 38 | + return client.TRES{Type: resourceType, Count: count} |
| 39 | +} |
| 40 | + |
| 41 | +// errNotProvisioned means the account does not exist on the cluster yet, so |
| 42 | +// writing the association now would make slurmctld cache a failed uid lookup |
| 43 | +// and reject the user's jobs. The reconciler retries these. |
| 44 | +var errNotProvisioned = errors.New("cluster account not provisioned yet") |
| 45 | + |
| 46 | +// assocKey identifies an association the way Slurm does. Cluster is not part |
| 47 | +// of it because callers scope their lookups to one cluster already. |
| 48 | +type assocKey struct { |
| 49 | + account string |
| 50 | + user string |
| 51 | + partition string |
| 52 | +} |
| 53 | + |
| 54 | +func keyOf(a client.Association) assocKey { |
| 55 | + return assocKey{account: a.Account, user: a.User, partition: a.Partition} |
| 56 | +} |
| 57 | + |
| 58 | +// sameLimits reports whether two associations already carry the same limits. |
| 59 | +// Only the limits are compared: the key fields match by construction. |
| 60 | +func sameLimits(want, got client.Association) bool { |
| 61 | + return sameTRES(want.Limits.GrpTRES, got.Limits.GrpTRES) && |
| 62 | + sameTRES(want.Limits.GrpTRESMins, got.Limits.GrpTRESMins) |
| 63 | +} |
| 64 | + |
| 65 | +func sameTRES(want, got []client.TRES) bool { |
| 66 | + if len(want) != len(got) { |
| 67 | + return false |
| 68 | + } |
| 69 | + counts := make(map[string]int64, len(want)) |
| 70 | + for _, t := range want { |
| 71 | + counts[t.Type] = t.Count |
| 72 | + } |
| 73 | + for _, t := range got { |
| 74 | + if c, ok := counts[t.Type]; !ok || c != t.Count { |
| 75 | + return false |
| 76 | + } |
| 77 | + } |
| 78 | + return true |
| 79 | +} |
| 80 | + |
| 81 | +// upsertAssociationsForMembership writes the association for every resource on |
| 82 | +// the membership's allocation, without checking what the cluster already has. |
| 83 | +// The event paths use this: an event fires because something changed. |
| 84 | +func (a *AssociationSubscriber) upsertAssociationsForMembership(ctx context.Context, membership models.ComputeAllocationMembership) error { |
| 85 | + return a.syncAssociationsForMembership(ctx, membership, nil) |
| 86 | +} |
| 87 | + |
| 88 | +// syncAssociationsForMembership builds the association for each of the |
| 89 | +// allocation's resources and writes the ones that differ from existing, the |
| 90 | +// state already on the cluster. A nil existing writes all of them. |
| 91 | +// |
| 92 | +// Every caller goes through here on purpose. Slurm keys an association by |
| 93 | +// (cluster, account, user, partition) and its upsert is last-write-wins, so a |
| 94 | +// caller that built the record without limits would wipe limits another caller |
| 95 | +// had set. Folding the per-member overrides in here keeps all writers |
| 96 | +// producing the same record for the same key. |
| 97 | +func (a *AssociationSubscriber) syncAssociationsForMembership(ctx context.Context, membership models.ComputeAllocationMembership, existing map[assocKey]client.Association) error { |
| 98 | + allocation, err := a.coreService.GetComputeAllocation(ctx, membership.ComputeAllocationID) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("get compute allocation: %w", err) |
| 101 | + } |
| 102 | + cluster, err := a.coreService.GetComputeCluster(ctx, allocation.ComputeClusterID) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("get compute cluster: %w", err) |
| 105 | + } |
| 106 | + csu, err := a.coreService.GetComputeClusterUserByPair(ctx, cluster.ID, membership.UserID) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("get compute cluster user: %w", err) |
| 109 | + } |
| 110 | + if csu.ProvisionedAt == nil { |
| 111 | + return errNotProvisioned |
| 112 | + } |
| 113 | + |
| 114 | + resources, err := a.coreService.ListResourcesForAllocation(ctx, allocation.ID) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("list resources for allocation: %w", err) |
| 117 | + } |
| 118 | + if len(resources) == 0 { |
| 119 | + // Nothing to map onto a partition. Skipping beats guessing a |
| 120 | + // partition name the cluster may not have. |
| 121 | + slog.Warn("Allocation has no resources, no association written", |
| 122 | + "allocation_id", allocation.ID, "user_id", membership.UserID) |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + overrides, err := a.coreService.ListOverridesForMembership(ctx, membership.ID) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("list overrides for membership: %w", err) |
| 129 | + } |
| 130 | + overrideByResource := make(map[string]models.ComputeAllocationMembershipResourceOverride, len(overrides)) |
| 131 | + for _, o := range overrides { |
| 132 | + overrideByResource[o.ComputeAllocationResourceID] = o |
| 133 | + } |
| 134 | + |
| 135 | + for _, resource := range resources { |
| 136 | + association := client.Association{ |
| 137 | + Account: allocation.Name, |
| 138 | + Cluster: cluster.Name, |
| 139 | + User: csu.LocalUsername, |
| 140 | + Partition: resource.Name, |
| 141 | + QoS: []string{"normal"}, |
| 142 | + Limits: limitsFor(resource, overrideByResource[resource.ID]), |
| 143 | + } |
| 144 | + if got, ok := existing[keyOf(association)]; ok && sameLimits(association, got) { |
| 145 | + continue |
| 146 | + } |
| 147 | + if err := a.slurmClient.UpsertAssociation(association); err != nil { |
| 148 | + return fmt.Errorf("upsert association for partition %s: %w", resource.Name, err) |
| 149 | + } |
| 150 | + slog.Info("Upserted association", "association", association) |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// limitsFor turns a per-member override into association limits. A zero |
| 156 | +// override means the member draws on the allocation-wide limits, so the |
| 157 | +// association carries none of its own. |
| 158 | +func limitsFor(resource models.ComputeAllocationResource, override models.ComputeAllocationMembershipResourceOverride) client.AssocLimits { |
| 159 | + var limits client.AssocLimits |
| 160 | + if override.OverrideResourceAmount > 0 { |
| 161 | + limits.GrpTRES = []client.TRES{tresFor(resource.ResourceType, override.OverrideResourceAmount)} |
| 162 | + } |
| 163 | + if override.OverrideResourceTime > 0 { |
| 164 | + limits.GrpTRESMins = []client.TRES{tresFor(resource.ResourceType, override.OverrideResourceTime)} |
| 165 | + } |
| 166 | + return limits |
| 167 | +} |
0 commit comments