Skip to content

Commit ad50c0c

Browse files
authored
Make public enums into structs or hide them (part 1) (#1025)
* DowningSettings * OpLog * Fix warnings * Cluster.Event
1 parent 31e4247 commit ad50c0c

13 files changed

+83
-32
lines changed

Sources/DistributedActors/Cluster/Cluster+Event.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extension Cluster {
2424
case membershipChange(MembershipChange)
2525
case reachabilityChange(ReachabilityChange)
2626
case leadershipChange(LeadershipChange)
27+
case _PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE
2728
}
2829
}
2930

Sources/DistributedActors/Cluster/Cluster+Membership.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Actors open source project
44
//
5-
// Copyright (c) 2018-2019 Apple Inc. and the Swift Distributed Actors project authors
5+
// Copyright (c) 2018-2022 Apple Inc. and the Swift Distributed Actors project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -604,6 +604,9 @@ extension Cluster.Membership {
604604

605605
case .reachabilityChange(let change):
606606
_ = self.applyReachabilityChange(change)
607+
608+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
609+
() // do nothing
607610
}
608611
}
609612
}

Sources/DistributedActors/Cluster/ClusterShellState.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ extension ClusterShellState {
456456
changeWasApplied = changeWasApplied || directive.applied
457457
// actions we'll calculate below, once
458458
}
459+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
460+
self.log.error("Received Cluster.Event [\(event)]. This should not happen, please file an issue.")
461+
changeWasApplied = false
459462
}
460463

461464
guard changeWasApplied else {

Sources/DistributedActors/Cluster/DistributedNodeDeathWatcher.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ internal actor DistributedNodeDeathWatcher {
6666
}
6767
case .leadershipChange, .reachabilityChange:
6868
break // ignore those, they don't affect downing
69+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
70+
self.log.error("Received Cluster.Event [\(event)]. This should not happen, please file an issue.")
6971
}
7072
}
7173
}

Sources/DistributedActors/Cluster/Downing/DowningSettings.swift

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,51 @@ import Logging
1717
// ==== ----------------------------------------------------------------------------------------------------------------
1818
// MARK: DowningStrategySettings
1919

20-
public enum DowningStrategySettings {
21-
case none
22-
case timeout(TimeoutBasedDowningStrategySettings)
20+
public struct DowningStrategySettings {
21+
private enum _DowningStrategySettings {
22+
case none
23+
case timeout(TimeoutBasedDowningStrategySettings)
24+
}
25+
26+
private let underlying: _DowningStrategySettings
27+
28+
private init(_ underlying: _DowningStrategySettings) {
29+
self.underlying = underlying
30+
}
2331

2432
func make(_ clusterSystemSettings: ClusterSystemSettings) -> DowningStrategy? {
25-
switch self {
33+
switch self.underlying {
2634
case .none:
2735
return nil
2836
case .timeout(let settings):
2937
return TimeoutBasedDowningStrategy(settings, selfNode: clusterSystemSettings.uniqueBindNode)
3038
}
3139
}
40+
41+
public static let none: DowningStrategySettings = .init(.none)
42+
43+
public static func timeout(_ settings: TimeoutBasedDowningStrategySettings) -> DowningStrategySettings {
44+
.init(.timeout(settings))
45+
}
3246
}
3347

3448
// ==== ----------------------------------------------------------------------------------------------------------------
3549
// MARK: OnDownActionStrategySettings
3650

37-
public enum OnDownActionStrategySettings {
38-
/// Take no (automatic) action upon noticing that this member is marked as [.down].
39-
///
40-
/// When using this mode you should take special care to implement some form of shutting down of this node (!).
41-
/// As a ``Cluster/MemberStatus/down`` node is effectively useless for the rest of the cluster -- i.e. other
42-
/// members MUST refuse communication with this down node.
43-
case none
44-
/// Upon noticing that this member is marked as [.down], initiate a shutdown.
45-
case gracefulShutdown(delay: Duration)
51+
public struct OnDownActionStrategySettings {
52+
private enum _OnDownActionStrategySettings {
53+
case none
54+
case gracefulShutdown(delay: Duration)
55+
}
56+
57+
private let underlying: _OnDownActionStrategySettings
58+
59+
private init(_ underlying: _OnDownActionStrategySettings) {
60+
self.underlying = underlying
61+
}
4662

4763
func make() -> (ClusterSystem) throws -> Void {
48-
switch self {
64+
switch self.underlying {
4965
case .none:
5066
return { _ in () } // do nothing
5167

@@ -78,4 +94,16 @@ public enum OnDownActionStrategySettings {
7894
}
7995
}
8096
}
97+
98+
/// Take no (automatic) action upon noticing that this member is marked as [.down].
99+
///
100+
/// When using this mode you should take special care to implement some form of shutting down of this node (!).
101+
/// As a ``Cluster/MemberStatus/down`` node is effectively useless for the rest of the cluster -- i.e. other
102+
/// members MUST refuse communication with this down node.
103+
public static let none: OnDownActionStrategySettings = .init(.none)
104+
105+
/// Upon noticing that this member is marked as [.down], initiate a shutdown.
106+
public static func gracefulShutdown(delay: Duration) -> OnDownActionStrategySettings {
107+
.init(.gracefulShutdown(delay: delay))
108+
}
81109
}

Sources/DistributedActors/Cluster/Downing/TimeoutBasedDowningStrategy.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public final class TimeoutBasedDowningStrategy: DowningStrategy {
7474
} else {
7575
return self.onMemberReachable(change)
7676
}
77+
78+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
79+
return .none // do nothing
7780
}
7881
}
7982

Sources/DistributedActors/Cluster/Leadership.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ extension Leadership {
141141

142142
case .leadershipChange:
143143
return .same // we are the source of such events!
144+
145+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
146+
context.log.error("Received Cluster.Event [\(event)]. This should not happen, please file an issue.")
147+
return .same
144148
}
145149
}
146150
}

Sources/DistributedActors/Cluster/Reception/OperationLog.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ extension OpLog.SequencedOp: Codable where Op: Codable {
151151
extension OpLog.SequencedOp: Equatable where Op: Equatable {}
152152

153153
extension OpLog.SequencedOp.SequenceRange: Codable {
154-
public enum DiscriminatorKeys: String, Codable {
154+
enum DiscriminatorKeys: String, Codable {
155155
case single
156156
case inclusiveRange
157157
}
158158

159-
public enum CodingKeys: CodingKey {
159+
enum CodingKeys: CodingKey {
160160
case _case
161161

162162
case single_value

Sources/DistributedActors/Cluster/Reception/OperationLogDistributedReceptionist.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,9 @@ extension OpLogDistributedReceptionist {
866866

867867
case .leadershipChange, .reachabilityChange:
868868
return // we ignore those
869+
870+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
871+
self.log.error("Received Cluster.Event [\(event)]. This should not happen, please file an issue.")
869872
}
870873
}
871874

@@ -960,21 +963,21 @@ extension OpLogDistributedReceptionist {
960963
self.sequencedOps.lazy.map(\.sequenceRange.max).max() ?? 0
961964
}
962965

963-
public enum CodingKeys: CodingKey {
966+
enum CodingKeys: CodingKey {
964967
case peer
965968
case observedSeqNrs
966969
case sequencedOps
967970
}
968971

969-
public required init(from decoder: Decoder) throws {
972+
required init(from decoder: Decoder) throws {
970973
let container = try decoder.container(keyedBy: CodingKeys.self)
971974
self.peer = try container.decode(OpLogDistributedReceptionist.self, forKey: .peer)
972975
self.observedSeqNrs = try container.decode(VersionVector.self, forKey: .observedSeqNrs)
973976
self.sequencedOps = try container.decode([OpLog<ReceptionistOp>.SequencedOp].self, forKey: .sequencedOps)
974977
super.init()
975978
}
976979

977-
override public func encode(to encoder: Encoder) throws {
980+
override func encode(to encoder: Encoder) throws {
978981
var container = encoder.container(keyedBy: CodingKeys.self)
979982
try container.encode(self.peer, forKey: .peer)
980983
try container.encode(self.observedSeqNrs, forKey: .observedSeqNrs)
@@ -1004,14 +1007,14 @@ extension OpLogDistributedReceptionist {
10041007
super.init()
10051008
}
10061009

1007-
public enum CodingKeys: CodingKey {
1010+
enum CodingKeys: CodingKey {
10081011
case until
10091012
case otherObservedSeqNrs
10101013
case peer
10111014
}
10121015

10131016
// TODO: annoyance; init MUST be defined here rather than in extension since it is required
1014-
public required init(from decoder: Decoder) throws {
1017+
required init(from decoder: Decoder) throws {
10151018
let container = try decoder.container(keyedBy: CodingKeys.self)
10161019
let until = try container.decode(UInt64.self, forKey: .until)
10171020
let otherObservedSeqNrs = try container.decode(VersionVector.self, forKey: .otherObservedSeqNrs)
@@ -1023,7 +1026,7 @@ extension OpLogDistributedReceptionist {
10231026
super.init()
10241027
}
10251028

1026-
override public func encode(to encoder: Encoder) throws {
1029+
override func encode(to encoder: Encoder) throws {
10271030
var container = encoder.container(keyedBy: CodingKeys.self)
10281031
try container.encode(self.until, forKey: .until)
10291032
try container.encode(self.otherObservedSeqNrs, forKey: .otherObservedSeqNrs)

Sources/DistributedActors/Cluster/Reception/_OperationLogClusterReceptionistBehavior.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ extension _OperationLogClusterReceptionist {
608608

609609
case .leadershipChange, .reachabilityChange:
610610
return // we ignore those
611+
612+
case ._PLEASE_DO_NOT_EXHAUSTIVELY_MATCH_THIS_ENUM_NEW_CASES_MIGHT_BE_ADDED_IN_THE_FUTURE:
613+
context.log.error("Received Cluster.Event [\(event)]. This should not happen, please file an issue.")
611614
}
612615
}
613616

@@ -694,22 +697,22 @@ extension _OperationLogClusterReceptionist {
694697
self.sequencedOps.lazy.map(\.sequenceRange.max).max() ?? 0
695698
}
696699

697-
public enum CodingKeys: CodingKey {
700+
enum CodingKeys: CodingKey {
698701
case peer
699702
case observedSeqNrs
700703
case sequencedOps
701704
}
702705

703706
// TODO: annoyance; init MUST be defined here rather than in extension since it is required
704-
public required init(from decoder: Decoder) throws {
707+
required init(from decoder: Decoder) throws {
705708
let container = try decoder.container(keyedBy: CodingKeys.self)
706709
self.peer = try container.decode(_ActorRef<Receptionist.Message>.self, forKey: .peer)
707710
self.observedSeqNrs = try container.decode(VersionVector.self, forKey: .observedSeqNrs)
708711
self.sequencedOps = try container.decode([OpLog<ReceptionistOp>.SequencedOp].self, forKey: .sequencedOps)
709712
super.init()
710713
}
711714

712-
override public func encode(to encoder: Encoder) throws {
715+
override func encode(to encoder: Encoder) throws {
713716
var container = encoder.container(keyedBy: CodingKeys.self)
714717
try container.encode(self.peer, forKey: .peer)
715718
try container.encode(self.observedSeqNrs, forKey: .observedSeqNrs)
@@ -739,14 +742,14 @@ extension _OperationLogClusterReceptionist {
739742
super.init()
740743
}
741744

742-
public enum CodingKeys: CodingKey {
745+
enum CodingKeys: CodingKey {
743746
case until
744747
case otherObservedSeqNrs
745748
case peer
746749
}
747750

748751
// TODO: annoyance; init MUST be defined here rather than in extension since it is required
749-
public required init(from decoder: Decoder) throws {
752+
required init(from decoder: Decoder) throws {
750753
let container = try decoder.container(keyedBy: CodingKeys.self)
751754
let until = try container.decode(UInt64.self, forKey: .until)
752755
let otherObservedSeqNrs = try container.decode(VersionVector.self, forKey: .otherObservedSeqNrs)
@@ -758,7 +761,7 @@ extension _OperationLogClusterReceptionist {
758761
super.init()
759762
}
760763

761-
override public func encode(to encoder: Encoder) throws {
764+
override func encode(to encoder: Encoder) throws {
762765
var container = encoder.container(keyedBy: CodingKeys.self)
763766
try container.encode(self.until, forKey: .until)
764767
try container.encode(self.otherObservedSeqNrs, forKey: .otherObservedSeqNrs)

0 commit comments

Comments
 (0)