### Elasticsearch Version

7.16.2, master

### Installed Plugins

no

### Java Version

_bundled_

### OS Version

Linux iZbp1dn0ktfed9mjoyuiajZ 5.10.84-10.2.al8.x86_64 #1 SMP Thu Feb 10 15:03:09 CST 2022 x86_64 x86_64 x86_64 GNU/Linux

### Problem Description

When put repository using PutRespository API for an existing repository, NullPointerException was thrown, short stack shows as below
```
1> java.lang.Exception: java.lang.NullPointerException: Cannot invoke "org.elasticsearch.repositories.Repository.getMetadata()" because "repository" is null
  1>   at org.elasticsearch.cluster.service.MasterService.innerExecuteTasks(MasterService.java:916) ~[main/:?]
  1>   at org.elasticsearch.cluster.service.MasterService.executeTasks(MasterService.java:871) ~[main/:?]
  1>   at org.elasticsearch.cluster.service.MasterService.runTasks(MasterService.java:253) [main/:?]
  1>   at org.elasticsearch.cluster.service.MasterService$Batcher.run(MasterService.java:153) [main/:?]
  1>   at org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:114) [main/:?]
  1>   at org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:170) [main/:?]
  1>   at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:714) [main/:?]
  1>   at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:260) [main/:?]
  1>   at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:223) [main/:?]
  1>   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
  1>   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
  1>   at java.lang.Thread.run(Thread.java:833) [?:?]
```

### Steps to Reproduce

I've created an IT to reproduce this issue in [#85551](https://github.com/elastic/elasticsearch/pull/85551), steps can be described as following:
- put repository using `RestPutRepository` API and make sure non-master node failed to create repository.
- restart master let one of the previous non-master node become master
- put the same repository using the `RestPutRepository` API.

### Logs (if relevant)

_No response_
server/src/test/java/org/elasticsearch/repositories/RepositoriesServiceTests.java
repositoriesService.applyClusterState(new ClusterChangedEvent("new repo", clusterStateWithRepoTypeB, emptyState()));
        List<RepositoryStatsSnapshot> repositoriesStats = repositoriesService.repositoriesStats();
        assertThat(repositoriesStats.size(), equalTo(2));
        RepositoryStatsSnapshot repositoryStatsTypeA = repositoriesStats.get(0);
        assertThat(repositoryStatsTypeA.getRepositoryInfo().type, equalTo(MeteredRepositoryTypeA.TYPE));
        assertThat(repositoryStatsTypeA.getRepositoryStats(), equalTo(MeteredRepositoryTypeA.STATS));
        RepositoryStatsSnapshot repositoryStatsTypeB = repositoriesStats.get(1);
        assertThat(repositoryStatsTypeB.getRepositoryInfo().type, equalTo(MeteredRepositoryTypeB.TYPE));
        assertThat(repositoryStatsTypeB.getRepositoryStats(), equalTo(MeteredRepositoryTypeB.STATS));
    }
    
    public void testHandlesUnknownRepositoryTypeWhenApplyingClusterState() {
        var repoName = randomAlphaOfLengthBetween(10, 25);
        var clusterState = createClusterStateWithRepo(repoName, "unknown");
        repositoriesService.applyClusterState(new ClusterChangedEvent("starting", clusterState, emptyState()));
        var repo = repositoriesService.repository(repoName);
        assertThat(repo, isA(UnknownTypeRepository.class));
    }
    public void testRemoveUnknownRepositoryTypeWhenApplyingClusterState() {
        var repoName = randomAlphaOfLengthBetween(10, 25);
        var clusterState = createClusterStateWithRepo(repoName, "unknown");
        repositoriesService.applyClusterState(new ClusterChangedEvent("starting", clusterState, emptyState()));
        repositoriesService.applyClusterState(new ClusterChangedEvent("removing repo", emptyState(), clusterState));
        assertThatThrows(
            () -> repositoriesService.repository(repoName),
            RepositoryMissingException.class,
            equalTo("[" + repoName + "] missing")
        );
    }
    public void testRegisterRepositoryFailsForUnknownType() {