`IndexMetadataUpdater.applyChanges` asserts the indices lookup for the old and new metadata is the same instance. Somehow this assertion does not always hold.

**Build scan:**
https://gradle-enterprise.elastic.co/s/kbk2xlunkwkfu/tests/:server:internalClusterTest/org.elasticsearch.aliases.IndexAliasesIT/testGetAllAliasesWorks

**Reproduction line:**
`./gradlew ':server:internalClusterTest' --tests "org.elasticsearch.aliases.IndexAliasesIT.testGetAllAliasesWorks" -Dtests.seed=2C9E27B978B87A4E -Dtests.locale=ja-JP -Dtests.timezone=America/Cancun -Druntime.java=17`

**Applicable branches:**
master

**Reproduces locally?:**
No

**Failure history:**
https://gradle-enterprise.elastic.co/scans/tests?tests.container=org.elasticsearch.aliases.IndexAliasesIT&tests.test=testGetAllAliasesWorks

**Failure excerpt:**
```
com.carrotsearch.randomizedtesting.UncaughtExceptionError: Captured an uncaught exception in thread: Thread[id=783, name=elasticsearch[node_s0][masterService#updateTask][T#1], state=RUNNABLE, group=TGRP-IndexAliasesIT]

  at __randomizedtesting.SeedInfo.seed([2C9E27B978B87A4E:7B9FD0D51559F652]:0)

  Caused by: java.lang.AssertionError: (No message provided)

    at __randomizedtesting.SeedInfo.seed([2C9E27B978B87A4E]:0)
    at org.elasticsearch.cluster.routing.allocation.IndexMetadataUpdater.applyChanges(IndexMetadataUpdater.java:134)
    at org.elasticsearch.cluster.routing.allocation.RoutingAllocation.updateMetadataWithRoutingChanges(RoutingAllocation.java:274)
    at org.elasticsearch.cluster.routing.allocation.AllocationService.buildResultAndLogHealthChange(AllocationService.java:150)
    at org.elasticsearch.cluster.routing.allocation.AllocationService.applyStartedShards(AllocationService.java:143)
    at org.elasticsearch.cluster.action.shard.ShardStateAction$ShardStartedClusterStateTaskExecutor.execute(ShardStateAction.java:734)
    at org.elasticsearch.cluster.service.MasterService.innerExecuteTasks(MasterService.java:901)
    at org.elasticsearch.cluster.service.MasterService.executeTasks(MasterService.java:871)
    at org.elasticsearch.cluster.service.MasterService.runTasks(MasterService.java:253)
    at org.elasticsearch.cluster.service.MasterService$Batcher.run(MasterService.java:153)
    at org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:114)
    at org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:170)
    at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:714)
    at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:260)
    at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:223)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.lang.Thread.run(Thread.java:833)

```
server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetadataUpdater.java
* we update {@link IndexMetadata#getInSyncAllocationIds()} and {@link IndexMetadata#primaryTerm(int)} based on
     * the changes made during this allocation.
     *
     * @param oldMetadata {@link Metadata} object from before the routing nodes was changed.
     * @param newRoutingTable {@link RoutingTable} object after routing changes were applied.
     * @return adapted {@link Metadata}, potentially the original one if no change was needed.
     */
    public Metadata applyChanges(Metadata oldMetadata, RoutingTable newRoutingTable) {
        Map<Index, List<Map.Entry<ShardId, Updates>>> changesGroupedByIndex = shardChanges.entrySet()
            .stream()
            .collect(Collectors.groupingBy(e -> e.getKey().getIndex()));
        Metadata.Builder metadataBuilder = null;
        for (Map.Entry<Index, List<Map.Entry<ShardId, Updates>>> indexChanges : changesGroupedByIndex.entrySet()) {
            Index index = indexChanges.getKey();
            final IndexMetadata oldIndexMetadata = oldMetadata.getIndexSafe(index);
            IndexMetadata.Builder indexMetadataBuilder = null;
            for (Map.Entry<ShardId, Updates> shardEntry : indexChanges.getValue()) {
                ShardId shardId = shardEntry.getKey();
                Updates updates = shardEntry.getValue();
                indexMetadataBuilder = updateInSyncAllocations(newRoutingTable, oldIndexMetadata, indexMetadataBuilder, shardId, updates);
                indexMetadataBuilder = updatePrimaryTerm(oldIndexMetadata, indexMetadataBuilder, shardId, updates);
            }
            if (indexMetadataBuilder != null) {
                if (metadataBuilder == null) {
                    metadataBuilder = Metadata.builder(oldMetadata);
                }
                metadataBuilder.put(indexMetadataBuilder);
            }
        }
        if (metadataBuilder != null) {