@@ -604,24 +604,48 @@ func (s *Server) UpsertAndReleaseConfigFile(ctx context.Context,
604604
605605 tx , err := s .storage .StartTx ()
606606 if err != nil {
607- log .Error ("[Config][File] upsert config file when begin tx." , utils .RequestID (ctx ), zap .Error (err ))
607+ log .Error ("[Config][File] upsert config file when begin tx." , utils .RequestID (ctx ),
608+ zap .String ("namespace" , req .GetNamespace ().GetValue ()), zap .String ("group" , req .GetGroup ().GetValue ()),
609+ zap .String ("fileName" , req .GetFileName ().GetValue ()), zap .Error (err ))
608610 return api .NewConfigResponse (commonstore .StoreCode2APICode (err ))
609611 }
610612
611613 defer func () {
612614 _ = tx .Rollback ()
613615 }()
616+ saveFile , err := s .storage .LockConfigFile (tx , & model.ConfigFileKey {
617+ Namespace : req .GetNamespace ().GetValue (),
618+ Group : req .GetGroup ().GetValue (),
619+ Name : req .GetFileName ().GetValue (),
620+ })
621+ if err != nil {
622+ log .Error ("[Config][File] lock config file when begin tx." , utils .RequestID (ctx ),
623+ zap .String ("namespace" , req .GetNamespace ().GetValue ()), zap .String ("group" , req .GetGroup ().GetValue ()),
624+ zap .String ("fileName" , req .GetFileName ().GetValue ()), zap .Error (err ))
625+ return api .NewConfigResponse (commonstore .StoreCode2APICode (err ))
626+ }
614627
615628 historyRecords := []func (){}
616- upsertResp := s .handleCreateConfigFile (ctx , tx , upsertFileReq )
617- if upsertResp .GetCode ().GetValue () == uint32 (apimodel .Code_ExistedResource ) {
618- upsertResp = s .handleUpdateConfigFile (ctx , tx , upsertFileReq )
629+
630+ var upsertResp * apiconfig.ConfigResponse
631+ if saveFile == nil {
632+ upsertResp = s .handleCreateConfigFile (ctx , tx , upsertFileReq )
619633 historyRecords = append (historyRecords , func () {
620- s .RecordHistory (ctx , configFileRecordEntry (ctx , upsertFileReq , model .OUpdate ))
634+ s .RecordHistory (ctx , configFileRecordEntry (ctx , upsertFileReq , model .OCreate ))
621635 })
622636 } else {
637+ actualMd5 := CalMd5 (saveFile .Content )
638+ // 只有显示设置了 md5 字段值才会进入 CAS 发布流程
639+ if req .GetMd5 ().GetValue () != "" && req .GetMd5 ().GetValue () != actualMd5 {
640+ log .Error ("[Config][File] cas compare config file." , utils .RequestID (ctx ),
641+ zap .String ("namespace" , req .GetNamespace ().GetValue ()), zap .String ("group" , req .GetGroup ().GetValue ()),
642+ zap .String ("fileName" , req .GetFileName ().GetValue ()),
643+ zap .String ("expect" , req .GetMd5 ().GetValue ()), zap .String ("actual" , actualMd5 ))
644+ return api .NewConfigResponse (apimodel .Code_DataConflict )
645+ }
646+ upsertResp = s .handleUpdateConfigFile (ctx , tx , upsertFileReq )
623647 historyRecords = append (historyRecords , func () {
624- s .RecordHistory (ctx , configFileRecordEntry (ctx , upsertFileReq , model .OCreate ))
648+ s .RecordHistory (ctx , configFileRecordEntry (ctx , upsertFileReq , model .OUpdate ))
625649 })
626650 }
627651 if upsertResp .GetCode ().GetValue () != uint32 (apimodel .Code_ExecuteSuccess ) {
0 commit comments