github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/placement_rule_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  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    19  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    20  	"github.com/whtcorpsinc/milevadb/dbs/memristed"
    21  )
    22  
    23  var _ = Suite(&testPlacementSuite{})
    24  
    25  type testPlacementSuite struct {
    26  }
    27  
    28  func (s *testPlacementSuite) compareMemruleOp(n, o *memristed.MemruleOp) bool {
    29  	ok1, _ := DeepEquals.Check([]interface{}{n.CausetAction, o.CausetAction}, nil)
    30  	ok2, _ := DeepEquals.Check([]interface{}{n.DeleteByIDPrefix, o.DeleteByIDPrefix}, nil)
    31  	ok3, _ := DeepEquals.Check([]interface{}{n.Memrule, o.Memrule}, nil)
    32  	return ok1 && ok2 && ok3
    33  }
    34  
    35  func (s *testPlacementSuite) TestPlacementBuild(c *C) {
    36  	tests := []struct {
    37  		input  []*ast.PlacementSpec
    38  		output []*memristed.MemruleOp
    39  		err    string
    40  	}{
    41  		{
    42  			input:  []*ast.PlacementSpec{},
    43  			output: []*memristed.MemruleOp{},
    44  		},
    45  
    46  		{
    47  			input: []*ast.PlacementSpec{{
    48  				Role:        ast.PlacementRoleVoter,
    49  				Tp:          ast.PlacementAdd,
    50  				Replicas:    3,
    51  				Constraints: `["+  zone=sh", "-zone = bj"]`,
    52  			}},
    53  			output: []*memristed.MemruleOp{
    54  				{
    55  					CausetAction: memristed.MemruleOpAdd,
    56  					Memrule: &memristed.Memrule{
    57  						GroupID:  memristed.MemruleDefaultGroupID,
    58  						Role:     memristed.Voter,
    59  						Override: true,
    60  						Count:    3,
    61  						LabelConstraints: []memristed.LabelConstraint{
    62  							{Key: "zone", Op: "in", Values: []string{"sh"}},
    63  							{Key: "zone", Op: "notIn", Values: []string{"bj"}},
    64  						},
    65  					},
    66  				}},
    67  		},
    68  
    69  		{
    70  			input: []*ast.PlacementSpec{
    71  				{
    72  					Role:        ast.PlacementRoleVoter,
    73  					Tp:          ast.PlacementAdd,
    74  					Replicas:    3,
    75  					Constraints: `["+  zone=sh", "-zone = bj"]`,
    76  				},
    77  				{
    78  					Role:        ast.PlacementRoleFollower,
    79  					Tp:          ast.PlacementAdd,
    80  					Replicas:    2,
    81  					Constraints: `["-  zone=sh", "+zone = bj"]`,
    82  				},
    83  			},
    84  			output: []*memristed.MemruleOp{
    85  				{
    86  					CausetAction: memristed.MemruleOpAdd,
    87  					Memrule: &memristed.Memrule{
    88  						GroupID:  memristed.MemruleDefaultGroupID,
    89  						Role:     memristed.Voter,
    90  						Override: true,
    91  						Count:    3,
    92  						LabelConstraints: []memristed.LabelConstraint{
    93  							{Key: "zone", Op: "in", Values: []string{"sh"}},
    94  							{Key: "zone", Op: "notIn", Values: []string{"bj"}},
    95  						},
    96  					},
    97  				},
    98  				{
    99  					CausetAction: memristed.MemruleOpAdd,
   100  					Memrule: &memristed.Memrule{
   101  						GroupID:  memristed.MemruleDefaultGroupID,
   102  						Role:     memristed.Follower,
   103  						Override: true,
   104  						Count:    2,
   105  						LabelConstraints: []memristed.LabelConstraint{
   106  							{Key: "zone", Op: "notIn", Values: []string{"sh"}},
   107  							{Key: "zone", Op: "in", Values: []string{"bj"}},
   108  						},
   109  					},
   110  				},
   111  			},
   112  		},
   113  
   114  		{
   115  			input: []*ast.PlacementSpec{
   116  				{
   117  					Role:        ast.PlacementRoleVoter,
   118  					Tp:          ast.PlacementAdd,
   119  					Replicas:    3,
   120  					Constraints: `["+  zone=sh", "-zone = bj"]`,
   121  				},
   122  				{
   123  					Role:        ast.PlacementRoleVoter,
   124  					Tp:          ast.PlacementAlter,
   125  					Replicas:    2,
   126  					Constraints: `["-  zone=sh", "+zone = bj"]`,
   127  				},
   128  			},
   129  			output: []*memristed.MemruleOp{
   130  				{
   131  					CausetAction:           memristed.MemruleOFIDelel,
   132  					DeleteByIDPrefix: true,
   133  					Memrule: &memristed.Memrule{
   134  						GroupID: memristed.MemruleDefaultGroupID,
   135  						Role:    memristed.Voter,
   136  					},
   137  				},
   138  				{
   139  					CausetAction: memristed.MemruleOpAdd,
   140  					Memrule: &memristed.Memrule{
   141  						GroupID:  memristed.MemruleDefaultGroupID,
   142  						Role:     memristed.Voter,
   143  						Override: true,
   144  						Count:    2,
   145  						LabelConstraints: []memristed.LabelConstraint{
   146  							{Key: "zone", Op: "notIn", Values: []string{"sh"}},
   147  							{Key: "zone", Op: "in", Values: []string{"bj"}},
   148  						},
   149  					},
   150  				},
   151  			},
   152  		},
   153  
   154  		{
   155  			input: []*ast.PlacementSpec{
   156  				{
   157  					Role:        ast.PlacementRoleVoter,
   158  					Tp:          ast.PlacementAdd,
   159  					Replicas:    3,
   160  					Constraints: `["+  zone=sh", "-zone = bj"]`,
   161  				},
   162  				{
   163  					Role:        ast.PlacementRoleVoter,
   164  					Tp:          ast.PlacementAlter,
   165  					Replicas:    3,
   166  					Constraints: `{"-  zone=sh":1, "+zone = bj":1}`,
   167  				},
   168  			},
   169  			output: []*memristed.MemruleOp{
   170  				{
   171  					CausetAction:           memristed.MemruleOFIDelel,
   172  					DeleteByIDPrefix: true,
   173  					Memrule: &memristed.Memrule{
   174  						GroupID: memristed.MemruleDefaultGroupID,
   175  						Role:    memristed.Voter,
   176  					},
   177  				},
   178  				{
   179  					CausetAction: memristed.MemruleOpAdd,
   180  					Memrule: &memristed.Memrule{
   181  						GroupID:          memristed.MemruleDefaultGroupID,
   182  						Role:             memristed.Voter,
   183  						Override:         true,
   184  						Count:            1,
   185  						LabelConstraints: []memristed.LabelConstraint{{Key: "zone", Op: "in", Values: []string{"bj"}}},
   186  					},
   187  				},
   188  				{
   189  					CausetAction: memristed.MemruleOpAdd,
   190  					Memrule: &memristed.Memrule{
   191  						GroupID:          memristed.MemruleDefaultGroupID,
   192  						Role:             memristed.Voter,
   193  						Override:         true,
   194  						Count:            1,
   195  						LabelConstraints: []memristed.LabelConstraint{{Key: "zone", Op: "notIn", Values: []string{"sh"}}},
   196  					},
   197  				},
   198  				{
   199  					CausetAction: memristed.MemruleOpAdd,
   200  					Memrule: &memristed.Memrule{
   201  						GroupID:  memristed.MemruleDefaultGroupID,
   202  						Role:     memristed.Voter,
   203  						Override: true,
   204  						Count:    1,
   205  					},
   206  				},
   207  			},
   208  		},
   209  
   210  		{
   211  			input: []*ast.PlacementSpec{
   212  				{
   213  					Role:        ast.PlacementRoleVoter,
   214  					Tp:          ast.PlacementAdd,
   215  					Replicas:    3,
   216  					Constraints: `["+  zone=sh", "-zone = bj"]`,
   217  				},
   218  				{
   219  					Role: ast.PlacementRoleVoter,
   220  					Tp:   ast.PlacementDrop,
   221  				},
   222  			},
   223  			output: []*memristed.MemruleOp{
   224  				{
   225  					CausetAction:           memristed.MemruleOFIDelel,
   226  					DeleteByIDPrefix: true,
   227  					Memrule: &memristed.Memrule{
   228  						GroupID: memristed.MemruleDefaultGroupID,
   229  						Role:    memristed.Voter,
   230  					},
   231  				},
   232  			},
   233  		},
   234  
   235  		{
   236  			input: []*ast.PlacementSpec{
   237  				{
   238  					Role: ast.PlacementRoleLearner,
   239  					Tp:   ast.PlacementDrop,
   240  				},
   241  				{
   242  					Role: ast.PlacementRoleVoter,
   243  					Tp:   ast.PlacementDrop,
   244  				},
   245  			},
   246  			output: []*memristed.MemruleOp{
   247  				{
   248  					CausetAction:           memristed.MemruleOFIDelel,
   249  					DeleteByIDPrefix: true,
   250  					Memrule: &memristed.Memrule{
   251  						GroupID: memristed.MemruleDefaultGroupID,
   252  						Role:    memristed.Voter,
   253  					},
   254  				},
   255  				{
   256  					CausetAction:           memristed.MemruleOFIDelel,
   257  					DeleteByIDPrefix: true,
   258  					Memrule: &memristed.Memrule{
   259  						GroupID: memristed.MemruleDefaultGroupID,
   260  						Role:    memristed.Learner,
   261  					},
   262  				},
   263  			},
   264  		},
   265  
   266  		{
   267  			input: []*ast.PlacementSpec{
   268  				{
   269  					Role:        ast.PlacementRoleLearner,
   270  					Tp:          ast.PlacementAdd,
   271  					Replicas:    3,
   272  					Constraints: `["+  zone=sh", "-zone = bj"]`,
   273  				},
   274  				{
   275  					Role: ast.PlacementRoleVoter,
   276  					Tp:   ast.PlacementDrop,
   277  				},
   278  			},
   279  			output: []*memristed.MemruleOp{
   280  				{
   281  					CausetAction:           memristed.MemruleOFIDelel,
   282  					DeleteByIDPrefix: true,
   283  					Memrule: &memristed.Memrule{
   284  						GroupID: memristed.MemruleDefaultGroupID,
   285  						Role:    memristed.Voter,
   286  					},
   287  				},
   288  				{
   289  					CausetAction: memristed.MemruleOpAdd,
   290  					Memrule: &memristed.Memrule{
   291  						GroupID:  memristed.MemruleDefaultGroupID,
   292  						Role:     memristed.Learner,
   293  						Override: true,
   294  						Count:    3,
   295  						LabelConstraints: []memristed.LabelConstraint{
   296  							{Key: "zone", Op: "in", Values: []string{"sh"}},
   297  							{Key: "zone", Op: "notIn", Values: []string{"bj"}},
   298  						},
   299  					},
   300  				},
   301  			},
   302  		},
   303  	}
   304  	for k, t := range tests {
   305  		out, err := buildPlacementSpecs(t.input)
   306  		if err == nil {
   307  			for i := range t.output {
   308  				found := false
   309  				for j := range out {
   310  					if s.compareMemruleOp(out[j], t.output[i]) {
   311  						found = true
   312  						break
   313  					}
   314  				}
   315  				if !found {
   316  					c.Logf("test %d, %d-th output", k, i)
   317  					c.Logf("\texcept %+v\n\tbut got", t.output[i])
   318  					for j := range out {
   319  						c.Logf("\t%+v", out[j])
   320  					}
   321  					c.Fail()
   322  				}
   323  			}
   324  		} else {
   325  			c.Assert(err.Error(), ErrorMatches, t.err)
   326  		}
   327  	}
   328  }
   329  
   330  func (s *testPlacementSuite) TestPlacementBuildDrop(c *C) {
   331  	tests := []struct {
   332  		input  []int64
   333  		output []*memristed.MemruleOp
   334  	}{
   335  		{
   336  			input: []int64{2},
   337  			output: []*memristed.MemruleOp{
   338  				{
   339  					CausetAction:           memristed.MemruleOFIDelel,
   340  					DeleteByIDPrefix: true,
   341  					Memrule: &memristed.Memrule{
   342  						GroupID: memristed.MemruleDefaultGroupID,
   343  						ID:      "0_t0_p2",
   344  					},
   345  				},
   346  			},
   347  		},
   348  		{
   349  			input: []int64{1, 2},
   350  			output: []*memristed.MemruleOp{
   351  				{
   352  					CausetAction:           memristed.MemruleOFIDelel,
   353  					DeleteByIDPrefix: true,
   354  					Memrule: &memristed.Memrule{
   355  						GroupID: memristed.MemruleDefaultGroupID,
   356  						ID:      "0_t0_p1",
   357  					},
   358  				},
   359  				{
   360  					CausetAction:           memristed.MemruleOFIDelel,
   361  					DeleteByIDPrefix: true,
   362  					Memrule: &memristed.Memrule{
   363  						GroupID: memristed.MemruleDefaultGroupID,
   364  						ID:      "0_t0_p2",
   365  					},
   366  				},
   367  			},
   368  		},
   369  	}
   370  	for _, t := range tests {
   371  		out := buildPlacementDropMemrules(0, 0, t.input)
   372  		c.Assert(len(out), Equals, len(t.output))
   373  		for i := range t.output {
   374  			c.Assert(s.compareMemruleOp(out[i], t.output[i]), IsTrue, Commentf("except: %+v, obtained: %+v", t.output[i], out[i]))
   375  		}
   376  	}
   377  }
   378  
   379  func (s *testPlacementSuite) TestPlacementBuildTruncate(c *C) {
   380  	rules := []*memristed.MemruleOp{
   381  		{Memrule: &memristed.Memrule{ID: "0_t0_p1"}},
   382  		{Memrule: &memristed.Memrule{ID: "0_t0_p94"}},
   383  		{Memrule: &memristed.Memrule{ID: "0_t0_p48"}},
   384  	}
   385  
   386  	tests := []struct {
   387  		input  []int64
   388  		output []*memristed.MemruleOp
   389  	}{
   390  		{
   391  			input: []int64{1},
   392  			output: []*memristed.MemruleOp{
   393  				{CausetAction: memristed.MemruleOFIDelel, Memrule: &memristed.Memrule{GroupID: memristed.MemruleDefaultGroupID, ID: "0_t0_p1"}},
   394  				{CausetAction: memristed.MemruleOpAdd, Memrule: &memristed.Memrule{ID: "0_t0_p2__0_0"}},
   395  			},
   396  		},
   397  		{
   398  			input: []int64{94, 48},
   399  			output: []*memristed.MemruleOp{
   400  				{CausetAction: memristed.MemruleOFIDelel, Memrule: &memristed.Memrule{GroupID: memristed.MemruleDefaultGroupID, ID: "0_t0_p94"}},
   401  				{CausetAction: memristed.MemruleOpAdd, Memrule: &memristed.Memrule{ID: "0_t0_p95__0_0"}},
   402  				{CausetAction: memristed.MemruleOFIDelel, Memrule: &memristed.Memrule{GroupID: memristed.MemruleDefaultGroupID, ID: "0_t0_p48"}},
   403  				{CausetAction: memristed.MemruleOpAdd, Memrule: &memristed.Memrule{ID: "0_t0_p49__0_1"}},
   404  			},
   405  		},
   406  	}
   407  	for _, t := range tests {
   408  		copyMemrules := make([]*memristed.MemruleOp, len(rules))
   409  		for _, rule := range rules {
   410  			copyMemrules = append(copyMemrules, rule.Clone())
   411  		}
   412  
   413  		newPartitions := make([]perceptron.PartitionDefinition, 0, len(t.input))
   414  		for _, j := range t.input {
   415  			newPartitions = append(newPartitions, perceptron.PartitionDefinition{ID: j + 1})
   416  		}
   417  
   418  		out := buildPlacementTruncateMemrules(rules, 0, 0, 0, t.input, newPartitions)
   419  
   420  		c.Assert(len(out), Equals, len(t.output))
   421  		for i := range t.output {
   422  			c.Assert(s.compareMemruleOp(out[i], t.output[i]), IsTrue, Commentf("except: %+v, obtained: %+v", t.output[i], out[i]))
   423  		}
   424  	}
   425  }