Use repository generations to determine the best leader to elect
As we have generations for each repository we could use this info to determine a new primary node more efficiently and correctly. The new primary is the one that has less delay in total for all repositories. If there is no data to determine the primary in the database the first candidate is the new primary.
Closes: #3008 (closed)