1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #1206 from jimmycmh/master

Remove Lock in datastore for global scope
This commit is contained in:
Jana Radhakrishnan 2016-06-24 06:38:05 -07:00 committed by GitHub
commit d82e80e221

View file

@ -54,11 +54,12 @@ var (
) )
type datastore struct { type datastore struct {
scope string scope string
store store.Store store store.Store
cache *cache cache *cache
watchCh chan struct{} watchCh chan struct{}
active bool active bool
sequential bool
sync.Mutex sync.Mutex
} }
@ -190,6 +191,10 @@ func newClient(scope string, kv string, addr string, config *store.Config, cache
if cached && scope != LocalScope { if cached && scope != LocalScope {
return nil, fmt.Errorf("caching supported only for scope %s", LocalScope) return nil, fmt.Errorf("caching supported only for scope %s", LocalScope)
} }
sequential := false
if scope == LocalScope {
sequential = true
}
if config == nil { if config == nil {
config = &store.Config{} config = &store.Config{}
@ -216,7 +221,7 @@ func newClient(scope string, kv string, addr string, config *store.Config, cache
return nil, err return nil, err
} }
ds := &datastore{scope: scope, store: store, active: true, watchCh: make(chan struct{})} ds := &datastore{scope: scope, store: store, active: true, watchCh: make(chan struct{}), sequential: sequential}
if cached { if cached {
ds.cache = newCache(ds) ds.cache = newCache(ds)
} }
@ -375,8 +380,10 @@ func (ds *datastore) PutObjectAtomic(kvObject KVObject) error {
pair *store.KVPair pair *store.KVPair
err error err error
) )
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
if kvObject == nil { if kvObject == nil {
return types.BadRequestErrorf("invalid KV Object : nil") return types.BadRequestErrorf("invalid KV Object : nil")
@ -420,8 +427,10 @@ add_cache:
// PutObject adds a new Record based on an object into the datastore // PutObject adds a new Record based on an object into the datastore
func (ds *datastore) PutObject(kvObject KVObject) error { func (ds *datastore) PutObject(kvObject KVObject) error {
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
if kvObject == nil { if kvObject == nil {
return types.BadRequestErrorf("invalid KV Object : nil") return types.BadRequestErrorf("invalid KV Object : nil")
@ -456,8 +465,10 @@ func (ds *datastore) putObjectWithKey(kvObject KVObject, key ...string) error {
// GetObject returns a record matching the key // GetObject returns a record matching the key
func (ds *datastore) GetObject(key string, o KVObject) error { func (ds *datastore) GetObject(key string, o KVObject) error {
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
if ds.cache != nil { if ds.cache != nil {
return ds.cache.get(key, o) return ds.cache.get(key, o)
@ -490,8 +501,10 @@ func (ds *datastore) ensureParent(parent string) error {
} }
func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) { func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
if ds.cache != nil { if ds.cache != nil {
return ds.cache.list(kvObject) return ds.cache.list(kvObject)
@ -536,8 +549,10 @@ func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
// DeleteObject unconditionally deletes a record from the store // DeleteObject unconditionally deletes a record from the store
func (ds *datastore) DeleteObject(kvObject KVObject) error { func (ds *datastore) DeleteObject(kvObject KVObject) error {
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
// cleaup the cache first // cleaup the cache first
if ds.cache != nil { if ds.cache != nil {
@ -555,8 +570,10 @@ func (ds *datastore) DeleteObject(kvObject KVObject) error {
// DeleteObjectAtomic performs atomic delete on a record // DeleteObjectAtomic performs atomic delete on a record
func (ds *datastore) DeleteObjectAtomic(kvObject KVObject) error { func (ds *datastore) DeleteObjectAtomic(kvObject KVObject) error {
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
if kvObject == nil { if kvObject == nil {
return types.BadRequestErrorf("invalid KV Object : nil") return types.BadRequestErrorf("invalid KV Object : nil")
@ -588,8 +605,10 @@ del_cache:
// DeleteTree unconditionally deletes a record from the store // DeleteTree unconditionally deletes a record from the store
func (ds *datastore) DeleteTree(kvObject KVObject) error { func (ds *datastore) DeleteTree(kvObject KVObject) error {
ds.Lock() if ds.sequential {
defer ds.Unlock() ds.Lock()
defer ds.Unlock()
}
// cleaup the cache first // cleaup the cache first
if ds.cache != nil { if ds.cache != nil {