Skip to content

Commit 23514ce

Browse files
committed
fix: Do not initialise local querier, when their are no local blocks
This builds on top of #4151 and #4144, as previously initilised heads already created a folder on all ingesters. Hence the check is not detecting that there are actually no local blocks to be considered.
1 parent ea23dc5 commit 23514ce

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

pkg/ingester/ingester.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ func (i *Ingester) getOrCreateInstance(tenantID string) (*instance, error) {
195195
return inst, nil
196196
}
197197

198+
func (i *Ingester) hasLocalBlocks(tenantID string) (bool, error) {
199+
entries, err := os.ReadDir(filepath.Join(i.dbConfig.DataPath, tenantID, "local"))
200+
if err != nil {
201+
if os.IsNotExist(err) {
202+
return false, nil
203+
}
204+
return false, err
205+
}
206+
for _, entry := range entries {
207+
if entry.IsDir() {
208+
return true, nil
209+
}
210+
}
211+
return false, nil
212+
}
213+
198214
func (i *Ingester) getOrOpenInstance(tenantID string) (*instance, error) {
199215
inst, ok := i.getInstanceByID(tenantID)
200216
if ok {
@@ -208,14 +224,14 @@ func (i *Ingester) getOrOpenInstance(tenantID string) (*instance, error) {
208224
return inst, nil
209225
}
210226

211-
if _, err := os.Stat(filepath.Join(i.dbConfig.DataPath, tenantID)); err != nil {
212-
if os.IsNotExist(err) {
213-
return nil, nil
214-
}
227+
hasLocalBlocks, err := i.hasLocalBlocks(tenantID)
228+
if err != nil {
215229
return nil, err
216230
}
231+
if !hasLocalBlocks {
232+
return nil, nil
233+
}
217234

218-
var err error
219235
limiter := NewLimiter(tenantID, i.limits, i.lifecycler, i.cfg.LifecyclerConfig.RingConfig.ReplicationFactor)
220236
inst, err = newInstance(i.phlarectx, i.dbConfig, tenantID, i.localBucket, i.storageBucket, limiter)
221237
if err != nil {

pkg/ingester/ingester_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"os"
7+
"path/filepath"
78
"runtime/pprof"
89
"testing"
910
"time"
@@ -132,11 +133,19 @@ func Test_Query_TenantNotFound(t *testing.T) {
132133
},
133134
}
134135

136+
// set the localPath
137+
localPath := t.TempDir()
138+
139+
// foo has an empty local dir
140+
fooLocalPath := filepath.Join(localPath, "foo", "local")
141+
require.NoError(t, os.MkdirAll(fooLocalPath, 0o755))
142+
require.NoError(t, os.WriteFile(filepath.Join(fooLocalPath, "shipper.json"), []byte(`{"version":1,"uploaded":null}`), 0o755))
143+
135144
fs, err := client.NewBucket(ctx, cfg, "storage")
136145
require.NoError(t, err)
137146

138147
ing, err := New(ctx, defaultIngesterTestConfig(t), phlaredb.Config{
139-
DataPath: dbPath,
148+
DataPath: localPath,
140149
MaxBlockDuration: 30 * time.Hour,
141150
}, fs, &fakeLimits{}, 0)
142151
require.NoError(t, err)
@@ -150,6 +159,11 @@ func Test_Query_TenantNotFound(t *testing.T) {
150159
require.NoError(t, err)
151160
require.Empty(t, labelsNames.Msg.Names)
152161

162+
// check that no tenant are initialized
163+
ing.instancesMtx.RLock()
164+
require.Len(t, ing.instances, 0)
165+
ing.instancesMtx.RUnlock()
166+
153167
require.NoError(t, services.StopAndAwaitTerminated(context.Background(), ing))
154168
}
155169

0 commit comments

Comments
 (0)