manta-server 2.0.0-beta.61

Manta HTTP server — single API that proxies to CSM / Ochami backends.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Pin / Unpin node-selection algorithms and their shared infrastructure
//! (pattern parsing, target-group existence check, resource-sufficiency
//! validation, and group-membership update orchestration).

use std::collections::HashMap;

use manta_backend_dispatcher::{
  error::Error, interfaces::hsm::group::GroupTrait, types::Group,
};

use super::{NodeHwCountVec, scoring};
use crate::server::common::app_context::InfraContext;

// ── Pin algorithm ────────────────────────────────────────────────────────────

/// Node selection algorithm for PIN mode — keeps as many existing target nodes
/// as possible, pulling from parent only when needed.
//
// Scores are HW-component scarcity ratios — always non-negative, always
// well within `usize` range. The `f64 as usize` casts used as hashmap
// keys for bucketing nodes are intentional truncation; Rust's saturating
// `as` semantics handle any non-finite edge case.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn calculate_target_group_pin(
  user_defined_hsm_hw_components_count_hashmap: &HashMap<String, usize>,
  user_defined_hw_component_vec: &[String],
  combination_target_parent_hsm_node_hw_component_count_vec: &mut NodeHwCountVec,
  target_hsm_node_hw_component_count_vec: &mut NodeHwCountVec,
  parent_hsm_node_hw_component_count_vec: &mut NodeHwCountVec,
  hw_component_scarcity_scores_hashmap: &HashMap<String, f64>,
) -> Result<NodeHwCountVec, Error> {
  let mut combination_target_parent_hsm_hw_component_summary_hashmap: HashMap<
    String,
    usize,
  > = scoring::calculate_group_hw_component_summary(
    combination_target_parent_hsm_node_hw_component_count_vec,
  );
  let target_hsm_hw_component_summary_hashmap: HashMap<String, usize> =
    scoring::calculate_group_hw_component_summary(
      target_hsm_node_hw_component_count_vec,
    );
  let parent_hsm_hw_component_summary_hashmap: HashMap<String, usize> =
    scoring::calculate_group_hw_component_summary(
      parent_hsm_node_hw_component_count_vec,
    );

  let mut target_hsm_node_score_tuple_vec: Vec<(String, f64)> =
    scoring::calculate_group_node_scores_from_final_hsm(
      target_hsm_node_hw_component_count_vec,
      &target_hsm_hw_component_summary_hashmap,
      user_defined_hsm_hw_components_count_hashmap,
      hw_component_scarcity_scores_hashmap,
    );

  let mut parent_hsm_node_score_tuple_vec: Vec<(String, f64)> =
    scoring::calculate_group_node_scores_from_final_hsm(
      parent_hsm_node_hw_component_count_vec,
      &parent_hsm_hw_component_summary_hashmap,
      user_defined_hsm_hw_components_count_hashmap,
      hw_component_scarcity_scores_hashmap,
    );

  let mut group_target_hsm_node_by_score_hashmap: HashMap<usize, Vec<String>> =
    HashMap::new();
  for (node, score) in &target_hsm_node_score_tuple_vec {
    group_target_hsm_node_by_score_hashmap
      .entry(*score as usize)
      .and_modify(|node_vec| node_vec.push(node.clone()))
      .or_insert(vec![node.clone()]);
  }

  let mut group_parent_hsm_node_by_score_hashmap: HashMap<usize, Vec<String>> =
    HashMap::new();
  for (node, score) in &parent_hsm_node_score_tuple_vec {
    group_parent_hsm_node_by_score_hashmap
      .entry(*score as usize)
      .and_modify(|node_vec| node_vec.push(node.clone()))
      .or_insert(vec![node.clone()]);
  }

  let mut nodes_migrated_from_combination_target_parent_hsm: Vec<(
    String,
    HashMap<String, usize>,
  )> = Vec::new();

  let (mut best_candidate, mut best_candidate_counters) =
    scoring::get_best_candidate_in_target_and_parent_hsm(
      &mut target_hsm_node_score_tuple_vec,
      &mut parent_hsm_node_score_tuple_vec,
      target_hsm_node_hw_component_count_vec,
      parent_hsm_node_hw_component_count_vec,
    )
    .ok_or_else(|| {
      Error::InsufficientResources("No best candidate found".to_string())
    })?;

  let mut work_to_do = scoring::keep_iterating_final_hsm(
    user_defined_hsm_hw_components_count_hashmap,
    &combination_target_parent_hsm_hw_component_summary_hashmap,
  );

  let mut iter = 0;

  while work_to_do {
    tracing::info!("----- ITERATION {} -----", iter);

    tracing::info!(
      "HSM group hw component counters: {:?}",
      combination_target_parent_hsm_hw_component_summary_hashmap
    );
    tracing::info!(
      "Final hw component counters the user wants: {:?}",
      user_defined_hsm_hw_components_count_hashmap
    );
    tracing::info!(
      "Best candidate is '{}' with score {} and hw \
       component counters {:?}",
      best_candidate.0,
      best_candidate.1,
      best_candidate_counters
    );

    scoring::print_score_table(
      user_defined_hw_component_vec,
      target_hsm_node_hw_component_count_vec,
      &target_hsm_node_score_tuple_vec,
    );

    scoring::print_score_table(
      user_defined_hw_component_vec,
      parent_hsm_node_hw_component_count_vec,
      &parent_hsm_node_score_tuple_vec,
    );

    nodes_migrated_from_combination_target_parent_hsm
      .push((best_candidate.0.clone(), best_candidate_counters.clone()));

    combination_target_parent_hsm_node_hw_component_count_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    target_hsm_node_hw_component_count_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    parent_hsm_node_hw_component_count_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    if combination_target_parent_hsm_node_hw_component_count_vec.is_empty() {
      break;
    }

    combination_target_parent_hsm_hw_component_summary_hashmap =
      scoring::calculate_group_hw_component_summary(
        combination_target_parent_hsm_node_hw_component_count_vec,
      );

    target_hsm_node_score_tuple_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    parent_hsm_node_score_tuple_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    let mut target_hsm_node_score_tuple_vec: Vec<(String, f64)> =
      scoring::calculate_group_node_scores_from_final_hsm(
        target_hsm_node_hw_component_count_vec,
        &combination_target_parent_hsm_hw_component_summary_hashmap,
        user_defined_hsm_hw_components_count_hashmap,
        hw_component_scarcity_scores_hashmap,
      );

    let mut parent_hsm_node_score_tuple_vec: Vec<(String, f64)> =
      scoring::calculate_group_node_scores_from_final_hsm(
        parent_hsm_node_hw_component_count_vec,
        &combination_target_parent_hsm_hw_component_summary_hashmap,
        user_defined_hsm_hw_components_count_hashmap,
        hw_component_scarcity_scores_hashmap,
      );

    let mut group_target_hsm_node_by_score_hashmap: HashMap<
      usize,
      Vec<String>,
    > = HashMap::new();
    for (node, score) in &target_hsm_node_score_tuple_vec {
      group_target_hsm_node_by_score_hashmap
        .entry(*score as usize)
        .and_modify(|node_vec| node_vec.push(node.clone()))
        .or_insert(vec![node.clone()]);
    }

    let mut group_parent_hsm_node_by_score_hashmap: HashMap<
      usize,
      Vec<String>,
    > = HashMap::new();
    for (node, score) in &parent_hsm_node_score_tuple_vec {
      group_parent_hsm_node_by_score_hashmap
        .entry(*score as usize)
        .and_modify(|node_vec| node_vec.push(node.clone()))
        .or_insert(vec![node.clone()]);
    }

    (best_candidate, best_candidate_counters) =
      scoring::get_best_candidate_in_target_and_parent_hsm(
        &mut target_hsm_node_score_tuple_vec,
        &mut parent_hsm_node_score_tuple_vec,
        target_hsm_node_hw_component_count_vec,
        parent_hsm_node_hw_component_count_vec,
      )
      .ok_or_else(|| {
        Error::InsufficientResources("No best candidate found".to_string())
      })?;

    work_to_do = scoring::keep_iterating_final_hsm(
      user_defined_hsm_hw_components_count_hashmap,
      &combination_target_parent_hsm_hw_component_summary_hashmap,
    );

    iter += 1;
  }

  tracing::info!("----- FINAL RESULT -----");
  tracing::info!("No candidates found");

  scoring::print_score_table(
    user_defined_hw_component_vec,
    target_hsm_node_hw_component_count_vec,
    &target_hsm_node_score_tuple_vec,
  );

  scoring::print_score_table(
    user_defined_hw_component_vec,
    parent_hsm_node_hw_component_count_vec,
    &parent_hsm_node_score_tuple_vec,
  );

  Ok(nodes_migrated_from_combination_target_parent_hsm)
}

// ── Unpin algorithm ──────────────────────────────────────────────────────────

/// Node selection algorithm for UNPIN mode — merges target and parent, then
/// selects nodes to move back to parent.
pub fn calculate_target_group_unpin(
  user_defined_hsm_hw_components_count_hashmap: &HashMap<String, usize>,
  user_defined_hw_component_vec: &[String],
  combination_target_parent_hsm_node_hw_component_count_vec: &mut NodeHwCountVec,
  hw_component_scarcity_scores_hashmap: &HashMap<String, f64>,
) -> Result<NodeHwCountVec, Error> {
  let mut combination_target_parent_hsm_hw_component_summary_hashmap: HashMap<
    String,
    usize,
  > = scoring::calculate_group_hw_component_summary(
    combination_target_parent_hsm_node_hw_component_count_vec,
  );

  let mut combination_target_parent_hsm_node_score_tuple_vec: Vec<(
    String,
    f64,
  )> = scoring::calculate_group_node_scores_from_final_hsm(
    combination_target_parent_hsm_node_hw_component_count_vec,
    &combination_target_parent_hsm_hw_component_summary_hashmap,
    user_defined_hsm_hw_components_count_hashmap,
    hw_component_scarcity_scores_hashmap,
  );

  let mut nodes_migrated_from_combination_target_parent_hsm: Vec<(
    String,
    HashMap<String, usize>,
  )> = Vec::new();

  let (mut best_candidate, mut best_candidate_counters) =
    scoring::get_best_candidate_in_hsm(
      &mut combination_target_parent_hsm_node_score_tuple_vec,
      combination_target_parent_hsm_node_hw_component_count_vec,
    )
    .ok_or_else(|| {
      Error::InsufficientResources("No best candidate found".to_string())
    })?;

  let mut work_to_do = scoring::keep_iterating_final_hsm(
    user_defined_hsm_hw_components_count_hashmap,
    &combination_target_parent_hsm_hw_component_summary_hashmap,
  );

  let mut iter = 0;

  while work_to_do {
    tracing::info!("----- ITERATION {} -----", iter);

    tracing::info!(
      "HSM group hw component counters: {:?}",
      combination_target_parent_hsm_hw_component_summary_hashmap
    );
    tracing::info!(
      "Final hw component counters the user wants: {:?}",
      user_defined_hsm_hw_components_count_hashmap
    );
    tracing::info!(
      "Best candidate is '{}' with score {} and hw \
       component counters {:?}",
      best_candidate.0,
      combination_target_parent_hsm_node_score_tuple_vec
        .iter()
        .find(|(node, _score)| node.eq(&best_candidate.0))
        .map_or(0.0, |(_, score)| *score),
      best_candidate_counters
    );

    scoring::print_score_table(
      user_defined_hw_component_vec,
      combination_target_parent_hsm_node_hw_component_count_vec,
      &combination_target_parent_hsm_node_score_tuple_vec,
    );

    nodes_migrated_from_combination_target_parent_hsm
      .push((best_candidate.0.clone(), best_candidate_counters.clone()));

    combination_target_parent_hsm_node_hw_component_count_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    if combination_target_parent_hsm_node_hw_component_count_vec.is_empty() {
      break;
    }

    combination_target_parent_hsm_hw_component_summary_hashmap =
      scoring::calculate_group_hw_component_summary(
        combination_target_parent_hsm_node_hw_component_count_vec,
      );

    combination_target_parent_hsm_node_score_tuple_vec
      .retain(|(node, _)| !node.eq(&best_candidate.0));

    let mut target_hsm_node_score_tuple_vec: Vec<(String, f64)> =
      scoring::calculate_group_node_scores_from_final_hsm(
        combination_target_parent_hsm_node_hw_component_count_vec,
        &combination_target_parent_hsm_hw_component_summary_hashmap,
        user_defined_hsm_hw_components_count_hashmap,
        hw_component_scarcity_scores_hashmap,
      );

    (best_candidate, best_candidate_counters) =
      scoring::get_best_candidate_in_hsm(
        &mut target_hsm_node_score_tuple_vec,
        combination_target_parent_hsm_node_hw_component_count_vec,
      )
      .ok_or_else(|| {
        Error::InsufficientResources("No best candidate found".to_string())
      })?;

    work_to_do = scoring::keep_iterating_final_hsm(
      user_defined_hsm_hw_components_count_hashmap,
      &combination_target_parent_hsm_hw_component_summary_hashmap,
    );

    iter += 1;
  }

  tracing::info!("----- FINAL RESULT -----");
  tracing::info!("No candidates found");

  scoring::print_score_table(
    user_defined_hw_component_vec,
    combination_target_parent_hsm_node_hw_component_count_vec,
    &combination_target_parent_hsm_node_score_tuple_vec,
  );

  Ok(nodes_migrated_from_combination_target_parent_hsm)
}

// ── apply_hw_configuration support ───────────────────────────────────────────

/// Parse user pattern `"a100:4:epyc:10"` into hw component names and a hashmap
/// of `{component -> usize count}`.
pub fn parse_hw_pattern_usize(
  target_hsm_group_name: &str,
  pattern: &str,
) -> Result<(Vec<String>, HashMap<String, usize>), Error> {
  let pattern = format!("{target_hsm_group_name}:{pattern}");
  tracing::info!("pattern: {}", pattern);

  let pattern_lowercase = pattern.to_lowercase();

  let (_group_name, pattern_hw_component) =
    pattern_lowercase.split_once(':').ok_or_else(|| {
      Error::InvalidPattern(
        "Invalid pattern format: \
         expected 'group:component:count'"
          .to_string(),
      )
    })?;

  let pattern_element_vec: Vec<&str> =
    pattern_hw_component.split(':').collect();

  if !pattern_element_vec.len().is_multiple_of(2) {
    return Err(Error::InvalidPattern(
      "Error in pattern: odd number of elements. \
       Expected pairs of <hw component>:<count>. \
       eg a100:4:epyc:10:instinct:8"
        .to_string(),
    ));
  }

  let mut hw_component_count: HashMap<String, usize> = HashMap::new();

  for chunk in pattern_element_vec.chunks_exact(2) {
    if let Ok(count) = chunk[1].parse::<usize>() {
      hw_component_count.insert(chunk[0].to_string(), count);
    } else {
      return Err(Error::InvalidPattern(
        "Error in pattern. Please make sure to follow \
         <hsm name>:<hw component>:<counter>:... \
         eg <tasna>:a100:4:epyc:10:instinct:8"
          .to_string(),
      ));
    }
  }

  tracing::info!(
    "User defined hw components with counters: {:?}",
    hw_component_count
  );

  let mut hw_component_vec: Vec<String> =
    hw_component_count.keys().cloned().collect();
  hw_component_vec.sort();

  Ok((hw_component_vec, hw_component_count))
}

/// Ensure the target HSM group exists, creating it if `create_target_hsm_group` is set.
pub async fn ensure_target_group_exists(
  infra: &InfraContext<'_>,
  shasta_token: &str,
  target_hsm_group_name: &str,
  dryrun: bool,
  create_target_hsm_group: bool,
) -> Result<(), Error> {
  if infra
    .backend
    .get_group(shasta_token, target_hsm_group_name)
    .await
    .is_ok()
  {
    tracing::debug!(
      "Target HSM group '{}' exists, good.",
      target_hsm_group_name
    );
    return Ok(());
  }
  if !create_target_hsm_group {
    return Err(Error::NotFound(format!(
      "Target HSM group '{target_hsm_group_name}' does not exist, \
       but the option to create the group was \
       NOT specified, cannot continue.",
    )));
  }
  tracing::info!(
    "Target HSM group '{}' does not exist, \
     but the option to create the group has \
     been selected, creating it now.",
    target_hsm_group_name
  );
  if dryrun {
    return Err(Error::BadRequest(
      "Dryrun selected, cannot create the \
       new group and continue."
        .to_string(),
    ));
  }
  let group = Group {
    label: target_hsm_group_name.to_string(),
    description: None,
    tags: None,
    members: None,
    exclusive_group: Some("false".to_string()),
  };
  infra
    .backend
    .add_group(shasta_token, group)
    .await
    .map_err(|e| {
      Error::BadRequest(format!("Unable to create new target HSM group: {e}"))
    })?;
  Ok(())
}

/// Validate that combined target+parent resources can fulfil the user request.
pub fn validate_resource_sufficiency(
  target_hw: &[(String, HashMap<String, usize>)],
  parent_hw: &[(String, HashMap<String, usize>)],
  requested: &HashMap<String, usize>,
) -> Result<(), Error> {
  let mut combined = parent_hw.to_vec();
  for elem in target_hw {
    if !parent_hw.iter().any(|(xname, _)| xname.eq(&elem.0)) {
      combined.push(elem.clone());
    }
  }

  let combined_summary =
    scoring::calculate_group_hw_component_summary(&combined);

  for (hw_component, qty) in requested {
    if combined_summary
      .get(hw_component)
      .is_none_or(|value| value < qty)
    {
      return Err(Error::InsufficientResources(
        "There are not enough resources \
         to fulfil user request."
          .to_string(),
      ));
    }
  }

  Ok(())
}

/// Inputs to [`apply_group_updates`] bundled to avoid a ten-arg
/// positional call. Pairs the old and new membership lists per
/// group; the function uses the pair to decide whether the parent
/// will be left empty by the move.
pub struct GroupUpdate<'a> {
  /// Destination group label.
  pub target_group: &'a str,
  /// Source group label.
  pub parent_group: &'a str,
  /// Membership of the target group before the update.
  pub old_target_members: &'a [String],
  /// Membership of the parent group before the update.
  pub old_parent_members: &'a [String],
  /// Membership of the target group after the update.
  pub new_target_members: &'a [String],
  /// Membership of the parent group after the update.
  pub new_parent_members: &'a [String],
  /// When `true`, skip every backend mutation but still walk the plan.
  pub dryrun: bool,
  /// When `true` and the parent group has no remaining members after
  /// the update, delete the parent group too.
  pub delete_empty_parent: bool,
}

/// Apply group membership updates to both target and parent HSM groups.
pub async fn apply_group_updates(
  infra: &InfraContext<'_>,
  shasta_token: &str,
  u: GroupUpdate<'_>,
) -> Result<(), Error> {
  tracing::info!("Updating target HSM group '{}' members", u.target_group);
  if u.dryrun {
    tracing::info!(
      "Dry run enabled, not modifying the \
       HSM groups on the system."
    );
  } else {
    let target_remove_ref: Vec<&str> =
      u.old_target_members.iter().map(String::as_str).collect();
    let target_add_ref: Vec<&str> =
      u.new_target_members.iter().map(String::as_str).collect();
    infra
      .backend
      .update_group_members(
        shasta_token,
        u.target_group,
        &target_remove_ref,
        &target_add_ref,
      )
      .await
      .map_err(|e| {
        Error::BadRequest(format!(
          "Failed to update target HSM group members: {e}"
        ))
      })?;
  }

  tracing::info!("Updating parent HSM group '{}' members", u.parent_group);
  if u.dryrun {
    tracing::info!(
      "Dry run enabled, not modifying the \
       HSM groups on the system."
    );
  } else {
    let parent_will_be_empty =
      u.old_target_members.len() == u.old_parent_members.len();
    let parent_remove_ref: Vec<&str> =
      u.old_parent_members.iter().map(String::as_str).collect();
    let parent_add_ref: Vec<&str> =
      u.new_parent_members.iter().map(String::as_str).collect();
    infra
      .backend
      .update_group_members(
        shasta_token,
        u.parent_group,
        &parent_remove_ref,
        &parent_add_ref,
      )
      .await
      .map_err(|e| {
        Error::BadRequest(format!(
          "Failed to update parent HSM group members: {e}"
        ))
      })?;

    if parent_will_be_empty && u.delete_empty_parent {
      tracing::info!(
        "Parent HSM group '{}' is now empty and \
         the option to delete empty groups has \
         been selected, removing it.",
        u.parent_group
      );
      match infra
        .backend
        .delete_group(shasta_token, u.parent_group)
        .await
      {
        Ok(_) => tracing::info!("HSM group removed successfully."),
        Err(e) => tracing::debug!(
          "Error removing the HSM group. \
           This always fails, ignore please. \
           Reported: {}",
          e
        ),
      }
    } else if parent_will_be_empty {
      tracing::debug!(
        "Parent HSM group '{}' is now empty and \
         the option to delete empty groups has \
         NOT been selected, will not remove it.",
        u.parent_group
      );
    }
  }

  Ok(())
}