Guest User

demo_insert.expect

a guest
May 6th, 2023
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 5.24 KB | Source Code | 0 0
  1. #!/usr/bin/env expect
  2. #
  3. # Copyright (c) YMatrix Inc.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the “Software”),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. # DEALINGS IN THE SOFTWARE.
  22.  
  23. # 演示 YMatrix 5.0 的 master 故障自动转移功能。
  24. #
  25. # 本脚本需要在已经部署了 YMatrix 5.0 数据库集群的 master 机器上用 mxadmin 用户执行。
  26. # 本脚本使用 psql 客户端用 multiple hosts 连接方式连接到 YMatrix 5.0 数据库集群,
  27. # 并持续向数据库的一个普通表中插入当前的时间和当前连接的 postgresql 数据库实例的 IP 地址。
  28. #
  29. # 当发生 master 故障自动转移时,
  30. # - 插入行为不会中断。这表明数据库支持了故障自动转移功能。
  31. # - 连接的 postgresql 数据库实例的地址会从 master 机器变为standby 机器。
  32. #   这表明客户端也能够支持故障自动转移功能。
  33.  
  34. # 本脚本有两个命令行参数
  35. # - argv[0] - master 主机的 postgresql 连接地址,格式为 host:port
  36. # - argv[1] - standby 主机的 postgresql 连接地址,格式为 host:port
  37.  
  38. # 设置运行参数
  39. set master_host_port [lindex $argv 0]
  40. set master [split $master_host_port ":"]
  41. set master_host [lindex $master 0]
  42. set master_port [lindex $master 1]
  43.  
  44. set standby_host_port [lindex $argv 1]
  45. set standby [split $standby_host_port ":"]
  46. set standby_host [lindex $standby 0]
  47. set standby_port [lindex $standby 1]
  48.  
  49. set user "mxadmin"
  50. set database "postgres"
  51.  
  52. # 输出连接信息
  53. send_user "Using psql to connect to cluster:\n"
  54.  
  55. # 启动 psql 连接到 YMatrix 5.0 集群
  56. spawn psql -d "user=$user host=$master_host,$standby_host port=$master_port,$standby_port dbname=$database"
  57.  
  58. expect "postgres=# "
  59. sleep 2
  60.  
  61. # 创建用于测试的表 demo_auto_failover
  62. # 表的列如下
  63. # - insert_id      - 主键,serial类型
  64. # - ts             - 插入的时间戳
  65. # - connected_host - 当前插入命令连接的 postgresql 数据库实例所在的机器 IP
  66. #                    因为集群 master 和 standby 数据库实例分别部署在不同的机器上,它们的 IP 不同。
  67. send -- "DROP TABLE IF EXISTS demo_auto_failover;\n"
  68.  
  69. expect "postgres=# "
  70. sleep 2
  71.  
  72. send -- "CREATE TABLE demo_auto_failover(
  73.    insert_id SERIAL PRIMARY KEY,
  74.    ts TIMESTAMP WITH TIME ZONE,
  75.    connected_host INET
  76. ) DISTRIBUTED BY (insert_id);\n"
  77.  
  78.  
  79. expect "postgres=# "
  80. sleep 2
  81.  
  82. # 循环运行插入操作
  83. # 该循环插入的操作应该不会被 master 数据库实例被杀死而中断。
  84. while { true } {
  85.     # 插入当前时间和连接的数据库实例的 IP 地址。
  86.     send -- "INSERT INTO demo_auto_failover(ts, connected_host)
  87.        SELECT now() AS ts, inet_server_addr() AS connected_host;\n"
  88.     expect {
  89.         # 插入成功
  90.         "postgres=# " {
  91.             # 显示插入的最新一行的结果
  92.             send -- "SELECT * FROM demo_auto_failover WHERE insert_id = (SELECT max(insert_id) FROM demo_auto_failover);\n"
  93.  
  94.             expect {
  95.                 # 显示成功,进入下一次循环
  96.                 "postgres=# " {
  97.                     sleep 1
  98.                     continue
  99.                 }
  100.  
  101.                 # psql connection reset 发生重新连接数据库集群时的异常处理
  102.                 "Attempting reset: Failed." {
  103.                     send_user "Failed to connect to cluster. Now reconnect.\n"
  104.                     send -- "\\q\n"
  105.  
  106.                     expect "\$"
  107.                     sleep 2
  108.  
  109.                     send_user "Reconnect with psql:"
  110.                     spawn psql -d "user=$user host=$master_host,$standby_host port=$master_port,$standby_port dbname=$database"
  111.                    
  112.                     expect "postgres=# "
  113.                     sleep 1
  114.                     continue
  115.                 }
  116.             }
  117.         }
  118.  
  119.         # psql connection reset 发生重新连接数据库集群时的异常处理
  120.         "Attempting reset: Failed." {
  121.             send_user "Failed to connect to cluster. Now reconnect.\n"
  122.             send -- "\\q\n"
  123.  
  124.             expect "\$"
  125.             sleep 2
  126.  
  127.             send_user "Reconnect with psql:"
  128.             spawn psql -d "user=$user host=$master_host,$standby_host port=$master_port,$standby_port dbname=$database"
  129.            
  130.             expect "postgres=# "
  131.             sleep 1
  132.             continue
  133.         }
  134.     }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment