This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
WIP: PostgreSQL LISTEN/NOTIFY: invalidate wit cache #2172
Open
kwk
wants to merge
8
commits into
fabric8-services:master
Choose a base branch
from
kwk:invalidate_wit_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
35ad6db
Invalidate WIT cache on all pods
kwk 04d3c8c
Add channel to subscriber callback
kwk 0bd16a6
Added tests
kwk 1bfe26e
Add doc
kwk 6857c09
Test that migration sends the correct signal
kwk 5caadbd
more error checking
kwk b227b9f
Fixup
kwk 68f445a
Merge branch 'master' into invalidate_wit_cache
kwk 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package gormsupport | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/fabric8-services/fabric8-wit/configuration" | ||
"github.com/fabric8-services/fabric8-wit/log" | ||
"github.com/lib/pq" | ||
errs "github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// ChanSpaceTemplateUpdates is the name for the postgres notification | ||
// channel on which subscribers are informed about updates to the space | ||
// templates (e.g. when a migration has happened). | ||
ChanSpaceTemplateUpdates = "f8_space_template_updates" | ||
) | ||
|
||
// A SubscriberFunc describes the function signature that a subscriber needs to | ||
// have. The channel parameter is just an arbitrary identifier string the | ||
// identities a channel. The extra parameter is can contain optional data that | ||
// was sent along with the notification. | ||
type SubscriberFunc func(channel, extra string) | ||
|
||
// SetupDatabaseListener sets up a Postgres LISTEN/NOTIFY connection and listens | ||
// on events that we have subscribers for. | ||
func SetupDatabaseListener(config configuration.Registry, subscribers map[string]SubscriberFunc) error { | ||
if len(subscribers) == 0 { | ||
return nil | ||
} | ||
|
||
dbConnectCallback := func(ev pq.ListenerEventType, err error) { | ||
switch ev { | ||
case pq.ListenerEventConnected: | ||
log.Logger().Infof("database connection for LISTEN/NOTIFY established successfully") | ||
case pq.ListenerEventDisconnected: | ||
log.Logger().Errorf("lost LISTEN/NOTIFY database connection: %+v", err) | ||
case pq.ListenerEventReconnected: | ||
log.Logger().Infof("database connection for LISTEN/NOTIFY re-established successfully") | ||
case pq.ListenerEventConnectionAttemptFailed: | ||
log.Logger().Errorf("failed to connect to database for LISTEN/NOTIFY: %+v", err) | ||
} | ||
} | ||
|
||
listener := pq.NewListener(config.GetPostgresConfigString(), config.GetPostgresListenNotifyMinReconnectInterval(), config.GetPostgresListenNotifyMaxReconnectInterval(), dbConnectCallback) | ||
|
||
// listen on every subscribed channel | ||
for channel := range subscribers { | ||
err := listener.Listen(channel) | ||
if err != nil { | ||
log.Logger().Errorf("unable to open connection to database for LISTEN/NOTIFY %v", err) | ||
return errs.Wrapf(err, "failed listen to postgres channel \"%s\"", channel) | ||
} | ||
} | ||
|
||
// asynchronously handle notifications | ||
go func() { | ||
for { | ||
select { | ||
case n := <-listener.Notify: | ||
sub, ok := subscribers[n.Channel] | ||
if ok { | ||
log.Logger().Debugf("received notification from postgres channel \"%s\": %s", n.Channel, n.Extra) | ||
sub(n.Channel, n.Extra) | ||
} | ||
case <-time.After(90 * time.Second): | ||
log.Logger().Infof("received no events for 90 seconds, checking connection") | ||
go func() { | ||
err := listener.Ping() | ||
if err != nil { | ||
log.Panic(nil, map[string]interface{}{ | ||
"err": err, | ||
}, "failed to ping for LISTEN/NOTIFY database connection") | ||
} | ||
}() | ||
} | ||
} | ||
}() | ||
return nil | ||
} |
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,71 @@ | ||
package gormsupport_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/fabric8-services/fabric8-wit/gormsupport" | ||
"github.com/fabric8-services/fabric8-wit/gormtestsupport" | ||
"github.com/fabric8-services/fabric8-wit/migration" | ||
"github.com/fabric8-services/fabric8-wit/resource" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type TestListenerSuite struct { | ||
gormtestsupport.DBTestSuite | ||
} | ||
|
||
func TestListener(t *testing.T) { | ||
resource.Require(t, resource.Database) | ||
suite.Run(t, &TestListenerSuite{DBTestSuite: gormtestsupport.NewDBTestSuite()}) | ||
} | ||
|
||
func (s *TestListenerSuite) TestSetupDatabaseListener() { | ||
s.T().Run("setup listener", func(t *testing.T) { | ||
// given | ||
channelName := "f8_custom_event_channel" | ||
payload := "some additional info about the event" | ||
wg := sync.WaitGroup{} | ||
wg.Add(2) | ||
var executedMigration bool | ||
|
||
err := gormsupport.SetupDatabaseListener(*s.Configuration, map[string]gormsupport.SubscriberFunc{ | ||
// This is the channel we send to from this test | ||
channelName: func(channel, extra string) { | ||
t.Logf("received notification on channel %s: %s", channel, extra) | ||
require.Equal(t, channelName, channel) | ||
require.Equal(t, payload, extra) | ||
wg.Done() | ||
}, | ||
// This is the channel that we send to from | ||
// migration.PopulateCommonTypes() which is called by | ||
// gormtestsupport.DBTestSuite internally. | ||
gormsupport.ChanSpaceTemplateUpdates: func(channel, extra string) { | ||
// potentially the migration is executed twice but we're only | ||
// interested in one event. | ||
if !executedMigration { | ||
executedMigration = true | ||
t.Logf("received notification on channel %s: %s", channel, extra) | ||
require.Equal(t, gormsupport.ChanSpaceTemplateUpdates, channel) | ||
require.Equal(t, "", extra) | ||
wg.Done() | ||
} | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Send a notification from a completely different connection than the | ||
// one we established to listen to channels. | ||
db := s.DB.Debug().Exec("SELECT pg_notify($1, $2)", channelName, payload) | ||
require.NoError(t, db.Error) | ||
|
||
// This will send a notification on the | ||
// gormsupport.ChanSpaceTemplateUpdates channel | ||
err = migration.PopulateCommonTypes(nil, s.DB) | ||
require.NoError(t, err) | ||
|
||
// wait until notification was received | ||
wg.Wait() | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -9,6 +9,9 @@ import ( | |
"runtime" | ||
"time" | ||
|
||
"github.com/fabric8-services/fabric8-wit/gormsupport" | ||
"github.com/fabric8-services/fabric8-wit/workitem" | ||
|
||
"github.com/fabric8-services/fabric8-wit/closeable" | ||
|
||
"github.com/fabric8-services/fabric8-wit/account" | ||
|
@@ -140,6 +143,13 @@ func main() { | |
os.Exit(0) | ||
} | ||
|
||
// Ensure we delete the work item cache when we receive a notification from postgres | ||
gormsupport.SetupDatabaseListener(*config, map[string]gormsupport.SubscriberFunc{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I will have a look when this PR becomes more relevant. |
||
gormsupport.ChanSpaceTemplateUpdates: func(channel, extra string) { | ||
workitem.ClearGlobalWorkItemTypeCache() | ||
}, | ||
}) | ||
|
||
// Make sure the database is populated with the correct types (e.g. bug etc.) | ||
if config.GetPopulateCommonTypes() { | ||
ctx := migration.NewMigrationContext(context.Background()) | ||
|
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
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.
The extra parameter
iscan contain