-
Notifications
You must be signed in to change notification settings - Fork 96
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
Generify DruidDimensionLoader #449
Merged
garyluoex
merged 7 commits into
yahoo:master
from
kevinhinterlong:cleanupDruidDimensionLoader
Aug 16, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85edf57
Cleanup Druid Dimension Loader
kevinhinterlong 1cc7be6
Address my own comments :hand:
kevinhinterlong a7af922
Generify the DruidDimensionLoader
kevinhinterlong 7ac5eee
Fix usages of DruidDimensionLoader and rename -> DimensionLoader
kevinhinterlong 5bc1861
Address comments
kevinhinterlong cdcaebb
Remove unused imports and add changelog
kevinhinterlong cbcc50c
Address comments
kevinhinterlong 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
This file contains 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
This file contains 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
71 changes: 71 additions & 0 deletions
71
fili-core/src/main/java/com/yahoo/bard/webservice/application/DimensionValueLoadTask.java
This file contains 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,71 @@ | ||
// Copyright 2016 Yahoo Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.application; | ||
|
||
import com.yahoo.bard.webservice.config.SystemConfig; | ||
import com.yahoo.bard.webservice.config.SystemConfigProvider; | ||
import com.yahoo.bard.webservice.druid.client.FailureCallback; | ||
import com.yahoo.bard.webservice.druid.client.HttpErrorCallback; | ||
|
||
import org.joda.time.DateTime; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import javax.inject.Singleton; | ||
|
||
/** | ||
* DimensionLoaderTask takes a collection of {@link DimensionValueLoader} to update the values for dimensions. | ||
*/ | ||
@Singleton | ||
public class DimensionValueLoadTask extends LoadTask<Boolean> { | ||
private static final SystemConfig SYSTEM_CONFIG = SystemConfigProvider.getInstance(); | ||
|
||
public static final String LOADER_TIMER_DURATION = | ||
SYSTEM_CONFIG.getPackageVariableName("druid_dim_loader_timer_duration"); | ||
public static final String LOADER_TIMER_DELAY = | ||
SYSTEM_CONFIG.getPackageVariableName("druid_dim_loader_timer_delay"); | ||
|
||
private final AtomicReference<DateTime> lastRunTimestamp; | ||
private final Collection<DimensionValueLoader> dimensionRowProviders; | ||
|
||
/** | ||
* DimensionLoaderTask tells all of the {@link DimensionValueLoader}s to update and add values to the dimension | ||
* cache. | ||
* | ||
* @param dimensionRowProviders A Collection of {@link DimensionValueLoader} to initialize dimensions. | ||
*/ | ||
public DimensionValueLoadTask(Collection<DimensionValueLoader> dimensionRowProviders) { | ||
super( | ||
DimensionValueLoadTask.class.getSimpleName(), | ||
SYSTEM_CONFIG.getLongProperty(LOADER_TIMER_DELAY, 0), | ||
SYSTEM_CONFIG.getLongProperty( | ||
LOADER_TIMER_DURATION, | ||
TimeUnit.MINUTES.toMillis(1) | ||
) | ||
); | ||
|
||
this.dimensionRowProviders = dimensionRowProviders; | ||
lastRunTimestamp = new AtomicReference<>(); | ||
|
||
HttpErrorCallback errorCallback = getErrorCallback(); | ||
FailureCallback failureCallback = getFailureCallback(); | ||
|
||
dimensionRowProviders.forEach(dimensionRowProvider -> { | ||
dimensionRowProvider.setErrorCallback(errorCallback); | ||
dimensionRowProvider.setFailureCallback(failureCallback); | ||
}); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
dimensionRowProviders.forEach(DimensionValueLoader::load); | ||
// tell all dimensionRowProviders to load | ||
lastRunTimestamp.set(DateTime.now()); | ||
} | ||
|
||
public DateTime getLastRunTimestamp() { | ||
return lastRunTimestamp.get(); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
fili-core/src/main/java/com/yahoo/bard/webservice/application/DimensionValueLoader.java
This file contains 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,113 @@ | ||
// Copyright 2017 Yahoo Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.application; | ||
|
||
import com.yahoo.bard.webservice.data.dimension.Dimension; | ||
import com.yahoo.bard.webservice.data.dimension.DimensionRow; | ||
import com.yahoo.bard.webservice.druid.client.FailureCallback; | ||
import com.yahoo.bard.webservice.druid.client.HttpErrorCallback; | ||
import com.yahoo.bard.webservice.druid.model.datasource.DataSource; | ||
|
||
import org.joda.time.DateTime; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* The DimensionValueLoader provides a way to update dimension values. | ||
*/ | ||
public interface DimensionValueLoader { | ||
Logger LOG = LoggerFactory.getLogger(DimensionValueLoader.class); | ||
|
||
/** | ||
* Checks if a {@link Dimension} exists in a {@link DataSource}. | ||
* | ||
* @param dimension The dimension to look for in the datasource. | ||
* @param dataSource The datasource to look through for the dimension. | ||
* | ||
* @return true if the dimension was found. | ||
*/ | ||
default boolean dimensionExistsInDataSource(Dimension dimension, DataSource dataSource) { | ||
return dataSource.getPhysicalTable() | ||
.getDimensions() | ||
.stream() | ||
.anyMatch(dimension::equals); | ||
} | ||
|
||
/** | ||
* Start loading all dimensions. | ||
*/ | ||
default void load() { | ||
getDimensions().stream() | ||
.peek(dimension -> LOG.trace("Querying values for dimension: {}", dimension)) | ||
.forEach(this::queryDimension); | ||
} | ||
|
||
/** | ||
* Queries a specific dimension. This only queries against a {@link DataSource} if it's table contains the given | ||
* {@link Dimension}. | ||
* | ||
* @param dimension The dimension to load values for. | ||
*/ | ||
default void queryDimension(Dimension dimension) { | ||
getDataSources().stream() | ||
.filter(dataSource -> dimensionExistsInDataSource(dimension, dataSource)) | ||
.forEach(dataSource -> query(dimension, dataSource)); | ||
} | ||
|
||
/** | ||
* Queries for a specific {@link Dimension} against the given {@link DataSource}. | ||
* | ||
* @param dimension The dimension to load. | ||
* @param dataSource The datasource to query values for. | ||
*/ | ||
void query(Dimension dimension, DataSource dataSource); | ||
|
||
/** | ||
* Set a callback if an error occurs while querying. | ||
* | ||
* @param errorCallback The callback to invoke on http errors. | ||
*/ | ||
void setErrorCallback(HttpErrorCallback errorCallback); | ||
|
||
/** | ||
* Set a callback if an exception occurs while querying. | ||
* | ||
* @param failureCallback The callback to invoke on exceptions. | ||
*/ | ||
void setFailureCallback(FailureCallback failureCallback); | ||
|
||
/** | ||
* Gets the list of dimensions to load. | ||
* | ||
* @return the list of dimensions. | ||
*/ | ||
Set<Dimension> getDimensions(); | ||
|
||
/** | ||
* Gets the list of datasources to query against. | ||
* | ||
* @return the list of datasources. | ||
*/ | ||
Set<DataSource> getDataSources(); | ||
|
||
/** | ||
* Tell the dimension it's been updated. | ||
* | ||
* @param dimension The dimension to update saying it's been loaded. | ||
*/ | ||
default void updateDimension(Dimension dimension) { | ||
dimension.setLastUpdated(DateTime.now()); | ||
} | ||
|
||
/** | ||
* Adds dimension row values to a dimension. | ||
* | ||
* @param dimension The dimension to add the row to. | ||
* @param dimensionRow The dimension row to be added. | ||
*/ | ||
default void updateDimensionWithValue(Dimension dimension, DimensionRow dimensionRow) { | ||
dimension.addDimensionRow(dimensionRow); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to keep two separate method for dimension loader setup, would be nice to change this name also.