Skip to content
Open
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
1 change: 1 addition & 0 deletions stovepipe/core/issuelog/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_test(
srcs = ["materializer_test.go"],
embed = [":go_default_library"],
deps = [
"//platform/metrics:go_default_library",
"//stovepipe/entity:go_default_library",
"//stovepipe/extension/storage:go_default_library",
"//stovepipe/extension/storage/mock:go_default_library",
Expand Down
16 changes: 16 additions & 0 deletions stovepipe/core/issuelog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ Each iteration reads the open-issue row before the summary. Otherwise, a worker
The two projections are not updated atomically. A future list reader must still verify that each referenced summary is open and its opening matches. No status filtering, cross-record transaction, or queue-wide scan is required to maintain one open-issue row.

This materializer does not backfill existing history. The [open-issues API](../../../doc/rfc/stovepipe/open-issues-api.md) reads the lookup and validates referenced summaries. Source builds must remain readable while their transitions can be replayed.

## Lifecycle metrics

Under `issue_log_materializer.issue_lifecycle`, `opened` counts openings, including
reopenings, and `closed` counts closures. Repeated reports do not change these counters.
Metrics carry the processing context's queue tag, not issue identities.

`time_to_resolution` records opening-to-closure duration when the preceding summary
is open and belongs to the same occurrence. Reopenings start a new duration;
clock skew is clamped to zero. Missing or superseded openings produce no duration.
Buckets span 1 minute to 30 days, with finer bands below an hour.

Emission follows a successful conditional summary write, so retries and projection
repairs do not repeat observations. Metrics are best-effort: a crash or ambiguous
storage response between persistence and emission can lose a sample. Older history
that cannot advance the summary is not backfilled into metrics.
46 changes: 44 additions & 2 deletions stovepipe/core/issuelog/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ import (
"github.com/uber/submitqueue/stovepipe/extension/storage"
)

var _issueResolutionBuckets = tally.DurationBuckets{
time.Minute,
5 * time.Minute,
10 * time.Minute,
15 * time.Minute,
30 * time.Minute,
45 * time.Minute,
time.Hour,
2 * time.Hour,
4 * time.Hour,
8 * time.Hour,
12 * time.Hour,
24 * time.Hour,
48 * time.Hour,
7 * 24 * time.Hour,
14 * 24 * time.Hour,
30 * 24 * time.Hour,
}

// Materializer retains issue events and repairs lifecycle and request-level projections.
type Materializer interface {
// PersistLog repairs projections even when the event already exists.
Expand Down Expand Up @@ -90,7 +109,7 @@ func (m *materializer) PersistLog(ctx context.Context, stores storage.Storage, l
if err := summary.Validate(); err != nil {
return fmt.Errorf("invalid issue summary: %w", err)
}
if err := materializeIssueSummary(ctx, stores.GetIssueSummaryStore(), summary); err != nil {
if err := m.materializeIssueSummary(ctx, stores.GetIssueSummaryStore(), summary); err != nil {
return err
}
if err := materializeOpenIssue(ctx, stores, summary.TaskID, summary.UnitID); err != nil {
Expand Down Expand Up @@ -133,7 +152,7 @@ func retainIssueLog(ctx context.Context, logs storage.IssueLogStore, log entity.
return log, nil
}

func materializeIssueSummary(ctx context.Context, summaries storage.IssueSummaryStore, incoming entity.IssueSummary) error {
func (m *materializer) materializeIssueSummary(ctx context.Context, summaries storage.IssueSummaryStore, incoming entity.IssueSummary) error {
for {
if err := ctx.Err(); err != nil {
return err
Expand All @@ -145,6 +164,7 @@ func materializeIssueSummary(ctx context.Context, summaries storage.IssueSummary
} else if err != nil {
return fmt.Errorf("create issue summary: %w", err)
}
m.recordLifecycleTransition(ctx, entity.IssueSummary{}, incoming)
return nil
}
if err != nil {
Expand Down Expand Up @@ -182,10 +202,32 @@ func materializeIssueSummary(ctx context.Context, summaries storage.IssueSummary
} else if err != nil {
return fmt.Errorf("replace issue summary: %w", err)
}
replacement.Version = current.Version + 1
m.recordLifecycleTransition(ctx, current, replacement)
return nil
}
}

func (m *materializer) recordLifecycleTransition(ctx context.Context, before, after entity.IssueSummary) {
if before.Status == after.Status && before.FirstSeenBuildID == after.FirstSeenBuildID {
return
}
tags := metrics.TagsFromContext(ctx)
switch after.Status {
case entity.IssueStatusOpen:
metrics.NamedCounter(m.scope, "issue_lifecycle", "opened", 1, tags...)
case entity.IssueStatusClosed:
metrics.NamedCounter(m.scope, "issue_lifecycle", "closed", 1, tags...)
if before.Status != entity.IssueStatusOpen || before.FirstSeenBuildID != after.FirstSeenBuildID {
return
}
elapsed := max(time.Duration(after.ChangedAtMs-before.ChangedAtMs)*time.Millisecond, 0)
metrics.NamedHistogram(
m.scope, "issue_lifecycle", "time_to_resolution", _issueResolutionBuckets, tags...,
).RecordDuration(elapsed)
}
}

func materializeOpenIssue(ctx context.Context, stores storage.Storage, taskID, unitID string) error {
openIssues := stores.GetOpenIssueStore()
summaries := stores.GetIssueSummaryStore()
Expand Down
184 changes: 184 additions & 0 deletions stovepipe/core/issuelog/materializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"context"
"math"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber-go/tally"
"github.com/uber/submitqueue/platform/metrics"
"github.com/uber/submitqueue/stovepipe/entity"
"github.com/uber/submitqueue/stovepipe/extension/storage"
storagemock "github.com/uber/submitqueue/stovepipe/extension/storage/mock"
Expand Down Expand Up @@ -618,3 +620,185 @@ func TestLegacyHistoryDoesNotInventRequestSummary(t *testing.T) {
require.Equal(t, entity.IssueStatusOpen, f.summary.Status)
require.Empty(t, f.requestSummaries)
}

func TestMaterializerLifecycleMetrics(t *testing.T) {
f := newMaterializerFixture(t)
scope := tally.NewTestScope("", nil)
f.m = NewMaterializer(scope)
ctx := metrics.WithContextTags(context.Background(), metrics.NewTag("queue", "q"))

opened := testIssueLog("build/z", entity.IssueStatusOpen)
closed := testIssueLog("build/a", entity.IssueStatusClosed)
closed.TimestampMs = opened.TimestampMs + (10 * time.Minute).Milliseconds()
reopened := testIssueLog("build/m", entity.IssueStatusOpen)
reopened.TimestampMs = closed.TimestampMs + (5 * time.Minute).Milliseconds()
resolved := testIssueLog("build/m", entity.IssueStatusClosed)
resolved.TimestampMs = reopened.TimestampMs + (2 * time.Hour).Milliseconds()
reported := entity.IssueLog{TaskID: opened.TaskID, UnitID: opened.UnitID, BuildID: closed.BuildID,
Event: entity.IssueEventReported, TimestampMs: opened.TimestampMs + (5 * time.Minute).Milliseconds()}

for _, step := range []struct {
log entity.IssueLog
opening string
}{
{opened, opened.BuildID},
{reported, opened.BuildID},
{closed, opened.BuildID},
{reopened, reopened.BuildID},
{resolved, reopened.BuildID},
{opened, opened.BuildID},
{closed, opened.BuildID},
{reopened, reopened.BuildID},
} {
for range 2 {
require.NoError(t, f.m.PersistLog(ctx, f.stores, step.log, step.opening))
}
}

snapshot := scope.Snapshot()
for name, want := range map[string]int64{"opened": 2, "closed": 2} {
key := "issue_log_materializer.issue_lifecycle." + name + "+queue=q"
require.Contains(t, snapshot.Counters(), key)
assert.Equal(t, want, snapshot.Counters()[key].Value())
}
key := "issue_log_materializer.issue_lifecycle.time_to_resolution+queue=q"
require.Contains(t, snapshot.Histograms(), key)
counts := snapshot.Histograms()[key].Durations()
assert.EqualValues(t, 1, counts[10*time.Minute])
assert.EqualValues(t, 1, counts[2*time.Hour])
var samples int64
for _, count := range counts {
samples += count
}
assert.EqualValues(t, 2, samples)
}

func TestLifecycleMetricsProjectionRetry(t *testing.T) {
for _, failedWrite := range []string{"summary/create", "open_issue/create", "request-summary/create"} {
t.Run(failedWrite, func(t *testing.T) {
f := newMaterializerFixture(t)
scope := tally.NewTestScope("", nil)
f.m = NewMaterializer(scope)
opened := testIssueLog("build/z", entity.IssueStatusOpen)
key := "issue_log_materializer.issue_lifecycle.opened+"

f.failures[failedWrite] = assert.AnError
require.ErrorIs(t, f.m.PersistLog(context.Background(), f.stores, opened, opened.BuildID), assert.AnError)
if failedWrite == "summary/create" {
assert.NotContains(t, scope.Snapshot().Counters(), key)
} else {
require.Contains(t, scope.Snapshot().Counters(), key)
assert.EqualValues(t, 1, scope.Snapshot().Counters()[key].Value())
}

delete(f.failures, failedWrite)
for range 2 {
require.NoError(t, f.m.PersistLog(context.Background(), f.stores, opened, opened.BuildID))
}
require.Contains(t, scope.Snapshot().Counters(), key)
assert.EqualValues(t, 1, scope.Snapshot().Counters()[key].Value())
})
}
}

func TestLifecycleMetricsConditionalWriteConflict(t *testing.T) {
for _, write := range []string{"create", "update"} {
t.Run(write, func(t *testing.T) {
f := newMaterializerFixture(t)
scope := tally.NewTestScope("", nil)
f.m = NewMaterializer(scope)
opened := testIssueLog("build/z", entity.IssueStatusOpen)
incoming := opened
counter := "opened"
if write == "update" {
require.NoError(t, f.m.PersistLog(context.Background(), f.stores, opened, opened.BuildID))
incoming = testIssueLog("build/a", entity.IssueStatusClosed)
counter = "closed"
}
winner := func() {
require.NoError(t, f.m.PersistLog(context.Background(), f.stores, incoming, opened.BuildID))
}
if write == "create" {
f.beforeCreate = winner
} else {
f.beforeUpdate = winner
}

require.NoError(t, f.m.PersistLog(context.Background(), f.stores, incoming, opened.BuildID))
key := "issue_log_materializer.issue_lifecycle." + counter + "+"
require.Contains(t, scope.Snapshot().Counters(), key)
assert.EqualValues(t, 1, scope.Snapshot().Counters()[key].Value())
if write == "update" {
key := "issue_log_materializer.issue_lifecycle.time_to_resolution+"
require.Contains(t, scope.Snapshot().Histograms(), key)
assert.EqualValues(t, 1, scope.Snapshot().Histograms()[key].Durations()[time.Minute])
}
})
}
}

func TestLifecycleResolutionDuration(t *testing.T) {
for _, test := range []struct {
name string
missingOpen bool
opening string
elapsed time.Duration
wantSample bool
wantBucket time.Duration
}{
{name: "missing opening", missingOpen: true},
{name: "different opening", opening: "build/a"},
{name: "clock skew", elapsed: -time.Millisecond, wantSample: true, wantBucket: time.Minute},
{name: "long-lived issue", elapsed: 45 * 24 * time.Hour, wantSample: true, wantBucket: time.Duration(math.MaxInt64)},
} {
t.Run(test.name, func(t *testing.T) {
f := newMaterializerFixture(t)
scope := tally.NewTestScope("", nil)
f.m = NewMaterializer(scope)
opened := testIssueLog("build/z", entity.IssueStatusOpen)
if !test.missingOpen {
require.NoError(t, f.m.PersistLog(context.Background(), f.stores, opened, opened.BuildID))
}
closed := testIssueLog("build/a", entity.IssueStatusClosed)
closed.TimestampMs = opened.TimestampMs + test.elapsed.Milliseconds()
opening := opened.BuildID
if test.opening != "" {
opening = test.opening
}

require.NoError(t, f.m.PersistLog(context.Background(), f.stores, closed, opening))
snapshot := scope.Snapshot()
counterKey := "issue_log_materializer.issue_lifecycle.closed+"
require.Contains(t, snapshot.Counters(), counterKey)
assert.EqualValues(t, 1, snapshot.Counters()[counterKey].Value())
key := "issue_log_materializer.issue_lifecycle.time_to_resolution+"
if !test.wantSample {
assert.NotContains(t, snapshot.Histograms(), key)
return
}
require.Contains(t, snapshot.Histograms(), key)
assert.EqualValues(t, 1, snapshot.Histograms()[key].Durations()[test.wantBucket])
})
}
}

func TestLifecycleMetricsIgnoreRepeatedClosure(t *testing.T) {
f := newMaterializerFixture(t)
scope := tally.NewTestScope("", nil)
f.m = NewMaterializer(scope)
opened := testIssueLog("build/z", entity.IssueStatusOpen)
require.NoError(t, f.m.PersistLog(context.Background(), f.stores, opened, opened.BuildID))
for _, build := range []string{"build/a", "build/m"} {
closed := testIssueLog(build, entity.IssueStatusClosed)
require.NoError(t, f.m.PersistLog(context.Background(), f.stores, closed, opened.BuildID))
}

require.Equal(t, "build/m", f.summary.BuildID)
snapshot := scope.Snapshot()
counter := "issue_log_materializer.issue_lifecycle.closed+"
require.Contains(t, snapshot.Counters(), counter)
assert.EqualValues(t, 1, snapshot.Counters()[counter].Value())
histogram := "issue_log_materializer.issue_lifecycle.time_to_resolution+"
require.Contains(t, snapshot.Histograms(), histogram)
assert.EqualValues(t, 1, snapshot.Histograms()[histogram].Durations()[time.Minute])
}
Loading