github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/caching_file_service_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileservice
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/fileservice/memorycache"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/api"
    24  	"github.com/matrixorigin/matrixone/pkg/perfcounter"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func testCachingFileService(
    29  	t *testing.T,
    30  	newFS func() CachingFileService,
    31  ) {
    32  
    33  	fs := newFS()
    34  	fs.SetAsyncUpdate(false)
    35  	ctx := context.Background()
    36  	var counterSet perfcounter.CounterSet
    37  	ctx = perfcounter.WithCounterSet(ctx, &counterSet)
    38  
    39  	m := api.Int64Map{
    40  		M: map[int64]int64{42: 42},
    41  	}
    42  	data, err := m.Marshal()
    43  	assert.Nil(t, err)
    44  
    45  	err = fs.Write(ctx, IOVector{
    46  		FilePath: "foo",
    47  		Entries: []IOEntry{
    48  			{
    49  				Size: int64(len(data)),
    50  				Data: data,
    51  			},
    52  		},
    53  		Policy: SkipAllCache,
    54  	})
    55  	assert.Nil(t, err)
    56  
    57  	makeVec := func() *IOVector {
    58  		return &IOVector{
    59  			FilePath: "foo",
    60  			Entries: []IOEntry{
    61  				{
    62  					Size: int64(len(data)),
    63  					ToCacheData: func(r io.Reader, data []byte, allocator CacheDataAllocator) (memorycache.CacheData, error) {
    64  						bs, err := io.ReadAll(r)
    65  						assert.Nil(t, err)
    66  						if len(data) > 0 {
    67  							assert.Equal(t, bs, data)
    68  						}
    69  						cacheData := allocator.Alloc(len(bs))
    70  						copy(cacheData.Bytes(), bs)
    71  						return cacheData, nil
    72  					},
    73  				},
    74  			},
    75  		}
    76  	}
    77  
    78  	// nocache
    79  	vec := makeVec()
    80  	vec.Policy = SkipAllCache
    81  	err = fs.Read(ctx, vec)
    82  	assert.Nil(t, err)
    83  
    84  	err = m.Unmarshal(vec.Entries[0].CachedData.Bytes())
    85  	assert.NoError(t, err)
    86  	assert.Equal(t, 1, len(m.M))
    87  	assert.Equal(t, int64(42), m.M[42])
    88  	assert.Equal(t, int64(0), counterSet.FileService.Cache.Read.Load())
    89  	assert.Equal(t, int64(0), counterSet.FileService.Cache.Hit.Load())
    90  
    91  	vec.Release()
    92  
    93  	// read, not hit
    94  	vec = makeVec()
    95  	err = fs.Read(ctx, vec)
    96  	assert.Nil(t, err)
    97  	err = m.Unmarshal(vec.Entries[0].CachedData.Bytes())
    98  	assert.NoError(t, err)
    99  	assert.Equal(t, 1, len(m.M))
   100  	assert.Equal(t, int64(42), m.M[42])
   101  	assert.Equal(t, int64(1), counterSet.FileService.Cache.Read.Load())
   102  	assert.Equal(t, int64(0), counterSet.FileService.Cache.Hit.Load())
   103  
   104  	vec.Release()
   105  
   106  	// read again, hit cache
   107  	vec = makeVec()
   108  	err = fs.Read(ctx, vec)
   109  	assert.Nil(t, err)
   110  	err = m.Unmarshal(vec.Entries[0].CachedData.Bytes())
   111  	assert.NoError(t, err)
   112  	assert.Equal(t, 1, len(m.M))
   113  	assert.Equal(t, int64(42), m.M[42])
   114  	assert.Equal(t, int64(2), counterSet.FileService.Cache.Read.Load())
   115  	assert.Equal(t, int64(1), counterSet.FileService.Cache.Hit.Load())
   116  
   117  	vec.Release()
   118  
   119  	// flush
   120  	fs.FlushCache()
   121  
   122  }