-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(blooms): Builder retrieves tasks from planner #13046
Merged
salvacorts
merged 12 commits into
main
from
salvacorts/bloom-refactor/builder-pull-task
May 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7fbfb2f
Pull tasks
salvacorts 4b42a26
revert deletion of compiled protos
salvacorts c51127b
undo service proto change
salvacorts b0382a3
Revert "undo service proto change"
salvacorts 46f8454
update protos
salvacorts 6b69ac6
Metrics
salvacorts 403c730
Add test and fixes
salvacorts 48596d9
lint issues
salvacorts 102a372
wait for ctx done instead of stop chan
salvacorts 34fcd85
typo
salvacorts 40e1daa
CR feedback
salvacorts ef0f785
typo
salvacorts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add test and fixes
- Loading branch information
commit 403c730c4e65723fb4029d4908e4c7f8baa7fb54
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,127 @@ | ||
package builder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/grafana/dskit/flagext" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/grafana/dskit/services" | ||
"github.com/grafana/loki/v3/pkg/bloombuild/protos" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func Test_BuilderLoop(t *testing.T) { | ||
logger := log.NewNopLogger() | ||
|
||
tasks := make([]*protos.ProtoTask, 256) | ||
for i := range tasks { | ||
tasks[i] = &protos.ProtoTask{ | ||
Id: fmt.Sprintf("task-%d", i), | ||
} | ||
} | ||
|
||
server, err := newFakePlannerServer(tasks) | ||
require.NoError(t, err) | ||
|
||
cfg := Config{ | ||
PlannerAddress: server.Addr(), | ||
} | ||
flagext.DefaultValues(&cfg.GrpcConfig) | ||
|
||
builder, err := New(cfg, logger, prometheus.DefaultRegisterer) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
err = services.StopAndAwaitTerminated(context.Background(), builder) | ||
require.NoError(t, err) | ||
|
||
server.Stop() | ||
}) | ||
|
||
err = services.StartAndAwaitRunning(context.Background(), builder) | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
return server.completedTasks == len(tasks) | ||
}, 5*time.Second, 100*time.Millisecond) | ||
|
||
err = services.StopAndAwaitTerminated(context.Background(), builder) | ||
require.NoError(t, err) | ||
|
||
require.True(t, server.shutdownCalled) | ||
} | ||
|
||
type fakePlannerServer struct { | ||
tasks []*protos.ProtoTask | ||
completedTasks int | ||
shutdownCalled bool | ||
|
||
addr string | ||
grpcServer *grpc.Server | ||
stop chan struct{} | ||
} | ||
|
||
func newFakePlannerServer(tasks []*protos.ProtoTask) (*fakePlannerServer, error) { | ||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
server := &fakePlannerServer{ | ||
tasks: tasks, | ||
addr: lis.Addr().String(), | ||
grpcServer: grpc.NewServer(), | ||
stop: make(chan struct{}), | ||
} | ||
|
||
protos.RegisterPlannerForBuilderServer(server.grpcServer, server) | ||
go func() { | ||
if err := server.grpcServer.Serve(lis); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
return server, nil | ||
} | ||
|
||
func (f *fakePlannerServer) Addr() string { | ||
return f.addr | ||
} | ||
|
||
func (f *fakePlannerServer) Stop() { | ||
close(f.stop) | ||
f.grpcServer.Stop() | ||
} | ||
|
||
func (f *fakePlannerServer) BuilderLoop(srv protos.PlannerForBuilder_BuilderLoopServer) error { | ||
// Receive Ready | ||
if _, err := srv.Recv(); err != nil { | ||
return fmt.Errorf("failed to receive ready: %w", err) | ||
} | ||
|
||
for _, task := range f.tasks { | ||
if err := srv.Send(&protos.PlannerToBuilder{Task: task}); err != nil { | ||
return fmt.Errorf("failed to send task: %w", err) | ||
} | ||
if _, err := srv.Recv(); err != nil { | ||
return fmt.Errorf("failed to receive task response: %w", err) | ||
} | ||
f.completedTasks++ | ||
} | ||
|
||
// No more tasks. Emulate waiting for requeue until shutdown. | ||
select { | ||
case <-f.stop: | ||
return nil | ||
} | ||
} | ||
|
||
func (f *fakePlannerServer) NotifyBuilderShutdown(_ context.Context, _ *protos.NotifyBuilderShutdownRequest) (*protos.NotifyBuilderShutdownResponse, error) { | ||
f.shutdownCalled = true | ||
return &protos.NotifyBuilderShutdownResponse{}, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should check
err
for context.Canceled, or, check ifc.Context() == nil
beforec.Recv()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the connection context is canceled,
c.Recv()
will return right after calling it.(I actually had it as you suggest but noticed the behaviour while testing and decided to remove it for simplicity)