Today the `ReservedClusterStateService` decides whether or not to submit an update-error task depending on the content of the last-applied state as exposed by `ClusterService#state()`. This may happen concurrently with another state being published, so does not give a fully consistent view of the latest state. Instead it should check the state within the cluster state task executor, triggering a no-op update as appropriate.
server/src/test/java/org/elasticsearch/reservedstate/service/ReservedClusterStateServiceTests.java
is("Cycle found in settings dependencies: two -> one -> two")
            )
        );
    }
    public void testDuplicateHandlerNames() {
        ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
        ClusterService clusterService = mock(ClusterService.class);
        final ClusterName clusterName = new ClusterName("elasticsearch");
        ClusterState state = ClusterState.builder(clusterName).build();
        when(clusterService.state()).thenReturn(state);
        assertTrue(
            expectThrows(
                IllegalStateException.class,
                () -> new ReservedClusterStateService(
                    clusterService,
                    List.of(new ReservedClusterSettingsAction(clusterSettings), new TestHandler())
                )
            ).getMessage().startsWith("Duplicate key cluster_settings")
        );
    }
    public void testCheckAndReportError() {
        ClusterService clusterService = mock(ClusterService.class);
        final var controller = spy(new ReservedClusterStateService(clusterService, List.of()));
        assertNull(controller.checkAndReportError("test", List.of(), null, null));
        verify(controller, times(0)).saveErrorState(any(), any());
        var state = ClusterState.builder(new ClusterName("elasticsearch")).build();
        var version = new ReservedStateVersion(2L, Version.CURRENT);
        var error = controller.checkAndReportError("test", List.of("test error"), state, version);
        assertThat(error, allOf(notNullValue(), instanceOf(IllegalStateException.class)));
        assertEquals("Error processing state change request for test, errors: test error", error.getMessage());