Skip to content

Commit

Permalink
Fix sequence number bug when storage fails
Browse files Browse the repository at this point in the history
  • Loading branch information
letian0805 authored and Letian Yi committed Oct 30, 2023
1 parent 9d2a3c2 commit b85ce2b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,34 +278,34 @@ func (store *fileStore) NextTargetMsgSeqNum() int {

// SetNextSenderMsgSeqNum sets the next MsgSeqNum that will be sent.
func (store *fileStore) SetNextSenderMsgSeqNum(next int) error {
if err := store.cache.SetNextSenderMsgSeqNum(next); err != nil {
return errors.Wrap(err, "cache")
if err := store.setSeqNum(store.senderSeqNumsFile, next); err != nil {
return errors.Wrap(err, "file")
}
return store.setSeqNum(store.senderSeqNumsFile, next)
return store.cache.SetNextSenderMsgSeqNum(next)
}

// SetNextTargetMsgSeqNum sets the next MsgSeqNum that should be received.
func (store *fileStore) SetNextTargetMsgSeqNum(next int) error {
if err := store.cache.SetNextTargetMsgSeqNum(next); err != nil {
return errors.Wrap(err, "cache")
if err := store.setSeqNum(store.targetSeqNumsFile, next); err != nil {
return errors.Wrap(err, "file")
}
return store.setSeqNum(store.targetSeqNumsFile, next)
return store.cache.SetNextTargetMsgSeqNum(next)
}

// IncrNextSenderMsgSeqNum increments the next MsgSeqNum that will be sent.
func (store *fileStore) IncrNextSenderMsgSeqNum() error {
if err := store.cache.IncrNextSenderMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache")
if err := store.SetNextSenderMsgSeqNum(store.cache.NextSenderMsgSeqNum() + 1); err != nil {
return errors.Wrap(err, "file")
}
return store.setSeqNum(store.senderSeqNumsFile, store.cache.NextSenderMsgSeqNum())
return nil
}

// IncrNextTargetMsgSeqNum increments the next MsgSeqNum that should be received.
func (store *fileStore) IncrNextTargetMsgSeqNum() error {
if err := store.cache.IncrNextTargetMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache")
if err := store.SetNextTargetMsgSeqNum(store.cache.NextTargetMsgSeqNum() + 1); err != nil {
return errors.Wrap(err, "file")
}
return store.setSeqNum(store.targetSeqNumsFile, store.cache.NextTargetMsgSeqNum())
return nil
}

// CreationTime returns the creation time of the store.
Expand Down
12 changes: 6 additions & 6 deletions mongostore.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,18 @@ func (store *mongoStore) SetNextTargetMsgSeqNum(next int) error {

// IncrNextSenderMsgSeqNum increments the next MsgSeqNum that will be sent.
func (store *mongoStore) IncrNextSenderMsgSeqNum() error {
if err := store.cache.IncrNextSenderMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache incr")
if err := store.SetNextSenderMsgSeqNum(store.cache.NextSenderMsgSeqNum() + 1); err != nil {
return errors.Wrap(err, "save sequence number")
}
return store.SetNextSenderMsgSeqNum(store.cache.NextSenderMsgSeqNum())
return nil
}

// IncrNextTargetMsgSeqNum increments the next MsgSeqNum that should be received.
func (store *mongoStore) IncrNextTargetMsgSeqNum() error {
if err := store.cache.IncrNextTargetMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache incr")
if err := store.SetNextTargetMsgSeqNum(store.cache.NextTargetMsgSeqNum() + 1); err != nil {
return errors.Wrap(err, "save sequence number")
}
return store.SetNextTargetMsgSeqNum(store.cache.NextTargetMsgSeqNum())
return nil
}

// CreationTime returns the creation time of the store.
Expand Down
12 changes: 6 additions & 6 deletions sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,18 @@ func (store *sqlStore) SetNextTargetMsgSeqNum(next int) error {

// IncrNextSenderMsgSeqNum increments the next MsgSeqNum that will be sent.
func (store *sqlStore) IncrNextSenderMsgSeqNum() error {
if err := store.cache.IncrNextSenderMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache incr next")
if err := store.SetNextSenderMsgSeqNum(store.cache.NextSenderMsgSeqNum() + 1); err != nil {
return errors.Wrap(err, "store next")
}
return store.SetNextSenderMsgSeqNum(store.cache.NextSenderMsgSeqNum())
return nil
}

// IncrNextTargetMsgSeqNum increments the next MsgSeqNum that should be received.
func (store *sqlStore) IncrNextTargetMsgSeqNum() error {
if err := store.cache.IncrNextTargetMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache incr next")
if err := store.SetNextTargetMsgSeqNum(store.cache.NextTargetMsgSeqNum() + 1); err != nil {
return errors.Wrap(err, "store next")
}
return store.SetNextTargetMsgSeqNum(store.cache.NextTargetMsgSeqNum())
return nil
}

// CreationTime returns the creation time of the store.
Expand Down

0 comments on commit b85ce2b

Please sign in to comment.