nic-chen commented on a change in pull request #2065: URL: https://github.com/apache/apisix-dashboard/pull/2065#discussion_r687628692
########## File path: api/cmd/cache_verify.go ########## @@ -0,0 +1,49 @@ +/* + * 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 cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "io/ioutil" + "net/http" +) + +func newCacheVerifyCommand() *cobra.Command { + return &cobra.Command{ + Use: "cache-verify", + Short: "verify that data in cache are consistent with that in ETCD", + Run: func(cmd *cobra.Command, args []string) { + rsp, err := http.Get("http://localhost:9000/apisix/admin/cache_verify") Review comment: the port is not fixed ########## File path: api/internal/filter/authentication.go ########## @@ -45,7 +45,7 @@ func (mw *AuthenticationMiddleware) Handle(ctx droplet.Context) error { req := httpReq.(*http.Request) - if req.URL.Path == "/apisix/admin/tool/version" || req.URL.Path == "/apisix/admin/user/login" { + if req.URL.Path == "/apisix/admin/tool/version" || req.URL.Path == "/apisix/admin/user/login" || req.URL.Path == "/apisix/admin/cache_verify" { Review comment: need auth for the new API ########## File path: web/cypress/integration/route/search-route.spec.js ########## @@ -17,7 +17,7 @@ /* eslint-disable */ context('Create and Search Route', () => { - const timeout = 500; + const timeout = 5000; Review comment: why change this? ########## File path: api/internal/handler/cache_verify/cache_verify.go ########## @@ -0,0 +1,155 @@ +/* + * 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 cache_verify + +import ( + "context" + "encoding/json" + "fmt" + "github.com/apisix/manager-api/internal/core/storage" + "github.com/apisix/manager-api/internal/core/store" + "github.com/apisix/manager-api/internal/handler" + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet" + wgin "github.com/shiningrush/droplet/wrapper/gin" + "reflect" +) + +// we read from cache and etcd,then compare them + +type Handler struct { +} + +type compareResult struct { + K string `json:"key"` Review comment: K —> Key ########## File path: api/internal/handler/cache_verify/cache_verify.go ########## @@ -0,0 +1,155 @@ +/* + * 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 cache_verify + +import ( + "context" + "encoding/json" + "fmt" + "github.com/apisix/manager-api/internal/core/storage" + "github.com/apisix/manager-api/internal/core/store" + "github.com/apisix/manager-api/internal/handler" + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet" + wgin "github.com/shiningrush/droplet/wrapper/gin" + "reflect" +) + +// we read from cache and etcd,then compare them + +type Handler struct { +} + +type compareResult struct { + K string `json:"key"` + CacheValue string `json:"cache_value"` + EtcdValue string `json:"etcd_value"` +} + +type resultOuput struct { + Consumers []compareResult `json:"inconsistent_consumers"` + Routes []compareResult `json:"inconsistent_routes"` + Services []compareResult `json:"inconsistent_services"` + SSLs []compareResult `json:"inconsistent_ssls"` + Upstreams []compareResult `json:"inconsistent_upstreams"` + Scripts []compareResult `json:"inconsistent_scripts"` + GlobalPlugins []compareResult `json:"inconsistent_global_plugins"` + PluginConfigs []compareResult `json:"inconsistent_plugin_configs"` + ServerInfos []compareResult `json:"inconsistent_server_infos"` +} + +var infixMap = map[store.HubKey]string{ + store.HubKeyConsumer: "consumers", + store.HubKeyRoute: "routes", + store.HubKeyService: "services", + store.HubKeySsl: "ssl", + store.HubKeyUpstream: "upstreams", + store.HubKeyScript: "scripts", + store.HubKeyGlobalRule: "global_rules", + store.HubKeyServerInfo: "data_plane/server_info", + store.HubKeyPluginConfig: "plugin_configs", +} + +func NewHandler() (handler.RouteRegister, error) { + return &Handler{}, nil +} + +func (h *Handler) ApplyRoute(r *gin.Engine) { + r.GET("/apisix/admin/cache_verify", wgin.Wraps(h.CacheVerify)) +} + +func (h *Handler) CacheVerify(_ droplet.Context) (interface{}, error) { + + var rs resultOuput + store.RangeStore(func(key store.HubKey, s *store.GenericStore) bool { + s.Range(context.TODO(), func(k string, obj interface{}) bool { + cmp, cacheValue, etcdValue := compare(k, infixMap[key], obj) + if !cmp { + if key == store.HubKeyConsumer { + rs.Consumers = append(rs.Consumers, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyRoute { + rs.Routes = append(rs.Routes, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyScript { + rs.Scripts = append(rs.Scripts, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyService { + rs.Services = append(rs.Services, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyGlobalRule { + rs.GlobalPlugins = append(rs.GlobalPlugins, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyPluginConfig { + rs.PluginConfigs = append(rs.PluginConfigs, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyUpstream { + rs.Upstreams = append(rs.Upstreams, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeySsl { + rs.SSLs = append(rs.SSLs, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + if key == store.HubKeyServerInfo { + rs.ServerInfos = append(rs.ServerInfos, compareResult{EtcdValue: etcdValue, CacheValue: cacheValue, K: k}) + } + + } + return true + }) + return true + }) + return rs, nil +} + +func compare(k, infix string, v interface{}) (bool, string, string) { + s, err := json.Marshal(v) + if err != nil { + fmt.Printf("json marsharl failed : %v\n", err) + return false, "", "" + } + cacheValue := string(s) + key := fmt.Sprintf("/apisix/%s/%s", infix, k) + val, err := storage.GenEtcdStorage().Get(context.TODO(), key) Review comment: We could get it in batches by prefix, not every key to get once. -- 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]
