-
Notifications
You must be signed in to change notification settings - Fork 273
Extract DelegateRefresher to deduplicate JSON resource wrapper logic #1870
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
external/opensearch/src/main/java/org/apache/stormcrawler/opensearch/DelegateRefresher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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. | ||
| */ | ||
|
|
||
| package org.apache.stormcrawler.opensearch; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.Timer; | ||
| import java.util.TimerTask; | ||
| import org.apache.stormcrawler.JSONResource; | ||
| import org.opensearch.action.get.GetRequest; | ||
| import org.opensearch.action.get.GetResponse; | ||
| import org.opensearch.client.RequestOptions; | ||
| import org.opensearch.client.RestHighLevelClient; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Loads a delegate class that implements both a required base type and {@link JSONResource}, then | ||
| * periodically refreshes its configuration from OpenSearch. Used by {@link | ||
| * org.apache.stormcrawler.opensearch.filtering.JSONURLFilterWrapper} and {@link | ||
| * org.apache.stormcrawler.opensearch.parse.filter.JSONResourceWrapper} to eliminate duplicated | ||
| * setup/refresh/cleanup logic. | ||
| * | ||
| * @param <T> the base type that the delegate must extend (e.g. URLFilter or ParseFilter) | ||
| */ | ||
| public class DelegateRefresher<T> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(DelegateRefresher.class); | ||
|
|
||
| private final T delegate; | ||
| private Timer refreshTimer; | ||
| private RestHighLevelClient osClient; | ||
|
|
||
| /** | ||
| * Creates a refresher by loading the delegate class from the JSON configuration. | ||
| * | ||
| * @param baseType the required base class (e.g. URLFilter.class or ParseFilter.class) | ||
| * @param stormConf the Storm configuration map | ||
| * @param filterParams the JSON params node containing "delegate" and optional "refresh" | ||
| * @param configurer callback to configure the delegate after instantiation | ||
| */ | ||
| public DelegateRefresher( | ||
| Class<T> baseType, | ||
| Map<String, Object> stormConf, | ||
| JsonNode filterParams, | ||
| DelegateConfigure<T> configurer) { | ||
|
|
||
| JsonNode delegateNode = filterParams.get("delegate"); | ||
| if (delegateNode == null) { | ||
| throw new RuntimeException("delegateNode undefined!"); | ||
| } | ||
|
|
||
| String delegateClassName = null; | ||
| JsonNode node = delegateNode.get("class"); | ||
| if (node != null && node.isTextual()) { | ||
| delegateClassName = node.asText(); | ||
| } | ||
| if (delegateClassName == null) { | ||
| throw new RuntimeException(baseType.getSimpleName() + " delegate class undefined!"); | ||
| } | ||
|
|
||
| try { | ||
| Class<?> filterClass = Class.forName(delegateClassName); | ||
|
|
||
| if (!baseType.isAssignableFrom(filterClass)) { | ||
| throw new RuntimeException( | ||
| "Filter " + delegateClassName + " does not extend " + baseType.getName()); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| T instance = (T) filterClass.getDeclaredConstructor().newInstance(); | ||
|
|
||
| if (!(instance instanceof JSONResource)) { | ||
| throw new RuntimeException( | ||
| "Filter " + delegateClassName + " does not implement JSONResource"); | ||
| } | ||
|
|
||
| this.delegate = instance; | ||
| } catch (RuntimeException e) { | ||
| throw e; | ||
| } catch (Exception e) { | ||
| LOG.error("Can't setup {}: {}", delegateClassName, e); | ||
| throw new RuntimeException("Can't setup " + delegateClassName, e); | ||
| } | ||
|
|
||
| // configure the delegate | ||
| JsonNode paramsNode = delegateNode.get("params"); | ||
| configurer.configure(delegate, stormConf, paramsNode); | ||
|
|
||
| // set up periodic refresh from OpenSearch | ||
| int refreshRate = 600; | ||
| node = filterParams.get("refresh"); | ||
| if (node != null && (node.isInt() || node.isTextual())) { | ||
| refreshRate = node.asInt(refreshRate); | ||
| } | ||
|
|
||
| final JSONResource resource = (JSONResource) delegate; | ||
|
|
||
| refreshTimer = new Timer(); | ||
| refreshTimer.schedule( | ||
| new TimerTask() { | ||
| public void run() { | ||
| if (osClient == null) { | ||
| try { | ||
| osClient = OpenSearchConnection.getClient(stormConf, "config"); | ||
| } catch (Exception e) { | ||
| LOG.error("Exception while creating OpenSearch connection", e); | ||
| } | ||
| } | ||
| if (osClient != null) { | ||
| LOG.info("Reloading json resources from OpenSearch"); | ||
| try { | ||
| GetResponse response = | ||
| osClient.get( | ||
| new GetRequest( | ||
| "config", resource.getResourceFile()), | ||
| RequestOptions.DEFAULT); | ||
| resource.loadJSONResources( | ||
| new ByteArrayInputStream(response.getSourceAsBytes())); | ||
| } catch (Exception e) { | ||
| LOG.error("Can't load config from OpenSearch", e); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| refreshRate * 1000L, | ||
| refreshRate * 1000L); | ||
| } | ||
|
|
||
| /** Returns the delegate instance. */ | ||
| public T getDelegate() { | ||
| return delegate; | ||
| } | ||
|
|
||
| /** Cancels the refresh timer and closes the OpenSearch client. */ | ||
| public void cleanup() { | ||
| if (refreshTimer != null) { | ||
| refreshTimer.cancel(); | ||
| } | ||
| if (osClient != null) { | ||
| try { | ||
| osClient.close(); | ||
| } catch (IOException e) { | ||
| LOG.error("Exception when closing OpenSearch client", e); | ||
| } | ||
| osClient = null; | ||
| } | ||
| } | ||
|
|
||
| /** Callback interface for configuring the delegate after instantiation. */ | ||
| @FunctionalInterface | ||
| public interface DelegateConfigure<T> { | ||
| void configure(T delegate, Map<String, Object> stormConf, JsonNode params); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.