Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
feature/issue 59 (#69)
Browse files Browse the repository at this point in the history
* [issue59] Added Passivation Strategy options and adapted to new TCK tests. Passes with 186/186.
* [issue59] added optional comma in aggregated prototext value; to be inline with the main repository.
* [issue59] aligned with minor differences in *.proto files regarding formatting and small differences like options.
  • Loading branch information
marcellanz authored Jan 13, 2021
1 parent 005c956 commit 1169391
Show file tree
Hide file tree
Showing 28 changed files with 815 additions and 134 deletions.
9 changes: 6 additions & 3 deletions cloudstate/cloudstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func New(c protocol.Config) (*CloudState, error) {
}

// RegisterEventSourced registers an event sourced entity.
func (cs *CloudState) RegisterEventSourced(entity *eventsourced.Entity, config protocol.DescriptorConfig) error {
func (cs *CloudState) RegisterEventSourced(entity *eventsourced.Entity, config protocol.DescriptorConfig, options ...eventsourced.Option) error {
entity.Options(options...)
if err := cs.eventSourcedServer.Register(entity); err != nil {
return err
}
Expand All @@ -72,7 +73,8 @@ func (cs *CloudState) RegisterEventSourced(entity *eventsourced.Entity, config p
}

// RegisterCRDT registers a CRDT entity.
func (cs *CloudState) RegisterCRDT(entity *crdt.Entity, config protocol.DescriptorConfig) error {
func (cs *CloudState) RegisterCRDT(entity *crdt.Entity, config protocol.DescriptorConfig, options ...crdt.Option) error {
entity.Options(options...)
if err := cs.crdtServer.Register(entity); err != nil {
return err
}
Expand All @@ -94,7 +96,8 @@ func (cs *CloudState) RegisterAction(entity *action.Entity, config protocol.Desc
}

// RegisterValueEntity registers a Value entity.
func (cs *CloudState) RegisterValueEntity(entity *value.Entity, config protocol.DescriptorConfig) error {
func (cs *CloudState) RegisterValueEntity(entity *value.Entity, config protocol.DescriptorConfig, options ...value.Option) error {
entity.Options(options...)
if err := cs.valueServer.Register(entity); err != nil {
return err
}
Expand Down
26 changes: 25 additions & 1 deletion cloudstate/crdt/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package crdt

import (
"time"

"github.com/cloudstateio/go-support/cloudstate/protocol"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
)
Expand All @@ -27,7 +30,28 @@ type Entity struct {
// this entities interface. Setting it is mandatory.
ServiceName ServiceName
// EntityFunc creates a new entity.
EntityFunc func(id EntityID) EntityHandler
EntityFunc func(id EntityID) EntityHandler
PassivationStrategy protocol.EntityPassivationStrategy
}

type Option func(s *Entity)

func (e *Entity) Options(options ...Option) {
for _, opt := range options {
opt(e)
}
}

func WithPassivationStrategyTimeout(duration time.Duration) Option {
return func(e *Entity) {
e.PassivationStrategy = protocol.EntityPassivationStrategy{
Strategy: &protocol.EntityPassivationStrategy_Timeout{
Timeout: &protocol.TimeoutPassivationStrategy{
Timeout: duration.Milliseconds(),
},
},
}
}
}

// EntityHandler has to be implemented by any type that wants to get
Expand Down
32 changes: 22 additions & 10 deletions cloudstate/discovery/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ func (s *EntityDiscoveryServer) RegisterEventSourcedEntity(entity *eventsourced.
if err := s.resolveFileDescriptors(config); err != nil {
return fmt.Errorf("failed to resolve FileDescriptor for DescriptorConfig: %+v: %w", config, err)
}
s.entitySpec.Entities = append(s.entitySpec.Entities, &protocol.Entity{
e := &protocol.Entity{
EntityType: protocol.EventSourced,
ServiceName: entity.ServiceName.String(),
PersistenceId: entity.PersistenceID,
})
}
if entity.PassivationStrategy.Strategy != nil {
e.PassivationStrategy = &entity.PassivationStrategy
}
s.entitySpec.Entities = append(s.entitySpec.Entities, e)
return s.updateSpec()
}

Expand All @@ -141,12 +145,16 @@ func (s *EntityDiscoveryServer) RegisterCRDTEntity(entity *crdt.Entity, config p
if err := s.resolveFileDescriptors(config); err != nil {
return fmt.Errorf("failed to resolveFileDescriptor for DescriptorConfig: %+v: %w", config, err)
}
s.entitySpec.Entities = append(s.entitySpec.Entities, &protocol.Entity{
EntityType: protocol.CRDT,
ServiceName: entity.ServiceName.String(),
PersistenceId: entity.ServiceName.String(), // make sure CRDT entities have unique keys per service

})
e := &protocol.Entity{
EntityType: protocol.CRDT,
ServiceName: entity.ServiceName.String(),
// TODO: as per https://github.com/cloudstateio/go-support/pull/67#issuecomment-749838999 this is temporary.
PersistenceId: entity.ServiceName.String(), // make sure CRDT entities have unique keys per service.
}
if entity.PassivationStrategy.GetStrategy() != nil {
e.PassivationStrategy = &entity.PassivationStrategy
}
s.entitySpec.Entities = append(s.entitySpec.Entities, e)
return s.updateSpec()
}

Expand All @@ -169,11 +177,15 @@ func (s *EntityDiscoveryServer) RegisterValueEntity(entity *value.Entity, config
if err := s.resolveFileDescriptors(config); err != nil {
return fmt.Errorf("failed to resolveFileDescriptor for DescriptorConfig: %+v: %w", config, err)
}
s.entitySpec.Entities = append(s.entitySpec.Entities, &protocol.Entity{
e := &protocol.Entity{
EntityType: protocol.Value,
ServiceName: entity.ServiceName.String(),
PersistenceId: entity.PersistenceID,
})
}
if entity.PassivationStrategy.GetStrategy() != nil {
e.PassivationStrategy = &entity.PassivationStrategy
}
s.entitySpec.Entities = append(s.entitySpec.Entities, e)
return s.updateSpec()
}

Expand Down
25 changes: 25 additions & 0 deletions cloudstate/eventsourced/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package eventsourced

import (
"time"

"github.com/cloudstateio/go-support/cloudstate/protocol"
"github.com/golang/protobuf/proto"
)

Expand All @@ -40,6 +43,28 @@ type Entity struct {
SnapshotEvery int64
// EntityFunc is a factory method which generates a new Entity.
EntityFunc func(id EntityID) EntityHandler

PassivationStrategy protocol.EntityPassivationStrategy
}

type Option func(s *Entity)

func (e *Entity) Options(options ...Option) {
for _, opt := range options {
opt(e)
}
}

func WithPassivationStrategyTimeout(duration time.Duration) Option {
return func(e *Entity) {
e.PassivationStrategy = protocol.EntityPassivationStrategy{
Strategy: &protocol.EntityPassivationStrategy_Timeout{
Timeout: &protocol.TimeoutPassivationStrategy{
Timeout: duration.Milliseconds(),
},
},
}
}
}

type (
Expand Down
Loading

0 comments on commit 1169391

Please sign in to comment.