From 40063a6aa9645bf0478222b1fb20c04827981f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Tue, 9 Feb 2021 20:13:38 +0100 Subject: [PATCH] Fix TestHandler_PostSchemaCluster --- disco/disco.go | 2 ++ etcd/embed.go | 7 ++----- holder.go | 28 ++++++++++++++++------------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/disco/disco.go b/disco/disco.go index 06187da56..de77f88dd 100644 --- a/disco/disco.go +++ b/disco/disco.go @@ -26,6 +26,8 @@ var ( ErrTooManyResults error = fmt.Errorf("too many results") ErrNoResults error = fmt.Errorf("no results") ErrKeyDeleted error = fmt.Errorf("key deleted") + ErrIndexExists error = fmt.Errorf("index already exists") + ErrFieldExists error = fmt.Errorf("field already exists") ) type Peer struct { diff --git a/etcd/embed.go b/etcd/embed.go index 2870bf051..3caef98ca 100644 --- a/etcd/embed.go +++ b/etcd/embed.go @@ -61,9 +61,6 @@ var ( _ disco.Metadator = &Etcd{} _ disco.Resizer = &Etcd{} _ disco.Sharder = &Etcd{} - - ErrIndexExists = errors.New("index already exists") - ErrFieldExists = errors.New("field already exists") ) const ( @@ -567,7 +564,7 @@ func (e *Etcd) CreateIndex(ctx context.Context, name string, val []byte) error { } if !resp.Succeeded { - return ErrIndexExists + return disco.ErrIndexExists } return nil @@ -624,7 +621,7 @@ func (e *Etcd) CreateField(ctx context.Context, indexName string, name string, v } if !resp.Succeeded { - return ErrFieldExists + return disco.ErrFieldExists } return nil diff --git a/holder.go b/holder.go index ae0b6575a..f6528d718 100644 --- a/holder.go +++ b/holder.go @@ -1077,27 +1077,31 @@ func (h *Holder) CreateIndexIfNotExists(name string, opt IndexOptions) (*Index, h.mu.Lock() defer h.mu.Unlock() - // Return index if it exists. - if index := h.Index(name); index != nil { - return index, nil - } - cim := &CreateIndexMessage{ Index: name, CreatedAt: 0, Meta: &opt, } + err := h.persistIndex(context.Background(), cim) // Create the index in etcd as the system of record. - if err := h.persistIndex(context.Background(), cim); err != nil { - // There is a case where the index is not in memory, but it is in - // persistent storage. In that case, this will return an "index exists" - // error, which in that case should return the index. TODO: We may need - // to allow for that in the future. - return nil, errors.Wrap(err, "persisting index") + if err == nil { + return h.createIndex(cim, false) } - return h.createIndex(cim, false) + if errors.Cause(err) == disco.ErrIndexExists { + // Return index if it exists. + if index := h.Index(name); index != nil { + return index, nil + } + return h.createIndex(cim, false) + } + + // There is a case where the index is not in memory, but it is in + // persistent storage. In that case, this will return an "index exists" + // error, which in that case should return the index. TODO: We may need + // to allow for that in the future. + return nil, errors.Wrap(err, "persisting index") } // persistIndex stores the index information in etcd.