feat: reset projections and remove failed events (#2770)

* feat: change failed events to new projection

* feat: change failed events to new projection

* feat: change current sequences to new projection

* feat: add tests

* Update internal/api/grpc/admin/failed_event.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* Update internal/api/grpc/admin/view.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: truncate

* fix reset

* fix reset

* Rename V1.102__queries.sql to V1.103__queries.sql

* improve current_sequence and truncate view tables

* check sub tables of view are tables

* Update internal/query/current_sequence_test.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* fixes and use squirrel

* missing error handling

* lock before reset

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: Silvan <silvan.reusser@gmail.com>
This commit is contained in:
Fabi
2021-12-16 14:44:26 +01:00
committed by GitHub
parent d2ea9a1b8c
commit a43e1fc34a
15 changed files with 821 additions and 40 deletions

View File

@@ -3,19 +3,33 @@ package admin
import (
"context"
"github.com/caos/zitadel/internal/query"
admin_pb "github.com/caos/zitadel/pkg/grpc/admin"
)
func (s *Server) ListFailedEvents(ctx context.Context, req *admin_pb.ListFailedEventsRequest) (*admin_pb.ListFailedEventsResponse, error) {
failedEvents, err := s.administrator.GetFailedEvents(ctx)
failedEventsOld, err := s.administrator.GetFailedEvents(ctx)
if err != nil {
return nil, err
}
return &admin_pb.ListFailedEventsResponse{Result: FailedEventsToPb(failedEvents)}, nil
convertedOld := FailedEventsViewToPb(failedEventsOld)
failedEvents, err := s.query.SearchFailedEvents(ctx, new(query.FailedEventSearchQueries))
if err != nil {
return nil, err
}
convertedNew := FailedEventsToPb(failedEvents)
convertedOld = append(convertedOld, convertedNew...)
return &admin_pb.ListFailedEventsResponse{Result: convertedOld}, nil
}
func (s *Server) RemoveFailedEvent(ctx context.Context, req *admin_pb.RemoveFailedEventRequest) (*admin_pb.RemoveFailedEventResponse, error) {
err := s.administrator.RemoveFailedEvent(ctx, RemoveFailedEventRequestToModel(req))
var err error
if req.Database != "zitadel" {
err = s.administrator.RemoveFailedEvent(ctx, RemoveFailedEventRequestToModel(req))
} else {
err = s.query.RemoveFailedEvent(ctx, req.ViewName, req.FailedSequence)
}
if err != nil {
return nil, err
}

View File

@@ -1,19 +1,20 @@
package admin
import (
"github.com/caos/zitadel/internal/query"
"github.com/caos/zitadel/internal/view/model"
admin_pb "github.com/caos/zitadel/pkg/grpc/admin"
)
func FailedEventsToPb(failedEvents []*model.FailedEvent) []*admin_pb.FailedEvent {
func FailedEventsViewToPb(failedEvents []*model.FailedEvent) []*admin_pb.FailedEvent {
events := make([]*admin_pb.FailedEvent, len(failedEvents))
for i, failedEvent := range failedEvents {
events[i] = FailedEventToPb(failedEvent)
events[i] = FailedEventViewToPb(failedEvent)
}
return events
}
func FailedEventToPb(failedEvent *model.FailedEvent) *admin_pb.FailedEvent {
func FailedEventViewToPb(failedEvent *model.FailedEvent) *admin_pb.FailedEvent {
return &admin_pb.FailedEvent{
Database: failedEvent.Database,
ViewName: failedEvent.ViewName,
@@ -23,6 +24,24 @@ func FailedEventToPb(failedEvent *model.FailedEvent) *admin_pb.FailedEvent {
}
}
func FailedEventsToPb(failedEvents *query.FailedEvents) []*admin_pb.FailedEvent {
events := make([]*admin_pb.FailedEvent, len(failedEvents.FailedEvents))
for i, failedEvent := range failedEvents.FailedEvents {
events[i] = FailedEventToPb(failedEvent)
}
return events
}
func FailedEventToPb(failedEvent *query.FailedEvent) *admin_pb.FailedEvent {
return &admin_pb.FailedEvent{
Database: "zitadel",
ViewName: failedEvent.ProjectionName,
FailedSequence: failedEvent.FailedSequence,
FailureCount: failedEvent.FailureCount,
ErrorMessage: failedEvent.Error,
}
}
func RemoveFailedEventRequestToModel(req *admin_pb.RemoveFailedEventRequest) *model.FailedEvent {
return &model.FailedEvent{
Database: req.Database,

View File

@@ -34,7 +34,7 @@ func TestFailedEventsToPbFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := admin_grpc.FailedEventsToPb(tt.args.failedEvents)
got := admin_grpc.FailedEventsViewToPb(tt.args.failedEvents)
for _, g := range got {
test.AssertFieldsMapped(t, g)
}
@@ -64,7 +64,7 @@ func TestFailedEventToPbFields(t *testing.T) {
},
}
for _, tt := range tests {
converted := admin_grpc.FailedEventToPb(tt.args.failedEvent)
converted := admin_grpc.FailedEventViewToPb(tt.args.failedEvent)
test.AssertFieldsMapped(t, converted)
}
}

View File

@@ -3,19 +3,33 @@ package admin
import (
"context"
"github.com/caos/zitadel/internal/query"
admin_pb "github.com/caos/zitadel/pkg/grpc/admin"
)
func (s *Server) ListViews(context.Context, *admin_pb.ListViewsRequest) (*admin_pb.ListViewsResponse, error) {
func (s *Server) ListViews(ctx context.Context, _ *admin_pb.ListViewsRequest) (*admin_pb.ListViewsResponse, error) {
currentSequences, err := s.query.SearchCurrentSequences(ctx, new(query.CurrentSequencesSearchQueries))
if err != nil {
return nil, err
}
convertedCurrentSequences := CurrentSequencesToPb(currentSequences)
views, err := s.administrator.GetViews()
if err != nil {
return nil, err
}
return &admin_pb.ListViewsResponse{Result: ViewsToPb(views)}, nil
convertedViews := ViewsToPb(views)
convertedCurrentSequences = append(convertedCurrentSequences, convertedViews...)
return &admin_pb.ListViewsResponse{Result: convertedCurrentSequences}, nil
}
func (s *Server) ClearView(ctx context.Context, req *admin_pb.ClearViewRequest) (*admin_pb.ClearViewResponse, error) {
err := s.administrator.ClearView(ctx, req.Database, req.ViewName)
var err error
if req.Database != "zitadel" {
err = s.administrator.ClearView(ctx, req.Database, req.ViewName)
} else {
err = s.query.ClearCurrentSequence(ctx, req.ViewName)
}
if err != nil {
return nil, err
}

View File

@@ -1,10 +1,10 @@
package admin
import (
"github.com/caos/logging"
"github.com/caos/zitadel/internal/query"
"github.com/caos/zitadel/internal/view/model"
admin_pb "github.com/caos/zitadel/pkg/grpc/admin"
"github.com/golang/protobuf/ptypes"
"google.golang.org/protobuf/types/known/timestamppb"
)
func ViewsToPb(views []*model.View) []*admin_pb.View {
@@ -16,17 +16,28 @@ func ViewsToPb(views []*model.View) []*admin_pb.View {
}
func ViewToPb(view *model.View) *admin_pb.View {
lastSuccessfulSpoolerRun, err := ptypes.TimestampProto(view.LastSuccessfulSpoolerRun)
logging.Log("ADMIN-4zs01").OnError(err).Debug("unable to parse last successful spooler run")
eventTs, err := ptypes.TimestampProto(view.EventTimestamp)
logging.Log("ADMIN-q2Wzj").OnError(err).Debug("unable to parse event timestamp")
return &admin_pb.View{
Database: view.Database,
ViewName: view.ViewName,
LastSuccessfulSpoolerRun: lastSuccessfulSpoolerRun,
LastSuccessfulSpoolerRun: timestamppb.New(view.LastSuccessfulSpoolerRun),
ProcessedSequence: view.CurrentSequence,
EventTimestamp: eventTs,
EventTimestamp: timestamppb.New(view.EventTimestamp),
}
}
func CurrentSequencesToPb(currentSequences *query.CurrentSequences) []*admin_pb.View {
v := make([]*admin_pb.View, len(currentSequences.CurrentSequences))
for i, currentSequence := range currentSequences.CurrentSequences {
v[i] = CurrentSequenceToPb(currentSequence)
}
return v
}
func CurrentSequenceToPb(currentSequence *query.CurrentSequence) *admin_pb.View {
return &admin_pb.View{
Database: "zitadel",
ViewName: currentSequence.ProjectionName,
ProcessedSequence: currentSequence.CurrentSequence,
EventTimestamp: timestamppb.New(currentSequence.Timestamp),
}
}