From d506fc76897bb0ef134db9701eb11063042985ca Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Thu, 16 Jun 2022 23:39:25 -0700 Subject: [PATCH 01/15] Deprecate public class names with master terminology Signed-off-by: Tianli Feng --- .../health/TransportClusterHealthAction.java | 4 +- .../ClusterManagerNodeChangePredicate.java | 8 ++- .../cluster/LocalNodeMasterListener.java | 44 ++++++++++++++ .../cluster/MasterNodeChangePredicate.java | 48 ++++++++++++++++ .../cluster/NotMasterException.java | 57 +++++++++++++++++++ .../coordination/NoMasterBlockService.java | 50 ++++++++++++++++ .../UnsafeBootstrapMasterCommand.java | 47 +++++++++++++++ .../cluster/service/MasterService.java | 52 +++++++++++++++++ .../settings/ConsistentSettingsService.java | 8 +-- .../MasterNotDiscoveredException.java | 56 ++++++++++++++++++ .../rest/action/cat/RestMasterAction.java | 44 ++++++++++++++ .../service/FakeThreadPoolMasterService.java | 53 +++++++++++++++++ .../BlockMasterServiceOnMaster.java | 46 +++++++++++++++ .../BusyMasterServiceDisruption.java | 48 ++++++++++++++++ 14 files changed, 557 insertions(+), 8 deletions(-) create mode 100644 server/src/main/java/org/opensearch/cluster/LocalNodeMasterListener.java create mode 100644 server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java create mode 100644 server/src/main/java/org/opensearch/cluster/NotMasterException.java create mode 100644 server/src/main/java/org/opensearch/cluster/coordination/NoMasterBlockService.java create mode 100644 server/src/main/java/org/opensearch/cluster/coordination/UnsafeBootstrapMasterCommand.java create mode 100644 server/src/main/java/org/opensearch/cluster/service/MasterService.java create mode 100644 server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java create mode 100644 server/src/main/java/org/opensearch/rest/action/cat/RestMasterAction.java create mode 100644 test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java create mode 100644 test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java create mode 100644 test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthAction.java b/server/src/main/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthAction.java index 8a2ad77ac1693..3e3d373ff3b31 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthAction.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthAction.java @@ -226,8 +226,8 @@ public void onNoLongerMaster(String source) { "stopped being cluster-manager while waiting for events with priority [{}]. retrying.", request.waitForEvents() ); - // TransportClusterManagerNodeAction implements the retry logic, which is triggered by passing a - // NotClusterManagerException + // TransportClusterManagerNodeAction implements the retry logic, + // which is triggered by passing a NotClusterManagerException listener.onFailure(new NotClusterManagerException("no longer cluster-manager. source: [" + source + "]")); } diff --git a/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java b/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java index ecb99e06f3ef0..a28b1690dabac 100644 --- a/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java +++ b/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java @@ -41,9 +41,13 @@ * * @opensearch.internal */ -public final class ClusterManagerNodeChangePredicate { +// TODO: add final keyword to the class and private keyword to the default constructor, +// after removing the deprecated class MasterNodeChangePredicate. +// Removed the final keyword temporarily only for making the class MasterNodeChangePredicate as a subclass, +// so that preserving the both class names by maintaining one class implementation for backwards compatibility. +public class ClusterManagerNodeChangePredicate { - private ClusterManagerNodeChangePredicate() { + ClusterManagerNodeChangePredicate() { } diff --git a/server/src/main/java/org/opensearch/cluster/LocalNodeMasterListener.java b/server/src/main/java/org/opensearch/cluster/LocalNodeMasterListener.java new file mode 100644 index 0000000000000..eebfb60d8472d --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/LocalNodeMasterListener.java @@ -0,0 +1,44 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster; + +/** + * Enables listening to cluster-manager changes events of the local node (when the local node becomes the cluster-manager, and when the local + * node cease being a cluster-manager). + * + * @opensearch.internal + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link LocalNodeClusterManagerListener} + */ +@Deprecated +public interface LocalNodeMasterListener extends LocalNodeClusterManagerListener { + +} diff --git a/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java b/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java new file mode 100644 index 0000000000000..3c86d9bbedc03 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster; + +/** + * Utility class to build a predicate that accepts cluster state changes + * + * @opensearch.internal + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link ClusterManagerNodeChangePredicate} + */ +@Deprecated +public final class MasterNodeChangePredicate extends ClusterManagerNodeChangePredicate { + + private MasterNodeChangePredicate() { + + } + +} diff --git a/server/src/main/java/org/opensearch/cluster/NotMasterException.java b/server/src/main/java/org/opensearch/cluster/NotMasterException.java new file mode 100644 index 0000000000000..1dbf13212d2dd --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/NotMasterException.java @@ -0,0 +1,57 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster; + +import org.opensearch.common.io.stream.StreamInput; + +import java.io.IOException; + +/** + * Thrown when a node join request or a cluster-manager ping reaches a node which is not + * currently acting as a cluster-manager or when a cluster state update task is to be executed + * on a node that is no longer cluster-manager. + * + * @opensearch.internal + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link NotClusterManagerException} + */ +@Deprecated +public class NotMasterException extends NotClusterManagerException { + + public NotMasterException(String msg) { + super(msg); + } + + public NotMasterException(StreamInput in) throws IOException { + super(in); + } + +} diff --git a/server/src/main/java/org/opensearch/cluster/coordination/NoMasterBlockService.java b/server/src/main/java/org/opensearch/cluster/coordination/NoMasterBlockService.java new file mode 100644 index 0000000000000..1dff7b2a8f0d6 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/coordination/NoMasterBlockService.java @@ -0,0 +1,50 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster.coordination; + +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Settings; + +/** + * Service to block the master node + * + * @opensearch.internal + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link NoClusterManagerBlockService} + */ +@Deprecated +public class NoMasterBlockService extends NoClusterManagerBlockService { + + public NoMasterBlockService(Settings settings, ClusterSettings clusterSettings) { + super(settings, clusterSettings); + } + +} diff --git a/server/src/main/java/org/opensearch/cluster/coordination/UnsafeBootstrapMasterCommand.java b/server/src/main/java/org/opensearch/cluster/coordination/UnsafeBootstrapMasterCommand.java new file mode 100644 index 0000000000000..6014dc0b44ab8 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/coordination/UnsafeBootstrapMasterCommand.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster.coordination; + +/** + * Tool to run an unsafe bootstrap + * + * @opensearch.internal + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link UnsafeBootstrapClusterManagerCommand} + */ +@Deprecated +public class UnsafeBootstrapMasterCommand extends UnsafeBootstrapClusterManagerCommand { + + UnsafeBootstrapMasterCommand() { + super(); + } + +} diff --git a/server/src/main/java/org/opensearch/cluster/service/MasterService.java b/server/src/main/java/org/opensearch/cluster/service/MasterService.java new file mode 100644 index 0000000000000..952c995cee549 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/service/MasterService.java @@ -0,0 +1,52 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster.service; + +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Settings; +import org.opensearch.threadpool.ThreadPool; + +/** + * Main Master Node Service + * + * @opensearch.internal + * @deprecated As of 2.1, because supporting inclusive language, replaced by {@link ClusterManagerService} + */ +@Deprecated +public class MasterService extends ClusterManagerService { + + public MasterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { + super(settings, clusterSettings, threadPool); + } + +} diff --git a/server/src/main/java/org/opensearch/common/settings/ConsistentSettingsService.java b/server/src/main/java/org/opensearch/common/settings/ConsistentSettingsService.java index 062d62956d377..3be1c4b080b5f 100644 --- a/server/src/main/java/org/opensearch/common/settings/ConsistentSettingsService.java +++ b/server/src/main/java/org/opensearch/common/settings/ConsistentSettingsService.java @@ -36,7 +36,7 @@ import org.apache.logging.log4j.Logger; import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.ClusterStateUpdateTask; -import org.opensearch.cluster.LocalNodeClusterManagerListener; +import org.opensearch.cluster.LocalNodeMasterListener; import org.opensearch.cluster.metadata.Metadata; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.Priority; @@ -88,10 +88,10 @@ public ConsistentSettingsService(Settings settings, ClusterService clusterServic } /** - * Returns a {@link LocalNodeClusterManagerListener} that will publish hashes of all the settings passed in the constructor. These hashes are + * Returns a {@link LocalNodeMasterListener} that will publish hashes of all the settings passed in the constructor. These hashes are * published by the cluster-manager node only. Note that this is not designed for {@link SecureSettings} implementations that are mutable. */ - public LocalNodeClusterManagerListener newHashPublisher() { + public LocalNodeMasterListener newHashPublisher() { // eagerly compute hashes to be published final Map computedHashesOfConsistentSettings = computeHashesOfConsistentSecureSettings(); return new HashesPublisher(computedHashesOfConsistentSettings, clusterService); @@ -246,7 +246,7 @@ private byte[] computeSaltedPBKDF2Hash(byte[] bytes, byte[] salt) { } } - static final class HashesPublisher implements LocalNodeClusterManagerListener { + static final class HashesPublisher implements LocalNodeMasterListener { // eagerly compute hashes to be published final Map computedHashesOfConsistentSettings; diff --git a/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java b/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java new file mode 100644 index 0000000000000..32ed409c500e1 --- /dev/null +++ b/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java @@ -0,0 +1,56 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.discovery; + +/** + * Exception when the cluster-manager is not discovered + * + * @opensearch.internal + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link ClusterManagerNotDiscoveredException} + */ +@Deprecated +public class MasterNotDiscoveredException extends ClusterManagerNotDiscoveredException { + + public MasterNotDiscoveredException() { + super(); + } + + public MasterNotDiscoveredException(Throwable cause) { + super(cause); + } + + public MasterNotDiscoveredException(String message) { + super(message); + } + +} diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestMasterAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestMasterAction.java new file mode 100644 index 0000000000000..20f7b01ef2b42 --- /dev/null +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestMasterAction.java @@ -0,0 +1,44 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.rest.action.cat; + +/** + * _cat API action to list cluster_manager information + * + * @opensearch.api + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link RestClusterManagerAction} + */ +@Deprecated +public class RestMasterAction extends RestClusterManagerAction { + +} diff --git a/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java b/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java new file mode 100644 index 0000000000000..da23714771eb6 --- /dev/null +++ b/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java @@ -0,0 +1,53 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.cluster.service; + +import org.opensearch.threadpool.ThreadPool; + +import java.util.function.Consumer; + +/** + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link FakeThreadPoolClusterManagerService} + */ +@Deprecated +public class FakeThreadPoolMasterService extends FakeThreadPoolClusterManagerService { + + public FakeThreadPoolMasterService( + String nodeName, + String serviceName, + ThreadPool threadPool, + Consumer onTaskAvailableToRun + ) { + super(nodeName, serviceName, threadPool, onTaskAvailableToRun); + } + +} diff --git a/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java b/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java new file mode 100644 index 0000000000000..3fd3843f9c4c3 --- /dev/null +++ b/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java @@ -0,0 +1,46 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.test.disruption; + +import java.util.Random; + +/** + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link BlockClusterManagerServiceOnClusterManager} + */ +@Deprecated +public class BlockMasterServiceOnMaster extends BlockClusterManagerServiceOnClusterManager { + + public BlockMasterServiceOnMaster(Random random) { + super(random); + } + +} diff --git a/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java b/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java new file mode 100644 index 0000000000000..1a88856489907 --- /dev/null +++ b/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.test.disruption; + +import org.opensearch.common.Priority; + +import java.util.Random; + +/** + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link BusyClusterManagerServiceDisruption} + */ +@Deprecated +public class BusyMasterServiceDisruption extends BusyClusterManagerServiceDisruption { + + public BusyMasterServiceDisruption(Random random, Priority priority) { + super(random, priority); + } + +} From ad991b724186a47c83473d9f3b42fbd70ae69e72 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Tue, 12 Jul 2022 16:39:16 -0700 Subject: [PATCH 02/15] Register back MasterNotDiscoveredException and NotMasterException Signed-off-by: Tianli Feng --- .../org/opensearch/OpenSearchException.java | 33 ++++++++++++++++--- .../MasterNotDiscoveredException.java | 7 ++++ .../ExceptionSerializationTests.java | 6 ++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/opensearch/OpenSearchException.java b/server/src/main/java/org/opensearch/OpenSearchException.java index 4c12d031a028f..d6c2b00ac8563 100644 --- a/server/src/main/java/org/opensearch/OpenSearchException.java +++ b/server/src/main/java/org/opensearch/OpenSearchException.java @@ -33,7 +33,6 @@ package org.opensearch; import org.opensearch.action.support.replication.ReplicationOperation; -import org.opensearch.cluster.NotClusterManagerException; import org.opensearch.cluster.action.shard.ShardStateAction; import org.opensearch.common.CheckedFunction; import org.opensearch.common.Nullable; @@ -47,7 +46,6 @@ import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentParseException; import org.opensearch.common.xcontent.XContentParser; -import org.opensearch.discovery.ClusterManagerNotDiscoveredException; import org.opensearch.index.Index; import org.opensearch.index.shard.ShardId; import org.opensearch.rest.RestStatus; @@ -791,8 +789,18 @@ private enum OpenSearchExceptionHandle { UNKNOWN_VERSION_ADDED ), CLUSTER_MANAGER_NOT_DISCOVERED_EXCEPTION( - ClusterManagerNotDiscoveredException.class, - ClusterManagerNotDiscoveredException::new, + org.opensearch.discovery.ClusterManagerNotDiscoveredException.class, + org.opensearch.discovery.ClusterManagerNotDiscoveredException::new, + 3, + UNKNOWN_VERSION_ADDED + ), + /** + * @deprecated As of 2.2, because supporting inclusive language, replaced by CLUSTER_MANAGER_NOT_DISCOVERED_EXCEPTION + */ + @Deprecated + MASTER_NOT_DISCOVERED_EXCEPTION( + org.opensearch.discovery.MasterNotDiscoveredException.class, + org.opensearch.discovery.MasterNotDiscoveredException::new, 3, UNKNOWN_VERSION_ADDED ), @@ -1501,7 +1509,22 @@ private enum OpenSearchExceptionHandle { 143, UNKNOWN_VERSION_ADDED ), - NOT_CLUSTER_MANAGER_EXCEPTION(NotClusterManagerException.class, NotClusterManagerException::new, 144, UNKNOWN_VERSION_ADDED), + NOT_CLUSTER_MANAGER_EXCEPTION( + org.opensearch.cluster.NotClusterManagerException.class, + org.opensearch.cluster.NotClusterManagerException::new, + 144, + UNKNOWN_VERSION_ADDED + ), + /** + * @deprecated As of 2.2, because supporting inclusive language, replaced by NOT_CLUSTER_MANAGER_EXCEPTION + */ + @Deprecated + NOT_MASTER_EXCEPTION( + org.opensearch.cluster.NotMasterException.class, + org.opensearch.cluster.NotMasterException::new, + 144, + UNKNOWN_VERSION_ADDED + ), STATUS_EXCEPTION( org.opensearch.OpenSearchStatusException.class, org.opensearch.OpenSearchStatusException::new, diff --git a/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java b/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java index 32ed409c500e1..7ed21f924b27c 100644 --- a/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java +++ b/server/src/main/java/org/opensearch/discovery/MasterNotDiscoveredException.java @@ -32,6 +32,10 @@ package org.opensearch.discovery; +import org.opensearch.common.io.stream.StreamInput; + +import java.io.IOException; + /** * Exception when the cluster-manager is not discovered * @@ -53,4 +57,7 @@ public MasterNotDiscoveredException(String message) { super(message); } + public MasterNotDiscoveredException(StreamInput in) throws IOException { + super(in); + } } diff --git a/server/src/test/java/org/opensearch/ExceptionSerializationTests.java b/server/src/test/java/org/opensearch/ExceptionSerializationTests.java index 2af68bbedb456..ae1b6e665db78 100644 --- a/server/src/test/java/org/opensearch/ExceptionSerializationTests.java +++ b/server/src/test/java/org/opensearch/ExceptionSerializationTests.java @@ -44,7 +44,6 @@ import org.opensearch.action.search.ShardSearchFailure; import org.opensearch.action.support.replication.ReplicationOperation; import org.opensearch.client.AbstractClientHeadersTestCase; -import org.opensearch.cluster.NotClusterManagerException; import org.opensearch.cluster.action.shard.ShardStateAction; import org.opensearch.cluster.block.ClusterBlockException; import org.opensearch.cluster.coordination.CoordinationStateRejectedException; @@ -70,7 +69,6 @@ import org.opensearch.common.util.CancellableThreadsTests; import org.opensearch.common.util.set.Sets; import org.opensearch.common.xcontent.XContentLocation; -import org.opensearch.discovery.ClusterManagerNotDiscoveredException; import org.opensearch.env.ShardLockObtainFailedException; import org.opensearch.index.Index; import org.opensearch.index.engine.RecoveryEngineException; @@ -697,7 +695,7 @@ public void testIds() { ids.put(0, org.opensearch.index.snapshots.IndexShardSnapshotFailedException.class); ids.put(1, org.opensearch.search.dfs.DfsPhaseExecutionException.class); ids.put(2, org.opensearch.common.util.CancellableThreads.ExecutionCancelledException.class); - ids.put(3, ClusterManagerNotDiscoveredException.class); + ids.put(3, org.opensearch.discovery.ClusterManagerNotDiscoveredException.class); ids.put(4, org.opensearch.OpenSearchSecurityException.class); ids.put(5, org.opensearch.index.snapshots.IndexShardRestoreException.class); ids.put(6, org.opensearch.indices.IndexClosedException.class); @@ -835,7 +833,7 @@ public void testIds() { ids.put(141, org.opensearch.index.query.QueryShardException.class); ids.put(142, ShardStateAction.NoLongerPrimaryShardException.class); ids.put(143, org.opensearch.script.ScriptException.class); - ids.put(144, NotClusterManagerException.class); + ids.put(144, org.opensearch.cluster.NotClusterManagerException.class); ids.put(145, org.opensearch.OpenSearchStatusException.class); ids.put(146, org.opensearch.tasks.TaskCancelledException.class); ids.put(147, org.opensearch.env.ShardLockObtainFailedException.class); From 2d826b6dfa0d940c6d3c88d0428155175f003588 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Tue, 12 Jul 2022 16:52:29 -0700 Subject: [PATCH 03/15] Adjust format for a comment Signed-off-by: Tianli Feng --- .../java/org/opensearch/cluster/coordination/JoinHelper.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java b/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java index aeff856bef51a..b2d1c6d14ebbb 100644 --- a/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java +++ b/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java @@ -143,9 +143,8 @@ public class JoinHelper { @Override public ClusterTasksResult execute(ClusterState currentState, List joiningTasks) throws Exception { - // The current state that ClusterManagerService uses might have been updated by a (different) cluster-manager in a higher - // term - // already + // The current state that ClusterManagerService uses might have been updated by a (different) cluster-manager + // in a higher term already // Stop processing the current cluster state update, as there's no point in continuing to compute it as // it will later be rejected by Coordinator.publish(...) anyhow if (currentState.term() > term) { From a862943d688e7737458b6e25039a63b7aa75662f Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Tue, 12 Jul 2022 17:17:59 -0700 Subject: [PATCH 04/15] Remove deprecated exception classes from the unregistered exception list in ExceptionSerializationTests Signed-off-by: Tianli Feng --- .../org/opensearch/OpenSearchException.java | 20 ------------------- .../ExceptionSerializationTests.java | 5 +++++ 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/server/src/main/java/org/opensearch/OpenSearchException.java b/server/src/main/java/org/opensearch/OpenSearchException.java index d6c2b00ac8563..4ebcd9622ce38 100644 --- a/server/src/main/java/org/opensearch/OpenSearchException.java +++ b/server/src/main/java/org/opensearch/OpenSearchException.java @@ -794,16 +794,6 @@ private enum OpenSearchExceptionHandle { 3, UNKNOWN_VERSION_ADDED ), - /** - * @deprecated As of 2.2, because supporting inclusive language, replaced by CLUSTER_MANAGER_NOT_DISCOVERED_EXCEPTION - */ - @Deprecated - MASTER_NOT_DISCOVERED_EXCEPTION( - org.opensearch.discovery.MasterNotDiscoveredException.class, - org.opensearch.discovery.MasterNotDiscoveredException::new, - 3, - UNKNOWN_VERSION_ADDED - ), OPENSEARCH_SECURITY_EXCEPTION( org.opensearch.OpenSearchSecurityException.class, org.opensearch.OpenSearchSecurityException::new, @@ -1515,16 +1505,6 @@ private enum OpenSearchExceptionHandle { 144, UNKNOWN_VERSION_ADDED ), - /** - * @deprecated As of 2.2, because supporting inclusive language, replaced by NOT_CLUSTER_MANAGER_EXCEPTION - */ - @Deprecated - NOT_MASTER_EXCEPTION( - org.opensearch.cluster.NotMasterException.class, - org.opensearch.cluster.NotMasterException::new, - 144, - UNKNOWN_VERSION_ADDED - ), STATUS_EXCEPTION( org.opensearch.OpenSearchStatusException.class, org.opensearch.OpenSearchStatusException::new, diff --git a/server/src/test/java/org/opensearch/ExceptionSerializationTests.java b/server/src/test/java/org/opensearch/ExceptionSerializationTests.java index ae1b6e665db78..c0d30a92e2d4d 100644 --- a/server/src/test/java/org/opensearch/ExceptionSerializationTests.java +++ b/server/src/test/java/org/opensearch/ExceptionSerializationTests.java @@ -44,6 +44,7 @@ import org.opensearch.action.search.ShardSearchFailure; import org.opensearch.action.support.replication.ReplicationOperation; import org.opensearch.client.AbstractClientHeadersTestCase; +import org.opensearch.cluster.NotMasterException; import org.opensearch.cluster.action.shard.ShardStateAction; import org.opensearch.cluster.block.ClusterBlockException; import org.opensearch.cluster.coordination.CoordinationStateRejectedException; @@ -69,6 +70,7 @@ import org.opensearch.common.util.CancellableThreadsTests; import org.opensearch.common.util.set.Sets; import org.opensearch.common.xcontent.XContentLocation; +import org.opensearch.discovery.MasterNotDiscoveredException; import org.opensearch.env.ShardLockObtainFailedException; import org.opensearch.index.Index; import org.opensearch.index.engine.RecoveryEngineException; @@ -235,6 +237,9 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx Files.walkFileTree(testStartPath, visitor); assertTrue(notRegistered.remove(TestException.class)); assertTrue(notRegistered.remove(UnknownHeaderException.class)); + // Remove the deprecated exception classes from the unregistered list. + assertTrue(notRegistered.remove(NotMasterException.class)); + assertTrue(notRegistered.remove(MasterNotDiscoveredException.class)); assertTrue("Classes subclassing OpenSearchException must be registered \n" + notRegistered.toString(), notRegistered.isEmpty()); assertTrue(registered.removeAll(OpenSearchException.getRegisteredKeys())); // check assertEquals(registered.toString(), 0, registered.size()); From 5c085d8b2fa6e2e29d58589fe83163ca70a0c8a2 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 15:06:26 -0700 Subject: [PATCH 05/15] Add unit test for validating backwards compatibility of LocalNodeMasterListener Signed-off-by: Tianli Feng --- .../service/ClusterApplierServiceTests.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/server/src/test/java/org/opensearch/cluster/service/ClusterApplierServiceTests.java b/server/src/test/java/org/opensearch/cluster/service/ClusterApplierServiceTests.java index 166fb7a935009..a603dac9f42ca 100644 --- a/server/src/test/java/org/opensearch/cluster/service/ClusterApplierServiceTests.java +++ b/server/src/test/java/org/opensearch/cluster/service/ClusterApplierServiceTests.java @@ -39,6 +39,7 @@ import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.ClusterStateObserver; import org.opensearch.cluster.LocalNodeClusterManagerListener; +import org.opensearch.cluster.LocalNodeMasterListener; import org.opensearch.cluster.block.ClusterBlocks; import org.opensearch.cluster.coordination.NoClusterManagerBlockService; import org.opensearch.cluster.metadata.Metadata; @@ -331,6 +332,43 @@ public void offMaster() { timedClusterApplierService.close(); } + /* Validate the backwards compatibility of LocalNodeMasterListener remains + * after making it a subclass of LocalNodeClusterManagerListener. + * Overriding the methods with non-inclusive words are intentional. + * To support inclusive language, LocalNodeMasterListener is deprecated in 2.2. + */ + public void testDeprecatedLocalNodeMasterListenerCallbacks() { + TimedClusterApplierService timedClusterApplierService = createTimedClusterService(false); + + AtomicBoolean isClusterManager = new AtomicBoolean(); + timedClusterApplierService.addLocalNodeMasterListener(new LocalNodeMasterListener() { + @Override + public void onMaster() { + isClusterManager.set(true); + } + + @Override + public void offMaster() { + isClusterManager.set(false); + } + }); + + ClusterState state = timedClusterApplierService.state(); + DiscoveryNodes nodes = state.nodes(); + DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(nodes.getLocalNodeId()); + state = ClusterState.builder(state).nodes(nodesBuilder).build(); + setState(timedClusterApplierService, state); + assertThat(isClusterManager.get(), is(true)); + + nodes = state.nodes(); + nodesBuilder = DiscoveryNodes.builder(nodes).masterNodeId(null); + state = ClusterState.builder(state).nodes(nodesBuilder).build(); + setState(timedClusterApplierService, state); + assertThat(isClusterManager.get(), is(false)); + + timedClusterApplierService.close(); + } + public void testClusterStateApplierCantSampleClusterState() throws InterruptedException { AtomicReference error = new AtomicReference<>(); AtomicBoolean applierCalled = new AtomicBoolean(); From 10925bc7b1278a36e4a8362e1c6abd7f82d5f90d Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 15:26:20 -0700 Subject: [PATCH 06/15] Add unit test to use the deprecated class MasterService to call the deprecated master setting Signed-off-by: Tianli Feng --- .../RenamedTimeoutRequestParameterTests.java | 8 +++++ ...sterManagerServiceRenamedSettingTests.java | 29 +++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java b/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java index 976ce5cfee63e..b1ebc02842d25 100644 --- a/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java +++ b/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java @@ -67,6 +67,7 @@ import org.opensearch.rest.action.admin.cluster.RestGetStoredScriptAction; import org.opensearch.rest.action.admin.cluster.RestPutStoredScriptAction; import org.opensearch.rest.action.cat.RestAllocationAction; +import org.opensearch.rest.action.cat.RestMasterAction; import org.opensearch.rest.action.cat.RestRepositoriesAction; import org.opensearch.rest.action.cat.RestThreadPoolAction; import org.opensearch.rest.action.cat.RestClusterManagerAction; @@ -167,6 +168,13 @@ public void testCatClusterManager() { assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); } + public void testCatMaster() { + RestMasterAction action = new RestMasterAction(); + Exception e = assertThrows(OpenSearchParseException.class, () -> action.doCatRequest(getRestRequestWithBothParams(), client)); + assertThat(e.getMessage(), containsString(DUPLICATE_PARAMETER_ERROR_MESSAGE)); + assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); + } + public void testCatNodeattrs() { RestNodeAttrsAction action = new RestNodeAttrsAction(); Exception e = assertThrows(OpenSearchParseException.class, () -> action.doCatRequest(getRestRequestWithBothParams(), client)); diff --git a/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java b/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java index e71d872c87527..d86964ab9c82b 100644 --- a/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java +++ b/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java @@ -40,12 +40,29 @@ public void testClusterManagerServiceSettingsExist() { ); } + /** + * Validate the both settings are known and supported, + * and validate the both settings can be called from the deprecated MasterService class. + */ + public void testMasterServiceSettingsExist() { + Set> settings = ClusterSettings.BUILT_IN_CLUSTER_SETTINGS; + assertTrue( + "Both 'cluster.service.slow_cluster_manager_task_logging_threshold' and its predecessor should be supported built-in settings", + settings.containsAll( + Arrays.asList( + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); + } + /** * Validate the default value of the both settings is the same. */ public void testSettingFallback() { assertEquals( - ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY), + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY), ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY) ); } @@ -60,8 +77,8 @@ public void testSettingGetValue() { ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); assertEquals( - ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.getDefault(Settings.EMPTY), - ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.getDefault(Settings.EMPTY), + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); } @@ -76,7 +93,7 @@ public void testSettingGetValueWithFallback() { ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); - assertSettingDeprecationsAndWarnings(new Setting[] { ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING }); + assertSettingDeprecationsAndWarnings(new Setting[] { MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING }); } /** @@ -92,8 +109,8 @@ public void testSettingGetValueWhenBothAreConfigured() { ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); - assertEquals(TimeValue.timeValueSeconds(8), ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings)); - assertSettingDeprecationsAndWarnings(new Setting[] { ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING }); + assertEquals(TimeValue.timeValueSeconds(8), MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings)); + assertSettingDeprecationsAndWarnings(new Setting[] { MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING }); } } From c186bec9ad29fdf7180b22461478a1305d5aa856 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 17:12:31 -0700 Subject: [PATCH 07/15] Change return value of public method from ClusterManagerService to MasterService to keep backwards compatibility Signed-off-by: Tianli Feng --- .../org/opensearch/cluster/service/ClusterService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index 7fd8ed62def07..8c62b009b8e51 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -62,7 +62,7 @@ * @opensearch.internal */ public class ClusterService extends AbstractLifecycleComponent { - private final ClusterManagerService clusterManagerService; + private final MasterService clusterManagerService; private final ClusterApplierService clusterApplierService; @@ -92,7 +92,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread this( settings, clusterSettings, - new ClusterManagerService(settings, clusterSettings, threadPool), + new MasterService(settings, clusterSettings, threadPool), new ClusterApplierService(Node.NODE_NAME_SETTING.get(settings), settings, clusterSettings, threadPool) ); } @@ -100,7 +100,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread public ClusterService( Settings settings, ClusterSettings clusterSettings, - ClusterManagerService clusterManagerService, + MasterService clusterManagerService, ClusterApplierService clusterApplierService ) { this.settings = settings; @@ -218,7 +218,7 @@ public void addLocalNodeMasterListener(LocalNodeClusterManagerListener listener) clusterApplierService.addLocalNodeMasterListener(listener); } - public ClusterManagerService getMasterService() { + public MasterService getMasterService() { return clusterManagerService; } From d10fec200033ee2260443f01ff9d7df5d9b06aff Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 17:44:26 -0700 Subject: [PATCH 08/15] Call the static method of ClusterManagerNodeChangePredicate in MasterNodeChangePredicate instead of using inheritance Signed-off-by: Tianli Feng --- .../cluster/ClusterManagerNodeChangePredicate.java | 8 ++------ .../org/opensearch/cluster/MasterNodeChangePredicate.java | 7 ++++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java b/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java index a28b1690dabac..ecb99e06f3ef0 100644 --- a/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java +++ b/server/src/main/java/org/opensearch/cluster/ClusterManagerNodeChangePredicate.java @@ -41,13 +41,9 @@ * * @opensearch.internal */ -// TODO: add final keyword to the class and private keyword to the default constructor, -// after removing the deprecated class MasterNodeChangePredicate. -// Removed the final keyword temporarily only for making the class MasterNodeChangePredicate as a subclass, -// so that preserving the both class names by maintaining one class implementation for backwards compatibility. -public class ClusterManagerNodeChangePredicate { +public final class ClusterManagerNodeChangePredicate { - ClusterManagerNodeChangePredicate() { + private ClusterManagerNodeChangePredicate() { } diff --git a/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java b/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java index 3c86d9bbedc03..d06aa219e3ca6 100644 --- a/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java +++ b/server/src/main/java/org/opensearch/cluster/MasterNodeChangePredicate.java @@ -32,6 +32,8 @@ package org.opensearch.cluster; +import java.util.function.Predicate; + /** * Utility class to build a predicate that accepts cluster state changes * @@ -39,10 +41,13 @@ * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link ClusterManagerNodeChangePredicate} */ @Deprecated -public final class MasterNodeChangePredicate extends ClusterManagerNodeChangePredicate { +public final class MasterNodeChangePredicate { private MasterNodeChangePredicate() { } + public static Predicate build(ClusterState currentState) { + return ClusterManagerNodeChangePredicate.build(currentState); + } } From 951af92cb316bf0d9f1b56338fd0bec90d029375 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 17:54:06 -0700 Subject: [PATCH 09/15] Revert - Change return value of public method from ClusterManagerService to MasterService Signed-off-by: Tianli Feng --- .../org/opensearch/cluster/service/ClusterService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index 8c62b009b8e51..7fd8ed62def07 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -62,7 +62,7 @@ * @opensearch.internal */ public class ClusterService extends AbstractLifecycleComponent { - private final MasterService clusterManagerService; + private final ClusterManagerService clusterManagerService; private final ClusterApplierService clusterApplierService; @@ -92,7 +92,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread this( settings, clusterSettings, - new MasterService(settings, clusterSettings, threadPool), + new ClusterManagerService(settings, clusterSettings, threadPool), new ClusterApplierService(Node.NODE_NAME_SETTING.get(settings), settings, clusterSettings, threadPool) ); } @@ -100,7 +100,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread public ClusterService( Settings settings, ClusterSettings clusterSettings, - MasterService clusterManagerService, + ClusterManagerService clusterManagerService, ClusterApplierService clusterApplierService ) { this.settings = settings; @@ -218,7 +218,7 @@ public void addLocalNodeMasterListener(LocalNodeClusterManagerListener listener) clusterApplierService.addLocalNodeMasterListener(listener); } - public MasterService getMasterService() { + public ClusterManagerService getMasterService() { return clusterManagerService; } From 694d876f35424c2b059710035cae6e7fb93d035c Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 21:09:02 -0700 Subject: [PATCH 10/15] Restore method getMasterService() to return MasterService Signed-off-by: Tianli Feng --- .../cluster/service/ClusterService.java | 30 ++++++++++++++++++- .../cluster/service/ClusterServiceTests.java | 29 ++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index 7fd8ed62def07..e276c66c47cab 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -55,6 +55,7 @@ import java.util.Collections; import java.util.Map; +import java.util.Objects; /** * Main Cluster Service @@ -63,6 +64,8 @@ */ public class ClusterService extends AbstractLifecycleComponent { private final ClusterManagerService clusterManagerService; + @Deprecated + private MasterService masterService = null; private final ClusterApplierService clusterApplierService; @@ -114,6 +117,20 @@ public ClusterService( this.clusterApplierService = clusterApplierService; } + /** + * @deprecated As of 2.2, because supporting inclusive language. + */ + @Deprecated + public ClusterService( + Settings settings, + ClusterSettings clusterSettings, + MasterService masterService, + ClusterApplierService clusterApplierService + ) { + this(settings, clusterSettings, (ClusterManagerService) masterService, clusterApplierService); + this.masterService = masterService; + } + public synchronized void setNodeConnectionsService(NodeConnectionsService nodeConnectionsService) { clusterApplierService.setNodeConnectionsService(nodeConnectionsService); } @@ -218,10 +235,21 @@ public void addLocalNodeMasterListener(LocalNodeClusterManagerListener listener) clusterApplierService.addLocalNodeMasterListener(listener); } - public ClusterManagerService getMasterService() { + public ClusterManagerService getClusterManagerService() { return clusterManagerService; } + /** + * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link #getClusterManagerService()} + */ + @Deprecated + public MasterService getMasterService() { + return Objects.requireNonNullElseGet( + masterService, + () -> new MasterService(settings, clusterSettings, clusterManagerService.threadPool) + ); + } + /** * Getter and Setter for IndexingPressureService, This method exposes IndexingPressureService stats to other plugins for usage. * Although Indexing Pressure instances can be accessed via Node and NodeService class but none of them are diff --git a/server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java b/server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java new file mode 100644 index 0000000000000..a28b85338645a --- /dev/null +++ b/server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java @@ -0,0 +1,29 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.service; + +import org.hamcrest.Matchers; +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Settings; +import org.opensearch.test.OpenSearchTestCase; + +public class ClusterServiceTests extends OpenSearchTestCase { + public void testDeprecatedGetMasterServiceWhenUsingMasterServiceToInitializeClusterService() { + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + MasterService masterService = new MasterService(Settings.EMPTY, clusterSettings, null); + ClusterService clusterServiceWithMasterService = new ClusterService(Settings.EMPTY, clusterSettings, masterService, null); + assertThat(clusterServiceWithMasterService.getMasterService(), Matchers.equalTo(masterService)); + } + + public void testDeprecatedGetMasterServiceWithoutAssigningMasterService() { + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, null); + assertThat(clusterService.getMasterService(), Matchers.instanceOf(MasterService.class)); + } +} From 1fdd17556e065458a9e07c770019b2a18aa222da Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Wed, 13 Jul 2022 22:50:49 -0700 Subject: [PATCH 11/15] Revert "Restore method getMasterService() to return MasterService" This reverts commit 694d876f35424c2b059710035cae6e7fb93d035c. Signed-off-by: Tianli Feng --- .../cluster/service/ClusterService.java | 30 +------------------ .../cluster/service/ClusterServiceTests.java | 29 ------------------ 2 files changed, 1 insertion(+), 58 deletions(-) delete mode 100644 server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index e276c66c47cab..7fd8ed62def07 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -55,7 +55,6 @@ import java.util.Collections; import java.util.Map; -import java.util.Objects; /** * Main Cluster Service @@ -64,8 +63,6 @@ */ public class ClusterService extends AbstractLifecycleComponent { private final ClusterManagerService clusterManagerService; - @Deprecated - private MasterService masterService = null; private final ClusterApplierService clusterApplierService; @@ -117,20 +114,6 @@ public ClusterService( this.clusterApplierService = clusterApplierService; } - /** - * @deprecated As of 2.2, because supporting inclusive language. - */ - @Deprecated - public ClusterService( - Settings settings, - ClusterSettings clusterSettings, - MasterService masterService, - ClusterApplierService clusterApplierService - ) { - this(settings, clusterSettings, (ClusterManagerService) masterService, clusterApplierService); - this.masterService = masterService; - } - public synchronized void setNodeConnectionsService(NodeConnectionsService nodeConnectionsService) { clusterApplierService.setNodeConnectionsService(nodeConnectionsService); } @@ -235,21 +218,10 @@ public void addLocalNodeMasterListener(LocalNodeClusterManagerListener listener) clusterApplierService.addLocalNodeMasterListener(listener); } - public ClusterManagerService getClusterManagerService() { + public ClusterManagerService getMasterService() { return clusterManagerService; } - /** - * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link #getClusterManagerService()} - */ - @Deprecated - public MasterService getMasterService() { - return Objects.requireNonNullElseGet( - masterService, - () -> new MasterService(settings, clusterSettings, clusterManagerService.threadPool) - ); - } - /** * Getter and Setter for IndexingPressureService, This method exposes IndexingPressureService stats to other plugins for usage. * Although Indexing Pressure instances can be accessed via Node and NodeService class but none of them are diff --git a/server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java b/server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java deleted file mode 100644 index a28b85338645a..0000000000000 --- a/server/src/test/java/org/opensearch/cluster/service/ClusterServiceTests.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -package org.opensearch.cluster.service; - -import org.hamcrest.Matchers; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Settings; -import org.opensearch.test.OpenSearchTestCase; - -public class ClusterServiceTests extends OpenSearchTestCase { - public void testDeprecatedGetMasterServiceWhenUsingMasterServiceToInitializeClusterService() { - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - MasterService masterService = new MasterService(Settings.EMPTY, clusterSettings, null); - ClusterService clusterServiceWithMasterService = new ClusterService(Settings.EMPTY, clusterSettings, masterService, null); - assertThat(clusterServiceWithMasterService.getMasterService(), Matchers.equalTo(masterService)); - } - - public void testDeprecatedGetMasterServiceWithoutAssigningMasterService() { - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, null); - assertThat(clusterService.getMasterService(), Matchers.instanceOf(MasterService.class)); - } -} From 50a322bab287198429d66ff26004f9ac0dbe6615 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Thu, 14 Jul 2022 13:52:45 -0700 Subject: [PATCH 12/15] Revert renaming class MasterService and FakeThreadPoolMasterService Signed-off-by: Tianli Feng --- .../cluster/ClusterStateTaskListener.java | 4 +- .../cluster/coordination/Coordinator.java | 6 +- .../cluster/coordination/JoinHelper.java | 6 +- .../service/ClusterManagerService.java | 940 ------------------ .../cluster/service/ClusterService.java | 10 +- .../cluster/service/MasterService.java | 896 ++++++++++++++++- .../common/settings/ClusterSettings.java | 6 +- .../common/util/concurrent/BaseFuture.java | 4 +- .../opensearch/discovery/DiscoveryModule.java | 4 +- ...rnalClusterInfoServiceSchedulingTests.java | 6 +- .../cluster/coordination/NodeJoinTests.java | 22 +- ... => MasterServiceRenamedSettingTests.java} | 14 +- ...viceTests.java => MasterServiceTests.java} | 90 +- .../discovery/DiscoveryModuleTests.java | 6 +- .../snapshots/SnapshotResiliencyTests.java | 8 +- .../AbstractCoordinatorTestCase.java | 10 +- .../FakeThreadPoolClusterManagerService.java | 213 ---- .../service/FakeThreadPoolMasterService.java | 172 +++- .../opensearch/test/ClusterServiceUtils.java | 10 +- ... => FakeThreadPoolMasterServiceTests.java} | 4 +- 20 files changed, 1162 insertions(+), 1269 deletions(-) delete mode 100644 server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java rename server/src/test/java/org/opensearch/cluster/service/{ClusterManagerServiceRenamedSettingTests.java => MasterServiceRenamedSettingTests.java} (85%) rename server/src/test/java/org/opensearch/cluster/service/{ClusterManagerServiceTests.java => MasterServiceTests.java} (92%) delete mode 100644 test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerService.java rename test/framework/src/test/java/org/opensearch/cluster/service/{FakeThreadPoolClusterManagerServiceTests.java => FakeThreadPoolMasterServiceTests.java} (97%) diff --git a/server/src/main/java/org/opensearch/cluster/ClusterStateTaskListener.java b/server/src/main/java/org/opensearch/cluster/ClusterStateTaskListener.java index 6a12e0cd5ed46..74cc118892579 100644 --- a/server/src/main/java/org/opensearch/cluster/ClusterStateTaskListener.java +++ b/server/src/main/java/org/opensearch/cluster/ClusterStateTaskListener.java @@ -31,7 +31,7 @@ package org.opensearch.cluster; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import java.util.List; @@ -49,7 +49,7 @@ public interface ClusterStateTaskListener { /** * called when the task was rejected because the local node is no longer cluster-manager. - * Used only for tasks submitted to {@link ClusterManagerService}. + * Used only for tasks submitted to {@link MasterService}. */ default void onNoLongerMaster(String source) { onFailure(source, new NotClusterManagerException("no longer cluster-manager. source: [" + source + "]")); diff --git a/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java b/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java index ee71307e82ec7..611261320a453 100644 --- a/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java +++ b/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java @@ -58,7 +58,7 @@ import org.opensearch.cluster.routing.allocation.AllocationService; import org.opensearch.cluster.service.ClusterApplier; import org.opensearch.cluster.service.ClusterApplier.ClusterApplyListener; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.Booleans; import org.opensearch.common.Nullable; import org.opensearch.common.Priority; @@ -141,7 +141,7 @@ public class Coordinator extends AbstractLifecycleComponent implements Discovery private final boolean singleNodeDiscovery; private final ElectionStrategy electionStrategy; private final TransportService transportService; - private final ClusterManagerService clusterManagerService; + private final MasterService clusterManagerService; private final AllocationService allocationService; private final JoinHelper joinHelper; private final NodeRemovalClusterStateTaskExecutor nodeRemovalExecutor; @@ -191,7 +191,7 @@ public Coordinator( TransportService transportService, NamedWriteableRegistry namedWriteableRegistry, AllocationService allocationService, - ClusterManagerService clusterManagerService, + MasterService clusterManagerService, Supplier persistedStateSupplier, SeedHostsProvider seedHostsProvider, ClusterApplier clusterApplier, diff --git a/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java b/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java index b2d1c6d14ebbb..18a9579547d56 100644 --- a/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java +++ b/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java @@ -46,7 +46,7 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.routing.RerouteService; import org.opensearch.cluster.routing.allocation.AllocationService; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.Priority; import org.opensearch.common.collect.Tuple; import org.opensearch.common.io.stream.StreamInput; @@ -106,7 +106,7 @@ public class JoinHelper { Setting.Property.Deprecated ); - private final ClusterManagerService clusterManagerService; + private final MasterService clusterManagerService; private final TransportService transportService; private volatile JoinTaskExecutor joinTaskExecutor; @@ -122,7 +122,7 @@ public class JoinHelper { JoinHelper( Settings settings, AllocationService allocationService, - ClusterManagerService clusterManagerService, + MasterService clusterManagerService, TransportService transportService, LongSupplier currentTermSupplier, Supplier currentStateSupplier, diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java deleted file mode 100644 index df691c1443a56..0000000000000 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java +++ /dev/null @@ -1,940 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.cluster.service; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.message.ParameterizedMessage; -import org.opensearch.Assertions; -import org.opensearch.action.support.PlainActionFuture; -import org.opensearch.cluster.AckedClusterStateTaskListener; -import org.opensearch.cluster.ClusterChangedEvent; -import org.opensearch.cluster.ClusterState; -import org.opensearch.cluster.ClusterState.Builder; -import org.opensearch.cluster.ClusterStateTaskConfig; -import org.opensearch.cluster.ClusterStateTaskExecutor; -import org.opensearch.cluster.ClusterStateTaskExecutor.ClusterTasksResult; -import org.opensearch.cluster.ClusterStateTaskListener; -import org.opensearch.cluster.coordination.ClusterStatePublisher; -import org.opensearch.cluster.coordination.FailedToCommitClusterStateException; -import org.opensearch.cluster.metadata.Metadata; -import org.opensearch.cluster.metadata.ProcessClusterEventTimeoutException; -import org.opensearch.cluster.node.DiscoveryNode; -import org.opensearch.cluster.node.DiscoveryNodes; -import org.opensearch.cluster.routing.RoutingTable; -import org.opensearch.common.Nullable; -import org.opensearch.common.Priority; -import org.opensearch.common.component.AbstractLifecycleComponent; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Setting; -import org.opensearch.common.settings.Settings; -import org.opensearch.common.text.Text; -import org.opensearch.common.unit.TimeValue; -import org.opensearch.common.util.concurrent.CountDown; -import org.opensearch.common.util.concurrent.OpenSearchExecutors; -import org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException; -import org.opensearch.common.util.concurrent.FutureUtils; -import org.opensearch.common.util.concurrent.PrioritizedOpenSearchThreadPoolExecutor; -import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.discovery.Discovery; -import org.opensearch.node.Node; -import org.opensearch.threadpool.Scheduler; -import org.opensearch.threadpool.ThreadPool; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.TimeUnit; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -import static org.opensearch.common.util.concurrent.OpenSearchExecutors.daemonThreadFactory; - -/** - * Main Master Node Service - * - * @opensearch.internal - */ -public class ClusterManagerService extends AbstractLifecycleComponent { - private static final Logger logger = LogManager.getLogger(ClusterManagerService.class); - - public static final Setting MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING = Setting.positiveTimeSetting( - "cluster.service.slow_master_task_logging_threshold", - TimeValue.timeValueSeconds(10), - Setting.Property.Dynamic, - Setting.Property.NodeScope, - Setting.Property.Deprecated - ); - // The setting below is going to replace the above. - // To keep backwards compatibility, the old usage is remained, and it's also used as the fallback for the new usage. - public static final Setting CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING = Setting.positiveTimeSetting( - "cluster.service.slow_cluster_manager_task_logging_threshold", - MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - Setting.Property.Dynamic, - Setting.Property.NodeScope - ); - - static final String MASTER_UPDATE_THREAD_NAME = "masterService#updateTask"; - - ClusterStatePublisher clusterStatePublisher; - - private final String nodeName; - - private java.util.function.Supplier clusterStateSupplier; - - private volatile TimeValue slowTaskLoggingThreshold; - - protected final ThreadPool threadPool; - - private volatile PrioritizedOpenSearchThreadPoolExecutor threadPoolExecutor; - private volatile Batcher taskBatcher; - - public ClusterManagerService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { - this.nodeName = Objects.requireNonNull(Node.NODE_NAME_SETTING.get(settings)); - - this.slowTaskLoggingThreshold = CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings); - clusterSettings.addSettingsUpdateConsumer( - CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - this::setSlowTaskLoggingThreshold - ); - - this.threadPool = threadPool; - } - - private void setSlowTaskLoggingThreshold(TimeValue slowTaskLoggingThreshold) { - this.slowTaskLoggingThreshold = slowTaskLoggingThreshold; - } - - public synchronized void setClusterStatePublisher(ClusterStatePublisher publisher) { - clusterStatePublisher = publisher; - } - - public synchronized void setClusterStateSupplier(java.util.function.Supplier clusterStateSupplier) { - this.clusterStateSupplier = clusterStateSupplier; - } - - @Override - protected synchronized void doStart() { - Objects.requireNonNull(clusterStatePublisher, "please set a cluster state publisher before starting"); - Objects.requireNonNull(clusterStateSupplier, "please set a cluster state supplier before starting"); - threadPoolExecutor = createThreadPoolExecutor(); - taskBatcher = new Batcher(logger, threadPoolExecutor); - } - - protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { - return OpenSearchExecutors.newSinglePrioritizing( - nodeName + "/" + MASTER_UPDATE_THREAD_NAME, - daemonThreadFactory(nodeName, MASTER_UPDATE_THREAD_NAME), - threadPool.getThreadContext(), - threadPool.scheduler() - ); - } - - @SuppressWarnings("unchecked") - class Batcher extends TaskBatcher { - - Batcher(Logger logger, PrioritizedOpenSearchThreadPoolExecutor threadExecutor) { - super(logger, threadExecutor); - } - - @Override - protected void onTimeout(List tasks, TimeValue timeout) { - threadPool.generic() - .execute( - () -> tasks.forEach( - task -> ((UpdateTask) task).listener.onFailure( - task.source, - new ProcessClusterEventTimeoutException(timeout, task.source) - ) - ) - ); - } - - @Override - protected void run(Object batchingKey, List tasks, String tasksSummary) { - ClusterStateTaskExecutor taskExecutor = (ClusterStateTaskExecutor) batchingKey; - List updateTasks = (List) tasks; - runTasks(new TaskInputs(taskExecutor, updateTasks, tasksSummary)); - } - - class UpdateTask extends BatchedTask { - final ClusterStateTaskListener listener; - - UpdateTask( - Priority priority, - String source, - Object task, - ClusterStateTaskListener listener, - ClusterStateTaskExecutor executor - ) { - super(priority, source, executor, task); - this.listener = listener; - } - - @Override - public String describeTasks(List tasks) { - return ((ClusterStateTaskExecutor) batchingKey).describeTasks( - tasks.stream().map(BatchedTask::getTask).collect(Collectors.toList()) - ); - } - } - } - - @Override - protected synchronized void doStop() { - ThreadPool.terminate(threadPoolExecutor, 10, TimeUnit.SECONDS); - } - - @Override - protected synchronized void doClose() {} - - /** - * The current cluster state exposed by the discovery layer. Package-visible for tests. - */ - ClusterState state() { - return clusterStateSupplier.get(); - } - - private static boolean isMasterUpdateThread() { - return Thread.currentThread().getName().contains(MASTER_UPDATE_THREAD_NAME); - } - - public static boolean assertMasterUpdateThread() { - assert isMasterUpdateThread() : "not called from the cluster-manager service thread"; - return true; - } - - public static boolean assertNotMasterUpdateThread(String reason) { - assert isMasterUpdateThread() == false : "Expected current thread [" - + Thread.currentThread() - + "] to not be the cluster-maanger service thread. Reason: [" - + reason - + "]"; - return true; - } - - private void runTasks(TaskInputs taskInputs) { - final String summary = taskInputs.summary; - if (!lifecycle.started()) { - logger.debug("processing [{}]: ignoring, cluster-manager service not started", summary); - return; - } - - logger.debug("executing cluster state update for [{}]", summary); - final ClusterState previousClusterState = state(); - - if (!previousClusterState.nodes().isLocalNodeElectedMaster() && taskInputs.runOnlyWhenClusterManager()) { - logger.debug("failing [{}]: local node is no longer cluster-manager", summary); - taskInputs.onNoLongerClusterManager(); - return; - } - - final long computationStartTime = threadPool.relativeTimeInMillis(); - final TaskOutputs taskOutputs = calculateTaskOutputs(taskInputs, previousClusterState); - taskOutputs.notifyFailedTasks(); - final TimeValue computationTime = getTimeSince(computationStartTime); - logExecutionTime(computationTime, "compute cluster state update", summary); - - if (taskOutputs.clusterStateUnchanged()) { - final long notificationStartTime = threadPool.relativeTimeInMillis(); - taskOutputs.notifySuccessfulTasksOnUnchangedClusterState(); - final TimeValue executionTime = getTimeSince(notificationStartTime); - logExecutionTime(executionTime, "notify listeners on unchanged cluster state", summary); - } else { - final ClusterState newClusterState = taskOutputs.newClusterState; - if (logger.isTraceEnabled()) { - logger.trace("cluster state updated, source [{}]\n{}", summary, newClusterState); - } else { - logger.debug("cluster state updated, version [{}], source [{}]", newClusterState.version(), summary); - } - final long publicationStartTime = threadPool.relativeTimeInMillis(); - try { - ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent(summary, newClusterState, previousClusterState); - // new cluster state, notify all listeners - final DiscoveryNodes.Delta nodesDelta = clusterChangedEvent.nodesDelta(); - if (nodesDelta.hasChanges() && logger.isInfoEnabled()) { - String nodesDeltaSummary = nodesDelta.shortSummary(); - if (nodesDeltaSummary.length() > 0) { - logger.info( - "{}, term: {}, version: {}, delta: {}", - summary, - newClusterState.term(), - newClusterState.version(), - nodesDeltaSummary - ); - } - } - - logger.debug("publishing cluster state version [{}]", newClusterState.version()); - publish(clusterChangedEvent, taskOutputs, publicationStartTime); - } catch (Exception e) { - handleException(summary, publicationStartTime, newClusterState, e); - } - } - } - - private TimeValue getTimeSince(long startTimeMillis) { - return TimeValue.timeValueMillis(Math.max(0, threadPool.relativeTimeInMillis() - startTimeMillis)); - } - - protected void publish(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeMillis) { - final PlainActionFuture fut = new PlainActionFuture() { - @Override - protected boolean blockingAllowed() { - return isMasterUpdateThread() || super.blockingAllowed(); - } - }; - clusterStatePublisher.publish(clusterChangedEvent, fut, taskOutputs.createAckListener(threadPool, clusterChangedEvent.state())); - - // indefinitely wait for publication to complete - try { - FutureUtils.get(fut); - onPublicationSuccess(clusterChangedEvent, taskOutputs); - } catch (Exception e) { - onPublicationFailed(clusterChangedEvent, taskOutputs, startTimeMillis, e); - } - } - - void onPublicationSuccess(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs) { - final long notificationStartTime = threadPool.relativeTimeInMillis(); - taskOutputs.processedDifferentClusterState(clusterChangedEvent.previousState(), clusterChangedEvent.state()); - - try { - taskOutputs.clusterStatePublished(clusterChangedEvent); - } catch (Exception e) { - logger.error( - () -> new ParameterizedMessage( - "exception thrown while notifying executor of new cluster state publication [{}]", - clusterChangedEvent.source() - ), - e - ); - } - final TimeValue executionTime = getTimeSince(notificationStartTime); - logExecutionTime( - executionTime, - "notify listeners on successful publication of cluster state (version: " - + clusterChangedEvent.state().version() - + ", uuid: " - + clusterChangedEvent.state().stateUUID() - + ')', - clusterChangedEvent.source() - ); - } - - void onPublicationFailed(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeMillis, Exception exception) { - if (exception instanceof FailedToCommitClusterStateException) { - final long version = clusterChangedEvent.state().version(); - logger.warn( - () -> new ParameterizedMessage( - "failing [{}]: failed to commit cluster state version [{}]", - clusterChangedEvent.source(), - version - ), - exception - ); - taskOutputs.publishingFailed((FailedToCommitClusterStateException) exception); - } else { - handleException(clusterChangedEvent.source(), startTimeMillis, clusterChangedEvent.state(), exception); - } - } - - private void handleException(String summary, long startTimeMillis, ClusterState newClusterState, Exception e) { - final TimeValue executionTime = getTimeSince(startTimeMillis); - final long version = newClusterState.version(); - final String stateUUID = newClusterState.stateUUID(); - final String fullState = newClusterState.toString(); - logger.warn( - new ParameterizedMessage( - "took [{}] and then failed to publish updated cluster state (version: {}, uuid: {}) for [{}]:\n{}", - executionTime, - version, - stateUUID, - summary, - fullState - ), - e - ); - // TODO: do we want to call updateTask.onFailure here? - } - - private TaskOutputs calculateTaskOutputs(TaskInputs taskInputs, ClusterState previousClusterState) { - ClusterTasksResult clusterTasksResult = executeTasks(taskInputs, previousClusterState); - ClusterState newClusterState = patchVersions(previousClusterState, clusterTasksResult); - return new TaskOutputs( - taskInputs, - previousClusterState, - newClusterState, - getNonFailedTasks(taskInputs, clusterTasksResult), - clusterTasksResult.executionResults - ); - } - - private ClusterState patchVersions(ClusterState previousClusterState, ClusterTasksResult executionResult) { - ClusterState newClusterState = executionResult.resultingState; - - if (previousClusterState != newClusterState) { - // only the cluster-manager controls the version numbers - Builder builder = incrementVersion(newClusterState); - if (previousClusterState.routingTable() != newClusterState.routingTable()) { - builder.routingTable( - RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1).build() - ); - } - if (previousClusterState.metadata() != newClusterState.metadata()) { - builder.metadata(Metadata.builder(newClusterState.metadata()).version(newClusterState.metadata().version() + 1)); - } - - newClusterState = builder.build(); - } - - return newClusterState; - } - - public Builder incrementVersion(ClusterState clusterState) { - return ClusterState.builder(clusterState).incrementVersion(); - } - - /** - * Submits a cluster state update task; unlike {@link #submitStateUpdateTask(String, Object, ClusterStateTaskConfig, - * ClusterStateTaskExecutor, ClusterStateTaskListener)}, submitted updates will not be batched. - * - * @param source the source of the cluster state update task - * @param updateTask the full context for the cluster state update - * task - * - */ - public & ClusterStateTaskListener> void submitStateUpdateTask( - String source, - T updateTask - ) { - submitStateUpdateTask(source, updateTask, updateTask, updateTask, updateTask); - } - - /** - * Submits a cluster state update task; submitted updates will be - * batched across the same instance of executor. The exact batching - * semantics depend on the underlying implementation but a rough - * guideline is that if the update task is submitted while there - * are pending update tasks for the same executor, these update - * tasks will all be executed on the executor in a single batch - * - * @param source the source of the cluster state update task - * @param task the state needed for the cluster state update task - * @param config the cluster state update task configuration - * @param executor the cluster state update task executor; tasks - * that share the same executor will be executed - * batches on this executor - * @param listener callback after the cluster state update task - * completes - * @param the type of the cluster state update task state - * - */ - public void submitStateUpdateTask( - String source, - T task, - ClusterStateTaskConfig config, - ClusterStateTaskExecutor executor, - ClusterStateTaskListener listener - ) { - submitStateUpdateTasks(source, Collections.singletonMap(task, listener), config, executor); - } - - /** - * Output created by executing a set of tasks provided as TaskInputs - */ - class TaskOutputs { - final TaskInputs taskInputs; - final ClusterState previousClusterState; - final ClusterState newClusterState; - final List nonFailedTasks; - final Map executionResults; - - TaskOutputs( - TaskInputs taskInputs, - ClusterState previousClusterState, - ClusterState newClusterState, - List nonFailedTasks, - Map executionResults - ) { - this.taskInputs = taskInputs; - this.previousClusterState = previousClusterState; - this.newClusterState = newClusterState; - this.nonFailedTasks = nonFailedTasks; - this.executionResults = executionResults; - } - - void publishingFailed(FailedToCommitClusterStateException t) { - nonFailedTasks.forEach(task -> task.listener.onFailure(task.source(), t)); - } - - void processedDifferentClusterState(ClusterState previousClusterState, ClusterState newClusterState) { - nonFailedTasks.forEach(task -> task.listener.clusterStateProcessed(task.source(), previousClusterState, newClusterState)); - } - - void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) { - taskInputs.executor.clusterStatePublished(clusterChangedEvent); - } - - Discovery.AckListener createAckListener(ThreadPool threadPool, ClusterState newClusterState) { - return new DelegatingAckListener( - nonFailedTasks.stream() - .filter(task -> task.listener instanceof AckedClusterStateTaskListener) - .map( - task -> new AckCountDownListener( - (AckedClusterStateTaskListener) task.listener, - newClusterState.version(), - newClusterState.nodes(), - threadPool - ) - ) - .collect(Collectors.toList()) - ); - } - - boolean clusterStateUnchanged() { - return previousClusterState == newClusterState; - } - - void notifyFailedTasks() { - // fail all tasks that have failed - for (Batcher.UpdateTask updateTask : taskInputs.updateTasks) { - assert executionResults.containsKey(updateTask.task) : "missing " + updateTask; - final ClusterStateTaskExecutor.TaskResult taskResult = executionResults.get(updateTask.task); - if (taskResult.isSuccess() == false) { - updateTask.listener.onFailure(updateTask.source(), taskResult.getFailure()); - } - } - } - - void notifySuccessfulTasksOnUnchangedClusterState() { - nonFailedTasks.forEach(task -> { - if (task.listener instanceof AckedClusterStateTaskListener) { - // no need to wait for ack if nothing changed, the update can be counted as acknowledged - ((AckedClusterStateTaskListener) task.listener).onAllNodesAcked(null); - } - task.listener.clusterStateProcessed(task.source(), newClusterState, newClusterState); - }); - } - } - - /** - * Returns the tasks that are pending. - */ - public List pendingTasks() { - return Arrays.stream(threadPoolExecutor.getPending()).map(pending -> { - assert pending.task instanceof SourcePrioritizedRunnable - : "thread pool executor should only use SourcePrioritizedRunnable instances but found: " - + pending.task.getClass().getName(); - SourcePrioritizedRunnable task = (SourcePrioritizedRunnable) pending.task; - return new PendingClusterTask( - pending.insertionOrder, - pending.priority, - new Text(task.source()), - task.getAgeInMillis(), - pending.executing - ); - }).collect(Collectors.toList()); - } - - /** - * Returns the number of currently pending tasks. - */ - public int numberOfPendingTasks() { - return threadPoolExecutor.getNumberOfPendingTasks(); - } - - /** - * Returns the maximum wait time for tasks in the queue - * - * @return A zero time value if the queue is empty, otherwise the time value oldest task waiting in the queue - */ - public TimeValue getMaxTaskWaitTime() { - return threadPoolExecutor.getMaxTaskWaitTime(); - } - - private SafeClusterStateTaskListener safe(ClusterStateTaskListener listener, Supplier contextSupplier) { - if (listener instanceof AckedClusterStateTaskListener) { - return new SafeAckedClusterStateTaskListener((AckedClusterStateTaskListener) listener, contextSupplier, logger); - } else { - return new SafeClusterStateTaskListener(listener, contextSupplier, logger); - } - } - - private static class SafeClusterStateTaskListener implements ClusterStateTaskListener { - private final ClusterStateTaskListener listener; - protected final Supplier context; - private final Logger logger; - - SafeClusterStateTaskListener(ClusterStateTaskListener listener, Supplier context, Logger logger) { - this.listener = listener; - this.context = context; - this.logger = logger; - } - - @Override - public void onFailure(String source, Exception e) { - try (ThreadContext.StoredContext ignore = context.get()) { - listener.onFailure(source, e); - } catch (Exception inner) { - inner.addSuppressed(e); - logger.error(() -> new ParameterizedMessage("exception thrown by listener notifying of failure from [{}]", source), inner); - } - } - - @Override - public void onNoLongerMaster(String source) { - try (ThreadContext.StoredContext ignore = context.get()) { - listener.onNoLongerMaster(source); - } catch (Exception e) { - logger.error( - () -> new ParameterizedMessage( - "exception thrown by listener while notifying no longer cluster-manager from [{}]", - source - ), - e - ); - } - } - - @Override - public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { - try (ThreadContext.StoredContext ignore = context.get()) { - listener.clusterStateProcessed(source, oldState, newState); - } catch (Exception e) { - logger.error( - () -> new ParameterizedMessage( - "exception thrown by listener while notifying of cluster state processed from [{}], old cluster state:\n" - + "{}\nnew cluster state:\n{}", - source, - oldState, - newState - ), - e - ); - } - } - } - - private static class SafeAckedClusterStateTaskListener extends SafeClusterStateTaskListener implements AckedClusterStateTaskListener { - private final AckedClusterStateTaskListener listener; - private final Logger logger; - - SafeAckedClusterStateTaskListener( - AckedClusterStateTaskListener listener, - Supplier context, - Logger logger - ) { - super(listener, context, logger); - this.listener = listener; - this.logger = logger; - } - - @Override - public boolean mustAck(DiscoveryNode discoveryNode) { - return listener.mustAck(discoveryNode); - } - - @Override - public void onAllNodesAcked(@Nullable Exception e) { - try (ThreadContext.StoredContext ignore = context.get()) { - listener.onAllNodesAcked(e); - } catch (Exception inner) { - inner.addSuppressed(e); - logger.error("exception thrown by listener while notifying on all nodes acked", inner); - } - } - - @Override - public void onAckTimeout() { - try (ThreadContext.StoredContext ignore = context.get()) { - listener.onAckTimeout(); - } catch (Exception e) { - logger.error("exception thrown by listener while notifying on ack timeout", e); - } - } - - @Override - public TimeValue ackTimeout() { - return listener.ackTimeout(); - } - } - - private void logExecutionTime(TimeValue executionTime, String activity, String summary) { - if (executionTime.getMillis() > slowTaskLoggingThreshold.getMillis()) { - logger.warn("took [{}], which is over [{}], to {} for [{}]", executionTime, slowTaskLoggingThreshold, activity, summary); - } else { - logger.debug("took [{}] to {} for [{}]", executionTime, activity, summary); - } - } - - private static class DelegatingAckListener implements Discovery.AckListener { - - private final List listeners; - - private DelegatingAckListener(List listeners) { - this.listeners = listeners; - } - - @Override - public void onCommit(TimeValue commitTime) { - for (Discovery.AckListener listener : listeners) { - listener.onCommit(commitTime); - } - } - - @Override - public void onNodeAck(DiscoveryNode node, @Nullable Exception e) { - for (Discovery.AckListener listener : listeners) { - listener.onNodeAck(node, e); - } - } - } - - private static class AckCountDownListener implements Discovery.AckListener { - - private static final Logger logger = LogManager.getLogger(AckCountDownListener.class); - - private final AckedClusterStateTaskListener ackedTaskListener; - private final CountDown countDown; - private final DiscoveryNode clusterManagerNode; - private final ThreadPool threadPool; - private final long clusterStateVersion; - private volatile Scheduler.Cancellable ackTimeoutCallback; - private Exception lastFailure; - - AckCountDownListener( - AckedClusterStateTaskListener ackedTaskListener, - long clusterStateVersion, - DiscoveryNodes nodes, - ThreadPool threadPool - ) { - this.ackedTaskListener = ackedTaskListener; - this.clusterStateVersion = clusterStateVersion; - this.threadPool = threadPool; - this.clusterManagerNode = nodes.getMasterNode(); - int countDown = 0; - for (DiscoveryNode node : nodes) { - // we always wait for at least the cluster-manager node - if (node.equals(clusterManagerNode) || ackedTaskListener.mustAck(node)) { - countDown++; - } - } - logger.trace("expecting {} acknowledgements for cluster_state update (version: {})", countDown, clusterStateVersion); - this.countDown = new CountDown(countDown + 1); // we also wait for onCommit to be called - } - - @Override - public void onCommit(TimeValue commitTime) { - TimeValue ackTimeout = ackedTaskListener.ackTimeout(); - if (ackTimeout == null) { - ackTimeout = TimeValue.ZERO; - } - final TimeValue timeLeft = TimeValue.timeValueNanos(Math.max(0, ackTimeout.nanos() - commitTime.nanos())); - if (timeLeft.nanos() == 0L) { - onTimeout(); - } else if (countDown.countDown()) { - finish(); - } else { - this.ackTimeoutCallback = threadPool.schedule(this::onTimeout, timeLeft, ThreadPool.Names.GENERIC); - // re-check if onNodeAck has not completed while we were scheduling the timeout - if (countDown.isCountedDown()) { - ackTimeoutCallback.cancel(); - } - } - } - - @Override - public void onNodeAck(DiscoveryNode node, @Nullable Exception e) { - if (node.equals(clusterManagerNode) == false && ackedTaskListener.mustAck(node) == false) { - return; - } - if (e == null) { - logger.trace("ack received from node [{}], cluster_state update (version: {})", node, clusterStateVersion); - } else { - this.lastFailure = e; - logger.debug( - () -> new ParameterizedMessage( - "ack received from node [{}], cluster_state update (version: {})", - node, - clusterStateVersion - ), - e - ); - } - - if (countDown.countDown()) { - finish(); - } - } - - private void finish() { - logger.trace("all expected nodes acknowledged cluster_state update (version: {})", clusterStateVersion); - if (ackTimeoutCallback != null) { - ackTimeoutCallback.cancel(); - } - ackedTaskListener.onAllNodesAcked(lastFailure); - } - - public void onTimeout() { - if (countDown.fastForward()) { - logger.trace("timeout waiting for acknowledgement for cluster_state update (version: {})", clusterStateVersion); - ackedTaskListener.onAckTimeout(); - } - } - } - - private ClusterTasksResult executeTasks(TaskInputs taskInputs, ClusterState previousClusterState) { - ClusterTasksResult clusterTasksResult; - try { - List inputs = taskInputs.updateTasks.stream().map(tUpdateTask -> tUpdateTask.task).collect(Collectors.toList()); - clusterTasksResult = taskInputs.executor.execute(previousClusterState, inputs); - if (previousClusterState != clusterTasksResult.resultingState - && previousClusterState.nodes().isLocalNodeElectedMaster() - && (clusterTasksResult.resultingState.nodes().isLocalNodeElectedMaster() == false)) { - throw new AssertionError("update task submitted to ClusterManagerService cannot remove cluster-manager"); - } - } catch (Exception e) { - logger.trace( - () -> new ParameterizedMessage( - "failed to execute cluster state update (on version: [{}], uuid: [{}]) for [{}]\n{}{}{}", - previousClusterState.version(), - previousClusterState.stateUUID(), - taskInputs.summary, - previousClusterState.nodes(), - previousClusterState.routingTable(), - previousClusterState.getRoutingNodes() - ), // may be expensive => construct message lazily - e - ); - clusterTasksResult = ClusterTasksResult.builder() - .failures(taskInputs.updateTasks.stream().map(updateTask -> updateTask.task)::iterator, e) - .build(previousClusterState); - } - - assert clusterTasksResult.executionResults != null; - assert clusterTasksResult.executionResults.size() == taskInputs.updateTasks.size() : String.format( - Locale.ROOT, - "expected [%d] task result%s but was [%d]", - taskInputs.updateTasks.size(), - taskInputs.updateTasks.size() == 1 ? "" : "s", - clusterTasksResult.executionResults.size() - ); - if (Assertions.ENABLED) { - ClusterTasksResult finalClusterTasksResult = clusterTasksResult; - taskInputs.updateTasks.forEach( - updateTask -> { - assert finalClusterTasksResult.executionResults.containsKey(updateTask.task) : "missing task result for " + updateTask; - } - ); - } - - return clusterTasksResult; - } - - private List getNonFailedTasks(TaskInputs taskInputs, ClusterTasksResult clusterTasksResult) { - return taskInputs.updateTasks.stream().filter(updateTask -> { - assert clusterTasksResult.executionResults.containsKey(updateTask.task) : "missing " + updateTask; - final ClusterStateTaskExecutor.TaskResult taskResult = clusterTasksResult.executionResults.get(updateTask.task); - return taskResult.isSuccess(); - }).collect(Collectors.toList()); - } - - /** - * Represents a set of tasks to be processed together with their executor - */ - private class TaskInputs { - final String summary; - final List updateTasks; - final ClusterStateTaskExecutor executor; - - TaskInputs(ClusterStateTaskExecutor executor, List updateTasks, String summary) { - this.summary = summary; - this.executor = executor; - this.updateTasks = updateTasks; - } - - boolean runOnlyWhenClusterManager() { - return executor.runOnlyOnMaster(); - } - - void onNoLongerClusterManager() { - updateTasks.forEach(task -> task.listener.onNoLongerMaster(task.source())); - } - } - - /** - * Submits a batch of cluster state update tasks; submitted updates are guaranteed to be processed together, - * potentially with more tasks of the same executor. - * - * @param source the source of the cluster state update task - * @param tasks a map of update tasks and their corresponding listeners - * @param config the cluster state update task configuration - * @param executor the cluster state update task executor; tasks - * that share the same executor will be executed - * batches on this executor - * @param the type of the cluster state update task state - * - */ - public void submitStateUpdateTasks( - final String source, - final Map tasks, - final ClusterStateTaskConfig config, - final ClusterStateTaskExecutor executor - ) { - if (!lifecycle.started()) { - return; - } - final ThreadContext threadContext = threadPool.getThreadContext(); - final Supplier supplier = threadContext.newRestorableContext(true); - try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - threadContext.markAsSystemContext(); - - List safeTasks = tasks.entrySet() - .stream() - .map(e -> taskBatcher.new UpdateTask(config.priority(), source, e.getKey(), safe(e.getValue(), supplier), executor)) - .collect(Collectors.toList()); - taskBatcher.submitTasks(safeTasks, config.timeout()); - } catch (OpenSearchRejectedExecutionException e) { - // ignore cases where we are shutting down..., there is really nothing interesting - // to be done here... - if (!lifecycle.stoppedOrClosed()) { - throw e; - } - } - } - -} diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index 7fd8ed62def07..9dc907f7afadd 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -62,7 +62,7 @@ * @opensearch.internal */ public class ClusterService extends AbstractLifecycleComponent { - private final ClusterManagerService clusterManagerService; + private final MasterService clusterManagerService; private final ClusterApplierService clusterApplierService; @@ -92,7 +92,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread this( settings, clusterSettings, - new ClusterManagerService(settings, clusterSettings, threadPool), + new MasterService(settings, clusterSettings, threadPool), new ClusterApplierService(Node.NODE_NAME_SETTING.get(settings), settings, clusterSettings, threadPool) ); } @@ -100,7 +100,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread public ClusterService( Settings settings, ClusterSettings clusterSettings, - ClusterManagerService clusterManagerService, + MasterService clusterManagerService, ClusterApplierService clusterApplierService ) { this.settings = settings; @@ -218,7 +218,7 @@ public void addLocalNodeMasterListener(LocalNodeClusterManagerListener listener) clusterApplierService.addLocalNodeMasterListener(listener); } - public ClusterManagerService getMasterService() { + public MasterService getMasterService() { return clusterManagerService; } @@ -242,7 +242,7 @@ public ClusterApplierService getClusterApplierService() { public static boolean assertClusterOrMasterStateThread() { assert Thread.currentThread().getName().contains(ClusterApplierService.CLUSTER_UPDATE_THREAD_NAME) - || Thread.currentThread().getName().contains(ClusterManagerService.MASTER_UPDATE_THREAD_NAME) + || Thread.currentThread().getName().contains(MasterService.MASTER_UPDATE_THREAD_NAME) : "not called from the master/cluster state update thread"; return true; } diff --git a/server/src/main/java/org/opensearch/cluster/service/MasterService.java b/server/src/main/java/org/opensearch/cluster/service/MasterService.java index 952c995cee549..2d52887858372 100644 --- a/server/src/main/java/org/opensearch/cluster/service/MasterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/MasterService.java @@ -32,21 +32,909 @@ package org.opensearch.cluster.service; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.ParameterizedMessage; +import org.opensearch.Assertions; +import org.opensearch.action.support.PlainActionFuture; +import org.opensearch.cluster.AckedClusterStateTaskListener; +import org.opensearch.cluster.ClusterChangedEvent; +import org.opensearch.cluster.ClusterState; +import org.opensearch.cluster.ClusterState.Builder; +import org.opensearch.cluster.ClusterStateTaskConfig; +import org.opensearch.cluster.ClusterStateTaskExecutor; +import org.opensearch.cluster.ClusterStateTaskExecutor.ClusterTasksResult; +import org.opensearch.cluster.ClusterStateTaskListener; +import org.opensearch.cluster.coordination.ClusterStatePublisher; +import org.opensearch.cluster.coordination.FailedToCommitClusterStateException; +import org.opensearch.cluster.metadata.Metadata; +import org.opensearch.cluster.metadata.ProcessClusterEventTimeoutException; +import org.opensearch.cluster.node.DiscoveryNode; +import org.opensearch.cluster.node.DiscoveryNodes; +import org.opensearch.cluster.routing.RoutingTable; +import org.opensearch.common.Nullable; +import org.opensearch.common.Priority; +import org.opensearch.common.component.AbstractLifecycleComponent; import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Settings; +import org.opensearch.common.text.Text; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.common.util.concurrent.CountDown; +import org.opensearch.common.util.concurrent.OpenSearchExecutors; +import org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException; +import org.opensearch.common.util.concurrent.FutureUtils; +import org.opensearch.common.util.concurrent.PrioritizedOpenSearchThreadPoolExecutor; +import org.opensearch.common.util.concurrent.ThreadContext; +import org.opensearch.discovery.Discovery; +import org.opensearch.node.Node; +import org.opensearch.threadpool.Scheduler; import org.opensearch.threadpool.ThreadPool; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import static org.opensearch.common.util.concurrent.OpenSearchExecutors.daemonThreadFactory; + /** * Main Master Node Service * * @opensearch.internal - * @deprecated As of 2.1, because supporting inclusive language, replaced by {@link ClusterManagerService} */ -@Deprecated -public class MasterService extends ClusterManagerService { +public class MasterService extends AbstractLifecycleComponent { + private static final Logger logger = LogManager.getLogger(MasterService.class); + + public static final Setting MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING = Setting.positiveTimeSetting( + "cluster.service.slow_master_task_logging_threshold", + TimeValue.timeValueSeconds(10), + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); + // The setting below is going to replace the above. + // To keep backwards compatibility, the old usage is remained, and it's also used as the fallback for the new usage. + public static final Setting CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING = Setting.positiveTimeSetting( + "cluster.service.slow_cluster_manager_task_logging_threshold", + MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + + static final String MASTER_UPDATE_THREAD_NAME = "masterService#updateTask"; + + ClusterStatePublisher clusterStatePublisher; + + private final String nodeName; + + private java.util.function.Supplier clusterStateSupplier; + + private volatile TimeValue slowTaskLoggingThreshold; + + protected final ThreadPool threadPool; + + private volatile PrioritizedOpenSearchThreadPoolExecutor threadPoolExecutor; + private volatile Batcher taskBatcher; public MasterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { - super(settings, clusterSettings, threadPool); + this.nodeName = Objects.requireNonNull(Node.NODE_NAME_SETTING.get(settings)); + + this.slowTaskLoggingThreshold = CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings); + clusterSettings.addSettingsUpdateConsumer( + CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + this::setSlowTaskLoggingThreshold + ); + + this.threadPool = threadPool; + } + + private void setSlowTaskLoggingThreshold(TimeValue slowTaskLoggingThreshold) { + this.slowTaskLoggingThreshold = slowTaskLoggingThreshold; + } + + public synchronized void setClusterStatePublisher(ClusterStatePublisher publisher) { + clusterStatePublisher = publisher; + } + + public synchronized void setClusterStateSupplier(java.util.function.Supplier clusterStateSupplier) { + this.clusterStateSupplier = clusterStateSupplier; + } + + @Override + protected synchronized void doStart() { + Objects.requireNonNull(clusterStatePublisher, "please set a cluster state publisher before starting"); + Objects.requireNonNull(clusterStateSupplier, "please set a cluster state supplier before starting"); + threadPoolExecutor = createThreadPoolExecutor(); + taskBatcher = new Batcher(logger, threadPoolExecutor); + } + + protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { + return OpenSearchExecutors.newSinglePrioritizing( + nodeName + "/" + MASTER_UPDATE_THREAD_NAME, + daemonThreadFactory(nodeName, MASTER_UPDATE_THREAD_NAME), + threadPool.getThreadContext(), + threadPool.scheduler() + ); + } + + @SuppressWarnings("unchecked") + class Batcher extends TaskBatcher { + + Batcher(Logger logger, PrioritizedOpenSearchThreadPoolExecutor threadExecutor) { + super(logger, threadExecutor); + } + + @Override + protected void onTimeout(List tasks, TimeValue timeout) { + threadPool.generic() + .execute( + () -> tasks.forEach( + task -> ((UpdateTask) task).listener.onFailure( + task.source, + new ProcessClusterEventTimeoutException(timeout, task.source) + ) + ) + ); + } + + @Override + protected void run(Object batchingKey, List tasks, String tasksSummary) { + ClusterStateTaskExecutor taskExecutor = (ClusterStateTaskExecutor) batchingKey; + List updateTasks = (List) tasks; + runTasks(new TaskInputs(taskExecutor, updateTasks, tasksSummary)); + } + + class UpdateTask extends BatchedTask { + final ClusterStateTaskListener listener; + + UpdateTask( + Priority priority, + String source, + Object task, + ClusterStateTaskListener listener, + ClusterStateTaskExecutor executor + ) { + super(priority, source, executor, task); + this.listener = listener; + } + + @Override + public String describeTasks(List tasks) { + return ((ClusterStateTaskExecutor) batchingKey).describeTasks( + tasks.stream().map(BatchedTask::getTask).collect(Collectors.toList()) + ); + } + } + } + + @Override + protected synchronized void doStop() { + ThreadPool.terminate(threadPoolExecutor, 10, TimeUnit.SECONDS); + } + + @Override + protected synchronized void doClose() {} + + /** + * The current cluster state exposed by the discovery layer. Package-visible for tests. + */ + ClusterState state() { + return clusterStateSupplier.get(); + } + + private static boolean isMasterUpdateThread() { + return Thread.currentThread().getName().contains(MASTER_UPDATE_THREAD_NAME); + } + + public static boolean assertMasterUpdateThread() { + assert isMasterUpdateThread() : "not called from the cluster-manager service thread"; + return true; + } + + public static boolean assertNotMasterUpdateThread(String reason) { + assert isMasterUpdateThread() == false : "Expected current thread [" + + Thread.currentThread() + + "] to not be the cluster-maanger service thread. Reason: [" + + reason + + "]"; + return true; + } + + private void runTasks(TaskInputs taskInputs) { + final String summary = taskInputs.summary; + if (!lifecycle.started()) { + logger.debug("processing [{}]: ignoring, cluster-manager service not started", summary); + return; + } + + logger.debug("executing cluster state update for [{}]", summary); + final ClusterState previousClusterState = state(); + + if (!previousClusterState.nodes().isLocalNodeElectedMaster() && taskInputs.runOnlyWhenClusterManager()) { + logger.debug("failing [{}]: local node is no longer cluster-manager", summary); + taskInputs.onNoLongerClusterManager(); + return; + } + + final long computationStartTime = threadPool.relativeTimeInMillis(); + final TaskOutputs taskOutputs = calculateTaskOutputs(taskInputs, previousClusterState); + taskOutputs.notifyFailedTasks(); + final TimeValue computationTime = getTimeSince(computationStartTime); + logExecutionTime(computationTime, "compute cluster state update", summary); + + if (taskOutputs.clusterStateUnchanged()) { + final long notificationStartTime = threadPool.relativeTimeInMillis(); + taskOutputs.notifySuccessfulTasksOnUnchangedClusterState(); + final TimeValue executionTime = getTimeSince(notificationStartTime); + logExecutionTime(executionTime, "notify listeners on unchanged cluster state", summary); + } else { + final ClusterState newClusterState = taskOutputs.newClusterState; + if (logger.isTraceEnabled()) { + logger.trace("cluster state updated, source [{}]\n{}", summary, newClusterState); + } else { + logger.debug("cluster state updated, version [{}], source [{}]", newClusterState.version(), summary); + } + final long publicationStartTime = threadPool.relativeTimeInMillis(); + try { + ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent(summary, newClusterState, previousClusterState); + // new cluster state, notify all listeners + final DiscoveryNodes.Delta nodesDelta = clusterChangedEvent.nodesDelta(); + if (nodesDelta.hasChanges() && logger.isInfoEnabled()) { + String nodesDeltaSummary = nodesDelta.shortSummary(); + if (nodesDeltaSummary.length() > 0) { + logger.info( + "{}, term: {}, version: {}, delta: {}", + summary, + newClusterState.term(), + newClusterState.version(), + nodesDeltaSummary + ); + } + } + + logger.debug("publishing cluster state version [{}]", newClusterState.version()); + publish(clusterChangedEvent, taskOutputs, publicationStartTime); + } catch (Exception e) { + handleException(summary, publicationStartTime, newClusterState, e); + } + } + } + + private TimeValue getTimeSince(long startTimeMillis) { + return TimeValue.timeValueMillis(Math.max(0, threadPool.relativeTimeInMillis() - startTimeMillis)); + } + + protected void publish(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeMillis) { + final PlainActionFuture fut = new PlainActionFuture() { + @Override + protected boolean blockingAllowed() { + return isMasterUpdateThread() || super.blockingAllowed(); + } + }; + clusterStatePublisher.publish(clusterChangedEvent, fut, taskOutputs.createAckListener(threadPool, clusterChangedEvent.state())); + + // indefinitely wait for publication to complete + try { + FutureUtils.get(fut); + onPublicationSuccess(clusterChangedEvent, taskOutputs); + } catch (Exception e) { + onPublicationFailed(clusterChangedEvent, taskOutputs, startTimeMillis, e); + } + } + + void onPublicationSuccess(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs) { + final long notificationStartTime = threadPool.relativeTimeInMillis(); + taskOutputs.processedDifferentClusterState(clusterChangedEvent.previousState(), clusterChangedEvent.state()); + + try { + taskOutputs.clusterStatePublished(clusterChangedEvent); + } catch (Exception e) { + logger.error( + () -> new ParameterizedMessage( + "exception thrown while notifying executor of new cluster state publication [{}]", + clusterChangedEvent.source() + ), + e + ); + } + final TimeValue executionTime = getTimeSince(notificationStartTime); + logExecutionTime( + executionTime, + "notify listeners on successful publication of cluster state (version: " + + clusterChangedEvent.state().version() + + ", uuid: " + + clusterChangedEvent.state().stateUUID() + + ')', + clusterChangedEvent.source() + ); + } + + void onPublicationFailed(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeMillis, Exception exception) { + if (exception instanceof FailedToCommitClusterStateException) { + final long version = clusterChangedEvent.state().version(); + logger.warn( + () -> new ParameterizedMessage( + "failing [{}]: failed to commit cluster state version [{}]", + clusterChangedEvent.source(), + version + ), + exception + ); + taskOutputs.publishingFailed((FailedToCommitClusterStateException) exception); + } else { + handleException(clusterChangedEvent.source(), startTimeMillis, clusterChangedEvent.state(), exception); + } + } + + private void handleException(String summary, long startTimeMillis, ClusterState newClusterState, Exception e) { + final TimeValue executionTime = getTimeSince(startTimeMillis); + final long version = newClusterState.version(); + final String stateUUID = newClusterState.stateUUID(); + final String fullState = newClusterState.toString(); + logger.warn( + new ParameterizedMessage( + "took [{}] and then failed to publish updated cluster state (version: {}, uuid: {}) for [{}]:\n{}", + executionTime, + version, + stateUUID, + summary, + fullState + ), + e + ); + // TODO: do we want to call updateTask.onFailure here? + } + + private TaskOutputs calculateTaskOutputs(TaskInputs taskInputs, ClusterState previousClusterState) { + ClusterTasksResult clusterTasksResult = executeTasks(taskInputs, previousClusterState); + ClusterState newClusterState = patchVersions(previousClusterState, clusterTasksResult); + return new TaskOutputs( + taskInputs, + previousClusterState, + newClusterState, + getNonFailedTasks(taskInputs, clusterTasksResult), + clusterTasksResult.executionResults + ); + } + + private ClusterState patchVersions(ClusterState previousClusterState, ClusterTasksResult executionResult) { + ClusterState newClusterState = executionResult.resultingState; + + if (previousClusterState != newClusterState) { + // only the cluster-manager controls the version numbers + Builder builder = incrementVersion(newClusterState); + if (previousClusterState.routingTable() != newClusterState.routingTable()) { + builder.routingTable( + RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1).build() + ); + } + if (previousClusterState.metadata() != newClusterState.metadata()) { + builder.metadata(Metadata.builder(newClusterState.metadata()).version(newClusterState.metadata().version() + 1)); + } + + newClusterState = builder.build(); + } + + return newClusterState; + } + + public Builder incrementVersion(ClusterState clusterState) { + return ClusterState.builder(clusterState).incrementVersion(); + } + + /** + * Submits a cluster state update task; unlike {@link #submitStateUpdateTask(String, Object, ClusterStateTaskConfig, + * ClusterStateTaskExecutor, ClusterStateTaskListener)}, submitted updates will not be batched. + * + * @param source the source of the cluster state update task + * @param updateTask the full context for the cluster state update + * task + * + */ + public & ClusterStateTaskListener> void submitStateUpdateTask( + String source, + T updateTask + ) { + submitStateUpdateTask(source, updateTask, updateTask, updateTask, updateTask); + } + + /** + * Submits a cluster state update task; submitted updates will be + * batched across the same instance of executor. The exact batching + * semantics depend on the underlying implementation but a rough + * guideline is that if the update task is submitted while there + * are pending update tasks for the same executor, these update + * tasks will all be executed on the executor in a single batch + * + * @param source the source of the cluster state update task + * @param task the state needed for the cluster state update task + * @param config the cluster state update task configuration + * @param executor the cluster state update task executor; tasks + * that share the same executor will be executed + * batches on this executor + * @param listener callback after the cluster state update task + * completes + * @param the type of the cluster state update task state + * + */ + public void submitStateUpdateTask( + String source, + T task, + ClusterStateTaskConfig config, + ClusterStateTaskExecutor executor, + ClusterStateTaskListener listener + ) { + submitStateUpdateTasks(source, Collections.singletonMap(task, listener), config, executor); + } + + /** + * Output created by executing a set of tasks provided as TaskInputs + */ + class TaskOutputs { + final TaskInputs taskInputs; + final ClusterState previousClusterState; + final ClusterState newClusterState; + final List nonFailedTasks; + final Map executionResults; + + TaskOutputs( + TaskInputs taskInputs, + ClusterState previousClusterState, + ClusterState newClusterState, + List nonFailedTasks, + Map executionResults + ) { + this.taskInputs = taskInputs; + this.previousClusterState = previousClusterState; + this.newClusterState = newClusterState; + this.nonFailedTasks = nonFailedTasks; + this.executionResults = executionResults; + } + + void publishingFailed(FailedToCommitClusterStateException t) { + nonFailedTasks.forEach(task -> task.listener.onFailure(task.source(), t)); + } + + void processedDifferentClusterState(ClusterState previousClusterState, ClusterState newClusterState) { + nonFailedTasks.forEach(task -> task.listener.clusterStateProcessed(task.source(), previousClusterState, newClusterState)); + } + + void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) { + taskInputs.executor.clusterStatePublished(clusterChangedEvent); + } + + Discovery.AckListener createAckListener(ThreadPool threadPool, ClusterState newClusterState) { + return new DelegatingAckListener( + nonFailedTasks.stream() + .filter(task -> task.listener instanceof AckedClusterStateTaskListener) + .map( + task -> new AckCountDownListener( + (AckedClusterStateTaskListener) task.listener, + newClusterState.version(), + newClusterState.nodes(), + threadPool + ) + ) + .collect(Collectors.toList()) + ); + } + + boolean clusterStateUnchanged() { + return previousClusterState == newClusterState; + } + + void notifyFailedTasks() { + // fail all tasks that have failed + for (Batcher.UpdateTask updateTask : taskInputs.updateTasks) { + assert executionResults.containsKey(updateTask.task) : "missing " + updateTask; + final ClusterStateTaskExecutor.TaskResult taskResult = executionResults.get(updateTask.task); + if (taskResult.isSuccess() == false) { + updateTask.listener.onFailure(updateTask.source(), taskResult.getFailure()); + } + } + } + + void notifySuccessfulTasksOnUnchangedClusterState() { + nonFailedTasks.forEach(task -> { + if (task.listener instanceof AckedClusterStateTaskListener) { + // no need to wait for ack if nothing changed, the update can be counted as acknowledged + ((AckedClusterStateTaskListener) task.listener).onAllNodesAcked(null); + } + task.listener.clusterStateProcessed(task.source(), newClusterState, newClusterState); + }); + } + } + + /** + * Returns the tasks that are pending. + */ + public List pendingTasks() { + return Arrays.stream(threadPoolExecutor.getPending()).map(pending -> { + assert pending.task instanceof SourcePrioritizedRunnable + : "thread pool executor should only use SourcePrioritizedRunnable instances but found: " + + pending.task.getClass().getName(); + SourcePrioritizedRunnable task = (SourcePrioritizedRunnable) pending.task; + return new PendingClusterTask( + pending.insertionOrder, + pending.priority, + new Text(task.source()), + task.getAgeInMillis(), + pending.executing + ); + }).collect(Collectors.toList()); + } + + /** + * Returns the number of currently pending tasks. + */ + public int numberOfPendingTasks() { + return threadPoolExecutor.getNumberOfPendingTasks(); + } + + /** + * Returns the maximum wait time for tasks in the queue + * + * @return A zero time value if the queue is empty, otherwise the time value oldest task waiting in the queue + */ + public TimeValue getMaxTaskWaitTime() { + return threadPoolExecutor.getMaxTaskWaitTime(); + } + + private SafeClusterStateTaskListener safe(ClusterStateTaskListener listener, Supplier contextSupplier) { + if (listener instanceof AckedClusterStateTaskListener) { + return new SafeAckedClusterStateTaskListener((AckedClusterStateTaskListener) listener, contextSupplier, logger); + } else { + return new SafeClusterStateTaskListener(listener, contextSupplier, logger); + } + } + + private static class SafeClusterStateTaskListener implements ClusterStateTaskListener { + private final ClusterStateTaskListener listener; + protected final Supplier context; + private final Logger logger; + + SafeClusterStateTaskListener(ClusterStateTaskListener listener, Supplier context, Logger logger) { + this.listener = listener; + this.context = context; + this.logger = logger; + } + + @Override + public void onFailure(String source, Exception e) { + try (ThreadContext.StoredContext ignore = context.get()) { + listener.onFailure(source, e); + } catch (Exception inner) { + inner.addSuppressed(e); + logger.error(() -> new ParameterizedMessage("exception thrown by listener notifying of failure from [{}]", source), inner); + } + } + + @Override + public void onNoLongerMaster(String source) { + try (ThreadContext.StoredContext ignore = context.get()) { + listener.onNoLongerMaster(source); + } catch (Exception e) { + logger.error( + () -> new ParameterizedMessage( + "exception thrown by listener while notifying no longer cluster-manager from [{}]", + source + ), + e + ); + } + } + + @Override + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { + try (ThreadContext.StoredContext ignore = context.get()) { + listener.clusterStateProcessed(source, oldState, newState); + } catch (Exception e) { + logger.error( + () -> new ParameterizedMessage( + "exception thrown by listener while notifying of cluster state processed from [{}], old cluster state:\n" + + "{}\nnew cluster state:\n{}", + source, + oldState, + newState + ), + e + ); + } + } + } + + private static class SafeAckedClusterStateTaskListener extends SafeClusterStateTaskListener implements AckedClusterStateTaskListener { + private final AckedClusterStateTaskListener listener; + private final Logger logger; + + SafeAckedClusterStateTaskListener( + AckedClusterStateTaskListener listener, + Supplier context, + Logger logger + ) { + super(listener, context, logger); + this.listener = listener; + this.logger = logger; + } + + @Override + public boolean mustAck(DiscoveryNode discoveryNode) { + return listener.mustAck(discoveryNode); + } + + @Override + public void onAllNodesAcked(@Nullable Exception e) { + try (ThreadContext.StoredContext ignore = context.get()) { + listener.onAllNodesAcked(e); + } catch (Exception inner) { + inner.addSuppressed(e); + logger.error("exception thrown by listener while notifying on all nodes acked", inner); + } + } + + @Override + public void onAckTimeout() { + try (ThreadContext.StoredContext ignore = context.get()) { + listener.onAckTimeout(); + } catch (Exception e) { + logger.error("exception thrown by listener while notifying on ack timeout", e); + } + } + + @Override + public TimeValue ackTimeout() { + return listener.ackTimeout(); + } + } + + private void logExecutionTime(TimeValue executionTime, String activity, String summary) { + if (executionTime.getMillis() > slowTaskLoggingThreshold.getMillis()) { + logger.warn("took [{}], which is over [{}], to {} for [{}]", executionTime, slowTaskLoggingThreshold, activity, summary); + } else { + logger.debug("took [{}] to {} for [{}]", executionTime, activity, summary); + } + } + + private static class DelegatingAckListener implements Discovery.AckListener { + + private final List listeners; + + private DelegatingAckListener(List listeners) { + this.listeners = listeners; + } + + @Override + public void onCommit(TimeValue commitTime) { + for (Discovery.AckListener listener : listeners) { + listener.onCommit(commitTime); + } + } + + @Override + public void onNodeAck(DiscoveryNode node, @Nullable Exception e) { + for (Discovery.AckListener listener : listeners) { + listener.onNodeAck(node, e); + } + } + } + + private static class AckCountDownListener implements Discovery.AckListener { + + private static final Logger logger = LogManager.getLogger(AckCountDownListener.class); + + private final AckedClusterStateTaskListener ackedTaskListener; + private final CountDown countDown; + private final DiscoveryNode clusterManagerNode; + private final ThreadPool threadPool; + private final long clusterStateVersion; + private volatile Scheduler.Cancellable ackTimeoutCallback; + private Exception lastFailure; + + AckCountDownListener( + AckedClusterStateTaskListener ackedTaskListener, + long clusterStateVersion, + DiscoveryNodes nodes, + ThreadPool threadPool + ) { + this.ackedTaskListener = ackedTaskListener; + this.clusterStateVersion = clusterStateVersion; + this.threadPool = threadPool; + this.clusterManagerNode = nodes.getMasterNode(); + int countDown = 0; + for (DiscoveryNode node : nodes) { + // we always wait for at least the cluster-manager node + if (node.equals(clusterManagerNode) || ackedTaskListener.mustAck(node)) { + countDown++; + } + } + logger.trace("expecting {} acknowledgements for cluster_state update (version: {})", countDown, clusterStateVersion); + this.countDown = new CountDown(countDown + 1); // we also wait for onCommit to be called + } + + @Override + public void onCommit(TimeValue commitTime) { + TimeValue ackTimeout = ackedTaskListener.ackTimeout(); + if (ackTimeout == null) { + ackTimeout = TimeValue.ZERO; + } + final TimeValue timeLeft = TimeValue.timeValueNanos(Math.max(0, ackTimeout.nanos() - commitTime.nanos())); + if (timeLeft.nanos() == 0L) { + onTimeout(); + } else if (countDown.countDown()) { + finish(); + } else { + this.ackTimeoutCallback = threadPool.schedule(this::onTimeout, timeLeft, ThreadPool.Names.GENERIC); + // re-check if onNodeAck has not completed while we were scheduling the timeout + if (countDown.isCountedDown()) { + ackTimeoutCallback.cancel(); + } + } + } + + @Override + public void onNodeAck(DiscoveryNode node, @Nullable Exception e) { + if (node.equals(clusterManagerNode) == false && ackedTaskListener.mustAck(node) == false) { + return; + } + if (e == null) { + logger.trace("ack received from node [{}], cluster_state update (version: {})", node, clusterStateVersion); + } else { + this.lastFailure = e; + logger.debug( + () -> new ParameterizedMessage( + "ack received from node [{}], cluster_state update (version: {})", + node, + clusterStateVersion + ), + e + ); + } + + if (countDown.countDown()) { + finish(); + } + } + + private void finish() { + logger.trace("all expected nodes acknowledged cluster_state update (version: {})", clusterStateVersion); + if (ackTimeoutCallback != null) { + ackTimeoutCallback.cancel(); + } + ackedTaskListener.onAllNodesAcked(lastFailure); + } + + public void onTimeout() { + if (countDown.fastForward()) { + logger.trace("timeout waiting for acknowledgement for cluster_state update (version: {})", clusterStateVersion); + ackedTaskListener.onAckTimeout(); + } + } + } + + private ClusterTasksResult executeTasks(TaskInputs taskInputs, ClusterState previousClusterState) { + ClusterTasksResult clusterTasksResult; + try { + List inputs = taskInputs.updateTasks.stream().map(tUpdateTask -> tUpdateTask.task).collect(Collectors.toList()); + clusterTasksResult = taskInputs.executor.execute(previousClusterState, inputs); + if (previousClusterState != clusterTasksResult.resultingState + && previousClusterState.nodes().isLocalNodeElectedMaster() + && (clusterTasksResult.resultingState.nodes().isLocalNodeElectedMaster() == false)) { + throw new AssertionError("update task submitted to ClusterManagerService cannot remove cluster-manager"); + } + } catch (Exception e) { + logger.trace( + () -> new ParameterizedMessage( + "failed to execute cluster state update (on version: [{}], uuid: [{}]) for [{}]\n{}{}{}", + previousClusterState.version(), + previousClusterState.stateUUID(), + taskInputs.summary, + previousClusterState.nodes(), + previousClusterState.routingTable(), + previousClusterState.getRoutingNodes() + ), // may be expensive => construct message lazily + e + ); + clusterTasksResult = ClusterTasksResult.builder() + .failures(taskInputs.updateTasks.stream().map(updateTask -> updateTask.task)::iterator, e) + .build(previousClusterState); + } + + assert clusterTasksResult.executionResults != null; + assert clusterTasksResult.executionResults.size() == taskInputs.updateTasks.size() : String.format( + Locale.ROOT, + "expected [%d] task result%s but was [%d]", + taskInputs.updateTasks.size(), + taskInputs.updateTasks.size() == 1 ? "" : "s", + clusterTasksResult.executionResults.size() + ); + if (Assertions.ENABLED) { + ClusterTasksResult finalClusterTasksResult = clusterTasksResult; + taskInputs.updateTasks.forEach( + updateTask -> { + assert finalClusterTasksResult.executionResults.containsKey(updateTask.task) : "missing task result for " + updateTask; + } + ); + } + + return clusterTasksResult; + } + + private List getNonFailedTasks(TaskInputs taskInputs, ClusterTasksResult clusterTasksResult) { + return taskInputs.updateTasks.stream().filter(updateTask -> { + assert clusterTasksResult.executionResults.containsKey(updateTask.task) : "missing " + updateTask; + final ClusterStateTaskExecutor.TaskResult taskResult = clusterTasksResult.executionResults.get(updateTask.task); + return taskResult.isSuccess(); + }).collect(Collectors.toList()); + } + + /** + * Represents a set of tasks to be processed together with their executor + */ + private class TaskInputs { + final String summary; + final List updateTasks; + final ClusterStateTaskExecutor executor; + + TaskInputs(ClusterStateTaskExecutor executor, List updateTasks, String summary) { + this.summary = summary; + this.executor = executor; + this.updateTasks = updateTasks; + } + + boolean runOnlyWhenClusterManager() { + return executor.runOnlyOnMaster(); + } + + void onNoLongerClusterManager() { + updateTasks.forEach(task -> task.listener.onNoLongerMaster(task.source())); + } + } + + /** + * Submits a batch of cluster state update tasks; submitted updates are guaranteed to be processed together, + * potentially with more tasks of the same executor. + * + * @param source the source of the cluster state update task + * @param tasks a map of update tasks and their corresponding listeners + * @param config the cluster state update task configuration + * @param executor the cluster state update task executor; tasks + * that share the same executor will be executed + * batches on this executor + * @param the type of the cluster state update task state + * + */ + public void submitStateUpdateTasks( + final String source, + final Map tasks, + final ClusterStateTaskConfig config, + final ClusterStateTaskExecutor executor + ) { + if (!lifecycle.started()) { + return; + } + final ThreadContext threadContext = threadPool.getThreadContext(); + final Supplier supplier = threadContext.newRestorableContext(true); + try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { + threadContext.markAsSystemContext(); + + List safeTasks = tasks.entrySet() + .stream() + .map(e -> taskBatcher.new UpdateTask(config.priority(), source, e.getKey(), safe(e.getValue(), supplier), executor)) + .collect(Collectors.toList()); + taskBatcher.submitTasks(safeTasks, config.timeout()); + } catch (OpenSearchRejectedExecutionException e) { + // ignore cases where we are shutting down..., there is really nothing interesting + // to be done here... + if (!lifecycle.stoppedOrClosed()) { + throw e; + } + } } } diff --git a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java index 4451d11a7b976..f3d2ab0859513 100644 --- a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java +++ b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java @@ -81,7 +81,7 @@ import org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider; import org.opensearch.cluster.service.ClusterApplierService; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.logging.Loggers; import org.opensearch.common.network.NetworkModule; import org.opensearch.common.network.NetworkService; @@ -334,8 +334,8 @@ public void apply(Settings value, Settings current, Settings previous) { IndexModule.NODE_STORE_ALLOW_MMAP, ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, ClusterService.USER_DEFINED_METADATA, - ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, // deprecated - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, // deprecated + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, SearchService.DEFAULT_SEARCH_TIMEOUT_SETTING, SearchService.DEFAULT_ALLOW_PARTIAL_SEARCH_RESULTS, TransportSearchAction.SHARD_COUNT_LIMIT_SETTING, diff --git a/server/src/main/java/org/opensearch/common/util/concurrent/BaseFuture.java b/server/src/main/java/org/opensearch/common/util/concurrent/BaseFuture.java index e4f8e1a221a0d..fef37299b349d 100644 --- a/server/src/main/java/org/opensearch/common/util/concurrent/BaseFuture.java +++ b/server/src/main/java/org/opensearch/common/util/concurrent/BaseFuture.java @@ -33,7 +33,7 @@ package org.opensearch.common.util.concurrent; import org.opensearch.cluster.service.ClusterApplierService; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.Nullable; import org.opensearch.threadpool.ThreadPool; import org.opensearch.transport.Transports; @@ -109,7 +109,7 @@ protected boolean blockingAllowed() { return Transports.assertNotTransportThread(BLOCKING_OP_REASON) && ThreadPool.assertNotScheduleThread(BLOCKING_OP_REASON) && ClusterApplierService.assertNotClusterStateUpdateThread(BLOCKING_OP_REASON) - && ClusterManagerService.assertNotMasterUpdateThread(BLOCKING_OP_REASON); + && MasterService.assertNotMasterUpdateThread(BLOCKING_OP_REASON); } @Override diff --git a/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java b/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java index 44f44fa055b2b..c12283df1dbc9 100644 --- a/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java +++ b/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java @@ -41,7 +41,7 @@ import org.opensearch.cluster.routing.RerouteService; import org.opensearch.cluster.routing.allocation.AllocationService; import org.opensearch.cluster.service.ClusterApplier; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.Randomness; import org.opensearch.common.io.stream.NamedWriteableRegistry; import org.opensearch.common.network.NetworkService; @@ -121,7 +121,7 @@ public DiscoveryModule( TransportService transportService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService, - ClusterManagerService clusterManagerService, + MasterService clusterManagerService, ClusterApplier clusterApplier, ClusterSettings clusterSettings, List plugins, diff --git a/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java b/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java index 6005ca9150488..1c247a7b5a3da 100644 --- a/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java +++ b/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java @@ -48,8 +48,8 @@ import org.opensearch.cluster.service.ClusterApplier; import org.opensearch.cluster.service.ClusterApplierService; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.cluster.service.FakeThreadPoolClusterManagerService; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.FakeThreadPoolMasterService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.PrioritizedOpenSearchThreadPoolExecutor; @@ -87,7 +87,7 @@ protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { } }; - final ClusterManagerService clusterManagerService = new FakeThreadPoolClusterManagerService( + final MasterService clusterManagerService = new FakeThreadPoolMasterService( "test", "clusterManagerService", threadPool, diff --git a/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java b/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java index 3606d24222679..61a81a8c08389 100644 --- a/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java +++ b/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java @@ -43,9 +43,9 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNodeRole; import org.opensearch.cluster.node.DiscoveryNodes; -import org.opensearch.cluster.service.FakeThreadPoolClusterManagerService; -import org.opensearch.cluster.service.ClusterManagerService; -import org.opensearch.cluster.service.ClusterManagerServiceTests; +import org.opensearch.cluster.service.FakeThreadPoolMasterService; +import org.opensearch.cluster.service.MasterService; +import org.opensearch.cluster.service.MasterServiceTests; import org.opensearch.common.Randomness; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; @@ -98,7 +98,7 @@ public class NodeJoinTests extends OpenSearchTestCase { private static ThreadPool threadPool; - private ClusterManagerService clusterManagerService; + private MasterService clusterManagerService; private Coordinator coordinator; private DeterministicTaskQueue deterministicTaskQueue; private Transport transport; @@ -144,7 +144,7 @@ private void setupFakeClusterManagerServiceAndCoordinator(long term, ClusterStat random() ); final ThreadPool fakeThreadPool = deterministicTaskQueue.getThreadPool(); - FakeThreadPoolClusterManagerService fakeClusterManagerService = new FakeThreadPoolClusterManagerService( + FakeThreadPoolMasterService fakeClusterManagerService = new FakeThreadPoolMasterService( "test_node", "test", fakeThreadPool, @@ -166,7 +166,7 @@ private void setupFakeClusterManagerServiceAndCoordinator(long term, ClusterStat } private void setupRealClusterManagerServiceAndCoordinator(long term, ClusterState initialState) { - ClusterManagerService clusterManagerService = new ClusterManagerService( + MasterService clusterManagerService = new MasterService( Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test_node").build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool @@ -191,7 +191,7 @@ private void setupRealClusterManagerServiceAndCoordinator(long term, ClusterStat private void setupClusterManagerServiceAndCoordinator( long term, ClusterState initialState, - ClusterManagerService clusterManagerService, + MasterService clusterManagerService, ThreadPool threadPool, Random random, NodeHealthService nodeHealthService @@ -514,7 +514,7 @@ public void testJoinUpdateVotingConfigExclusion() throws Exception { ); assertTrue( - ClusterManagerServiceTests.discoveryState(clusterManagerService) + MasterServiceTests.discoveryState(clusterManagerService) .getVotingConfigExclusions() .stream() .anyMatch( @@ -746,7 +746,7 @@ public void testConcurrentJoining() { throw new RuntimeException(e); } - assertTrue(ClusterManagerServiceTests.discoveryState(clusterManagerService).nodes().isLocalNodeElectedMaster()); + assertTrue(MasterServiceTests.discoveryState(clusterManagerService).nodes().isLocalNodeElectedMaster()); for (DiscoveryNode successfulNode : successfulNodes) { assertTrue(successfulNode + " joined cluster", clusterStateHasNode(successfulNode)); assertFalse(successfulNode + " voted for cluster-manager", coordinator.missingJoinVoteFrom(successfulNode)); @@ -776,10 +776,10 @@ public void testJoinElectedLeaderWithDeprecatedMasterRole() { } private boolean isLocalNodeElectedMaster() { - return ClusterManagerServiceTests.discoveryState(clusterManagerService).nodes().isLocalNodeElectedMaster(); + return MasterServiceTests.discoveryState(clusterManagerService).nodes().isLocalNodeElectedMaster(); } private boolean clusterStateHasNode(DiscoveryNode node) { - return node.equals(ClusterManagerServiceTests.discoveryState(clusterManagerService).nodes().get(node.getId())); + return node.equals(MasterServiceTests.discoveryState(clusterManagerService).nodes().get(node.getId())); } } diff --git a/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java b/server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java similarity index 85% rename from server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java rename to server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java index d86964ab9c82b..0a3e6f48c5f10 100644 --- a/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceRenamedSettingTests.java +++ b/server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java @@ -22,7 +22,7 @@ * after it is deprecated, so that the backwards compatibility is maintained. * The test can be removed along with removing support of the deprecated setting. */ -public class ClusterManagerServiceRenamedSettingTests extends OpenSearchTestCase { +public class MasterServiceRenamedSettingTests extends OpenSearchTestCase { /** * Validate the both settings are known and supported. @@ -33,8 +33,8 @@ public void testClusterManagerServiceSettingsExist() { "Both 'cluster.service.slow_cluster_manager_task_logging_threshold' and its predecessor should be supported built-in settings", settings.containsAll( Arrays.asList( - ClusterManagerService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING ) ) ); @@ -63,7 +63,7 @@ public void testMasterServiceSettingsExist() { public void testSettingFallback() { assertEquals( MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY), - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY) + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY) ); } @@ -74,7 +74,7 @@ public void testSettingGetValue() { Settings settings = Settings.builder().put("cluster.service.slow_cluster_manager_task_logging_threshold", "9s").build(); assertEquals( TimeValue.timeValueSeconds(9), - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); assertEquals( MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.getDefault(Settings.EMPTY), @@ -90,7 +90,7 @@ public void testSettingGetValueWithFallback() { Settings settings = Settings.builder().put("cluster.service.slow_master_task_logging_threshold", "8s").build(); assertEquals( TimeValue.timeValueSeconds(8), - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); assertSettingDeprecationsAndWarnings(new Setting[] { MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING }); @@ -106,7 +106,7 @@ public void testSettingGetValueWhenBothAreConfigured() { .build(); assertEquals( TimeValue.timeValueSeconds(9), - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings) ); assertEquals(TimeValue.timeValueSeconds(8), MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(settings)); diff --git a/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceTests.java b/server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java similarity index 92% rename from server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceTests.java rename to server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java index 7c7a725032b52..d5f7344c544b9 100644 --- a/server/src/test/java/org/opensearch/cluster/service/ClusterManagerServiceTests.java +++ b/server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java @@ -93,14 +93,14 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasKey; -public class ClusterManagerServiceTests extends OpenSearchTestCase { +public class MasterServiceTests extends OpenSearchTestCase { private static ThreadPool threadPool; private static long relativeTimeInMillis; @BeforeClass public static void createThreadPool() { - threadPool = new TestThreadPool(ClusterManagerServiceTests.class.getName()) { + threadPool = new TestThreadPool(MasterServiceTests.class.getName()) { @Override public long relativeTimeInMillis() { return relativeTimeInMillis; @@ -121,17 +121,17 @@ public void randomizeCurrentTime() { relativeTimeInMillis = randomLongBetween(0L, 1L << 62); } - private ClusterManagerService createClusterManagerService(boolean makeClusterManager) { + private MasterService createClusterManagerService(boolean makeClusterManager) { final DiscoveryNode localNode = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); - final ClusterManagerService clusterManagerService = new ClusterManagerService( + final MasterService clusterManagerService = new MasterService( Settings.builder() - .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), ClusterManagerServiceTests.class.getSimpleName()) + .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()) .put(Node.NODE_NAME_SETTING.getKey(), "test_node") .build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool ); - final ClusterState initialClusterState = ClusterState.builder(new ClusterName(ClusterManagerServiceTests.class.getSimpleName())) + final ClusterState initialClusterState = ClusterState.builder(new ClusterName(MasterServiceTests.class.getSimpleName())) .nodes( DiscoveryNodes.builder() .add(localNode) @@ -151,7 +151,7 @@ private ClusterManagerService createClusterManagerService(boolean makeClusterMan } public void testClusterManagerAwareExecution() throws Exception { - final ClusterManagerService nonClusterManager = createClusterManagerService(false); + final MasterService nonClusterManager = createClusterManagerService(false); final boolean[] taskFailed = { false }; final CountDownLatch latch1 = new CountDownLatch(1); @@ -194,7 +194,7 @@ public void onFailure(String source, Exception e) { } public void testThreadContext() throws InterruptedException { - final ClusterManagerService clusterManager = createClusterManagerService(true); + final MasterService clusterManager = createClusterManagerService(true); final CountDownLatch latch = new CountDownLatch(1); try (ThreadContext.StoredContext ignored = threadPool.getThreadContext().stashContext()) { @@ -292,7 +292,7 @@ public void testClusterStateTaskListenerThrowingExceptionIsOkay() throws Interru final CountDownLatch latch = new CountDownLatch(1); AtomicBoolean published = new AtomicBoolean(); - try (ClusterManagerService clusterManagerService = createClusterManagerService(true)) { + try (MasterService clusterManagerService = createClusterManagerService(true)) { clusterManagerService.submitStateUpdateTask( "testClusterStateTaskListenerThrowingExceptionIsOkay", new Object(), @@ -328,11 +328,11 @@ public void onFailure(String source, Exception e) {} @TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level") public void testClusterStateUpdateLogging() throws Exception { - try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(ClusterManagerService.class))) { + try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(MasterService.class))) { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test1 start", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test1]" ) @@ -340,7 +340,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test1 computation", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "took [1s] to compute cluster state update for [test1]" ) @@ -348,7 +348,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test1 notification", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "took [0s] to notify listeners on unchanged cluster state for [test1]" ) @@ -357,7 +357,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test2 start", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test2]" ) @@ -365,7 +365,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test2 failure", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.TRACE, "failed to execute cluster state update (on version: [*], uuid: [*]) for [test2]*" ) @@ -373,7 +373,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test2 computation", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "took [2s] to compute cluster state update for [test2]" ) @@ -381,7 +381,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test2 notification", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "took [0s] to notify listeners on unchanged cluster state for [test2]" ) @@ -390,7 +390,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test3 start", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test3]" ) @@ -398,7 +398,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test3 computation", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "took [3s] to compute cluster state update for [test3]" ) @@ -406,7 +406,7 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test3 notification", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "took [4s] to notify listeners on successful publication of cluster state (version: *, uuid: *) for [test3]" ) @@ -415,13 +415,13 @@ public void testClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test4", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test4]" ) ); - try (ClusterManagerService clusterManagerService = createClusterManagerService(true)) { + try (MasterService clusterManagerService = createClusterManagerService(true)) { clusterManagerService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { @@ -617,7 +617,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS } }; - try (ClusterManagerService clusterManagerService = createClusterManagerService(true)) { + try (MasterService clusterManagerService = createClusterManagerService(true)) { final ConcurrentMap submittedTasksPerThread = new ConcurrentHashMap<>(); CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads); for (int i = 0; i < numberOfThreads; i++) { @@ -696,7 +696,7 @@ public void testBlockingCallInClusterStateTaskListenerFails() throws Interrupted final CountDownLatch latch = new CountDownLatch(1); final AtomicReference assertionRef = new AtomicReference<>(); - try (ClusterManagerService clusterManagerService = createClusterManagerService(true)) { + try (MasterService clusterManagerService = createClusterManagerService(true)) { clusterManagerService.submitStateUpdateTask( "testBlockingCallInClusterStateTaskListenerFails", new Object(), @@ -737,11 +737,11 @@ public void onFailure(String source, Exception e) {} @TestLogging(value = "org.opensearch.cluster.service:WARN", reason = "to ensure that we log cluster state events on WARN level") public void testLongClusterStateUpdateLogging() throws Exception { - try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(ClusterManagerService.class))) { + try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(MasterService.class))) { mockAppender.addExpectation( new MockLogAppender.UnseenEventExpectation( "test1 shouldn't log because it was fast enough", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.WARN, "*took*test1*" ) @@ -749,7 +749,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test2", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.WARN, "*took [*], which is over [10s], to compute cluster state update for [test2]" ) @@ -757,7 +757,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test3", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.WARN, "*took [*], which is over [10s], to compute cluster state update for [test3]" ) @@ -765,7 +765,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test4", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.WARN, "*took [*], which is over [10s], to compute cluster state update for [test4]" ) @@ -773,7 +773,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.UnseenEventExpectation( "test5 should not log despite publishing slowly", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.WARN, "*took*test5*" ) @@ -781,16 +781,16 @@ public void testLongClusterStateUpdateLogging() throws Exception { mockAppender.addExpectation( new MockLogAppender.SeenEventExpectation( "test6 should log due to slow and failing publication", - ClusterManagerService.class.getCanonicalName(), + MasterService.class.getCanonicalName(), Level.WARN, "took [*] and then failed to publish updated cluster state (version: *, uuid: *) for [test6]:*" ) ); try ( - ClusterManagerService clusterManagerService = new ClusterManagerService( + MasterService clusterManagerService = new MasterService( Settings.builder() - .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), ClusterManagerServiceTests.class.getSimpleName()) + .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()) .put(Node.NODE_NAME_SETTING.getKey(), "test_node") .build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), @@ -805,21 +805,19 @@ public void testLongClusterStateUpdateLogging() throws Exception { emptySet(), Version.CURRENT ); - final ClusterState initialClusterState = ClusterState.builder( - new ClusterName(ClusterManagerServiceTests.class.getSimpleName()) - ) + final ClusterState initialClusterState = ClusterState.builder(new ClusterName(MasterServiceTests.class.getSimpleName())) .nodes(DiscoveryNodes.builder().add(localNode).localNodeId(localNode.getId()).masterNodeId(localNode.getId())) .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK) .build(); final AtomicReference clusterStateRef = new AtomicReference<>(initialClusterState); clusterManagerService.setClusterStatePublisher((event, publishListener, ackListener) -> { if (event.source().contains("test5")) { - relativeTimeInMillis += ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( + relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( Settings.EMPTY ).millis() + randomLongBetween(1, 1000000); } if (event.source().contains("test6")) { - relativeTimeInMillis += ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( + relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( Settings.EMPTY ).millis() + randomLongBetween(1, 1000000); throw new OpenSearchException("simulated error during slow publication which should trigger logging"); @@ -837,7 +835,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += randomLongBetween( 0L, - ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() + MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() ); return currentState; } @@ -858,7 +856,7 @@ public void onFailure(String source, Exception e) { clusterManagerService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { - relativeTimeInMillis += ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( + relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( Settings.EMPTY ).millis() + randomLongBetween(1, 1000000); throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task"); @@ -877,7 +875,7 @@ public void onFailure(String source, Exception e) { clusterManagerService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { - relativeTimeInMillis += ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( + relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( Settings.EMPTY ).millis() + randomLongBetween(1, 1000000); return ClusterState.builder(currentState).incrementVersion().build(); @@ -896,7 +894,7 @@ public void onFailure(String source, Exception e) { clusterManagerService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { - relativeTimeInMillis += ClusterManagerService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( + relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( Settings.EMPTY ).millis() + randomLongBetween(1, 1000000); return currentState; @@ -973,9 +971,9 @@ public void testAcking() throws InterruptedException { final DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); final DiscoveryNode node3 = new DiscoveryNode("node3", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); try ( - ClusterManagerService clusterManagerService = new ClusterManagerService( + MasterService clusterManagerService = new MasterService( Settings.builder() - .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), ClusterManagerServiceTests.class.getSimpleName()) + .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()) .put(Node.NODE_NAME_SETTING.getKey(), "test_node") .build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), @@ -983,7 +981,7 @@ public void testAcking() throws InterruptedException { ) ) { - final ClusterState initialClusterState = ClusterState.builder(new ClusterName(ClusterManagerServiceTests.class.getSimpleName())) + final ClusterState initialClusterState = ClusterState.builder(new ClusterName(MasterServiceTests.class.getSimpleName())) .nodes(DiscoveryNodes.builder().add(node1).add(node2).add(node3).localNodeId(node1.getId()).masterNodeId(node1.getId())) .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK) .build(); @@ -1103,7 +1101,7 @@ public void onAckTimeout() { /** * Returns the cluster state that the cluster-manager service uses (and that is provided by the discovery layer) */ - public static ClusterState discoveryState(ClusterManagerService clusterManagerService) { + public static ClusterState discoveryState(MasterService clusterManagerService) { return clusterManagerService.state(); } diff --git a/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java b/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java index 5588e9c1ceba8..efcefab6c9f8b 100644 --- a/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java +++ b/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java @@ -37,7 +37,7 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.routing.RerouteService; import org.opensearch.cluster.service.ClusterApplier; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.io.stream.NamedWriteableRegistry; import org.opensearch.common.network.NetworkService; import org.opensearch.common.settings.ClusterSettings; @@ -70,7 +70,7 @@ public class DiscoveryModuleTests extends OpenSearchTestCase { private TransportService transportService; private NamedWriteableRegistry namedWriteableRegistry; - private ClusterManagerService clusterManagerService; + private MasterService clusterManagerService; private ClusterApplier clusterApplier; private ThreadPool threadPool; private ClusterSettings clusterSettings; @@ -93,7 +93,7 @@ public void setupDummyServices() { threadPool = mock(ThreadPool.class); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); transportService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null); - clusterManagerService = mock(ClusterManagerService.class); + clusterManagerService = mock(MasterService.class); namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); clusterApplier = mock(ClusterApplier.class); clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); diff --git a/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java b/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java index 6faaa6973953c..9558e898f8832 100644 --- a/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java +++ b/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java @@ -144,8 +144,8 @@ import org.opensearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand; import org.opensearch.cluster.service.ClusterApplierService; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.cluster.service.FakeThreadPoolClusterManagerService; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.FakeThreadPoolMasterService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.CheckedConsumer; import org.opensearch.common.Nullable; import org.opensearch.common.io.stream.NamedWriteableRegistry; @@ -1643,7 +1643,7 @@ private final class TestClusterNode { private final DiscoveryNode node; - private final ClusterManagerService clusterManagerService; + private final MasterService clusterManagerService; private final AllocationService allocationService; @@ -1663,7 +1663,7 @@ private final class TestClusterNode { this.node = node; final Environment environment = createEnvironment(node.getName()); threadPool = deterministicTaskQueue.getThreadPool(runnable -> CoordinatorTests.onNodeLog(node, runnable)); - clusterManagerService = new FakeThreadPoolClusterManagerService( + clusterManagerService = new FakeThreadPoolMasterService( node.getName(), "test", threadPool, diff --git a/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java b/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java index 0e0b466408f77..fb18aa1b5749b 100644 --- a/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java +++ b/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java @@ -54,7 +54,7 @@ import org.opensearch.cluster.routing.allocation.AllocationService; import org.opensearch.cluster.service.ClusterApplierService; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.cluster.service.FakeThreadPoolClusterManagerService; +import org.opensearch.cluster.service.FakeThreadPoolMasterService; import org.opensearch.common.Nullable; import org.opensearch.common.Randomness; import org.opensearch.common.UUIDs; @@ -1025,7 +1025,7 @@ class ClusterNode { private final DiscoveryNode localNode; final MockPersistedState persistedState; final Settings nodeSettings; - private AckedFakeThreadPoolClusterManagerService clusterManagerService; + private AckedFakeThreadPoolMasterService clusterManagerService; private DisruptableClusterApplierService clusterApplierService; private ClusterService clusterService; TransportService transportService; @@ -1105,7 +1105,7 @@ protected Optional getDisruptableMockTransport(Transpo null, emptySet() ); - clusterManagerService = new AckedFakeThreadPoolClusterManagerService( + clusterManagerService = new AckedFakeThreadPoolMasterService( localNode.getId(), "test", threadPool, @@ -1510,11 +1510,11 @@ int getSuccessfulAckIndex(ClusterNode clusterNode) { } } - static class AckedFakeThreadPoolClusterManagerService extends FakeThreadPoolClusterManagerService { + static class AckedFakeThreadPoolMasterService extends FakeThreadPoolMasterService { AckCollector nextAckCollector = new AckCollector(); - AckedFakeThreadPoolClusterManagerService( + AckedFakeThreadPoolMasterService( String nodeName, String serviceName, ThreadPool threadPool, diff --git a/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerService.java b/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerService.java deleted file mode 100644 index 46893fe08bd76..0000000000000 --- a/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerService.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.cluster.service; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.opensearch.action.ActionListener; -import org.opensearch.cluster.ClusterChangedEvent; -import org.opensearch.cluster.ClusterState; -import org.opensearch.cluster.coordination.ClusterStatePublisher.AckListener; -import org.opensearch.common.UUIDs; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Settings; -import org.opensearch.common.unit.TimeValue; -import org.opensearch.common.util.concurrent.OpenSearchExecutors; -import org.opensearch.common.util.concurrent.PrioritizedOpenSearchThreadPoolExecutor; -import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.node.Node; -import org.opensearch.threadpool.ThreadPool; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; - -import static org.apache.lucene.tests.util.LuceneTestCase.random; -import static org.opensearch.test.OpenSearchTestCase.randomInt; - -public class FakeThreadPoolClusterManagerService extends ClusterManagerService { - private static final Logger logger = LogManager.getLogger(FakeThreadPoolClusterManagerService.class); - - private final String name; - private final List pendingTasks = new ArrayList<>(); - private final Consumer onTaskAvailableToRun; - private boolean scheduledNextTask = false; - private boolean taskInProgress = false; - private boolean waitForPublish = false; - - public FakeThreadPoolClusterManagerService( - String nodeName, - String serviceName, - ThreadPool threadPool, - Consumer onTaskAvailableToRun - ) { - super( - Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), nodeName).build(), - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - threadPool - ); - this.name = serviceName; - this.onTaskAvailableToRun = onTaskAvailableToRun; - } - - @Override - protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { - return new PrioritizedOpenSearchThreadPoolExecutor( - name, - 1, - 1, - 1, - TimeUnit.SECONDS, - OpenSearchExecutors.daemonThreadFactory(name), - null, - null - ) { - - @Override - public void execute(Runnable command, final TimeValue timeout, final Runnable timeoutCallback) { - execute(command); - } - - @Override - public void execute(Runnable command) { - pendingTasks.add(command); - scheduleNextTaskIfNecessary(); - } - }; - } - - public int getFakeMasterServicePendingTaskCount() { - return pendingTasks.size(); - } - - private void scheduleNextTaskIfNecessary() { - if (taskInProgress == false && pendingTasks.isEmpty() == false && scheduledNextTask == false) { - scheduledNextTask = true; - onTaskAvailableToRun.accept(new Runnable() { - @Override - public String toString() { - return "cluster-manager service scheduling next task"; - } - - @Override - public void run() { - assert taskInProgress == false; - assert waitForPublish == false; - assert scheduledNextTask; - final int taskIndex = randomInt(pendingTasks.size() - 1); - logger.debug("next cluster-manager service task: choosing task {} of {}", taskIndex, pendingTasks.size()); - final Runnable task = pendingTasks.remove(taskIndex); - taskInProgress = true; - scheduledNextTask = false; - final ThreadContext threadContext = threadPool.getThreadContext(); - try (ThreadContext.StoredContext ignored = threadContext.stashContext()) { - threadContext.markAsSystemContext(); - task.run(); - } - if (waitForPublish == false) { - taskInProgress = false; - } - FakeThreadPoolClusterManagerService.this.scheduleNextTaskIfNecessary(); - } - }); - } - } - - @Override - public ClusterState.Builder incrementVersion(ClusterState clusterState) { - // generate cluster UUID deterministically for repeatable tests - return ClusterState.builder(clusterState).incrementVersion().stateUUID(UUIDs.randomBase64UUID(random())); - } - - @Override - protected void publish(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeMillis) { - assert waitForPublish == false; - waitForPublish = true; - final AckListener ackListener = taskOutputs.createAckListener(threadPool, clusterChangedEvent.state()); - final ActionListener publishListener = new ActionListener() { - - private boolean listenerCalled = false; - - @Override - public void onResponse(Void aVoid) { - assert listenerCalled == false; - listenerCalled = true; - assert waitForPublish; - waitForPublish = false; - try { - onPublicationSuccess(clusterChangedEvent, taskOutputs); - } finally { - taskInProgress = false; - scheduleNextTaskIfNecessary(); - } - } - - @Override - public void onFailure(Exception e) { - assert listenerCalled == false; - listenerCalled = true; - assert waitForPublish; - waitForPublish = false; - try { - onPublicationFailed(clusterChangedEvent, taskOutputs, startTimeMillis, e); - } finally { - taskInProgress = false; - scheduleNextTaskIfNecessary(); - } - } - }; - threadPool.generic().execute(threadPool.getThreadContext().preserveContext(new Runnable() { - @Override - public void run() { - clusterStatePublisher.publish(clusterChangedEvent, publishListener, wrapAckListener(ackListener)); - } - - @Override - public String toString() { - return "publish change of cluster state from version [" - + clusterChangedEvent.previousState().version() - + "] in term [" - + clusterChangedEvent.previousState().term() - + "] to version [" - + clusterChangedEvent.state().version() - + "] in term [" - + clusterChangedEvent.state().term() - + "]"; - } - })); - } - - protected AckListener wrapAckListener(AckListener ackListener) { - return ackListener; - } -} diff --git a/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java b/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java index da23714771eb6..c532a0cc36472 100644 --- a/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java +++ b/test/framework/src/main/java/org/opensearch/cluster/service/FakeThreadPoolMasterService.java @@ -31,15 +31,39 @@ package org.opensearch.cluster.service; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.action.ActionListener; +import org.opensearch.cluster.ClusterChangedEvent; +import org.opensearch.cluster.ClusterState; +import org.opensearch.cluster.coordination.ClusterStatePublisher.AckListener; +import org.opensearch.common.UUIDs; +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Settings; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.common.util.concurrent.OpenSearchExecutors; +import org.opensearch.common.util.concurrent.PrioritizedOpenSearchThreadPoolExecutor; +import org.opensearch.common.util.concurrent.ThreadContext; +import org.opensearch.node.Node; import org.opensearch.threadpool.ThreadPool; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; -/** - * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link FakeThreadPoolClusterManagerService} - */ -@Deprecated -public class FakeThreadPoolMasterService extends FakeThreadPoolClusterManagerService { +import static org.apache.lucene.tests.util.LuceneTestCase.random; +import static org.opensearch.test.OpenSearchTestCase.randomInt; + +public class FakeThreadPoolMasterService extends MasterService { + private static final Logger logger = LogManager.getLogger(FakeThreadPoolMasterService.class); + + private final String name; + private final List pendingTasks = new ArrayList<>(); + private final Consumer onTaskAvailableToRun; + private boolean scheduledNextTask = false; + private boolean taskInProgress = false; + private boolean waitForPublish = false; public FakeThreadPoolMasterService( String nodeName, @@ -47,7 +71,143 @@ public FakeThreadPoolMasterService( ThreadPool threadPool, Consumer onTaskAvailableToRun ) { - super(nodeName, serviceName, threadPool, onTaskAvailableToRun); + super( + Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), nodeName).build(), + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), + threadPool + ); + this.name = serviceName; + this.onTaskAvailableToRun = onTaskAvailableToRun; } + @Override + protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { + return new PrioritizedOpenSearchThreadPoolExecutor( + name, + 1, + 1, + 1, + TimeUnit.SECONDS, + OpenSearchExecutors.daemonThreadFactory(name), + null, + null + ) { + + @Override + public void execute(Runnable command, final TimeValue timeout, final Runnable timeoutCallback) { + execute(command); + } + + @Override + public void execute(Runnable command) { + pendingTasks.add(command); + scheduleNextTaskIfNecessary(); + } + }; + } + + public int getFakeMasterServicePendingTaskCount() { + return pendingTasks.size(); + } + + private void scheduleNextTaskIfNecessary() { + if (taskInProgress == false && pendingTasks.isEmpty() == false && scheduledNextTask == false) { + scheduledNextTask = true; + onTaskAvailableToRun.accept(new Runnable() { + @Override + public String toString() { + return "cluster-manager service scheduling next task"; + } + + @Override + public void run() { + assert taskInProgress == false; + assert waitForPublish == false; + assert scheduledNextTask; + final int taskIndex = randomInt(pendingTasks.size() - 1); + logger.debug("next cluster-manager service task: choosing task {} of {}", taskIndex, pendingTasks.size()); + final Runnable task = pendingTasks.remove(taskIndex); + taskInProgress = true; + scheduledNextTask = false; + final ThreadContext threadContext = threadPool.getThreadContext(); + try (ThreadContext.StoredContext ignored = threadContext.stashContext()) { + threadContext.markAsSystemContext(); + task.run(); + } + if (waitForPublish == false) { + taskInProgress = false; + } + FakeThreadPoolMasterService.this.scheduleNextTaskIfNecessary(); + } + }); + } + } + + @Override + public ClusterState.Builder incrementVersion(ClusterState clusterState) { + // generate cluster UUID deterministically for repeatable tests + return ClusterState.builder(clusterState).incrementVersion().stateUUID(UUIDs.randomBase64UUID(random())); + } + + @Override + protected void publish(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeMillis) { + assert waitForPublish == false; + waitForPublish = true; + final AckListener ackListener = taskOutputs.createAckListener(threadPool, clusterChangedEvent.state()); + final ActionListener publishListener = new ActionListener() { + + private boolean listenerCalled = false; + + @Override + public void onResponse(Void aVoid) { + assert listenerCalled == false; + listenerCalled = true; + assert waitForPublish; + waitForPublish = false; + try { + onPublicationSuccess(clusterChangedEvent, taskOutputs); + } finally { + taskInProgress = false; + scheduleNextTaskIfNecessary(); + } + } + + @Override + public void onFailure(Exception e) { + assert listenerCalled == false; + listenerCalled = true; + assert waitForPublish; + waitForPublish = false; + try { + onPublicationFailed(clusterChangedEvent, taskOutputs, startTimeMillis, e); + } finally { + taskInProgress = false; + scheduleNextTaskIfNecessary(); + } + } + }; + threadPool.generic().execute(threadPool.getThreadContext().preserveContext(new Runnable() { + @Override + public void run() { + clusterStatePublisher.publish(clusterChangedEvent, publishListener, wrapAckListener(ackListener)); + } + + @Override + public String toString() { + return "publish change of cluster state from version [" + + clusterChangedEvent.previousState().version() + + "] in term [" + + clusterChangedEvent.previousState().term() + + "] to version [" + + clusterChangedEvent.state().version() + + "] in term [" + + clusterChangedEvent.state().term() + + "]"; + } + })); + } + + protected AckListener wrapAckListener(AckListener ackListener) { + return ackListener; + } } diff --git a/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java b/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java index 93c437d3cf91b..f709d8bcaff34 100644 --- a/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java +++ b/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java @@ -47,7 +47,7 @@ import org.opensearch.cluster.service.ClusterApplier.ClusterApplyListener; import org.opensearch.cluster.service.ClusterApplierService; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.cluster.service.ClusterManagerService; +import org.opensearch.cluster.service.MasterService; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.node.Node; @@ -61,8 +61,8 @@ public class ClusterServiceUtils { - public static ClusterManagerService createMasterService(ThreadPool threadPool, ClusterState initialClusterState) { - ClusterManagerService clusterManagerService = new ClusterManagerService( + public static MasterService createMasterService(ThreadPool threadPool, ClusterState initialClusterState) { + MasterService clusterManagerService = new MasterService( Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test_cluster_manager_node").build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool @@ -77,7 +77,7 @@ public static ClusterManagerService createMasterService(ThreadPool threadPool, C return clusterManagerService; } - public static ClusterManagerService createMasterService(ThreadPool threadPool, DiscoveryNode localNode) { + public static MasterService createMasterService(ThreadPool threadPool, DiscoveryNode localNode) { ClusterState initialClusterState = ClusterState.builder(new ClusterName(ClusterServiceUtils.class.getSimpleName())) .nodes(DiscoveryNodes.builder().add(localNode).localNodeId(localNode.getId()).masterNodeId(localNode.getId())) .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK) @@ -114,7 +114,7 @@ public void onFailure(String source, Exception e) { } } - public static void setState(ClusterManagerService executor, ClusterState clusterState) { + public static void setState(MasterService executor, ClusterState clusterState) { CountDownLatch latch = new CountDownLatch(1); executor.submitStateUpdateTask("test setting state", new ClusterStateUpdateTask() { @Override diff --git a/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerServiceTests.java b/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java similarity index 97% rename from test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerServiceTests.java rename to test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java index f09e62fa574e6..dabeeb6742ec4 100644 --- a/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolClusterManagerServiceTests.java +++ b/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java @@ -61,7 +61,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class FakeThreadPoolClusterManagerServiceTests extends OpenSearchTestCase { +public class FakeThreadPoolMasterServiceTests extends OpenSearchTestCase { public void testFakeClusterManagerService() { List runnableTasks = new ArrayList<>(); @@ -84,7 +84,7 @@ public void testFakeClusterManagerService() { doAnswer(invocationOnMock -> runnableTasks.add((Runnable) invocationOnMock.getArguments()[0])).when(executorService).execute(any()); when(mockThreadPool.generic()).thenReturn(executorService); - FakeThreadPoolClusterManagerService clusterManagerService = new FakeThreadPoolClusterManagerService( + FakeThreadPoolMasterService clusterManagerService = new FakeThreadPoolMasterService( "test_node", "test", mockThreadPool, From e4ca29195ae372bf3a26e2ea4c48a99ff2c36b71 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Thu, 14 Jul 2022 13:55:38 -0700 Subject: [PATCH 13/15] Revert adding a unit test for cluster setting in MasterService class Signed-off-by: Tianli Feng --- .../MasterServiceRenamedSettingTests.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java b/server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java index 0a3e6f48c5f10..acf089dc43b56 100644 --- a/server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java +++ b/server/src/test/java/org/opensearch/cluster/service/MasterServiceRenamedSettingTests.java @@ -40,23 +40,6 @@ public void testClusterManagerServiceSettingsExist() { ); } - /** - * Validate the both settings are known and supported, - * and validate the both settings can be called from the deprecated MasterService class. - */ - public void testMasterServiceSettingsExist() { - Set> settings = ClusterSettings.BUILT_IN_CLUSTER_SETTINGS; - assertTrue( - "Both 'cluster.service.slow_cluster_manager_task_logging_threshold' and its predecessor should be supported built-in settings", - settings.containsAll( - Arrays.asList( - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING - ) - ) - ); - } - /** * Validate the default value of the both settings is the same. */ From 84b1dfbde153af010daba3c7d5d3501d3c6c8b99 Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Thu, 14 Jul 2022 14:17:53 -0700 Subject: [PATCH 14/15] Revert renaming class BlockMasterServiceOnMaster and BusyMasterServiceDisruption Signed-off-by: Tianli Feng --- .../discovery/ClusterManagerDisruptionIT.java | 4 +- .../DedicatedClusterSnapshotRestoreIT.java | 4 +- ...ClusterManagerServiceOnClusterManager.java | 117 ------------------ .../BlockMasterServiceOnMaster.java | 81 +++++++++++- .../BusyClusterManagerServiceDisruption.java | 100 --------------- .../BusyMasterServiceDisruption.java | 64 +++++++++- 6 files changed, 138 insertions(+), 232 deletions(-) delete mode 100644 test/framework/src/main/java/org/opensearch/test/disruption/BlockClusterManagerServiceOnClusterManager.java delete mode 100644 test/framework/src/main/java/org/opensearch/test/disruption/BusyClusterManagerServiceDisruption.java diff --git a/server/src/internalClusterTest/java/org/opensearch/discovery/ClusterManagerDisruptionIT.java b/server/src/internalClusterTest/java/org/opensearch/discovery/ClusterManagerDisruptionIT.java index 6c055c4015ff0..2f19d1e476c94 100644 --- a/server/src/internalClusterTest/java/org/opensearch/discovery/ClusterManagerDisruptionIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/discovery/ClusterManagerDisruptionIT.java @@ -43,7 +43,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.common.xcontent.XContentType; import org.opensearch.test.OpenSearchIntegTestCase; -import org.opensearch.test.disruption.BlockClusterManagerServiceOnClusterManager; +import org.opensearch.test.disruption.BlockMasterServiceOnMaster; import org.opensearch.test.disruption.IntermittentLongGCDisruption; import org.opensearch.test.disruption.NetworkDisruption; import org.opensearch.test.disruption.NetworkDisruption.TwoPartitions; @@ -308,7 +308,7 @@ public void testMappingTimeout() throws Exception { .setTransientSettings(Settings.builder().put("indices.mapping.dynamic_timeout", "1ms")) ); - ServiceDisruptionScheme disruption = new BlockClusterManagerServiceOnClusterManager(random()); + ServiceDisruptionScheme disruption = new BlockMasterServiceOnMaster(random()); setDisruptionScheme(disruption); disruption.startDisrupting(); diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/DedicatedClusterSnapshotRestoreIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/DedicatedClusterSnapshotRestoreIT.java index a4afdc0dba85c..2eca8555e1388 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/DedicatedClusterSnapshotRestoreIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/DedicatedClusterSnapshotRestoreIT.java @@ -94,7 +94,7 @@ import org.opensearch.test.OpenSearchIntegTestCase.Scope; import org.opensearch.test.InternalTestCluster; import org.opensearch.test.TestCustomMetadata; -import org.opensearch.test.disruption.BusyClusterManagerServiceDisruption; +import org.opensearch.test.disruption.BusyMasterServiceDisruption; import org.opensearch.test.disruption.ServiceDisruptionScheme; import org.opensearch.test.rest.FakeRestRequest; import org.opensearch.test.transport.MockTransportService; @@ -1157,7 +1157,7 @@ public void testDataNodeRestartWithBusyClusterManagerDuringSnapshot() throws Exc final String dataNode = blockNodeWithIndex("test-repo", "test-idx"); logger.info("--> snapshot"); - ServiceDisruptionScheme disruption = new BusyClusterManagerServiceDisruption(random(), Priority.HIGH); + ServiceDisruptionScheme disruption = new BusyMasterServiceDisruption(random(), Priority.HIGH); setDisruptionScheme(disruption); client(internalCluster().getMasterName()).admin() .cluster() diff --git a/test/framework/src/main/java/org/opensearch/test/disruption/BlockClusterManagerServiceOnClusterManager.java b/test/framework/src/main/java/org/opensearch/test/disruption/BlockClusterManagerServiceOnClusterManager.java deleted file mode 100644 index 8edb508625341..0000000000000 --- a/test/framework/src/main/java/org/opensearch/test/disruption/BlockClusterManagerServiceOnClusterManager.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.test.disruption; - -import org.apache.logging.log4j.core.util.Throwables; -import org.opensearch.cluster.ClusterState; -import org.opensearch.cluster.ClusterStateUpdateTask; -import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.Priority; -import org.opensearch.common.unit.TimeValue; -import org.opensearch.test.InternalTestCluster; - -import java.util.Random; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicReference; - -public class BlockClusterManagerServiceOnClusterManager extends SingleNodeDisruption { - - AtomicReference disruptionLatch = new AtomicReference<>(); - - public BlockClusterManagerServiceOnClusterManager(Random random) { - super(random); - } - - @Override - public void startDisrupting() { - disruptedNode = cluster.getMasterName(); - final String disruptionNodeCopy = disruptedNode; - if (disruptionNodeCopy == null) { - return; - } - ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptionNodeCopy); - if (clusterService == null) { - return; - } - logger.info("blocking cluster-manager service on node [{}]", disruptionNodeCopy); - boolean success = disruptionLatch.compareAndSet(null, new CountDownLatch(1)); - assert success : "startDisrupting called without waiting on stopDisrupting to complete"; - final CountDownLatch started = new CountDownLatch(1); - clusterService.getMasterService().submitStateUpdateTask("service_disruption_block", new ClusterStateUpdateTask() { - @Override - public Priority priority() { - return Priority.IMMEDIATE; - } - - @Override - public ClusterState execute(ClusterState currentState) throws Exception { - started.countDown(); - CountDownLatch latch = disruptionLatch.get(); - if (latch != null) { - try { - latch.await(); - } catch (InterruptedException e) { - Throwables.rethrow(e); - } - } - return currentState; - } - - @Override - public void onFailure(String source, Exception e) { - logger.error("unexpected error during disruption", e); - } - }); - try { - started.await(); - } catch (InterruptedException e) {} - } - - @Override - public void stopDisrupting() { - CountDownLatch latch = disruptionLatch.get(); - if (latch != null) { - latch.countDown(); - } - - } - - @Override - public void removeAndEnsureHealthy(InternalTestCluster cluster) { - removeFromCluster(cluster); - } - - @Override - public TimeValue expectedTimeToHeal() { - return TimeValue.timeValueMinutes(0); - } -} diff --git a/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java b/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java index 3fd3843f9c4c3..85f8e5c250066 100644 --- a/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java +++ b/test/framework/src/main/java/org/opensearch/test/disruption/BlockMasterServiceOnMaster.java @@ -31,16 +31,87 @@ package org.opensearch.test.disruption; +import org.apache.logging.log4j.core.util.Throwables; +import org.opensearch.cluster.ClusterState; +import org.opensearch.cluster.ClusterStateUpdateTask; +import org.opensearch.cluster.service.ClusterService; +import org.opensearch.common.Priority; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.test.InternalTestCluster; + import java.util.Random; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReference; -/** - * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link BlockClusterManagerServiceOnClusterManager} - */ -@Deprecated -public class BlockMasterServiceOnMaster extends BlockClusterManagerServiceOnClusterManager { +public class BlockMasterServiceOnMaster extends SingleNodeDisruption { + + AtomicReference disruptionLatch = new AtomicReference<>(); public BlockMasterServiceOnMaster(Random random) { super(random); } + @Override + public void startDisrupting() { + disruptedNode = cluster.getMasterName(); + final String disruptionNodeCopy = disruptedNode; + if (disruptionNodeCopy == null) { + return; + } + ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptionNodeCopy); + if (clusterService == null) { + return; + } + logger.info("blocking cluster-manager service on node [{}]", disruptionNodeCopy); + boolean success = disruptionLatch.compareAndSet(null, new CountDownLatch(1)); + assert success : "startDisrupting called without waiting on stopDisrupting to complete"; + final CountDownLatch started = new CountDownLatch(1); + clusterService.getMasterService().submitStateUpdateTask("service_disruption_block", new ClusterStateUpdateTask() { + @Override + public Priority priority() { + return Priority.IMMEDIATE; + } + + @Override + public ClusterState execute(ClusterState currentState) throws Exception { + started.countDown(); + CountDownLatch latch = disruptionLatch.get(); + if (latch != null) { + try { + latch.await(); + } catch (InterruptedException e) { + Throwables.rethrow(e); + } + } + return currentState; + } + + @Override + public void onFailure(String source, Exception e) { + logger.error("unexpected error during disruption", e); + } + }); + try { + started.await(); + } catch (InterruptedException e) {} + } + + @Override + public void stopDisrupting() { + CountDownLatch latch = disruptionLatch.get(); + if (latch != null) { + latch.countDown(); + } + + } + + @Override + public void removeAndEnsureHealthy(InternalTestCluster cluster) { + removeFromCluster(cluster); + } + + @Override + public TimeValue expectedTimeToHeal() { + return TimeValue.timeValueMinutes(0); + } } diff --git a/test/framework/src/main/java/org/opensearch/test/disruption/BusyClusterManagerServiceDisruption.java b/test/framework/src/main/java/org/opensearch/test/disruption/BusyClusterManagerServiceDisruption.java deleted file mode 100644 index 55eb90a4dc89d..0000000000000 --- a/test/framework/src/main/java/org/opensearch/test/disruption/BusyClusterManagerServiceDisruption.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.test.disruption; - -import org.opensearch.cluster.ClusterState; -import org.opensearch.cluster.ClusterStateUpdateTask; -import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.Priority; -import org.opensearch.common.unit.TimeValue; -import org.opensearch.test.InternalTestCluster; - -import java.util.Random; -import java.util.concurrent.atomic.AtomicBoolean; - -public class BusyClusterManagerServiceDisruption extends SingleNodeDisruption { - private final AtomicBoolean active = new AtomicBoolean(); - private final Priority priority; - - public BusyClusterManagerServiceDisruption(Random random, Priority priority) { - super(random); - this.priority = priority; - } - - @Override - public void startDisrupting() { - disruptedNode = cluster.getMasterName(); - final String disruptionNodeCopy = disruptedNode; - if (disruptionNodeCopy == null) { - return; - } - ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptionNodeCopy); - if (clusterService == null) { - return; - } - logger.info("making cluster-manager service busy on node [{}] at priority [{}]", disruptionNodeCopy, priority); - active.set(true); - submitTask(clusterService); - } - - private void submitTask(ClusterService clusterService) { - clusterService.getMasterService().submitStateUpdateTask("service_disruption_block", new ClusterStateUpdateTask(priority) { - @Override - public ClusterState execute(ClusterState currentState) { - if (active.get()) { - submitTask(clusterService); - } - return currentState; - } - - @Override - public void onFailure(String source, Exception e) { - logger.error("unexpected error during disruption", e); - } - }); - } - - @Override - public void stopDisrupting() { - active.set(false); - } - - @Override - public void removeAndEnsureHealthy(InternalTestCluster cluster) { - removeFromCluster(cluster); - } - - @Override - public TimeValue expectedTimeToHeal() { - return TimeValue.timeValueMinutes(0); - } -} diff --git a/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java b/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java index 1a88856489907..4830f9b0359fb 100644 --- a/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java +++ b/test/framework/src/main/java/org/opensearch/test/disruption/BusyMasterServiceDisruption.java @@ -31,18 +31,70 @@ package org.opensearch.test.disruption; +import org.opensearch.cluster.ClusterState; +import org.opensearch.cluster.ClusterStateUpdateTask; +import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.Priority; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.test.InternalTestCluster; import java.util.Random; +import java.util.concurrent.atomic.AtomicBoolean; -/** - * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link BusyClusterManagerServiceDisruption} - */ -@Deprecated -public class BusyMasterServiceDisruption extends BusyClusterManagerServiceDisruption { +public class BusyMasterServiceDisruption extends SingleNodeDisruption { + private final AtomicBoolean active = new AtomicBoolean(); + private final Priority priority; public BusyMasterServiceDisruption(Random random, Priority priority) { - super(random, priority); + super(random); + this.priority = priority; + } + + @Override + public void startDisrupting() { + disruptedNode = cluster.getMasterName(); + final String disruptionNodeCopy = disruptedNode; + if (disruptionNodeCopy == null) { + return; + } + ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptionNodeCopy); + if (clusterService == null) { + return; + } + logger.info("making cluster-manager service busy on node [{}] at priority [{}]", disruptionNodeCopy, priority); + active.set(true); + submitTask(clusterService); + } + + private void submitTask(ClusterService clusterService) { + clusterService.getMasterService().submitStateUpdateTask("service_disruption_block", new ClusterStateUpdateTask(priority) { + @Override + public ClusterState execute(ClusterState currentState) { + if (active.get()) { + submitTask(clusterService); + } + return currentState; + } + + @Override + public void onFailure(String source, Exception e) { + logger.error("unexpected error during disruption", e); + } + }); } + @Override + public void stopDisrupting() { + active.set(false); + } + + @Override + public void removeAndEnsureHealthy(InternalTestCluster cluster) { + removeFromCluster(cluster); + } + + @Override + public TimeValue expectedTimeToHeal() { + return TimeValue.timeValueMinutes(0); + } } From b10e291c6de0b867bc94b2103e91c6a7c18bf24d Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Thu, 14 Jul 2022 14:39:13 -0700 Subject: [PATCH 15/15] Revert renaming some variable name of MasterService Signed-off-by: Tianli Feng --- .../cluster/coordination/Coordinator.java | 16 ++-- .../cluster/coordination/JoinHelper.java | 10 +-- .../cluster/service/ClusterService.java | 16 ++-- .../opensearch/discovery/DiscoveryModule.java | 4 +- ...rnalClusterInfoServiceSchedulingTests.java | 8 +- .../cluster/coordination/NodeJoinTests.java | 30 +++---- .../cluster/service/MasterServiceTests.java | 78 +++++++++---------- .../discovery/DiscoveryModuleTests.java | 6 +- .../snapshots/SnapshotResiliencyTests.java | 15 ++-- .../AbstractCoordinatorTestCase.java | 16 ++-- .../opensearch/test/ClusterServiceUtils.java | 10 +-- .../FakeThreadPoolMasterServiceTests.java | 12 +-- 12 files changed, 108 insertions(+), 113 deletions(-) diff --git a/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java b/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java index 611261320a453..56d585d81e9dc 100644 --- a/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java +++ b/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java @@ -141,7 +141,7 @@ public class Coordinator extends AbstractLifecycleComponent implements Discovery private final boolean singleNodeDiscovery; private final ElectionStrategy electionStrategy; private final TransportService transportService; - private final MasterService clusterManagerService; + private final MasterService masterService; private final AllocationService allocationService; private final JoinHelper joinHelper; private final NodeRemovalClusterStateTaskExecutor nodeRemovalExecutor; @@ -191,7 +191,7 @@ public Coordinator( TransportService transportService, NamedWriteableRegistry namedWriteableRegistry, AllocationService allocationService, - MasterService clusterManagerService, + MasterService masterService, Supplier persistedStateSupplier, SeedHostsProvider seedHostsProvider, ClusterApplier clusterApplier, @@ -203,7 +203,7 @@ public Coordinator( ) { this.settings = settings; this.transportService = transportService; - this.clusterManagerService = clusterManagerService; + this.masterService = masterService; this.allocationService = allocationService; this.onJoinValidators = JoinTaskExecutor.addBuiltInJoinValidators(onJoinValidators); this.singleNodeDiscovery = DiscoveryModule.isSingleNodeDiscovery(settings); @@ -211,7 +211,7 @@ public Coordinator( this.joinHelper = new JoinHelper( settings, allocationService, - clusterManagerService, + masterService, transportService, this::getCurrentTerm, this::getStateForClusterManagerService, @@ -260,7 +260,7 @@ public Coordinator( ); this.nodeRemovalExecutor = new NodeRemovalClusterStateTaskExecutor(allocationService, logger); this.clusterApplier = clusterApplier; - clusterManagerService.setClusterStateSupplier(this::getStateForClusterManagerService); + masterService.setClusterStateSupplier(this::getStateForClusterManagerService); this.reconfigurator = new Reconfigurator(settings, clusterSettings); this.clusterBootstrapService = new ClusterBootstrapService( settings, @@ -310,7 +310,7 @@ private void onLeaderFailure(Exception e) { private void removeNode(DiscoveryNode discoveryNode, String reason) { synchronized (mutex) { if (mode == Mode.LEADER) { - clusterManagerService.submitStateUpdateTask( + masterService.submitStateUpdateTask( "node-left", new NodeRemovalClusterStateTaskExecutor.Task(discoveryNode, reason), ClusterStateTaskConfig.build(Priority.IMMEDIATE), @@ -756,7 +756,7 @@ void becomeFollower(String method, DiscoveryNode leaderNode) { } private void cleanClusterManagerService() { - clusterManagerService.submitStateUpdateTask("clean-up after stepping down as cluster-manager", new LocalClusterUpdateTask() { + masterService.submitStateUpdateTask("clean-up after stepping down as cluster-manager", new LocalClusterUpdateTask() { @Override public void onFailure(String source, Exception e) { // ignore @@ -1126,7 +1126,7 @@ private void scheduleReconfigurationIfNeeded() { final ClusterState state = getLastAcceptedState(); if (improveConfiguration(state) != state && reconfigurationTaskScheduled.compareAndSet(false, true)) { logger.trace("scheduling reconfiguration"); - clusterManagerService.submitStateUpdateTask("reconfigure", new ClusterStateUpdateTask(Priority.URGENT) { + masterService.submitStateUpdateTask("reconfigure", new ClusterStateUpdateTask(Priority.URGENT) { @Override public ClusterState execute(ClusterState currentState) { reconfigurationTaskScheduled.set(false); diff --git a/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java b/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java index 18a9579547d56..3accc4f1d5baf 100644 --- a/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java +++ b/server/src/main/java/org/opensearch/cluster/coordination/JoinHelper.java @@ -106,7 +106,7 @@ public class JoinHelper { Setting.Property.Deprecated ); - private final MasterService clusterManagerService; + private final MasterService masterService; private final TransportService transportService; private volatile JoinTaskExecutor joinTaskExecutor; @@ -122,7 +122,7 @@ public class JoinHelper { JoinHelper( Settings settings, AllocationService allocationService, - MasterService clusterManagerService, + MasterService masterService, TransportService transportService, LongSupplier currentTermSupplier, Supplier currentStateSupplier, @@ -132,7 +132,7 @@ public class JoinHelper { RerouteService rerouteService, NodeHealthService nodeHealthService ) { - this.clusterManagerService = clusterManagerService; + this.masterService = masterService; this.transportService = transportService; this.nodeHealthService = nodeHealthService; this.joinTimeout = JOIN_TIMEOUT_SETTING.get(settings); @@ -454,7 +454,7 @@ class LeaderJoinAccumulator implements JoinAccumulator { public void handleJoinRequest(DiscoveryNode sender, JoinCallback joinCallback) { final JoinTaskExecutor.Task task = new JoinTaskExecutor.Task(sender, "join existing leader"); assert joinTaskExecutor != null; - clusterManagerService.submitStateUpdateTask( + masterService.submitStateUpdateTask( "node-join", task, ClusterStateTaskConfig.build(Priority.URGENT), @@ -539,7 +539,7 @@ public void close(Mode newMode) { pendingAsTasks.put(JoinTaskExecutor.newBecomeMasterTask(), (source, e) -> {}); pendingAsTasks.put(JoinTaskExecutor.newFinishElectionTask(), (source, e) -> {}); joinTaskExecutor = joinTaskExecutorGenerator.get(); - clusterManagerService.submitStateUpdateTasks( + masterService.submitStateUpdateTasks( stateUpdateSource, pendingAsTasks, ClusterStateTaskConfig.build(Priority.URGENT), diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index 9dc907f7afadd..baf453d18b3b3 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -62,7 +62,7 @@ * @opensearch.internal */ public class ClusterService extends AbstractLifecycleComponent { - private final MasterService clusterManagerService; + private final MasterService masterService; private final ClusterApplierService clusterApplierService; @@ -100,12 +100,12 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread public ClusterService( Settings settings, ClusterSettings clusterSettings, - MasterService clusterManagerService, + MasterService masterService, ClusterApplierService clusterApplierService ) { this.settings = settings; this.nodeName = Node.NODE_NAME_SETTING.get(settings); - this.clusterManagerService = clusterManagerService; + this.masterService = masterService; this.operationRouting = new OperationRouting(settings, clusterSettings); this.clusterSettings = clusterSettings; this.clusterName = ClusterName.CLUSTER_NAME_SETTING.get(settings); @@ -131,18 +131,18 @@ public RerouteService getRerouteService() { @Override protected synchronized void doStart() { clusterApplierService.start(); - clusterManagerService.start(); + masterService.start(); } @Override protected synchronized void doStop() { - clusterManagerService.stop(); + masterService.stop(); clusterApplierService.stop(); } @Override protected synchronized void doClose() { - clusterManagerService.close(); + masterService.close(); clusterApplierService.close(); } @@ -219,7 +219,7 @@ public void addLocalNodeMasterListener(LocalNodeClusterManagerListener listener) } public MasterService getMasterService() { - return clusterManagerService; + return masterService; } /** @@ -333,6 +333,6 @@ public void submitStateUpdateTasks( final ClusterStateTaskConfig config, final ClusterStateTaskExecutor executor ) { - clusterManagerService.submitStateUpdateTasks(source, tasks, config, executor); + masterService.submitStateUpdateTasks(source, tasks, config, executor); } } diff --git a/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java b/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java index c12283df1dbc9..6b746e5963bdc 100644 --- a/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java +++ b/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java @@ -121,7 +121,7 @@ public DiscoveryModule( TransportService transportService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService, - MasterService clusterManagerService, + MasterService masterService, ClusterApplier clusterApplier, ClusterSettings clusterSettings, List plugins, @@ -197,7 +197,7 @@ public DiscoveryModule( transportService, namedWriteableRegistry, allocationService, - clusterManagerService, + masterService, gatewayMetaState::getPersistedState, seedHostsProvider, clusterApplier, diff --git a/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java b/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java index 1c247a7b5a3da..7f09f1eb8e2be 100644 --- a/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java +++ b/server/src/test/java/org/opensearch/cluster/InternalClusterInfoServiceSchedulingTests.java @@ -87,14 +87,14 @@ protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { } }; - final MasterService clusterManagerService = new FakeThreadPoolMasterService( + final MasterService masterService = new FakeThreadPoolMasterService( "test", "clusterManagerService", threadPool, r -> { fail("cluster-manager service should not run any tasks"); } ); - final ClusterService clusterService = new ClusterService(settings, clusterSettings, clusterManagerService, clusterApplierService); + final ClusterService clusterService = new ClusterService(settings, clusterSettings, masterService, clusterApplierService); final FakeClusterInfoServiceClient client = new FakeClusterInfoServiceClient(threadPool); final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService(settings, clusterService, threadPool, client); @@ -103,8 +103,8 @@ protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { clusterService.setNodeConnectionsService(ClusterServiceUtils.createNoOpNodeConnectionsService()); clusterApplierService.setInitialState(ClusterState.builder(new ClusterName("cluster")).nodes(noClusterManager).build()); - clusterManagerService.setClusterStatePublisher((clusterChangedEvent, publishListener, ackListener) -> fail("should not publish")); - clusterManagerService.setClusterStateSupplier(clusterApplierService::state); + masterService.setClusterStatePublisher((clusterChangedEvent, publishListener, ackListener) -> fail("should not publish")); + masterService.setClusterStateSupplier(clusterApplierService::state); clusterService.start(); final AtomicBoolean becameClusterManager1 = new AtomicBoolean(); diff --git a/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java b/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java index 61a81a8c08389..806468ed5e761 100644 --- a/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java +++ b/server/src/test/java/org/opensearch/cluster/coordination/NodeJoinTests.java @@ -98,7 +98,7 @@ public class NodeJoinTests extends OpenSearchTestCase { private static ThreadPool threadPool; - private MasterService clusterManagerService; + private MasterService masterService; private Coordinator coordinator; private DeterministicTaskQueue deterministicTaskQueue; private Transport transport; @@ -117,7 +117,7 @@ public static void afterClass() { @After public void tearDown() throws Exception { super.tearDown(); - clusterManagerService.close(); + masterService.close(); } private static ClusterState initialState(DiscoveryNode localNode, long term, long version, VotingConfiguration config) { @@ -166,40 +166,40 @@ private void setupFakeClusterManagerServiceAndCoordinator(long term, ClusterStat } private void setupRealClusterManagerServiceAndCoordinator(long term, ClusterState initialState) { - MasterService clusterManagerService = new MasterService( + MasterService masterService = new MasterService( Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test_node").build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool ); AtomicReference clusterStateRef = new AtomicReference<>(initialState); - clusterManagerService.setClusterStatePublisher((event, publishListener, ackListener) -> { + masterService.setClusterStatePublisher((event, publishListener, ackListener) -> { clusterStateRef.set(event.state()); publishListener.onResponse(null); }); setupClusterManagerServiceAndCoordinator( term, initialState, - clusterManagerService, + masterService, threadPool, new Random(Randomness.get().nextLong()), () -> new StatusInfo(HEALTHY, "healthy-info") ); - clusterManagerService.setClusterStateSupplier(clusterStateRef::get); - clusterManagerService.start(); + masterService.setClusterStateSupplier(clusterStateRef::get); + masterService.start(); } private void setupClusterManagerServiceAndCoordinator( long term, ClusterState initialState, - MasterService clusterManagerService, + MasterService masterService, ThreadPool threadPool, Random random, NodeHealthService nodeHealthService ) { - if (this.clusterManagerService != null || coordinator != null) { + if (this.masterService != null || coordinator != null) { throw new IllegalStateException("method setupClusterManagerServiceAndCoordinator can only be called once"); } - this.clusterManagerService = clusterManagerService; + this.masterService = masterService; CapturingTransport capturingTransport = new CapturingTransport() { @Override protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode destination) { @@ -231,7 +231,7 @@ protected void onSendRequest(long requestId, String action, TransportRequest req transportService, writableRegistry(), OpenSearchAllocationTestCase.createAllocationService(Settings.EMPTY), - clusterManagerService, + masterService, () -> new InMemoryPersistedState(term, initialState), r -> emptyList(), new NoOpClusterApplier(), @@ -514,7 +514,7 @@ public void testJoinUpdateVotingConfigExclusion() throws Exception { ); assertTrue( - MasterServiceTests.discoveryState(clusterManagerService) + MasterServiceTests.discoveryState(masterService) .getVotingConfigExclusions() .stream() .anyMatch( @@ -746,7 +746,7 @@ public void testConcurrentJoining() { throw new RuntimeException(e); } - assertTrue(MasterServiceTests.discoveryState(clusterManagerService).nodes().isLocalNodeElectedMaster()); + assertTrue(MasterServiceTests.discoveryState(masterService).nodes().isLocalNodeElectedMaster()); for (DiscoveryNode successfulNode : successfulNodes) { assertTrue(successfulNode + " joined cluster", clusterStateHasNode(successfulNode)); assertFalse(successfulNode + " voted for cluster-manager", coordinator.missingJoinVoteFrom(successfulNode)); @@ -776,10 +776,10 @@ public void testJoinElectedLeaderWithDeprecatedMasterRole() { } private boolean isLocalNodeElectedMaster() { - return MasterServiceTests.discoveryState(clusterManagerService).nodes().isLocalNodeElectedMaster(); + return MasterServiceTests.discoveryState(masterService).nodes().isLocalNodeElectedMaster(); } private boolean clusterStateHasNode(DiscoveryNode node) { - return node.equals(MasterServiceTests.discoveryState(clusterManagerService).nodes().get(node.getId())); + return node.equals(MasterServiceTests.discoveryState(masterService).nodes().get(node.getId())); } } diff --git a/server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java b/server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java index d5f7344c544b9..c829bef576be5 100644 --- a/server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java +++ b/server/src/test/java/org/opensearch/cluster/service/MasterServiceTests.java @@ -123,7 +123,7 @@ public void randomizeCurrentTime() { private MasterService createClusterManagerService(boolean makeClusterManager) { final DiscoveryNode localNode = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); - final MasterService clusterManagerService = new MasterService( + final MasterService masterService = new MasterService( Settings.builder() .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()) .put(Node.NODE_NAME_SETTING.getKey(), "test_node") @@ -141,13 +141,13 @@ private MasterService createClusterManagerService(boolean makeClusterManager) { .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK) .build(); final AtomicReference clusterStateRef = new AtomicReference<>(initialClusterState); - clusterManagerService.setClusterStatePublisher((event, publishListener, ackListener) -> { + masterService.setClusterStatePublisher((event, publishListener, ackListener) -> { clusterStateRef.set(event.state()); publishListener.onResponse(null); }); - clusterManagerService.setClusterStateSupplier(clusterStateRef::get); - clusterManagerService.start(); - return clusterManagerService; + masterService.setClusterStateSupplier(clusterStateRef::get); + masterService.start(); + return masterService; } public void testClusterManagerAwareExecution() throws Exception { @@ -194,7 +194,7 @@ public void onFailure(String source, Exception e) { } public void testThreadContext() throws InterruptedException { - final MasterService clusterManager = createClusterManagerService(true); + final MasterService masterService = createClusterManagerService(true); final CountDownLatch latch = new CountDownLatch(1); try (ThreadContext.StoredContext ignored = threadPool.getThreadContext().stashContext()) { @@ -208,7 +208,7 @@ public void testThreadContext() throws InterruptedException { final TimeValue ackTimeout = randomBoolean() ? TimeValue.ZERO : TimeValue.timeValueMillis(randomInt(10000)); final TimeValue clusterManagerTimeout = randomBoolean() ? TimeValue.ZERO : TimeValue.timeValueMillis(randomInt(10000)); - clusterManager.submitStateUpdateTask("test", new AckedClusterStateUpdateTask(null, null) { + masterService.submitStateUpdateTask("test", new AckedClusterStateUpdateTask(null, null) { @Override public ClusterState execute(ClusterState currentState) { assertTrue(threadPool.getThreadContext().isSystemContext()); @@ -280,7 +280,7 @@ public void onAckTimeout() { latch.await(); - clusterManager.close(); + masterService.close(); } /* @@ -292,8 +292,8 @@ public void testClusterStateTaskListenerThrowingExceptionIsOkay() throws Interru final CountDownLatch latch = new CountDownLatch(1); AtomicBoolean published = new AtomicBoolean(); - try (MasterService clusterManagerService = createClusterManagerService(true)) { - clusterManagerService.submitStateUpdateTask( + try (MasterService masterService = createClusterManagerService(true)) { + masterService.submitStateUpdateTask( "testClusterStateTaskListenerThrowingExceptionIsOkay", new Object(), ClusterStateTaskConfig.build(Priority.NORMAL), @@ -421,8 +421,8 @@ public void testClusterStateUpdateLogging() throws Exception { ) ); - try (MasterService clusterManagerService = createClusterManagerService(true)) { - clusterManagerService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { + try (MasterService masterService = createClusterManagerService(true)) { + masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += TimeValue.timeValueSeconds(1).millis(); @@ -437,7 +437,7 @@ public void onFailure(String source, Exception e) { fail(); } }); - clusterManagerService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += TimeValue.timeValueSeconds(2).millis(); @@ -452,7 +452,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS @Override public void onFailure(String source, Exception e) {} }); - clusterManagerService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += TimeValue.timeValueSeconds(3).millis(); @@ -469,7 +469,7 @@ public void onFailure(String source, Exception e) { fail(); } }); - clusterManagerService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { return currentState; @@ -617,7 +617,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS } }; - try (MasterService clusterManagerService = createClusterManagerService(true)) { + try (MasterService masterService = createClusterManagerService(true)) { final ConcurrentMap submittedTasksPerThread = new ConcurrentHashMap<>(); CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads); for (int i = 0; i < numberOfThreads; i++) { @@ -632,7 +632,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS submittedTasksPerThread.computeIfAbsent(threadName, key -> new AtomicInteger()).addAndGet(tasks.size()); final TaskExecutor executor = assignment.v1(); if (tasks.size() == 1) { - clusterManagerService.submitStateUpdateTask( + masterService.submitStateUpdateTask( threadName, tasks.stream().findFirst().get(), ClusterStateTaskConfig.build(randomFrom(Priority.values())), @@ -642,7 +642,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS } else { Map taskListeners = new HashMap<>(); tasks.forEach(t -> taskListeners.put(t, listener)); - clusterManagerService.submitStateUpdateTasks( + masterService.submitStateUpdateTasks( threadName, taskListeners, ClusterStateTaskConfig.build(randomFrom(Priority.values())), @@ -696,8 +696,8 @@ public void testBlockingCallInClusterStateTaskListenerFails() throws Interrupted final CountDownLatch latch = new CountDownLatch(1); final AtomicReference assertionRef = new AtomicReference<>(); - try (MasterService clusterManagerService = createClusterManagerService(true)) { - clusterManagerService.submitStateUpdateTask( + try (MasterService masterService = createClusterManagerService(true)) { + masterService.submitStateUpdateTask( "testBlockingCallInClusterStateTaskListenerFails", new Object(), ClusterStateTaskConfig.build(Priority.NORMAL), @@ -788,7 +788,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { ); try ( - MasterService clusterManagerService = new MasterService( + MasterService masterService = new MasterService( Settings.builder() .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()) .put(Node.NODE_NAME_SETTING.getKey(), "test_node") @@ -810,7 +810,7 @@ public void testLongClusterStateUpdateLogging() throws Exception { .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK) .build(); final AtomicReference clusterStateRef = new AtomicReference<>(initialClusterState); - clusterManagerService.setClusterStatePublisher((event, publishListener, ackListener) -> { + masterService.setClusterStatePublisher((event, publishListener, ackListener) -> { if (event.source().contains("test5")) { relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( Settings.EMPTY @@ -825,12 +825,12 @@ public void testLongClusterStateUpdateLogging() throws Exception { clusterStateRef.set(event.state()); publishListener.onResponse(null); }); - clusterManagerService.setClusterStateSupplier(clusterStateRef::get); - clusterManagerService.start(); + masterService.setClusterStateSupplier(clusterStateRef::get); + masterService.start(); final CountDownLatch latch = new CountDownLatch(6); final CountDownLatch processedFirstTask = new CountDownLatch(1); - clusterManagerService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += randomLongBetween( @@ -853,7 +853,7 @@ public void onFailure(String source, Exception e) { }); processedFirstTask.await(); - clusterManagerService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( @@ -872,7 +872,7 @@ public void onFailure(String source, Exception e) { latch.countDown(); } }); - clusterManagerService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( @@ -891,7 +891,7 @@ public void onFailure(String source, Exception e) { fail(); } }); - clusterManagerService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { relativeTimeInMillis += MasterService.CLUSTER_MANAGER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get( @@ -910,7 +910,7 @@ public void onFailure(String source, Exception e) { fail(); } }); - clusterManagerService.submitStateUpdateTask("test5", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test5", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { return ClusterState.builder(currentState).incrementVersion().build(); @@ -926,7 +926,7 @@ public void onFailure(String source, Exception e) { fail(); } }); - clusterManagerService.submitStateUpdateTask("test6", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test6", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { return ClusterState.builder(currentState).incrementVersion().build(); @@ -944,7 +944,7 @@ public void onFailure(String source, Exception e) { }); // Additional update task to make sure all previous logging made it to the loggerName // We don't check logging for this on since there is no guarantee that it will occur before our check - clusterManagerService.submitStateUpdateTask("test7", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test7", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { return currentState; @@ -971,7 +971,7 @@ public void testAcking() throws InterruptedException { final DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); final DiscoveryNode node3 = new DiscoveryNode("node3", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); try ( - MasterService clusterManagerService = new MasterService( + MasterService masterService = new MasterService( Settings.builder() .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()) .put(Node.NODE_NAME_SETTING.getKey(), "test_node") @@ -986,9 +986,9 @@ public void testAcking() throws InterruptedException { .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK) .build(); final AtomicReference publisherRef = new AtomicReference<>(); - clusterManagerService.setClusterStatePublisher((e, pl, al) -> publisherRef.get().publish(e, pl, al)); - clusterManagerService.setClusterStateSupplier(() -> initialClusterState); - clusterManagerService.start(); + masterService.setClusterStatePublisher((e, pl, al) -> publisherRef.get().publish(e, pl, al)); + masterService.setClusterStateSupplier(() -> initialClusterState); + masterService.start(); // check that we don't time out before even committing the cluster state { @@ -1000,7 +1000,7 @@ public void testAcking() throws InterruptedException { ) ); - clusterManagerService.submitStateUpdateTask("test2", new AckedClusterStateUpdateTask(null, null) { + masterService.submitStateUpdateTask("test2", new AckedClusterStateUpdateTask(null, null) { @Override public ClusterState execute(ClusterState currentState) { return ClusterState.builder(currentState).build(); @@ -1055,7 +1055,7 @@ public void onAckTimeout() { ackListener.onNodeAck(node3, null); }); - clusterManagerService.submitStateUpdateTask("test2", new AckedClusterStateUpdateTask(null, null) { + masterService.submitStateUpdateTask("test2", new AckedClusterStateUpdateTask(null, null) { @Override public ClusterState execute(ClusterState currentState) { return ClusterState.builder(currentState).build(); @@ -1101,8 +1101,8 @@ public void onAckTimeout() { /** * Returns the cluster state that the cluster-manager service uses (and that is provided by the discovery layer) */ - public static ClusterState discoveryState(MasterService clusterManagerService) { - return clusterManagerService.state(); + public static ClusterState discoveryState(MasterService masterService) { + return masterService.state(); } } diff --git a/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java b/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java index efcefab6c9f8b..d1e3f406b4933 100644 --- a/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java +++ b/server/src/test/java/org/opensearch/discovery/DiscoveryModuleTests.java @@ -70,7 +70,7 @@ public class DiscoveryModuleTests extends OpenSearchTestCase { private TransportService transportService; private NamedWriteableRegistry namedWriteableRegistry; - private MasterService clusterManagerService; + private MasterService masterService; private ClusterApplier clusterApplier; private ThreadPool threadPool; private ClusterSettings clusterSettings; @@ -93,7 +93,7 @@ public void setupDummyServices() { threadPool = mock(ThreadPool.class); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); transportService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null); - clusterManagerService = mock(MasterService.class); + masterService = mock(MasterService.class); namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); clusterApplier = mock(ClusterApplier.class); clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); @@ -112,7 +112,7 @@ private DiscoveryModule newModule(Settings settings, List plugi transportService, namedWriteableRegistry, null, - clusterManagerService, + masterService, clusterApplier, clusterSettings, plugins, diff --git a/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java b/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java index 9558e898f8832..7e0f977e6e835 100644 --- a/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java +++ b/server/src/test/java/org/opensearch/snapshots/SnapshotResiliencyTests.java @@ -1643,7 +1643,7 @@ private final class TestClusterNode { private final DiscoveryNode node; - private final MasterService clusterManagerService; + private final MasterService masterService; private final AllocationService allocationService; @@ -1663,18 +1663,13 @@ private final class TestClusterNode { this.node = node; final Environment environment = createEnvironment(node.getName()); threadPool = deterministicTaskQueue.getThreadPool(runnable -> CoordinatorTests.onNodeLog(node, runnable)); - clusterManagerService = new FakeThreadPoolMasterService( - node.getName(), - "test", - threadPool, - deterministicTaskQueue::scheduleNow - ); + masterService = new FakeThreadPoolMasterService(node.getName(), "test", threadPool, deterministicTaskQueue::scheduleNow); final Settings settings = environment.settings(); final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); clusterService = new ClusterService( settings, clusterSettings, - clusterManagerService, + masterService, new ClusterApplierService(node.getName(), settings, clusterSettings, threadPool) { @Override protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { @@ -2205,7 +2200,7 @@ public void start(ClusterState initialState) { transportService, namedWriteableRegistry, allocationService, - clusterManagerService, + masterService, () -> persistedState, hostsResolver -> nodes.values() .stream() @@ -2219,7 +2214,7 @@ public void start(ClusterState initialState) { ElectionStrategy.DEFAULT_INSTANCE, () -> new StatusInfo(HEALTHY, "healthy-info") ); - clusterManagerService.setClusterStatePublisher(coordinator); + masterService.setClusterStatePublisher(coordinator); coordinator.start(); clusterService.getClusterApplierService().setNodeConnectionsService(nodeConnectionsService); nodeConnectionsService.start(); diff --git a/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java b/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java index fb18aa1b5749b..19551b0adecb2 100644 --- a/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java +++ b/test/framework/src/main/java/org/opensearch/cluster/coordination/AbstractCoordinatorTestCase.java @@ -557,7 +557,7 @@ void stabilise(long stabilisationDurationMillis) { final ClusterNode leader = getAnyLeader(); final long leaderTerm = leader.coordinator.getCurrentTerm(); - final int pendingTaskCount = leader.clusterManagerService.getFakeMasterServicePendingTaskCount(); + final int pendingTaskCount = leader.masterService.getFakeMasterServicePendingTaskCount(); runFor((pendingTaskCount + 1) * DEFAULT_CLUSTER_STATE_UPDATE_DELAY, "draining task queue"); final Matcher isEqualToLeaderVersion = equalTo(leader.coordinator.getLastAcceptedState().getVersion()); @@ -1025,7 +1025,7 @@ class ClusterNode { private final DiscoveryNode localNode; final MockPersistedState persistedState; final Settings nodeSettings; - private AckedFakeThreadPoolMasterService clusterManagerService; + private AckedFakeThreadPoolMasterService masterService; private DisruptableClusterApplierService clusterApplierService; private ClusterService clusterService; TransportService transportService; @@ -1105,7 +1105,7 @@ protected Optional getDisruptableMockTransport(Transpo null, emptySet() ); - clusterManagerService = new AckedFakeThreadPoolMasterService( + masterService = new AckedFakeThreadPoolMasterService( localNode.getId(), "test", threadPool, @@ -1119,7 +1119,7 @@ protected Optional getDisruptableMockTransport(Transpo deterministicTaskQueue, threadPool ); - clusterService = new ClusterService(settings, clusterSettings, clusterManagerService, clusterApplierService); + clusterService = new ClusterService(settings, clusterSettings, masterService, clusterApplierService); clusterService.setNodeConnectionsService( new NodeConnectionsService(clusterService.getSettings(), threadPool, transportService) ); @@ -1134,7 +1134,7 @@ protected Optional getDisruptableMockTransport(Transpo transportService, writableRegistry(), allocationService, - clusterManagerService, + masterService, this::getPersistedState, Cluster.this::provideSeedHosts, clusterApplierService, @@ -1144,7 +1144,7 @@ protected Optional getDisruptableMockTransport(Transpo getElectionStrategy(), nodeHealthService ); - clusterManagerService.setClusterStatePublisher(coordinator); + masterService.setClusterStatePublisher(coordinator); final GatewayService gatewayService = new GatewayService( settings, allocationService, @@ -1331,11 +1331,11 @@ AckCollector submitUpdateTask( onNode(() -> { logger.trace("[{}] submitUpdateTask: enqueueing [{}]", localNode.getId(), source); final long submittedTerm = coordinator.getCurrentTerm(); - clusterManagerService.submitStateUpdateTask(source, new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask(source, new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { assertThat(currentState.term(), greaterThanOrEqualTo(submittedTerm)); - clusterManagerService.nextAckCollector = ackCollector; + masterService.nextAckCollector = ackCollector; return clusterStateUpdate.apply(currentState); } diff --git a/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java b/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java index f709d8bcaff34..99f9b86fb6479 100644 --- a/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java +++ b/test/framework/src/main/java/org/opensearch/test/ClusterServiceUtils.java @@ -62,19 +62,19 @@ public class ClusterServiceUtils { public static MasterService createMasterService(ThreadPool threadPool, ClusterState initialClusterState) { - MasterService clusterManagerService = new MasterService( + MasterService masterService = new MasterService( Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test_cluster_manager_node").build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool ); AtomicReference clusterStateRef = new AtomicReference<>(initialClusterState); - clusterManagerService.setClusterStatePublisher((event, publishListener, ackListener) -> { + masterService.setClusterStatePublisher((event, publishListener, ackListener) -> { clusterStateRef.set(event.state()); publishListener.onResponse(null); }); - clusterManagerService.setClusterStateSupplier(clusterStateRef::get); - clusterManagerService.start(); - return clusterManagerService; + masterService.setClusterStateSupplier(clusterStateRef::get); + masterService.start(); + return masterService; } public static MasterService createMasterService(ThreadPool threadPool, DiscoveryNode localNode) { diff --git a/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java b/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java index dabeeb6742ec4..b4cdb0b33391a 100644 --- a/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java +++ b/test/framework/src/test/java/org/opensearch/cluster/service/FakeThreadPoolMasterServiceTests.java @@ -84,21 +84,21 @@ public void testFakeClusterManagerService() { doAnswer(invocationOnMock -> runnableTasks.add((Runnable) invocationOnMock.getArguments()[0])).when(executorService).execute(any()); when(mockThreadPool.generic()).thenReturn(executorService); - FakeThreadPoolMasterService clusterManagerService = new FakeThreadPoolMasterService( + FakeThreadPoolMasterService masterService = new FakeThreadPoolMasterService( "test_node", "test", mockThreadPool, runnableTasks::add ); - clusterManagerService.setClusterStateSupplier(lastClusterStateRef::get); - clusterManagerService.setClusterStatePublisher((event, publishListener, ackListener) -> { + masterService.setClusterStateSupplier(lastClusterStateRef::get); + masterService.setClusterStatePublisher((event, publishListener, ackListener) -> { lastClusterStateRef.set(event.state()); publishingCallback.set(publishListener); }); - clusterManagerService.start(); + masterService.start(); AtomicBoolean firstTaskCompleted = new AtomicBoolean(); - clusterManagerService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { return ClusterState.builder(currentState) @@ -138,7 +138,7 @@ public void onFailure(String source, Exception e) { assertThat(runnableTasks.size(), equalTo(0)); AtomicBoolean secondTaskCompleted = new AtomicBoolean(); - clusterManagerService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { + masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { return ClusterState.builder(currentState)