Skip to content

Commit

Permalink
use separate powerflex clients
Browse files Browse the repository at this point in the history
  • Loading branch information
atye committed Feb 14, 2023
1 parent 310b2a3 commit 3bfc34d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 87 deletions.
52 changes: 0 additions & 52 deletions internal/powerflex/client.go

This file was deleted.

12 changes: 3 additions & 9 deletions internal/powerflex/storage_pool_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ import (
"fmt"
"sync"

types "github.com/dell/goscaleio/types/v1"
"github.com/dell/goscaleio"
lru "github.com/hashicorp/golang-lru"
"go.opentelemetry.io/otel/trace"
)

// StoragePoolClient abstracts the PowerFlex calls required for the StoragePoolCache
type StoragePoolClient interface {
SetToken(token string)
FindStoragePool(id string, name string, href string, protectionDomain string) (*types.StoragePool, error)
}

// StoragePoolCache is a least recently used cache of PowerFlex storage pool names
type StoragePoolCache struct {
client StoragePoolClient
client *goscaleio.Client
cache *lru.Cache
mu sync.Mutex
}

// NewStoragePoolCache creates a new StoragePoolCache
// It requires a goscaleio client and a cache size
func NewStoragePoolCache(client StoragePoolClient, cacheSize int) (*StoragePoolCache, error) {
func NewStoragePoolCache(client *goscaleio.Client, cacheSize int) (*StoragePoolCache, error) {
if client == nil {
return nil, fmt.Errorf("goscaleio client is required")
}
Expand Down
17 changes: 6 additions & 11 deletions internal/powerflex/storage_pool_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func TestStoragePoolCache_GetStoragePoolNameByID(t *testing.T) {
})
defer powerFlexSvr.Close()

pfClient := newPowerFlexClient(t, powerFlexSvr.URL)
client := powerflex.NewClient(pfClient)
client := newPowerFlexClient(t, powerFlexSvr.URL)
tk := newTokenGetter(t, client, powerFlexSvr.URL)

// Create a new storage pool cache pointing to the httptest server PowerFlex
Expand Down Expand Up @@ -120,8 +119,7 @@ func TestStoragePoolCache_GetStoragePoolNameByID(t *testing.T) {
})
defer powerFlexSvr.Close()

pfClient := newPowerFlexClient(t, powerFlexSvr.URL)
client := powerflex.NewClient(pfClient)
client := newPowerFlexClient(t, powerFlexSvr.URL)
tk := newTokenGetter(t, client, powerFlexSvr.URL)

// Create a new storage pool cache pointing to the httptest server PowerFlex
Expand Down Expand Up @@ -182,8 +180,7 @@ func TestStoragePoolCache_GetStoragePoolNameByID(t *testing.T) {
})
defer powerFlexSvr.Close()

pfClient := newPowerFlexClient(t, powerFlexSvr.URL)
client := powerflex.NewClient(pfClient)
client := newPowerFlexClient(t, powerFlexSvr.URL)
tk := newTokenGetter(t, client, powerFlexSvr.URL)

// Create a new storage pool cache pointing to the httptest server PowerFlex
Expand Down Expand Up @@ -270,8 +267,7 @@ func TestStoragePoolCache_GetStoragePoolNameByID(t *testing.T) {
})
defer powerFlexSvr.Close()

pfClient := newPowerFlexClient(t, powerFlexSvr.URL)
client := powerflex.NewClient(pfClient)
client := newPowerFlexClient(t, powerFlexSvr.URL)

// Attempt to create new storage pool with cache size
_, gotErr := powerflex.NewStoragePoolCache(client, 0)
Expand Down Expand Up @@ -311,8 +307,7 @@ func TestStoragePoolCache_GetStoragePoolNameByID(t *testing.T) {
})
defer powerFlexSvr.Close()

pfClient := newPowerFlexClient(t, powerFlexSvr.URL)
client := powerflex.NewClient(pfClient)
client := newPowerFlexClient(t, powerFlexSvr.URL)
tk := newTokenGetter(t, client, powerFlexSvr.URL)

// Create a new storage pool cache pointing to the httptest server PowerFlex
Expand Down Expand Up @@ -380,7 +375,7 @@ func newPowerFlexClient(t *testing.T, addr string) *goscaleio.Client {
return client
}

func newTokenGetter(t *testing.T, client powerflex.TokenClient, addr string) *powerflex.TokenGetter {
func newTokenGetter(t *testing.T, client *goscaleio.Client, addr string) *powerflex.TokenGetter {
return powerflex.NewTokenGetter(powerflex.Config{
PowerFlexClient: client,
TokenRefreshInterval: 5 * time.Minute,
Expand Down
9 changes: 1 addition & 8 deletions internal/powerflex/token_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ type TokenGetter struct {
currentToken string
}

// TokenClient abstracts the PowerFlex calls required for the TokenGetter
type TokenClient interface {
Authenticate(configConnect *goscaleio.ConfigConnect) (goscaleio.Cluster, error)
GetToken() string
SetToken(token string)
}

// Config is the configuration for building a PowerFlexTokenGetter
type Config struct {
PowerFlexClient TokenClient
PowerFlexClient *goscaleio.Client
TokenRefreshInterval time.Duration
ConfigConnect *goscaleio.ConfigConnect
Logger *logrus.Entry
Expand Down
6 changes: 3 additions & 3 deletions internal/powerflex/token_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestLogin_GetToken(t *testing.T) {
// Create a new TokenGetter pointing to the httptest server PowerFlex
// TokenRefreshInterval shouldn't be relevant in this test case
config := powerflex.Config{
PowerFlexClient: powerflex.NewClient(newPowerFlexClient(t, powerFlexSvr.URL)),
PowerFlexClient: newPowerFlexClient(t, powerFlexSvr.URL),
TokenRefreshInterval: time.Minute,
Logger: logrus.WithTime(time.Now()),
ConfigConnect: &goscaleio.ConfigConnect{
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestLogin_GetToken(t *testing.T) {

// Create a new TokenGetter pointing to the httptest server PowerFlex
config := powerflex.Config{
PowerFlexClient: powerflex.NewClient(newPowerFlexClient(t, powerFlexSvr.URL)),
PowerFlexClient: newPowerFlexClient(t, powerFlexSvr.URL),
TokenRefreshInterval: time.Second,
Logger: logrus.WithTime(time.Now()),
ConfigConnect: &goscaleio.ConfigConnect{
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestLogin_GetToken(t *testing.T) {

// Create a new TokenGetter pointing to the httptest server PowerFlex
config := powerflex.Config{
PowerFlexClient: powerflex.NewClient(newPowerFlexClient(t, powerFlexSvr.URL)),
PowerFlexClient: newPowerFlexClient(t, powerFlexSvr.URL),
TokenRefreshInterval: time.Second,
Logger: logrus.WithTime(time.Now()),
ConfigConnect: &goscaleio.ConfigConnect{
Expand Down
16 changes: 12 additions & 4 deletions internal/proxy/powerflex_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,28 @@ func buildSystem(ctx context.Context, e SystemEntry, log *logrus.Entry) (*System
if err != nil {
return nil, err
}
c, err := goscaleio.NewClientWithArgs(tgt.String(), "", true, false)

// Cannot use the same powerflex client for the storage pool cache and
// the token getter because of data races with concurrent usage so we
// create a powerflex client for each

spCacheClient, err := goscaleio.NewClientWithArgs(tgt.String(), "", true, false)
if err != nil {
return nil, err
}

client := powerflex.NewClient(c)
tgClient, err := goscaleio.NewClientWithArgs(tgt.String(), "", true, false)
if err != nil {
return nil, err
}

spc, err := powerflex.NewStoragePoolCache(client, 100)
spc, err := powerflex.NewStoragePoolCache(spCacheClient, 100)
if err != nil {
return nil, err
}

tk := powerflex.NewTokenGetter(powerflex.Config{
PowerFlexClient: client,
PowerFlexClient: tgClient,
TokenRefreshInterval: 5 * time.Minute,
ConfigConnect: &goscaleio.ConfigConnect{
Endpoint: e.Endpoint,
Expand Down

0 comments on commit 3bfc34d

Please sign in to comment.