-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat(eap-sampling): add mv migration for sampled views #6940
Open
volokluev
wants to merge
1
commit into
master
Choose a base branch
from
volo/sampling_matviews_impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−0
Open
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
165 changes: 165 additions & 0 deletions
165
snuba/snuba_migrations/events_analytics_platform/0033_materialize_sampled_storage_views.py
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,165 @@ | ||
from typing import List, Sequence | ||
|
||
from snuba.clickhouse.columns import Array, Column, UInt | ||
from snuba.clusters.storage_sets import StorageSetKey | ||
from snuba.migrations import migration, operations | ||
from snuba.migrations.columns import MigrationModifiers as Modifiers | ||
from snuba.migrations.operations import OperationTarget, SqlOperation | ||
from snuba.utils.schemas import UUID, Bool, DateTime, Float, Int, Map, String | ||
|
||
num_attr_buckets = 40 | ||
|
||
|
||
def hash_map_column_name(attribute_type: str, i: int) -> str: | ||
return f"_hash_map_{attribute_type}_{i}" | ||
|
||
|
||
columns: List[Column[Modifiers]] = [ | ||
Column("organization_id", UInt(64)), | ||
Column("project_id", UInt(64)), | ||
Column("item_type", UInt(8)), | ||
Column("timestamp", DateTime(Modifiers(codecs=["DoubleDelta", "ZSTD(1)"]))), | ||
Column("trace_id", UUID()), | ||
Column("item_id", UInt(128)), | ||
Column("sampling_weight", UInt(64, modifiers=Modifiers(codecs=["ZSTD(1)"]))), | ||
Column( | ||
"retention_days", | ||
UInt(16, modifiers=Modifiers(codecs=["T64", "ZSTD(1)"])), | ||
), | ||
Column( | ||
"attributes_bool", | ||
Map( | ||
String(), | ||
Bool(), | ||
), | ||
), | ||
Column( | ||
"attributes_int", | ||
Map( | ||
String(), | ||
Int(64), | ||
), | ||
), | ||
] | ||
|
||
|
||
columns.extend( | ||
[ | ||
Column( | ||
f"attributes_string_{i}", | ||
Map( | ||
String(), | ||
String(), | ||
modifiers=Modifiers( | ||
codecs=["ZSTD(1)"], | ||
), | ||
), | ||
) | ||
for i in range(num_attr_buckets) | ||
] | ||
) | ||
|
||
columns.extend( | ||
[ | ||
Column( | ||
hash_map_column_name("string", i), | ||
Array( | ||
UInt(64), | ||
), | ||
) | ||
for i in range(num_attr_buckets) | ||
] | ||
) | ||
|
||
columns.extend( | ||
[ | ||
Column( | ||
f"attributes_float_{i}", | ||
Map( | ||
String(), | ||
Float(64), | ||
modifiers=Modifiers( | ||
codecs=["ZSTD(1)"], | ||
), | ||
), | ||
) | ||
for i in range(num_attr_buckets) | ||
] | ||
) | ||
|
||
|
||
columns.extend( | ||
[ | ||
Column( | ||
hash_map_column_name("float", i), | ||
Array( | ||
UInt(64), | ||
), | ||
) | ||
for i in range(num_attr_buckets) | ||
] | ||
) | ||
|
||
|
||
def get_mv_expr(sampling_rate: int) -> str: | ||
""" | ||
we use sampling_weight for our calculations, which is 1/sample_rate which means that when we downsample in storage, the downsampled sampling weight should be multiplied by the downsample rate. | ||
Example: | ||
original_sampling_weight = 1 // this means the sampling rate of the item was 1.0 | ||
tier_1_sampling_rate = 8 // tier 1 takes every 8 items | ||
tier_2_sampling_rate = 64 // tier 2 takes every 8 items from tier 1 | ||
tier_3_sampling_rate = 512 // tier 3 takes every 8 items from tier 2 | ||
""" | ||
column_names_str = ", ".join( | ||
[f"{c.name} AS {c.name}" for c in columns if c.name != "sampling_weight"] | ||
) | ||
|
||
# set sampling weight explicitly in mv | ||
return f"SELECT {column_names_str}, sampling_weight * {sampling_rate} AS sampling_weight FROM eap_items_1_local WHERE (cityHash64(item_id) % {sampling_rate}) = 0" | ||
|
||
|
||
storage_set_name = StorageSetKey.EVENTS_ANALYTICS_PLATFORM | ||
|
||
|
||
class Migration(migration.ClickhouseNodeMigration): | ||
|
||
blocking = False | ||
storage_set_key = StorageSetKey.EVENTS_ANALYTICS_PLATFORM | ||
granularity = "8192" | ||
|
||
sampling_rates = [8, 8**2, 8**3] | ||
|
||
def forwards_ops(self) -> Sequence[SqlOperation]: | ||
ops = [] | ||
for sample_rate in self.sampling_rates: | ||
local_table_name = f"eap_items_1_downsample_{sample_rate}_local" | ||
mv_name = f"eap_items_1_downsample_{sample_rate}_mv" | ||
mv_query = get_mv_expr(sample_rate) | ||
ops.append( | ||
operations.CreateMaterializedView( | ||
storage_set=self.storage_set_key, | ||
view_name=mv_name, | ||
columns=columns, | ||
destination_table_name=local_table_name, | ||
target=OperationTarget.LOCAL, | ||
query=mv_query, | ||
) | ||
) | ||
|
||
return ops | ||
|
||
def backwards_ops(self) -> Sequence[SqlOperation]: | ||
ops = [] | ||
for sample_rate in self.sampling_rates: | ||
mv_name = f"eap_items_1_downsample_{sample_rate}_mv" | ||
|
||
ops.extend( | ||
[ | ||
operations.DropTable( | ||
storage_set=self.storage_set_key, | ||
table_name=mv_name, | ||
target=OperationTarget.LOCAL, | ||
), | ||
] | ||
) | ||
return ops |
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.
Using
item_id
will ensure whatever is in the lowest tier will also be in every other tier. I don't think it's needed to enforce that and I'd rather see a random sampling. Varying the data in each tier might be a little safer in case we have spans that are abnormal.