Add comment for cache, add interface CacheReader, to make it easy to understand (#615)
diff --git a/server/core/backend/defer_instance.go b/server/core/backend/defer_instance.go
index ffba31d..8c7b62c 100644
--- a/server/core/backend/defer_instance.go
+++ b/server/core/backend/defer_instance.go
@@ -35,7 +35,7 @@
type InstanceEventDeferHandler struct {
Percent float64
- cache discovery.Cache
+ cache discovery.CacheReader
once sync.Once
enabled bool
items map[string]*deferItem
@@ -44,7 +44,7 @@
resetCh chan struct{}
}
-func (iedh *InstanceEventDeferHandler) OnCondition(cache discovery.Cache, evts []discovery.KvEvent) bool {
+func (iedh *InstanceEventDeferHandler) OnCondition(cache discovery.CacheReader, evts []discovery.KvEvent) bool {
if iedh.Percent <= 0 {
return false
}
diff --git a/server/plugin/pkg/discovery/aggregate/adaptor.go b/server/plugin/pkg/discovery/aggregate/adaptor.go
index 3ff356d..367e7dc 100644
--- a/server/plugin/pkg/discovery/aggregate/adaptor.go
+++ b/server/plugin/pkg/discovery/aggregate/adaptor.go
@@ -23,14 +23,18 @@
"github.com/apache/servicecomb-service-center/server/plugin/pkg/discovery"
)
-// Aggregator is a discovery service adaptor implement of one registry cluster
+// Aggregator implements discovery.Adaptor.
+// Aggregator is an aggregator of multi Adaptors, and it aggregates all the
+// Adaptors' data as it's result.
type Aggregator struct {
+ // Indexer searches data from all the adapters
discovery.Indexer
Type discovery.Type
Adaptors []discovery.Adaptor
}
-func (as *Aggregator) Cache() discovery.Cache {
+// Cache gets all the adapters' cache
+func (as *Aggregator) Cache() discovery.CacheReader {
var cache Cache
for _, a := range as.Adaptors {
cache = append(cache, a.Cache())
diff --git a/server/plugin/pkg/discovery/aggregate/cache.go b/server/plugin/pkg/discovery/aggregate/cache.go
index 381559d..ee375a6 100644
--- a/server/plugin/pkg/discovery/aggregate/cache.go
+++ b/server/plugin/pkg/discovery/aggregate/cache.go
@@ -20,7 +20,9 @@
"github.com/apache/servicecomb-service-center/server/plugin/pkg/discovery"
)
-type Cache []discovery.Cache
+// Cache implements CacheReader.
+// Cache is a multi-CacheReader, it reads cache from all CacheReaders.
+type Cache []discovery.CacheReader
func (c Cache) Name() string {
if len(c) == 0 {
@@ -94,5 +96,3 @@
})
}
}
-func (c Cache) Put(k string, v *discovery.KeyValue) { return }
-func (c Cache) Remove(k string) { return }
diff --git a/server/plugin/pkg/discovery/aggregate/conflict_checker.go b/server/plugin/pkg/discovery/aggregate/conflict_checker.go
index f69324f..d4a033b 100644
--- a/server/plugin/pkg/discovery/aggregate/conflict_checker.go
+++ b/server/plugin/pkg/discovery/aggregate/conflict_checker.go
@@ -25,7 +25,7 @@
)
type ConflictChecker struct {
- Cache discovery.Cache
+ Cache discovery.CacheReader
ConflictHandleFunc func(origin, conflict *discovery.KeyValue)
}
@@ -66,7 +66,7 @@
}
}
-func NewConflictChecker(cache discovery.Cache, f func(origin, conflict *discovery.KeyValue)) *ConflictChecker {
+func NewConflictChecker(cache discovery.CacheReader, f func(origin, conflict *discovery.KeyValue)) *ConflictChecker {
checker := &ConflictChecker{Cache: cache, ConflictHandleFunc: f}
gopool.Go(checker.Run)
return checker
diff --git a/server/plugin/pkg/discovery/aggregate/indexer.go b/server/plugin/pkg/discovery/aggregate/indexer.go
index b524a69..d425b0d 100644
--- a/server/plugin/pkg/discovery/aggregate/indexer.go
+++ b/server/plugin/pkg/discovery/aggregate/indexer.go
@@ -22,6 +22,9 @@
"golang.org/x/net/context"
)
+// AdaptorsIndexer implements discovery.Indexer.
+// AdaptorsIndexer is an aggregator of multi Indexers, and it aggregates all the
+// Indexers' data as it's result.
type AdaptorsIndexer struct {
Adaptors []discovery.Adaptor
}
@@ -52,10 +55,16 @@
return &AdaptorsIndexer{Adaptors: as}
}
+// AggregatorIndexer implements discovery.Indexer.
+// AggregatorIndexer consists of multi Indexers and it decides which Indexer to
+// use based on it's mechanism.
type AggregatorIndexer struct {
+ // CacheIndexer searches data from all the adaptors's cache.
*discovery.CacheIndexer
+ // AdaptorsIndexer searches data from all the adaptors.
AdaptorsIndexer discovery.Indexer
- LocalIndexer discovery.Indexer
+ // LocalIndexer data from local adaptor.
+ LocalIndexer discovery.Indexer
}
func (i *AggregatorIndexer) Search(ctx context.Context, opts ...registry.PluginOpOption) (resp *discovery.Response, err error) {
diff --git a/server/plugin/pkg/discovery/cache.go b/server/plugin/pkg/discovery/cache.go
index 08376b0..b0ac547 100644
--- a/server/plugin/pkg/discovery/cache.go
+++ b/server/plugin/pkg/discovery/cache.go
@@ -16,19 +16,32 @@
*/
package discovery
+// Cacher manages cache of some data source, e.g. etcd, kubernetes.
+// An user can do nothing but read the managed cache.
+type Cacher interface {
+ // Cache gets the cache that Cacher manages.
+ Cache() CacheReader
+}
+
+// Cache stores k-v data.
type Cache interface {
- Name() string
- Size() int // the bytes size of the cache
-
- Get(k string) *KeyValue
- GetAll(arr *[]*KeyValue) int
- GetPrefix(prefix string, arr *[]*KeyValue) int
- ForEach(iter func(k string, v *KeyValue) (next bool))
-
+ CacheReader
+ // Put puts a k-v
Put(k string, v *KeyValue)
+ // Remove removes a k-v data
Remove(k string)
}
-type Cacher interface {
- Cache() Cache
+// CacheReader reads k-v data.
+type CacheReader interface {
+ Name() string // The name of implementation
+ Size() int // the bytes size of the cache
+ // Get gets a value by input key
+ Get(k string) *KeyValue
+ // GetAll gets all the data stored in Cache
+ GetAll(arr *[]*KeyValue) int
+ // GetPrefix gets values by the key prefix
+ GetPrefix(prefix string, arr *[]*KeyValue) int
+ // ForEach executes the given function for each of the k-v
+ ForEach(iter func(k string, v *KeyValue) (next bool))
}
diff --git a/server/plugin/pkg/discovery/cache_kv.go b/server/plugin/pkg/discovery/cache_kv.go
index aaec37e..f51f1d2 100644
--- a/server/plugin/pkg/discovery/cache_kv.go
+++ b/server/plugin/pkg/discovery/cache_kv.go
@@ -23,6 +23,9 @@
"time"
)
+// KvCache implements Cache.
+// KvCache is dedicated to stores service discovery data,
+// e.g. service, instance, lease.
type KvCache struct {
Cfg *Config
name string
diff --git a/server/plugin/pkg/discovery/cache_null.go b/server/plugin/pkg/discovery/cache_null.go
index d650032..8bb00fe 100644
--- a/server/plugin/pkg/discovery/cache_null.go
+++ b/server/plugin/pkg/discovery/cache_null.go
@@ -36,4 +36,4 @@
type nullCacher struct {
}
-func (n *nullCacher) Cache() Cache { return NullCache }
+func (n *nullCacher) Cache() CacheReader { return NullCache }
diff --git a/server/plugin/pkg/discovery/cacher.go b/server/plugin/pkg/discovery/cacher.go
index 9b19719..d929daa 100644
--- a/server/plugin/pkg/discovery/cacher.go
+++ b/server/plugin/pkg/discovery/cacher.go
@@ -21,6 +21,9 @@
"github.com/apache/servicecomb-service-center/server/core/proto"
)
+// CommonCacher implements discovery.Cacher.
+// CommonCacher is universal to manage cache of any registry.
+// Use Cfg to set it's behavior.
type CommonCacher struct {
Cfg *Config
// cache for indexer
@@ -29,7 +32,7 @@
ready chan struct{}
}
-func (c *CommonCacher) Cache() Cache {
+func (c *CommonCacher) Cache() CacheReader {
return c.cache
}
diff --git a/server/plugin/pkg/discovery/config_test.go b/server/plugin/pkg/discovery/config_test.go
index 1aeaba3..798f315 100644
--- a/server/plugin/pkg/discovery/config_test.go
+++ b/server/plugin/pkg/discovery/config_test.go
@@ -24,7 +24,7 @@
type mockDeferHandler struct {
}
-func (m *mockDeferHandler) OnCondition(Cache, []KvEvent) bool {
+func (m *mockDeferHandler) OnCondition(CacheReader, []KvEvent) bool {
return false
}
func (m *mockDeferHandler) HandleChan() <-chan KvEvent {
diff --git a/server/plugin/pkg/discovery/defer.go b/server/plugin/pkg/discovery/defer.go
index 3fcb6a6..10e6de2 100644
--- a/server/plugin/pkg/discovery/defer.go
+++ b/server/plugin/pkg/discovery/defer.go
@@ -17,7 +17,7 @@
package discovery
type DeferHandler interface {
- OnCondition(Cache, []KvEvent) bool
+ OnCondition(CacheReader, []KvEvent) bool
HandleChan() <-chan KvEvent
Reset() bool
}
diff --git a/server/plugin/pkg/discovery/discovery.go b/server/plugin/pkg/discovery/discovery.go
index d0c0d94..a41f6af 100644
--- a/server/plugin/pkg/discovery/discovery.go
+++ b/server/plugin/pkg/discovery/discovery.go
@@ -16,16 +16,19 @@
*/
package discovery
+// Adaptor is used to do service discovery.
+// To improve the performance, Adaptor may use cache firstly in
+// service discovery.
type Adaptor interface {
Runnable
- // Indexer is the key indexer of cache
+ // Indexer is used to search data from the registry.
Indexer
- // Cacher is the cache manager to sync data from registry server
- // and publish the cache event if cache changed
+ // Cacher is used to manage the registry's cache.
Cacher
}
+// AdaptorRepository creates Adaptors
type AdaptorRepository interface {
- // New is the method new an instance of specify Type adaptor
+ // New news an instance of specify Type adaptor
New(t Type, cfg *Config) Adaptor
}
diff --git a/server/plugin/pkg/discovery/etcd/adaptor.go b/server/plugin/pkg/discovery/etcd/adaptor.go
index ae4f443..5a73e75 100644
--- a/server/plugin/pkg/discovery/etcd/adaptor.go
+++ b/server/plugin/pkg/discovery/etcd/adaptor.go
@@ -22,6 +22,8 @@
"github.com/apache/servicecomb-service-center/server/plugin/pkg/discovery"
)
+// EtcdAdaptor implements discovery.Adaptor.
+// EtcdAdaptor does service discovery with etcd as it's registry.
type EtcdAdaptor struct {
discovery.Cacher
discovery.Indexer
diff --git a/server/plugin/pkg/discovery/etcd/cacher_kv.go b/server/plugin/pkg/discovery/etcd/cacher_kv.go
index 323e1d8..442c0fc 100644
--- a/server/plugin/pkg/discovery/etcd/cacher_kv.go
+++ b/server/plugin/pkg/discovery/etcd/cacher_kv.go
@@ -34,6 +34,12 @@
"time"
)
+// KvCacher implements discovery.Cacher.
+// KvCacher manages etcd cache.
+// To update cache, KvCacher watch etcd event and pull data periodly from etcd.
+// When the cache data changes, KvCacher creates events and notifies it's
+// subscribers.
+// Use Cfg to set it's behaviors.
type KvCacher struct {
Cfg *discovery.Config
@@ -416,7 +422,7 @@
return
}
-func (c *KvCacher) Cache() discovery.Cache {
+func (c *KvCacher) Cache() discovery.CacheReader {
return c.cache
}
diff --git a/server/plugin/pkg/discovery/etcd/indexer_cache.go b/server/plugin/pkg/discovery/etcd/indexer_cache.go
index 09b371a..dfd1ae4 100644
--- a/server/plugin/pkg/discovery/etcd/indexer_cache.go
+++ b/server/plugin/pkg/discovery/etcd/indexer_cache.go
@@ -24,6 +24,9 @@
"golang.org/x/net/context"
)
+// CacheIndexer implements discovery.Indexer.
+// CacheIndexer searches data from etcd cache(firstly) and
+// etcd server(secondly).
type CacheIndexer struct {
*EtcdIndexer
*discovery.CacheIndexer
diff --git a/server/plugin/pkg/discovery/etcd/indexer_etcd.go b/server/plugin/pkg/discovery/etcd/indexer_etcd.go
index a38e9ba..e5d201b 100644
--- a/server/plugin/pkg/discovery/etcd/indexer_etcd.go
+++ b/server/plugin/pkg/discovery/etcd/indexer_etcd.go
@@ -28,6 +28,8 @@
"strings"
)
+// EtcdIndexer implements discovery.Indexer.
+// EtcdIndexer searches data from etcd server.
type EtcdIndexer struct {
Client registry.Registry
Parser pb.Parser
diff --git a/server/plugin/pkg/discovery/indexer.go b/server/plugin/pkg/discovery/indexer.go
index 98b9913..b7e42b5 100644
--- a/server/plugin/pkg/discovery/indexer.go
+++ b/server/plugin/pkg/discovery/indexer.go
@@ -21,6 +21,10 @@
"golang.org/x/net/context"
)
+// Indexer searches k-v data.
+// An indexer may search k-v data from many data sources,
+// e.g. cache, registry, file, other Indexers...
type Indexer interface {
+ // Search searches k-v data based on the input options
Search(ctx context.Context, opts ...registry.PluginOpOption) (*Response, error)
}
diff --git a/server/plugin/pkg/discovery/indexer_cache.go b/server/plugin/pkg/discovery/indexer_cache.go
index 8ceec43..0f3c675 100644
--- a/server/plugin/pkg/discovery/indexer_cache.go
+++ b/server/plugin/pkg/discovery/indexer_cache.go
@@ -24,8 +24,10 @@
"time"
)
+// CacheIndexer implements discovery.Indexer.
+// CacheIndexer searches data from cache.
type CacheIndexer struct {
- Cache Cache
+ Cache CacheReader
}
func (i *CacheIndexer) Search(ctx context.Context, opts ...registry.PluginOpOption) (resp *Response, _ error) {
@@ -74,7 +76,7 @@
return resp
}
-func NewCacheIndexer(cache Cache) *CacheIndexer {
+func NewCacheIndexer(cache CacheReader) *CacheIndexer {
return &CacheIndexer{
Cache: cache,
}
diff --git a/server/plugin/pkg/discovery/k8s/adaptor/adaptor.go b/server/plugin/pkg/discovery/k8s/adaptor/adaptor.go
index 0d362df..d5c88bd 100644
--- a/server/plugin/pkg/discovery/k8s/adaptor/adaptor.go
+++ b/server/plugin/pkg/discovery/k8s/adaptor/adaptor.go
@@ -19,7 +19,8 @@
"github.com/apache/servicecomb-service-center/server/plugin/pkg/discovery"
)
-// K8sAdaptor is a discovery service adaptor implement of one kubernetes cluster
+// K8sAdaptor implements discovery.Adaptor.
+// K8sAdaptor does service discovery with kubernetes as it's registry.
type K8sAdaptor struct {
discovery.Cacher
discovery.Indexer
diff --git a/server/plugin/pkg/discovery/servicecenter/adaptor.go b/server/plugin/pkg/discovery/servicecenter/adaptor.go
index 77e0086..ec268ba 100644
--- a/server/plugin/pkg/discovery/servicecenter/adaptor.go
+++ b/server/plugin/pkg/discovery/servicecenter/adaptor.go
@@ -20,7 +20,9 @@
"github.com/apache/servicecomb-service-center/server/plugin/pkg/discovery"
)
-// ServiceCenterAdaptor is a discovery service adaptor implement of one kubernetes cluster
+// ServiceCenterAdaptor implements discovery.Adaptor.
+// ServiceCenterAdaptor does service discovery with other service-centers
+// as it's registry.
type ServiceCenterAdaptor struct {
discovery.Cacher
discovery.Indexer
diff --git a/server/plugin/pkg/discovery/servicecenter/indexer.go b/server/plugin/pkg/discovery/servicecenter/indexer.go
index 5ac4768..00ab11d 100644
--- a/server/plugin/pkg/discovery/servicecenter/indexer.go
+++ b/server/plugin/pkg/discovery/servicecenter/indexer.go
@@ -27,6 +27,9 @@
"golang.org/x/net/context"
)
+// ClusterIndexer implements discovery.Indexer.
+// ClusterIndexer searches data from cache(firstly) and
+// other service-centers(secondly).
type ClusterIndexer struct {
*discovery.CacheIndexer
Client *SCClientAggregate