github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/einsteindb/interlock_cache_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package einsteindb
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/ekvproto/pkg/interlock"
    21  	"github.com/whtcorpsinc/milevadb/config"
    22  	"github.com/whtcorpsinc/milevadb/ekv"
    23  )
    24  
    25  type testCoprocessorCacheSuite struct {
    26  	OneByOneSuite
    27  }
    28  
    29  var _ = Suite(&testCoprocessorCacheSuite{})
    30  
    31  func (s *testCoprocessorSuite) TestBuildCacheKey(c *C) {
    32  	req := interlock.Request{
    33  		Tp:      0xAB,
    34  		StartTs: 0xAABBCC,
    35  		Data:    []uint8{0x18, 0x0, 0x20, 0x0, 0x40, 0x0, 0x5a, 0x0},
    36  		Ranges: []*interlock.KeyRange{
    37  			{
    38  				Start: ekv.Key{0x01},
    39  				End:   ekv.Key{0x01, 0x02},
    40  			},
    41  			{
    42  				Start: ekv.Key{0x01, 0x01, 0x02},
    43  				End:   ekv.Key{0x01, 0x01, 0x03},
    44  			},
    45  		},
    46  	}
    47  
    48  	key, err := coprCacheBuildKey(&req)
    49  	c.Assert(err, IsNil)
    50  	expectKey := ""
    51  	expectKey += "\xab"                             // 1 byte Tp
    52  	expectKey += "\x08\x00\x00\x00"                 // 4 bytes Data len
    53  	expectKey += "\x18\x00\x20\x00\x40\x00\x5a\x00" // Data
    54  	expectKey += "\x01\x00"                         // 2 bytes StartKey len
    55  	expectKey += "\x01"                             // StartKey
    56  	expectKey += "\x02\x00"                         // 2 bytes EndKey len
    57  	expectKey += "\x01\x02"                         // EndKey
    58  	expectKey += "\x03\x00"                         // 2 bytes StartKey len
    59  	expectKey += "\x01\x01\x02"                     // StartKey
    60  	expectKey += "\x03\x00"                         // 2 bytes EndKey len
    61  	expectKey += "\x01\x01\x03"                     // EndKey
    62  	c.Assert(key, DeepEquals, []byte(expectKey))
    63  
    64  	req = interlock.Request{
    65  		Tp:      0xABCC, // Tp too big
    66  		StartTs: 0xAABBCC,
    67  		Data:    []uint8{0x18},
    68  		Ranges:  []*interlock.KeyRange{},
    69  	}
    70  
    71  	_, err = coprCacheBuildKey(&req)
    72  	c.Assert(err, NotNil)
    73  }
    74  
    75  func (s *testCoprocessorSuite) TestDisable(c *C) {
    76  	cache, err := newCoprCache(&config.CoprocessorCache{Enable: false})
    77  	c.Assert(err, IsNil)
    78  	c.Assert(cache, IsNil)
    79  
    80  	v := cache.Set([]byte("foo"), &coprCacheValue{})
    81  	c.Assert(v, Equals, false)
    82  
    83  	v2 := cache.Get([]byte("foo"))
    84  	c.Assert(v2, IsNil)
    85  
    86  	v = cache.CheckAdmission(1024, time.Second*5)
    87  	c.Assert(v, Equals, false)
    88  
    89  	cache, err = newCoprCache(&config.CoprocessorCache{Enable: true, CapacityMB: 0, AdmissionMaxResultMB: 1})
    90  	c.Assert(err, NotNil)
    91  	c.Assert(cache, IsNil)
    92  
    93  	cache, err = newCoprCache(&config.CoprocessorCache{Enable: true, CapacityMB: 0.001})
    94  	c.Assert(err, NotNil)
    95  	c.Assert(cache, IsNil)
    96  
    97  	cache, err = newCoprCache(&config.CoprocessorCache{Enable: true, CapacityMB: 0.001, AdmissionMaxResultMB: 1})
    98  	c.Assert(err, IsNil)
    99  	c.Assert(cache, NotNil)
   100  }
   101  
   102  func (s *testCoprocessorSuite) TestAdmission(c *C) {
   103  	cache, err := newCoprCache(&config.CoprocessorCache{Enable: true, AdmissionMinProcessMs: 5, AdmissionMaxResultMB: 1, CapacityMB: 1})
   104  	c.Assert(err, IsNil)
   105  	c.Assert(cache, NotNil)
   106  
   107  	v := cache.CheckAdmission(0, 0)
   108  	c.Assert(v, Equals, false)
   109  
   110  	v = cache.CheckAdmission(0, 4*time.Millisecond)
   111  	c.Assert(v, Equals, false)
   112  
   113  	v = cache.CheckAdmission(0, 5*time.Millisecond)
   114  	c.Assert(v, Equals, false)
   115  
   116  	v = cache.CheckAdmission(1, 0)
   117  	c.Assert(v, Equals, false)
   118  
   119  	v = cache.CheckAdmission(1, 4*time.Millisecond)
   120  	c.Assert(v, Equals, false)
   121  
   122  	v = cache.CheckAdmission(1, 5*time.Millisecond)
   123  	c.Assert(v, Equals, true)
   124  
   125  	v = cache.CheckAdmission(1024, 5*time.Millisecond)
   126  	c.Assert(v, Equals, true)
   127  
   128  	v = cache.CheckAdmission(1024*1024, 5*time.Millisecond)
   129  	c.Assert(v, Equals, true)
   130  
   131  	v = cache.CheckAdmission(1024*1024+1, 5*time.Millisecond)
   132  	c.Assert(v, Equals, false)
   133  
   134  	v = cache.CheckAdmission(1024*1024+1, 4*time.Millisecond)
   135  	c.Assert(v, Equals, false)
   136  }
   137  
   138  func (s *testCoprocessorSuite) TestCacheValueLen(c *C) {
   139  	v := coprCacheValue{
   140  		TimeStamp:         0x123,
   141  		RegionID:          0x1,
   142  		RegionDataVersion: 0x3,
   143  	}
   144  	// 72 = (8 byte pointer + 8 byte for length + 8 byte for cap) * 2 + 8 byte * 3
   145  	c.Assert(v.Len(), Equals, 72)
   146  
   147  	v = coprCacheValue{
   148  		Key:               []byte("foobar"),
   149  		Data:              []byte("12345678"),
   150  		TimeStamp:         0x123,
   151  		RegionID:          0x1,
   152  		RegionDataVersion: 0x3,
   153  	}
   154  	c.Assert(v.Len(), Equals, 72+6+8)
   155  }
   156  
   157  func (s *testCoprocessorSuite) TestGetSet(c *C) {
   158  	cache, err := newCoprCache(&config.CoprocessorCache{Enable: true, AdmissionMinProcessMs: 5, AdmissionMaxResultMB: 1, CapacityMB: 1})
   159  	c.Assert(err, IsNil)
   160  	c.Assert(cache, NotNil)
   161  
   162  	v := cache.Get([]byte("foo"))
   163  	c.Assert(v, IsNil)
   164  
   165  	v2 := cache.Set([]byte("foo"), &coprCacheValue{
   166  		Data:              []byte("bar"),
   167  		TimeStamp:         0x123,
   168  		RegionID:          0x1,
   169  		RegionDataVersion: 0x3,
   170  	})
   171  	c.Assert(v2, Equals, true)
   172  
   173  	// See https://github.com/dgraph-io/ristretto/blob/83508260cb49a2c3261c2774c991870fd18b5a1b/cache_test.go#L13
   174  	// Changed from 10ms to 50ms to resist from unsblock CI environment.
   175  	time.Sleep(time.Millisecond * 50)
   176  
   177  	v = cache.Get([]byte("foo"))
   178  	c.Assert(v, NotNil)
   179  	c.Assert(v.Data, DeepEquals, []byte("bar"))
   180  }