VSCode 远程调试 Vitess

齐凯康
2023-12-01

通过 Vitess 自带的 docker-compose 在本地拉起 Vitess 环境倒是很方便,但是调试起来很痛苦,本地编译的vtgate可以跑起来,连接到 compose 运行的 consul,但因为 vttablet 注册到counsul的地址是容器内的hostname,宿主机运行的vtgate会连接不上。同理,调试vttablet会更痛苦。于是尝试在服务器linuxlocal运行(参照这里,在 MacOS 上用vscode远程调试。

Linux 运行 Vitess

参考官方文档一步步执行,不过还是会踩到坑,和本地运行的kubernetesetcd组件有端口冲突,尝试通过修改etcd启动端口绕过,修改了 env.shscripts/etcd-up.sh,这里是diff:

diff --git a/env.sh b/env.sh
index c0b982a..b0c6b9e 100644
--- a/env.sh
+++ b/env.sh
@@ -62,7 +62,8 @@ elif [ "${TOPO}" = "k8s" ]; then
     # shellcheck disable=SC2034
     TOPOLOGY_FLAGS="-topo_implementation k8s -topo_k8s_kubeconfig ${K8S_KUBECONFIG} -topo_global_server_address ${K8S_ADDR}:${K8S_PORT} -topo_global_root /vitess/global"
 else
-    ETCD_SERVER="localhost:2379"
+    ETCD_SERVER="localhost:22379"
+    ETCD_PEERS="localhost:22380"
     TOPOLOGY_FLAGS="-topo_implementation etcd2 -topo_global_server_address $ETCD_SERVER -topo_global_root /vitess/global"
 
     mkdir -p "${VTDATAROOT}/etcd"
diff --git a/scripts/etcd-up.sh b/scripts/etcd-up.sh
index d8c0a86..1dc15d6 100755
--- a/scripts/etcd-up.sh
+++ b/scripts/etcd-up.sh
@@ -24,7 +24,7 @@ export ETCDCTL_API=2
 # Check that etcd is not already running
 curl "http://${ETCD_SERVER}" > /dev/null 2>&1 && fail "etcd is already running. Exiting."
 
-etcd --enable-v2=true --data-dir "${VTDATAROOT}/etcd/"  --listen-client-urls "http://${ETCD_SERVER}" --advertise-client-urls "http://${ETCD_SERVER}" > "${VTDATAROOT}"/tmp/etcd.out 2>&1 &
+etcd --enable-v2=true --data-dir "${VTDATAROOT}/etcd/" --listen-peer-urls "http://${ETCD_PEERS}"  --listen-client-urls "http://${ETCD_SERVER}" --advertise-client-urls "http://${ETCD_SERVER}" > "${VTDATAROOT}"/tmp/etcd.out 2>&1 &
 PID=$!
 echo $PID > "${VTDATAROOT}/tmp/etcd.pid"
 sleep 5

⚠️ 一定要按照说明,关掉apparmormysqld的保护,否则mysqld跑不起来。

配置 VSCode

参照官方文档,需要给vscode安装好remote-ssh插件,打开ssh远程目录后,需要重新在ssh远程的code-server上安装相关插件,比如goc++,否则远程debug功能无法使用。

配置 launch.json

参考这里配置lauch.jsonargs参数参考vtgate进程(通过ps aux | grep vtgate查看),但需要修改-port, -grpc_port-mysql_server_port-mysql_server_socket_path,以防冲突,完整配置如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Vtgate",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceRoot}/go/cmd/vtgate/",
            "env": {},
            "args": [
                "-topo_implementation", "etcd2",
                "-topo_global_server_address", "localhost:22379",
                "-topo_global_root", "/vitess/global",
                "-log_dir", "/home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp",
                "-log_queries_to_file", "/home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp/vtgate_querylog.txt",
                "-port", "25001",
                "-grpc_port", "25991",
                "-mysql_server_port", "25306",
                "-mysql_server_socket_path", "/tmp/mysql-debug.sock",
                "-cell", "zone1",
                "-cells_to_watch", "zone1",
                "-tablet_types_to_wait", "MASTER,REPLICA",
                "-gateway_implementation", "discoverygateway",
                "-service_map", "grpc-vtgateservice",
                "-pid_file", "/home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp/vtgate-debug.pid",
                "-mysql_auth_server_impl", "none"
            ]
        }
    ]
}

需要注意的是,program 需要指定到main package的目录,否则启动不起来。

在启动debug前,通过运行bcc-tools命令:

sudo execsnoop-bpfcc --max-args 100`

可以看到vscode远程启动debug程序的过程:

node             495459 444797   0 /home/pzhang/.vscode-server/bin/cd9ea6488829f560dc949a8b2fb789f3cdc05f5d/node /home/pzhang/.vscode-server/extensions/golang.
go-0.14.4/out/src/debugAdapter/goDebug.js                                                                                                                      
dlv              495466 495459   0 /home/pzhang/go/bin/dlv debug --headless=true --listen=127.0.0.1:47961 --api-version=2 -- -topo_implementation etcd2 -topo_g
lobal_server_address localhost:22379 -topo_global_root /vitess/global -log_dir /home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp -log_queries_to_fi
le /home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp/vtgate_querylog.txt -port 25001 -grpc_port 25991 -mysql_server_port 25306 -mysql_server_socket
_path /tmp/mysql-debug.sock -cell zone1 -cells_to_watch zone1 -tablet_types_to_wait MASTER,REPLICA -gateway_implementation discoverygateway -service_map grpc-v
tgateservice -pid_file /home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp/vtgate-debug.pid -mysql_auth_server_impl none                             
go               495477 495466   0 /usr/bin/go version                                                                                                         
go               495483 495466   0 /usr/bin/go build -o /home/pzhang/work/opensource/vitess/go/cmd/vtgate/__debug_bin -gcflags all=-N -l
compile          495504 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/compile -V=full
compile          495510 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/compile -V=full
compile          495557 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/compile -V=full
asm              495562 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/asm -V=full
compile          495566 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/compile -V=full
compile          495576 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/compile -V=full
cgo              495577 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/cgo -V=full
asm              495582 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/asm -V=full
link             495597 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/link -V=full
link             495604 495483   0 /usr/lib/go-1.13/pkg/tool/linux_amd64/link -o /tmp/go-build131155124/b001/exe/a.out -importcfg /tmp/go-build131155124/b001/i
mportcfg.link -buildmode=exe -buildid=Den5sWB14RZ0Z3is5M2-/ttVeImyRd02vnonS_hw-/YxxOjeBtIsEu80iha3C2/Den5sWB14RZ0Z3is5M2- -extld=gcc /home/pzhang/.cache/go-bui
ld/14/14c38e588bdb282f3d91f0b5167f6145f56850c69429f6d1bf67db379b0c6907-d
__debug_bin      495621 495466   0 /home/pzhang/work/opensource/vitess/go/cmd/vtgate/__debug_bin -topo_implementation etcd2 -topo_global_server_address localho
st:22379 -topo_global_root /vitess/global -log_dir /home/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp -log_queries_to_file /home/pzhang/work/opensou
rce/my-vitess-example/vtdataroot/tmp/vtgate_querylog.txt -port 25001 -grpc_port 25991 -mysql_server_port 25306 -mysql_server_socket_path /tmp/mysql-debug.sock 
-cell zone1 -cells_to_watch zone1 -tablet_types_to_wait MASTER,REPLICA -gateway_implementation discoverygateway -service_map grpc-vtgateservice -pid_file /home
/pzhang/work/opensource/my-vitess-example/vtdataroot/tmp/vtgate-debug.pid -mysql_auth_server_impl none

Enjoy hacking~~

 类似资料: