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

[Backport 2.x] Clean up miscellaneous code smells, eliminate most Guava use (#327) #332

Merged
Merged
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 @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework;

import com.google.common.collect.ImmutableList;
import org.opensearch.action.ActionRequest;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
Expand Down Expand Up @@ -127,7 +126,7 @@ public Collection<Object> createComponents(
settings
);

return ImmutableList.of(workflowStepFactory, workflowProcessSorter, encryptorUtils, flowFrameworkIndicesHandler);
return List.of(workflowStepFactory, workflowProcessSorter, encryptorUtils, flowFrameworkIndicesHandler);
}

@Override
Expand All @@ -140,7 +139,7 @@ public List<RestHandler> getRestHandlers(
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster
) {
return ImmutableList.of(
return List.of(
new RestCreateWorkflowAction(flowFrameworkFeatureEnabledSetting, settings, clusterService),
new RestDeleteWorkflowAction(flowFrameworkFeatureEnabledSetting),
new RestProvisionWorkflowAction(flowFrameworkFeatureEnabledSetting),
Expand All @@ -154,7 +153,7 @@ public List<RestHandler> getRestHandlers(

@Override
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
return ImmutableList.of(
return List.of(
new ActionHandler<>(CreateWorkflowAction.INSTANCE, CreateWorkflowTransportAction.class),
new ActionHandler<>(DeleteWorkflowAction.INSTANCE, DeleteWorkflowTransportAction.class),
new ActionHandler<>(ProvisionWorkflowAction.INSTANCE, ProvisionWorkflowTransportAction.class),
Expand All @@ -168,20 +167,13 @@ public List<RestHandler> getRestHandlers(

@Override
public List<Setting<?>> getSettings() {
List<Setting<?>> settings = ImmutableList.of(
FLOW_FRAMEWORK_ENABLED,
MAX_WORKFLOWS,
MAX_WORKFLOW_STEPS,
WORKFLOW_REQUEST_TIMEOUT,
MAX_GET_TASK_REQUEST_RETRY
);
return settings;
return List.of(FLOW_FRAMEWORK_ENABLED, MAX_WORKFLOWS, MAX_WORKFLOW_STEPS, WORKFLOW_REQUEST_TIMEOUT, MAX_GET_TASK_REQUEST_RETRY);
}

@Override
public List<ExecutorBuilder<?>> getExecutorBuilders(Settings settings) {
// TODO : Determine final size/queueSize values for the provision thread pool
return ImmutableList.of(
return List.of(
new FixedExecutorBuilder(
settings,
PROVISION_THREAD_POOL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static String getResourceByWorkflowStep(String workflowStep) throws FlowF
}
}
}
logger.error("Unable to find resource type for step: " + workflowStep);
logger.error("Unable to find resource type for step: {}", workflowStep);
throw new FlowFrameworkException("Unable to find resource type for step: " + workflowStep, RestStatus.BAD_REQUEST);
}

Expand All @@ -109,7 +109,7 @@ public static String getDeprovisionStepByWorkflowStep(String workflowStep) throw
}
}
}
logger.error("Unable to find deprovision step for step: " + workflowStep);
logger.error("Unable to find deprovision step for step: {}", workflowStep);
throw new FlowFrameworkException("Unable to find deprovision step for step: " + workflowStep, RestStatus.BAD_REQUEST);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.indices;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -32,7 +31,6 @@
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.flowframework.common.WorkflowResources;
import org.opensearch.flowframework.exception.FlowFrameworkException;
Expand All @@ -47,6 +45,7 @@

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -165,7 +164,7 @@ public void initFlowFrameworkIndexIfAbsent(FlowFrameworkIndex index, ActionListe
String mapping = index.getMapping();

try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, () -> threadContext.restore());
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, threadContext::restore);
if (!clusterService.state().metadata().hasIndex(indexName)) {
@SuppressWarnings("deprecation")
ActionListener<CreateIndexResponse> actionListener = ActionListener.wrap(r -> {
Expand All @@ -176,7 +175,7 @@ public void initFlowFrameworkIndexIfAbsent(FlowFrameworkIndex index, ActionListe
internalListener.onResponse(false);
}
}, e -> {
logger.error("Failed to create index " + indexName, e);
logger.error("Failed to create index {}", indexName, e);
internalListener.onFailure(new FlowFrameworkException(e.getMessage(), ExceptionsHelper.status(e)));
});
CreateIndexRequest request = new CreateIndexRequest(indexName).mapping(mapping).settings(indexSettings);
Expand Down Expand Up @@ -274,7 +273,7 @@ private void shouldUpdateIndex(String indexName, Integer newVersion, ActionListe
Integer oldVersion = NO_SCHEMA_VERSION;
Map<String, Object> indexMapping = indexMetaData.mapping().getSourceAsMap();
Object meta = indexMapping.get(META);
if (meta != null && meta instanceof Map) {
if (meta instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> metaMapping = (Map<String, Object>) meta;
Object schemaVersion = metaMapping.get(SCHEMA_VERSION_FIELD);
Expand All @@ -294,7 +293,7 @@ private void shouldUpdateIndex(String indexName, Integer newVersion, ActionListe
*/
public static String getIndexMappings(String mapping) throws IOException {
URL url = FlowFrameworkIndicesHandler.class.getClassLoader().getResource(mapping);
return Resources.toString(url, Charsets.UTF_8);
return Resources.toString(url, StandardCharsets.UTF_8);
}

/**
Expand All @@ -316,7 +315,7 @@ public void putTemplateToGlobalContext(Template template, ActionListener<IndexRe
Template templateWithEncryptedCredentials = encryptorUtils.encryptTemplateCredentials(template);
request.source(templateWithEncryptedCredentials.toXContent(builder, ToXContent.EMPTY_PARAMS))
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
client.index(request, ActionListener.runBefore(listener, () -> context.restore()));
client.index(request, ActionListener.runBefore(listener, context::restore));
} catch (Exception e) {
String errorMessage = "Failed to index global_context index";
logger.error(errorMessage);
Expand Down Expand Up @@ -372,7 +371,7 @@ public void putInitialStateToWorkflowState(String workflowId, User user, ActionL
) {
request.source(state.toXContent(builder, ToXContent.EMPTY_PARAMS)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
request.id(workflowId);
client.index(request, ActionListener.runBefore(listener, () -> context.restore()));
client.index(request, ActionListener.runBefore(listener, context::restore));
} catch (Exception e) {
String errorMessage = "Failed to put state index document";
logger.error(errorMessage, e);
Expand Down Expand Up @@ -408,7 +407,7 @@ public void updateTemplateInGlobalContext(String documentId, Template template,
Template encryptedTemplate = encryptorUtils.encryptTemplateCredentials(template);
request.source(encryptedTemplate.toXContent(builder, ToXContent.EMPTY_PARAMS))
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
client.index(request, ActionListener.runBefore(listener, () -> context.restore()));
client.index(request, ActionListener.runBefore(listener, context::restore));
} catch (Exception e) {
String errorMessage = "Failed to update global_context entry : " + documentId;
logger.error(errorMessage, e);
Expand Down Expand Up @@ -441,7 +440,7 @@ public void updateFlowFrameworkSystemIndexDoc(
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
updateRequest.retryOnConflict(3);
// TODO: decide what condition can be considered as an update conflict and add retry strategy
client.update(updateRequest, ActionListener.runBefore(listener, () -> context.restore()));
client.update(updateRequest, ActionListener.runBefore(listener, context::restore));
} catch (Exception e) {
String errorMessage = "Failed to update " + WORKFLOW_STATE_INDEX + " entry : " + documentId;
logger.error(errorMessage, e);
Expand Down Expand Up @@ -475,7 +474,7 @@ public void updateFlowFrameworkSystemIndexDocWithScript(
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
updateRequest.retryOnConflict(3);
// TODO: Implement our own concurrency control to improve on retry mechanism
client.update(updateRequest, ActionListener.runBefore(listener, () -> context.restore()));
client.update(updateRequest, ActionListener.runBefore(listener, context::restore));
} catch (Exception e) {
logger.error("Failed to update {} entry : {}. {}", indexName, documentId, e.getMessage());
listener.onFailure(
Expand Down Expand Up @@ -508,7 +507,7 @@ public void updateResourceInStateIndex(
resourceId
);
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
newResource.toXContent(builder, ToXContentObject.EMPTY_PARAMS);
newResource.toXContent(builder, ToXContent.EMPTY_PARAMS);

// The script to append a new object to the resources_created array
Script script = new Script(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*/
package org.opensearch.flowframework.model;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.util.ParseUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -63,7 +63,7 @@ public static WorkflowValidator parse(XContentParser parser) throws IOException
*/
public static WorkflowValidator parse(String file) throws IOException {
URL url = WorkflowValidator.class.getClassLoader().getResource(file);
String json = Resources.toString(url, Charsets.UTF_8);
String json = Resources.toString(url, StandardCharsets.UTF_8);
return parse(ParseUtils.jsonToParser(json));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.node.NodeClient;
Expand Down Expand Up @@ -70,7 +69,7 @@ public String getName() {

@Override
public List<Route> routes() {
return ImmutableList.of(
return List.of(
// Create new workflow
new Route(RestRequest.Method.POST, String.format(Locale.ROOT, "%s", WORKFLOW_URI)),
// Update use case template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
Expand Down Expand Up @@ -57,7 +56,7 @@ public String getName() {

@Override
public List<Route> routes() {
return ImmutableList.of(new Route(RestRequest.Method.DELETE, String.format(Locale.ROOT, "%s/{%s}", WORKFLOW_URI, WORKFLOW_ID)));
return List.of(new Route(RestRequest.Method.DELETE, String.format(Locale.ROOT, "%s/{%s}", WORKFLOW_URI, WORKFLOW_ID)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
Expand Down Expand Up @@ -101,7 +100,7 @@ protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest request

@Override
public List<Route> routes() {
return ImmutableList.of(
return List.of(
new Route(RestRequest.Method.POST, String.format(Locale.ROOT, "%s/{%s}/%s", WORKFLOW_URI, WORKFLOW_ID, "_deprovision"))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
Expand Down Expand Up @@ -57,7 +56,7 @@ public String getName() {

@Override
public List<Route> routes() {
return ImmutableList.of(new Route(RestRequest.Method.GET, String.format(Locale.ROOT, "%s/{%s}", WORKFLOW_URI, WORKFLOW_ID)));
return List.of(new Route(RestRequest.Method.GET, String.format(Locale.ROOT, "%s/{%s}", WORKFLOW_URI, WORKFLOW_ID)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
Expand Down Expand Up @@ -104,8 +103,6 @@ protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest request

@Override
public List<Route> routes() {
return ImmutableList.of(
new Route(RestRequest.Method.GET, String.format(Locale.ROOT, "%s/{%s}/%s", WORKFLOW_URI, WORKFLOW_ID, "_status"))
);
return List.of(new Route(RestRequest.Method.GET, String.format(Locale.ROOT, "%s/{%s}/%s", WORKFLOW_URI, WORKFLOW_ID, "_status")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.node.NodeClient;
Expand Down Expand Up @@ -59,7 +58,7 @@ public String getName() {

@Override
public List<Route> routes() {
return ImmutableList.of(
return List.of(
// Provision workflow from indexed use case template
new Route(RestRequest.Method.POST, String.format(Locale.ROOT, "%s/{%s}/%s", WORKFLOW_URI, WORKFLOW_ID, "_provision"))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.opensearch.flowframework.common.FlowFrameworkFeatureEnabledSetting;
import org.opensearch.flowframework.model.Template;
import org.opensearch.flowframework.transport.SearchWorkflowAction;

import java.util.List;

import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX;
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_URI;

Expand All @@ -31,7 +32,7 @@ public class RestSearchWorkflowAction extends AbstractSearchWorkflowAction<Templ
*/
public RestSearchWorkflowAction(FlowFrameworkFeatureEnabledSetting flowFrameworkFeatureEnabledSetting) {
super(
ImmutableList.of(SEARCH_WORKFLOW_PATH),
List.of(SEARCH_WORKFLOW_PATH),
GLOBAL_CONTEXT_INDEX,
Template.class,
SearchWorkflowAction.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
*/
package org.opensearch.flowframework.rest;

import com.google.common.collect.ImmutableList;
import org.opensearch.flowframework.common.FlowFrameworkFeatureEnabledSetting;
import org.opensearch.flowframework.model.WorkflowState;
import org.opensearch.flowframework.transport.SearchWorkflowStateAction;

import java.util.List;

import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_STATE_INDEX;
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_URI;

Expand All @@ -31,7 +32,7 @@ public class RestSearchWorkflowStateAction extends AbstractSearchWorkflowAction<
*/
public RestSearchWorkflowStateAction(FlowFrameworkFeatureEnabledSetting flowFrameworkFeatureEnabledSetting) {
super(
ImmutableList.of(SEARCH_WORKFLOW_STATE_PATH),
List.of(SEARCH_WORKFLOW_STATE_PATH),
WORKFLOW_STATE_INDEX,
WorkflowState.class,
SearchWorkflowStateAction.INSTANCE,
Expand Down
Loading
Loading