summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/Node.java
blob: d9e0e4292e852af7d6d99f08d23bb42fb320855c (plain) (blame)
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.ClusterMembership;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Flavor;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.provision.lb.LoadBalancers;
import com.yahoo.vespa.hosted.provision.node.Agent;
import com.yahoo.vespa.hosted.provision.node.Allocation;
import com.yahoo.vespa.hosted.provision.node.Generation;
import com.yahoo.vespa.hosted.provision.node.History;
import com.yahoo.vespa.hosted.provision.node.IP;
import com.yahoo.vespa.hosted.provision.node.NodeAcl;
import com.yahoo.vespa.hosted.provision.node.Reports;
import com.yahoo.vespa.hosted.provision.node.Status;
import com.yahoo.vespa.hosted.provision.node.TrustStoreItem;

import java.time.Instant;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * A node in the node repository. The identity of a node is given by its id.
 * The classes making up the node model are found in the node package.
 * This (and hence all classes referenced from it) is immutable.
 *
 * @author bratseth
 * @author mpolden
 */
public final class Node implements Nodelike {

    private final String hostname;
    private final IP.Config ipConfig;
    private final String id;
    private final Optional<String> parentHostname;
    private final Flavor flavor;
    private final Status status;
    private final State state;
    private final NodeType type;
    private final Reports reports;
    private final Optional<String> modelName;
    private final Optional<TenantName> reservedTo;
    private final Optional<ApplicationId> exclusiveToApplicationId;
    private final Optional<ClusterSpec.Type> exclusiveToClusterType;
    private final Optional<String> switchHostname;
    private final List<TrustStoreItem> trustStoreItems;
    private final Optional<CloudAccount> cloudAccount;

    /** Record of the last event of each type happening to this node */
    private final History history;

    /** The current allocation of this node, if any */
    private final Optional<Allocation> allocation;

    /** Creates a node builder in the initial state (reserved) */
    public static Node.Builder reserve(Set<String> ipAddresses, String hostname, String parentHostname, NodeResources resources, NodeType type) {
        return new Node.Builder(UUID.randomUUID().toString(), hostname, new Flavor(resources), State.reserved, type)
                .ipConfig(IP.Config.ofEmptyPool(ipAddresses))
                .parentHostname(parentHostname);
    }

    /** Creates a node builder in the initial state (provisioned) */
    public static Node.Builder create(String id, IP.Config ipConfig, String hostname, Flavor flavor, NodeType type) {
        return new Node.Builder(id, hostname, flavor, State.provisioned, type).ipConfig(ipConfig);
    }

    /** Creates a node builder */
    public static Node.Builder create(String id, String hostname, Flavor flavor, Node.State state, NodeType type) {
        return new Node.Builder(id, hostname, flavor, state, type);
    }

    /** DO NOT USE: public for serialization purposes. See {@code create} helper methods. */
    public Node(String id, IP.Config ipConfig, String hostname, Optional<String> parentHostname,
                Flavor flavor, Status status, State state, Optional<Allocation> allocation, History history, NodeType type,
                Reports reports, Optional<String> modelName, Optional<TenantName> reservedTo,
                Optional<ApplicationId> exclusiveToApplicationId, Optional<ClusterSpec.Type> exclusiveToClusterType,
                Optional<String> switchHostname, List<TrustStoreItem> trustStoreItems,
                Optional<CloudAccount> cloudAccount) {
        this.id = Objects.requireNonNull(id, "A node must have an ID");
        this.hostname = requireNonEmptyString(hostname, "A node must have a hostname");
        this.ipConfig = Objects.requireNonNull(ipConfig, "A node must a have an IP config");
        this.parentHostname = requireNonEmptyString(parentHostname, "A parent host name must be a proper value");
        this.flavor = Objects.requireNonNull(flavor, "A node must have a flavor");
        this.status = Objects.requireNonNull(status, "A node must have a status");
        this.state = Objects.requireNonNull(state, "A null node state is not permitted");
        this.allocation = Objects.requireNonNull(allocation, "A null node allocation is not permitted");
        this.history = Objects.requireNonNull(history, "A null node history is not permitted");
        this.type = Objects.requireNonNull(type, "A null node type is not permitted");
        this.reports = Objects.requireNonNull(reports, "A null reports is not permitted");
        this.modelName = Objects.requireNonNull(modelName, "A null modelName is not permitted");
        this.reservedTo = Objects.requireNonNull(reservedTo, "reservedTo cannot be null");
        this.exclusiveToApplicationId = Objects.requireNonNull(exclusiveToApplicationId, "exclusiveToApplicationId cannot be null");
        this.exclusiveToClusterType = Objects.requireNonNull(exclusiveToClusterType, "exclusiveToClusterType cannot be null");
        this.switchHostname = requireNonEmptyString(switchHostname, "switchHostname cannot be null");
        this.trustStoreItems = Objects.requireNonNull(trustStoreItems).stream().distinct().collect(Collectors.toUnmodifiableList());
        this.cloudAccount = Objects.requireNonNull(cloudAccount);

        if (state == State.active)
            requireNonEmpty(ipConfig.primary(), "Active node " + hostname + " must have at least one valid IP address");

        if (state == State.ready && type.isHost()) {
            requireNonEmpty(ipConfig.primary(), "A " + type + " must have at least one primary IP address in state " + state);
            requireNonEmpty(ipConfig.pool().ipSet(), "A " + type + " must have a non-empty IP address pool in state " + state);
        }

        if (parentHostname.isPresent()) {
            if (!ipConfig.pool().ipSet().isEmpty()) throw new IllegalArgumentException("A child node cannot have an IP address pool");
            if (modelName.isPresent()) throw new IllegalArgumentException("A child node cannot have model name set");
            if (switchHostname.isPresent()) throw new IllegalArgumentException("A child node cannot have switch hostname set");
            if (cloudAccount().isPresent()) throw new IllegalArgumentException("A child node cannot have cloud account set");
        }

        if (type != NodeType.host && reservedTo.isPresent())
            throw new IllegalArgumentException("Only tenant hosts can be reserved to a tenant");

        if (type != NodeType.host && exclusiveToApplicationId.isPresent())
            throw new IllegalArgumentException("Only tenant hosts can be exclusive to an application");

        if (type != NodeType.host && exclusiveToClusterType.isPresent())
            throw new IllegalArgumentException("Only tenant hosts can be exclusive to a cluster type");
    }

    /** Returns the IP config of this node */
    public IP.Config ipConfig() { return ipConfig; }

    /** Returns the host name of this node */
    public String hostname() { return hostname; }

    /**
     * Unique identifier for this node. Code should not depend on this as its main purpose is to aid human operators in
     * mapping a node to the corresponding cloud instance. No particular format is enforced.
     *
     * Formats used vary between the underlying cloud providers:
     *
     * - OpenStack: UUID
     * - AWS: Instance ID
     * - Linux containers: UUID
     */
    public String id() { return id; }

    @Override
    public Optional<String> parentHostname() { return parentHostname; }

    public boolean hasParent(String hostname) {
        return parentHostname.isPresent() && parentHostname.get().equals(hostname);
    }

    @Override
    public NodeResources resources() { return flavor.resources(); }

    /** Returns the flavor of this node */
    public Flavor flavor() { return flavor; }

    /** Returns the known information about the node's ephemeral status */
    public Status status() { return status; }

    /** Returns the current state of this node (in the node state machine) */
    public State state() { return state; }

    @Override
    public NodeType type() { return type; }

    /** Returns the current allocation of this, if any */
    public Optional<Allocation> allocation() { return allocation; }

    /** Returns the current allocation when it must exist, or throw exception there is not allocation. */
    private Allocation requireAllocation(String message) {
        final Optional<Allocation> allocation = this.allocation;
        if ( ! allocation.isPresent())
            throw new IllegalStateException(message + " for  " + hostname() + ": The node is unallocated");

        return allocation.get();
    }

    /** Returns a history of the last events happening to this node */
    public History history() { return history; }

    /** Returns all the reports on this node. */
    public Reports reports() { return reports; }

    /** Returns the hardware model of this node, if any */
    public Optional<String> modelName() { return modelName; }

    /**
     * Returns the tenant this node is reserved to, if any. Only hosts can be reserved to a tenant.
     * If this is set, resources on this host cannot be allocated to any other tenant
     */
    public Optional<TenantName> reservedTo() { return reservedTo; }

    /**
     * Returns the application this host is exclusive to, if any. Only tenant hosts can be exclusive to an application.
     * If this is set, resources on this host cannot be allocated to any other application. This is set during
     * provisioning and applies for the entire lifetime of the host
     */
    public Optional<ApplicationId> exclusiveToApplicationId() { return exclusiveToApplicationId; }

    /**
     * Returns the cluster type this host is exclusive to, if any. Only tenant hosts can be exclusive to a cluster type.
     * If this is set, resources on this host cannot be allocated to any other cluster type. This is set during
     * provisioning and applies for the entire lifetime of the host
     */
    public Optional<ClusterSpec.Type> exclusiveToClusterType() { return exclusiveToClusterType; }

    /** Returns the hostname of the switch this node is connected to, if any */
    public Optional<String> switchHostname() {
        return switchHostname;
    }

    /** Returns the trusted certificates for this host if any. */
    public List<TrustStoreItem> trustedCertificates() {
        return trustStoreItems;
    }

    /** Returns the cloud account of this host. This is empty if the host is in the zone's default account */
    public Optional<CloudAccount> cloudAccount() {
        return cloudAccount;
    }

    /**
     * Returns a copy of this where wantToFail is set to true and history is updated to reflect this.
     */
    public Node withWantToFail(boolean wantToFail, Agent agent, Instant at) {
        Node node = this.with(status.withWantToFail(wantToFail));
        if (wantToFail)
            node = node.with(history.with(new History.Event(History.Event.Type.wantToFail, agent, at)));
        return node;

    }

    /**
     * Returns a copy of this node with wantToRetire and wantToDeprovision set to the given values and updated history.
     *
     * If both given wantToRetire and wantToDeprovision are equal to the current values, the method is no-op.
     */
    public Node withWantToRetire(boolean wantToRetire, boolean wantToDeprovision, Agent agent, Instant at) {
        return withWantToRetire(wantToRetire, wantToDeprovision, false, agent, at);
    }

    /**
     * Returns a copy of this node with wantToRetire, wantToDeprovision and wantToRebuild set to the given values
     * and updated history.
     *
     * If all given values are equal to the current ones, the method is no-op.
     */
    public Node withWantToRetire(boolean wantToRetire, boolean wantToDeprovision, boolean wantToRebuild, Agent agent, Instant at) {
        if (wantToRetire == status.wantToRetire() &&
            wantToDeprovision == status.wantToDeprovision() &&
            wantToRebuild == status.wantToRebuild()) return this;
        if (wantToRebuild && !wantToRetire && resources().storageType() != NodeResources.StorageType.remote) {
            throw new IllegalArgumentException("Cannot rebuild " + this + " without retiring because storage is " +
                                               resources().storageType());
        }
        Node node = this.with(status.withWantToRetire(wantToRetire, wantToDeprovision, wantToRebuild));
        if (wantToRetire)
            node = node.with(history.with(new History.Event(History.Event.Type.wantToRetire, agent, at)));
        return node;
    }

    public Node withWantToRetire(boolean wantToRetire, Agent agent, Instant at) {
        return withWantToRetire(wantToRetire, status.wantToDeprovision(), agent, at);
    }

    /** Returns a copy of this node with preferToRetire set to given value and updated history */
    public Node withPreferToRetire(boolean preferToRetire, Agent agent, Instant at) {
        if (preferToRetire == status.preferToRetire()) return this;
        Node node = this.with(status.withPreferToRetire(preferToRetire));
        if (preferToRetire) {
            node = node.with(history.with(new History.Event(History.Event.Type.preferToRetire, agent, at)));
        }
        return node;
    }

    /**
     * Returns a copy of this node which is retired.
     * If the node was already retired it is returned as-is.
     */
    public Node retire(Agent agent, Instant retiredAt) {
        Allocation allocation = requireAllocation("Cannot retire");
        if (allocation.membership().retired()) return this;
        return with(allocation.retire())
                .with(history.with(new History.Event(History.Event.Type.retired, agent, retiredAt)));
    }

    /** Returns a copy of this node which is retired */
    public Node retire(Instant retiredAt) {
        if (status.wantToRetire() || status.preferToRetire())
            return retire(Agent.system, retiredAt);
        else
            return retire(Agent.application, retiredAt);
    }

    /** Returns a copy of this node which is not retired */
    public Node unretire() {
        return with(requireAllocation("Cannot unretire").unretire());
    }

    /** Returns a copy of this with removable set to the given value */
    public Node removable(boolean removable) {
        return with(requireAllocation("Cannot set removable").removable(removable, false));
    }

    /** Returns a copy of this with the restart generation set to generation */
    public Node withRestart(Generation generation) {
        Allocation allocation = requireAllocation("Cannot set restart generation");
        return with(allocation.withRestart(generation));
    }

    /** Returns a node with the status assigned to the given value */
    public Node with(Status status) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state, allocation, history, type,
                        reports, modelName, reservedTo, exclusiveToApplicationId, exclusiveToClusterType, switchHostname,
                        trustStoreItems, cloudAccount);
    }

    /** Returns a node with the type assigned to the given value */
    public Node with(NodeType type) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state, allocation, history, type,
                        reports, modelName, reservedTo, exclusiveToApplicationId, exclusiveToClusterType, switchHostname,
                        trustStoreItems, cloudAccount);
    }

    /** Returns a node with the flavor assigned to the given value */
    public Node with(Flavor flavor, Agent agent, Instant instant) {
        if (flavor.equals(this.flavor)) return this;
        History updateHistory = history.with(new History.Event(History.Event.Type.resized, agent, instant));
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state, allocation, updateHistory, type,
                        reports, modelName, reservedTo, exclusiveToApplicationId, exclusiveToClusterType, switchHostname,
                        trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this with the reboot generation set to generation */
    public Node withReboot(Generation generation) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status.withReboot(generation), state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this with given id set */
    public Node withId(String id) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this with model name set to given value */
    public Node withModelName(String modelName) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, Optional.of(modelName), reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this with model name cleared */
    public Node withoutModelName() {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, Optional.empty(), reservedTo,
                        exclusiveToApplicationId, exclusiveToClusterType, switchHostname, trustStoreItems,
                        cloudAccount);
    }

    /** Returns a copy of this with a history record saying it was detected to be down at this instant */
    public Node downAt(Instant instant, Agent agent) {
        return with(history.with(new History.Event(History.Event.Type.down, agent, instant)));
    }

    /** Returns a copy of this with any history record saying it has been detected down removed */
    public Node upAt(Instant instant, Agent agent) {
        return with(history.with(new History.Event(History.Event.Type.up, agent, instant)));
    }

    /** Returns whether this node is down, according to its recorded 'down' and 'up' events */
    public boolean isDown() {
        Optional<Instant> downAt = history().event(History.Event.Type.down).map(History.Event::at);
        if (downAt.isEmpty()) return false;

        return !history().hasEventAfter(History.Event.Type.up, downAt.get());
    }

    /** Returns a copy of this with allocation set as specified. <code>node.state</code> is *not* changed. */
    public Node allocate(ApplicationId owner, ClusterMembership membership, NodeResources requestedResources, Instant at) {
        return this.with(new Allocation(owner, membership, requestedResources, new Generation(0, 0), false))
                   .with(history.with(new History.Event(History.Event.Type.reserved, Agent.application, at)));
    }

    /**
     * Returns a copy of this node with the allocation assigned to the given allocation.
     * Do not use this to allocate a node.
     */
    public Node with(Allocation allocation) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        Optional.of(allocation), history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a new Node without an allocation. */
    public Node withoutAllocation() {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        Optional.empty(), history, type, reports, modelName, reservedTo,
                        exclusiveToApplicationId, exclusiveToClusterType, switchHostname, trustStoreItems,
                        cloudAccount);
    }


    /** Returns a copy of this node with IP config set to the given value. */
    public Node with(IP.Config ipConfig) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this node with the parent hostname assigned to the given value. */
    public Node withParentHostname(String parentHostname) {
        return new Node(id, ipConfig, hostname, Optional.of(parentHostname), flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    public Node withReservedTo(TenantName tenant) {
        if (type != NodeType.host)
            throw new IllegalArgumentException("Only host nodes can be reserved, " + hostname + " has type " + type);
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, Optional.of(tenant), exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this node which is not reserved to a tenant */
    public Node withoutReservedTo() {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, Optional.empty(),
                        exclusiveToApplicationId, exclusiveToClusterType, switchHostname, trustStoreItems,
                        cloudAccount);
    }

    public Node withExclusiveToApplicationId(ApplicationId exclusiveTo) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, Optional.ofNullable(exclusiveTo),
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    public Node withExclusiveToClusterType(ClusterSpec.Type exclusiveTo) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        Optional.ofNullable(exclusiveTo), switchHostname, trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this node with switch hostname set to given value */
    public Node withSwitchHostname(String switchHostname) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, Optional.ofNullable(switchHostname), trustStoreItems, cloudAccount);
    }

    /** Returns a copy of this node with switch hostname unset */
    public Node withoutSwitchHostname() {
        return withSwitchHostname(null);
    }

    /** Returns a copy of this node with the current reboot generation set to the given number at the given instant */
    public Node withCurrentRebootGeneration(long generation, Instant instant) {
        if (generation == status.reboot().current()) {
            return this; // No change
        }
        if (generation < status.reboot().current()) {
            throw new IllegalArgumentException("Cannot set reboot generation to " + generation +
                                               ": lower than current generation: " + status.reboot().current());
        }
        Status newStatus = status().withReboot(status().reboot().withCurrent(generation));
        History newHistory = history.with(new History.Event(History.Event.Type.rebooted, Agent.system, instant));
        return this.with(newStatus).with(newHistory);
    }

    /** Returns a copy of this node with the current OS version set to the given version at the given instant */
    public Node withCurrentOsVersion(Version version, Instant instant) {
        Optional<Version> newVersion = Optional.of(version);
        if (status.osVersion().current().equals(newVersion)) return this; // No change

        History newHistory = history();
        // Only update history if version was non-empty and changed to a different version
        if (status.osVersion().current().isPresent() && !status.osVersion().current().equals(newVersion)) {
            newHistory = history.with(new History.Event(History.Event.Type.osUpgraded, Agent.system, instant));
        }
        Status newStatus = status.withOsVersion(status.osVersion().withCurrent(newVersion));
        return this.with(newStatus).with(newHistory);
    }

    /** Returns a copy of this node with wanted OS version set to given version */
    public Node withWantedOsVersion(Optional<Version> version) {
        if (status.osVersion().wanted().equals(version)) return this;
        return with(status.withOsVersion(status.osVersion().withWanted(version)));
    }

    /** Returns a copy of this node with firmware verified at the given instant */
    public Node withFirmwareVerifiedAt(Instant instant) {
        var newStatus = status.withFirmwareVerifiedAt(instant);
        var newHistory = history.with(new History.Event(History.Event.Type.firmwareVerified, Agent.system, instant));
        return this.with(newStatus).with(newHistory);
    }

    /** Returns a copy of this node with the given history. */
    public Node with(History history) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    public Node with(Reports reports) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    public Node with(List<TrustStoreItem> trustStoreItems) {
        return new Node(id, ipConfig, hostname, parentHostname, flavor, status, state,
                        allocation, history, type, reports, modelName, reservedTo, exclusiveToApplicationId,
                        exclusiveToClusterType, switchHostname, trustStoreItems, cloudAccount);
    }

    private static Optional<String> requireNonEmptyString(Optional<String> value, String message) {
        Objects.requireNonNull(value, message);
        value.ifPresent(v -> requireNonEmptyString(v, message));
        return value;
    }

    private static String requireNonEmptyString(String value, String message) {
        Objects.requireNonNull(value, message);
        if (value.trim().isEmpty())
            throw new IllegalArgumentException(message + ", but was '" + value + "'");
        return value;
    }

    private static Set<String> requireNonEmpty(Set<String> values, String message) {
        if (values == null || values.isEmpty())
            throw new IllegalArgumentException(message);
        return values;
    }

    /** Computes the allocation skew of a host node */
    public static double skew(NodeResources totalHostCapacity, NodeResources freeHostCapacity) {
        NodeResources all = totalHostCapacity.justNumbers();
        NodeResources allocated = all.subtract(freeHostCapacity.justNumbers());

        return new Mean(allocated.vcpu() / all.vcpu(),
                        allocated.memoryGb() / all.memoryGb(),
                        allocated.diskGb() / all.diskGb())
                       .deviation();
    }

    /** Returns the ACL for the node (trusted nodes, networks and ports) */
    public NodeAcl acl(NodeList allNodes, LoadBalancers loadBalancers) {
        return NodeAcl.from(this, allNodes, loadBalancers);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return hostname.equals(node.hostname);
    }

    @Override
    public int hashCode() {
        return Objects.hash(hostname);
    }

    @Override
    public String toString() {
        return state +
               ( parentHostname.isPresent() ? " child node " : " host " ) +
               hostname +
               ( allocation.isPresent() ? " " + allocation.get() : "");
    }

    public enum State {

        /** This node has been requested, but is not yet ready for use */
        provisioned,

        /** This node is free and ready for use */
        ready,

        /** This node has been reserved by an application but is not yet used by it */
        reserved,

        /** This node is in active use by an application */
        active,

        /** This node has been used by an application, is still allocated to it and retains the data needed for its allocated role */
        inactive,

        /** This node is not allocated to an application but may contain data which must be cleaned before it is ready */
        dirty,

        /** This node has failed and must be repaired or removed. The node retains any allocation data for diagnosis. */
        failed,

        /**
         * This node should not currently be used.
         * This state follows the same rules as failed except that it will never be automatically moved out of
         * this state.
         */
        parked,

        /** This host has previously been in use but is now removed. */
        deprovisioned,

        /** This host is currently undergoing repair. */
        breakfixed;

        /** Returns whether this is a state where the node is assigned to an application */
        public boolean isAllocated() {
            return allocatedStates().contains(this);
        }

        public static Set<State> allocatedStates() {
            return EnumSet.of(reserved, active, inactive, dirty, failed, parked);
        }

    }

    /** The mean and mean deviation (squared difference) of a bunch of numbers */
    private static class Mean {

        private final double mean;
        private final double deviation;

        private Mean(double ... numbers) {
            mean = Arrays.stream(numbers).sum() / numbers.length;
            deviation = Arrays.stream(numbers).map(n -> Math.pow(mean - n, 2)).sum() / numbers.length;
        }

        public double deviation() {  return deviation; }

    }

    public static class Builder {

        private final String id;
        private final String hostname;
        private final Flavor flavor;
        private final State state;
        private final NodeType type;

        private String parentHostname;
        private String modelName;
        private TenantName reservedTo;
        private ApplicationId exclusiveToApplicationId;
        private ClusterSpec.Type exclusiveToClusterType;
        private String switchHostname;
        private Allocation allocation;
        private IP.Config ipConfig;
        private Status status;
        private Reports reports;
        private History history;
        private List<TrustStoreItem> trustStoreItems;
        private CloudAccount cloudAccount;

        private Builder(String id, String hostname, Flavor flavor, State state, NodeType type) {
            this.id = id;
            this.hostname = hostname;
            this.flavor = flavor;
            this.state = state;
            this.type = type;
        }

        public Builder parentHostname(String parentHostname) {
            this.parentHostname = parentHostname;
            return this;
        }

        public Builder modelName(String modelName) {
            this.modelName = modelName;
            return this;
        }

        public Builder reservedTo(TenantName reservedTo) {
            this.reservedTo = reservedTo;
            return this;
        }

        public Builder exclusiveToApplicationId(ApplicationId exclusiveTo) {
            this.exclusiveToApplicationId = exclusiveTo;
            return this;
        }

        public Builder exclusiveToClusterType(ClusterSpec.Type exclusiveTo) {
            this.exclusiveToClusterType = exclusiveTo;
            return this;
        }

        public Builder switchHostname(String switchHostname) {
            this.switchHostname = switchHostname;
            return this;
        }

        public Builder allocation(Allocation allocation) {
            this.allocation = allocation;
            return this;
        }

        public Builder ipConfig(IP.Config ipConfig) {
            this.ipConfig = ipConfig;
            return this;
        }

        public Builder ipConfigWithEmptyPool(Set<String> primary) {
            this.ipConfig = IP.Config.ofEmptyPool(primary);
            return this;
        }

        public Builder status(Status status) {
            this.status = status;
            return this;
        }

        public Builder reports(Reports reports) {
            this.reports = reports;
            return this;
        }

        public Builder history(History history) {
            this.history = history;
            return this;
        }

        public Builder trustedCertificates(List<TrustStoreItem> trustStoreItems) {
            this.trustStoreItems = trustStoreItems;
            return this;
        }

        public Builder cloudAccount(CloudAccount cloudAccount) {
            this.cloudAccount = cloudAccount;
            return this;
        }

        public Node build() {
            return new Node(id, Optional.ofNullable(ipConfig).orElse(IP.Config.EMPTY), hostname, Optional.ofNullable(parentHostname),
                            flavor, Optional.ofNullable(status).orElseGet(Status::initial), state, Optional.ofNullable(allocation),
                            Optional.ofNullable(history).orElseGet(History::empty), type, Optional.ofNullable(reports).orElseGet(Reports::new),
                            Optional.ofNullable(modelName), Optional.ofNullable(reservedTo), Optional.ofNullable(exclusiveToApplicationId),
                            Optional.ofNullable(exclusiveToClusterType), Optional.ofNullable(switchHostname),
                            Optional.ofNullable(trustStoreItems).orElseGet(List::of),
                            Optional.ofNullable(cloudAccount));
        }

    }

}