|
| 1 | +package maxworkersattr |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "runtime" |
| 7 | + "strconv" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "ocm.software/ocm/api/datacontext" |
| 11 | + ocmlog "ocm.software/ocm/api/utils/logging" |
| 12 | + rtruntime "ocm.software/ocm/api/utils/runtime" |
| 13 | +) |
| 14 | + |
| 15 | +var realm = ocmlog.SubRealm("api/ocm/extensions/attrs/maxworkersattr") |
| 16 | + |
| 17 | +const ( |
| 18 | + // TransferWorkersEnvVar defines the environment variable that configures |
| 19 | + // the maximum number of concurrent transfer workers. |
| 20 | + // |
| 21 | + // If set to a positive integer value, that number of concurrent workers is used. |
| 22 | + // If set to "auto", the number of logical CPU cores on the system is used. |
| 23 | + // If unset, the attribute value (if any) or the default of SingleWorker (1) is used. |
| 24 | + TransferWorkersEnvVar = "OCM_TRANSFER_WORKER_COUNT" |
| 25 | + |
| 26 | + // ATTR_KEY is the globally unique key under which this attribute is registered |
| 27 | + // in the OCM data context. It follows the ocm.software naming convention. |
| 28 | + ATTR_KEY = "ocm.software/ocm/api/ocm/extensions/attrs/maxworkers" |
| 29 | + |
| 30 | + // ATTR_SHORT is the short alias of the attribute, suitable for CLI or YAML use. |
| 31 | + ATTR_SHORT = "maxworkers" |
| 32 | + |
| 33 | + // SingleWorker is the default number of workers (1) used when no configuration |
| 34 | + // is provided. This mode guarantees deterministic ordering of operations. |
| 35 | + SingleWorker uint = 1 |
| 36 | + |
| 37 | + // AutomaticWorkersBasedOnCPU is the string literal used to indicate that the number of workers |
| 38 | + // should be automatically determined based on the number of logical CPU cores. |
| 39 | + AutomaticWorkersBasedOnCPU = "auto" |
| 40 | +) |
| 41 | + |
| 42 | +func init() { |
| 43 | + if err := datacontext.RegisterAttributeType(ATTR_KEY, AttributeType{}, ATTR_SHORT); err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// AttributeType implements the datacontext.AttributeType interface for |
| 49 | +// the `maxworkers` attribute. It controls the maximum concurrency used |
| 50 | +// during resource and source transfer operations. |
| 51 | +type AttributeType struct{} |
| 52 | + |
| 53 | +// Name returns the globally unique key for this attribute. |
| 54 | +func (a AttributeType) Name() string { return ATTR_KEY } |
| 55 | + |
| 56 | +// Description provides extended docs for this attribute. |
| 57 | +func (a AttributeType) Description() string { |
| 58 | + return ` |
| 59 | +*integer* or *"auto"* |
| 60 | +Specifies the maximum number of concurrent workers to use for resource and source, |
| 61 | +as well as reference transfer operations. |
| 62 | +
|
| 63 | +Supported values: |
| 64 | + - A positive integer: use exactly that number of workers. |
| 65 | + - The string "auto": automatically use the number of logical CPU cores. |
| 66 | + - Zero or omitted: fall back to single-worker mode (1). This is the default. |
| 67 | + This mode guarantees deterministic ordering of operations. |
| 68 | +
|
| 69 | +Precedence: |
| 70 | + 1. Attribute set in the current OCM context. |
| 71 | + 2. Environment variable OCM_TRANSFER_WORKER_COUNT. |
| 72 | + 3. Default value (1). |
| 73 | +
|
| 74 | +WARNING: This is an experimental feature and may cause unexpected behavior |
| 75 | +depending on workload concurrency. Values above 1 may result in non-deterministic |
| 76 | +transfer ordering. |
| 77 | +` |
| 78 | +} |
| 79 | + |
| 80 | +// Encode converts the attribute's Go value into its marshaled representation. |
| 81 | +// It supports uint, int, and string ("auto") forms. |
| 82 | +func (a AttributeType) Encode(v interface{}, m rtruntime.Marshaler) ([]byte, error) { |
| 83 | + switch val := v.(type) { |
| 84 | + case uint: |
| 85 | + return m.Marshal(val) |
| 86 | + case int: |
| 87 | + if val < 0 { |
| 88 | + return nil, fmt.Errorf("negative integer for %s not allowed", ATTR_SHORT) |
| 89 | + } |
| 90 | + return m.Marshal(uint(val)) |
| 91 | + case string: |
| 92 | + if val != AutomaticWorkersBasedOnCPU { |
| 93 | + return nil, fmt.Errorf("invalid string value for %s: %q", ATTR_SHORT, val) |
| 94 | + } |
| 95 | + return m.Marshal(val) |
| 96 | + default: |
| 97 | + return nil, fmt.Errorf("unsupported type %T for %s", v, ATTR_SHORT) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Decode converts marshaled bytes back into Go form (either uint or "auto"). |
| 102 | +func (a AttributeType) Decode(data []byte, unmarshaller rtruntime.Unmarshaler) (interface{}, error) { |
| 103 | + // Try uint first (e.g., `6`) |
| 104 | + var value uint |
| 105 | + if err := unmarshaller.Unmarshal(data, &value); err == nil { |
| 106 | + return value, nil |
| 107 | + } |
| 108 | + |
| 109 | + // Try string next (e.g., `"auto"` or `"6"`) |
| 110 | + var s string |
| 111 | + if err := unmarshaller.Unmarshal(data, &s); err == nil { |
| 112 | + switch s { |
| 113 | + case AutomaticWorkersBasedOnCPU: |
| 114 | + return s, nil |
| 115 | + default: |
| 116 | + if parsedVal, err := strconv.ParseUint(s, 10, 32); err == nil { |
| 117 | + return uint(parsedVal), nil |
| 118 | + } |
| 119 | + return nil, fmt.Errorf("invalid string value for %s: %q", ATTR_SHORT, s) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return nil, fmt.Errorf("failed to decode %s", ATTR_SHORT) |
| 124 | +} |
| 125 | + |
| 126 | +//////////////////////////////////////////////////////////////////////////////// |
| 127 | + |
| 128 | +// Get returns the resolved number of concurrent transfer workers from the context. |
| 129 | +// Resolution order: |
| 130 | +// 1. Attribute value (ctx) |
| 131 | +// 2. Environment variable OCM_TRANSFER_WORKER_COUNT |
| 132 | +// 3. Default SingleWorker (1) |
| 133 | +// |
| 134 | +// The resolver only auto-detects CPUs if the value is exactly "auto". |
| 135 | +// Any 0 resolves to SingleWorker. |
| 136 | +func Get(ctx datacontext.Context) (uint, error) { |
| 137 | + val := SingleWorker |
| 138 | + var err error |
| 139 | + |
| 140 | + if attribute := ctx.GetAttributes().GetAttribute(ATTR_KEY); attribute != nil { |
| 141 | + val, err = resolveWorkers(attribute) |
| 142 | + } else if env, ok := os.LookupEnv(TransferWorkersEnvVar); ok { |
| 143 | + val, err = resolveWorkers(env) |
| 144 | + } |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + return 0, err |
| 148 | + } |
| 149 | + |
| 150 | + if val > SingleWorker { |
| 151 | + warnUnstableOnce.Do(func() { |
| 152 | + ctx.Logger(realm).Warn("attribute is set to more than 1 worker, this may cause unexpected behavior") |
| 153 | + }) |
| 154 | + } |
| 155 | + |
| 156 | + // 3) Default |
| 157 | + return val, nil |
| 158 | +} |
| 159 | + |
| 160 | +// warnUnstableOnce ensures we only log only one warning if the attribute is retrieved multiple times. |
| 161 | +var warnUnstableOnce sync.Once |
| 162 | + |
| 163 | +// Set stores the attribute after validation via the unified resolver. |
| 164 | +// Accepts uint, int>=0, or the string "auto". |
| 165 | +func Set(ctx datacontext.Context, workers any) error { |
| 166 | + val, err := resolveWorkers(workers) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + return ctx.GetAttributes().SetAttribute(ATTR_KEY, val) |
| 171 | +} |
| 172 | + |
| 173 | +//////////////////////////////////////////////////////////////////////////////// |
| 174 | + |
| 175 | +// resolveWorkers normalizes all supported input forms into a concrete uint. |
| 176 | +// Supported forms: |
| 177 | +// - uint, int >= 0 |
| 178 | +// - string "auto" → runtime.NumCPU() |
| 179 | +// - numeric string (e.g. "4") → parsed value |
| 180 | +// - 0 → SingleWorker |
| 181 | +func resolveWorkers(v any) (uint, error) { |
| 182 | + switch t := v.(type) { |
| 183 | + case nil: |
| 184 | + return SingleWorker, nil |
| 185 | + |
| 186 | + case uint: |
| 187 | + if t == 0 { |
| 188 | + return SingleWorker, nil |
| 189 | + } |
| 190 | + return t, nil |
| 191 | + |
| 192 | + case int: |
| 193 | + if t < 0 { |
| 194 | + return 0, fmt.Errorf("%s cannot be negative", ATTR_SHORT) |
| 195 | + } |
| 196 | + if t == 0 { |
| 197 | + return SingleWorker, nil |
| 198 | + } |
| 199 | + return uint(t), nil |
| 200 | + |
| 201 | + case string: |
| 202 | + if t == AutomaticWorkersBasedOnCPU { |
| 203 | + n := runtime.NumCPU() |
| 204 | + if n <= 0 { |
| 205 | + return SingleWorker, nil |
| 206 | + } |
| 207 | + return uint(n), nil |
| 208 | + } |
| 209 | + // Try numeric string conversion |
| 210 | + if parsed, err := strconv.ParseUint(t, 10, 32); err == nil { |
| 211 | + if parsed == 0 { |
| 212 | + return SingleWorker, nil |
| 213 | + } |
| 214 | + return uint(parsed), nil |
| 215 | + } |
| 216 | + return 0, fmt.Errorf("invalid string value for %s: %q", ATTR_SHORT, t) |
| 217 | + |
| 218 | + default: |
| 219 | + return 0, fmt.Errorf("unexpected %s type %T", ATTR_SHORT, v) |
| 220 | + } |
| 221 | +} |
0 commit comments