-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
388 additions
and
225 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -10,7 +10,9 @@ import ( | |
"errors" | ||
"fmt" | ||
"log/slog" | ||
"maps" | ||
"os" | ||
"slices" | ||
"sync" | ||
"sync/atomic" | ||
|
||
|
@@ -35,13 +37,15 @@ import ( | |
//go:generate go run github.com/maxbrunsfeld/counterfeiter/[email protected] -generate | ||
//counterfeiter:generate . fileManagerServiceInterface | ||
|
||
const maxAttempts = 5 | ||
|
||
type ( | ||
fileOperator interface { | ||
Write(ctx context.Context, fileContent []byte, file *mpi.FileMeta) error | ||
} | ||
|
||
fileManagerServiceInterface interface { | ||
UpdateOverview(ctx context.Context, instanceID string, filesToUpdate []*mpi.File) error | ||
UpdateOverview(ctx context.Context, instanceID string, filesToUpdate []*mpi.File, iteration int) error | ||
ConfigApply(ctx context.Context, configApplyRequest *mpi.ConfigApplyRequest) (writeStatus model.WriteStatus, | ||
err error) | ||
Rollback(ctx context.Context, instanceID string) error | ||
|
@@ -87,18 +91,21 @@ func (fms *FileManagerService) UpdateOverview( | |
ctx context.Context, | ||
instanceID string, | ||
filesToUpdate []*mpi.File, | ||
iteration int, | ||
) error { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
requestCorrelationID := logger.GenerateCorrelationID() | ||
newCtx := context.WithValue(ctx, logger.CorrelationIDContextKey, requestCorrelationID) | ||
|
||
slog.InfoContext(newCtx, "Updating file overview", "instance_id", instanceID, | ||
"parent_correlation_id", correlationID) | ||
// error case for the UpdateOverview attempts | ||
if iteration > maxAttempts { | ||
return errors.New("too many UpdateOverview attempts") | ||
} | ||
|
||
newCtx, correlationID := fms.setupIdentifiers(ctx, iteration) | ||
|
||
request := &mpi.UpdateOverviewRequest{ | ||
MessageMeta: &mpi.MessageMeta{ | ||
MessageId: proto.GenerateMessageID(), | ||
CorrelationId: requestCorrelationID.Value.String(), | ||
CorrelationId: correlationID, | ||
Timestamp: timestamppb.Now(), | ||
}, | ||
Overview: &mpi.FileOverview{ | ||
|
@@ -147,9 +154,57 @@ func (fms *FileManagerService) UpdateOverview( | |
|
||
slog.DebugContext(newCtx, "UpdateOverview response", "response", response) | ||
|
||
if response.GetOverview() == nil { | ||
slog.Debug("UpdateOverview response is empty") | ||
return nil | ||
} | ||
delta := files.ConvertToMapOfFiles(response.GetOverview().GetFiles()) | ||
|
||
if len(delta) != 0 { | ||
return fms.updateFiles(ctx, delta, instanceID, iteration) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (fms *FileManagerService) setupIdentifiers(ctx context.Context, iteration int) (context.Context, string) { | ||
correlationID := logger.GetCorrelationID(ctx) | ||
var requestCorrelationID slog.Attr | ||
|
||
if iteration == 0 { | ||
requestCorrelationID = logger.GenerateCorrelationID() | ||
} else { | ||
requestCorrelationID = logger.GetCorrelationIDAttr(ctx) | ||
} | ||
|
||
newCtx := context.WithValue(ctx, logger.CorrelationIDContextKey, requestCorrelationID) | ||
slog.InfoContext(newCtx, "Updating file overview", "instance_id", logger.GetCorrelationIDAttr(ctx), | ||
"parent_correlation_id", correlationID) | ||
|
||
return newCtx, correlationID | ||
} | ||
|
||
func (fms *FileManagerService) updateFiles( | ||
ctx context.Context, | ||
delta map[string]*mpi.File, | ||
instanceID string, | ||
iteration int, | ||
) error { | ||
diffFiles := slices.Collect(maps.Values(delta)) | ||
|
||
for _, file := range diffFiles { | ||
updateErr := fms.UpdateFile(ctx, instanceID, file) | ||
if updateErr != nil { | ||
return updateErr | ||
} | ||
} | ||
|
||
iteration++ | ||
slog.Debug("Updating file overview", "attempt_number", iteration) | ||
|
||
return fms.UpdateOverview(ctx, instanceID, diffFiles, iteration) | ||
} | ||
|
||
func (fms *FileManagerService) UpdateFile( | ||
ctx context.Context, | ||
instanceID string, | ||
|
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
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
18 changes: 10 additions & 8 deletions
18
internal/file/filefakes/fake_file_manager_service_interface.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.