Skip to content

Commit e73f180

Browse files
committed
fix wellKnown name handling from props
1 parent 18b27fa commit e73f180

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

Sources/DistributedActors/ActorID.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Distributed
16+
import Foundation
1617

1718
// ==== ----------------------------------------------------------------------------------------------------------------
1819
// MARK: ActorID
@@ -58,7 +59,7 @@ extension ClusterSystem.ActorID {
5859
let metadata = myself.id.metadata
5960
let key = myself[keyPath: storageKeyPath]
6061
if let value = metadata[key.id] {
61-
fatalError("Attempted to override ActorID Metadata for key \(key.id):\(key.keyType) which already had value: \(value); with new value: \(String(describing: newValue))")
62+
fatalError("Attempted to override ActorID Metadata for key \(key.id):\(key.keyType) which already had value: [\(value)] with new value: [\(String(describing: newValue))]")
6263
}
6364
metadata[key.id] = newValue
6465

@@ -140,6 +141,10 @@ extension ClusterSystem {
140141
case .remote(let node): return node
141142
}
142143
}
144+
145+
#if DEBUG
146+
private var debugID: UUID = UUID()
147+
#endif
143148

144149
/// Collection of tags associated with this actor identity.
145150
///
@@ -367,6 +372,10 @@ extension ActorID: CustomStringConvertible {
367372
if !self.metadata.isEmpty {
368373
res += self.metadata.description
369374
}
375+
376+
#if DEBUG
377+
res += "{debugID:\(debugID)}"
378+
#endif
370379

371380
return res
372381
}

Sources/DistributedActors/ActorMetadata.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public struct ActorMetadataKeys {
2929
}
3030

3131
// ==== ----------------------------------------------------------------------------------------------------------------
32-
// MARK: Pre-defined ActorMetadata keys
32+
// MARK: Metadata Keys: Well Known
3333

3434
extension ActorMetadataKeys {
3535
internal var path: Key<ActorPath> { "$path" }
36-
36+
3737
/// Actor metadata which impacts how actors with this ID are resolved.
3838
///
3939
/// Rather than resolving them by their concrete incarnation (unique id), identifiers with
@@ -48,7 +48,18 @@ extension ActorMetadataKeys {
4848
/// **WARNING:** Do not use this mechanism for "normal" actors, as it makes their addressess "guessable",
4949
/// which is bad from a security and system independence stand point. Please use the cluster receptionist instead.
5050
public var wellKnown: Key<String> { "$wellKnown" }
51+
}
52+
53+
extension ActorID {
54+
internal var isWellKnown: Bool {
55+
self.metadata.wellKnown != nil
56+
}
57+
}
58+
59+
// ==== ----------------------------------------------------------------------------------------------------------------
60+
// MARK: Metadata Keys: Type
5161

62+
extension ActorMetadataKeys {
5263
/// The type of the distributed actor identified by this ``ActorID``.
5364
/// Used only for human radability and debugging purposes, does not participate in equality checks of an actor ID.
5465
internal var type: Key<ActorTypeTagValue> { "$type" } // TODO: remove Tag from name

Sources/DistributedActors/Cluster/Reception/OperationLogDistributedReceptionist.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public distributed actor OpLogDistributedReceptionist: DistributedReceptionist,
235235
var ps = _Props()
236236
ps._systemActor = true
237237
ps._wellKnown = true
238-
// _knownActorName name is set with @ActorID.Metadata
238+
ps._knownActorName = ActorPath.distributedActorReceptionist.name
239239
return ps
240240
}
241241

@@ -254,6 +254,7 @@ public distributed actor OpLogDistributedReceptionist: DistributedReceptionist,
254254

255255
// === listen to cluster events ------------------
256256
self.wellKnownName = ActorPath.distributedActorReceptionist.name
257+
assert(self.id.path.description == "/system/receptionist") // TODO(distributed): remove when we remove paths entirely
257258

258259
self.eventsListeningTask = Task.detached {
259260
try await self.whenLocal { __secretlyKnownToBeLocal in // TODO(distributed): this is annoying, we must track "known to be local" in typesystem instead

Sources/DistributedActors/ClusterSystem.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ extension ClusterSystem {
983983
)
984984
}
985985

986-
if let wellKnownName = props._knownActorName {
986+
if let wellKnownName = props._wellKnownName {
987987
id.metadata.wellKnown = wellKnownName
988988
}
989989

@@ -1007,7 +1007,9 @@ extension ClusterSystem {
10071007

10081008
self.namingLock.lock()
10091009
defer { self.namingLock.unlock() }
1010-
// precondition(self._reservedNames.remove(actor.id) != nil, "Attempted to ready an identity that was not reserved: \(actor.id)")
1010+
if !actor.id.isWellKnown {
1011+
precondition(self._reservedNames.remove(actor.id) != nil, "Attempted to ready an identity that was not reserved: \(actor.id)")
1012+
}
10111013

10121014
// Spawn a behavior actor for it:
10131015
let behavior = InvocationBehavior.behavior(instance: Weak(actor))

Sources/DistributedActors/Props.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public struct _Props: @unchecked Sendable {
4545
/// INTERNAL API: Allows spawning a "well known" actor. Use with great care,
4646
/// only if a single incarnation of actor will ever exist under the given path.
4747
internal var _wellKnown: Bool = false
48+
49+
/// Sets the ``ActorMetadataKeys/wellKnown`` key to this name during spawning.
50+
internal var _wellKnownName: String? {
51+
willSet {
52+
self._wellKnown = newValue != nil
53+
}
54+
}
4855

4956
/// INTERNAL API: Internal system actor, spawned under the /system namespace.
5057
/// This is likely to go away as we remove the actor tree, and move completely to 'distributed actor'.

0 commit comments

Comments
 (0)