Today the stable-master health indicator reports `yellow` health if it sees the elected master change 4 times in 30 minutes. However, this could be caused by planned activity such as a rolling restart, and in that case we should not consider a sequence of master failovers as a sign of ill health.

I think we can solve this by refining this check to disregard master nodes that have restarted since they were master, or are no longer in the cluster at all. Restarting the master will always trigger a failover, and the stable-master indicator need not worry about whether the node restart was planned or not; separately, unplanned node restarts are definitely a sign of ill health, but that applies whether the node was the elected master or not. Similarly, if a node is no longer in the cluster then either it is no longer running (again, whether that's planned or not is irrelevant to the stable-master indicator) or else it is running and failing to join the cluster (and hence reporting its own ill health).
server/src/test/java/org/elasticsearch/cluster/coordination/MasterHistoryTests.java
public void testHasSeenMasterInLastNSeconds() {
        var clusterService = mock(ClusterService.class);
        when(clusterService.getSettings()).thenReturn(Settings.EMPTY);
        ThreadPool threadPool = mock(ThreadPool.class);
        MasterHistory masterHistory = new MasterHistory(threadPool, clusterService);
        /*
         * 60 minutes ago we get these master changes:
         * null -> node1 -> node2 -> node3
         * Except for when only null had been master, there has been a non-null master node in the last 5 seconds all along
         */
        long sixtyMinutesAgo = System.currentTimeMillis() - new TimeValue(60, TimeUnit.MINUTES).getMillis();
        when(threadPool.relativeTimeInMillis()).thenReturn(sixtyMinutesAgo);
        masterHistory.clusterChanged(new ClusterChangedEvent(TEST_SOURCE, nullMasterClusterState, nullMasterClusterState));
        assertFalse(masterHistory.hasSeenMasterInLastNSeconds(5));
        masterHistory.clusterChanged(new ClusterChangedEvent(TEST_SOURCE, node1MasterClusterState, nullMasterClusterState));
        masterHistory.clusterChanged(new ClusterChangedEvent(TEST_SOURCE, node2MasterClusterState, node1MasterClusterState));
        masterHistory.clusterChanged(new ClusterChangedEvent(TEST_SOURCE, node3MasterClusterState, node2MasterClusterState));
        assertTrue(masterHistory.hasSeenMasterInLastNSeconds(5));
        /*
         * 40 minutes ago we get these master changes (the master was node3 when this section began):
         * null -> node1 -> null -> null -> node1
         * There has been a non-null master for the last 5 seconds every step at this time
         */
        long fourtyMinutesAgo = System.currentTimeMillis() - new TimeValue(40, TimeUnit.MINUTES).getMillis();
        when(threadPool.relativeTimeInMillis()).thenReturn(fourtyMinutesAgo);
        assertTrue(masterHistory.hasSeenMasterInLastNSeconds(5));
        masterHistory.clusterChanged(new ClusterChangedEvent(TEST_SOURCE, nullMasterClusterState, node3MasterClusterState));
        assertTrue(masterHistory.hasSeenMasterInLastNSeconds(5));
        masterHistory.clusterChanged(new ClusterChangedEvent(TEST_SOURCE, node1MasterClusterState, nullMasterClusterState));
        assertTrue(masterHistory.hasSeenMasterInLastNSeconds(5));