Merge pull request #25 from kuba--/index-exists

Fix TestHandler_PostSchemaCluster
This commit is contained in:
Travis Turner 2021-02-09 21:23:55 -06:00 committed by GitHub
commit c85c42c58a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 17 deletions

View file

@ -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 {

View file

@ -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

View file

@ -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.