Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public NotMasterException(StreamInput in) throws IOException {
super(in);
}

public NotMasterException(String msg, Object... args) {
super(msg, args);
}

public NotMasterException(String msg, Throwable cause, Object... args) {
super(msg, cause, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.cluster.ClusterStatePublicationEvent;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.LocalMasterServiceTask;
import org.elasticsearch.cluster.NotMasterException;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.coordination.ClusterFormationFailureHelper.ClusterFormationState;
import org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion;
Expand Down Expand Up @@ -1552,7 +1553,7 @@ public void publish(
clusterStatePublicationEvent.getNewState().term()
)
);
throw new FailedToCommitClusterStateException(
throw new NotMasterException(
"node is no longer master for term "
+ clusterStatePublicationEvent.getNewState().term()
+ " while handling publication"
Expand Down Expand Up @@ -1638,8 +1639,8 @@ assert getLocalNode().equals(clusterState.getNodes().get(getLocalNode().getId())
}
}
}
} catch (FailedToCommitClusterStateException failedToCommitClusterStateException) {
publishListener.onFailure(failedToCommitClusterStateException);
} catch (FailedToCommitClusterStateException | NotMasterException e) {
publishListener.onFailure(e);
} catch (Exception e) {
assert false : e; // all exceptions should already be caught and wrapped in a FailedToCommitClusterStateException
logger.error(() -> "[" + clusterStatePublicationEvent.getSummary() + "] publishing unexpectedly failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,30 @@ public void onResponse(Void unused) {

@Override
public void onFailure(Exception exception) {
if (exception instanceof FailedToCommitClusterStateException failedToCommitClusterStateException) {
if (exception instanceof FailedToCommitClusterStateException || exception instanceof NotMasterException) {
final long notificationStartTime = threadPool.rawRelativeTimeInMillis();
final long version = newClusterState.version();
logger.warn(() -> format("failing [%s]: failed to commit cluster state version [%s]", summary, version), exception);

if (exception instanceof FailedToCommitClusterStateException) {
logger.warn(
() -> format("Failing [%s]: failed to commit cluster state version [%s]", summary, version),
exception
);
} else {
logger.debug(
() -> format(
"Failing [%s]: node is no longer the master. Failed to publish cluster state version [%s]",
summary,
version
),
exception
);
}

for (final var executionResult : executionResults) {
executionResult.onPublishFailure(failedToCommitClusterStateException);
executionResult.onPublishFailure(exception);
}

final long notificationMillis = threadPool.rawRelativeTimeInMillis() - notificationStartTime;
clusterStateUpdateStatsTracker.onPublicationFailure(
threadPool.rawRelativeTimeInMillis(),
Expand Down Expand Up @@ -985,11 +1002,18 @@ void onClusterStateUnchanged(ClusterState clusterState) {
}
}

void onPublishFailure(FailedToCommitClusterStateException e) {
void onPublishFailure(Exception e) {
Comment on lines -988 to +1005
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is only called with FailedToCommitClusterStateException or NotMasterException from above, but is it worth trying to be as specific as possible here instead of Exception? Maybe the common base class ElasticsearchException, and also adding an assertion that it is one of the two expected types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the parameter e is passed in from an onfailure method here, and is of type Exception, so I can't narrow this down.

But, since this method is currently called from within this IF statement:

if (exception instanceof FailedToCommitClusterStateException || exception instanceof NotMasterException) {
    ....

I know that currently the type must be either FailedToCommitClusterStateException or NotMasterException so it's a good call to add an assertion in case this method gets used anywhere in future, and that doesn't hold

assert e instanceof FailedToCommitClusterStateException || e instanceof NotMasterException : e;
if (publishedStateConsumer == null && onPublicationSuccess == null) {
assert failure != null;
var taskFailure = failure;
failure = new FailedToCommitClusterStateException(e.getMessage(), e);

if (e instanceof FailedToCommitClusterStateException) {
failure = new FailedToCommitClusterStateException(e.getMessage(), e);
} else {
failure = new NotMasterException(e.getMessage(), e);
}

failure.addSuppressed(taskFailure);
notifyFailure();
return;
Expand Down