Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow client restart if required #19554

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.UUID;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
Expand Down Expand Up @@ -434,4 +435,22 @@ public Response reindexAllJobLastStatus(
}
return Response.status(Response.Status.NOT_FOUND).entity("No Last Run.").build();
}

@PUT
@Path("/client/restart")
@Operation(
operationId = "fixesElasticSearchClient",
summary = "Fix Elastic Search Client",
description = "Fix Elastic Search Client",
responses = {
@ApiResponse(responseCode = "200", description = "Success"),
@ApiResponse(responseCode = "404", description = "Status not found")
})
public Response fixSearchClient(
@Context UriInfo uriInfo, @Context SecurityContext securityContext) throws IOException {
authorizer.authorizeAdmin(securityContext);
SearchRepository repository = Entity.getSearchRepository();
repository.restartClientIfNeeded();
return Response.status(Response.Status.OK).entity("Fixing Client Initiated.").build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,6 @@ static boolean shouldApplyRbacConditions(
&& !subjectContext.isBot()
&& rbacConditionEvaluator != null;
}

void restartSearchHttpClient() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1095,4 +1095,8 @@ public List<EntityReference> getEntitiesContainingFQNFromES(
public Set<String> getSearchEntities() {
return new HashSet<>(entityIndexMap.keySet());
}

public void restartClientIfNeeded() throws IOException {
searchClient.restartSearchHttpClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public class ElasticSearchClient implements SearchClient {

@SuppressWarnings("deprecated")
@Getter
protected final RestHighLevelClient client;
protected RestHighLevelClient client;

private final RBACConditionEvaluator rbacConditionEvaluator;
private final QueryBuilderFactory queryBuilderFactory;
Expand Down Expand Up @@ -252,12 +252,15 @@ public class ElasticSearchClient implements SearchClient {
xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}

private final ElasticSearchConfiguration esConfig;

public ElasticSearchClient(ElasticSearchConfiguration config) {
client = createElasticSearchClient(config);
clusterAlias = config != null ? config.getClusterAlias() : "";
isClientAvailable = client != null;
queryBuilderFactory = new ElasticQueryBuilderFactory();
rbacConditionEvaluator = new RBACConditionEvaluator(queryBuilderFactory);
this.esConfig = config;
}

@Override
Expand Down Expand Up @@ -2878,6 +2881,18 @@ public Object getLowLevelClient() {
return client.getLowLevelClient();
}

@Override
public void restartSearchHttpClient() throws IOException {
if (client == null) {
client = createElasticSearchClient(esConfig);
} else {
if (!client.getLowLevelClient().isRunning()) {
client.close();
client = createElasticSearchClient(esConfig);
}
}
}

private void buildSearchRBACQuery(
SubjectContext subjectContext, SearchSourceBuilder searchSourceBuilder) {
if (SearchClient.shouldApplyRbacConditions(subjectContext, rbacConditionEvaluator)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
@Slf4j
// Not tagged with Repository annotation as it is programmatically initialized
public class OpenSearchClient implements SearchClient {
@Getter protected final RestHighLevelClient client;
@Getter protected RestHighLevelClient client;
public static final NamedXContentRegistry X_CONTENT_REGISTRY;
private final boolean isClientAvailable;
private final RBACConditionEvaluator rbacConditionEvaluator;
Expand Down Expand Up @@ -246,12 +246,15 @@ public class OpenSearchClient implements SearchClient {
X_CONTENT_REGISTRY = new NamedXContentRegistry(searchModule.getNamedXContents());
}

private final ElasticSearchConfiguration esConfig;

public OpenSearchClient(ElasticSearchConfiguration config) {
client = createOpenSearchClient(config);
clusterAlias = config != null ? config.getClusterAlias() : "";
isClientAvailable = client != null;
queryBuilderFactory = new OpenSearchQueryBuilderFactory();
rbacConditionEvaluator = new RBACConditionEvaluator(queryBuilderFactory);
this.esConfig = config;
}

@Override
Expand Down Expand Up @@ -2840,6 +2843,18 @@ public Object getLowLevelClient() {
return client.getLowLevelClient();
}

@Override
public void restartSearchHttpClient() throws IOException {
if (client == null) {
client = createOpenSearchClient(esConfig);
} else {
if (!client.getLowLevelClient().isRunning()) {
client.close();
client = createOpenSearchClient(esConfig);
}
}
}

private void buildSearchRBACQuery(
SubjectContext subjectContext, SearchSourceBuilder searchSourceBuilder) {
if (SearchClient.shouldApplyRbacConditions(subjectContext, rbacConditionEvaluator)) {
Expand Down
Loading