tokers commented on a change in pull request #2395:
URL: https://github.com/apache/apisix/pull/2395#discussion_r507372495



##########
File path: apisix/cmd/etcd.lua
##########
@@ -0,0 +1,196 @@
+--
+-- 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.
+--
+
+local dkjson = require "dkjson"
+local file = require "apisix.cmd.file"
+local util = require "apisix.cmd.util"
+local env = require "apisix.cmd.env"
+
+local base64_encode = require("base64").encode
+
+local type = type
+local ipairs = ipairs
+local print = print
+local tonumber = tonumber
+local str_format = string.format
+
+local _M = {}
+
+
+local function parse_semantic_version(ver)
+    local errmsg = "invalid semantic version: " .. ver
+
+    local parts = util.split(ver, "-")
+    if #parts > 2 then
+        return nil, errmsg
+    end
+
+    if #parts == 2 then
+        ver = parts[1]
+    end
+
+    local fields = util.split(ver, ".")
+    if #fields ~= 3 then
+        return nil, errmsg
+    end
+
+    local major = tonumber(fields[1])
+    local minor = tonumber(fields[2])
+    local patch = tonumber(fields[3])
+
+    if not (major and minor and patch) then
+        return nil, errmsg
+    end
+
+    return {
+        major = major,
+        minor = minor,
+        patch = patch,
+    }
+end
+
+
+local function compare_semantic_version(v1, v2)
+    local ver1, err = parse_semantic_version(v1)
+    if not ver1 then
+        return nil, err
+    end
+
+    local ver2, err = parse_semantic_version(v2)
+    if not ver2 then
+        return nil, err
+    end
+
+    if ver1.major ~= ver2.major then
+        return ver1.major < ver2.major
+    end
+
+    if ver1.minor ~= ver2.minor then
+        return ver1.minor < ver2.minor
+    end
+
+    return ver1.patch < ver2.patch
+end
+
+
+function _M.init(show_output)
+    -- read_yaml_conf
+    local yaml_conf, err = file.read_yaml_conf()
+    if not yaml_conf then
+        return util.die("failed to read local yaml config of apisix: ",
+                        err)
+    end
+
+    if not yaml_conf.apisix then
+        return util.die("failed to read `apisix` field from yaml file ",
+                        "while initializing etcd")
+    end
+
+    if yaml_conf.apisix.config_center ~= "etcd" then
+        return true
+    end
+
+    if not yaml_conf.etcd then
+        return util.die("failed to read `etcd` field from yaml file ",
+                        "while initializing etcd")
+    end
+
+    --convert old single etcd config to multiple etcd config
+    if type(yaml_conf.etcd.host) == "string" then
+        yaml_conf.etcd.host = { yaml_conf.etcd.host }
+    end
+
+    local etcd_conf = yaml_conf.etcd
+    local timeout = yaml_conf.etcd.timeout or 3
+    local host_count = #(yaml_conf.etcd.host)
+    local uri
+
+    for index, host in ipairs(yaml_conf.etcd.host) do
+        -- check the etcd cluster version
+        uri = etcd_conf.host[1] .. "/version"

Review comment:
       Yeah, to prevent the very corner case like the network partition.




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to