Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- (Bugfix) Fix Satellite collections in Agency
- (Bugfix) Fix backup creation timeout
- (Bugfix) ArangoSync port fix
- (Bugfix) Fix GetClient lock system

## [1.2.9](https://github.com/arangodb/kube-arangodb/tree/1.2.9) (2022-03-30)
- (Feature) Improve Kubernetes clientsets management
Expand Down
49 changes: 17 additions & 32 deletions pkg/deployment/client/client_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ import (
"strconv"
"sync"

"github.com/arangodb/kube-arangodb/pkg/util/errors"

"github.com/arangodb/go-driver/agency"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"

driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/agency"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/deployment/reconciler"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)

type Cache interface {
Expand Down Expand Up @@ -81,6 +79,8 @@ func (cc *cache) extendHost(host string) string {
}

func (cc *cache) getClient(group api.ServerGroup, id string) (driver.Client, error) {
cc.mutex.Lock()
defer cc.mutex.Unlock()
m, _, _ := cc.in.GetStatusSnapshot().Members.ElementByID(id)

endpoint, err := cc.in.GenerateMemberEndpoint(group, m)
Expand All @@ -95,7 +95,9 @@ func (cc *cache) getClient(group api.ServerGroup, id string) (driver.Client, err
return c, nil
}

func (cc *cache) get(ctx context.Context, group api.ServerGroup, id string) (driver.Client, error) {
// Get a cached client for the given ID in the given group, creating one
// if needed.
func (cc *cache) Get(ctx context.Context, group api.ServerGroup, id string) (driver.Client, error) {
client, err := cc.getClient(group, id)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -110,28 +112,24 @@ func (cc *cache) get(ctx context.Context, group api.ServerGroup, id string) (dri
}
}

// Get a cached client for the given ID in the given group, creating one
// if needed.
func (cc *cache) Get(ctx context.Context, group api.ServerGroup, id string) (driver.Client, error) {
cc.mutex.Lock()
defer cc.mutex.Unlock()

return cc.get(ctx, group, id)
}

func (cc *cache) GetAuth() conn.Auth {
return cc.factory.GetAuth()
}

func (cc *cache) getDatabaseClient() (driver.Client, error) {
cc.mutex.Lock()
defer cc.mutex.Unlock()

c, err := cc.factory.Client(cc.extendHost(k8sutil.CreateDatabaseClientServiceDNSName(cc.in.GetAPIObject())))
if err != nil {
return nil, errors.WithStack(err)
}
return c, nil
}

func (cc *cache) getDatabase(ctx context.Context) (driver.Client, error) {
// GetDatabase returns a cached client for the entire database (cluster coordinators or single server),
// creating one if needed.
func (cc *cache) GetDatabase(ctx context.Context) (driver.Client, error) {
client, err := cc.getDatabaseClient()
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -146,16 +144,11 @@ func (cc *cache) getDatabase(ctx context.Context) (driver.Client, error) {
}
}

// GetDatabase returns a cached client for the entire database (cluster coordinators or single server),
// creating one if needed.
func (cc *cache) GetDatabase(ctx context.Context) (driver.Client, error) {
// GetAgency returns a cached client for the agency
func (cc *cache) GetAgency(ctx context.Context) (agency.Agency, error) {
cc.mutex.Lock()
defer cc.mutex.Unlock()

return cc.getDatabase(ctx)
}

func (cc *cache) getAgencyClient() (agency.Agency, error) {
// Not found, create a new client
var dnsNames []string
for _, m := range cc.in.GetStatusSnapshot().Members.Agents {
Expand All @@ -177,11 +170,3 @@ func (cc *cache) getAgencyClient() (agency.Agency, error) {
}
return c, nil
}

// GetDatabase returns a cached client for the agency
func (cc *cache) GetAgency(ctx context.Context) (agency.Agency, error) {
cc.mutex.Lock()
defer cc.mutex.Unlock()

return cc.getAgencyClient()
}