Skip to content

Commit e73386f

Browse files
committed
more renames to Metadata
1 parent 7f5c757 commit e73386f

File tree

8 files changed

+112
-75
lines changed

8 files changed

+112
-75
lines changed

Sources/DistributedActors/ActorID.swift

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,14 @@ import Distributed
2020
/// Convenience alias for ``ClusterSystem/ActorID``.
2121
public typealias ActorID = ClusterSystem.ActorID
2222

23-
extension DistributedActor where ActorSystem == ClusterSystem {
24-
public nonisolated var metadata: ActorTags {
25-
self.id.tags
26-
}
27-
}
28-
2923
extension ClusterSystem.ActorID {
24+
3025
@propertyWrapper
3126
public struct Metadata<Value: Sendable & Codable, Key: ActorTagKey<Value>> {
32-
private var stored: Value?
33-
34-
public init(_ key: Key.Type) {}
35-
27+
public init(_: Key.Type) {
28+
// no initial value; it must be set during initialization
29+
}
30+
3631
public var wrappedValue: Value {
3732
get { fatalError("called wrappedValue getter") }
3833
set { fatalError("called wrappedValue setter") }
@@ -49,11 +44,17 @@ extension ClusterSystem.ActorID {
4944
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
5045
) -> Value where EnclosingSelf.ActorSystem == ClusterSystem, EnclosingSelf.ID == ClusterSystem.ActorID {
5146
get {
52-
return myself.id.tags[Key.self]!
47+
guard let value = myself.id.metadata[Key.self] else {
48+
fatalError("ActorID Metadata for key \(Key.self) was not assigned initial value, assign one in the distributed actor's initializer.")
49+
}
50+
return value
5351
}
5452
set {
55-
let tags = myself.id.tags
56-
tags[Key.self] = newValue
53+
let metadata = myself.id.metadata
54+
if let value = metadata[Key.self] {
55+
fatalError("Attempted to override ActorID Metadata for key \(Key.self) which already had value: \(value); with new value: \(String(describing: newValue))")
56+
}
57+
metadata[Key.self] = newValue
5758
}
5859
}
5960
}
@@ -79,7 +80,7 @@ extension ClusterSystem {
7980
/// The location of the distributed actor (i.e. it being "remote" or "local") is not taken into account
8081
/// during comparison of IDs, however does matter that it is on the same actual unique node.
8182
///
82-
/// Additional information can be attached to them using ``ActorTags`` however those do not impact
83+
/// Additional information can be attached to them using ``ActorMetadata`` however those do not impact
8384
/// the identity or equality of the ID, or distributed actors identified by those IDs.
8485
///
8586
/// ## Lifecycle
@@ -103,7 +104,7 @@ extension ClusterSystem {
103104
/// Some tags may be carried to remote peers, while others are intended only for local use,
104105
/// e.g. to inform the actor system to resolve the actor identity using some special treatment etc.
105106
///
106-
/// Please refer to ``ActorTags`` for an in depth discussion about tagging.
107+
/// Please refer to ``ActorMetadata`` for an in depth discussion about tagging.
107108
///
108109
/// ## Serialization
109110
///
@@ -135,9 +136,9 @@ extension ClusterSystem {
135136
/// Tags MAY be transferred to other peers as the identity is replicated, however they are not necessary to uniquely identify the actor.
136137
/// Tags can carry additional information such as the type of the actor identified by this identity, or any other user defined "roles" or similar tags.
137138
///
138-
/// - SeeAlso: `ActorTags` for a detailed discussion of some frequently used tags.
139-
public var tags: ActorTags {
140-
self.context.tags
139+
/// - SeeAlso: ``ActorMetadata`` for a detailed discussion of some frequently used tags.
140+
public var metadata: ActorMetadata {
141+
self.context.metadata
141142
}
142143

143144
/// Internal "actor context" which is used as storage for additional cluster actor features, such as watching.
@@ -147,13 +148,13 @@ extension ClusterSystem {
147148
// FIXME(distributed): make optional
148149
public var path: ActorPath {
149150
get {
150-
guard let path = tags[ActorTags.path] else {
151+
guard let path = metadata[ActorMetadata.path] else {
151152
fatalError("FIXME: ActorTags.path was not set on \(self.incarnation)! NOTE THAT PATHS ARE TO BECOME OPTIONAL!!!") // FIXME(distributed): must be removed
152153
}
153154
return path
154155
}
155156
set {
156-
self.tags[ActorTags.path] = newValue
157+
self.metadata[ActorMetadata.path] = newValue
157158
}
158159
}
159160

@@ -178,7 +179,7 @@ extension ClusterSystem {
178179
self._location = .local(node)
179180
self.incarnation = incarnation
180181
if let path {
181-
self.tags[ActorTags.path] = path
182+
self.context.metadata[ActorMetadata.path] = path
182183
}
183184
traceLog_DeathWatch("Made ID: \(self)")
184185
}
@@ -194,7 +195,7 @@ extension ClusterSystem {
194195
self._location = .remote(node)
195196
self.incarnation = incarnation
196197
if let path {
197-
self.context.tags[ActorTags.path] = path
198+
self.context.metadata[ActorMetadata.path] = path
198199
}
199200
traceLog_DeathWatch("Made ID: \(self)")
200201
}
@@ -206,7 +207,7 @@ extension ClusterSystem {
206207
self._location = .remote(node)
207208
self.incarnation = incarnation
208209
if let mangledName = _mangledTypeName(type) { // TODO: avoid mangling names on every spawn?
209-
self.context.tags[ActorTags.type] = .init(mangledName: mangledName)
210+
self.context.metadata[ActorMetadata.type] = .init(mangledName: mangledName)
210211
}
211212
traceLog_DeathWatch("Made ID: \(self)")
212213
}
@@ -219,7 +220,7 @@ extension ClusterSystem {
219220
self._location = .local(node)
220221
self.incarnation = incarnation
221222
if let mangledName = _mangledTypeName(type) { // TODO: avoid mangling names on every spawn?
222-
context.tags[ActorTags.type] = .init(mangledName: mangledName)
223+
self.context.metadata[ActorMetadata.type] = .init(mangledName: mangledName)
223224
}
224225
traceLog_DeathWatch("Made ID: \(self)")
225226
}
@@ -232,7 +233,7 @@ extension ClusterSystem {
232233
self._location = .remote(node)
233234
self.incarnation = incarnation
234235
if let mangledName = _mangledTypeName(type) { // TODO: avoid mangling names on every spawn?
235-
context.tags[ActorTags.type] = .init(mangledName: mangledName)
236+
self.context.metadata[ActorMetadata.type] = .init(mangledName: mangledName)
236237
}
237238
traceLog_DeathWatch("Made ID: \(self)")
238239
}
@@ -241,7 +242,7 @@ extension ClusterSystem {
241242
var copy = self
242243
copy.context = .init(
243244
lifecycle: nil,
244-
tags: self.tags
245+
metadata: self.metadata
245246
)
246247
return copy
247248
}
@@ -250,7 +251,7 @@ extension ClusterSystem {
250251
var copy = self
251252
copy.context = .init(
252253
lifecycle: self.context.lifecycle,
253-
tags: nil
254+
metadata: nil
254255
)
255256
return copy
256257
}
@@ -914,16 +915,16 @@ extension ActorID: Codable {
914915
try container.encode(self.path, forKey: ActorCoding.CodingKeys.path) // TODO: remove as we remove the tree
915916
try container.encode(self.incarnation, forKey: ActorCoding.CodingKeys.incarnation)
916917

917-
if !self.tags.isEmpty {
918+
if !self.metadata.isEmpty {
918919
var tagsContainer = container.nestedContainer(keyedBy: ActorCoding.TagKeys.self, forKey: ActorCoding.CodingKeys.tags)
919920

920-
if (tagSettings == nil || tagSettings!.propagateTags.contains(AnyActorTagKey(ActorTags.path))),
921-
let value = self.tags[ActorTags.path]
921+
if (tagSettings == nil || tagSettings!.propagateTags.contains(AnyActorTagKey(ActorMetadata.path))),
922+
let value = self.metadata[ActorMetadata.path]
922923
{
923924
try tagsContainer.encode(value, forKey: ActorCoding.TagKeys.path)
924925
}
925-
if (tagSettings == nil || tagSettings!.propagateTags.contains(AnyActorTagKey(ActorTags.type))),
926-
let value = self.tags[ActorTags.type]
926+
if (tagSettings == nil || tagSettings!.propagateTags.contains(AnyActorTagKey(ActorMetadata.type))),
927+
let value = self.metadata[ActorMetadata.type]
927928
{
928929
try tagsContainer.encode(value, forKey: ActorCoding.TagKeys.type)
929930
}
@@ -952,7 +953,7 @@ extension ActorID: Codable {
952953
for tag in try decodeCustomTags(tagsContainer) {
953954
func store<K: ActorTagKey>(_: K.Type) {
954955
if let value = tag.value as? K.Value {
955-
self.tags[K.self] = value
956+
self.metadata[K.self] = value
956957
}
957958
}
958959
_openExistential(tag.keyType as any ActorTagKey.Type, do: store) // the `as` here is required, because: inferred result type 'any ActorTagKey.Type' requires explicit coercion due to loss of generic requirements

Sources/DistributedActors/ActorTags.swift renamed to Sources/DistributedActors/ActorMetadata.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import Dispatch
1616
import Distributed
1717

1818
// ==== ----------------------------------------------------------------------------------------------------------------
19-
// MARK: ActorTags
19+
// MARK: ActorMetadata
2020

2121
/// Container of tags a concrete actor identity was tagged with.
22-
public final class ActorTags: CustomStringConvertible, CustomDebugStringConvertible {
22+
public final class ActorMetadata: CustomStringConvertible, CustomDebugStringConvertible {
2323
internal let lock = DispatchSemaphore(value: 1)
2424

2525
// We still might re-think how we represent the storage.
@@ -29,9 +29,9 @@ public final class ActorTags: CustomStringConvertible, CustomDebugStringConverti
2929
// empty tags
3030
}
3131

32-
public init(tags: [any ActorTag]) {
33-
for tag in tags {
34-
self._storage[tag.id] = tag.value
32+
public init(_ metadata: [any ActorTag]) {
33+
for m in metadata {
34+
self._storage[m.id] = m.value
3535
}
3636
}
3737

@@ -132,9 +132,9 @@ struct AnyActorTagKey: Hashable {
132132
}
133133

134134
// ==== ----------------------------------------------------------------------------------------------------------------
135-
// MARK: Known keys
135+
// MARK: Known keys: path
136136

137-
extension ActorTags {
137+
extension ActorMetadata {
138138
static let path = ActorPathTag.Key.self
139139
struct ActorPathTag: ActorTag {
140140
struct Key: ActorTagKey {
@@ -149,7 +149,8 @@ extension ActorTags {
149149
// ==== ----------------------------------------------------------------------------------------------------------------
150150
// MARK: Known tag: type
151151

152-
extension ActorTags {
152+
extension ActorMetadata {
153+
153154
static let type = ActorTypeTag.Key.self
154155
struct ActorTypeTag: ActorTag {
155156
struct Key: ActorTagKey {

Sources/DistributedActors/ActorTagSettings.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public struct ActorTagSettings {
4343
/// What type of tags, known and defined by the cluster system itself, should be automatically propagated.
4444
/// Other types of tags, such as user-defined tags, must be propagated by declaring apropriate functions for `encodeCustomTags` and `decodeCustomTags`.
4545
internal var propagateTags: Set<AnyActorTagKey> = [
46-
.init(ActorTags.path),
47-
.init(ActorTags.type),
46+
.init(ActorMetadata.path),
47+
.init(ActorMetadata.type),
4848
]
4949

5050
// TODO: expose this eventually

Sources/DistributedActors/ClusterSystem.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,11 @@ extension ClusterSystem {
858858
}
859859

860860
/// Allows creating a distributed actor with additional configuration applied during its initialization.
861-
internal func actorWith<Act: DistributedActor>(_ tags: (any ActorTag)...,
861+
internal func actorWith<Act: DistributedActor>(_ metadata: (any ActorTag)...,
862862
makeActor: () throws -> Act) rethrows -> Act
863863
{
864864
var props = _Props.forSpawn
865-
props.tags = .init(tags: tags)
865+
props.tags = .init(metadata)
866866

867867
return try _Props.$forSpawn.withValue(props) {
868868
try makeActor()
@@ -922,7 +922,7 @@ extension ClusterSystem {
922922
// but we can only do this when we remove the dependence on paths and behaviors entirely from DA actors https://github.com/apple/swift-distributed-actors/issues/957
923923
id.context = DistributedActorContext(
924924
lifecycle: lifecycleContainer,
925-
tags: id.context.tags
925+
metadata: id.context.metadata
926926
)
927927

928928
self.log.warning("Assign identity", metadata: [
@@ -947,13 +947,6 @@ extension ClusterSystem {
947947
defer { self.namingLock.unlock() }
948948
precondition(self._reservedNames.remove(actor.id) != nil, "Attempted to ready an identity that was not reserved: \(actor.id)")
949949

950-
// if let watcher = actor as? any LifecycleWatch {
951-
// func doMakeLifecycleWatch<Watcher: LifecycleWatch & DistributedActor>(watcher: Watcher) {
952-
// _ = LifecycleWatchContainer(watcher)
953-
// }
954-
// _openExistential(watcher, do: doMakeLifecycleWatch)
955-
// }
956-
957950
let behavior = InvocationBehavior.behavior(instance: Weak(actor))
958951
let ref = self._spawnDistributedActor(behavior, identifiedBy: actor.id)
959952
self._managedRefs[actor.id] = ref
@@ -970,11 +963,7 @@ extension ClusterSystem {
970963
}
971964
}
972965
id.context.terminate()
973-
// self.lifecycleWatchLock.withLockVoid {
974-
// if let watch = self._lifecycleWatches.removeValue(forKey: id) {
975-
// watch.notifyWatchersWeDied()
976-
// }
977-
// }
966+
978967
self.namingLock.withLockVoid {
979968
self._managedRefs.removeValue(forKey: id) // TODO: should not be necessary in the future
980969
_ = self._managedDistributedActors.removeActor(identifiedBy: id)

Sources/DistributedActors/DistributedActorContext.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import Distributed
2222
/// as at that point in time it can no longer be used–by the now deallocated–actor itself.
2323
public final class DistributedActorContext {
2424
let lifecycle: LifecycleWatchContainer?
25-
let tags: ActorTags
25+
let metadata: ActorMetadata
2626

2727
init(lifecycle: LifecycleWatchContainer?,
28-
tags: ActorTags? = nil)
28+
metadata: ActorMetadata? = nil)
2929
{
3030
self.lifecycle = lifecycle
31-
self.tags = tags ?? ActorTags(tags: [])
31+
self.metadata = metadata ?? ActorMetadata()
3232

3333
traceLog_DeathWatch("Create context; Lifecycle: \(lifecycle)")
3434
}

Sources/DistributedActors/Props.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct _Props: @unchecked Sendable {
3838
internal var supervision: _SupervisionProps
3939

4040
/// Tags to be passed to the actor's identity.
41-
internal var tags: ActorTags
41+
internal var tags: ActorMetadata
4242

4343
public var metrics: MetricsProps
4444

@@ -61,7 +61,7 @@ public struct _Props: @unchecked Sendable {
6161
internal var _distributedActor: Bool = false
6262

6363
public init(
64-
tags: ActorTags = ActorTags(),
64+
tags: ActorMetadata = ActorMetadata(),
6565
dispatcher: _DispatcherProps = .default,
6666
supervision: _SupervisionProps = .default,
6767
metrics: MetricsProps = .disabled

0 commit comments

Comments
 (0)