
org.elasticsearch.action.admin.cluster.state.ClusterStateResponse Maven / Gradle / Ivy
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.action.admin.cluster.state;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.ByteSizeValue;
import java.io.IOException;
import java.util.Objects;
/**
* The response for getting the cluster state.
*/
public class ClusterStateResponse extends ActionResponse {
private ClusterName clusterName;
private ClusterState clusterState;
private boolean waitForTimedOut = false;
public ClusterStateResponse(StreamInput in) throws IOException {
super(in);
clusterName = new ClusterName(in);
if (in.getVersion().onOrAfter(Version.V_6_6_0)) {
clusterState = in.readOptionalWriteable(innerIn -> ClusterState.readFrom(innerIn, null));
} else {
clusterState = ClusterState.readFrom(in, null);
}
if (in.getVersion().before(Version.V_7_0_0)) {
new ByteSizeValue(in);
}
if (in.getVersion().onOrAfter(Version.V_6_6_0)) {
waitForTimedOut = in.readBoolean();
}
}
public ClusterStateResponse(ClusterName clusterName, ClusterState clusterState, boolean waitForTimedOut) {
this.clusterName = clusterName;
this.clusterState = clusterState;
this.waitForTimedOut = waitForTimedOut;
}
/**
* The requested cluster state. Only the parts of the cluster state that were
* requested are included in the returned {@link ClusterState} instance.
*/
public ClusterState getState() {
return this.clusterState;
}
/**
* The name of the cluster.
*/
public ClusterName getClusterName() {
return this.clusterName;
}
/**
* Returns whether the request timed out waiting for a cluster state with a metadata version equal or
* higher than the specified metadata.
*/
public boolean isWaitForTimedOut() {
return waitForTimedOut;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
clusterName.writeTo(out);
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
out.writeOptionalWriteable(clusterState);
} else {
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
clusterState.writeTo(out);
} else {
ClusterModule.filterCustomsForPre63Clients(clusterState).writeTo(out);
}
}
if (out.getVersion().before(Version.V_7_0_0)) {
ByteSizeValue.ZERO.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
out.writeBoolean(waitForTimedOut);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClusterStateResponse response = (ClusterStateResponse) o;
return waitForTimedOut == response.waitForTimedOut && Objects.equals(clusterName, response.clusterName) &&
// Best effort. Only compare cluster state version and master node id,
// because cluster state doesn't implement equals()
Objects.equals(getVersion(clusterState), getVersion(response.clusterState))
&& Objects.equals(getMasterNodeId(clusterState), getMasterNodeId(response.clusterState));
}
@Override
public int hashCode() {
// Best effort. Only use cluster state version and master node id,
// because cluster state doesn't implement hashcode()
return Objects.hash(clusterName, getVersion(clusterState), getMasterNodeId(clusterState), waitForTimedOut);
}
private static String getMasterNodeId(ClusterState clusterState) {
if (clusterState == null) {
return null;
}
DiscoveryNodes nodes = clusterState.getNodes();
if (nodes != null) {
return nodes.getMasterNodeId();
} else {
return null;
}
}
private static Long getVersion(ClusterState clusterState) {
if (clusterState != null) {
return clusterState.getVersion();
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy