Skip to content

Conversation

@khanhtc1202
Copy link
Member

@khanhtc1202 khanhtc1202 commented Jul 6, 2021

What this PR does / why we need it:

Implement piped ReportStat handler in piped_api

Which issue(s) this PR fixes:

Fixes #

Does this PR introduce a user-facing change?:

NONE

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.18%. This pull request decreases coverage by -0.08%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewRedisHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Put -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%

@pipecd-bot pipecd-bot added size/L and removed size/M labels Jul 7, 2021
Copy link
Collaborator

@pipecd-bot pipecd-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GO_LINTER

Some issues were detected while linting go source files in your changes.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.18%. This pull request decreases coverage by -0.09%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Put -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%

Copy link
Collaborator

@pipecd-bot pipecd-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GO_LINTER

Some issues were detected while linting go source files in your changes.

Copy link
Collaborator

@pipecd-bot pipecd-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GO_LINTER

Some issues were detected while linting go source files in your changes.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.16%. This pull request decreases coverage by -0.10%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/cache.go multiGetter.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%

@khanhtc1202 khanhtc1202 changed the title WIP Handle reported piped stats in controlplane Handle reported piped stats in controlplane Jul 7, 2021
Copy link
Collaborator

@pipecd-bot pipecd-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GO_LINTER

Some issues were detected while linting go source files in your changes.

}

// TODO: Implement GetCurrentStats so that ops can use it to expose pipeds' stat to prometheus.
func (c *store) GetCurrentStats() ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

receiver name c should be consistent with previous receiver name s for store

https://golang.org/wiki/CodeReviewComments#receiver-names

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.15%. This pull request decreases coverage by -0.12%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/cache.go multiGetter.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%
pkg/app/api/grpcapi/piped_api.go PipedAPI.ReportStat 0.00% 0.00% +0.00%

@pipecd-bot
Copy link
Collaborator

TODO

The following ISSUES will be created once got merged. If you want me to skip creating the issue, you can use /todo skip command.

Details

1. Implement GetCurrentStats so that ops can use it to expose pipeds' stat to prometheus.

https://github.com/pipe-cd/pipe/blob/badf024988619474aabc093f3b715a770b2d5ec1/pkg/app/api/pipedstatstore/store.go#L48-L51

This was created by todo plugin since "TODO:" was found in badf024 when #2190 was merged. cc: @khanhtc1202.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.15%. This pull request decreases coverage by -0.12%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/cache.go multiGetter.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%
pkg/app/api/grpcapi/piped_api.go PipedAPI.ReportStat 0.00% 0.00% +0.00%

@khanhtc1202 khanhtc1202 changed the title Handle reported piped stats in controlplane Store reported piped stats to redis Jul 7, 2021
// Putter wraps a method to write to cache.
type Putter interface {
Put(key interface{}, value interface{}) error
PutHash(key interface{}, value interface{}) error
Copy link
Member

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 Put function.

Copy link
Member Author

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 existing Put implementation for RedisHashCache. 🤔

Copy link
Member

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

Can you explain more about this? That is for putting a set of (field, value) for a key?

Copy link
Member Author

@khanhtc1202 khanhtc1202 Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is for putting a set of (field, value) for a key?

Since the current RedisHashCache has embedded RedisCache instance, in the caller side, we can use RedisHashCache.Get(key) which call to RedisHashCache.RedisCache.Get(key) with the current implementation. In that case, It will return an error not found (even if we Put the (k,v) pair before) if we override the Put interface with our PutHash implementation. That why I think we should keep the existing Put interface and add PutHash which be used for the new purpose of storing (k,v) as values of the defined hashkey of RedisHashCache.

Copy link
Member

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 new cache: RedisCache field or use redis.Redis directly.
I think we have no reason to put PutHash function to all cache interfaces.

Copy link
Member

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.

Copy link
Member

@nghialv nghialv Jul 7, 2021

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 GetAll and RedisHashCache were introduced. We can have all stats of all pipeds by a single command in a fast way to expose for prometheus.

Copy link
Member Author

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 XXX commands with HXXX commands.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tell me why using HSET if any.

With only the single SET interface, 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 👀

// See the License for the specific language governing permissions and
// limitations under the License.

package pipedstatstore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing the cache directly to the api instance instead of creating this store package?
Here are some reasons:

  • This store is containing only cache
  • Ops also needs this, but this is putting inside api package

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right 👍 lets me address this 🙏

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed on ecfbd0d 🙏

cmds := commandstore.NewStore(ds, cache, t.Logger)
is := insightstore.NewStore(fs)
cmdOutputStore := commandoutputstore.NewStore(fs, t.Logger)
hcache := rediscache.NewTTLHashCache(rd, cfg.Cache.TTLDuration(), defaultPipedStatHashKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: hcache -> statCache

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.15%. This pull request decreases coverage by -0.12%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/cache.go multiGetter.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.PutHash -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.PutHash -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%
pkg/app/api/grpcapi/piped_api.go PipedAPI.ReportStat 0.00% 0.00% +0.00%

return err
}

func (r *RedisHashCache) GetAll() ([]interface{}, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who uses GetAll() currently? Any plan to use it later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this PR is focusing on storeing

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.12%. This pull request decreases coverage by -0.15%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Get -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Put -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Delete -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%
pkg/app/api/grpcapi/piped_api.go PipedAPI.ReportStat 0.00% 0.00% +0.00%

_, pipedID, _, err := rpcauth.ExtractPipedToken(ctx)
if err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a simple guard here to ensure that the sent data is not exceeding the maximum size we can accept?
Just to protect the stat cache.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 👍 I wonder which limit we should set it to 🤔
Suppose we have 100 pipeds commit its stats, then it will be 51.2MB for each? 🤔

A String value can be at max 512 Megabytes in length.

ref: https://redis.io/topics/data-types

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 1MB is enough.
But I just remembered that gRPC is also having its validation on the message size.
Its default value is 4MB. So it is good as well.
So I think we don't have to add any additional code, just leave to use the default value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha 👌
Another thing I have concerned about is the number of "field" in this hash. As mentioned in the docs

A hash with a few fields (where few means up to one hundred or so) is stored in a way that takes very little space, so you can store millions of objects in a small Redis instance.

We may have a limit of up to hundreds of piped which could be registered to controlplane 👀 Will open an issue for that later since up to this time, we don't have any limitation around how may piped can be controlled by controlplane.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. We may split the Piped list into smaller groups like grouping by projectID.
Nice to have an issue about the field number.

func (r *RedisHashCache) GetAll() ([]interface{}, error) {
conn := r.redis.Get()
defer conn.Close()
reply, err := redigo.StringMap(conn.Do("HGETALL", r.key))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can not use StringMap in this case.
Because HGETALL is returning an array that containing both field and value.
So we have to use Array function to parse the reply. https://pkg.go.dev/github.com/gomodule/redigo/redis#Values

One more point is that instead of returning []interface{} maybe we should return map[interface{}]interface{}.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we adopt map[interface{}]interface{} as return value, then the current redigo.StringMap is good enough since its convert reply to map[string]string, how do you think about that 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it. Agree about that. 👍

Copy link
Member Author

@khanhtc1202 khanhtc1202 Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adopted returning map[interface{}]interface{} idea on d7e89e0 🙆‍♀️
It looks much better now, thx 👍

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.12%. This pull request decreases coverage by -0.15%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Get -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Put -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Delete -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%
pkg/app/api/grpcapi/piped_api.go PipedAPI.ReportStat 0.00% 0.00% +0.00%

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 32.10%. This pull request decreases coverage by -0.16%.

File Function Base Head Diff
pkg/cache/cache.go multiGetter.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/cache.go Cache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/lru_cache.go LRUCache.GetAll -- 0.00% +0.00%
pkg/cache/memorycache/ttl_cache.go TTLCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/cache.go RedisCache.GetAll -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go NewTTLHashCache -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Get -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Put -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.Delete -- 0.00% +0.00%
pkg/cache/rediscache/hashcache.go RedisHashCache.GetAll -- 0.00% +0.00%
pkg/cli/cmd.go Telemetry.PrometheusMetricsHandlerFor -- 0.00% +0.00%
pkg/app/api/grpcapi/piped_api.go PipedAPI.ReportStat 0.00% 0.00% +0.00%

@nghialv
Copy link
Member

nghialv commented Jul 7, 2021

Nice.
/lgtm

@pipecd-bot pipecd-bot added the lgtm label Jul 7, 2021
@nakabonne
Copy link
Member

Way to get 🥇
/approve

@pipecd-bot
Copy link
Collaborator

APPROVE

This pull request is APPROVED by nakabonne.

Approvers can cancel the approval by writing /approve cancel in a comment. Any additional commits also will change this pull request to be not-approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants