Skip to content

Commit

Permalink
Merge pull request istio-ecosystem#122 from services-mesh/concurrent_…
Browse files Browse the repository at this point in the history
…map_fix

fixed concurrent map issue
  • Loading branch information
Jimin authored and GitHub Enterprise committed Jun 8, 2022
2 parents 54a6c4e + 4153b5d commit 5f0879e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
14 changes: 9 additions & 5 deletions admiral/pkg/controller/admiral/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package admiral

import (
"fmt"
"time"

"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
"time"

"sync"

k8sV1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"sync"
)

// Handler interface contains the methods that are required
Expand All @@ -34,7 +36,7 @@ type ServiceController struct {
type serviceCache struct {
//map of dependencies key=identity value array of onboarded identities
cache map[string]*ServiceClusterEntry
mutex *sync.Mutex
mutex *sync.RWMutex
}

func (s *serviceCache) Put(service *k8sV1.Service) {
Expand Down Expand Up @@ -69,6 +71,8 @@ func (s *serviceCache) getKey(service *k8sV1.Service) string {
}

func (s *serviceCache) Get(key string) *ServiceClusterEntry {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.cache[key]
}

Expand Down Expand Up @@ -117,7 +121,7 @@ func NewServiceController(stopCh <-chan struct{}, handler ServiceHandler, config

podCache := serviceCache{}
podCache.cache = make(map[string]*ServiceClusterEntry)
podCache.mutex = &sync.Mutex{}
podCache.mutex = &sync.RWMutex{}

serviceController.Cache = &podCache
var err error
Expand All @@ -139,7 +143,7 @@ func NewServiceController(stopCh <-chan struct{}, handler ServiceHandler, config
&k8sV1.Service{}, resyncPeriod, cache.Indexers{},
)

NewController("service-ctrl-" + config.Host , stopCh, &serviceController, serviceController.informer)
NewController("service-ctrl-"+config.Host, stopCh, &serviceController, serviceController.informer)

return &serviceController, nil
}
Expand Down
63 changes: 57 additions & 6 deletions admiral/pkg/controller/admiral/service_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package admiral

import (
"context"
"sync"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
"k8s.io/api/core/v1"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/tools/clientcmd"
"sync"
"testing"
"time"
)

func TestNewServiceController(t *testing.T) {
Expand All @@ -35,7 +40,7 @@ func TestNewServiceController(t *testing.T) {
func TestServiceCache_Put(t *testing.T) {
serviceCache := serviceCache{}
serviceCache.cache = make(map[string]*ServiceClusterEntry)
serviceCache.mutex = &sync.Mutex{}
serviceCache.mutex = &sync.RWMutex{}

//test service cache empty with admiral ignore should skip to save in cache

Expand Down Expand Up @@ -88,7 +93,7 @@ func TestServiceCache_Put(t *testing.T) {
func TestServiceCache_GetLoadBalancer(t *testing.T) {
sc := serviceCache{}
sc.cache = make(map[string]*ServiceClusterEntry)
sc.mutex = &sync.Mutex{}
sc.mutex = &sync.RWMutex{}

service := &v1.Service{}
service.Name = "test-service"
Expand Down Expand Up @@ -237,3 +242,49 @@ func TestServiceCache_GetLoadBalancer(t *testing.T) {
})
}
}

func TestConcurrentGetAndPut(t *testing.T) {
serviceCache := serviceCache{}
serviceCache.cache = make(map[string]*ServiceClusterEntry)
serviceCache.mutex = &sync.RWMutex{}

serviceCache.Put(&v1.Service{
ObjectMeta: metaV1.ObjectMeta{Name: "testname", Namespace: "testns"},
})

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(3*time.Second))
defer cancel()

var wg sync.WaitGroup
wg.Add(2)
// Producer go routine
go func(ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
serviceCache.Put(&v1.Service{
ObjectMeta: metaV1.ObjectMeta{Name: "testname", Namespace: string(uuid.NewUUID())},
})
}
}
}(ctx)

// Consumer go routine
go func(ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
assert.NotNil(t, serviceCache.Get("testns"))
}
}
}(ctx)

wg.Wait()

}

0 comments on commit 5f0879e

Please sign in to comment.