-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Materialized view implementation #5556
Merged
jihoonson
merged 9 commits into
apache:master
from
zhangxinyu1:feature-materialized-view-1.0
Jun 9, 2018
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9708da8
implement materialized view
3e16a28
modify code according to jihoonson's comments
60d13b0
modify code according to jihoonson's comments - 2
9a88e5e
add documentation about materialized view
f58f136
use new HadoopTuningConfig in pr 5583
dbd4ad3
add minDataLag and fix optimizer bug
7aab45f
correct value of DEFAULT_MIN_DATA_LAG_MS
50d6f4f
modify code according to jihoonson's comments - 3
3623f9b
use the boolean expression instead of if-else
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
114 changes: 114 additions & 0 deletions
114
docs/content/development/extensions-contrib/materialized-view.md
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,114 @@ | ||
--- | ||
layout: doc_page | ||
--- | ||
|
||
# Materialized View | ||
|
||
To use this feature, make sure to only load materialized-view-selection on broker and load materialized-view-maintenance on overlord. In addtion, this feature currently requires a hadoop cluster. | ||
|
||
This feature enables Druid to greatly improve the query performance, especially when the query dataSource has a very large number of dimensions but the query only required several dimensions. This feature includes two parts. One is `materialized-view-maintenance`, and the other is `materialized-view-selection`. | ||
|
||
## Materialized-view-maintenance | ||
In materialized-view-maintenance, dataSouces user ingested are called "base-dataSource". For each base-dataSource, we can submit `derivativeDataSource` supervisors to create and maintain other dataSources which we called "derived-dataSource". The deminsions and metrics of derived-dataSources are the subset of base-dataSource's. | ||
The `derivativeDataSource` supervisor is used to keep the timeline of derived-dataSource consistent with base-dataSource. Each `derivativeDataSource` supervisor is responsible for one derived-dataSource. | ||
|
||
A sample derivativeDataSource supervisor spec is shown below: | ||
```json | ||
{ | ||
"type": "derivativeDataSource", | ||
"baseDataSource": "wikiticker", | ||
"dimensionsSpec": { | ||
"dimensions": [ | ||
"isUnpatrolled", | ||
"metroCode", | ||
"namespace", | ||
"page", | ||
"regionIsoCode", | ||
"regionName", | ||
"user" | ||
] | ||
}, | ||
"metricsSpec": [ | ||
{ | ||
"name": "count", | ||
"type": "count" | ||
}, | ||
{ | ||
"name": "added", | ||
"type": "longSum", | ||
"fieldName": "added" | ||
} | ||
], | ||
"tuningConfig": { | ||
"type": "hadoop" | ||
} | ||
} | ||
``` | ||
|
||
**Supervisor Configuration** | ||
|
||
|Field|Description|Required| | ||
|--------|-----------|---------| | ||
|Type |The supervisor type. This should always be derivativeDataSource |yes| | ||
|baseDataSource |The name of base dataSource. This dataSource data should be already stored inside Druid, and the dataSource will be used as input data. See [dataSource inputSpec](http://druid.io/docs/latest/ingestion/update-existing-data.html#datasource "dataSource inputSpec"). |yes| | ||
|dimensionsSpec |Specifies the dimensions of the data. These dimensions must be the subset of baseDataSource's dimensions. |yes| | ||
|metricsSpec |A list of aggregators. These metrics must be the subset of baseDataSource's metrics. See [aggregations](http://druid.io/docs/latest/querying/aggregations.html "aggregations") |yes| | ||
|tuningConfig |TuningConfig must be HadoopTuningConfig. See [hadoop tuning config]( http://druid.io/docs/latest/ingestion/batch-ingestion.html#tuningconfig "hadoop tuning config") |yes| | ||
|dataSource |The name of this derived dataSource. |no(default=baseDataSource-hashCode of supervisor)| | ||
|hadoopDependencyCoordinates |A JSON array of Hadoop dependency coordinates that Druid will use, this property will override the default Hadoop coordinates. Once specified, Druid will look for those Hadoop dependencies from the location specified by druid.extensions.hadoopDependenciesDir |no| | ||
|classpathPrefix |Classpath that will be pre-appended for the peon process. |no| | ||
|context |See below. |no| | ||
|
||
**Context** | ||
|
||
|Field|Description|Required| | ||
|--------|-----------|---------| | ||
|maxTaskCount |The max number of tasks the supervisor can submit simultaneously. |no(default=1)| | ||
|
||
## Materialized-view-selection | ||
|
||
In materialized-view-selection, we implement a new query type `view`. When we request a view query, Druid will try its best to optimize the query based on query dataSource and intervals. | ||
|
||
A sample view query spec is shown below: | ||
```json | ||
{ | ||
"queryType": "view", | ||
"query": { | ||
"queryType": "groupBy", | ||
"dataSource": "wikiticker", | ||
"granularity": "all", | ||
"dimensions": [ | ||
"user" | ||
], | ||
"limitSpec": { | ||
"type": "default", | ||
"limit": 1, | ||
"columns": [ | ||
{ | ||
"dimension": "added", | ||
"direction": "descending", | ||
"dimensionOrder": "numeric" | ||
} | ||
] | ||
}, | ||
"aggregations": [ | ||
{ | ||
"type": "longSum", | ||
"name": "added", | ||
"fieldName": "added" | ||
} | ||
], | ||
"intervals": [ | ||
"2015-09-12/2015-09-13" | ||
] | ||
} | ||
} | ||
``` | ||
There are 2 parts in a view query: | ||
|
||
|Field|Description|Required| | ||
|--------|-----------|---------| | ||
|queryType |The query type. This should always be view |yes| | ||
|query |The real query of this `view` query. The real query must be [groupby](http://druid.io/docs/latest/querying/groupbyquery.html "groupby")/[topn](http://druid.io/docs/latest/querying/topnquery.html "topn")/[timeseries](http://druid.io/docs/latest/querying/timeseriesquery.html "timeseries") type. |yes| | ||
|
||
**Note that Materialized View is currently designated as experimental. Please make sure the time of all nodes are the same and increase monotonically. Otherwise, some unexpected errors may happen on query results.** |
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,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Licensed to Metamarkets Group Inc. (Metamarkets) under one | ||
~ or more contributor license agreements. See the NOTICE file | ||
~ distributed with this work for additional information | ||
~ regarding copyright ownership. Metamarkets 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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>druid</artifactId> | ||
<groupId>io.druid</groupId> | ||
<version>0.13.0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.druid.extensions.contrib</groupId> | ||
<artifactId>materialized-view-maintenance</artifactId> | ||
<name>materialized-view-maintenance</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.druid</groupId> | ||
<artifactId>druid-server</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.druid</groupId> | ||
<artifactId>druid-indexing-service</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.druid</groupId> | ||
<artifactId>druid-server</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.druid</groupId> | ||
<artifactId>druid-processing</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easymock</groupId> | ||
<artifactId>easymock</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
129 changes: 129 additions & 0 deletions
129
...enance/src/main/java/io/druid/indexing/materializedview/DerivativeDataSourceMetadata.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,129 @@ | ||
/* | ||
* Licensed to Metamarkets Group Inc. (Metamarkets) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Metamarkets 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 io.druid.indexing.materializedview; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Sets; | ||
import io.druid.indexing.overlord.DataSourceMetadata; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class DerivativeDataSourceMetadata implements DataSourceMetadata | ||
{ | ||
private final String baseDataSource; | ||
private final Set<String> dimensions; | ||
private final Set<String> metrics; | ||
|
||
@JsonCreator | ||
public DerivativeDataSourceMetadata( | ||
@JsonProperty("baseDataSource") String baseDataSource, | ||
@JsonProperty("dimensions") Set<String> dimensions, | ||
@JsonProperty("metrics") Set<String> metrics | ||
) | ||
{ | ||
this.baseDataSource = Preconditions.checkNotNull(baseDataSource, "baseDataSource cannot be null. This is not a valid DerivativeDataSourceMetadata."); | ||
this.dimensions = Preconditions.checkNotNull(dimensions, "dimensions cannot be null. This is not a valid DerivativeDataSourceMetadata."); | ||
this.metrics = Preconditions.checkNotNull(metrics, "metrics cannot be null. This is not a valid DerivativeDataSourceMetadata."); | ||
} | ||
|
||
@JsonProperty("baseDataSource") | ||
public String getBaseDataSource() | ||
{ | ||
return baseDataSource; | ||
} | ||
|
||
@JsonProperty("dimensions") | ||
public Set<String> getDimensions() | ||
{ | ||
return dimensions; | ||
} | ||
|
||
@JsonProperty("metrics") | ||
public Set<String> getMetrics() | ||
{ | ||
return metrics; | ||
} | ||
|
||
@Override | ||
public boolean isValidStart() | ||
{ | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean matches(DataSourceMetadata other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the logic is almost same with |
||
{ | ||
return equals(other); | ||
} | ||
|
||
@Override | ||
public DataSourceMetadata plus(DataSourceMetadata other) | ||
{ | ||
throw new UnsupportedOperationException("Derivative dataSource metadata is not allowed to plus"); | ||
} | ||
|
||
@Override | ||
public DataSourceMetadata minus(DataSourceMetadata other) | ||
{ | ||
throw new UnsupportedOperationException("Derivative dataSource metadata is not allowed to minus"); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
DerivativeDataSourceMetadata that = (DerivativeDataSourceMetadata) o; | ||
|
||
return baseDataSource.equals(that.getBaseDataSource()) && | ||
dimensions.equals(that.getDimensions()) && | ||
metrics.equals(that.getMetrics()); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(baseDataSource, dimensions, metrics); | ||
} | ||
|
||
public Set<String> getColumns() | ||
{ | ||
Set<String> fields = Sets.newHashSet(dimensions); | ||
fields.addAll(metrics); | ||
return fields; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please break a line. |
||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "DerivedDataSourceMetadata{" + | ||
"baseDataSource=" + baseDataSource + | ||
", dimensions=" + dimensions + | ||
", metrics=" + metrics + | ||
'}'; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
.../main/java/io/druid/indexing/materializedview/MaterializedViewMaintenanceDruidModule.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,51 @@ | ||
/* | ||
* Licensed to Metamarkets Group Inc. (Metamarkets) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Metamarkets 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 io.druid.indexing.materializedview; | ||
|
||
import com.fasterxml.jackson.databind.Module; | ||
import com.fasterxml.jackson.databind.jsontype.NamedType; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.inject.Binder; | ||
import io.druid.guice.JsonConfigProvider; | ||
import io.druid.initialization.DruidModule; | ||
|
||
import java.util.List; | ||
|
||
public class MaterializedViewMaintenanceDruidModule implements DruidModule | ||
{ | ||
@Override | ||
public List<? extends Module> getJacksonModules() | ||
{ | ||
return ImmutableList.of( | ||
new SimpleModule(getClass().getSimpleName()) | ||
.registerSubtypes( | ||
new NamedType(MaterializedViewSupervisorSpec.class, "derivativeDataSource"), | ||
new NamedType(DerivativeDataSourceMetadata.class, "derivativeDataSource") | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
public void configure(Binder binder) | ||
{ | ||
JsonConfigProvider.bind(binder, "druid.materialized.view.task", MaterializedViewTaskConfig.class); | ||
} | ||
} |
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.
Would you elaborate more on why this feature is split into two extensions? If we need to always load both extensions to use this feature, it would be better to make a single extension.
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.
I can't agree with you more. However,
DataSourceOptimizer
needBrokerServerView
to get the timeline of different dataSources to do optimizing, and only broker has this information. Then, materialized-view-selection module has to be only loaded in broker, so I have to split it into two extensions. I thought about this for a long time, but cannot figure out how to solve this problem. Do you have any suggestions?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.
Do you mean that
materialized-view-maintenance
should be loaded only in overlords whilematerialized-view-selection
should be loaded only in brokers?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.
materialized-view-selection
should be loaded only in brokers, butmaterialized-view-maintenance
can be loaded anywhere.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.
Ah, ok. We don't have a nice way to do this currently.. I think it's fine with going as it is. Would you please add some comments about this, especially
materialized-view-selection
should be loaded only in brokers?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.
Sure, I'm working on your comments these days. Thanks very much!