-
Notifications
You must be signed in to change notification settings - Fork 335
Store reported piped stats to redis #2190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
45d012a
b763f23
ad12b0c
5d193d5
e475add
064be33
badf024
ecfbd0d
478ee0d
d7e89e0
f30a710
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package rediscache | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| redigo "github.com/gomodule/redigo/redis" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/cache" | ||
| "github.com/pipe-cd/pipe/pkg/redis" | ||
| ) | ||
|
|
||
| type RedisHashCache struct { | ||
| RedisCache | ||
| key string | ||
| } | ||
|
|
||
| func NewHashCache(redis redis.Redis, ttl time.Duration, key string) *RedisHashCache { | ||
|
khanhtc1202 marked this conversation as resolved.
Outdated
|
||
| return &RedisHashCache{ | ||
| RedisCache: RedisCache{ | ||
| redis: redis, | ||
| }, | ||
| key: key, | ||
| } | ||
| } | ||
|
|
||
| func NewTTLHashCache(redis redis.Redis, ttl time.Duration, key string) *RedisHashCache { | ||
| return &RedisHashCache{ | ||
| RedisCache: RedisCache{ | ||
| redis: redis, | ||
| ttl: uint(ttl.Seconds()), | ||
| }, | ||
| key: key, | ||
| } | ||
| } | ||
|
|
||
| func (r *RedisHashCache) PutHash(k interface{}, v interface{}) error { | ||
| conn := r.redis.Get() | ||
| defer conn.Close() | ||
| _, err := conn.Do("HSET", r.key, k, v) | ||
| if r.ttl != 0 { | ||
| _, err = conn.Do("EXPIRE", r.key, r.ttl) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (r *RedisHashCache) GetAll() ([]interface{}, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who uses
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, this PR is focusing on storeing |
||
| conn := r.redis.Get() | ||
| defer conn.Close() | ||
| reply, err := redigo.StringMap(conn.Do("HGETALL", r.key)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can not use StringMap in this case. One more point is that instead of returning
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we adopt
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got it. Agree about that. 👍
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adopted returning |
||
| if err != nil { | ||
| if err == redigo.ErrNil { | ||
| return nil, cache.ErrNotFound | ||
| } | ||
| return nil, err | ||
| } | ||
| if len(reply) == 0 { | ||
| return nil, cache.ErrNotFound | ||
| } | ||
|
|
||
| out := make([]interface{}, 0, len(reply)) | ||
| for _, v := range reply { | ||
| out = append(out, v) | ||
| } | ||
| return out, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why do we need this? I think we can reuse
Putfunction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in case we want to use
RedisHashCache.Put(...)interface, I think we should keep the existingPutimplementation for RedisHashCache. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain more about this? That is for putting a set of (field, value) for a key?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the current
RedisHashCachehas embeddedRedisCacheinstance, in the caller side, we can useRedisHashCache.Get(key)which call toRedisHashCache.RedisCache.Get(key)with the current implementation. In that case, It will return an error not found (even if wePutthe (k,v) pair before) if we override thePutinterface with ourPutHashimplementation. That why I think we should keep the existingPutinterface and addPutHashwhich be used for the new purpose of storing (k,v) as values of the defined hashkey of RedisHashCache.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not embed
RedisCache. We can either add a newcache: RedisCachefield or useredis.Redisdirectly.I think we have no reason to put
PutHashfunction to all cache interfaces.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if I understand correctly, I feel like the current RedisCache is enough. We can just use piped-key as a key and use a stats byte sequence as a value. Tell me why using HSET if any.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is why
GetAllandRedisHashCachewere introduced. We can have all stats of all pipeds by a single command in a fast way to expose for prometheus.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙆♀️ Get you guys' point 🙏
Addressed on 478ee0d 👀
At this implementation, I change all implementation for Cache interface of RedisHashCache as it will treat the Hash object stored in redis as its backend and replace all
XXXcommands withHXXXcommands.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, makes sense. it's for retrieving all stats in bulk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With only the single
SETinterface, we need a way to know all possible piped (ID) currently registered to the controlplane and query for the data one by one which much slower, I guess 👀