Skip to content
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
5 changes: 5 additions & 0 deletions acceptance/bundle/telemetry/deploy-git/databricks.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
bundle:
name: test-bundle

# The data directory only gives the repository a known size.
sync:
exclude:
- data
22 changes: 21 additions & 1 deletion acceptance/bundle/telemetry/deploy-git/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,25 @@ Resources: 0 created, 0 changed, 0 deleted, 0 unchanged
"commit": "[COMMIT_SHA]"
},
"from_workspace_folder": null,
"in_git_repo": true
"in_git_repo": true,
"git_repo_size": {
"tracked_file_count": "FILES_UNDER_1K",
"pack_size": "PACK_UNDER_10_MB"
}
}

=== 1,202 tracked files and 12 MB of loose objects

>>> git_repo_size
{
"tracked_file_count": "FILES_1K_TO_5K",
"pack_size": "PACK_10_MB_TO_100_MB"
}

=== The same repository after git gc packs the objects

>>> git_repo_size
{
"tracked_file_count": "FILES_1K_TO_5K",
"pack_size": "PACK_10_MB_TO_100_MB"
}
25 changes: 24 additions & 1 deletion acceptance/bundle/telemetry/deploy-git/script
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ git remote add origin https://github.com/databricks/cli

trace $CLI bundle deploy

trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_deploy_event | {git, from_workspace_folder, in_git_repo}'
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_deploy_event | {git, from_workspace_folder, in_git_repo, git_repo_size}'
Comment thread
andersrexdb marked this conversation as resolved.
Resolved

rm "$OUT_REQUESTS"

# Prints git_repo_size of the last deploy.
git_repo_size() {
jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_deploy_event.git_repo_size' "$OUT_REQUESTS"
rm "$OUT_REQUESTS"
}

title "1,202 tracked files and 12 MB of loose objects"
mkdir data
for i in $(seq 1200); do echo "$i" > "data/f$i"; done
# Random bytes do not compress, so the object store grows by 12 MB.
head -c 12000000 /dev/urandom > data/blob.bin
git add data
git commit -qm 'Add data'

$CLI bundle deploy > LOG.deploy-loose 2>&1
trace git_repo_size

title "The same repository after git gc packs the objects"
git gc -q > LOG.gc 2>&1

$CLI bundle deploy > LOG.deploy-packed 2>&1
trace git_repo_size
1 change: 1 addition & 0 deletions acceptance/bundle/telemetry/deploy-git/test.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# git-repo-init creates a repo in the test directory so the deploy has git metadata.
Ignore = [
'.git',
'data',
]

# The commit is different on every run.
Expand Down
87 changes: 87 additions & 0 deletions bundle/phases/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/git"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/telemetry"
"github.com/databricks/cli/libs/telemetry/protos"
Expand Down Expand Up @@ -241,6 +242,91 @@ func bundleGitInfo(b *bundle.Bundle) *protos.BundleGitInfo {
}
}

// Exclusive upper bounds for the tracked file count buckets. Counts at or above
// the last bound fall in the final bucket. Kept in sync with
// GitTrackedFileCountBucket in the universe proto.
var trackedFileCountBuckets = []struct {
below int64
bucket protos.GitTrackedFileCountBucket
}{
{1_000, protos.GitTrackedFileCountUnder1K},
{5_000, protos.GitTrackedFileCount1KTo5K},
{10_000, protos.GitTrackedFileCount5KTo10K},
{20_000, protos.GitTrackedFileCount10KTo20K},
{50_000, protos.GitTrackedFileCount20KTo50K},
{100_000, protos.GitTrackedFileCount50KTo100K},
{500_000, protos.GitTrackedFileCount100KTo500K},
{1_000_000, protos.GitTrackedFileCount500KTo1M},
}

func trackedFileCountBucket(count int64) protos.GitTrackedFileCountBucket {
for _, b := range trackedFileCountBuckets {
if count < b.below {
return b.bucket
}
}
return protos.GitTrackedFileCountOver1M
}

// Exclusive upper bounds in bytes for the pack size buckets. Sizes at or above
// the last bound fall in the final bucket. Kept in sync with GitPackSizeBucket
// in the universe proto.
var packSizeBuckets = []struct {
below int64
bucket protos.GitPackSizeBucket
}{
{10 << 20, protos.GitPackSizeUnder10MB},
{100 << 20, protos.GitPackSize10MBTo100MB},
{500 << 20, protos.GitPackSize100MBTo500MB},
{1 << 30, protos.GitPackSize500MBTo1GB},
{2 << 30, protos.GitPackSize1GBTo2GB},
{5 << 30, protos.GitPackSize2GBTo5GB},
{20 << 30, protos.GitPackSize5GBTo20GB},
}

func packSizeBucket(size int64) protos.GitPackSizeBucket {
for _, b := range packSizeBuckets {
if size < b.below {
return b.bucket
}
}
return protos.GitPackSizeOver20GB
}

// Time budget for reading the git repository size. The read takes about a
// millisecond on a local disk. The budget keeps a slow filesystem from holding
// up the deploy.
const gitRepoSizeTimeout = 200 * time.Millisecond

// bundleGitRepoSize reports the coarse size of the local git repository the
// bundle is in. Only buckets are sent, exact values never leave the machine.
// Nil for Databricks Git folders, whose size the workspace already knows, and
// when neither value could be read.
func bundleGitRepoSize(ctx context.Context, b *bundle.Bundle) *protos.BundleGitRepoSize {
if b.InGitRepo == nil || !*b.InGitRepo || fromWorkspaceFolder(ctx, b) {
return nil
}
ctx, cancel := context.WithTimeout(ctx, gitRepoSizeTimeout)
defer cancel()
// On error the values that could be read are still reported.
size, err := git.FetchRepositorySize(ctx, b.WorktreeRoot)
if err != nil {
log.Debugf(ctx, "failed to read git repository size: %s", err)
}
if size.TrackedFileCount == nil && size.PackSizeBytes == nil {
return nil
}

out := &protos.BundleGitRepoSize{}
if size.TrackedFileCount != nil {
out.TrackedFileCount = trackedFileCountBucket(*size.TrackedFileCount)
}
if size.PackSizeBytes != nil {
out.PackSize = packSizeBucket(*size.PackSizeBytes)
}
return out
}

// fromWorkspaceFolder reports whether the bundle was deployed from a workspace
// folder: the CLI running on a Databricks cluster with the bundle source under
// /Workspace/, as opposed to a local checkout. Same signal as the source-linked
Expand Down Expand Up @@ -474,6 +560,7 @@ func LogDeployTelemetry(ctx context.Context, b *bundle.Bundle, errMsg string) {
Git: bundleGitInfo(b),
FromWorkspaceFolder: fromWorkspaceFolder(ctx, b),
InGitRepo: b.InGitRepo,
GitRepoSize: bundleGitRepoSize(ctx, b),
BundleBitmap: bundleBitmap(ctx, b),

Experimental: &protos.BundleDeployExperimental{
Expand Down
96 changes: 96 additions & 0 deletions bundle/phases/telemetry_git_repo_size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package phases

import (
"context"
"encoding/binary"
"os"
"path/filepath"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/telemetry/protos"
"github.com/databricks/cli/libs/vfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// gitRepoSizeContext isolates the global git config and sets whether the CLI
// runs on a Databricks cluster.
func gitRepoSizeContext(t *testing.T, onRuntime bool) context.Context {
ctx := env.WithUserHomeDir(t.Context(), t.TempDir())
ctx = env.Set(ctx, "XDG_CONFIG_HOME", "")
return dbr.MockRuntime(ctx, dbr.Environment{IsDbr: onRuntime})
}

// gitRepoSizeBundle returns a bundle in a repository with 1,500 tracked files
// and a 20 MiB pack. Without withRepo the .git directory is empty.
func gitRepoSizeBundle(t *testing.T, withRepo bool) *bundle.Bundle {
root := t.TempDir()
gitDir := filepath.Join(root, ".git")
require.NoError(t, os.MkdirAll(gitDir, 0o755))
if withRepo {
require.NoError(t, os.MkdirAll(filepath.Join(gitDir, "objects", "pack"), 0o755))
index := make([]byte, 12)
copy(index, "DIRC")
binary.BigEndian.PutUint32(index[4:8], 2)
binary.BigEndian.PutUint32(index[8:12], 1_500)
require.NoError(t, os.WriteFile(filepath.Join(gitDir, "index"), index, 0o644))
require.NoError(t, os.WriteFile(filepath.Join(gitDir, "objects", "pack", "pack-1.pack"), make([]byte, 20<<20), 0o644))
}
inGitRepo := true
return &bundle.Bundle{
WorktreeRoot: vfs.MustNew(root),
SyncRootPath: root,
InGitRepo: &inGitRepo,
}
}

func TestBundleGitRepoSize(t *testing.T) {
got := bundleGitRepoSize(gitRepoSizeContext(t, false), gitRepoSizeBundle(t, true))
assert.Equal(t, &protos.BundleGitRepoSize{
TrackedFileCount: protos.GitTrackedFileCount1KTo5K,
PackSize: protos.GitPackSize10MBTo100MB,
}, got)
}

func TestBundleGitRepoSizeSkipped(t *testing.T) {
notInRepo := false

tests := []struct {
name string
onRuntime bool
edit func(b *bundle.Bundle)
withRepo bool
}{
{
name: "git detection did not run",
edit: func(b *bundle.Bundle) { b.InGitRepo = nil },
withRepo: true,
},
{
name: "not in a git repository",
edit: func(b *bundle.Bundle) { b.InGitRepo = &notInRepo },
withRepo: true,
},
{
// The workspace already knows the size of a Databricks Git folder.
name: "deployed from a workspace folder",
onRuntime: true,
edit: func(b *bundle.Bundle) { b.SyncRootPath = "/Workspace/Users/someone@example.com/repo" },
withRepo: true,
},
{
name: "nothing readable",
edit: func(b *bundle.Bundle) {},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := gitRepoSizeBundle(t, tt.withRepo)
tt.edit(b)
assert.Nil(t, bundleGitRepoSize(gitRepoSizeContext(t, tt.onRuntime), b))
})
}
}
51 changes: 51 additions & 0 deletions bundle/phases/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,54 @@ func TestPydabsMetrics(t *testing.T) {
b := &bundle.Bundle{Config: config.Root{Experimental: &config.Experimental{Python: pythonConfig}}, Metrics: metrics}
assert.Equal(t, expected, pydabsMetrics(b))
}

func TestTrackedFileCountBucket(t *testing.T) {
tests := []struct {
count int64
want protos.GitTrackedFileCountBucket
}{
{0, protos.GitTrackedFileCountUnder1K},
{999, protos.GitTrackedFileCountUnder1K},
{1_000, protos.GitTrackedFileCount1KTo5K},
{4_999, protos.GitTrackedFileCount1KTo5K},
{5_000, protos.GitTrackedFileCount5KTo10K},
{10_000, protos.GitTrackedFileCount10KTo20K},
{19_999, protos.GitTrackedFileCount10KTo20K},
{20_000, protos.GitTrackedFileCount20KTo50K},
{50_000, protos.GitTrackedFileCount50KTo100K},
{99_999, protos.GitTrackedFileCount50KTo100K},
{100_000, protos.GitTrackedFileCount100KTo500K},
{499_999, protos.GitTrackedFileCount100KTo500K},
{500_000, protos.GitTrackedFileCount500KTo1M},
{1_000_000, protos.GitTrackedFileCountOver1M},
{5_000_000, protos.GitTrackedFileCountOver1M},
}
for _, tt := range tests {
assert.Equal(t, tt.want, trackedFileCountBucket(tt.count), "count %d", tt.count)
}
}

func TestPackSizeBucket(t *testing.T) {
const mb = int64(1 << 20)
tests := []struct {
size int64
want protos.GitPackSizeBucket
}{
{0, protos.GitPackSizeUnder10MB},
{10*mb - 1, protos.GitPackSizeUnder10MB},
{10 * mb, protos.GitPackSize10MBTo100MB},
{100 * mb, protos.GitPackSize100MBTo500MB},
{500*mb - 1, protos.GitPackSize100MBTo500MB},
{500 * mb, protos.GitPackSize500MBTo1GB},
{1024 * mb, protos.GitPackSize1GBTo2GB},
{2048*mb - 1, protos.GitPackSize1GBTo2GB},
{2048 * mb, protos.GitPackSize2GBTo5GB},
{5*1024*mb - 1, protos.GitPackSize2GBTo5GB},
{5 * 1024 * mb, protos.GitPackSize5GBTo20GB},
{20 * 1024 * mb, protos.GitPackSizeOver20GB},
{100 * 1024 * mb, protos.GitPackSizeOver20GB},
}
for _, tt := range tests {
assert.Equal(t, tt.want, packSizeBucket(tt.size), "size %d", tt.size)
}
}
Loading
Loading