nic-chen commented on a change in pull request #2065:
URL: https://github.com/apache/apisix-dashboard/pull/2065#discussion_r706299837



##########
File path: api/cmd/cache_verify.go
##########
@@ -0,0 +1,178 @@
+/*
+ * 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 (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "os"
+
+       "github.com/spf13/cobra"
+       "github.com/tidwall/gjson"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/handler/cache_verify"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+var (
+       port                     int
+       host, username, password string
+)
+
+type response struct {
+       Data cache_verify.OutputResult `json:"data"`
+}
+
+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) {
+                       conf.InitConf()
+                       log.InitLogger()
+
+                       port = conf.ServerPort
+                       host = conf.ServerHost
+                       username = "admin"
+                       password = conf.UserList[username].Password
+

Review comment:
       Unnecessary blank lines.

##########
File path: api/cmd/cache_verify.go
##########
@@ -0,0 +1,178 @@
+/*
+ * 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 (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "os"
+
+       "github.com/spf13/cobra"
+       "github.com/tidwall/gjson"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/handler/cache_verify"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+var (
+       port                     int
+       host, username, password string
+)
+
+type response struct {
+       Data cache_verify.OutputResult `json:"data"`
+}
+
+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) {
+                       conf.InitConf()
+                       log.InitLogger()
+
+                       port = conf.ServerPort
+                       host = conf.ServerHost

Review comment:
       why use `conf.ServerHost`? it can be `0.0.0.0` or something else. I 
think we could use `127.0.0.1` here.

##########
File path: api/cmd/cache_verify.go
##########
@@ -0,0 +1,178 @@
+/*
+ * 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 (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "os"
+
+       "github.com/spf13/cobra"
+       "github.com/tidwall/gjson"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/handler/cache_verify"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+var (
+       port                     int
+       host, username, password string
+)
+
+type response struct {
+       Data cache_verify.OutputResult `json:"data"`
+}
+
+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) {
+                       conf.InitConf()
+                       log.InitLogger()
+
+                       port = conf.ServerPort
+                       host = conf.ServerHost
+                       username = "admin"
+                       password = conf.UserList[username].Password
+
+                       token := getToken()
+

Review comment:
       ditto

##########
File path: api/cmd/cache_verify.go
##########
@@ -0,0 +1,178 @@
+/*
+ * 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 (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "os"
+
+       "github.com/spf13/cobra"
+       "github.com/tidwall/gjson"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/handler/cache_verify"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+var (
+       port                     int
+       host, username, password string
+)
+
+type response struct {
+       Data cache_verify.OutputResult `json:"data"`
+}
+
+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) {
+                       conf.InitConf()
+                       log.InitLogger()
+
+                       port = conf.ServerPort
+                       host = conf.ServerHost
+                       username = "admin"
+                       password = conf.UserList[username].Password
+
+                       token := getToken()
+
+                       url := 
fmt.Sprintf("http://%s:%d/apisix/admin/cache_verify";, host, port)
+                       client := &http.Client{}
+
+                       get, err := http.NewRequest("GET", url, nil)
+                       if err != nil {
+                               fmt.Fprintf(os.Stderr, "new http request 
failed: %s\n", err)
+                               return
+                       }
+
+                       get.Header.Set("Authorization", token)
+
+                       rsp, err := client.Do(get)
+                       if err != nil {
+                               fmt.Fprintf(os.Stderr, "get result from 
migrate/export failed: %s\n", err)
+                               return
+                       }
+                       defer func() {
+                               err := rsp.Body.Close()
+                               if err != nil {
+                                       fmt.Fprintf(os.Stderr, "close on 
response body failed: %s\n", err)
+                                       return
+                               }
+                       }()
+
+                       data, err := ioutil.ReadAll(rsp.Body)
+                       if err != nil {
+                               fmt.Fprintf(os.Stderr, "io read all failed: 
%s\n", err)
+                               return
+                       }
+
+                       var rs response
+                       err = json.Unmarshal(data, &rs)
+                       if err != nil {
+                               fmt.Fprintf(os.Stderr, "bad Data format,json 
unmarshal failed: %s\n", err)
+                               return
+                       }
+
+                       fmt.Printf("cache verification result as follows:\n\n")
+                       fmt.Printf("There are %d items in total,%d of them are 
consistent,%d of them are inconsistent\n",
+                               rs.Data.Total, rs.Data.ConsistentCount, 
rs.Data.InconsistentCount)
+
+                       names := []string{
+                               "ssls",
+                               "routes",
+                               "scripts",
+                               "services",
+                               "upstreams",
+                               "consumers",
+                               "server infos",
+                               "global plugins",
+                               "plugin configs",
+                       }
+                       datas := []cache_verify.StatisticalData{
+                               rs.Data.Items.SSLs,
+                               rs.Data.Items.Routes,
+                               rs.Data.Items.Scripts,
+                               rs.Data.Items.Services,
+                               rs.Data.Items.Upstreams,
+                               rs.Data.Items.Consumers,
+                               rs.Data.Items.ServerInfos,
+                               rs.Data.Items.GlobalPlugins,
+                               rs.Data.Items.PluginConfigs,
+                       }
+                       for i, v := range names {
+                               printResult(v, datas[i])
+                       }
+               },
+       }
+}
+
+func getToken() string {
+       account := map[string]string{
+               "username": username,
+               "password": password,
+       }
+
+       data, err := json.Marshal(account)
+       if err != nil {
+               fmt.Fprintf(os.Stderr, "json marshal failed: %s\n", err)
+               return ""

Review comment:
       should be better return error, not just ""

##########
File path: api/internal/handler/cache_verify/cache_verify.go
##########
@@ -0,0 +1,268 @@
+/*
+ * 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"
+       "path"
+       "reflect"
+)
+
+type Handler struct {
+       consumerStore     store.Interface
+       routeStore        store.Interface
+       serviceStore      store.Interface
+       sslStore          store.Interface
+       upstreamStore     store.Interface
+       scriptStore       store.Interface
+       globalPluginStore store.Interface
+       pluginConfigStore store.Interface
+       serverInfoStore   store.Interface
+       etcdStorage       storage.Interface
+}
+
+type inconsistentPair struct {
+       Key        string `json:"key"`
+       CacheValue string `json:"cache_value"`
+       EtcdValue  string `json:"etcd_value"`
+}
+
+type StatisticalData struct {
+       Total             int                `json:"total"`
+       ConsistentCount   int                `json:"consistent_count"`
+       InconsistentCount int                `json:"inconsistent_count"`
+       InconsistentPairs []inconsistentPair `json:"inconsistent_pairs"`
+}
+
+type items struct {
+       Consumers     StatisticalData `json:"consumers"`
+       Routes        StatisticalData `json:"routes"`
+       Services      StatisticalData `json:"services"`
+       SSLs          StatisticalData `json:"ssls"`
+       Upstreams     StatisticalData `json:"upstreams"`
+       Scripts       StatisticalData `json:"scripts"`
+       GlobalPlugins StatisticalData `json:"global_plugins"`
+       PluginConfigs StatisticalData `json:"plugin_configs"`
+       ServerInfos   StatisticalData `json:"server_infos"`
+}
+
+type OutputResult struct {
+       Total             int   `json:"total"`
+       ConsistentCount   int   `json:"consistent_count"`
+       InconsistentCount int   `json:"inconsistent_count"`
+       Items             items `json:"items"`
+}
+
+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{
+               consumerStore:     store.GetStore(store.HubKeyConsumer),
+               routeStore:        store.GetStore(store.HubKeyRoute),
+               serviceStore:      store.GetStore(store.HubKeyService),
+               sslStore:          store.GetStore(store.HubKeySsl),
+               upstreamStore:     store.GetStore(store.HubKeyUpstream),
+               scriptStore:       store.GetStore(store.HubKeyScript),
+               globalPluginStore: store.GetStore(store.HubKeyGlobalRule),
+               pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
+               serverInfoStore:   store.GetStore(store.HubKeyServerInfo),
+               etcdStorage:       storage.GenEtcdStorage(),
+       }, 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) {
+       checkConsistent := func(hubKey store.HubKey, s *store.Interface, rs 
*OutputResult, etcd *storage.Interface) {
+
+               keyPairs, err := (*etcd).List(context.TODO(), 
fmt.Sprintf("/apisix/%s/", infixMap[hubKey]))

Review comment:
       Please see here
   

##########
File path: api/internal/handler/cache_verify/cache_verify.go
##########
@@ -0,0 +1,268 @@
+/*
+ * 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"
+       "path"
+       "reflect"
+)
+
+type Handler struct {
+       consumerStore     store.Interface
+       routeStore        store.Interface
+       serviceStore      store.Interface
+       sslStore          store.Interface
+       upstreamStore     store.Interface
+       scriptStore       store.Interface
+       globalPluginStore store.Interface
+       pluginConfigStore store.Interface
+       serverInfoStore   store.Interface
+       etcdStorage       storage.Interface
+}
+
+type inconsistentPair struct {
+       Key        string `json:"key"`
+       CacheValue string `json:"cache_value"`
+       EtcdValue  string `json:"etcd_value"`
+}
+
+type StatisticalData struct {
+       Total             int                `json:"total"`
+       ConsistentCount   int                `json:"consistent_count"`
+       InconsistentCount int                `json:"inconsistent_count"`
+       InconsistentPairs []inconsistentPair `json:"inconsistent_pairs"`
+}
+
+type items struct {
+       Consumers     StatisticalData `json:"consumers"`
+       Routes        StatisticalData `json:"routes"`
+       Services      StatisticalData `json:"services"`
+       SSLs          StatisticalData `json:"ssls"`
+       Upstreams     StatisticalData `json:"upstreams"`
+       Scripts       StatisticalData `json:"scripts"`
+       GlobalPlugins StatisticalData `json:"global_plugins"`
+       PluginConfigs StatisticalData `json:"plugin_configs"`
+       ServerInfos   StatisticalData `json:"server_infos"`
+}
+
+type OutputResult struct {
+       Total             int   `json:"total"`
+       ConsistentCount   int   `json:"consistent_count"`
+       InconsistentCount int   `json:"inconsistent_count"`
+       Items             items `json:"items"`
+}
+
+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{
+               consumerStore:     store.GetStore(store.HubKeyConsumer),
+               routeStore:        store.GetStore(store.HubKeyRoute),
+               serviceStore:      store.GetStore(store.HubKeyService),
+               sslStore:          store.GetStore(store.HubKeySsl),
+               upstreamStore:     store.GetStore(store.HubKeyUpstream),
+               scriptStore:       store.GetStore(store.HubKeyScript),
+               globalPluginStore: store.GetStore(store.HubKeyGlobalRule),
+               pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
+               serverInfoStore:   store.GetStore(store.HubKeyServerInfo),
+               etcdStorage:       storage.GenEtcdStorage(),
+       }, 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) {
+       checkConsistent := func(hubKey store.HubKey, s *store.Interface, rs 
*OutputResult, etcd *storage.Interface) {
+
+               keyPairs, err := (*etcd).List(context.TODO(), 
fmt.Sprintf("/apisix/%s/", infixMap[hubKey]))

Review comment:
       prefix `/apisix/` can be changed in conf.yaml, please use 
`conf.ETCDConfig.Prefix`

##########
File path: api/cmd/cache_verify.go
##########
@@ -0,0 +1,177 @@
+/*
+ * 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 (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+
+       "github.com/spf13/cobra"
+       "github.com/tidwall/gjson"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/handler/cache_verify"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+var port int
+var host string
+
+var username, password string
+
+type response struct {
+       Data cache_verify.OutputResult `json:"data"`
+}
+
+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) {
+                       conf.InitConf()
+                       log.InitLogger()
+
+                       port = conf.ServerPort
+                       host = conf.ServerHost
+                       username = "admin"

Review comment:
       use the first one. username can be any character.

##########
File path: api/cmd/cache_verify.go
##########
@@ -0,0 +1,178 @@
+/*
+ * 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 (
+       "bytes"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "os"
+
+       "github.com/spf13/cobra"
+       "github.com/tidwall/gjson"
+
+       "github.com/apisix/manager-api/internal/conf"
+       "github.com/apisix/manager-api/internal/handler/cache_verify"
+       "github.com/apisix/manager-api/internal/log"
+)
+
+var (
+       port                     int
+       host, username, password string
+)
+
+type response struct {
+       Data cache_verify.OutputResult `json:"data"`
+}
+
+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) {
+                       conf.InitConf()
+                       log.InitLogger()
+
+                       port = conf.ServerPort
+                       host = conf.ServerHost
+                       username = "admin"
+                       password = conf.UserList[username].Password
+
+                       token := getToken()
+
+                       url := 
fmt.Sprintf("http://%s:%d/apisix/admin/cache_verify";, host, port)
+                       client := &http.Client{}
+
+                       get, err := http.NewRequest("GET", url, nil)

Review comment:
       `get` -> `req` or other meaningful variable name.

##########
File path: api/internal/handler/cache_verify/cache_verify.go
##########
@@ -0,0 +1,219 @@
+/*
+ * 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"
+       "path"
+       "reflect"
+
+       "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/apisix/manager-api/internal/log"
+       "github.com/gin-gonic/gin"
+       "github.com/shiningrush/droplet"
+       wgin "github.com/shiningrush/droplet/wrapper/gin"
+)
+
+type Handler struct {
+       consumerStore     store.Interface
+       routeStore        store.Interface
+       serviceStore      store.Interface
+       sslStore          store.Interface
+       upstreamStore     store.Interface
+       scriptStore       store.Interface
+       globalPluginStore store.Interface
+       pluginConfigStore store.Interface
+       serverInfoStore   store.Interface
+       etcdStorage       storage.Interface
+}
+
+type inconsistentPair struct {
+       Key        string `json:"key"`
+       CacheValue string `json:"cache_value"`
+       EtcdValue  string `json:"etcd_value"`
+}
+
+type StatisticalData struct {
+       Total             int                `json:"total"`
+       ConsistentCount   int                `json:"consistent_count"`
+       InconsistentCount int                `json:"inconsistent_count"`
+       InconsistentPairs []inconsistentPair `json:"inconsistent_pairs"`
+}
+
+type items struct {
+       Consumers     StatisticalData `json:"consumers"`
+       Routes        StatisticalData `json:"routes"`
+       Services      StatisticalData `json:"services"`
+       SSLs          StatisticalData `json:"ssls"`
+       Upstreams     StatisticalData `json:"upstreams"`
+       Scripts       StatisticalData `json:"scripts"`
+       GlobalPlugins StatisticalData `json:"global_plugins"`
+       PluginConfigs StatisticalData `json:"plugin_configs"`
+       ServerInfos   StatisticalData `json:"server_infos"`
+}
+
+type OutputResult struct {
+       Total             int   `json:"total"`
+       ConsistentCount   int   `json:"consistent_count"`
+       InconsistentCount int   `json:"inconsistent_count"`
+       Items             items `json:"items"`
+}
+
+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{
+               consumerStore:     store.GetStore(store.HubKeyConsumer),
+               routeStore:        store.GetStore(store.HubKeyRoute),
+               serviceStore:      store.GetStore(store.HubKeyService),
+               sslStore:          store.GetStore(store.HubKeySsl),
+               upstreamStore:     store.GetStore(store.HubKeyUpstream),
+               scriptStore:       store.GetStore(store.HubKeyScript),
+               globalPluginStore: store.GetStore(store.HubKeyGlobalRule),
+               pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
+               serverInfoStore:   store.GetStore(store.HubKeyServerInfo),
+               etcdStorage:       storage.GenEtcdStorage(),
+       }, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+       r.GET("/apisix/admin/cache_verify", wgin.Wraps(h.CacheVerify))
+}
+
+var hubToData map[store.HubKey]*StatisticalData
+
+func (h *Handler) CacheVerify(_ droplet.Context) (interface{}, error) {
+
+       checkConsistent := func(hubKey store.HubKey, s *store.Interface, rs 
*OutputResult, etcd *storage.Interface) {
+
+               keyPairs, err := (*etcd).List(context.TODO(), 
fmt.Sprintf("/apisix/%s/", infixMap[hubKey]))
+               if err != nil {
+                       log.Errorf("etcd list failed: %s", err)
+                       return
+               }
+
+               rs.Total += len(keyPairs)
+
+               for i := range keyPairs {
+                       key := path.Base(keyPairs[i].Key)
+
+                       cacheObj, err := (*s).Get(context.TODO(), key)

Review comment:
       what is `s`? please use a meaningful variable name




-- 
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]


Reply via email to