forked from asynkron/protoactor-go
/
cluster.go
213 lines (166 loc) · 5.09 KB
/
cluster.go
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
package cluster
import (
"log/slog"
"time"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/asynkron/gofun/set"
"github.com/keecon/protoactor-go/actor"
"github.com/keecon/protoactor-go/extensions"
"github.com/keecon/protoactor-go/remote"
)
var extensionID = extensions.NextExtensionID()
type Cluster struct {
ActorSystem *actor.ActorSystem
Config *Config
Gossip *Gossiper
PubSub *PubSub
Remote *remote.Remote
PidCache *PidCacheValue
MemberList *MemberList
IdentityLookup IdentityLookup
kinds map[string]*ActivatedKind
context Context
}
var _ extensions.Extension = &Cluster{}
func New(actorSystem *actor.ActorSystem, config *Config) *Cluster {
c := &Cluster{
ActorSystem: actorSystem,
Config: config,
kinds: map[string]*ActivatedKind{},
}
actorSystem.Extensions.Register(c)
c.context = config.ClusterContextProducer(c)
c.PidCache = NewPidCache()
c.MemberList = NewMemberList(c)
c.subscribeToTopologyEvents()
actorSystem.Extensions.Register(c)
var err error
c.Gossip, err = newGossiper(c)
c.PubSub = NewPubSub(c)
if err != nil {
panic(err)
}
return c
}
func (c *Cluster) subscribeToTopologyEvents() {
c.ActorSystem.EventStream.Subscribe(func(evt interface{}) {
if clusterTopology, ok := evt.(*ClusterTopology); ok {
for _, member := range clusterTopology.Left {
c.PidCache.RemoveByMember(member)
}
}
})
}
func (c *Cluster) ExtensionID() extensions.ExtensionID {
return extensionID
}
//goland:noinspection GoUnusedExportedFunction
func GetCluster(actorSystem *actor.ActorSystem) *Cluster {
c := actorSystem.Extensions.Get(extensionID)
return c.(*Cluster)
}
func (c *Cluster) GetBlockedMembers() set.Set[string] {
return c.Remote.BlockList().BlockedMembers()
}
func (c *Cluster) StartMember() {
cfg := c.Config
c.Remote = remote.NewRemote(c.ActorSystem, c.Config.RemoteConfig)
c.initKinds()
// TODO: make it possible to become a cluster even if remoting is already started
c.Remote.Start()
address := c.ActorSystem.Address()
c.Logger().Info("Starting Proto.Actor cluster member", slog.String("address", address))
c.IdentityLookup = cfg.IdentityLookup
c.IdentityLookup.Setup(c, c.GetClusterKinds(), false)
// TODO: Disable Gossip for now until API changes are done
// gossiper must be started whenever any topology events starts flowing
if err := c.Gossip.StartGossiping(); err != nil {
panic(err)
}
c.PubSub.Start()
c.MemberList.InitializeTopologyConsensus()
if err := cfg.ClusterProvider.StartMember(c); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
}
func (c *Cluster) GetClusterKinds() []string {
keys := make([]string, 0, len(c.kinds))
for k := range c.kinds {
keys = append(keys, k)
}
return keys
}
func (c *Cluster) StartClient() {
cfg := c.Config
c.Remote = remote.NewRemote(c.ActorSystem, c.Config.RemoteConfig)
c.Remote.Start()
address := c.ActorSystem.Address()
c.Logger().Info("Starting Proto.Actor cluster-client", slog.String("address", address))
c.IdentityLookup = cfg.IdentityLookup
c.IdentityLookup.Setup(c, c.GetClusterKinds(), true)
if err := cfg.ClusterProvider.StartClient(c); err != nil {
panic(err)
}
c.PubSub.Start()
}
func (c *Cluster) Shutdown(graceful bool) {
c.Gossip.SetState(GracefullyLeftKey, &emptypb.Empty{})
c.ActorSystem.Shutdown()
if graceful {
_ = c.Config.ClusterProvider.Shutdown(graceful)
c.IdentityLookup.Shutdown()
// This is to wait ownership transferring complete.
time.Sleep(time.Millisecond * 2000)
c.MemberList.stopMemberList()
c.IdentityLookup.Shutdown()
c.Gossip.Shutdown()
}
c.Remote.Shutdown(graceful)
address := c.ActorSystem.Address()
c.Logger().Info("Stopped Proto.Actor cluster", slog.String("address", address))
}
func (c *Cluster) Get(identity string, kind string) *actor.PID {
return c.IdentityLookup.Get(NewClusterIdentity(identity, kind))
}
func (c *Cluster) Request(identity string, kind string, message interface{}, option ...GrainCallOption) (interface{}, error) {
return c.context.Request(identity, kind, message)
}
func (c *Cluster) GetClusterKind(kind string) *ActivatedKind {
k, ok := c.kinds[kind]
if !ok {
c.Logger().Error("Invalid kind", slog.String("kind", kind))
return nil
}
return k
}
func (c *Cluster) TryGetClusterKind(kind string) (*ActivatedKind, bool) {
k, ok := c.kinds[kind]
return k, ok
}
func (c *Cluster) initKinds() {
for name, kind := range c.Config.Kinds {
c.kinds[name] = kind.Build(c)
}
c.ensureTopicKindRegistered()
}
// ensureTopicKindRegistered ensures that the topic kind is registered in the cluster
// if topic kind is not registered, it will be registered automatically
func (c *Cluster) ensureTopicKindRegistered() {
hasTopicKind := false
for name := range c.kinds {
if name == TopicActorKind {
hasTopicKind = true
break
}
}
if !hasTopicKind {
store := &EmptyKeyValueStore[*Subscribers]{}
c.kinds[TopicActorKind] = NewKind(TopicActorKind, actor.PropsFromProducer(func() actor.Actor {
return NewTopicActor(store, c.Logger())
})).Build(c)
}
}
func (c *Cluster) Logger() *slog.Logger {
return c.ActorSystem.Logger()
}