(ctx context.Context, instanceID string, query Query, priority int)
| 68 | } |
| 69 | |
| 70 | func (r *Runtime) Query(ctx context.Context, instanceID string, query Query, priority int) error { |
| 71 | qk := query.Key() |
| 72 | // If key is empty, skip caching |
| 73 | if qk == "" { |
| 74 | return query.Resolve(ctx, r, instanceID, priority) |
| 75 | } |
| 76 | |
| 77 | // Get dependency cache keys and optionally the underlying OLAP connector |
| 78 | ctrl, err := r.Controller(ctx, instanceID) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | deps := query.Deps() |
| 83 | depKeys := make([]string, 0, len(deps)) |
| 84 | for _, dep := range deps { |
| 85 | // Get the dependency resource |
| 86 | res, err := ctrl.Get(ctx, dep, false) |
| 87 | if err != nil { |
| 88 | // Deps are approximate, not exact (see docstring for Deps()), so they may not all exist |
| 89 | continue |
| 90 | } |
| 91 | |
| 92 | // Add to cache key. |
| 93 | // Using StateUpdatedOn instead of StateVersion because the state version is reset when the resource is deleted and recreated. |
| 94 | key := fmt.Sprintf("%s:%s:%d:%d", res.Meta.Name.Kind, res.Meta.Name.Name, res.Meta.StateUpdatedOn.Seconds, res.Meta.StateUpdatedOn.Nanos/int32(time.Millisecond)) |
| 95 | if mv := res.GetMetricsView(); mv != nil { |
| 96 | cacheKey, ok, err := r.metricsViewCacheKey(ctx, instanceID, res.Meta.Name.Name, priority) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | if !ok { |
| 101 | // skip caching |
| 102 | return query.Resolve(ctx, r, instanceID, priority) |
| 103 | } |
| 104 | key = key + ":" + string(cacheKey) |
| 105 | } |
| 106 | depKeys = append(depKeys, key) |
| 107 | } |
| 108 | |
| 109 | // If there were no known dependencies, skip caching |
| 110 | if len(depKeys) == 0 { |
| 111 | return query.Resolve(ctx, r, instanceID, priority) |
| 112 | } |
| 113 | |
| 114 | // Build cache key |
| 115 | depKey := strings.Join(depKeys, ";") |
| 116 | key := queryCacheKey{ |
| 117 | instanceID: instanceID, |
| 118 | queryKey: qk, |
| 119 | dependencyKey: depKey, |
| 120 | }.String() |
| 121 | |
| 122 | // Try to get from cache |
| 123 | if val, ok := r.queryCache.cache.Get(key); ok { |
| 124 | observability.AddRequestAttributes(ctx, attribute.Bool("query.cache_hit", true)) |
| 125 | return query.UnmarshalResult(val) |
| 126 | } |
| 127 | observability.AddRequestAttributes(ctx, attribute.Bool("query.cache_hit", false)) |
nothing calls this directly
no test coverage detected