@@ -140,10 +140,19 @@ func main() {
140140 Use : "finalize" ,
141141 Short : "Finalize Cluster Replication" ,
142142 RunE : func (cmd * cobra.Command , _ []string ) error {
143- return NewClient (port ).Finalize (cmd .Context ())
143+ ignoreHistoryLost , _ := cmd .Flags ().GetBool ("ignore-history-lost" )
144+
145+ finalizeOptions := finalizeRequest {
146+ IgnoreHistoryLost : ignoreHistoryLost ,
147+ }
148+
149+ return NewClient (port ).Finalize (cmd .Context (), finalizeOptions )
144150 },
145151 }
146152
153+ finalizeCmd .Flags ().Bool ("ignore-history-lost" , false , "Ignore history lost error" )
154+ finalizeCmd .Flags ().MarkHidden ("ignore-history-lost" )
155+
147156 pauseCmd := & cobra.Command {
148157 Use : "pause" ,
149158 Short : "Pause Cluster Replication" ,
@@ -618,7 +627,33 @@ func (s *server) handleFinalize(w http.ResponseWriter, r *http.Request) {
618627 return
619628 }
620629
621- err := s .mlink .Finalize (ctx )
630+ var params finalizeRequest
631+
632+ if r .ContentLength != 0 {
633+ data , err := io .ReadAll (r .Body )
634+ if err != nil {
635+ http .Error (w ,
636+ http .StatusText (http .StatusInternalServerError ),
637+ http .StatusInternalServerError )
638+
639+ return
640+ }
641+
642+ err = json .Unmarshal (data , & params )
643+ if err != nil {
644+ http .Error (w ,
645+ http .StatusText (http .StatusBadRequest ),
646+ http .StatusBadRequest )
647+
648+ return
649+ }
650+ }
651+
652+ options := & mongolink.FinalizeOptions {
653+ IgnoreHistoryLost : params .IgnoreHistoryLost ,
654+ }
655+
656+ err := s .mlink .Finalize (ctx , * options )
622657 if err != nil {
623658 writeResponse (w , finalizeResponse {Err : err .Error ()})
624659
@@ -719,6 +754,13 @@ type startResponse struct {
719754 Err string `json:"error,omitempty"`
720755}
721756
757+ // finalizeRequest represents the request body for the /finalize endpoint.
758+ type finalizeRequest struct {
759+ // IgnoreHistoryLost indicates whether the operation can ignore the ChangeStreamHistoryLost
760+ // error.
761+ IgnoreHistoryLost bool `json:"ignoreHistoryLost,omitempty"`
762+ }
763+
722764// finalizeResponse represents the response body for the /finalize endpoint.
723765type finalizeResponse struct {
724766 // Ok indicates if the operation was successful.
@@ -800,13 +842,13 @@ func (c MongoLinkClient) Status(ctx context.Context) error {
800842}
801843
802844// Start sends a request to start the cluster replication.
803- func (c MongoLinkClient ) Start (ctx context.Context , startOptions startRequest ) error {
804- return doClientRequest [startResponse ](ctx , c .port , http .MethodPost , "start" , startOptions )
845+ func (c MongoLinkClient ) Start (ctx context.Context , req startRequest ) error {
846+ return doClientRequest [startResponse ](ctx , c .port , http .MethodPost , "start" , req )
805847}
806848
807849// Finalize sends a request to finalize the cluster replication.
808- func (c MongoLinkClient ) Finalize (ctx context.Context ) error {
809- return doClientRequest [finalizeResponse ](ctx , c .port , http .MethodPost , "finalize" , nil )
850+ func (c MongoLinkClient ) Finalize (ctx context.Context , req finalizeRequest ) error {
851+ return doClientRequest [finalizeResponse ](ctx , c .port , http .MethodPost , "finalize" , req )
810852}
811853
812854// Pause sends a request to pause the cluster replication.
@@ -819,24 +861,24 @@ func (c MongoLinkClient) Resume(ctx context.Context) error {
819861 return doClientRequest [resumeResponse ](ctx , c .port , http .MethodPost , "resume" , nil )
820862}
821863
822- func doClientRequest [T any ](ctx context.Context , port , method , path string , options any ) error {
864+ func doClientRequest [T any ](ctx context.Context , port , method , path string , body any ) error {
823865 url := fmt .Sprintf ("http://localhost:%s/%s" , port , path )
824866
825- data := []byte ("" )
826- if options != nil {
867+ bodyData := []byte ("" )
868+ if body != nil {
827869 var err error
828- data , err = json .Marshal (options )
870+ bodyData , err = json .Marshal (body )
829871 if err != nil {
830872 return errors .Wrap (err , "encode request" )
831873 }
832874 }
833875
834- req , err := http .NewRequestWithContext (ctx , method , url , bytes .NewReader (data ))
876+ req , err := http .NewRequestWithContext (ctx , method , url , bytes .NewReader (bodyData ))
835877 if err != nil {
836878 return errors .Wrap (err , "build request" )
837879 }
838880
839- log .Ctx (ctx ).Debugf ("POST /%s %s" , path , string (data ))
881+ log .Ctx (ctx ).Debugf ("POST /%s %s" , path , string (bodyData ))
840882
841883 res , err := http .DefaultClient .Do (req )
842884 if err != nil {
0 commit comments