mochengqian commented on code in PR #1477:
URL: https://github.com/apache/dubbo-admin/pull/1477#discussion_r3294808853


##########
pkg/core/versioning/component.go:
##########
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package versioning
+
+import (
+       "errors"
+       "fmt"
+       "math"
+
+       versioningcfg "github.com/apache/dubbo-admin/pkg/config/versioning"
+       "github.com/apache/dubbo-admin/pkg/core/events"
+       "github.com/apache/dubbo-admin/pkg/core/governor"
+       "github.com/apache/dubbo-admin/pkg/core/logger"
+       "github.com/apache/dubbo-admin/pkg/core/manager"
+       "github.com/apache/dubbo-admin/pkg/core/runtime"
+       "gorm.io/gorm"
+)
+
+const ComponentType runtime.ComponentType = "rule versioning"
+
+func init() {
+       runtime.RegisterComponent(&component{})
+}
+
+type Component interface {
+       runtime.Component
+       Service() Service
+}
+
+type component struct {
+       service     Service
+       store       Store
+       subscribers []*Subscriber
+}
+
+func (c *component) Type() runtime.ComponentType {
+       return ComponentType
+}
+
+func (c *component) Order() int {
+       return math.MaxInt - 5
+}
+
+func (c *component) RequiredDependencies() []runtime.ComponentType {
+       return []runtime.ComponentType{
+               runtime.EventBus,
+               runtime.ResourceStore,
+               runtime.ResourceManager,
+       }
+}
+
+func (c *component) Init(ctx runtime.BuilderContext) error {
+       cfg := ctx.Config().Versioning
+       if cfg == nil {
+               cfg = versioningcfg.Default()
+       }
+       storeComponent, err := ctx.GetActivatedComponent(runtime.ResourceStore)
+       if err != nil {
+               return err
+       }
+       store := Store(NewMemoryStore())
+       if sc, ok := storeComponent.(interface {
+               GetDB() (*gorm.DB, bool)
+       }); ok {
+               if db, exists := sc.GetDB(); exists {
+                       gormStore := NewGormStore(db)
+                       if err := gormStore.AutoMigrate(); err != nil {
+                               return err

Review Comment:
   Fixed in 74dfd5c. The versioning component now initializes a disabled 
memory-backed service and returns before looking up the DB-backed store, so 
`versioning.enabled=false` no longer runs `AutoMigrate()` or creates rule 
versioning tables. Added `TestComponentAutoMigrateOnlyWhenEnabled` to cover 
both disabled and enabled startup.



##########
ui-vue3/src/views/traffic/tagRule/tabs/updateByYAMLView.vue:
##########
@@ -138,14 +147,25 @@ const updateTagRule = async () => {
   loading.value = true
   try {
     const data = yaml.load(YAMLValue.value)
-    const res = await updateTagRuleAPI(<string>route.params?.ruleName, data)
+    const res = await updateTagRuleAPI(route.params?.ruleName as string, data, 
{
+      expectedVersionId: currentVersionId.value
+    })
     if (res.code === HTTP_STATUS.SUCCESS) {
       message.success('update success')
       // 延迟 2 秒后再获取数据,确保数据库已更新
       await new Promise((resolve) => setTimeout(resolve, 2000))
       TAB_STATE.tagRule = null
       await getTagRuleDetail()
+      await reloadCurrentVersion()
     }
+  } catch (e: any) {
+    notifyRuleVersionError(e, {
+      reload: async () => {
+        TAB_STATE.tagRule = null
+        await getTagRuleDetail()
+        await reloadCurrentVersion()
+      }
+    })
   } finally {

Review Comment:
   Fixed in 74dfd5c. The tag-rule YAML save path now checks whether 
`notifyRuleVersionError` handled the error and falls back to `message.error` 
plus `console.error` for non-versioning failures. It also validates that 
`yaml.load` returns an object before sending the update.



##########
ui-vue3/src/views/traffic/routingRule/tabs/updateByYAMLView.vue:
##########
@@ -134,14 +143,25 @@ const updateRoutingRule = async () => {
   try {
     const data = yaml.load(YAMLValue.value)
     data.configVersion = 'v3.0'
-    const res = await updateConditionRuleAPI(<string>route.params?.ruleName, 
data)
+    const res = await updateConditionRuleAPI(route.params?.ruleName as string, 
data, {
+      expectedVersionId: currentVersionId.value
+    })
     if (res.code === HTTP_STATUS.SUCCESS) {
       message.success('update success')
       // 延迟 2 秒后再获取数据,确保数据库已更新
       await new Promise((resolve) => setTimeout(resolve, 2000))
       TAB_STATE.conditionRule = null
       await getRoutingRuleDetail()
+      await reloadCurrentVersion()
     }
+  } catch (e: any) {
+    notifyRuleVersionError(e, {
+      reload: async () => {
+        TAB_STATE.conditionRule = null
+        await getRoutingRuleDetail()
+        await reloadCurrentVersion()
+      }
+    })
   } finally {

Review Comment:
   Fixed in 74dfd5c. The condition-rule YAML save path now validates that 
`yaml.load` returned an object before mutating `configVersion`, and 
non-versioning failures fall back to `message.error` plus `console.error` when 
`notifyRuleVersionError` returns false.



##########
pkg/console/handler/rule_version.go:
##########
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package handler
+
+import (
+       "errors"
+       "net/http"
+       "strconv"
+       "strings"
+
+       "github.com/gin-contrib/sessions"
+       "github.com/gin-gonic/gin"
+
+       "github.com/apache/dubbo-admin/pkg/common/bizerror"
+       consolectx "github.com/apache/dubbo-admin/pkg/console/context"
+       "github.com/apache/dubbo-admin/pkg/console/model"
+       "github.com/apache/dubbo-admin/pkg/console/service"
+       coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
+       "github.com/apache/dubbo-admin/pkg/core/versioning"
+)
+
+type rollbackReq struct {
+       Reason            string `json:"reason"`
+       ExpectedVersionID *int64 `json:"expectedVersionId"`
+}
+
+type abandonIntentReq struct {
+       Reason string `json:"reason"`
+}
+
+const maxRuleVersionReasonLength = 1024
+
+func ListRuleVersions(cs consolectx.Context, kind coremodel.ResourceKind) 
gin.HandlerFunc {
+       return func(c *gin.Context) {
+               if !ensureVersioningEnabled(c, cs) {
+                       return
+               }
+               resp, err := service.ListRuleVersions(cs, 
service.RuleKindName{Kind: kind, Mesh: c.Query("mesh"), Name: 
c.Param("ruleName")})
+               writeVersioningResp(c, resp, err)
+       }
+}
+
+func GetRuleVersion(cs consolectx.Context, kind coremodel.ResourceKind) 
gin.HandlerFunc {
+       return func(c *gin.Context) {
+               if !ensureVersioningEnabled(c, cs) {
+                       return
+               }
+               id, ok := parseVersionID(c)
+               if !ok {
+                       return
+               }
+               resp, err := service.GetRuleVersion(cs, 
service.RuleKindName{Kind: kind, Mesh: c.Query("mesh"), Name: 
c.Param("ruleName")}, id)
+               writeVersioningResp(c, resp, err)
+       }
+}
+
+func DiffRuleVersion(cs consolectx.Context, kind coremodel.ResourceKind) 
gin.HandlerFunc {
+       return func(c *gin.Context) {
+               if !ensureVersioningEnabled(c, cs) {
+                       return
+               }
+               id, ok := parseVersionID(c)
+               if !ok {
+                       return
+               }
+               resp, err := service.DiffRuleVersion(cs, 
service.RuleKindName{Kind: kind, Mesh: c.Query("mesh"), Name: 
c.Param("ruleName")}, id, c.Query("against"))
+               writeVersioningResp(c, resp, err)
+       }
+}
+
+func RollbackRuleVersion(cs consolectx.Context, kind coremodel.ResourceKind) 
gin.HandlerFunc {
+       return func(c *gin.Context) {
+               if !ensureVersioningEnabled(c, cs) {
+                       return
+               }
+               id, ok := parseVersionID(c)
+               if !ok {
+                       return
+               }
+               req := rollbackReq{}
+               if err := c.ShouldBindJSON(&req); err != nil {
+                       c.JSON(http.StatusOK, 
model.NewBizErrorResp(bizerror.New(bizerror.InvalidArgument, err.Error())))

Review Comment:
   Fixed in 74dfd5c. Malformed rollback JSON now delegates through 
`writeVersioningInvalidArgument` / `writeVersioningResp`, so it returns HTTP 
400 consistently with the other `InvalidArgument` paths. Added handler 
regression coverage for malformed JSON.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to