Skip to content

Commit

Permalink
fix wellKnown name handling from props
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso committed Jul 20, 2022
1 parent 18b27fa commit e73f180
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
11 changes: 10 additions & 1 deletion Sources/DistributedActors/ActorID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

import Distributed
import Foundation

// ==== ----------------------------------------------------------------------------------------------------------------
// MARK: ActorID
Expand Down Expand Up @@ -58,7 +59,7 @@ extension ClusterSystem.ActorID {
let metadata = myself.id.metadata
let key = myself[keyPath: storageKeyPath]
if let value = metadata[key.id] {
fatalError("Attempted to override ActorID Metadata for key \(key.id):\(key.keyType) which already had value: \(value); with new value: \(String(describing: newValue))")
fatalError("Attempted to override ActorID Metadata for key \(key.id):\(key.keyType) which already had value: [\(value)] with new value: [\(String(describing: newValue))]")
}
metadata[key.id] = newValue

Expand Down Expand Up @@ -140,6 +141,10 @@ extension ClusterSystem {
case .remote(let node): return node
}
}

#if DEBUG
private var debugID: UUID = UUID()
#endif

/// Collection of tags associated with this actor identity.
///
Expand Down Expand Up @@ -367,6 +372,10 @@ extension ActorID: CustomStringConvertible {
if !self.metadata.isEmpty {
res += self.metadata.description
}

#if DEBUG
res += "{debugID:\(debugID)}"
#endif

return res
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/DistributedActors/ActorMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public struct ActorMetadataKeys {
}

// ==== ----------------------------------------------------------------------------------------------------------------
// MARK: Pre-defined ActorMetadata keys
// MARK: Metadata Keys: Well Known

extension ActorMetadataKeys {
internal var path: Key<ActorPath> { "$path" }

/// Actor metadata which impacts how actors with this ID are resolved.
///
/// Rather than resolving them by their concrete incarnation (unique id), identifiers with
Expand All @@ -48,7 +48,18 @@ extension ActorMetadataKeys {
/// **WARNING:** Do not use this mechanism for "normal" actors, as it makes their addressess "guessable",
/// which is bad from a security and system independence stand point. Please use the cluster receptionist instead.
public var wellKnown: Key<String> { "$wellKnown" }
}

extension ActorID {
internal var isWellKnown: Bool {
self.metadata.wellKnown != nil
}
}

// ==== ----------------------------------------------------------------------------------------------------------------
// MARK: Metadata Keys: Type

extension ActorMetadataKeys {
/// The type of the distributed actor identified by this ``ActorID``.
/// Used only for human radability and debugging purposes, does not participate in equality checks of an actor ID.
internal var type: Key<ActorTypeTagValue> { "$type" } // TODO: remove Tag from name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public distributed actor OpLogDistributedReceptionist: DistributedReceptionist,
var ps = _Props()
ps._systemActor = true
ps._wellKnown = true
// _knownActorName name is set with @ActorID.Metadata
ps._knownActorName = ActorPath.distributedActorReceptionist.name
return ps
}

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

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

self.eventsListeningTask = Task.detached {
try await self.whenLocal { __secretlyKnownToBeLocal in // TODO(distributed): this is annoying, we must track "known to be local" in typesystem instead
Expand Down
6 changes: 4 additions & 2 deletions Sources/DistributedActors/ClusterSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ extension ClusterSystem {
)
}

if let wellKnownName = props._knownActorName {
if let wellKnownName = props._wellKnownName {
id.metadata.wellKnown = wellKnownName
}

Expand All @@ -1007,7 +1007,9 @@ extension ClusterSystem {

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

// Spawn a behavior actor for it:
let behavior = InvocationBehavior.behavior(instance: Weak(actor))
Expand Down
7 changes: 7 additions & 0 deletions Sources/DistributedActors/Props.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public struct _Props: @unchecked Sendable {
/// INTERNAL API: Allows spawning a "well known" actor. Use with great care,
/// only if a single incarnation of actor will ever exist under the given path.
internal var _wellKnown: Bool = false

/// Sets the ``ActorMetadataKeys/wellKnown`` key to this name during spawning.
internal var _wellKnownName: String? {
willSet {
self._wellKnown = newValue != nil
}
}

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

0 comments on commit e73f180

Please sign in to comment.