Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

sql/plan: make sure outdated indexes can be dropped #677

Merged
merged 1 commit into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions sql/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,12 @@ func (r *IndexRegistry) LoadIndexes(dbs Databases) error {
r.statuses[k] = IndexReady
} else {
logrus.Warnf(
"index %q is outdated and will not be used, you can remove it using `DROP INDEX %s`",
"index %q is outdated and will not be used, you can remove it using `DROP INDEX %s ON %s`",
idx.ID(),
idx.ID(),
idx.Table(),
)
r.statuses[k] = IndexOutdated
r.MarkOutdated(idx)
}
}
}
Expand All @@ -267,6 +268,12 @@ func (r *IndexRegistry) LoadIndexes(dbs Databases) error {
return nil
}

// MarkOutdated sets the index status as outdated. This method is not thread
// safe and should not be used directly except for testing.
func (r *IndexRegistry) MarkOutdated(idx Index) {
r.statuses[indexKey{idx.Database(), idx.ID()}] = IndexOutdated
}

func (r *IndexRegistry) retainIndex(db, id string) {
r.rcmut.Lock()
defer r.rcmut.Unlock()
Expand Down Expand Up @@ -336,7 +343,7 @@ func (r *IndexRegistry) IndexesByTable(db, table string) []Index {
r.mut.RLock()
defer r.mut.RUnlock()

indexes := []Index{}
var indexes []Index
for _, key := range r.indexOrder {
idx := r.indexes[key]
if idx.Database() == db && idx.Table() == table {
Expand Down
2 changes: 1 addition & 1 deletion sql/plan/drop_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *DropIndex) RowIter(ctx *sql.Context) (sql.RowIter, error) {
}
d.Catalog.ReleaseIndex(index)

if !d.Catalog.CanUseIndex(index) {
if !d.Catalog.CanRemoveIndex(index) {
return nil, ErrIndexNotAvailable.New(d.Name)
}

Expand Down
44 changes: 44 additions & 0 deletions sql/plan/drop_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,47 @@ func TestDeleteIndexNotReady(t *testing.T) {
close(done)
<-ready
}

func TestDeleteIndexOutdated(t *testing.T) {
require := require.New(t)

table := mem.NewTable("foo", sql.Schema{
{Name: "a", Source: "foo"},
{Name: "b", Source: "foo"},
{Name: "c", Source: "foo"},
})

driver := new(mockDriver)
catalog := sql.NewCatalog()
catalog.RegisterIndexDriver(driver)
db := mem.NewDatabase("foo")
db.AddTable("foo", table)
catalog.AddDatabase(db)

var expressions = []sql.Expression{
expression.NewGetFieldWithTable(0, sql.Int64, "foo", "c", true),
expression.NewGetFieldWithTable(1, sql.Int64, "foo", "a", true),
}

done, ready, err := catalog.AddIndex(&mockIndex{id: "idx", db: "foo", table: "foo", exprs: expressions})
require.NoError(err)
close(done)
<-ready

idx := catalog.Index("foo", "idx")
require.NotNil(idx)
catalog.ReleaseIndex(idx)
catalog.MarkOutdated(idx)

di := NewDropIndex("idx", NewResolvedTable(table))
di.Catalog = catalog
di.CurrentDatabase = "foo"

_, err = di.RowIter(sql.NewEmptyContext())
require.NoError(err)

time.Sleep(50 * time.Millisecond)

require.Equal([]string{"idx"}, driver.deleted)
require.Nil(catalog.Index("foo", "idx"))
}