give indexes names, make stats available via expvar by default
This commit is contained in:
parent
a73a178923
commit
aa7658bbb0
|
@ -66,6 +66,8 @@ func init() {
|
|||
|
||||
bootDuration := time.Since(bootStart)
|
||||
bleveExpVar.Add("bootDuration", int64(bootDuration))
|
||||
indexStats = NewIndexStats()
|
||||
bleveExpVar.Set("indexes", indexStats)
|
||||
}
|
||||
|
||||
var logger = log.New(ioutil.Discard, "bleve", log.LstdFlags)
|
||||
|
|
|
@ -64,6 +64,7 @@ func (h *CreateIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
|
|||
showError(w, req, fmt.Sprintf("error creating index: %v", err), 500)
|
||||
return
|
||||
}
|
||||
newIndex.SetName(indexName)
|
||||
RegisterIndexName(indexName, newIndex)
|
||||
rv := struct {
|
||||
Status string `json:"status"`
|
||||
|
|
|
@ -18,7 +18,6 @@ import (
|
|||
|
||||
var indexNameMapping map[string]bleve.Index
|
||||
var indexNameMappingLock sync.RWMutex
|
||||
var indexStats = bleve.IndexStats{}
|
||||
|
||||
func RegisterIndexName(name string, idx bleve.Index) {
|
||||
indexNameMappingLock.Lock()
|
||||
|
@ -28,7 +27,6 @@ func RegisterIndexName(name string, idx bleve.Index) {
|
|||
indexNameMapping = make(map[string]bleve.Index)
|
||||
}
|
||||
indexNameMapping[name] = idx
|
||||
indexStats[name] = idx.Stats()
|
||||
}
|
||||
|
||||
func UnregisterIndexByName(name string) bleve.Index {
|
||||
|
@ -42,7 +40,6 @@ func UnregisterIndexByName(name string) bleve.Index {
|
|||
if rv != nil {
|
||||
delete(indexNameMapping, name)
|
||||
}
|
||||
delete(indexStats, name)
|
||||
return rv
|
||||
}
|
||||
|
||||
|
@ -66,10 +63,6 @@ func IndexNames() []string {
|
|||
return rv
|
||||
}
|
||||
|
||||
func IndexStats() bleve.IndexStats {
|
||||
return indexStats
|
||||
}
|
||||
|
||||
func UpdateAlias(alias string, add, remove []string) error {
|
||||
indexNameMappingLock.Lock()
|
||||
defer indexNameMappingLock.Unlock()
|
||||
|
|
5
index.go
5
index.go
|
@ -188,6 +188,11 @@ type Index interface {
|
|||
SetInternal(key, val []byte) error
|
||||
DeleteInternal(key []byte) error
|
||||
|
||||
// Name returns the name of the index (by deault this is the path)
|
||||
Name() string
|
||||
// SetName lets you assign your own logical name to this index
|
||||
SetName(string)
|
||||
|
||||
// Advanced returns the indexer and data store, exposing lower level
|
||||
// methods to enumerate records and access data.
|
||||
Advanced() (index.Index, store.KVStore, error)
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
)
|
||||
|
||||
type indexAliasImpl struct {
|
||||
name string
|
||||
indexes []Index
|
||||
mutex sync.RWMutex
|
||||
open bool
|
||||
|
@ -30,6 +31,7 @@ type indexAliasImpl struct {
|
|||
// Index objects.
|
||||
func NewIndexAlias(indexes ...Index) *indexAliasImpl {
|
||||
return &indexAliasImpl{
|
||||
name: "alias",
|
||||
indexes: indexes,
|
||||
open: true,
|
||||
}
|
||||
|
@ -561,6 +563,14 @@ func (i *indexAliasImpl) NewBatch() *Batch {
|
|||
return i.indexes[0].NewBatch()
|
||||
}
|
||||
|
||||
func (i *indexAliasImpl) Name() string {
|
||||
return i.name
|
||||
}
|
||||
|
||||
func (i *indexAliasImpl) SetName(name string) {
|
||||
i.name = name
|
||||
}
|
||||
|
||||
type indexAliasImplFieldDict struct {
|
||||
index *indexAliasImpl
|
||||
fieldDict index.FieldDict
|
||||
|
|
|
@ -683,6 +683,7 @@ func TestMultiSearchSecondPage(t *testing.T) {
|
|||
// corresponding operation result value has been
|
||||
// set, in which case that is returned instead
|
||||
type stubIndex struct {
|
||||
name string
|
||||
err error
|
||||
searchResult *SearchResult
|
||||
documentResult *document.Document
|
||||
|
@ -788,3 +789,11 @@ func (i *stubIndex) Advanced() (index.Index, store.KVStore, error) {
|
|||
func (i *stubIndex) NewBatch() *Batch {
|
||||
return &Batch{}
|
||||
}
|
||||
|
||||
func (i *stubIndex) Name() string {
|
||||
return i.name
|
||||
}
|
||||
|
||||
func (i *stubIndex) SetName(name string) {
|
||||
i.name = name
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ import (
|
|||
|
||||
type indexImpl struct {
|
||||
path string
|
||||
name string
|
||||
meta *indexMeta
|
||||
s store.KVStore
|
||||
i index.Index
|
||||
|
@ -50,6 +51,7 @@ func indexStorePath(path string) string {
|
|||
func newMemIndex(indexType string, mapping *IndexMapping) (*indexImpl, error) {
|
||||
rv := indexImpl{
|
||||
path: "",
|
||||
name: "mem",
|
||||
m: mapping,
|
||||
meta: newIndexMeta(indexType, gtreap.Name, nil),
|
||||
stats: &IndexStat{},
|
||||
|
@ -86,6 +88,7 @@ func newMemIndex(indexType string, mapping *IndexMapping) (*indexImpl, error) {
|
|||
rv.mutex.Lock()
|
||||
defer rv.mutex.Unlock()
|
||||
rv.open = true
|
||||
indexStats.Register(&rv)
|
||||
return &rv, nil
|
||||
}
|
||||
|
||||
|
@ -106,6 +109,7 @@ func newIndexUsing(path string, mapping *IndexMapping, indexType string, kvstore
|
|||
|
||||
rv := indexImpl{
|
||||
path: path,
|
||||
name: path,
|
||||
m: mapping,
|
||||
meta: newIndexMeta(indexType, kvstore, kvconfig),
|
||||
stats: &IndexStat{},
|
||||
|
@ -152,12 +156,14 @@ func newIndexUsing(path string, mapping *IndexMapping, indexType string, kvstore
|
|||
rv.mutex.Lock()
|
||||
defer rv.mutex.Unlock()
|
||||
rv.open = true
|
||||
indexStats.Register(&rv)
|
||||
return &rv, nil
|
||||
}
|
||||
|
||||
func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *indexImpl, err error) {
|
||||
rv = &indexImpl{
|
||||
path: path,
|
||||
name: path,
|
||||
stats: &IndexStat{},
|
||||
}
|
||||
|
||||
|
@ -238,6 +244,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
|
|||
}
|
||||
|
||||
rv.m = &im
|
||||
indexStats.Register(rv)
|
||||
return rv, err
|
||||
}
|
||||
|
||||
|
@ -725,6 +732,16 @@ func (i *indexImpl) NewBatch() *Batch {
|
|||
}
|
||||
}
|
||||
|
||||
func (i *indexImpl) Name() string {
|
||||
return i.name
|
||||
}
|
||||
|
||||
func (i *indexImpl) SetName(name string) {
|
||||
indexStats.UnRegister(i)
|
||||
i.name = name
|
||||
indexStats.Register(i)
|
||||
}
|
||||
|
||||
type indexImplFieldDict struct {
|
||||
index *indexImpl
|
||||
indexReader index.IndexReader
|
||||
|
|
|
@ -11,6 +11,7 @@ package bleve
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
|
@ -28,12 +29,37 @@ func (is *IndexStat) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
type IndexStats map[string]*IndexStat
|
||||
type IndexStats struct {
|
||||
indexes map[string]*IndexStat
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (i IndexStats) String() string {
|
||||
bytes, err := json.Marshal(i)
|
||||
func NewIndexStats() *IndexStats {
|
||||
return &IndexStats{
|
||||
indexes: make(map[string]*IndexStat),
|
||||
}
|
||||
}
|
||||
|
||||
func (i *IndexStats) Register(index Index) {
|
||||
i.mutex.Lock()
|
||||
defer i.mutex.Unlock()
|
||||
i.indexes[index.Name()] = index.Stats()
|
||||
}
|
||||
|
||||
func (i *IndexStats) UnRegister(index Index) {
|
||||
i.mutex.Lock()
|
||||
defer i.mutex.Unlock()
|
||||
delete(i.indexes, index.Name())
|
||||
}
|
||||
|
||||
func (i *IndexStats) String() string {
|
||||
i.mutex.RLock()
|
||||
defer i.mutex.RUnlock()
|
||||
bytes, err := json.Marshal(i.indexes)
|
||||
if err != nil {
|
||||
return "error marshaling stats"
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
var indexStats *IndexStats
|
||||
|
|
Loading…
Reference in New Issue