Skip to content

Commit

Permalink
Remove Redundant Cluster State Update Response Classes (#63646) (#64870)
Browse files Browse the repository at this point in the history
These intermediary response types don't contain any information
outside of what the shard acknowledged and acknowledged responses
contain so this PR removes them.
Also, it adds three constants for the three possible states of
`ShardsAcknowledgedResponse`.

Follow up to #63335
  • Loading branch information
original-brownbear authored Nov 10, 2020
1 parent d7697b4 commit 6181ba3
Show file tree
Hide file tree
Showing 54 changed files with 231 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public final void testBwcFromXContent() throws IOException {
{
final boolean acknowledged = randomBoolean();
final boolean shardsAcknowledged = acknowledged ? randomBoolean() : false;
final ShardsAcknowledgedResponse expected = new ShardsAcknowledgedResponse(acknowledged, shardsAcknowledged){};
final ShardsAcknowledgedResponse expected = ShardsAcknowledgedResponse.of(acknowledged, shardsAcknowledged);

final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes = toShuffledXContent(expected, xContentType, getParams(), randomBoolean());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ public void testAckedUpdateTask() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch processedLatch = new CountDownLatch(1);
clusterService.submitStateUpdateTask("test",
new AckedClusterStateUpdateTask<Void>(MasterServiceTests.ackedRequest(TEN_SECONDS, TEN_SECONDS), null) {
@Override
protected Void newResponse(boolean acknowledged) {
return null;
}

new AckedClusterStateUpdateTask(MasterServiceTests.ackedRequest(TEN_SECONDS, TEN_SECONDS), null) {
@Override
public boolean mustAck(DiscoveryNode discoveryNode) {
return true;
Expand Down Expand Up @@ -123,12 +118,7 @@ public void testAckedUpdateTaskSameClusterState() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch processedLatch = new CountDownLatch(1);
clusterService.submitStateUpdateTask("test",
new AckedClusterStateUpdateTask<Void>(MasterServiceTests.ackedRequest(TEN_SECONDS, TEN_SECONDS), null) {
@Override
protected Void newResponse(boolean acknowledged) {
return null;
}

new AckedClusterStateUpdateTask(MasterServiceTests.ackedRequest(TEN_SECONDS, TEN_SECONDS), null) {
@Override
public void onAllNodesAcked(@Nullable Exception e) {
allNodesAcked.set(true);
Expand Down Expand Up @@ -182,12 +172,7 @@ public void testAckedUpdateTaskNoAckExpected() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);

clusterService.submitStateUpdateTask(
"test", new AckedClusterStateUpdateTask<Void>(MasterServiceTests.ackedRequest(TEN_SECONDS, TEN_SECONDS), null) {
@Override
protected Void newResponse(boolean acknowledged) {
return null;
}

"test", new AckedClusterStateUpdateTask(MasterServiceTests.ackedRequest(TEN_SECONDS, TEN_SECONDS), null) {
@Override
public boolean mustAck(DiscoveryNode discoveryNode) {
return false;
Expand Down Expand Up @@ -243,12 +228,7 @@ public void testAckedUpdateTaskTimeoutZero() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch processedLatch = new CountDownLatch(1);
clusterService.submitStateUpdateTask("test",
new AckedClusterStateUpdateTask<Void>(MasterServiceTests.ackedRequest(TimeValue.ZERO, TEN_SECONDS), null) {
@Override
protected Void newResponse(boolean acknowledged) {
return null;
}

new AckedClusterStateUpdateTask(MasterServiceTests.ackedRequest(TimeValue.ZERO, TEN_SECONDS), null) {
@Override
public boolean mustAck(DiscoveryNode discoveryNode) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void submitStateUpdate(final ClusterRerouteRequest request, final Action
})));
}

static class ClusterRerouteResponseAckedClusterStateUpdateTask extends AckedClusterStateUpdateTask<ClusterRerouteResponse> {
static class ClusterRerouteResponseAckedClusterStateUpdateTask extends AckedClusterStateUpdateTask {

private final ClusterRerouteRequest request;
private final ActionListener<ClusterRerouteResponse> listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected void masterOperation(final ClusterUpdateSettingsRequest request, final
final ActionListener<ClusterUpdateSettingsResponse> listener) {
final SettingsUpdater updater = new SettingsUpdater(clusterSettings);
clusterService.submitStateUpdateTask("cluster_update_settings",
new AckedClusterStateUpdateTask<ClusterUpdateSettingsResponse>(Priority.IMMEDIATE, request, listener) {
new AckedClusterStateUpdateTask(Priority.IMMEDIATE, request, listener) {

private volatile boolean changed = false;

Expand Down Expand Up @@ -125,7 +125,7 @@ private void reroute(final boolean updateSettingsAcked) {
// to the components until the ClusterStateListener instances have been invoked, but are visible after
// the first update task has been completed.
clusterService.submitStateUpdateTask("reroute_after_cluster_update_settings",
new AckedClusterStateUpdateTask<ClusterUpdateSettingsResponse>(Priority.URGENT, request, listener) {
new AckedClusterStateUpdateTask(Priority.URGENT, request, listener) {

@Override
public boolean mustAck(DiscoveryNode discoveryNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.AliasAction;
Expand Down Expand Up @@ -146,10 +145,10 @@ protected void masterOperation(final IndicesAliasesRequest request, final Cluste
IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest(unmodifiableList(finalActions))
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout());

indexAliasesService.indicesAliases(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {
indexAliasesService.indicesAliases(updateRequest, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(AcknowledgedResponse.of(response.isAcknowledged()));
public void onResponse(AcknowledgedResponse response) {
listener.onResponse(response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.ActiveShardsObserver;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.AutoCreateIndex;
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
Expand Down Expand Up @@ -84,7 +84,7 @@ protected void masterOperation(CreateIndexRequest request,
ClusterState state,
ActionListener<CreateIndexResponse> finalListener) {
AtomicReference<String> indexNameRef = new AtomicReference<>();
ActionListener<ClusterStateUpdateResponse> listener = ActionListener.wrap(
ActionListener<AcknowledgedResponse> listener = ActionListener.wrap(
response -> {
String indexName = indexNameRef.get();
assert indexName != null;
Expand All @@ -105,12 +105,7 @@ protected void masterOperation(CreateIndexRequest request,
finalListener::onFailure
);
clusterService.submitStateUpdateTask("auto create [" + request.index() + "]",
new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, listener) {

@Override
protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
return new ClusterStateUpdateResponse(acknowledged);
}
new AckedClusterStateUpdateTask(Priority.URGENT, request, listener) {

@Override
public ClusterState execute(ClusterState currentState) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,7 @@ public void onFailure(Exception e) {
final String taskSource = "delete-dangling-index [" + indexName + "] [" + indexUUID + "]";

clusterService.submitStateUpdateTask(
taskSource,
new AckedClusterStateUpdateTask<AcknowledgedResponse>(deleteRequest, clusterStateUpdatedListener) {

@Override
protected AcknowledgedResponse newResponse(boolean acknowledged) {
return AcknowledgedResponse.of(acknowledged);
}

taskSource, new AckedClusterStateUpdateTask(deleteRequest, clusterStateUpdatedListener) {
@Override
public ClusterState execute(final ClusterState currentState) {
return deleteDanglingIndex(currentState, indexToDelete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetadataDeleteIndexService;
Expand Down Expand Up @@ -88,11 +87,11 @@ protected void masterOperation(final DeleteIndexRequest request, final ClusterSt
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.indices(concreteIndices.toArray(new Index[concreteIndices.size()]));

deleteIndexService.deleteIndices(deleteRequest, new ActionListener<ClusterStateUpdateResponse>() {
deleteIndexService.deleteIndices(deleteRequest, new ActionListener<AcknowledgedResponse>() {

@Override
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(AcknowledgedResponse.of(response.isAcknowledged()));
public void onResponse(AcknowledgedResponse response) {
listener.onResponse(response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
Expand Down Expand Up @@ -127,11 +126,11 @@ static void performMappingUpdate(Index[] concreteIndices,
.indices(concreteIndices).type(request.type())
.source(request.source());

metadataMappingService.putMapping(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {
metadataMappingService.putMapping(updateRequest, new ActionListener<AcknowledgedResponse>() {

@Override
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(AcknowledgedResponse.of(response.isAcknowledged()));
public void onResponse(AcknowledgedResponse response) {
listener.onResponse(response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse;
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.OpenIndexClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
Expand Down Expand Up @@ -84,10 +84,10 @@ protected void masterOperation(final OpenIndexRequest request, final ClusterStat
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.indices(concreteIndices).waitForActiveShards(request.waitForActiveShards());

indexStateService.openIndex(updateRequest, new ActionListener<OpenIndexClusterStateUpdateResponse>() {
indexStateService.openIndex(updateRequest, new ActionListener<ShardsAcknowledgedResponse>() {

@Override
public void onResponse(OpenIndexClusterStateUpdateResponse response) {
public void onResponse(ShardsAcknowledgedResponse response) {
listener.onResponse(new OpenIndexResponse(response.isAcknowledged(), response.isShardsAcknowledged()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexMetadata;
Expand Down Expand Up @@ -82,10 +81,10 @@ protected void masterOperation(final UpdateSettingsRequest request, final Cluste
.ackTimeout(request.timeout())
.masterNodeTimeout(request.masterNodeTimeout());

updateSettingsService.updateSettings(clusterStateUpdateRequest, new ActionListener<ClusterStateUpdateResponse>() {
updateSettingsService.updateSettings(clusterStateUpdateRequest, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(AcknowledgedResponse.of(response.isAcknowledged()));
public void onResponse(AcknowledgedResponse response) {
listener.onResponse(response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ protected void masterOperation(final DeleteIndexTemplateRequest request, final C
.masterTimeout(request.masterNodeTimeout()),
new MetadataIndexTemplateService.RemoveListener() {
@Override
public void onResponse(MetadataIndexTemplateService.RemoveResponse response) {
listener.onResponse(AcknowledgedResponse.of(response.acknowledged()));
public void onResponse(AcknowledgedResponse response) {
listener.onResponse(response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
Expand Down Expand Up @@ -65,10 +64,10 @@ protected void masterOperation(final UpgradeSettingsRequest request, final Clust
.versions(request.versions())
.masterNodeTimeout(request.masterNodeTimeout());

updateSettingsService.upgradeIndexSettings(clusterStateUpdateRequest, new ActionListener<ClusterStateUpdateResponse>() {
updateSettingsService.upgradeIndexSettings(clusterStateUpdateRequest, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(ClusterStateUpdateResponse response) {
listener.onResponse(AcknowledgedResponse.of(response.isAcknowledged()));
public void onResponse(AcknowledgedResponse response) {
listener.onResponse(response);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

public abstract class ShardsAcknowledgedResponse extends AcknowledgedResponse {
public class ShardsAcknowledgedResponse extends AcknowledgedResponse {

protected static final ParseField SHARDS_ACKNOWLEDGED = new ParseField("shards_acknowledged");

Expand All @@ -42,6 +42,10 @@ protected static <T extends ShardsAcknowledgedResponse> void declareAcknowledged
ObjectParser.ValueType.BOOLEAN);
}

public static final ShardsAcknowledgedResponse NOT_ACKNOWLEDGED = new ShardsAcknowledgedResponse(false, false);
private static final ShardsAcknowledgedResponse SHARDS_NOT_ACKNOWLEDGED = new ShardsAcknowledgedResponse(true, false);
private static final ShardsAcknowledgedResponse ACKNOWLEDGED = new ShardsAcknowledgedResponse(true, true);

private final boolean shardsAcknowledged;

protected ShardsAcknowledgedResponse(StreamInput in, boolean readShardsAcknowledged, boolean readAcknowledged) throws IOException {
Expand All @@ -53,6 +57,15 @@ protected ShardsAcknowledgedResponse(StreamInput in, boolean readShardsAcknowled
}
}

public static ShardsAcknowledgedResponse of(boolean acknowledged, boolean shardsAcknowledged) {
if (acknowledged) {
return shardsAcknowledged ? ACKNOWLEDGED : SHARDS_NOT_ACKNOWLEDGED;
} else {
assert shardsAcknowledged == false;
return NOT_ACKNOWLEDGED;
}
}

protected ShardsAcknowledgedResponse(boolean acknowledged, boolean shardsAcknowledged) {
super(acknowledged);
assert acknowledged || shardsAcknowledged == false; // if it's not acknowledged, then shards acked should be false too
Expand Down Expand Up @@ -90,5 +103,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(super.hashCode(), isShardsAcknowledged());
}

}
Loading

0 comments on commit 6181ba3

Please sign in to comment.