Use `Gitlab::OptimisticLocking` to avoid `StaleObjectError` when an object is updated by multiple processes

Description

Merge Train uses state_machine for leveraging object's state. It generally works fine, however, when refresh_pipeline and outdate_pipeline run on the same object almost simultaneously, it could cause StaleObjectError as that either of the objects is already been updated by the other.

We should use OptimisticLocking to retry the state update in such case.

Follow-up

Follow-up from "Use state machine in Merge Train to avoid race conditions"

The following discussion from !23395 (merged) should be addressed:

  • @ayufan started a discussion: (+1 comment)

    @dosuken123 WDYT about adding Optimistic Locking to merge_trains?

    Will that help with anything? Like status transitions?