Skip to content

Commit

Permalink
fetching term information from master using light-weight call before …
Browse files Browse the repository at this point in the history
…fetch of cluster-state
  • Loading branch information
rajiv-kv committed Feb 22, 2024
1 parent fb41756 commit e585e09
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void reg
actions.register(ClusterAllocationExplainAction.INSTANCE, TransportClusterAllocationExplainAction.class);
actions.register(ClusterStatsAction.INSTANCE, TransportClusterStatsAction.class);
actions.register(ClusterStateAction.INSTANCE, TransportClusterStateAction.class);
actions.register(org.opensearch.action.admin.cluster.state.term.GetTermVersionAction.INSTANCE, org.opensearch.action.admin.cluster.state.term.TransportGetTermVersionAction.class);
actions.register(ClusterHealthAction.INSTANCE, TransportClusterHealthAction.class);
actions.register(ClusterUpdateSettingsAction.INSTANCE, TransportClusterUpdateSettingsAction.class);
actions.register(ClusterRerouteAction.INSTANCE, TransportClusterRerouteAction.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,8 @@ private ClusterStateResponse buildResponse(final ClusterStateRequest request, fi
return new ClusterStateResponse(currentState.getClusterName(), builder.build(), false);
}

@Override
protected boolean checkTermVersion() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.action.admin.cluster.state.term;

import org.opensearch.action.ActionType;

/**
* Transport action for fetching cluster term
*
* @opensearch.internal
*/
public class GetTermVersionAction extends ActionType<GetTermVersionResponse> {

public static final GetTermVersionAction INSTANCE = new GetTermVersionAction();
public static final String NAME = "cluster:monitor/term";

private GetTermVersionAction() {
super(NAME, GetTermVersionResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.action.admin.cluster.state.term;

import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadRequest;
import org.opensearch.core.common.io.stream.StreamInput;

import java.io.IOException;

/**
* Request object to get cluster term
*
* @opensearch.internal
*/
public class GetTermVersionRequest extends ClusterManagerNodeReadRequest<GetTermVersionRequest> {

public GetTermVersionRequest() {}

public GetTermVersionRequest(StreamInput in) throws IOException {
super(in);
}

@Override
public ActionRequestValidationException validate() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.action.admin.cluster.state.term;

import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.ClusterState;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

import java.io.IOException;

/**
* Response object of cluster term
*
* @opensearch.internal
*/
public class GetTermVersionResponse extends ActionResponse {

private final ClusterName clusterName;
private final String stateUUID;
private final long term;
private final long version;

public GetTermVersionResponse(ClusterName clusterName, String stateUUID, long term, long version) {
this.clusterName = clusterName;
this.stateUUID = stateUUID;
this.term = term;
this.version = version;
}

public GetTermVersionResponse(StreamInput in) throws IOException {
super(in);
this.clusterName = new ClusterName(in);
this.stateUUID = in.readString();
this.term = in.readLong();
this.version = in.readLong();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
clusterName.writeTo(out);
out.writeString(stateUUID);
out.writeLong(term);
out.writeLong(version);
}

public long getTerm() {
return term;
}

public long getVersion() {
return version;
}

public ClusterName getClusterName() {
return clusterName;
}

public String getStateUUID() {
return stateUUID;
}

public boolean matches(ClusterState clusterState) {
return clusterName.equals(clusterState.getClusterName())
&& stateUUID.equals(clusterState.stateUUID())
&& term == clusterState.term()
&& version == clusterState.version();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.action.admin.cluster.state.term;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.clustermanager.TransportClusterManagerNodeReadAction;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.block.ClusterBlockException;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportService;

import java.io.IOException;

/**
* Transport action for obtaining cluster term and version from cluster-manager
*
* @opensearch.internal
*/
public class TransportGetTermVersionAction extends TransportClusterManagerNodeReadAction<GetTermVersionRequest, GetTermVersionResponse> {

private final Logger logger = LogManager.getLogger(getClass());

@Inject
public TransportGetTermVersionAction(
TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters,
IndexNameExpressionResolver indexNameExpressionResolver
) {
super(
GetTermVersionAction.NAME,
false,
transportService,
clusterService,
threadPool,
actionFilters,
GetTermVersionRequest::new,
indexNameExpressionResolver
);
}

@Override
protected String executor() {
return ThreadPool.Names.SAME;
}

@Override
public GetTermVersionResponse read(StreamInput in) throws IOException {
return new GetTermVersionResponse(in);
}

@Override
protected ClusterBlockException checkBlock(GetTermVersionRequest request, ClusterState state) {
// cluster state term and version needs to be retrieved even on a fully blocked cluster
return null;
}

@Override
protected void clusterManagerOperation(
GetTermVersionRequest request,
ClusterState state,
ActionListener<GetTermVersionResponse> listener
) throws Exception {
ActionListener.completeWith(listener, () -> buildResponse(request, state));
}

private GetTermVersionResponse buildResponse(GetTermVersionRequest request, ClusterState state) {
logger.trace("Serving cluster term version request using term {} and version {}", state.term(), state.version());
return new GetTermVersionResponse(state.getClusterName(), state.stateUUID(), state.term(), state.getVersion());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

/** Cluster Term transport handler. */
package org.opensearch.action.admin.cluster.state.term;
Loading

0 comments on commit e585e09

Please sign in to comment.