This MR is EE port of https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/19672
https://gitlab.com/gitlab-org/gitlab-ce/issues/43011