diff --git a/pkg/chunk/local/boltdb_index_client_test.go b/pkg/chunk/local/boltdb_index_client_test.go index 5bd5b776b3..5dc736f9d5 100644 --- a/pkg/chunk/local/boltdb_index_client_test.go +++ b/pkg/chunk/local/boltdb_index_client_test.go @@ -1,6 +1,8 @@ package local import ( + "context" + "fmt" "io/ioutil" "os" "path/filepath" @@ -8,6 +10,8 @@ import ( "github.com/stretchr/testify/require" "go.etcd.io/bbolt" + + "github.com/cortexproject/cortex/pkg/chunk" ) var ( @@ -114,3 +118,55 @@ func TestBoltDB_GetDB(t *testing.T) { _, err = boltdbIndexClient.GetDB(testDb1, DBOperationRead) require.NoError(t, err) } + +func Test_CreateTable_BoltdbRW(t *testing.T) { + tableName := "test" + dirname, err := ioutil.TempDir(os.TempDir(), "boltdb") + require.NoError(t, err) + + indexClient, err := NewBoltDBIndexClient(BoltDBConfig{ + Directory: dirname, + }) + require.NoError(t, err) + + tableClient, err := NewTableClient(dirname) + require.NoError(t, err) + + err = tableClient.CreateTable(context.Background(), chunk.TableDesc{ + Name: tableName, + }) + require.NoError(t, err) + + batch := indexClient.NewWriteBatch() + batch.Add(tableName, fmt.Sprintf("hash%s", "test"), []byte(fmt.Sprintf("range%s", "value")), nil) + + err = indexClient.BatchWrite(context.Background(), batch) + require.NoError(t, err) + + // try to create the same file which is already existing + err = tableClient.CreateTable(context.Background(), chunk.TableDesc{ + Name: tableName, + }) + require.NoError(t, err) + + // make sure file content is not modified + entry := chunk.IndexQuery{ + TableName: tableName, + HashValue: fmt.Sprintf("hash%s", "test"), + } + var have []chunk.IndexEntry + err = indexClient.query(context.Background(), entry, func(read chunk.ReadBatch) bool { + iter := read.Iterator() + for iter.Next() { + have = append(have, chunk.IndexEntry{ + RangeValue: iter.RangeValue(), + }) + } + return true + }) + require.NoError(t, err) + require.Equal(t, []chunk.IndexEntry{ + {RangeValue: []byte(fmt.Sprintf("range%s", "value"))}, + }, have) + +} diff --git a/pkg/chunk/local/boltdb_table_client.go b/pkg/chunk/local/boltdb_table_client.go index b85012f656..e60ca9e8db 100644 --- a/pkg/chunk/local/boltdb_table_client.go +++ b/pkg/chunk/local/boltdb_table_client.go @@ -36,7 +36,12 @@ func (c *TableClient) ListTables(ctx context.Context) ([]string, error) { } func (c *TableClient) CreateTable(ctx context.Context, desc chunk.TableDesc) error { - return nil + file, err := os.OpenFile(filepath.Join(c.directory, desc.Name), os.O_CREATE|os.O_RDONLY, 0666) + if err != nil { + return err + } + + return file.Close() } func (c *TableClient) DeleteTable(ctx context.Context, name string) error { diff --git a/pkg/chunk/local/fixtures.go b/pkg/chunk/local/fixtures.go index 5a6a6f1344..60a0b426a9 100644 --- a/pkg/chunk/local/fixtures.go +++ b/pkg/chunk/local/fixtures.go @@ -59,6 +59,10 @@ func (f *fixture) Clients() ( Prefix: "chunks", Period: 10 * time.Minute, }, + IndexTables: chunk.PeriodicTableConfig{ + Prefix: "index", + Period: 10 * time.Minute, + }, }}, }