Skip to content

Commit 10af797

Browse files
author
Thomas Mahlberg
committed
Add warnings for MVs
1 parent a64f13b commit 10af797

10 files changed

Lines changed: 82 additions & 11 deletions

File tree

KustoSchemaTools/Changes/BaseChange.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ protected BaseChange(string entityType, string entity, T from, T to)
2020
public List<DatabaseScriptContainer> Scripts { get; set; } = new List<DatabaseScriptContainer>();
2121

2222

23-
public string Markdown { get; protected set; }
23+
public string Markdown { get; set; }
2424

2525
public bool IsAsync { get; set; }
26-
26+
public Comment Comment { get; set; }
2727
}
2828

2929
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace KustoSchemaTools.Changes
2+
{
3+
public class Comment
4+
{
5+
public string Text { get; set; }
6+
public bool FailsRollout { get; set; }
7+
public CommentKind Kind { get; set; }
8+
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace KustoSchemaTools.Changes
2+
{
3+
public enum CommentKind
4+
{
5+
Note,
6+
Tip,
7+
Important,
8+
Warning,
9+
Caution
10+
}
11+
}

KustoSchemaTools/Changes/DatabaseChanges.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Kusto.Language;
1+
using Kusto.Cloud.Platform.Utils;
2+
using Kusto.Language;
23
using KustoSchemaTools.Model;
34
using Microsoft.Extensions.Logging;
45
using System.Data;
@@ -47,7 +48,43 @@ public static List<IChange> GenerateChanges(Database oldState, Database newState
4748
result.AddRange(GenerateDeletions(oldState, newState.Deletions, log));
4849

4950
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.Tables, nameof(newState.Tables), log, (oldItem, newItem) => oldItem != null || newItem.Columns?.Any() == true));
50-
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.MaterializedViews, nameof(newState.MaterializedViews), log));
51+
var mvChanges = GenerateScriptCompareChanges(oldState, newState, db => db.MaterializedViews, nameof(newState.MaterializedViews), log);
52+
foreach(var mvChange in mvChanges)
53+
{
54+
var relevantChange = mvChange.Scripts.FirstOrDefault(itm => itm.Kind== "CreateMaterializedViewAsync");
55+
if (relevantChange == null)
56+
continue;
57+
58+
59+
var newMv = newState.MaterializedViews[mvChange.Entity];
60+
61+
var specificCache = (newMv.Kind== "table" ?
62+
(newState.Tables.ContainsKey(newMv.Source) ? newState.Tables[newMv.Source].Policies?.HotCache : null) :
63+
(newState.MaterializedViews.ContainsKey(newMv.Source) ? newState.MaterializedViews[newMv.Source].Policies?.HotCache : null))
64+
?? newState.DefaultRetentionAndCache?.HotCache;
65+
66+
if(specificCache != null && specificCache.EndsWith("d") && int.TryParse(specificCache.TrimEnd("d"), out int lookBackInDays) && DateTime.TryParse(newMv.EffectiveDateTime, out var effectiveDateTime))
67+
{
68+
if(DateTime.UtcNow.AddDays(-lookBackInDays) < effectiveDateTime)
69+
{
70+
// Backfill will work
71+
var validUntil = effectiveDateTime.AddDays(lookBackInDays);
72+
mvChange.Comment = new Comment { FailsRollout = false, Kind = CommentKind.Note, Text = $"The materialized view {mvChange.Entity} is created with backfill configured. All required data is available in hot cache and the rollout is expected to succeed as long as it is rolled out before {validUntil:yyyy-MM-dd HH:mm}UTC. The rollout is executed asynchronusly, depending on the size of the backfill it might take a while." };
73+
}
74+
else
75+
{
76+
// Backfill will fail
77+
var validUntil = DateTime.UtcNow.Date.AddDays(1-lookBackInDays);
78+
mvChange.Comment = new Comment { FailsRollout = true, Kind = CommentKind.Caution, Text = $"Not all data for the backfill of {mvChange.Entity} is available hot. The backfill will fail! Please set the effective Date of the MV to {validUntil:yyyy-MM-dd} or newer." };
79+
}
80+
}
81+
else
82+
{
83+
mvChange.Comment = new Comment { FailsRollout = false, Kind = CommentKind.Warning, Text = $"The conditions for backfilling {mvChange.Entity} couldn't be validated. Please check for errors!" };
84+
}
85+
}
86+
87+
result.AddRange(mvChanges);
5188
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.ContinuousExports, nameof(newState.ContinuousExports), log));
5289
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.Functions, nameof(newState.Functions), log));
5390
result.AddRange(GenerateScriptCompareChanges(oldState, newState, db => db.ExternalTables, nameof(newState.ExternalTables), log));
@@ -63,7 +100,6 @@ public static List<IChange> GenerateChanges(Database oldState, Database newState
63100
}
64101
}
65102

66-
67103
return result;
68104
}
69105

KustoSchemaTools/Changes/DeletionChange.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ public string Markdown
5151
}
5252

5353

54+
public Comment Comment { get; set; }
5455
}
5556
}

KustoSchemaTools/Changes/FollowerChange.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ public BasicChange(string entityType, string entity, string markdown, List<Datab
2525

2626
public List<DatabaseScriptContainer> Scripts { get; set; }
2727

28+
public Comment Comment { get; set; }
2829
}
2930
}

KustoSchemaTools/Changes/Heading.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public Heading(string title)
1313
public List<DatabaseScriptContainer> Scripts => new List<DatabaseScriptContainer> { };
1414

1515
public string Markdown => $"# {Entity}";
16+
public Comment Comment { get; set; }
1617

1718
}
1819

KustoSchemaTools/Changes/IChange.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IChange
99

1010
public string Markdown { get; }
1111

12-
}
12+
public Comment Comment { get; set; }
1313

14-
}
14+
}
15+
}

KustoSchemaTools/KustoSchemaHandler.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ public KustoSchemaHandler(ILogger<KustoSchemaHandler<T>> schemaHandlerLogger, Ya
4747
var kustoDb = await dbHandler.LoadAsync();
4848
var changes = DatabaseChanges.GenerateChanges(kustoDb, yamlDb, escapedDbName, Log);
4949

50-
isValid &= changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false));
50+
var comments = changes.Select(itm => itm.Comment).Where(itm => itm != null).ToList();
51+
52+
53+
isValid &= changes.All(itm => itm.Scripts.All(itm => itm.IsValid != false)) && comments.All(itm => itm.FailsRollout == false);
5154

5255
sb.AppendLine($"# {cluster.Name}/{escapedDbName} ({cluster.Url})");
5356

54-
if(changes.Count == 0)
57+
foreach (var comment in comments)
58+
{
59+
sb.AppendLine($"> [!{comment.Kind.ToString().ToUpper()}]");
60+
sb.AppendLine($"> {comment.Text}");
61+
sb.AppendLine();
62+
}
63+
64+
if (changes.Count == 0)
5565
{
5666
sb.AppendLine("No changes detected");
5767
}

KustoSchemaTools/Model/MaterializedView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public List<DatabaseScriptContainer> CreateScripts(string name, bool isNew)
4747

4848
if (asyncSetup)
4949
{
50-
scripts.Add(new DatabaseScriptContainer("CreateMaterializedView", Kind == "table" ? 40 : 41, $".create async ifnotexists materialized-view with ({properties}) {name} on {Kind} {Source} {{ {Query} }}", true));
50+
scripts.Add(new DatabaseScriptContainer("CreateMaterializedViewAsync", Kind == "table" ? 40 : 41, $".create async ifnotexists materialized-view with ({properties}) {name} on {Kind} {Source} {{ {Query} }}", true));
5151
}
5252
else
5353
{
54-
scripts.Add(new DatabaseScriptContainer("CreateMaterializedView", Kind == "table" ? 40 : 41, $".create-or-alter materialized-view with ({properties}) {name} on {Kind} {Source} {{ {Query} }}"));
54+
scripts.Add(new DatabaseScriptContainer("CreateAlterMaterializedView", Kind == "table" ? 40 : 41, $".create-or-alter materialized-view with ({properties}) {name} on {Kind} {Source} {{ {Query} }}"));
5555
}
5656
if (Policies != null)
5757
{

0 commit comments

Comments
 (0)