From 732586ac00974606ebe32181721a8a0f99339b29 Mon Sep 17 00:00:00 2001 From: Nick Vyzas Date: Mon, 30 Sep 2019 19:00:02 +0000 Subject: [PATCH 1/2] Packaging updates: Debian 10 & SystemD for RHEL 7+ --- Makefile | 23 ++++- docker-compose.yml | 59 ++++++++++-- .../ctl/proxysql.ctl | 0 .../rhel7/entrypoint/entrypoint.bash | 48 ++++++++++ .../rhel-compliant/rhel7/rpmmacros/.rpmmacros | 2 + .../rpmmacros/rpmbuild/SPECS/proxysql.spec | 90 +++++++++++++++++++ .../rpmmacros/rpmbuild/SPECS/proxysql.spec | 1 - systemd/system/proxysql.service | 4 - 8 files changed, 212 insertions(+), 15 deletions(-) rename docker/images/proxysql/deb-compliant/{bionic-package => latest-package}/ctl/proxysql.ctl (100%) create mode 100755 docker/images/proxysql/rhel-compliant/rhel7/entrypoint/entrypoint.bash create mode 100644 docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/.rpmmacros create mode 100644 docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/rpmbuild/SPECS/proxysql.spec diff --git a/Makefile b/Makefile index 17f784660..d6287e9b3 100644 --- a/Makefile +++ b/Makefile @@ -147,7 +147,7 @@ clean: cd lib && ${MAKE} clean cd src && ${MAKE} clean -packages: centos6.7 centos7 centos6.7-dbg centos7-dbg ubuntu14 debian7 debian8 ubuntu14-dbg debian7-dbg debian8-dbg ubuntu16 ubuntu16-dbg fedora24 fedora24-dbg debian9 debian9-dbg ubuntu16-clickhouse debian9-clickhouse centos7-clickhouse fedora24-clickhouse fedora27 fedora27-dbg fedora27-clickhouse ubuntu18 ubuntu18-dbg ubuntu18-clickhouse fedora28 fedora28-dbg fedora28-clickhouse +packages: centos6.7 centos6.7-dbg centos7 centos7-dbg centos7-clickhouse ubuntu14 ubuntu14-dbg ubuntu16 ubuntu16-dbg ubuntu16-clickhouse ubuntu18 ubuntu18-dbg ubuntu18-clickhouse debian7 debian7-dbg debian8 debian8-dbg debian9 debian9-dbg debian9-clickhouse debian10 debian10-dbg debian10-clickhouse fedora24 fedora24-dbg fedora24-clickhouse fedora27 fedora27-dbg fedora27-clickhouse fedora28 fedora28-dbg fedora28-clickhouse .PHONY: packages centos5: binaries/proxysql-${CURVER}-1-centos5.x86_64.rpm @@ -204,6 +204,9 @@ debian8: binaries/proxysql_${CURVER}-debian8_amd64.deb debian9: binaries/proxysql_${CURVER}-debian9_amd64.deb .PHONY: debian9 +debian10: binaries/proxysql_${CURVER}-debian10_amd64.deb +.PHONY: debian10 + ubuntu14-dbg: binaries/proxysql_${CURVER}-dbg-ubuntu14_amd64.deb .PHONY: ubuntu14-dbg @@ -240,6 +243,12 @@ debian9.4-dbg: binaries/proxysql_${CURVER}-dbg-debian9.4_amd64.deb debian9.4-clickhouse: binaries/proxysql_${CURVER}-clickhouse-debian9.4_amd64.deb .PHONY: debian9.4-clickhouse +debian10-dbg: binaries/proxysql_${CURVER}-dbg-debian10_amd64.deb +.PHONY: debian10-dbg + +debian10-clickhouse: binaries/proxysql_${CURVER}-clickhouse-debian10_amd64.deb +.PHONY: debian10-clickhouse + centos7-clickhouse: binaries/proxysql-${CURVER}-clickhouse-1-centos7.x86_64.rpm .PHONY: centos7-clickhouse @@ -348,6 +357,10 @@ binaries/proxysql_${CURVER}-debian9.4_amd64.deb: docker-compose up debian9_build docker-compose rm -f +binaries/proxysql_${CURVER}-debian10_amd64.deb: + docker-compose up debian10_build + docker-compose rm -f + binaries/proxysql_${CURVER}-clickhouse-debian9_amd64.deb: docker-compose up debian9_ch_build docker-compose rm -f @@ -356,6 +369,10 @@ binaries/proxysql_${CURVER}-clickhouse-debian9.4_amd64.deb: docker-compose up debian9.4_ch_build docker-compose rm -f +binaries/proxysql_${CURVER}-clickhouse-debian10_amd64.deb: + docker-compose up debian10_ch_build + docker-compose rm -f + binaries/proxysql_${CURVER}-dbg-ubuntu14_amd64.deb: docker-compose up ubuntu14_dbg_build docker-compose rm -f @@ -392,6 +409,10 @@ binaries/proxysql_${CURVER}-dbg-debian9.4_amd64.deb: docker-compose up debian9_dbg_build docker-compose rm -f +binaries/proxysql_${CURVER}-dbg-debian10_amd64.deb: + docker-compose up debian10_dbg_build + docker-compose rm -f + .PHONY: cleanall cleanall: cd deps && ${MAKE} cleanall diff --git a/docker-compose.yml b/docker-compose.yml index faf292ec3..dd60fbaf5 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,8 +57,8 @@ services: centos7_build: image: proxysql/packaging:build-centos7 volumes: - - ./docker/images/proxysql/rhel-compliant/rpmmacros/:/root/ - - ./docker/images/proxysql/rhel-compliant/entrypoint/:/opt/entrypoint/ + - ./docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/:/root/ + - ./docker/images/proxysql/rhel-compliant/rhel7/entrypoint/:/opt/entrypoint/ - ./:/opt/proxysql/ environment: - MAKE @@ -70,8 +70,8 @@ services: centos7_dbg_build: image: proxysql/packaging:build-centos7 volumes: - - ./docker/images/proxysql/rhel-compliant/rpmmacros/:/root/ - - ./docker/images/proxysql/rhel-compliant/entrypoint/:/opt/entrypoint/ + - ./docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/:/root/ + - ./docker/images/proxysql/rhel-compliant/rhel7/entrypoint/:/opt/entrypoint/ - ./:/opt/proxysql/ environment: - MAKE @@ -84,8 +84,8 @@ services: centos7_ch_build: image: proxysql/packaging:build-centos7 volumes: - - ./docker/images/proxysql/rhel-compliant/rpmmacros/:/root/ - - ./docker/images/proxysql/rhel-compliant/entrypoint/:/opt/entrypoint/ + - ./docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/:/root/ + - ./docker/images/proxysql/rhel-compliant/rhel7/entrypoint/:/opt/entrypoint/ - ./:/opt/proxysql/ environment: - MAKE @@ -354,6 +354,47 @@ services: - PROXYSQL_BUILD_TYPE=clickhouse command: - /opt/entrypoint/entrypoint.bash + debian10_build: + image: proxysql/packaging:build-debian10 + volumes: + - ./docker/images/proxysql/deb-compliant/latest-package/ctl/:/root/ctl/ + - ./docker/images/proxysql/deb-compliant/entrypoint/:/opt/entrypoint/ + - ./:/opt/proxysql/ + environment: + - MAKE + - MAKEOPT + - CURVER + - PKG_RELEASE=debian10 + command: + - /opt/entrypoint/entrypoint.bash + debian10_dbg_build: + image: proxysql/packaging:build-debian10 + volumes: + - ./docker/images/proxysql/deb-compliant/latest-package/ctl/:/root/ctl/ + - ./docker/images/proxysql/deb-compliant/entrypoint/:/opt/entrypoint/ + - ./:/opt/proxysql/ + environment: + - MAKE + - MAKEOPT + - CURVER + - PKG_RELEASE=dbg-debian10 + - PROXYSQL_BUILD_TYPE=debug + command: + - /opt/entrypoint/entrypoint.bash + debian10_ch_build: + image: proxysql/packaging:build-debian10 + volumes: + - ./docker/images/proxysql/deb-compliant/latest-package/ctl/:/root/ctl/ + - ./docker/images/proxysql/deb-compliant/entrypoint/:/opt/entrypoint/ + - ./:/opt/proxysql/ + environment: + - MAKE + - MAKEOPT + - CURVER + - PKG_RELEASE=clickhouse-debian10 + - PROXYSQL_BUILD_TYPE=clickhouse + command: + - /opt/entrypoint/entrypoint.bash ubuntu12_build: image: proxysql/packaging:build-ubuntu12 volumes: @@ -452,7 +493,7 @@ services: ubuntu18_build: image: proxysql/packaging:build-ubuntu18 volumes: - - ./docker/images/proxysql/deb-compliant/bionic-package/ctl/:/root/ctl/ + - ./docker/images/proxysql/deb-compliant/latest-package/ctl/:/root/ctl/ - ./docker/images/proxysql/deb-compliant/entrypoint/:/opt/entrypoint/ - ./:/opt/proxysql/ environment: @@ -465,7 +506,7 @@ services: ubuntu18_dbg_build: image: proxysql/packaging:build-ubuntu18 volumes: - - ./docker/images/proxysql/deb-compliant/bionic-package/ctl/:/root/ctl/ + - ./docker/images/proxysql/deb-compliant/latest-package/ctl/:/root/ctl/ - ./docker/images/proxysql/deb-compliant/entrypoint/:/opt/entrypoint/ - ./:/opt/proxysql/ environment: @@ -479,7 +520,7 @@ services: ubuntu18_ch_build: image: proxysql/packaging:build-ubuntu18 volumes: - - ./docker/images/proxysql/deb-compliant/bionic-package/ctl/:/root/ctl/ + - ./docker/images/proxysql/deb-compliant/latest-package/ctl/:/root/ctl/ - ./docker/images/proxysql/deb-compliant/entrypoint/:/opt/entrypoint/ - ./:/opt/proxysql/ environment: diff --git a/docker/images/proxysql/deb-compliant/bionic-package/ctl/proxysql.ctl b/docker/images/proxysql/deb-compliant/latest-package/ctl/proxysql.ctl similarity index 100% rename from docker/images/proxysql/deb-compliant/bionic-package/ctl/proxysql.ctl rename to docker/images/proxysql/deb-compliant/latest-package/ctl/proxysql.ctl diff --git a/docker/images/proxysql/rhel-compliant/rhel7/entrypoint/entrypoint.bash b/docker/images/proxysql/rhel-compliant/rhel7/entrypoint/entrypoint.bash new file mode 100755 index 000000000..0d6424d12 --- /dev/null +++ b/docker/images/proxysql/rhel-compliant/rhel7/entrypoint/entrypoint.bash @@ -0,0 +1,48 @@ +#!/bin/bash +set -eu + +echo "==> Build environment:" +env + +echo "==> Cleaning" +# Delete package if exists +rm -f /opt/proxysql/binaries/proxysql-${CURVER}-1-${PKG_RELEASE}.x86_64.rpm || true +# Cleanup relic directories from a previously failed build +rm -fr /root/.pki /root/rpmbuild/{BUILDROOT,RPMS,SRPMS,BUILD,SOURCES,tmp} /opt/proxysql/proxysql /opt/proxysql/proxysql-${CURVER} || true + +# Clean and build dependancies and source +echo "==> Building" +cd /opt/proxysql +if [[ -z ${PROXYSQL_BUILD_TYPE:-} ]] ; then + deps_target="build_deps" + build_target="" +else + deps_target="build_deps_$PROXYSQL_BUILD_TYPE" + build_target="$PROXYSQL_BUILD_TYPE" +fi +${MAKE} cleanbuild +${MAKE} ${MAKEOPT} "${deps_target}" + +if [[ -z ${build_target} ]] ; then + ${MAKE} ${MAKEOPT} +else + ${MAKE} ${MAKEOPT} "${build_target}" +fi + +# Prepare package files and build RPM +echo "==> Packaging" +mkdir -p proxysql/usr/bin proxysql/etc +cp src/proxysql proxysql/usr/bin/ +cp -a systemd proxysql/etc/ +cp -a etc/proxysql.cnf proxysql/etc/ +cp -a etc/logrotate.d proxysql/etc/ +mkdir -p proxysql/usr/share/proxysql/tools +cp -a tools/proxysql_galera_checker.sh tools/proxysql_galera_writer.pl proxysql/usr/share/proxysql/tools +mv proxysql "proxysql-${CURVER}" +tar czvf "proxysql-${CURVER}.tar.gz" proxysql-${CURVER} +mkdir -p /root/rpmbuild/{RPMS,SRPMS,BUILD,SOURCES,SPECS,tmp} +mv "/opt/proxysql/proxysql-${CURVER}.tar.gz" /root/rpmbuild/SOURCES +cd /root/rpmbuild && rpmbuild -ba SPECS/proxysql.spec --define "version ${CURVER}" +mv "/root/rpmbuild/RPMS/x86_64/proxysql-${CURVER}-1.x86_64.rpm" "/opt/proxysql/binaries/proxysql-${CURVER}-1-${PKG_RELEASE}.x86_64.rpm" +# Cleanup current build +rm -fr /root/.pki /root/rpmbuild/{BUILDROOT,RPMS,SRPMS,BUILD,SOURCES,tmp} /opt/proxysql/proxysql "/opt/proxysql/proxysql-${CURVER}" diff --git a/docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/.rpmmacros b/docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/.rpmmacros new file mode 100644 index 000000000..a1d0f32f0 --- /dev/null +++ b/docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/.rpmmacros @@ -0,0 +1,2 @@ +%_topdir %(echo $HOME)/rpmbuild +%_tmppath %{_topdir}/tmp \ No newline at end of file diff --git a/docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/rpmbuild/SPECS/proxysql.spec b/docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/rpmbuild/SPECS/proxysql.spec new file mode 100644 index 000000000..4049b45df --- /dev/null +++ b/docker/images/proxysql/rhel-compliant/rhel7/rpmmacros/rpmbuild/SPECS/proxysql.spec @@ -0,0 +1,90 @@ +%define __spec_install_post %{nil} +%define debug_package %{nil} +%define __os_install_post %{_dbpath}/brp-compress + +Summary: A high-performance MySQL proxy +Name: proxysql +Version: %{version} +Release: 1 +License: GPL+ +Group: Development/Tools +SOURCE0 : %{name}-%{version}.tar.gz +URL: http://www.proxysql.com/ + +BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root + +%description +%{summary} + +%prep +%setup -q + +%pre +# Cleanup artifacts +if [ -f /var/lib/%{name}/PROXYSQL_UPGRADE ]; then + rm -fr /var/lib/%{name}/PROXYSQL_UPGRADE +fi + +%build +# Packages are pre-built, nothing to do + +%install +# Clean buildroot and install files +/bin/rm -rf %{buildroot} +/bin/mkdir -p %{buildroot} +/bin/cp -a * %{buildroot} + +%clean +/bin/rm -rf %{buildroot} + +%post +# Create relevant user, directories and configuration files +if [ ! -d /var/run/%{name} ]; then /bin/mkdir /var/run/%{name} ; fi +if [ ! -d /var/lib/%{name} ]; then /bin/mkdir /var/lib/%{name} ; fi +if ! id -u %{name} > /dev/null 2>&1; then useradd -r -U -s /bin/false -d /var/lib/%{name} -c "ProxySQL Server" %{name}; fi +/bin/chown -R %{name}: /var/lib/%{name} /var/run/%{name} +/bin/chown root:%{name} /etc/%{name}.cnf +/bin/chmod 640 /etc/%{name}.cnf +# Configure systemd appropriately. +/bin/systemctl daemon-reload +/bin/systemctl enable %{name}.service +# Notify that a package update is in progress in order to start service. +if [ $1 -eq 2 ]; then /bin/touch /var/lib/%{name}/PROXYSQL_UPGRADE ; fi + +%preun +# When uninstalling always try stop the service, ignore failures +/bin/systemctl stop %{name} || true + +%postun +if [ $1 -eq 0 ]; then + # This is a pure uninstall, systemd unit file removed + # only daemon-reload is needed. + /bin/systemctl daemon-reload +else + # This is an upgrade, ProxySQL should be started. This + # logic works for packages newer than 2.0.7 and ensures + # a faster restart time. + /bin/systemctl start %{name}.service + /bin/rm -fr /var/lib/%{name}/PROXYSQL_UPGRADE +fi + +%posttrans +if [ -f /var/lib/%{name}/PROXYSQL_UPGRADE ]; then + # This is a safeguard to start the service after an update + # which supports legacy "preun" / "postun" logic and will + # only execute for packages before 2.0.7. + /bin/systemctl start %{name}.service + /bin/rm -fr /var/lib/%{name}/PROXYSQL_UPGRADE +fi + +%files +%defattr(-,root,root,-) +%config(noreplace) %{_sysconfdir}/%{name}.cnf +%attr(640,root,%{name}) %{_sysconfdir}/%{name}.cnf +%config(noreplace) %attr(640,root,%{name}) %{_sysconfdir}/logrotate.d/%{name} +%{_bindir}/* +%{_sysconfdir}/systemd/system/%{name}.service +/usr/share/proxysql/tools/proxysql_galera_checker.sh +/usr/share/proxysql/tools/proxysql_galera_writer.pl + +%changelog diff --git a/docker/images/proxysql/rhel-compliant/rpmmacros/rpmbuild/SPECS/proxysql.spec b/docker/images/proxysql/rhel-compliant/rpmmacros/rpmbuild/SPECS/proxysql.spec index cbf3bbd35..8432928c7 100644 --- a/docker/images/proxysql/rhel-compliant/rpmmacros/rpmbuild/SPECS/proxysql.spec +++ b/docker/images/proxysql/rhel-compliant/rpmmacros/rpmbuild/SPECS/proxysql.spec @@ -43,7 +43,6 @@ chown -R %{name}: /var/lib/%{name} /var/run/%{name} chown root:%{name} /etc/%{name}.cnf chmod 640 /etc/%{name}.cnf chkconfig --add %{name} -#systemctl enable proxysql.service %preun /etc/init.d/%{name} stop diff --git a/systemd/system/proxysql.service b/systemd/system/proxysql.service index 0c18b94a3..2a1330793 100644 --- a/systemd/system/proxysql.service +++ b/systemd/system/proxysql.service @@ -20,15 +20,11 @@ UMask=0007 LimitNOFILE=102400 LimitCORE=1073741824 ProtectHome=yes -ReadOnlyPaths=/ -ReadWritePaths=/var/lib/proxysql /var/run/proxysql NoNewPrivileges=true CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_ALG ProtectSystem=full PrivateDevices=yes -ProtectKernelTunables=true -ProtectControlGroups=true [Install] WantedBy=multi-user.target From f9e19ba03eadb8a041d12221c35fadb2d9a815fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20del=20Campo?= Date: Thu, 3 Oct 2019 16:44:09 +0200 Subject: [PATCH 2/2] Ported back the galera support fix to v2.0.7 --- include/MySQL_HostGroups_Manager.h | 2 + lib/MySQL_HostGroups_Manager.cpp | 68 +++++++++++++++++++----------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 482bfac62..338bb2c5c 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -471,6 +471,8 @@ class MySQL_HostGroups_Manager { void purge_mysql_servers_table(); void generate_mysql_servers_table(int *_onlyhg=NULL); void generate_mysql_replication_hostgroups_table(); + Galera_Info *get_galera_node_info(int hostgroup); + SQLite3_result *incoming_replication_hostgroups; void generate_mysql_group_replication_hostgroups_table(); diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index d0b6c033b..6d9cd458b 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -1982,7 +1982,7 @@ void MySQL_HostGroups_Manager::generate_mysql_group_replication_hostgroups_table int cols=0; int affected_rows=0; SQLite3_result *resultset=NULL; - char *query=(char *)"SELECT writer_hostgroup, hostname, port, MAX(use_ssl) use_ssl , writer_is_also_reader , max_transactions_behind FROM mysql_servers JOIN mysql_group_replication_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=offline_hostgroup WHERE active=1 AND status NOT IN (2,3) GROUP BY hostname, port"; + char *query=(char *)"SELECT writer_hostgroup, hostname, port, MAX(use_ssl) use_ssl , writer_is_also_reader , max_transactions_behind FROM mysql_servers JOIN mysql_group_replication_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=offline_hostgroup GROUP BY hostgroup, hostname, port"; mydb->execute_statement(query, &error , &cols , &affected_rows , &resultset); if (resultset) { if (GloMyMon->Group_Replication_Hosts_resultset) { @@ -2078,7 +2078,7 @@ void MySQL_HostGroups_Manager::generate_mysql_galera_hostgroups_table() { int cols=0; int affected_rows=0; SQLite3_result *resultset=NULL; - char *query=(char *)"SELECT writer_hostgroup, hostname, port, MAX(use_ssl) use_ssl , writer_is_also_reader , max_transactions_behind FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=offline_hostgroup WHERE active=1 AND status NOT IN (2,3) GROUP BY hostname, port"; + char *query=(char *)"SELECT writer_hostgroup, hostname, port, MAX(use_ssl) use_ssl , writer_is_also_reader , max_transactions_behind FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=offline_hostgroup GROUP BY hostgroup_id, hostname, port"; mydb->execute_statement(query, &error , &cols , &affected_rows , &resultset); if (resultset) { if (GloMyMon->Galera_Hosts_resultset) { @@ -4292,7 +4292,7 @@ void MySQL_HostGroups_Manager::update_galera_set_offline(char *_hostname, int _p char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup WHERE hostname='%s' AND port=%d AND status<>3"; + q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup WHERE hostname='%s' AND port=%d"; query=(char *)malloc(strlen(q)+strlen(_hostname)+1024); // increased this buffer as it is used for other queries too sprintf(query,q,_hostname,_port); mydb->execute_statement(query, &error , &cols , &affected_rows , &resultset); @@ -4319,22 +4319,24 @@ void MySQL_HostGroups_Manager::update_galera_set_offline(char *_hostname, int _p delete numw_result; } } - if (set_offline) { + + auto info = get_galera_node_info(_writer_hostgroup); + if (set_offline && info) { mydb->execute("DELETE FROM mysql_servers_incoming"); mydb->execute("INSERT INTO mysql_servers_incoming SELECT hostgroup_id, hostname, port, gtid_port, weight, status, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM mysql_servers"); - q=(char *)"UPDATE OR IGNORE mysql_servers_incoming SET hostgroup_id=(SELECT offline_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d) WHERE hostname='%s' AND port=%d AND hostgroup_id<>(SELECT offline_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; - query=(char *)malloc(strlen(q)+strlen(_hostname)+64); - sprintf(query,q,_writer_hostgroup,_hostname,_port,_writer_hostgroup); + q=(char *)"UPDATE OR IGNORE mysql_servers_incoming SET hostgroup_id=%d WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d, %d)"; + query=(char *)malloc(strlen(q)+strlen(_hostname)+128); + sprintf(query,q,info->offline_hostgroup,_hostname,_port,_writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup); mydb->execute(query); //free(query); - q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id<>(SELECT offline_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; + q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d, %d)"; //query=(char *)malloc(strlen(q)+strlen(_hostname)+64); - sprintf(query,q,_hostname,_port,_writer_hostgroup); + sprintf(query,q,_hostname,_port,_writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup); mydb->execute(query); //free(query); - q=(char *)"UPDATE mysql_servers_incoming SET status=0 WHERE hostname='%s' AND port=%d AND hostgroup_id=(SELECT offline_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; + q=(char *)"UPDATE mysql_servers_incoming SET status=0 WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d, %d)"; //query=(char *)malloc(strlen(q)+strlen(_hostname)+64); - sprintf(query,q,_hostname,_port,_writer_hostgroup); + sprintf(query,q,_hostname,_port,_writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup); mydb->execute(query); //free(query); converge_galera_config(_writer_hostgroup); @@ -4425,7 +4427,7 @@ void MySQL_HostGroups_Manager::update_galera_set_read_only(char *_hostname, int char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=offline_hostgroup WHERE hostname='%s' AND port=%d AND status<>3"; + q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=offline_hostgroup WHERE hostname='%s' AND port=%d"; query=(char *)malloc(strlen(q)+strlen(_hostname)+32); sprintf(query,q,_hostname,_port); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset); @@ -4434,25 +4436,27 @@ void MySQL_HostGroups_Manager::update_galera_set_read_only(char *_hostname, int error=NULL; } free(query); - if (resultset) { // we lock only if needed + + auto info = get_galera_node_info(_writer_hostgroup); + if (resultset && info) { // we lock only if needed if (resultset->rows_count) { proxy_warning("Galera: setting host %s:%d (part of cluster with writer_hostgroup=%d) in read_only because: %s\n", _hostname, _port, _writer_hostgroup, _error); GloAdmin->mysql_servers_wrlock(); mydb->execute("DELETE FROM mysql_servers_incoming"); mydb->execute("INSERT INTO mysql_servers_incoming SELECT hostgroup_id, hostname, port, gtid_port, weight, status, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM mysql_servers"); - q=(char *)"UPDATE OR IGNORE mysql_servers_incoming SET hostgroup_id=(SELECT reader_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d) WHERE hostname='%s' AND port=%d AND hostgroup_id<>(SELECT reader_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; + q=(char *)"UPDATE OR IGNORE mysql_servers_incoming SET hostgroup_id=%d WHERE hostname='%s' AND port=%d AND hostgroup_id (%d, %d)"; query=(char *)malloc(strlen(q)+strlen(_hostname)+512); - sprintf(query,q,_writer_hostgroup,_hostname,_port,_writer_hostgroup); + sprintf(query, q, info->reader_hostgroup, _hostname, _port, info->writer_hostgroup, info->backup_writer_hostgroup); mydb->execute(query); //free(query); - q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id<>(SELECT reader_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; + q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d) FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; //query=(char *)malloc(strlen(q)+strlen(_hostname)+64); - sprintf(query,q,_hostname,_port,_writer_hostgroup); + sprintf(query,q,_writer_hostgroup,_hostname,_port, info->writer_hostgroup, info->backup_writer_hostgroup); mydb->execute(query); //free(query); - q=(char *)"UPDATE mysql_servers_incoming SET status=0 WHERE hostname='%s' AND port=%d AND hostgroup_id=(SELECT reader_hostgroup FROM mysql_galera_hostgroups WHERE writer_hostgroup=%d)"; + q=(char *)"UPDATE mysql_servers_incoming SET status=0 WHERE hostname='%s' AND port=%d AND hostgroup_id=%d"; //query=(char *)malloc(strlen(q)+strlen(_hostname)+64); - sprintf(query,q,_hostname,_port,_writer_hostgroup); + sprintf(query,q,_hostname,_port,info->reader_hostgroup); mydb->execute(query); //free(query); converge_galera_config(_writer_hostgroup); @@ -4494,6 +4498,18 @@ void MySQL_HostGroups_Manager::update_galera_set_read_only(char *_hostname, int } } +Galera_Info *MySQL_HostGroups_Manager::get_galera_node_info(int hostgroup) { + pthread_mutex_lock(&Galera_Info_mutex); + auto it2 = Galera_Info_Map.find(hostgroup); + Galera_Info *info = nullptr; + if (it2 != Galera_Info_Map.end()) { + info = it2->second; + } + pthread_mutex_unlock(&Galera_Info_mutex); + + return info; +} + void MySQL_HostGroups_Manager::update_galera_set_writer(char *_hostname, int _port, int _writer_hostgroup) { std::mutex local_mutex; std::lock_guard lock(local_mutex); @@ -4503,7 +4519,7 @@ void MySQL_HostGroups_Manager::update_galera_set_writer(char *_hostname, int _po char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=offline_hostgroup WHERE hostname='%s' AND port=%d AND status<>3"; + q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=offline_hostgroup WHERE hostname='%s' AND port=%d"; query=(char *)malloc(strlen(q)+strlen(_hostname)+32); sprintf(query,q,_hostname,_port); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset); @@ -4519,12 +4535,14 @@ void MySQL_HostGroups_Manager::update_galera_set_writer(char *_hostname, int _po int read_HG=-1; bool need_converge=false; int max_writers = 0; + Galera_Info *info=NULL; + if (resultset) { // let's get info about this cluster pthread_mutex_lock(&Galera_Info_mutex); std::map::iterator it2; it2 = Galera_Info_Map.find(_writer_hostgroup); - Galera_Info *info=NULL; + if (it2!=Galera_Info_Map.end()) { info=it2->second; writer_is_also_reader=info->writer_is_also_reader; @@ -4593,17 +4611,17 @@ void MySQL_HostGroups_Manager::update_galera_set_writer(char *_hostname, int _po GloAdmin->mysql_servers_wrlock(); mydb->execute("DELETE FROM mysql_servers_incoming"); mydb->execute("INSERT INTO mysql_servers_incoming SELECT hostgroup_id, hostname, port, gtid_port, weight, status, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM mysql_servers"); - q=(char *)"UPDATE OR IGNORE mysql_servers_incoming SET hostgroup_id=%d WHERE hostname='%s' AND port=%d AND hostgroup_id<>%d"; + q=(char *)"UPDATE OR IGNORE mysql_servers_incoming SET hostgroup_id=%d WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d, %d, %d)"; query=(char *)malloc(strlen(q)+strlen(_hostname)+1024); // increased this buffer as it is used for other queries too - sprintf(query,q,_writer_hostgroup,_hostname,_port,_writer_hostgroup); + sprintf(query,q,_writer_hostgroup,_hostname,_port,_writer_hostgroup, info->reader_hostgroup, info->backup_writer_hostgroup, info->offline_hostgroup); mydb->execute(query); q=(char *)"UPDATE mysql_servers_incoming SET status=0 WHERE hostname='%s' AND port=%d AND hostgroup_id=%d"; sprintf(query,q,_hostname,_port,_writer_hostgroup); mydb->execute(query); //free(query); - q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id<>%d"; + q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d, %d)"; //query=(char *)malloc(strlen(q)+strlen(_hostname)+64); - sprintf(query,q,_hostname,_port,_writer_hostgroup); + sprintf(query,q,_hostname,_port, info->reader_hostgroup, info->backup_writer_hostgroup, info->offline_hostgroup); mydb->execute(query); //free(query); q=(char *)"UPDATE mysql_servers_incoming SET status=0 WHERE hostname='%s' AND port=%d AND hostgroup_id=%d";