invoices: fix synchronization issue with single invoice subscribers

This commit fixes a synchronization issue where a single invoice
subscriber could receive duplicate and/or out of order invoice updates.
This commit is contained in:
Joost Jager 2019-08-09 12:22:53 +02:00
parent 9ab23de197
commit 395e0596c2
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
3 changed files with 73 additions and 42 deletions

@ -67,9 +67,11 @@ type InvoiceRegistry struct {
singleNotificationClients map[uint32]*SingleInvoiceSubscription singleNotificationClients map[uint32]*SingleInvoiceSubscription
newSubscriptions chan *InvoiceSubscription newSubscriptions chan *InvoiceSubscription
newSingleSubscriptions chan *SingleInvoiceSubscription
subscriptionCancels chan uint32 subscriptionCancels chan uint32
invoiceEvents chan *invoiceEvent
// invoiceEvents is a single channel over which both invoice updates and
// new single invoice subscriptions are carried.
invoiceEvents chan interface{}
// debugInvoices is a map which stores special "debug" invoices which // debugInvoices is a map which stores special "debug" invoices which
// should be only created/used when manual tests require an invoice // should be only created/used when manual tests require an invoice
@ -112,9 +114,8 @@ func NewRegistry(cdb *channeldb.DB, decodeFinalCltvExpiry func(invoice string) (
notificationClients: make(map[uint32]*InvoiceSubscription), notificationClients: make(map[uint32]*InvoiceSubscription),
singleNotificationClients: make(map[uint32]*SingleInvoiceSubscription), singleNotificationClients: make(map[uint32]*SingleInvoiceSubscription),
newSubscriptions: make(chan *InvoiceSubscription), newSubscriptions: make(chan *InvoiceSubscription),
newSingleSubscriptions: make(chan *SingleInvoiceSubscription),
subscriptionCancels: make(chan uint32), subscriptionCancels: make(chan uint32),
invoiceEvents: make(chan *invoiceEvent, 100), invoiceEvents: make(chan interface{}, 100),
hodlSubscriptions: make(map[lntypes.Hash]map[chan<- interface{}]struct{}), hodlSubscriptions: make(map[lntypes.Hash]map[chan<- interface{}]struct{}),
hodlReverseSubscriptions: make(map[chan<- interface{}]map[lntypes.Hash]struct{}), hodlReverseSubscriptions: make(map[chan<- interface{}]map[lntypes.Hash]struct{}),
decodeFinalCltvExpiry: decodeFinalCltvExpiry, decodeFinalCltvExpiry: decodeFinalCltvExpiry,
@ -176,23 +177,6 @@ func (i *InvoiceRegistry) invoiceEventNotifier() {
// continue. // continue.
i.notificationClients[newClient.id] = newClient i.notificationClients[newClient.id] = newClient
// A new single invoice subscription has arrived. We'll query
// for any backlog notifications, then add it to the set of
// clients.
case newClient := <-i.newSingleSubscriptions:
err := i.deliverSingleBacklogEvents(newClient)
if err != nil {
log.Errorf("Unable to deliver backlog invoice "+
"notifications: %v", err)
}
log.Infof("New single invoice subscription "+
"client: id=%v, hash=%v",
newClient.id, newClient.hash,
)
i.singleNotificationClients[newClient.id] = newClient
// A client no longer wishes to receive invoice notifications. // A client no longer wishes to receive invoice notifications.
// So we'll remove them from the set of active clients. // So we'll remove them from the set of active clients.
case clientID := <-i.subscriptionCancels: case clientID := <-i.subscriptionCancels:
@ -202,18 +186,39 @@ func (i *InvoiceRegistry) invoiceEventNotifier() {
delete(i.notificationClients, clientID) delete(i.notificationClients, clientID)
delete(i.singleNotificationClients, clientID) delete(i.singleNotificationClients, clientID)
// A sub-systems has just modified the invoice state, so we'll // An invoice event has come in. This can either be an update to
// dispatch notifications to all registered clients. // an invoice or a new single invoice subscriber. Both type of
// events are passed in via the same channel, to make sure that
// subscribers get a consistent view of the event sequence.
case event := <-i.invoiceEvents: case event := <-i.invoiceEvents:
// For backwards compatibility, do not notify all switch e := event.(type) {
// invoice subscribers of cancel and accept events.
state := event.invoice.Terms.State // A sub-systems has just modified the invoice state, so
// we'll dispatch notifications to all registered
// clients.
case *invoiceEvent:
// For backwards compatibility, do not notify
// all invoice subscribers of cancel and accept
// events.
state := e.invoice.Terms.State
if state != channeldb.ContractCanceled && if state != channeldb.ContractCanceled &&
state != channeldb.ContractAccepted { state != channeldb.ContractAccepted {
i.dispatchToClients(event) i.dispatchToClients(e)
}
i.dispatchToSingleClients(e)
// A new single invoice subscription has arrived. Add it
// to the set of clients. It is important to do this in
// sequence with any other invoice events, because an
// initial invoice update has already been sent out to
// the subscriber.
case *SingleInvoiceSubscription:
log.Infof("New single invoice subscription "+
"client: id=%v, hash=%v", e.id, e.hash)
i.singleNotificationClients[e.id] = e
} }
i.dispatchToSingleClients(event)
case <-i.quit: case <-i.quit:
return return
@ -367,7 +372,9 @@ func (i *InvoiceRegistry) deliverSingleBacklogEvents(
// It is possible that the invoice does not exist yet, but the client is // It is possible that the invoice does not exist yet, but the client is
// already watching it in anticipation. // already watching it in anticipation.
if err == channeldb.ErrInvoiceNotFound { if err == channeldb.ErrInvoiceNotFound ||
err == channeldb.ErrNoInvoicesCreated {
return nil return nil
} }
if err != nil { if err != nil {
@ -896,7 +903,7 @@ func (i *InvoiceRegistry) SubscribeNotifications(addIndex, settleIndex uint64) *
// SubscribeSingleInvoice returns an SingleInvoiceSubscription which allows the // SubscribeSingleInvoice returns an SingleInvoiceSubscription which allows the
// caller to receive async notifications for a specific invoice. // caller to receive async notifications for a specific invoice.
func (i *InvoiceRegistry) SubscribeSingleInvoice( func (i *InvoiceRegistry) SubscribeSingleInvoice(
hash lntypes.Hash) *SingleInvoiceSubscription { hash lntypes.Hash) (*SingleInvoiceSubscription, error) {
client := &SingleInvoiceSubscription{ client := &SingleInvoiceSubscription{
Updates: make(chan *channeldb.Invoice), Updates: make(chan *channeldb.Invoice),
@ -949,12 +956,24 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice(
} }
}() }()
select { // Within the lock, we both query the invoice state and pass the client
case i.newSingleSubscriptions <- client: // subscription to the invoiceEvents channel. This is to make sure that
case <-i.quit: // the client receives a consistent stream of events.
i.Lock()
defer i.Unlock()
err := i.deliverSingleBacklogEvents(client)
if err != nil {
return nil, err
} }
return client select {
case i.invoiceEvents <- client:
case <-i.quit:
return nil, ErrShuttingDown
}
return client, nil
} }
// notifyHodlSubscribers sends out the hodl event to all current subscribers. // notifyHodlSubscribers sends out the hodl event to all current subscribers.

@ -71,7 +71,10 @@ func TestSettleInvoice(t *testing.T) {
defer allSubscriptions.Cancel() defer allSubscriptions.Cancel()
// Subscribe to the not yet existing invoice. // Subscribe to the not yet existing invoice.
subscription := registry.SubscribeSingleInvoice(hash) subscription, err := registry.SubscribeSingleInvoice(hash)
if err != nil {
t.Fatal(err)
}
defer subscription.Cancel() defer subscription.Cancel()
if subscription.hash != hash { if subscription.hash != hash {
@ -225,7 +228,10 @@ func TestCancelInvoice(t *testing.T) {
} }
// Subscribe to the not yet existing invoice. // Subscribe to the not yet existing invoice.
subscription := registry.SubscribeSingleInvoice(hash) subscription, err := registry.SubscribeSingleInvoice(hash)
if err != nil {
t.Fatal(err)
}
defer subscription.Cancel() defer subscription.Cancel()
if subscription.hash != hash { if subscription.hash != hash {
@ -329,7 +335,10 @@ func TestHoldInvoice(t *testing.T) {
defer allSubscriptions.Cancel() defer allSubscriptions.Cancel()
// Subscribe to the not yet existing invoice. // Subscribe to the not yet existing invoice.
subscription := registry.SubscribeSingleInvoice(hash) subscription, err := registry.SubscribeSingleInvoice(hash)
if err != nil {
t.Fatal(err)
}
defer subscription.Cancel() defer subscription.Cancel()
if subscription.hash != hash { if subscription.hash != hash {

@ -178,7 +178,10 @@ func (s *Server) SubscribeSingleInvoice(req *SubscribeSingleInvoiceRequest,
return err return err
} }
invoiceClient := s.cfg.InvoiceRegistry.SubscribeSingleInvoice(hash) invoiceClient, err := s.cfg.InvoiceRegistry.SubscribeSingleInvoice(hash)
if err != nil {
return err
}
defer invoiceClient.Cancel() defer invoiceClient.Cancel()
for { for {