Skip to content

Commit

Permalink
feat(store-forward): Enable Custom Factory Registration (#1051)
Browse files Browse the repository at this point in the history
Move necessary contracts to public package.  Required moving
store client initialization from the end of service.Initialize
to the beginning of service.MakeItRun so there is a chance to
register custom store implementations.

Fixes #1045

Signed-off-by: Alex Ullrich <[email protected]>
  • Loading branch information
AlexCuse authored Mar 8, 2022
1 parent 6bcd8b5 commit c4fff4f
Show file tree
Hide file tree
Showing 20 changed files with 338 additions and 280 deletions.
33 changes: 2 additions & 31 deletions internal/app/configupdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@
package app

import (
"context"
"fmt"
"reflect"
"strings"
"sync"
"time"

"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/config"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/container"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/handlers"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/config"
)

// ConfigUpdateProcessor contains the data need to process configuration updates
Expand Down Expand Up @@ -117,21 +110,12 @@ func (processor *ConfigUpdateProcessor) processConfigChangedStoreForwardEnabled(
storeClient := container.StoreClientFrom(sdk.dic.Get)
// StoreClient must be set up for StoreAndForward
if storeClient == nil {
var err error
startupTimer := startup.NewStartUpTimer(sdk.serviceKey)
secretProvider := bootstrapContainer.SecretProviderFrom(sdk.dic.Get)
storeClient, err = handlers.InitializeStoreClient(secretProvider, sdk.config, startupTimer, sdk.LoggingClient())
err := initializeStoreClient(sdk.config, sdk)
if err != nil {
// Error already logged
sdk.config.Writable.StoreAndForward.Enabled = false
return
}

sdk.dic.Update(di.ServiceConstructorMap{
container.StoreClientName: func(get di.Get) interface{} {
return storeClient
},
})
}

sdk.startStoreForward()
Expand Down Expand Up @@ -163,19 +147,6 @@ func (processor *ConfigUpdateProcessor) processConfigChangedPipeline() {
}
}

func (svc *Service) startStoreForward() {
var storeForwardEnabledCtx context.Context
svc.ctx.storeForwardWg = &sync.WaitGroup{}
storeForwardEnabledCtx, svc.ctx.storeForwardCancelCtx = context.WithCancel(context.Background())
svc.runtime.StartStoreAndForward(svc.ctx.appWg, svc.ctx.appCtx, svc.ctx.storeForwardWg, storeForwardEnabledCtx, svc.serviceKey)
}

func (svc *Service) stopStoreForward() {
svc.LoggingClient().Info("Canceling Store and Forward retry loop")
svc.ctx.storeForwardCancelCtx()
svc.ctx.storeForwardWg.Wait()
}

func (svc *Service) findMatchingFunction(configurable reflect.Value, functionName string) (reflect.Value, reflect.Type, error) {
var functionValue reflect.Value
count := configurable.Type().NumMethod()
Expand Down
46 changes: 28 additions & 18 deletions internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v2/config"
nethttp "net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -76,23 +77,24 @@ func NewService(serviceKey string, targetType interface{}, profileSuffixPlacehol
// Service provides the necessary struct and functions to create an instance of the
// interfaces.ApplicationService interface.
type Service struct {
dic *di.Container
serviceKey string
targetType interface{}
config *common.ConfigurationStruct
lc logger.LoggingClient
usingConfigurablePipeline bool
runtime *runtime.GolangRuntime
webserver *webserver.WebServer
ctx contextGroup
deferredFunctions []bootstrap.Deferred
backgroundPublishChannel <-chan interfaces.BackgroundMessage
customTriggerFactories map[string]func(sdk *Service) (interfaces.Trigger, error)
profileSuffixPlaceholder string
commandLine commandLineFlags
flags *flags.Default
configProcessor *config.Processor
requestTimeout time.Duration
dic *di.Container
serviceKey string
targetType interface{}
config *common.ConfigurationStruct
lc logger.LoggingClient
usingConfigurablePipeline bool
runtime *runtime.GolangRuntime
webserver *webserver.WebServer
ctx contextGroup
deferredFunctions []bootstrap.Deferred
backgroundPublishChannel <-chan interfaces.BackgroundMessage
customTriggerFactories map[string]func(sdk *Service) (interfaces.Trigger, error)
customStoreClientFactories map[string]func(db interfaces.DatabaseInfo, cred bootstrapConfig.Credentials) (interfaces.StoreClient, error)
profileSuffixPlaceholder string
commandLine commandLineFlags
flags *flags.Default
configProcessor *config.Processor
requestTimeout time.Duration
}

type commandLineFlags struct {
Expand Down Expand Up @@ -160,6 +162,14 @@ func (svc *Service) MakeItStop() {
// configuration. It will also configure the webserver and start listening on
// the specified port.
func (svc *Service) MakeItRun() error {

config := container.ConfigurationFrom(svc.dic.Get)

err := initializeStoreClient(config, svc)
if err != nil {
return err
}

runCtx, stop := context.WithCancel(context.Background())

svc.ctx.stop = stop
Expand Down Expand Up @@ -485,6 +495,7 @@ func (svc *Service) Initialize() error {
svc.lc.Info(fmt.Sprintf("Starting %s %s ", svc.serviceKey, internal.ApplicationVersion))

svc.config = &common.ConfigurationStruct{}

svc.dic = di.NewContainer(di.ServiceConstructorMap{
container.ConfigurationName: func(get di.Get) interface{} {
return svc.config
Expand Down Expand Up @@ -516,7 +527,6 @@ func (svc *Service) Initialize() error {
svc.dic,
true,
[]bootstrapInterfaces.BootstrapHandler{
handlers.NewDatabase().BootstrapHandler,
bootstrapHandlers.NewClientsBootstrap().BootstrapHandler,
handlers.NewTelemetry().BootstrapHandler,
handlers.NewVersionValidator(svc.commandLine.skipVersionCheck, internal.SDKVersion).BootstrapHandler,
Expand Down
126 changes: 126 additions & 0 deletions internal/app/storeforward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// Copyright (c) 2021 One Track Consulting
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"context"
"fmt"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/container"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/store/db"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/store/db/redis"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/environment"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/secret"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"strings"
"sync"
"time"
)

// RegisterCustomStoreFactory allows registration of alternative storage implementation to back the Store&Forward loop
func (svc *Service) RegisterCustomStoreFactory(name string, factory func(cfg interfaces.DatabaseInfo, cred bootstrapConfig.Credentials) (interfaces.StoreClient, error)) error {
if name == db.RedisDB {
return fmt.Errorf("cannot register factory for reserved name %q", name)
}

if svc.customStoreClientFactories == nil {
svc.customStoreClientFactories = make(map[string]func(db interfaces.DatabaseInfo, cred bootstrapConfig.Credentials) (interfaces.StoreClient, error), 1)
}

svc.customStoreClientFactories[strings.ToUpper(name)] = factory

return nil
}

func (svc *Service) createStoreClient(database interfaces.DatabaseInfo, credentials bootstrapConfig.Credentials) (interfaces.StoreClient, error) {
switch strings.ToLower(database.Type) {
case db.RedisDB:
return redis.NewClient(database, credentials)
default:
if factory, found := svc.customStoreClientFactories[strings.ToUpper(database.Type)]; found {
return factory(database, credentials)
}
return nil, db.ErrUnsupportedDatabase
}
}

func (svc *Service) startStoreForward() {
var storeForwardEnabledCtx context.Context
svc.ctx.storeForwardWg = &sync.WaitGroup{}
storeForwardEnabledCtx, svc.ctx.storeForwardCancelCtx = context.WithCancel(context.Background())
svc.runtime.StartStoreAndForward(svc.ctx.appWg, svc.ctx.appCtx, svc.ctx.storeForwardWg, storeForwardEnabledCtx, svc.serviceKey)
}

func (svc *Service) stopStoreForward() {
svc.LoggingClient().Info("Canceling Store and Forward retry loop")
svc.ctx.storeForwardCancelCtx()
svc.ctx.storeForwardWg.Wait()
}

func initializeStoreClient(config *common.ConfigurationStruct, svc *Service) error {
// Only need the database client if Store and Forward is enabled
if !config.Writable.StoreAndForward.Enabled {
svc.dic.Update(di.ServiceConstructorMap{
container.StoreClientName: func(get di.Get) interface{} {
return nil
},
})
return nil
}

logger := bootstrapContainer.LoggingClientFrom(svc.dic.Get)
secretProvider := bootstrapContainer.SecretProviderFrom(svc.dic.Get)

var err error

secrets, err := secretProvider.GetSecret(config.Database.Type)
if err != nil {
return fmt.Errorf("unable to get Database Credentials for Store and Forward: %s", err.Error())
}

credentials := bootstrapConfig.Credentials{
Username: secrets[secret.UsernameKey],
Password: secrets[secret.PasswordKey],
}

startup := environment.GetStartupInfo(svc.serviceKey)

tryUntil := time.Now().Add(time.Duration(startup.Duration) * time.Second)

var storeClient interfaces.StoreClient
for time.Now().Before(tryUntil) {
if storeClient, err = svc.createStoreClient(config.Database, credentials); err != nil {
logger.Warnf("unable to initialize Database '%s' for Store and Forward: %s", config.Database.Type, err.Error())
time.Sleep(time.Duration(startup.Interval) * time.Second)
continue
}
break
}

if err != nil {
return fmt.Errorf("initialize Database for Store and Forward failed: %s", err.Error())
}

svc.dic.Update(di.ServiceConstructorMap{
container.StoreClientName: func(get di.Get) interface{} {
return storeClient
},
})
return nil
}
93 changes: 93 additions & 0 deletions internal/app/storeforward_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// Copyright (c) 2022 One Track Consulting
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/store/db"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/store/db/redis"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"testing"
)

func Test_service_RegisterCustomStoreFactory_Inbuilt_Name(t *testing.T) {
sut := &Service{}

err := sut.RegisterCustomStoreFactory(db.RedisDB, nil)

require.Error(t, err)
}

func Test_service_RegisterCustomStoreFactory(t *testing.T) {
sut := &Service{}

f := func(info interfaces.DatabaseInfo, credentials config.Credentials) (interfaces.StoreClient, error) {
return nil, nil
}

name := uuid.NewString()

err := sut.RegisterCustomStoreFactory(name, f)

assert.NoError(t, err)

_, found := sut.customStoreClientFactories[strings.ToUpper(name)]
assert.True(t, found)
}

func Test_service_NewStoreClient_Invalid(t *testing.T) {
sut := &Service{}

_, err := sut.createStoreClient(interfaces.DatabaseInfo{}, config.Credentials{})

assert.Error(t, err)
}

func Test_service_NewStoreClient_Redis(t *testing.T) {
sut := &Service{customStoreClientFactories: map[string]func(db interfaces.DatabaseInfo, cred config.Credentials) (interfaces.StoreClient, error){}}

result, err := sut.createStoreClient(interfaces.DatabaseInfo{Type: db.RedisDB, Timeout: "5s"}, config.Credentials{})

assert.NoError(t, err)
c, ok := result.(*redis.Client)
assert.NotNil(t, c)
assert.True(t, ok)
}

func Test_service_NewStoreClient_Custom(t *testing.T) {
var x interfaces.StoreClient

sut := &Service{}

f := func(info interfaces.DatabaseInfo, credentials config.Credentials) (interfaces.StoreClient, error) {
return x, nil
}

name := uuid.NewString()

err := sut.RegisterCustomStoreFactory(name, f)

require.NoError(t, err)

result, err := sut.createStoreClient(interfaces.DatabaseInfo{Type: name}, config.Credentials{})

assert.NoError(t, err)
assert.Equal(t, x, result)
}
2 changes: 1 addition & 1 deletion internal/bootstrap/container/storeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package container

import (
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/store/db/interfaces"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"

"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
)
Expand Down
Loading

0 comments on commit c4fff4f

Please sign in to comment.