-
Notifications
You must be signed in to change notification settings - Fork 25
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
Recreate listener if error is occured #236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,7 @@ func (f *Default) DirectDB() *sql.DB { | |
return f.db | ||
} | ||
|
||
func waitForNotification(ctx context.Context, l *pq.Listener, callback func(id string)) { | ||
func waitForNotification(ctx context.Context, l *pq.Listener, dbConfig *config.DatabaseConfig, channel string, callback func(id string)) { | ||
logger := ocmlogger.NewOCMLogger(ctx) | ||
for { | ||
select { | ||
|
@@ -149,21 +149,26 @@ func waitForNotification(ctx context.Context, l *pq.Listener, callback func(id s | |
if n != nil { | ||
logger.V(4).Infof("Received event from channel [%s] : %s", n.Channel, n.Extra) | ||
callback(n.Extra) | ||
} else { | ||
// nil notification means the connection was closed | ||
logger.Infof("recreate the listener due to the connection loss") | ||
l.Close() | ||
// recreate the listener | ||
l = newListener(ctx, dbConfig, channel) | ||
} | ||
case <-time.After(10 * time.Second): | ||
logger.V(10).Infof("Received no events on channel during interval. Pinging source") | ||
go func() { | ||
// TODO: Need to handle the error, especially in cases of network failure. | ||
err := l.Ping() | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
}() | ||
if err := l.Ping(); err != nil { | ||
logger.Infof("recreate the listener due to %s", err.Error()) | ||
l.Close() | ||
// recreate the listener | ||
l = newListener(ctx, dbConfig, channel) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func newListener(ctx context.Context, dbConfig *config.DatabaseConfig, channel string, callback func(id string)) { | ||
func newListener(ctx context.Context, dbConfig *config.DatabaseConfig, channel string) *pq.Listener { | ||
logger := ocmlogger.NewOCMLogger(ctx) | ||
|
||
plog := func(ev pq.ListenerEventType, err error) { | ||
|
@@ -189,12 +194,17 @@ func newListener(ctx context.Context, dbConfig *config.DatabaseConfig, channel s | |
panic(err) | ||
} | ||
|
||
logger.Infof("Starting channeling monitor for %s", channel) | ||
waitForNotification(ctx, listener, callback) | ||
return listener | ||
} | ||
|
||
func (f *Default) NewListener(ctx context.Context, channel string, callback func(id string)) { | ||
newListener(ctx, f.config, channel, callback) | ||
func (f *Default) NewListener(ctx context.Context, channel string, callback func(id string)) *pq.Listener { | ||
logger := ocmlogger.NewOCMLogger(ctx) | ||
|
||
listener := newListener(ctx, f.config, channel) | ||
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. how about new the listener in the goroutine waitForNotification? we can maintain the listener in one place, may like
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. if encapsulate the listener into 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. ok ... may add some comments, the returned listener only for test purpose, it cannot be shared by others? |
||
|
||
logger.Infof("Starting channeling monitor for %s", channel) | ||
go waitForNotification(ctx, listener, f.config, channel, callback) | ||
return listener | ||
} | ||
|
||
func (f *Default) New(ctx context.Context) *gorm.DB { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/openshift-online/maestro/test" | ||
) | ||
|
||
func TestWaitForNotification(t *testing.T) { | ||
// it is used to check the result of the notification | ||
result := make(chan string) | ||
|
||
h, _ := test.RegisterIntegration(t) | ||
|
||
account := h.NewRandAccount() | ||
ctx, cancel := context.WithCancel(h.NewAuthenticatedContext(account)) | ||
defer func() { | ||
cancel() | ||
}() | ||
|
||
g2 := h.Env().Database.SessionFactory.New(ctx) | ||
listener := h.Env().Database.SessionFactory.NewListener(ctx, "channel", func(id string) { | ||
result <- id | ||
}) | ||
var originalListenerId string | ||
// find the original listener id in the pg_stat_activity table | ||
g2.Raw("SELECT pid FROM pg_stat_activity WHERE query LIKE 'LISTEN%channel%'").Scan(&originalListenerId) | ||
if originalListenerId == "" { | ||
t.Errorf("the original Listener was not recreated") | ||
} | ||
|
||
// Simulate an errListenerClosed and wait for the listener to be recreated | ||
listener.Close() | ||
|
||
Eventually(func() error { | ||
var newListenerId string | ||
g2.Raw("SELECT pid FROM pg_stat_activity WHERE query LIKE 'LISTEN%channel%'").Scan(&newListenerId) | ||
if newListenerId == "" { | ||
return fmt.Errorf("the new Listener was not created") | ||
} | ||
// Validate the listener was re-created | ||
if originalListenerId == newListenerId { | ||
return fmt.Errorf("Listener was not re-created") | ||
} | ||
return nil | ||
}, 10*time.Second, 1*time.Second).Should(Succeed()) | ||
|
||
// send a notification to the new listener | ||
g2.Exec("NOTIFY channel, 'test'") | ||
|
||
// wait for the notification to be received | ||
time.Sleep(1 * time.Second) | ||
|
||
if <-result != "test" { | ||
t.Errorf("the notification was not received") | ||
} | ||
} |
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.
will this be a problem? it seems we are changing a shared variable without lock. What happened when another is using the listener, and it is changed here?
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 reaching here, it means the listener has problem. it cannot be used anymore.
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.
what happens when a caller calls the listener at the same time, will it panic?
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 listener is initialized when the maestro server is starting.
maestro/cmd/maestro/server/controllers.go
Line 61 in e28d043
so there is no new caller to call the listener. we leverage LISTEN/NOTIFY mechanism to sends a notification event together with an optional “payload” string to listener that has previously executed LISTEN channel.