-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dbnode] Support dynamically registered background processes in media…
…tor (#2634) * [dbnode] Support dynamically registered background processes in mediator * [dbnode] Pass background processes via RunOptions * Add a comment for BackgroundProcess * Add integration test TestRunCustomBackgroundProcess * Include backgroundProcess in TestDatabaseMediatorOpenClose * Move the source of BackgroundProcess asynchrony outside for consistency/easier testing * Fix test * Formatting * Update BackgroundProcess doc * go mod tidy * Verify reporting in TestRunCustomBackgroundProcess * Address review feedback * Resurrect BackgroundProcess.Start() * Enforce mediator.RegisterBackgroundProcess before Open * Remove locking from mediator.Report * Fix comment
- Loading branch information
Showing
14 changed files
with
365 additions
and
60 deletions.
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
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
98 changes: 98 additions & 0 deletions
98
src/dbnode/integration/run_custom_background_process_test.go
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,98 @@ | ||
// +build integration | ||
|
||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package integration | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/m3db/m3/src/dbnode/storage" | ||
xclock "github.com/m3db/m3/src/x/clock" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/uber-go/atomic" | ||
) | ||
|
||
type testBackgroundProcess struct { | ||
executed, reported, stopped atomic.Int32 | ||
} | ||
|
||
func (p *testBackgroundProcess) run() { | ||
xclock.WaitUntil(func() bool { | ||
return p.reported.Load() > 0 | ||
}, time.Minute) | ||
p.executed.Inc() | ||
xclock.WaitUntil(func() bool { | ||
return p.stopped.Load() > 0 | ||
}, time.Minute) | ||
} | ||
|
||
func (p *testBackgroundProcess) Start() { | ||
go p.run() | ||
} | ||
|
||
func (p *testBackgroundProcess) Stop() { | ||
p.stopped.Inc() | ||
return | ||
} | ||
|
||
func (p *testBackgroundProcess) Report() { | ||
p.reported.Inc() | ||
return | ||
} | ||
|
||
func TestRunCustomBackgroundProcess(t *testing.T) { | ||
testOpts := NewTestOptions(t).SetReportInterval(time.Millisecond) | ||
testSetup := newTestSetupWithCommitLogAndFilesystemBootstrapper(t, testOpts) | ||
defer testSetup.Close() | ||
|
||
backgroundProcess := &testBackgroundProcess{} | ||
|
||
storageOpts := testSetup.StorageOpts(). | ||
SetBackgroundProcessFns([]storage.NewBackgroundProcessFn{ | ||
func(storage.Database, storage.Options) (storage.BackgroundProcess, error) { | ||
return backgroundProcess, nil | ||
}}) | ||
testSetup.SetStorageOpts(storageOpts) | ||
|
||
log := storageOpts.InstrumentOptions().Logger() | ||
|
||
// Start the server. | ||
require.NoError(t, testSetup.StartServer()) | ||
|
||
// Stop the server. | ||
defer func() { | ||
require.NoError(t, testSetup.StopServer()) | ||
log.Debug("server is now down") | ||
assert.Equal(t, int32(1), backgroundProcess.stopped.Load(), "failed to stop") | ||
}() | ||
|
||
log.Info("waiting for the custom background process to execute") | ||
xclock.WaitUntil(func() bool { | ||
return backgroundProcess.executed.Load() > 0 | ||
}, time.Minute) | ||
|
||
assert.Equal(t, int32(1), backgroundProcess.executed.Load(), "failed to execute") | ||
assert.True(t, backgroundProcess.reported.Load() > 0, "failed to report") | ||
} |
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
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
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
Oops, something went wrong.