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

Generify DruidDimensionLoader #449

Merged
merged 7 commits into from
Aug 16, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Current

### Changed:

- [DruidDimensionLoader is now a more generic DimensionValueLoadTask](https://github.com/yahoo/fili/pull/449)
* The `DimensionValueLoadTask` takes in a collection of `DimensionValueLoader`s to allow for non-Druid dimensions to be loaded.

- [DruidQuery::getInnerQuery and Datasource::getQuery return Optional](https://github.com/yahoo/fili/pull/485)
* Returning `Optional` is more correct for their usage and should protect against unexpected null values.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import com.yahoo.bard.webservice.druid.util.FieldConverterSupplier;
import com.yahoo.bard.webservice.druid.util.FieldConverters;
import com.yahoo.bard.webservice.druid.util.SketchFieldConverter;
import com.yahoo.bard.webservice.metadata.DataSourceMetadataLoader;
import com.yahoo.bard.webservice.metadata.DataSourceMetadataLoadTask;
import com.yahoo.bard.webservice.metadata.DataSourceMetadataService;
import com.yahoo.bard.webservice.metadata.QuerySigningService;
import com.yahoo.bard.webservice.metadata.RequestedIntervalsFunction;
Expand Down Expand Up @@ -283,7 +283,7 @@ protected void configure() {
);

if (DRUID_COORDINATOR_METADATA.isOn()) {
DataSourceMetadataLoader dataSourceMetadataLoader = buildDataSourceMetadataLoader(
DataSourceMetadataLoadTask dataSourceMetadataLoader = buildDataSourceMetadataLoader(
metadataDruidWebService,
loader.getPhysicalTableDictionary(),
dataSourceMetadataService,
Expand Down Expand Up @@ -314,12 +314,12 @@ protected void configure() {
bind(buildResponseWriter(getMappers())).to(ResponseWriter.class);

if (DRUID_DIMENSIONS_LOADER.isOn()) {
DruidDimensionsLoader druidDimensionsLoader = buildDruidDimensionsLoader(
DimensionValueLoadTask dimensionLoader = buildDruidDimensionsLoader(
nonUiDruidWebService,
loader.getPhysicalTableDictionary(),
loader.getDimensionDictionary()
);
setupDruidDimensionsLoader(healthCheckRegistry, druidDimensionsLoader);
setupDruidDimensionsLoader(healthCheckRegistry, dimensionLoader);
}
if (SYSTEM_CONFIG.getBooleanProperty(DEPRECATED_PERMISSIVE_AVAILABILITY_FLAG, false)) {
LOG.warn(
Expand Down Expand Up @@ -666,13 +666,13 @@ protected Map<Class, RequestedIntervalsFunction> buildSigningFunctions() {
*
* @return A datasource metadata loader.
*/
protected DataSourceMetadataLoader buildDataSourceMetadataLoader(
protected DataSourceMetadataLoadTask buildDataSourceMetadataLoader(
DruidWebService webService,
PhysicalTableDictionary physicalTableDictionary,
DataSourceMetadataService metadataService,
ObjectMapper mapper
) {
return new DataSourceMetadataLoader(
return new DataSourceMetadataLoadTask(
physicalTableDictionary,
metadataService,
webService,
Expand All @@ -681,24 +681,25 @@ protected DataSourceMetadataLoader buildDataSourceMetadataLoader(
}

/**
* Build a DruidDimensionsLoader.
* Build a DimensionValueLoadTask.
*
* @param webService The web service used by the loader to query dimension values
* @param physicalTableDictionary The table to update dimensions on
* @param dimensionDictionary The dimensions to update
*
* @return A DruidDimensionsLoader
* @return A DimensionValueLoadTask
*/
protected DruidDimensionsLoader buildDruidDimensionsLoader(
protected DimensionValueLoadTask buildDruidDimensionsLoader(
DruidWebService webService,
PhysicalTableDictionary physicalTableDictionary,
DimensionDictionary dimensionDictionary
) {
return new DruidDimensionsLoader(
DruidDimensionValueLoader druidDimensionRowProvider = new DruidDimensionValueLoader(
physicalTableDictionary,
dimensionDictionary,
webService
);
return new DimensionValueLoadTask(Collections.singletonList(druidDimensionRowProvider));
}

/**
Expand All @@ -709,7 +710,7 @@ protected DruidDimensionsLoader buildDruidDimensionsLoader(
*/
protected final void setupDataSourceMetaData(
HealthCheckRegistry healthCheckRegistry,
DataSourceMetadataLoader dataSourceMetadataLoader
DataSourceMetadataLoadTask dataSourceMetadataLoader
) {
scheduleLoader(dataSourceMetadataLoader);

Expand All @@ -722,20 +723,20 @@ protected final void setupDataSourceMetaData(
}

/**
* Schedule DruidDimensionsLoader and register its health check.
* Schedule DimensionValueLoadTask and register its health check.
*
* @param healthCheckRegistry The health check registry to register Dimension lookup health checks
* @param dataDruidDimensionsLoader The DruidDimensionLoader used for monitoring and health checks
* @param dataDimensionLoader The DruidDimensionLoader used for monitoring and health checks
*/
protected final void setupDruidDimensionsLoader(
Copy link
Collaborator

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.

HealthCheckRegistry healthCheckRegistry,
DruidDimensionsLoader dataDruidDimensionsLoader
DimensionValueLoadTask dataDimensionLoader
) {
scheduleLoader(dataDruidDimensionsLoader);
scheduleLoader(dataDimensionLoader);

// Register DruidDimensionsLoader health check
// Register DimensionValueLoadTask health check
HealthCheck druidDimensionsLoaderHealthCheck = new DruidDimensionsLoaderHealthCheck(
dataDruidDimensionsLoader,
dataDimensionLoader,
DRUID_DIM_LOADER_HC_LAST_RUN_PERIOD_MILLIS
);
healthCheckRegistry.register(HEALTH_CHECK_NAME_DRUID_DIM_LOADER, druidDimensionsLoaderHealthCheck);
Expand Down Expand Up @@ -1112,20 +1113,20 @@ protected void afterBinding(AbstractBinder abstractBinder) {
}

/**
* Schedule a loader task.
* Schedule a loadTask task.
*
* @param loader The loader task to run.
* @param loadTask The loadTask task to run.
*/
protected void scheduleLoader(Loader<?> loader) {
loader.setFuture(
loader.isPeriodic ?
protected void scheduleLoader(LoadTask<?> loadTask) {
loadTask.setFuture(
loadTask.isPeriodic ?
loaderScheduler.scheduleAtFixedRate(
loader,
loader.getDefinedDelay(),
loader.getDefinedPeriod(),
loadTask,
loadTask.getDefinedDelay(),
loadTask.getDefinedPeriod(),
TimeUnit.MILLISECONDS
) :
loaderScheduler.schedule(loader, loader.getDefinedDelay(), TimeUnit.MILLISECONDS)
loaderScheduler.schedule(loadTask, loadTask.getDefinedDelay(), TimeUnit.MILLISECONDS)
);
}

Expand Down
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();
}
}
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);
}
}
Loading