Skip to content
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

🐛 Add race option to detect raced codes #10899

Merged
merged 6 commits into from
Oct 23, 2024
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
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -941,43 +941,43 @@ test-cover: ## Run unit and integration tests and generate a coverage report

.PHONY: test-docker-infrastructure
test-docker-infrastructure: $(SETUP_ENVTEST) ## Run unit and integration tests for docker infrastructure provider
cd $(CAPD_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
cd $(CAPD_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)

.PHONY: test-docker-infrastructure-verbose
test-docker-infrastructure-verbose: ## Run unit and integration tests for docker infrastructure provider with verbose flag
$(MAKE) test-docker-infrastructure TEST_ARGS="$(TEST_ARGS) -v"

.PHONY: test-docker-infrastructure-junit
test-docker-infrastructure-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report for docker infrastructure provider
cd $(CAPD_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_docker.exitcode) | tee $(ARTIFACTS)/junit.infra_docker.stdout
cd $(CAPD_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_docker.exitcode) | tee $(ARTIFACTS)/junit.infra_docker.stdout
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.infra_docker.xml --raw-command cat $(ARTIFACTS)/junit.infra_docker.stdout
exit $$(cat $(ARTIFACTS)/junit.infra_docker.exitcode)

.PHONY: test-in-memory-infrastructure
test-in-memory-infrastructure: $(SETUP_ENVTEST) ## Run unit and integration tests for in-memory infrastructure provider
cd $(CAPIM_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
cd $(CAPIM_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)

.PHONY: test-in-memory-infrastructure-verbose
test-in-memory-infrastructure-verbose: ## Run unit and integration tests for in-memory infrastructure provider with verbose flag
$(MAKE) test-in-memory-infrastructure TEST_ARGS="$(TEST_ARGS) -v"

.PHONY: test-in-memory-infrastructure-junit
test-in-memory-infrastructure-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report for in-memory infrastructure provider
cd $(CAPIM_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_inmemory.exitcode) | tee $(ARTIFACTS)/junit.infra_inmemory.stdout
cd $(CAPIM_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_inmemory.exitcode) | tee $(ARTIFACTS)/junit.infra_inmemory.stdout
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.infra_inmemory.xml --raw-command cat $(ARTIFACTS)/junit.infra_inmemory.stdout
exit $$(cat $(ARTIFACTS)/junit.infra_inmemory.exitcode)

.PHONY: test-test-extension
test-test-extension: $(SETUP_ENVTEST) ## Run unit and integration tests for the test extension
cd $(TEST_EXTENSION_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
cd $(TEST_EXTENSION_DIR); KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)

.PHONY: test-test-extension-verbose
test-test-extension-verbose: ## Run unit and integration tests with verbose flag
$(MAKE) test-test-extension TEST_ARGS="$(TEST_ARGS) -v"

.PHONY: test-test-extension-junit
test-test-extension-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report for the test extension
cd $(TEST_EXTENSION_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.test_extension.exitcode) | tee $(ARTIFACTS)/junit.test_extension.stdout
cd $(TEST_EXTENSION_DIR); set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.test_extension.exitcode) | tee $(ARTIFACTS)/junit.test_extension.stdout
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.test_extension.xml --raw-command cat $(ARTIFACTS)/junit.test_extension.stdout
exit $$(cat $(ARTIFACTS)/junit.test_extension.exitcode)

Expand Down Expand Up @@ -1197,7 +1197,7 @@ release-notes: release-notes-tool

.PHONY: test-release-notes-tool
test-release-notes-tool:
go test -C hack/tools -v -tags tools,integration sigs.k8s.io/cluster-api/hack/tools/release/notes
go test -race -C hack/tools -v -tags tools,integration sigs.k8s.io/cluster-api/hack/tools/release/notes

.PHONY: release-provider-issues-tool
release-provider-issues-tool: # Creates GitHub issues in a pre-defined list of CAPI provider repositories
Expand Down
13 changes: 6 additions & 7 deletions test/infrastructure/inmemory/pkg/runtime/cache/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func (c *cache) startSyncer(ctx context.Context) error {
c.syncQueue.ShutDown()
}()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivchari I took another look. Let's keep this but make it concurrency safe
l.53

var syncLoopStarted atomic.Bool

l.56

syncLoopStarted.Store(true)

l.85

if !syncLoopStarted.Load() {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SubhasmitaSw
Thank you for commenting about it. Surely, it might be right, but I think it's not necessary. In l.83, we check if all workers starts by atomic.Load(&workers) < int64(c.syncConcurrency) and I believe it's enough to achieve to block data race. Thanks.

Copy link
Member

@sbueringer sbueringer Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check in l.83++ does not include the syncloop (l.53-l.63)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you for your comment. Sorry, I mentioned codes after changing.

syncLoopStarted is only used in codes below

	go func() {
		log.Info("Starting sync loop")
		syncLoopStarted = true
		for {
			select {
			case <-time.After(c.syncPeriod / 4):
				c.syncGroup(ctx)
			case <-ctx.Done():
				return
			}
		}
	}()
	if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, false, func(context.Context) (done bool, err error) {
		if !syncLoopStarted {
			return false, nil
		}
		return true, nil
	}); err != nil {
		return fmt.Errorf("failed to start sync loop: %v", err)
	}

c.syncGroup is originally concurrency program since using goroutine. So it depends on the timing of Go runtime to change syncLoopStarted from false to true. In other words, we don't originally consider the timing when this variable is true. Thus, I think the syncLoopStarted is unnecessary. We can keep concurrency safe here.
What do you think ?

Copy link
Member

@sbueringer sbueringer Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you are saying. Yes the old code was not correctly waiting for syncLoopStarted to be true. So it might not be that important. But I would prefer cleanly waiting for the "syncLoop" go routine to be started compared to not waiting for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I wanted to say is that syncLoopStarted might we can be worth removing. But when I confirmed your previous comment, I'd decide to keep this variable while changing the type to atomic.Bool. Thanks.

syncLoopStarted := false
var syncLoopStarted atomic.Bool
go func() {
log.Info("Starting sync loop")
syncLoopStarted = true
syncLoopStarted.Store(true)
for {
select {
case <-time.After(c.syncPeriod / 4):
Expand All @@ -63,15 +63,14 @@ func (c *cache) startSyncer(ctx context.Context) error {
}
}
}()

var workers int64
var workers atomic.Int64
go func() {
log.Info("Starting sync workers", "count", c.syncConcurrency)
wg := &sync.WaitGroup{}
wg.Add(c.syncConcurrency)
for range c.syncConcurrency {
go func() {
atomic.AddInt64(&workers, 1)
workers.Add(1)
defer wg.Done()
for c.processSyncWorkItem(ctx) {
}
Expand All @@ -82,7 +81,7 @@ func (c *cache) startSyncer(ctx context.Context) error {
}()

if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, false, func(context.Context) (done bool, err error) {
if !syncLoopStarted {
if !syncLoopStarted.Load() {
return false, nil
}
return true, nil
Expand All @@ -91,7 +90,7 @@ func (c *cache) startSyncer(ctx context.Context) error {
}

if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, false, func(context.Context) (done bool, err error) {
if atomic.LoadInt64(&workers) < int64(c.syncConcurrency) {
if workers.Load() < int64(c.syncConcurrency) {
return false, nil
}
return true, nil
Expand Down