Do recovery on HA master/slave switch

This PR ensures that HA master/slave switches do recovery during the process, rather than waiting for a live transaction to fail and cause a recovery.

Also includes refactorings to make it easier to test HA.