Skip to content

Commit c950c4d

Browse files
committed
fixup: cache server source code
Signed-off-by: kaikaila <[email protected]>
1 parent 47bffe6 commit c950c4d

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

backend/src/cache/client_manager.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,14 @@ func initDBClient(params WhSvrDBParameters, initConnectionTimeout time.Duration)
114114
glog.Fatalf("Failed to update the execution template type. Error: %s", err)
115115
}
116116

117-
var tableNames []string
118-
gormDB.Raw(`show tables`).Pluck("Tables_in_caches", &tableNames)
119-
for _, tableName := range tableNames {
120-
log.Printf("%s", tableName)
117+
// List all tables using GORM Migrator API (works for both MySQL and PostgreSQL)
118+
tableNames, err := gormDB.Migrator().GetTables()
119+
if err != nil {
120+
glog.Warningf("Failed to get table list: %v", err)
121+
} else {
122+
for _, tableName := range tableNames {
123+
log.Printf("Table: %s", tableName)
124+
}
121125
}
122126

123127
return gormDB, dbDialect
@@ -214,6 +218,7 @@ func initDBDriver(params WhSvrDBParameters, initConnectionTimeout time.Duration)
214218
db.Close()
215219
glog.Fatalf("Failed to create database: %v", err)
216220
}
221+
log.Printf("Database created")
217222
db.Close()
218223

219224
// Return DSN with target DB

backend/src/cache/server/admission.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func doServeAdmitFunc(w http.ResponseWriter, r *http.Request, admit admitFunc, c
114114
return errorResponse(admissionReviewReq.Request.UID, err), nil
115115
}
116116

117+
// If no patch operations, return response without patch field
118+
if patchOps == nil {
119+
return allowedResponse(admissionReviewReq.Request.UID, nil), nil
120+
}
121+
117122
patchBytes, err := json.Marshal(patchOps)
118123
if err != nil {
119124
w.WriteHeader(http.StatusInternalServerError)

backend/src/cache/server/mutation.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ func MutatePodIfCached(req *v1beta1.AdmissionRequest, clientMgr ClientManagerInt
104104
annotations := pod.ObjectMeta.Annotations
105105
labels := pod.ObjectMeta.Labels
106106

107+
if annotations == nil {
108+
annotations = make(map[string]string)
109+
}
110+
if labels == nil {
111+
labels = make(map[string]string)
112+
}
113+
107114
template, exists := getArgoTemplate(&pod)
108115
if !exists {
109116
return patches, nil

backend/src/cache/server/mutation_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,19 @@ func TestMutatePodIfCachedWithTeamplateCleanup(t *testing.T) {
270270
require.Equal(t, patchOperation[1].Op, OperationTypeAdd)
271271
require.Equal(t, patchOperation[2].Op, OperationTypeAdd)
272272
}
273+
274+
func TestMutatePodIfCachedWithNilAnnotations(t *testing.T) {
275+
podWithoutAnnotations := *fakePod.DeepCopy()
276+
podWithoutAnnotations.Annotations = nil
277+
podWithoutAnnotations.Labels = map[string]string{
278+
KFPCacheEnabledLabelKey: KFPCacheEnabledLabelValue,
279+
}
280+
281+
patchOperations, err := MutatePodIfCached(
282+
GetFakeRequestFromPod(&podWithoutAnnotations),
283+
fakeClientManager,
284+
)
285+
286+
assert.NotNil(t, patchOperations)
287+
assert.Nil(t, err)
288+
}

backend/src/cache/storage/execution_cache_store.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,31 @@ func (s *ExecutionCacheStore) GetExecutionCache(executionCacheKey string, cacheS
4646
if cacheStaleness == 0 {
4747
return nil, fmt.Errorf("CacheStaleness=0, Cache is disabled.")
4848
}
49-
r, err := s.db.Table("execution_caches").Where("ExecutionCacheKey = ?", executionCacheKey).Rows()
50-
if err != nil {
51-
return nil, fmt.Errorf("Failed to get execution cache: %q, err: %v", executionCacheKey, err)
49+
// Use GORM's standard query method with struct field name to avoid case-sensitivity issues
50+
var executionCaches []model.ExecutionCache
51+
result := s.db.Where(&model.ExecutionCache{ExecutionCacheKey: executionCacheKey}).Find(&executionCaches)
52+
if result.Error != nil {
53+
return nil, fmt.Errorf("failed to get execution cache: %q, err: %v", executionCacheKey, result.Error)
5254
}
53-
defer r.Close()
54-
executionCaches, err := s.scanRows(r, cacheStaleness)
55-
if err != nil {
56-
return nil, fmt.Errorf("Failed to scan rows on execution cache: %q, err: %v", executionCacheKey, err)
55+
56+
// Filter by cache staleness
57+
var validCaches []*model.ExecutionCache
58+
for i := range executionCaches {
59+
cache := &executionCaches[i]
60+
log.Println("Get id: " + strconv.FormatInt(cache.ID, 10))
61+
log.Println("Get template: " + cache.ExecutionTemplate)
62+
// maxCacheStaleness comes from the database entry.
63+
// cacheStaleness is computed from the pods annotation and environment variables.
64+
if (cache.MaxCacheStaleness < 0 || s.time.Now().UTC().Unix()-cache.StartedAtInSec <= cache.MaxCacheStaleness) &&
65+
(cacheStaleness < 0 || s.time.Now().UTC().Unix()-cache.StartedAtInSec <= cacheStaleness) {
66+
validCaches = append(validCaches, cache)
67+
}
5768
}
58-
if len(executionCaches) == 0 {
69+
70+
if len(validCaches) == 0 {
5971
return nil, fmt.Errorf("Execution cache not found with cache key: %q", executionCacheKey)
6072
}
61-
latestCache, err := getLatestCacheEntry(executionCaches)
73+
latestCache, err := getLatestCacheEntry(validCaches)
6274
if err != nil {
6375
return nil, err
6476
}
@@ -140,17 +152,15 @@ func getLatestCacheEntry(executionCaches []*model.ExecutionCache) (*model.Execut
140152
func (s *ExecutionCacheStore) CreateExecutionCache(executionCache *model.ExecutionCache) (*model.ExecutionCache, error) {
141153
log.Printf("checking for existing row with cache key: %s before insertion", executionCache.ExecutionCacheKey)
142154

143-
r, err := s.db.Table("execution_caches").Where("ExecutionCacheKey = ?", executionCache.ExecutionCacheKey).Rows()
144-
if err != nil {
145-
log.Printf("Failed to get execution cache with key: %s, err: %v", executionCache.ExecutionCacheKey, err)
146-
return nil, err
155+
// Use GORM's standard query method with struct field name to avoid case-sensitivity issues
156+
var existingCaches []model.ExecutionCache
157+
result := s.db.Where(&model.ExecutionCache{ExecutionCacheKey: executionCache.ExecutionCacheKey}).Find(&existingCaches)
158+
if result.Error != nil {
159+
log.Printf("Failed to get execution cache with key: %s, err: %v", executionCache.ExecutionCacheKey, result.Error)
160+
return nil, result.Error
147161
}
148162

149-
rowCount := 0
150-
151-
for r.Next() {
152-
rowCount++
153-
}
163+
rowCount := len(existingCaches)
154164
log.Printf("number of rows returned for existing rows check: %d", rowCount)
155165

156166
if rowCount == 0 {

0 commit comments

Comments
 (0)