From cca40042c500fc479d6db750600f58a8dff39b7d Mon Sep 17 00:00:00 2001 From: Yohei Ueda Date: Thu, 26 Jul 2018 17:13:33 +0900 Subject: [PATCH] [FAB-11321] Alleviating lock contention of MSP cache This patch replaces LRU in MSP cache with a second-chance alogorithm, an approximate LRU algorithm, in order to remove mutex locks. With the second chance algorithm, we can use RW locks to guard cache items for cucurrent accesses, so this change significantly reducdes lock contention when TPS is high. Change-Id: Ic21873596ab83c5605f41e7a14987e586d970b63 Signed-off-by: Yohei Ueda --- Gopkg.lock | 9 - Gopkg.toml | 4 - msp/cache/cache.go | 55 ++--- msp/cache/cache_test.go | 18 +- msp/cache/second_chance.go | 114 +++++++++++ msp/cache/second_chance_test.go | 109 ++++++++++ vendor/github.com/golang/groupcache/LICENSE | 191 ------------------ .../github.com/golang/groupcache/lru/lru.go | 133 ------------ 8 files changed, 249 insertions(+), 384 deletions(-) create mode 100644 msp/cache/second_chance.go create mode 100644 msp/cache/second_chance_test.go delete mode 100644 vendor/github.com/golang/groupcache/LICENSE delete mode 100644 vendor/github.com/golang/groupcache/lru/lru.go diff --git a/Gopkg.lock b/Gopkg.lock index 1a576ea25ed..7830f1030c0 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -219,14 +219,6 @@ revision = "1adfc126b41513cc696b209667c8656ea7aac67c" version = "v1.0.0" -[[projects]] - branch = "master" - digest = "1:3fb07f8e222402962fa190eb060608b34eddfb64562a18e2167df2de0ece85d8" - name = "github.com/golang/groupcache" - packages = ["lru"] - pruneopts = "NUT" - revision = "66deaeb636dff1ac7d938ce666d090556056a4b0" - [[projects]] digest = "1:6f8bed5a0e510c667026ac9b89371d833007ed373b6e95656acf90324209f545" name = "github.com/golang/protobuf" @@ -854,7 +846,6 @@ "github.com/coreos/etcd/raft", "github.com/davecgh/go-spew/spew", "github.com/fsouza/go-dockerclient", - "github.com/golang/groupcache/lru", "github.com/golang/protobuf/jsonpb", "github.com/golang/protobuf/proto", "github.com/golang/protobuf/protoc-gen-go", diff --git a/Gopkg.toml b/Gopkg.toml index 2c51b802f90..9f75a60294b 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -36,10 +36,6 @@ noverify = [ name = "github.com/fsouza/go-dockerclient" version = "1.2.0" -[[constraint]] - branch = "master" - name = "github.com/golang/groupcache" - [[constraint]] name = "github.com/golang/protobuf" version = "1.1.0" diff --git a/msp/cache/cache.go b/msp/cache/cache.go index 3e036f798e8..5505dfde8a5 100644 --- a/msp/cache/cache.go +++ b/msp/cache/cache.go @@ -7,13 +7,10 @@ SPDX-License-Identifier: Apache-2.0 package cache import ( - "fmt" - "sync" - - "github.com/golang/groupcache/lru" "github.com/hyperledger/fabric/common/flogging" "github.com/hyperledger/fabric/msp" pmsp "github.com/hyperledger/fabric/protos/msp" + "github.com/pkg/errors" ) const ( @@ -27,13 +24,13 @@ var mspLogger = flogging.MustGetLogger("msp") func New(o msp.MSP) (msp.MSP, error) { mspLogger.Debugf("Creating Cache-MSP instance") if o == nil { - return nil, fmt.Errorf("Invalid passed MSP. It must be different from nil.") + return nil, errors.Errorf("Invalid passed MSP. It must be different from nil.") } theMsp := &cachedMSP{MSP: o} - theMsp.deserializeIdentityCache = lru.New(deserializeIdentityCacheSize) - theMsp.satisfiesPrincipalCache = lru.New(satisfiesPrincipalCacheSize) - theMsp.validateIdentityCache = lru.New(validateIdentityCacheSize) + theMsp.deserializeIdentityCache = newSecondChanceCache(deserializeIdentityCacheSize) + theMsp.satisfiesPrincipalCache = newSecondChanceCache(satisfiesPrincipalCacheSize) + theMsp.validateIdentityCache = newSecondChanceCache(validateIdentityCacheSize) return theMsp, nil } @@ -42,20 +39,14 @@ type cachedMSP struct { msp.MSP // cache for DeserializeIdentity. - deserializeIdentityCache *lru.Cache - - dicMutex sync.Mutex // synchronize access to cache + deserializeIdentityCache *secondChanceCache // cache for validateIdentity - validateIdentityCache *lru.Cache - - vicMutex sync.Mutex // synchronize access to cache + validateIdentityCache *secondChanceCache // basically a map of principals=>identities=>stringified to booleans // specifying whether this identity satisfies this principal - satisfiesPrincipalCache *lru.Cache - - spcMutex sync.Mutex // synchronize access to cache + satisfiesPrincipalCache *secondChanceCache } type cachedIdentity struct { @@ -72,9 +63,7 @@ func (id *cachedIdentity) Validate() error { } func (c *cachedMSP) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) { - c.dicMutex.Lock() - id, ok := c.deserializeIdentityCache.Get(string(serializedIdentity)) - c.dicMutex.Unlock() + id, ok := c.deserializeIdentityCache.get(string(serializedIdentity)) if ok { return &cachedIdentity{ cache: c, @@ -84,9 +73,7 @@ func (c *cachedMSP) DeserializeIdentity(serializedIdentity []byte) (msp.Identity id, err := c.MSP.DeserializeIdentity(serializedIdentity) if err == nil { - c.dicMutex.Lock() - defer c.dicMutex.Unlock() - c.deserializeIdentityCache.Add(string(serializedIdentity), id) + c.deserializeIdentityCache.add(string(serializedIdentity), id) return &cachedIdentity{ cache: c, Identity: id.(msp.Identity), @@ -105,9 +92,7 @@ func (c *cachedMSP) Validate(id msp.Identity) error { identifier := id.GetIdentifier() key := string(identifier.Mspid + ":" + identifier.Id) - c.vicMutex.Lock() - _, ok := c.validateIdentityCache.Get(key) - c.vicMutex.Unlock() + _, ok := c.validateIdentityCache.get(key) if ok { // cache only stores if the identity is valid. return nil @@ -115,9 +100,7 @@ func (c *cachedMSP) Validate(id msp.Identity) error { err := c.MSP.Validate(id) if err == nil { - c.vicMutex.Lock() - defer c.vicMutex.Unlock() - c.validateIdentityCache.Add(key, true) + c.validateIdentityCache.add(key, true) } return err @@ -129,9 +112,7 @@ func (c *cachedMSP) SatisfiesPrincipal(id msp.Identity, principal *pmsp.MSPPrinc principalKey := string(principal.PrincipalClassification) + string(principal.Principal) key := identityKey + principalKey - c.spcMutex.Lock() - v, ok := c.satisfiesPrincipalCache.Get(key) - c.spcMutex.Unlock() + v, ok := c.satisfiesPrincipalCache.get(key) if ok { if v == nil { return nil @@ -142,16 +123,14 @@ func (c *cachedMSP) SatisfiesPrincipal(id msp.Identity, principal *pmsp.MSPPrinc err := c.MSP.SatisfiesPrincipal(id, principal) - c.spcMutex.Lock() - defer c.spcMutex.Unlock() - c.satisfiesPrincipalCache.Add(key, err) + c.satisfiesPrincipalCache.add(key, err) return err } func (c *cachedMSP) cleanCash() error { - c.deserializeIdentityCache = lru.New(deserializeIdentityCacheSize) - c.satisfiesPrincipalCache = lru.New(satisfiesPrincipalCacheSize) - c.validateIdentityCache = lru.New(validateIdentityCacheSize) + c.deserializeIdentityCache = newSecondChanceCache(deserializeIdentityCacheSize) + c.satisfiesPrincipalCache = newSecondChanceCache(satisfiesPrincipalCacheSize) + c.validateIdentityCache = newSecondChanceCache(validateIdentityCacheSize) return nil } diff --git a/msp/cache/cache_test.go b/msp/cache/cache_test.go index 0e54793e651..1c933458b17 100644 --- a/msp/cache/cache_test.go +++ b/msp/cache/cache_test.go @@ -38,9 +38,9 @@ func TestSetup(t *testing.T) { err = i.Setup(nil) assert.NoError(t, err) mockMSP.AssertExpectations(t) - assert.Equal(t, 0, i.(*cachedMSP).deserializeIdentityCache.Len()) - assert.Equal(t, 0, i.(*cachedMSP).satisfiesPrincipalCache.Len()) - assert.Equal(t, 0, i.(*cachedMSP).validateIdentityCache.Len()) + assert.Equal(t, 0, i.(*cachedMSP).deserializeIdentityCache.len()) + assert.Equal(t, 0, i.(*cachedMSP).satisfiesPrincipalCache.len()) + assert.Equal(t, 0, i.(*cachedMSP).validateIdentityCache.len()) } func TestGetType(t *testing.T) { @@ -152,7 +152,7 @@ func TestDeserializeIdentity(t *testing.T) { mockMSP.AssertExpectations(t) // Check the cache - _, ok := wrappedMSP.(*cachedMSP).deserializeIdentityCache.Get(string(serializedIdentity)) + _, ok := wrappedMSP.(*cachedMSP).deserializeIdentityCache.get(string(serializedIdentity)) assert.True(t, ok) // Check the same object is returned @@ -170,7 +170,7 @@ func TestDeserializeIdentity(t *testing.T) { assert.Contains(t, err.Error(), "Invalid identity") mockMSP.AssertExpectations(t) - _, ok = wrappedMSP.(*cachedMSP).deserializeIdentityCache.Get(string(serializedIdentity)) + _, ok = wrappedMSP.(*cachedMSP).deserializeIdentityCache.get(string(serializedIdentity)) assert.False(t, ok) } @@ -190,7 +190,7 @@ func TestValidate(t *testing.T) { // Check the cache identifier := mockIdentity.GetIdentifier() key := string(identifier.Mspid + ":" + identifier.Id) - v, ok := i.(*cachedMSP).validateIdentityCache.Get(string(key)) + v, ok := i.(*cachedMSP).validateIdentityCache.get(string(key)) assert.True(t, ok) assert.True(t, v.(bool)) @@ -209,7 +209,7 @@ func TestValidate(t *testing.T) { // Check the cache identifier = mockIdentity.GetIdentifier() key = string(identifier.Mspid + ":" + identifier.Id) - _, ok = i.(*cachedMSP).validateIdentityCache.Get(string(key)) + _, ok = i.(*cachedMSP).validateIdentityCache.get(string(key)) assert.False(t, ok) } @@ -293,7 +293,7 @@ func TestSatisfiesPrincipal(t *testing.T) { identityKey := string(identifier.Mspid + ":" + identifier.Id) principalKey := string(mockMSPPrincipal.PrincipalClassification) + string(mockMSPPrincipal.Principal) key := identityKey + principalKey - v, ok := i.(*cachedMSP).satisfiesPrincipalCache.Get(key) + v, ok := i.(*cachedMSP).satisfiesPrincipalCache.get(key) assert.True(t, ok) assert.Nil(t, v) @@ -316,7 +316,7 @@ func TestSatisfiesPrincipal(t *testing.T) { identityKey = string(identifier.Mspid + ":" + identifier.Id) principalKey = string(mockMSPPrincipal.PrincipalClassification) + string(mockMSPPrincipal.Principal) key = identityKey + principalKey - v, ok = i.(*cachedMSP).satisfiesPrincipalCache.Get(key) + v, ok = i.(*cachedMSP).satisfiesPrincipalCache.get(key) assert.True(t, ok) assert.NotNil(t, v) assert.Contains(t, "Invalid", v.(error).Error()) diff --git a/msp/cache/second_chance.go b/msp/cache/second_chance.go new file mode 100644 index 00000000000..cf7dddec57b --- /dev/null +++ b/msp/cache/second_chance.go @@ -0,0 +1,114 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package cache + +import ( + "sync" + "sync/atomic" +) + +// This package implements Second-Chance Algorithm, an approximate LRU algorithms. +// https://www.cs.jhu.edu/~yairamir/cs418/os6/tsld023.htm + +// secondChanceCache holds key-value items with a limited size. +// When the number cached items exceeds the limit, victims are selected based on the +// Second-Chance Algorithm and get purged +type secondChanceCache struct { + // manages mapping between keys and items + table map[string]*cacheItem + + // holds a list of cached items. + items []*cacheItem + + // indicates the next candidate of a victim in the items list + position int + + // read lock for get, and write lock for add + rwlock sync.RWMutex +} + +type cacheItem struct { + key string + value interface{} + // set to 1 when get() is called. set to 0 when victim scan + referenced int32 +} + +func newSecondChanceCache(cacheSize int) *secondChanceCache { + var cache secondChanceCache + cache.position = 0 + cache.items = make([]*cacheItem, cacheSize) + cache.table = make(map[string]*cacheItem) + + return &cache +} + +func (cache *secondChanceCache) len() int { + cache.rwlock.RLock() + defer cache.rwlock.RUnlock() + + return len(cache.table) +} + +func (cache *secondChanceCache) get(key string) (interface{}, bool) { + cache.rwlock.RLock() + defer cache.rwlock.RUnlock() + + item, ok := cache.table[key] + if !ok { + return nil, false + } + + // referenced bit is set to true to indicate that this item is recently accessed. + atomic.StoreInt32(&item.referenced, 1) + + return item.value, true +} + +func (cache *secondChanceCache) add(key string, value interface{}) { + cache.rwlock.Lock() + defer cache.rwlock.Unlock() + + if old, ok := cache.table[key]; ok { + old.value = value + atomic.StoreInt32(&old.referenced, 1) + return + } + + var item cacheItem + item.key = key + item.value = value + atomic.StoreInt32(&item.referenced, 1) + + size := len(cache.items) + num := len(cache.table) + if num < size { + // cache is not full, so just store the new item at the end of the list + cache.table[key] = &item + cache.items[num] = &item + return + } + + // starts victim scan since cache is full + for { + // checks whether this item is recently accsessed or not + victim := cache.items[cache.position] + if atomic.LoadInt32(&victim.referenced) == 0 { + // a victim is found. delete it, and store the new item here. + delete(cache.table, victim.key) + cache.table[key] = &item + cache.items[cache.position] = &item + cache.position = (cache.position + 1) % size + return + } + + // referenced bit is set to false so that this item will be get purged + // unless it is accessed until a next victim scan + atomic.StoreInt32(&victim.referenced, 0) + cache.position = (cache.position + 1) % size + } +} diff --git a/msp/cache/second_chance_test.go b/msp/cache/second_chance_test.go new file mode 100644 index 00000000000..2d7fb46245c --- /dev/null +++ b/msp/cache/second_chance_test.go @@ -0,0 +1,109 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package cache + +import ( + "fmt" + "sync" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSecondChanceCache(t *testing.T) { + cache := newSecondChanceCache(2) + assert.NotNil(t, cache) + + cache.add("a", "xyz") + + obj, ok := cache.get("a") + assert.True(t, ok) + assert.Equal(t, "xyz", obj.(string)) + + cache.add("b", "123") + + obj, ok = cache.get("b") + assert.True(t, ok) + assert.Equal(t, "123", obj.(string)) + + cache.add("c", "777") + + obj, ok = cache.get("c") + assert.True(t, ok) + assert.Equal(t, "777", obj.(string)) + + _, ok = cache.get("a") + assert.False(t, ok) + + _, ok = cache.get("b") + assert.True(t, ok) + + cache.add("b", "456") + + obj, ok = cache.get("b") + assert.True(t, ok) + assert.Equal(t, "456", obj.(string)) + + cache.add("d", "555") + + obj, ok = cache.get("b") + _, ok = cache.get("b") + assert.False(t, ok) +} + +func TestSecondChanceCacheConcurrent(t *testing.T) { + cache := newSecondChanceCache(25) + + workers := 16 + wg := sync.WaitGroup{} + wg.Add(workers) + + key1 := fmt.Sprintf("key1") + val1 := key1 + + for i := 0; i < workers; i++ { + id := i + key2 := fmt.Sprintf("key2-%d", i) + val2 := key2 + + go func() { + for j := 0; j < 10000; j++ { + key3 := fmt.Sprintf("key3-%d-%d", id, j) + val3 := key3 + cache.add(key3, val3) + + val, ok := cache.get(key1) + if ok { + assert.Equal(t, val1, val.(string)) + } + cache.add(key1, val1) + + val, ok = cache.get(key2) + if ok { + assert.Equal(t, val2, val.(string)) + } + cache.add(key2, val2) + + key4 := fmt.Sprintf("key4-%d", j) + val4 := key4 + val, ok = cache.get(key4) + if ok { + assert.Equal(t, val4, val.(string)) + } + cache.add(key4, val4) + + val, ok = cache.get(key3) + if ok { + assert.Equal(t, val3, val.(string)) + } + } + + wg.Done() + }() + } + wg.Wait() +} diff --git a/vendor/github.com/golang/groupcache/LICENSE b/vendor/github.com/golang/groupcache/LICENSE deleted file mode 100644 index 37ec93a14fd..00000000000 --- a/vendor/github.com/golang/groupcache/LICENSE +++ /dev/null @@ -1,191 +0,0 @@ -Apache License -Version 2.0, January 2004 -http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - -"License" shall mean the terms and conditions for use, reproduction, and -distribution as defined by Sections 1 through 9 of this document. - -"Licensor" shall mean the copyright owner or entity authorized by the copyright -owner that is granting the License. - -"Legal Entity" shall mean the union of the acting entity and all other entities -that control, are controlled by, or are under common control with that entity. -For the purposes of this definition, "control" means (i) the power, direct or -indirect, to cause the direction or management of such entity, whether by -contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the -outstanding shares, or (iii) beneficial ownership of such entity. - -"You" (or "Your") shall mean an individual or Legal Entity exercising -permissions granted by this License. - -"Source" form shall mean the preferred form for making modifications, including -but not limited to software source code, documentation source, and configuration -files. - -"Object" form shall mean any form resulting from mechanical transformation or -translation of a Source form, including but not limited to compiled object code, -generated documentation, and conversions to other media types. - -"Work" shall mean the work of authorship, whether in Source or Object form, made -available under the License, as indicated by a copyright notice that is included -in or attached to the work (an example is provided in the Appendix below). - -"Derivative Works" shall mean any work, whether in Source or Object form, that -is based on (or derived from) the Work and for which the editorial revisions, -annotations, elaborations, or other modifications represent, as a whole, an -original work of authorship. For the purposes of this License, Derivative Works -shall not include works that remain separable from, or merely link (or bind by -name) to the interfaces of, the Work and Derivative Works thereof. - -"Contribution" shall mean any work of authorship, including the original version -of the Work and any modifications or additions to that Work or Derivative Works -thereof, that is intentionally submitted to Licensor for inclusion in the Work -by the copyright owner or by an individual or Legal Entity authorized to submit -on behalf of the copyright owner. For the purposes of this definition, -"submitted" means any form of electronic, verbal, or written communication sent -to the Licensor or its representatives, including but not limited to -communication on electronic mailing lists, source code control systems, and -issue tracking systems that are managed by, or on behalf of, the Licensor for -the purpose of discussing and improving the Work, but excluding communication -that is conspicuously marked or otherwise designated in writing by the copyright -owner as "Not a Contribution." - -"Contributor" shall mean Licensor and any individual or Legal Entity on behalf -of whom a Contribution has been received by Licensor and subsequently -incorporated within the Work. - -2. Grant of Copyright License. - -Subject to the terms and conditions of this License, each Contributor hereby -grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, -irrevocable copyright license to reproduce, prepare Derivative Works of, -publicly display, publicly perform, sublicense, and distribute the Work and such -Derivative Works in Source or Object form. - -3. Grant of Patent License. - -Subject to the terms and conditions of this License, each Contributor hereby -grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, -irrevocable (except as stated in this section) patent license to make, have -made, use, offer to sell, sell, import, and otherwise transfer the Work, where -such license applies only to those patent claims licensable by such Contributor -that are necessarily infringed by their Contribution(s) alone or by combination -of their Contribution(s) with the Work to which such Contribution(s) was -submitted. If You institute patent litigation against any entity (including a -cross-claim or counterclaim in a lawsuit) alleging that the Work or a -Contribution incorporated within the Work constitutes direct or contributory -patent infringement, then any patent licenses granted to You under this License -for that Work shall terminate as of the date such litigation is filed. - -4. Redistribution. - -You may reproduce and distribute copies of the Work or Derivative Works thereof -in any medium, with or without modifications, and in Source or Object form, -provided that You meet the following conditions: - -You must give any other recipients of the Work or Derivative Works a copy of -this License; and -You must cause any modified files to carry prominent notices stating that You -changed the files; and -You must retain, in the Source form of any Derivative Works that You distribute, -all copyright, patent, trademark, and attribution notices from the Source form -of the Work, excluding those notices that do not pertain to any part of the -Derivative Works; and -If the Work includes a "NOTICE" text file as part of its distribution, then any -Derivative Works that You distribute must include a readable copy of the -attribution notices contained within such NOTICE file, excluding those notices -that do not pertain to any part of the Derivative Works, in at least one of the -following places: within a NOTICE text file distributed as part of the -Derivative Works; within the Source form or documentation, if provided along -with the Derivative Works; or, within a display generated by the Derivative -Works, if and wherever such third-party notices normally appear. The contents of -the NOTICE file are for informational purposes only and do not modify the -License. You may add Your own attribution notices within Derivative Works that -You distribute, alongside or as an addendum to the NOTICE text from the Work, -provided that such additional attribution notices cannot be construed as -modifying the License. -You may add Your own copyright statement to Your modifications and may provide -additional or different license terms and conditions for use, reproduction, or -distribution of Your modifications, or for any such Derivative Works as a whole, -provided Your use, reproduction, and distribution of the Work otherwise complies -with the conditions stated in this License. - -5. Submission of Contributions. - -Unless You explicitly state otherwise, any Contribution intentionally submitted -for inclusion in the Work by You to the Licensor shall be under the terms and -conditions of this License, without any additional terms or conditions. -Notwithstanding the above, nothing herein shall supersede or modify the terms of -any separate license agreement you may have executed with Licensor regarding -such Contributions. - -6. Trademarks. - -This License does not grant permission to use the trade names, trademarks, -service marks, or product names of the Licensor, except as required for -reasonable and customary use in describing the origin of the Work and -reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. - -Unless required by applicable law or agreed to in writing, Licensor provides the -Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, -including, without limitation, any warranties or conditions of TITLE, -NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are -solely responsible for determining the appropriateness of using or -redistributing the Work and assume any risks associated with Your exercise of -permissions under this License. - -8. Limitation of Liability. - -In no event and under no legal theory, whether in tort (including negligence), -contract, or otherwise, unless required by applicable law (such as deliberate -and grossly negligent acts) or agreed to in writing, shall any Contributor be -liable to You for damages, including any direct, indirect, special, incidental, -or consequential damages of any character arising as a result of this License or -out of the use or inability to use the Work (including but not limited to -damages for loss of goodwill, work stoppage, computer failure or malfunction, or -any and all other commercial damages or losses), even if such Contributor has -been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. - -While redistributing the Work or Derivative Works thereof, You may choose to -offer, and charge a fee for, acceptance of support, warranty, indemnity, or -other liability obligations and/or rights consistent with this License. However, -in accepting such obligations, You may act only on Your own behalf and on Your -sole responsibility, not on behalf of any other Contributor, and only if You -agree to indemnify, defend, and hold each Contributor harmless for any liability -incurred by, or claims asserted against, such Contributor by reason of your -accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work - -To apply the Apache License to your work, attach the following boilerplate -notice, with the fields enclosed by brackets "[]" replaced with your own -identifying information. (Don't include the brackets!) The text should be -enclosed in the appropriate comment syntax for the file format. We also -recommend that a file or class name and description of purpose be included on -the same "printed page" as the copyright notice for easier identification within -third-party archives. - - Copyright [yyyy] [name of copyright owner] - - 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. diff --git a/vendor/github.com/golang/groupcache/lru/lru.go b/vendor/github.com/golang/groupcache/lru/lru.go deleted file mode 100644 index 532cc45e6dc..00000000000 --- a/vendor/github.com/golang/groupcache/lru/lru.go +++ /dev/null @@ -1,133 +0,0 @@ -/* -Copyright 2013 Google Inc. - -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 lru implements an LRU cache. -package lru - -import "container/list" - -// Cache is an LRU cache. It is not safe for concurrent access. -type Cache struct { - // MaxEntries is the maximum number of cache entries before - // an item is evicted. Zero means no limit. - MaxEntries int - - // OnEvicted optionally specificies a callback function to be - // executed when an entry is purged from the cache. - OnEvicted func(key Key, value interface{}) - - ll *list.List - cache map[interface{}]*list.Element -} - -// A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators -type Key interface{} - -type entry struct { - key Key - value interface{} -} - -// New creates a new Cache. -// If maxEntries is zero, the cache has no limit and it's assumed -// that eviction is done by the caller. -func New(maxEntries int) *Cache { - return &Cache{ - MaxEntries: maxEntries, - ll: list.New(), - cache: make(map[interface{}]*list.Element), - } -} - -// Add adds a value to the cache. -func (c *Cache) Add(key Key, value interface{}) { - if c.cache == nil { - c.cache = make(map[interface{}]*list.Element) - c.ll = list.New() - } - if ee, ok := c.cache[key]; ok { - c.ll.MoveToFront(ee) - ee.Value.(*entry).value = value - return - } - ele := c.ll.PushFront(&entry{key, value}) - c.cache[key] = ele - if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries { - c.RemoveOldest() - } -} - -// Get looks up a key's value from the cache. -func (c *Cache) Get(key Key) (value interface{}, ok bool) { - if c.cache == nil { - return - } - if ele, hit := c.cache[key]; hit { - c.ll.MoveToFront(ele) - return ele.Value.(*entry).value, true - } - return -} - -// Remove removes the provided key from the cache. -func (c *Cache) Remove(key Key) { - if c.cache == nil { - return - } - if ele, hit := c.cache[key]; hit { - c.removeElement(ele) - } -} - -// RemoveOldest removes the oldest item from the cache. -func (c *Cache) RemoveOldest() { - if c.cache == nil { - return - } - ele := c.ll.Back() - if ele != nil { - c.removeElement(ele) - } -} - -func (c *Cache) removeElement(e *list.Element) { - c.ll.Remove(e) - kv := e.Value.(*entry) - delete(c.cache, kv.key) - if c.OnEvicted != nil { - c.OnEvicted(kv.key, kv.value) - } -} - -// Len returns the number of items in the cache. -func (c *Cache) Len() int { - if c.cache == nil { - return 0 - } - return c.ll.Len() -} - -// Clear purges all stored items from the cache. -func (c *Cache) Clear() { - if c.OnEvicted != nil { - for _, e := range c.cache { - kv := e.Value.(*entry) - c.OnEvicted(kv.key, kv.value) - } - } - c.ll = nil - c.cache = nil -}