mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:07:30 +00:00
feat: handle instanceID in projections (#3442)
* feat: handle instanceID in projections * rename functions * fix key lock * fix import
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
"strings"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
view_model "github.com/caos/zitadel/internal/view/model"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
errViewNameKey = "view_name"
|
||||
errFailedSeqKey = "failed_sequence"
|
||||
)
|
||||
|
||||
type FailedEvent struct {
|
||||
@@ -19,6 +15,7 @@ type FailedEvent struct {
|
||||
FailedSequence uint64 `gorm:"column:failed_sequence;primary_key"`
|
||||
FailureCount uint64 `gorm:"column:failure_count"`
|
||||
ErrMsg string `gorm:"column:err_msg"`
|
||||
InstanceID string `gorm:"column:instance_id"`
|
||||
}
|
||||
|
||||
type FailedEventSearchQuery struct {
|
||||
@@ -45,6 +42,7 @@ const (
|
||||
FailedEventKeyUndefined FailedEventSearchKey = iota
|
||||
FailedEventKeyViewName
|
||||
FailedEventKeyFailedSequence
|
||||
FailedEventKeyInstanceID
|
||||
)
|
||||
|
||||
type failedEventSearchKey FailedEventSearchKey
|
||||
@@ -55,6 +53,8 @@ func (key failedEventSearchKey) ToColumnName() string {
|
||||
return "view_name"
|
||||
case FailedEventKeyFailedSequence:
|
||||
return "failed_sequence"
|
||||
case FailedEventKeyInstanceID:
|
||||
return "instance_id"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
@@ -93,15 +93,17 @@ func RemoveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) erro
|
||||
delete := PrepareDeleteByKeys(table,
|
||||
Key{Key: failedEventSearchKey(FailedEventKeyViewName), Value: failedEvent.ViewName},
|
||||
Key{Key: failedEventSearchKey(FailedEventKeyFailedSequence), Value: failedEvent.FailedSequence},
|
||||
Key{Key: failedEventSearchKey(FailedEventKeyInstanceID), Value: failedEvent.InstanceID},
|
||||
)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func LatestFailedEvent(db *gorm.DB, table, viewName string, sequence uint64) (*FailedEvent, error) {
|
||||
func LatestFailedEvent(db *gorm.DB, table, viewName, instanceID string, sequence uint64) (*FailedEvent, error) {
|
||||
failedEvent := new(FailedEvent)
|
||||
queries := []SearchQuery{
|
||||
FailedEventSearchQuery{Key: FailedEventKeyViewName, Method: domain.SearchMethodEqualsIgnoreCase, Value: viewName},
|
||||
FailedEventSearchQuery{Key: FailedEventKeyFailedSequence, Method: domain.SearchMethodEquals, Value: sequence},
|
||||
FailedEventSearchQuery{Key: FailedEventKeyInstanceID, Method: domain.SearchMethodEquals, Value: instanceID},
|
||||
}
|
||||
query := PrepareGetByQuery(table, queries...)
|
||||
err := query(db, failedEvent)
|
||||
|
@@ -16,6 +16,7 @@ type CurrentSequence struct {
|
||||
CurrentSequence uint64 `gorm:"column:current_sequence"`
|
||||
EventTimestamp time.Time `gorm:"column:event_timestamp"`
|
||||
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
|
||||
InstanceID string `gorm:"column:instance_id;primary_key"`
|
||||
}
|
||||
|
||||
type currentSequenceViewWithSequence struct {
|
||||
@@ -35,6 +36,7 @@ const (
|
||||
SequenceSearchKeyUndefined SequenceSearchKey = iota
|
||||
SequenceSearchKeyViewName
|
||||
SequenceSearchKeyAggregateType
|
||||
SequenceSearchKeyInstanceID
|
||||
)
|
||||
|
||||
type sequenceSearchKey SequenceSearchKey
|
||||
@@ -45,6 +47,8 @@ func (key sequenceSearchKey) ToColumnName() string {
|
||||
return "view_name"
|
||||
case SequenceSearchKeyAggregateType:
|
||||
return "aggregate_type"
|
||||
case SequenceSearchKeyInstanceID:
|
||||
return "instance_id"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
@@ -67,6 +71,34 @@ func (q *sequenceSearchQuery) GetValue() interface{} {
|
||||
return q.value
|
||||
}
|
||||
|
||||
type sequenceSearchRequest struct {
|
||||
queries []sequenceSearchQuery
|
||||
}
|
||||
|
||||
func (s *sequenceSearchRequest) GetLimit() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *sequenceSearchRequest) GetOffset() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *sequenceSearchRequest) GetSortingColumn() ColumnKey {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sequenceSearchRequest) GetAsc() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *sequenceSearchRequest) GetQueries() []SearchQuery {
|
||||
result := make([]SearchQuery, len(s.queries))
|
||||
for i, q := range s.queries {
|
||||
result[i] = &sequenceSearchQuery{key: q.key, value: q.value}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
|
||||
dbView := strings.Split(sequence.ViewName, ".")
|
||||
return &model.View{
|
||||
@@ -78,8 +110,17 @@ func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
|
||||
}
|
||||
}
|
||||
|
||||
func SaveCurrentSequence(db *gorm.DB, table, viewName string, sequence uint64, eventTimestamp time.Time) error {
|
||||
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now()})
|
||||
func SaveCurrentSequence(db *gorm.DB, table, viewName, instanceID string, sequence uint64, eventTimestamp time.Time) error {
|
||||
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now(), instanceID})
|
||||
}
|
||||
|
||||
func SaveCurrentSequences(db *gorm.DB, table, viewName string, sequence uint64, eventTimestamp time.Time) error {
|
||||
err := db.Table(table).Where("view_name = ?", viewName).
|
||||
Updates(map[string]interface{}{"current_sequence": sequence, "event_timestamp": eventTimestamp, "last_successful_spooler_run": time.Now()}).Error
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "VIEW-Sfdqs", "unable to updated processed sequence")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) (err error) {
|
||||
@@ -91,9 +132,24 @@ func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSe
|
||||
return nil
|
||||
}
|
||||
|
||||
func LatestSequence(db *gorm.DB, table, viewName string) (*CurrentSequence, error) {
|
||||
searchQueries := make([]SearchQuery, 0, 2)
|
||||
searchQueries = append(searchQueries, &sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyViewName), value: viewName})
|
||||
func UpdateCurrentSequences(db *gorm.DB, table string, currentSequences []*CurrentSequence) (err error) {
|
||||
save := PrepareBulkSave(table)
|
||||
s := make([]interface{}, len(currentSequences))
|
||||
for i, currentSequence := range currentSequences {
|
||||
s[i] = currentSequence
|
||||
}
|
||||
err = save(db, s...)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "VIEW-5kOhP", "unable to updated processed sequence")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LatestSequence(db *gorm.DB, table, viewName, instanceID string) (*CurrentSequence, error) {
|
||||
searchQueries := []SearchQuery{
|
||||
&sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyViewName), value: viewName},
|
||||
&sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyInstanceID), value: instanceID},
|
||||
}
|
||||
|
||||
// ensure highest sequence of view
|
||||
db = db.Order("current_sequence DESC")
|
||||
@@ -112,6 +168,27 @@ func LatestSequence(db *gorm.DB, table, viewName string) (*CurrentSequence, erro
|
||||
return nil, caos_errs.ThrowInternalf(err, "VIEW-9LyCB", "unable to get latest sequence of %s", viewName)
|
||||
}
|
||||
|
||||
func LatestSequences(db *gorm.DB, table, viewName string) ([]*CurrentSequence, error) {
|
||||
searchQueries := make([]SearchQuery, 0, 2)
|
||||
searchQueries = append(searchQueries)
|
||||
searchRequest := &sequenceSearchRequest{
|
||||
queries: []sequenceSearchQuery{
|
||||
{key: sequenceSearchKey(SequenceSearchKeyViewName), value: viewName},
|
||||
},
|
||||
}
|
||||
|
||||
// ensure highest sequence of view
|
||||
db = db.Order("current_sequence DESC")
|
||||
|
||||
sequences := make([]*CurrentSequence, 0)
|
||||
query := PrepareSearchQuery(table, searchRequest)
|
||||
_, err := query(db, &sequences)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sequences, nil
|
||||
}
|
||||
|
||||
func AllCurrentSequences(db *gorm.DB, table string) ([]*CurrentSequence, error) {
|
||||
sequences := make([]*CurrentSequence, 0)
|
||||
query := PrepareSearchQuery(table, GeneralSearchRequest{})
|
||||
@@ -128,5 +205,5 @@ func ClearView(db *gorm.DB, truncateView, sequenceTable string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return SaveCurrentSequence(db, sequenceTable, truncateView, 0, time.Now())
|
||||
return SaveCurrentSequences(db, sequenceTable, truncateView, 0, time.Now())
|
||||
}
|
||||
|
Reference in New Issue
Block a user