github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/localstore/compactor_test.go (about)

     1  // Copyright 2015 PingCAP, 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 localstore
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore/engine"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    23  )
    24  
    25  var _ = Suite(&testLocalstoreCompactorSuite{})
    26  
    27  type testLocalstoreCompactorSuite struct {
    28  }
    29  
    30  func count(db engine.DB) int {
    31  	var k kv.Key
    32  	totalCnt := 0
    33  	for {
    34  		var err error
    35  		k, _, err = db.Seek(k)
    36  		if err != nil {
    37  			break
    38  		}
    39  		k = k.Next()
    40  		totalCnt++
    41  	}
    42  	return totalCnt
    43  }
    44  
    45  func (s *testLocalstoreCompactorSuite) TestCompactor(c *C) {
    46  	defer testleak.AfterTest(c)()
    47  	store := createMemStore(time.Now().Nanosecond())
    48  	db := store.(*dbStore).db
    49  	store.(*dbStore).compactor.Stop()
    50  
    51  	policy := compactPolicy{
    52  		SafePoint:       500,
    53  		BatchDeleteCnt:  1,
    54  		TriggerInterval: 100 * time.Millisecond,
    55  	}
    56  	compactor := newLocalCompactor(policy, db)
    57  	store.(*dbStore).compactor = compactor
    58  
    59  	compactor.Start()
    60  
    61  	txn, _ := store.Begin()
    62  	txn.Set([]byte("a"), []byte("1"))
    63  	txn.Commit()
    64  	txn, _ = store.Begin()
    65  	txn.Set([]byte("a"), []byte("2"))
    66  	txn.Commit()
    67  	txn, _ = store.Begin()
    68  	txn.Set([]byte("a"), []byte("3"))
    69  	txn.Commit()
    70  	txn, _ = store.Begin()
    71  	txn.Set([]byte("a"), []byte("3"))
    72  	txn.Commit()
    73  	txn, _ = store.Begin()
    74  	txn.Set([]byte("a"), []byte("4"))
    75  	txn.Commit()
    76  	txn, _ = store.Begin()
    77  	txn.Set([]byte("a"), []byte("5"))
    78  	txn.Commit()
    79  	t := count(db)
    80  	c.Assert(t, Equals, 6)
    81  
    82  	// Simulating timeout
    83  	time.Sleep(1 * time.Second)
    84  	// Touch a, tigger GC
    85  	txn, _ = store.Begin()
    86  	txn.Set([]byte("a"), []byte("b"))
    87  	txn.Commit()
    88  	time.Sleep(1 * time.Second)
    89  	// Do background GC
    90  	t = count(db)
    91  	c.Assert(t, Equals, 2)
    92  
    93  	err := store.Close()
    94  	c.Assert(err, IsNil)
    95  }
    96  
    97  func (s *testLocalstoreCompactorSuite) TestGetAllVersions(c *C) {
    98  	defer testleak.AfterTest(c)()
    99  	store := createMemStore(time.Now().Nanosecond())
   100  	compactor := store.(*dbStore).compactor
   101  	txn, _ := store.Begin()
   102  	txn.Set([]byte("a"), []byte("1"))
   103  	txn.Commit()
   104  	txn, _ = store.Begin()
   105  	txn.Set([]byte("a"), []byte("2"))
   106  	txn.Commit()
   107  	txn, _ = store.Begin()
   108  	txn.Set([]byte("b"), []byte("1"))
   109  	txn.Commit()
   110  	txn, _ = store.Begin()
   111  	txn.Set([]byte("b"), []byte("2"))
   112  	txn.Commit()
   113  
   114  	keys, err := compactor.getAllVersions([]byte("a"))
   115  	c.Assert(err, IsNil)
   116  	c.Assert(keys, HasLen, 2)
   117  
   118  	err = store.Close()
   119  	c.Assert(err, IsNil)
   120  }
   121  
   122  // TestStartStop is to test `Panic: sync: WaitGroup is reused before previous Wait has returned`
   123  // in Stop function.
   124  func (s *testLocalstoreCompactorSuite) TestStartStop(c *C) {
   125  	defer testleak.AfterTest(c)()
   126  	store := createMemStore(time.Now().Nanosecond())
   127  	db := store.(*dbStore).db
   128  
   129  	for i := 0; i < 10000; i++ {
   130  		policy := compactPolicy{
   131  			SafePoint:       500,
   132  			BatchDeleteCnt:  1,
   133  			TriggerInterval: 100 * time.Millisecond,
   134  		}
   135  		compactor := newLocalCompactor(policy, db)
   136  		compactor.Start()
   137  		compactor.Stop()
   138  		c.Logf("Test compactor stop and start %d times", i)
   139  	}
   140  
   141  	err := store.Close()
   142  	c.Assert(err, IsNil)
   143  }