-
Notifications
You must be signed in to change notification settings - Fork 0
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
Incremental Configuration Updates #191
Open
oxzi
wants to merge
1
commit into
main
Choose a base branch
from
incremental-config-updates-i5
base: main
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package baseconf | ||
|
||
import ( | ||
"github.com/icinga/icinga-go-library/types" | ||
) | ||
|
||
// IncrementalDbEntry contains the changed_at and deleted columns as struct fields. | ||
// | ||
// This type partially implements config.IncrementalConfigurable with GetChangedAt and IsDeleted. Thus, it can be | ||
// embedded in other types with the _`db:",inline"`_ struct tag. However, most structs might want to embed the | ||
// IncrementalPkDbEntry struct instead. | ||
type IncrementalDbEntry struct { | ||
ChangedAt types.UnixMilli `db:"changed_at"` | ||
Deleted types.Bool `db:"deleted"` | ||
} | ||
|
||
// GetChangedAt returns the changed_at value of this entry from the database. | ||
// | ||
// It is required by the config.IncrementalConfigurable interface. | ||
func (i IncrementalDbEntry) GetChangedAt() types.UnixMilli { | ||
return i.ChangedAt | ||
} | ||
|
||
// IsDeleted indicates if this entry is marked as deleted in the database. | ||
// | ||
// It is required by the config.IncrementalConfigurable interface. | ||
func (i IncrementalDbEntry) IsDeleted() bool { | ||
return i.Deleted.Valid && i.Deleted.Bool | ||
} | ||
|
||
// IncrementalPkDbEntry implements a single primary key named id of a generic type next to IncrementalDbEntry. | ||
// | ||
// This type embeds IncrementalDbEntry and adds a single column/value id field, getting one step closer to implementing | ||
// the config.IncrementalConfigurable interface. Thus, it needs to be embedded with the _`db:",inline"`_ struct tag. | ||
type IncrementalPkDbEntry[PK comparable] struct { | ||
IncrementalDbEntry `db:",inline"` | ||
ID PK `db:"id"` | ||
} | ||
|
||
// GetID returns the id of this entry from the database. | ||
// | ||
// It is required by the config.IncrementalConfigurable interface. | ||
func (i IncrementalPkDbEntry[PK]) GetID() PK { | ||
return i.ID | ||
} |
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 |
---|---|---|
@@ -1,62 +1,54 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/icinga/icinga-notifications/internal/recipient" | ||
"github.com/jmoiron/sqlx" | ||
"go.uber.org/zap" | ||
"slices" | ||
) | ||
|
||
func (r *RuntimeConfig) fetchContacts(ctx context.Context, tx *sqlx.Tx) error { | ||
var contactPtr *recipient.Contact | ||
stmt := r.db.BuildSelectStmt(contactPtr, contactPtr) | ||
r.logger.Debugf("Executing query %q", stmt) | ||
|
||
var contacts []*recipient.Contact | ||
if err := tx.SelectContext(ctx, &contacts, stmt); err != nil { | ||
r.logger.Errorln(err) | ||
return err | ||
} | ||
|
||
contactsByID := make(map[int64]*recipient.Contact) | ||
for _, c := range contacts { | ||
contactsByID[c.ID] = c | ||
|
||
r.logger.Debugw("loaded contact config", | ||
zap.Int64("id", c.ID), | ||
zap.String("name", c.FullName)) | ||
} | ||
|
||
if r.Contacts != nil { | ||
// mark no longer existing contacts for deletion | ||
for id := range r.Contacts { | ||
if _, ok := contactsByID[id]; !ok { | ||
contactsByID[id] = nil | ||
func (r *RuntimeConfig) applyPendingContacts() { | ||
incrementalApplyPending( | ||
r, | ||
&r.Contacts, &r.configChange.Contacts, | ||
nil, | ||
func(curElement, update *recipient.Contact) error { | ||
curElement.FullName = update.FullName | ||
curElement.Username = update.Username | ||
curElement.DefaultChannelID = update.DefaultChannelID | ||
return nil | ||
}, | ||
nil) | ||
|
||
incrementalApplyPending( | ||
r, | ||
&r.ContactAddresses, &r.configChange.ContactAddresses, | ||
func(newElement *recipient.Address) error { | ||
contact, ok := r.Contacts[newElement.ContactID] | ||
if !ok { | ||
return fmt.Errorf("contact address refers unknown contact %d", newElement.ContactID) | ||
} | ||
} | ||
} | ||
|
||
r.pending.Contacts = contactsByID | ||
|
||
return nil | ||
} | ||
|
||
func (r *RuntimeConfig) applyPendingContacts() { | ||
if r.Contacts == nil { | ||
r.Contacts = make(map[int64]*recipient.Contact) | ||
} | ||
contact.Addresses = append(contact.Addresses, newElement) | ||
return nil | ||
}, | ||
func(curElement, update *recipient.Address) error { | ||
if curElement.ContactID != update.ContactID { | ||
return reAddUpdateFnErr | ||
} | ||
|
||
for id, pendingContact := range r.pending.Contacts { | ||
if pendingContact == nil { | ||
delete(r.Contacts, id) | ||
} else if currentContact := r.Contacts[id]; currentContact != nil { | ||
currentContact.FullName = pendingContact.FullName | ||
currentContact.Username = pendingContact.Username | ||
currentContact.DefaultChannelID = pendingContact.DefaultChannelID | ||
} else { | ||
r.Contacts[id] = pendingContact | ||
} | ||
} | ||
curElement.Type = update.Type | ||
curElement.Address = update.Address | ||
return nil | ||
}, | ||
func(delElement *recipient.Address) error { | ||
contact, ok := r.Contacts[delElement.ContactID] | ||
if !ok { | ||
return fmt.Errorf("contact address refers unknown contact %d", delElement.ContactID) | ||
} | ||
|
||
r.pending.Contacts = nil | ||
contact.Addresses = slices.DeleteFunc(contact.Addresses, func(address *recipient.Address) bool { | ||
return address.ID == delElement.ID | ||
}) | ||
return nil | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Previously, the addresses were conditionally added via: