Skip to content

Commit

Permalink
objectio: make metadata cache capacity dynamic adjustable
Browse files Browse the repository at this point in the history
  • Loading branch information
reusee committed Nov 6, 2024
1 parent 9577256 commit e8c0760
Show file tree
Hide file tree
Showing 5 changed files with 694 additions and 195 deletions.
16 changes: 16 additions & 0 deletions pkg/cnservice/server_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/matrixorigin/matrixone/pkg/fileservice"
"github.com/matrixorigin/matrixone/pkg/lockservice"
"github.com/matrixorigin/matrixone/pkg/logutil"
"github.com/matrixorigin/matrixone/pkg/objectio"
pblock "github.com/matrixorigin/matrixone/pkg/pb/lock"
"github.com/matrixorigin/matrixone/pkg/pb/query"
"github.com/matrixorigin/matrixone/pkg/pb/status"
Expand Down Expand Up @@ -92,6 +93,7 @@ func (s *service) initQueryCommandHandler() {
s.queryService.AddHandleFunc(query.CmdMethod_GOMEMLIMIT, s.handleGoMemLimit, false)
s.queryService.AddHandleFunc(query.CmdMethod_FileServiceCache, s.handleFileServiceCacheRequest, false)
s.queryService.AddHandleFunc(query.CmdMethod_FileServiceCacheEvict, s.handleFileServiceCacheEvictRequest, false)
s.queryService.AddHandleFunc(query.CmdMethod_MetadataCache, s.handleMetadataCacheRequest, false)
}

func (s *service) handleKillConn(ctx context.Context, req *query.Request, resp *query.Response, _ *morpc.Buffer) error {
Expand Down Expand Up @@ -572,3 +574,17 @@ func (s *service) handleFileServiceCacheEvictRequest(

return nil
}

func (s *service) handleMetadataCacheRequest(
ctx context.Context, req *query.Request, resp *query.Response, _ *morpc.Buffer,
) error {

// set capacity hint
objectio.GlobalCacheCapacityHint.Store(req.MetadataCacheRequest.CacheSize)
// evict
target := objectio.EvictCache()
// response
resp.MetadataCacheResponse.CacheCapacity = target

return nil
}
15 changes: 15 additions & 0 deletions pkg/cnservice/server_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,18 @@ func Test_service_copy(t *testing.T) {
require.Falsef(t, unsafe.Pointer(&srcLock.Rows) == unsafe.Pointer(&got.Rows), "copyTxnInfo Rows should diff. src: %p, got: %p", srcLock.Rows, got.Rows)
require.Falsef(t, unsafe.Pointer(&srcLock.Options) == unsafe.Pointer(got.Options), "copyTxnInfo Options should diff. src: %p, got: %p", &srcLock.Options, got.Options)
}

func Test_service_handleMetadataCacheRequest(t *testing.T) {
ctx := context.Background()
s := &service{}
var resp query.Response
err := s.handleMetadataCacheRequest(ctx, &query.Request{
MetadataCacheRequest: query.MetadataCacheRequest{
CacheSize: 42,
},
}, &resp, nil)
require.Nil(t, err)
if resp.MetadataCacheResponse.CacheCapacity != 42 {
t.Fatal()
}
}
34 changes: 32 additions & 2 deletions pkg/objectio/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package objectio
import (
"context"
"sync"
"sync/atomic"

"github.com/matrixorigin/matrixone/pkg/logutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -96,16 +97,45 @@ func shardMetaCacheKey(key mataCacheKey) uint64 {
return xxhash.Sum64(key[:])
}

var GlobalCacheCapacityHint atomic.Int64

func cacheCapacityFunc(size int64) fscache.CapacityFunc {
return func() int64 {
if n := GlobalCacheCapacityHint.Load(); n > 0 {
return n
}
return size
}
}

func init() {
metaCache = fifocache.New[mataCacheKey, []byte](fscache.ConstCapacity(metaCacheSize()), shardMetaCacheKey, nil, nil, nil)
metaCache = fifocache.New[mataCacheKey, []byte](
cacheCapacityFunc(metaCacheSize()),
shardMetaCacheKey,
nil, nil, nil,
)
}

func InitMetaCache(size int64) {
onceInit.Do(func() {
metaCache = fifocache.New[mataCacheKey, []byte](fscache.ConstCapacity(size), shardMetaCacheKey, nil, nil, nil)
metaCache = fifocache.New[mataCacheKey, []byte](
cacheCapacityFunc(size),
shardMetaCacheKey,
nil, nil, nil,
)
})
}

func EvictCache() (target int64) {
ch := make(chan int64, 1)
metaCache.Evict(ch)
target = <-ch
logutil.Info("metadata cache forced evicted",
zap.Any("target", target),
)
return
}

func encodeCacheKey(name ObjectNameShort, cacheKeyType uint16) mataCacheKey {
var key mataCacheKey
copy(key[:], name[:])
Expand Down
Loading

0 comments on commit e8c0760

Please sign in to comment.