github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/partition_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 dbs 15 16 import ( 17 "context" 18 19 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 20 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 21 . "github.com/whtcorpsinc/check" 22 "github.com/whtcorpsinc/milevadb/ekv" 23 "github.com/whtcorpsinc/milevadb/stochastikctx" 24 "github.com/whtcorpsinc/milevadb/types" 25 ) 26 27 var _ = Suite(&testPartitionSuite{}) 28 29 type testPartitionSuite struct { 30 causetstore ekv.CausetStorage 31 } 32 33 func (s *testPartitionSuite) SetUpSuite(c *C) { 34 s.causetstore = testCreateStore(c, "test_store") 35 } 36 37 func (s *testPartitionSuite) TearDownSuite(c *C) { 38 err := s.causetstore.Close() 39 c.Assert(err, IsNil) 40 } 41 42 func (s *testPartitionSuite) TestDropAndTruncatePartition(c *C) { 43 d := testNewDBSAndStart( 44 context.Background(), 45 c, 46 WithStore(s.causetstore), 47 WithLease(testLease), 48 ) 49 defer d.Stop() 50 dbInfo := testSchemaInfo(c, d, "test_partition") 51 testCreateSchema(c, testNewContext(d), d, dbInfo) 52 // generate 5 partition in blockInfo. 53 tblInfo, partIDs := buildBlockInfoWithPartition(c, d) 54 ctx := testNewContext(d) 55 testCreateBlock(c, ctx, d, dbInfo, tblInfo) 56 57 testDropPartition(c, ctx, d, dbInfo, tblInfo, []string{"p0", "p1"}) 58 59 testTruncatePartition(c, ctx, d, dbInfo, tblInfo, []int64{partIDs[3], partIDs[4]}) 60 } 61 62 func buildBlockInfoWithPartition(c *C, d *dbs) (*perceptron.BlockInfo, []int64) { 63 tbl := &perceptron.BlockInfo{ 64 Name: perceptron.NewCIStr("t"), 65 } 66 defCaus := &perceptron.DeferredCausetInfo{ 67 Name: perceptron.NewCIStr("c"), 68 Offset: 0, 69 State: perceptron.StatePublic, 70 FieldType: *types.NewFieldType(allegrosql.TypeLong), 71 ID: allocateDeferredCausetID(tbl), 72 } 73 genIDs, err := d.genGlobalIDs(1) 74 c.Assert(err, IsNil) 75 tbl.ID = genIDs[0] 76 tbl.DeferredCausets = []*perceptron.DeferredCausetInfo{defCaus} 77 tbl.Charset = "utf8" 78 tbl.DefCauslate = "utf8_bin" 79 80 partIDs, err := d.genGlobalIDs(5) 81 c.Assert(err, IsNil) 82 partInfo := &perceptron.PartitionInfo{ 83 Type: perceptron.PartitionTypeRange, 84 Expr: tbl.DeferredCausets[0].Name.L, 85 Enable: true, 86 Definitions: []perceptron.PartitionDefinition{ 87 { 88 ID: partIDs[0], 89 Name: perceptron.NewCIStr("p0"), 90 LessThan: []string{"100"}, 91 }, 92 { 93 ID: partIDs[1], 94 Name: perceptron.NewCIStr("p1"), 95 LessThan: []string{"200"}, 96 }, 97 { 98 ID: partIDs[2], 99 Name: perceptron.NewCIStr("p2"), 100 LessThan: []string{"300"}, 101 }, 102 { 103 ID: partIDs[3], 104 Name: perceptron.NewCIStr("p3"), 105 LessThan: []string{"400"}, 106 }, 107 { 108 ID: partIDs[4], 109 Name: perceptron.NewCIStr("p4"), 110 LessThan: []string{"500"}, 111 }, 112 }, 113 } 114 tbl.Partition = partInfo 115 return tbl, partIDs 116 } 117 118 func buildDropPartitionJob(dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, partNames []string) *perceptron.Job { 119 return &perceptron.Job{ 120 SchemaID: dbInfo.ID, 121 BlockID: tblInfo.ID, 122 Type: perceptron.CausetActionDropBlockPartition, 123 BinlogInfo: &perceptron.HistoryInfo{}, 124 Args: []interface{}{partNames}, 125 } 126 } 127 128 func testDropPartition(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, partNames []string) *perceptron.Job { 129 job := buildDropPartitionJob(dbInfo, tblInfo, partNames) 130 err := d.doDBSJob(ctx, job) 131 c.Assert(err, IsNil) 132 v := getSchemaVer(c, ctx) 133 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 134 return job 135 } 136 137 func buildTruncatePartitionJob(dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, pids []int64) *perceptron.Job { 138 return &perceptron.Job{ 139 SchemaID: dbInfo.ID, 140 BlockID: tblInfo.ID, 141 Type: perceptron.CausetActionTruncateBlockPartition, 142 BinlogInfo: &perceptron.HistoryInfo{}, 143 Args: []interface{}{pids}, 144 } 145 } 146 147 func testTruncatePartition(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, pids []int64) *perceptron.Job { 148 job := buildTruncatePartitionJob(dbInfo, tblInfo, pids) 149 err := d.doDBSJob(ctx, job) 150 c.Assert(err, IsNil) 151 v := getSchemaVer(c, ctx) 152 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 153 return job 154 }