From 378d30999d040702f0afeb82c9917cfc6674c1ee Mon Sep 17 00:00:00 2001 From: wangxw <996268132@qq.com> Date: Sat, 5 Nov 2022 16:11:08 +0800 Subject: [PATCH] add Activate and Deactivate interface for actor, do sth when create or remove actors --- actor/actor.go | 4 ++++ actor/manager/container.go | 11 ++++++++++- actor/manager/manager.go | 3 ++- actor/mock/mock_factory_impl.go | 16 ++++++++++++++++ actor/mock/mock_server.go | 8 ++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/actor/actor.go b/actor/actor.go index 4dfaa79c..8b147c93 100644 --- a/actor/actor.go +++ b/actor/actor.go @@ -43,6 +43,10 @@ type Server interface { // SaveState is impl by ServerImplBase, It saves the state cache of this actor instance to state store component by calling api of daprd. // Save state is called at two places: 1. On invocation of this actor instance. 2. When new actor starts. SaveState() error + // Activate called when actor created by actor manager + Activate() error + // Deactivate called before actor removed by actor manager + Deactivate() error } type ReminderCallee interface { diff --git a/actor/manager/container.go b/actor/manager/container.go index 35f9db50..19bf0027 100644 --- a/actor/manager/container.go +++ b/actor/manager/container.go @@ -28,6 +28,7 @@ import ( type ActorContainer interface { Invoke(methodName string, param []byte) ([]reflect.Value, actorErr.ActorErr) GetActor() actor.Server + Deactivate() error } // DefaultActorContainer contains actor instance and methods type info generated from actor. @@ -43,8 +44,12 @@ func NewDefaultActorContainer(actorID string, impl actor.Server, serializer code daprClient, _ := dapr.NewClient() // create state manager for this new actor impl.SetStateManager(state.NewActorStateManager(impl.Type(), actorID, state.NewDaprStateAsyncProvider(daprClient))) + err := impl.Activate() + if err != nil { + return nil, actorErr.ErrSaveStateFailed + } // save state of this actor - err := impl.SaveState() + err = impl.SaveState() if err != nil { return nil, actorErr.ErrSaveStateFailed } @@ -84,3 +89,7 @@ func (d *DefaultActorContainer) Invoke(methodName string, param []byte) ([]refle returnValue := methodType.method.Func.Call(argsValues) return returnValue, actorErr.Success } + +func (d *DefaultActorContainer) Deactivate() error { + return d.actor.Deactivate() +} diff --git a/actor/manager/manager.go b/actor/manager/manager.go index ae476126..6c63e127 100644 --- a/actor/manager/manager.go +++ b/actor/manager/manager.go @@ -122,10 +122,11 @@ func (m *DefaultActorManager) InvokeMethod(actorID, methodName string, request [ // DeactivateActor removes actor from actor manager. func (m *DefaultActorManager) DeactivateActor(actorID string) actorErr.ActorErr { - _, ok := m.activeActors.Load(actorID) + actor, ok := m.activeActors.Load(actorID) if !ok { return actorErr.ErrActorIDNotFound } + actor.(ActorContainer).Deactivate() m.activeActors.Delete(actorID) return actorErr.Success } diff --git a/actor/mock/mock_factory_impl.go b/actor/mock/mock_factory_impl.go index 2fb954a4..1da68e45 100644 --- a/actor/mock/mock_factory_impl.go +++ b/actor/mock/mock_factory_impl.go @@ -27,6 +27,14 @@ type ActorImpl struct { actor.ServerImplBase } +func (t *ActorImpl) Activate() error { + return nil +} + +func (t *ActorImpl) Deactivate() error { + return nil +} + func (t *ActorImpl) Type() string { return "testActorType" } @@ -46,6 +54,14 @@ type NotReminderCalleeActor struct { actor.ServerImplBase } +func (t *NotReminderCalleeActor) Activate() error { + return nil +} + +func (t *NotReminderCalleeActor) Deactivate() error { + return nil +} + func (t *NotReminderCalleeActor) Type() string { return "testActorNotReminderCalleeType" } diff --git a/actor/mock/mock_server.go b/actor/mock/mock_server.go index 7972ebab..3f3fa6e9 100644 --- a/actor/mock/mock_server.go +++ b/actor/mock/mock_server.go @@ -114,3 +114,11 @@ func (mr *MockServerMockRecorder) Type() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Type", reflect.TypeOf((*MockServer)(nil).Type)) } + +func (m *MockServer) Activate() error { + return nil +} + +func (m *MockServer) Deactivate() error { + return nil +}