-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some tasks, such as loading shards, require priviledged access on startup. Here I introduce a systemtenant we can use for these things. This is motivated by bug where the symbol sidebar in multi-tenant node wouldn't work because ranked shards were not loaded correctly which in turn caused `SelectRepoSet` to return 0 shards always. Test plan: - added unit test - manual testing: symbol sidebar works now
- Loading branch information
1 parent
c52a9cd
commit 6bbab33
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Package systemtenant contains function to mark a context as allowed to | ||
// access shards across all tenants. This must only be used for tasks that are | ||
// not request specific. | ||
package systemtenant | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sourcegraph/zoekt/internal/tenant/internal/tenanttype" | ||
) | ||
|
||
type contextKey int | ||
|
||
const systemTenantKey contextKey = iota | ||
|
||
// With marks a ctx to be allowed to access shards across all tenants. This MUST | ||
// NOT BE USED on the user request path. | ||
func With(ctx context.Context) (context.Context, error) { | ||
// We don't want to allow setting the system tenant on a context that already | ||
// has a user tenant set. | ||
if _, ok := tenanttype.GetTenant(ctx); ok { | ||
return nil, fmt.Errorf("tenant context already set") | ||
} | ||
return context.WithValue(ctx, systemTenantKey, systemTenantKey), nil | ||
} | ||
|
||
// Is returns true if the context has been marked to allow queries across all | ||
// tenants. | ||
func Is(ctx context.Context) bool { | ||
_, ok := ctx.Value(systemTenantKey).(contextKey) | ||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package systemtenant | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSystemtenantRoundtrip(t *testing.T) { | ||
if Is(context.Background()) { | ||
t.Fatal() | ||
} | ||
ctx, err := With(context.Background()) | ||
require.NoError(t, err) | ||
require.True(t, Is(ctx)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters