Skip to content

Commit ef26518

Browse files
committed
removed async logic + addressed feedback
1 parent eddf974 commit ef26518

File tree

10 files changed

+159
-783
lines changed

10 files changed

+159
-783
lines changed

acceptance/cache/clear/output.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
[DEBUG_TIMESTAMP] Debug: [Local Cache] found authorization header with length: 45
44
[DEBUG_TIMESTAMP] Debug: [Local Cache] local cache is enabled
55
[DEBUG_TIMESTAMP] Debug: [Local Cache] using cache key: [SHA256_HASH]
6-
[DEBUG_TIMESTAMP] Debug: [Local Cache] writing to cache
7-
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache miss, computed and stored result
6+
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache miss, computing
7+
[DEBUG_TIMESTAMP] Debug: [Local Cache] computed and stored result
88

99
=== Second call in a session is expected to be a cache hit
1010
[DEBUG_TIMESTAMP] Debug: [Local Cache] found authorization header with length: 45
1111
[DEBUG_TIMESTAMP] Debug: [Local Cache] local cache is enabled
1212
[DEBUG_TIMESTAMP] Debug: [Local Cache] using cache key: [SHA256_HASH]
13-
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache hit: disk-read
13+
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache hit
1414

1515
>>> [CLI] cache clear
1616
Cache cleared successfully from [TEST_TMP_DIR]/.cache
@@ -19,5 +19,5 @@ Cache cleared successfully from [TEST_TMP_DIR]/.cache
1919
[DEBUG_TIMESTAMP] Debug: [Local Cache] found authorization header with length: 45
2020
[DEBUG_TIMESTAMP] Debug: [Local Cache] local cache is enabled
2121
[DEBUG_TIMESTAMP] Debug: [Local Cache] using cache key: [SHA256_HASH]
22-
[DEBUG_TIMESTAMP] Debug: [Local Cache] writing to cache
23-
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache miss, computed and stored result
22+
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache miss, computing
23+
[DEBUG_TIMESTAMP] Debug: [Local Cache] computed and stored result

acceptance/cache/clear/script

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export DATABRICKS_CACHE_FOLDER=$(pwd)/.cache
1+
export DATABRICKS_CACHE_DIR=$(pwd)/.cache
22

33
$CLI cache clear &> /dev/null
44

@@ -13,4 +13,4 @@ trace $CLI cache clear
1313
title "First call after a clear is expected to be a cache miss:\n"
1414
trace $CLI bundle validate --debug 2>&1 | grep "Local Cache" | grep -v "cache path"
1515

16-
rm -rf "${DATABRICKS_CACHE_FOLDER}"
16+
rm -rf "${DATABRICKS_CACHE_DIR}"

acceptance/cache/simple/output.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
[DEBUG_TIMESTAMP] Debug: [Local Cache] found authorization header with length: 45
44
[DEBUG_TIMESTAMP] Debug: [Local Cache] local cache is enabled
55
[DEBUG_TIMESTAMP] Debug: [Local Cache] using cache key: [SHA256_HASH]
6-
[DEBUG_TIMESTAMP] Debug: [Local Cache] writing to cache
7-
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache miss, computed and stored result
6+
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache miss, computing
7+
[DEBUG_TIMESTAMP] Debug: [Local Cache] computed and stored result
88

99
=== Second call in a session is expected to be a cache hit
1010
[DEBUG_TIMESTAMP] Debug: [Local Cache] found authorization header with length: 45
1111
[DEBUG_TIMESTAMP] Debug: [Local Cache] local cache is enabled
1212
[DEBUG_TIMESTAMP] Debug: [Local Cache] using cache key: [SHA256_HASH]
13-
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache hit: disk-read
13+
[DEBUG_TIMESTAMP] Debug: [Local Cache] cache hit
1414

1515
=== Bundle deploy should send telemetry values
1616

acceptance/cache/simple/script

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export DATABRICKS_CACHE_FOLDER=$(pwd)/.cache
1+
export DATABRICKS_CACHE_DIR=$(pwd)/.cache
22

33
$CLI cache clear &> /dev/null
44

@@ -13,4 +13,4 @@ trace $CLI bundle deploy -p dogfood
1313

1414
trace print_telemetry_bool_values | grep "local.cache"
1515
rm out.requests.txt
16-
rm -rf "${DATABRICKS_CACHE_FOLDER}"
16+
rm -rf "${DATABRICKS_CACHE_DIR}"

libs/cache/cache.go

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,45 @@ import (
66
"encoding/hex"
77
"encoding/json"
88
"fmt"
9-
"sort"
109
)
1110

1211
// Cache provides an abstract interface for caching content to local disk.
1312
// Implementations should handle storing and retrieving cached components
1413
// using fingerprints for cache invalidation.
14+
// Cache operations fail open: if caching fails, the compute function is still called.
1515
type Cache[T any] interface {
1616
// GetOrCompute retrieves cached content for the given fingerprint, or computes it using the provided function.
1717
// If the content is found in cache, it is returned directly.
1818
// If not found, the compute function is called, its result is cached, and then returned.
1919
// The fingerprint can be any struct that will be serialized deterministically for cache key generation.
20-
// Returns an error if the cache operation or compute function fails.
20+
// Cache failures do not block computation - if caching fails, compute is called anyway.
21+
// Returns an error only if the compute function fails.
2122
GetOrCompute(ctx context.Context, fingerprint any, compute func(ctx context.Context) (T, error)) (T, error)
2223
}
2324

2425
// fingerprintToHash converts any struct to a deterministic string representation for use as a cache key.
26+
// For structs, json.Marshal uses struct field order, not JSON tag order. To ensure deterministic
27+
// hashing regardless of struct field order, we convert to a map which json.Marshal sorts by key.
2528
func fingerprintToHash(fingerprint any) (string, error) {
26-
// Serialize to JSON with sorted keys for deterministic output
29+
// Marshal to JSON
2730
data, err := json.Marshal(fingerprint)
2831
if err != nil {
2932
return "", fmt.Errorf("failed to marshal fingerprint: %w", err)
3033
}
3134

32-
// Parse back to ensure consistent key ordering
33-
var obj any
34-
if err := json.Unmarshal(data, &obj); err != nil {
35+
// Unmarshal to map to ensure key ordering
36+
var m map[string]any
37+
if err := json.Unmarshal(data, &m); err != nil {
3538
return "", fmt.Errorf("failed to unmarshal fingerprint: %w", err)
3639
}
3740

38-
// Sort keys deterministically
39-
normalized := normalizeForFingerprint(obj)
40-
41-
// Re-marshal with normalized structure
42-
normalizedData, err := json.Marshal(normalized)
41+
// Marshal map (map keys are sorted by json.Marshal)
42+
normalizedData, err := json.Marshal(m)
4343
if err != nil {
4444
return "", fmt.Errorf("failed to marshal normalized fingerprint: %w", err)
4545
}
4646

47-
// Hash the result for a consistent, reasonably-sized key
47+
// Hash for consistent, reasonably-sized key
4848
hash := sha256.Sum256(normalizedData)
4949
return hex.EncodeToString(hash[:]), nil
5050
}
51-
52-
// normalizeForFingerprint recursively sorts map keys to ensure deterministic serialization.
53-
func normalizeForFingerprint(obj any) any {
54-
switch v := obj.(type) {
55-
case map[string]any:
56-
// Sort keys
57-
keys := make([]string, 0, len(v))
58-
for k := range v {
59-
keys = append(keys, k)
60-
}
61-
sort.Strings(keys)
62-
63-
// Create ordered map
64-
result := make(map[string]any, len(v))
65-
for _, k := range keys {
66-
result[k] = normalizeForFingerprint(v[k])
67-
}
68-
return result
69-
case []any:
70-
// Normalize each element in the slice
71-
result := make([]any, len(v))
72-
for i, item := range v {
73-
result[i] = normalizeForFingerprint(item)
74-
}
75-
return result
76-
default:
77-
// Primitive types are returned as-is
78-
return v
79-
}
80-
}

libs/cache/cleanup.go

Lines changed: 0 additions & 200 deletions
This file was deleted.

0 commit comments

Comments
 (0)