From 37611564b6a83a73eac6e5242f23bb30619d94a7 Mon Sep 17 00:00:00 2001 From: Yiwen Chen <2058782+handlerww@users.noreply.github.com> Date: Thu, 1 Apr 2021 09:13:24 +0800 Subject: [PATCH] Add retry for DNS lookup failure exception in TiDBInitializer (#3884) --- pkg/manager/member/template.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/manager/member/template.go b/pkg/manager/member/template.go index 56bcad545b..7162ab1e26 100644 --- a/pkg/manager/member/template.go +++ b/pkg/manager/member/template.go @@ -337,15 +337,27 @@ func RenderPumpStartScript(model *PumpStartScriptModel) (string, error) { } // tidbInitStartScriptTpl is the template string of tidb initializer start script -var tidbInitStartScriptTpl = template.Must(template.New("tidb-init-start-script").Parse(`import os, MySQLdb +var tidbInitStartScriptTpl = template.Must(template.New("tidb-init-start-script").Parse(`import os, sys, time, MySQLdb host = '{{ .ClusterName }}-tidb' permit_host = '{{ .PermitHost }}' port = 4000 +retry_count = 0 +for i in range(0, 10): + try: {{- if .TLS }} -conn = MySQLdb.connect(host=host, port=port, user='root', charset='utf8mb4',connect_timeout=5, ssl={'ca': '{{ .CAPath }}', 'cert': '{{ .CertPath }}', 'key': '{{ .KeyPath }}'}) + conn = MySQLdb.connect(host=host, port=port, user='root', charset='utf8mb4',connect_timeout=5, ssl={'ca': '{{ .CAPath }}', 'cert': '{{ .CertPath }}', 'key': '{{ .KeyPath }}'}) {{- else }} -conn = MySQLdb.connect(host=host, port=port, user='root', connect_timeout=5, charset='utf8mb4') + conn = MySQLdb.connect(host=host, port=port, user='root', connect_timeout=5, charset='utf8mb4') {{- end }} + except MySQLdb.OperationalError as e: + print(e) + retry_count += 1 + time.sleep(1) + continue + break +if retry_count == 10: + sys.exit(1) + {{- if .PasswordSet }} password_dir = '/etc/tidb/password' for file in os.listdir(password_dir):