vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/gc/tablegc_test.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package gc
    18  
    19  import (
    20  	"testing"
    21  
    22  	"vitess.io/vitess/go/vt/schema"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestNextTableToPurge(t *testing.T) {
    28  	tt := []struct {
    29  		tables []string
    30  		next   string
    31  		ok     bool
    32  	}{
    33  		{
    34  			tables: []string{},
    35  			ok:     false,
    36  		},
    37  		{
    38  			tables: []string{
    39  				"_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    40  				"_vt_PURGE_2ace8bcef73211ea87e9f875a4d24e90_20200915120411",
    41  				"_vt_PURGE_3ace8bcef73211ea87e9f875a4d24e90_20200915120412",
    42  				"_vt_PURGE_4ace8bcef73211ea87e9f875a4d24e90_20200915120413",
    43  			},
    44  			next: "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    45  			ok:   true,
    46  		},
    47  		{
    48  			tables: []string{
    49  				"_vt_PURGE_2ace8bcef73211ea87e9f875a4d24e90_20200915120411",
    50  				"_vt_PURGE_3ace8bcef73211ea87e9f875a4d24e90_20200915120412",
    51  				"_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    52  				"_vt_PURGE_4ace8bcef73211ea87e9f875a4d24e90_20200915120413",
    53  			},
    54  			next: "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
    55  			ok:   true,
    56  		},
    57  	}
    58  	for _, ts := range tt {
    59  		collector := &TableGC{
    60  			purgingTables: make(map[string]bool),
    61  		}
    62  		for _, table := range ts.tables {
    63  			collector.purgingTables[table] = true
    64  		}
    65  		next, ok := collector.nextTableToPurge()
    66  		assert.Equal(t, ts.ok, ok)
    67  		if ok {
    68  			assert.Equal(t, ts.next, next)
    69  		}
    70  	}
    71  }
    72  
    73  func TestNextState(t *testing.T) {
    74  	tt := []struct {
    75  		lifecycle string
    76  		state     schema.TableGCState
    77  		next      schema.TableGCState
    78  	}{
    79  		{
    80  			lifecycle: "hold,purge,evac,drop",
    81  			state:     schema.HoldTableGCState,
    82  			next:      schema.PurgeTableGCState,
    83  		},
    84  		{
    85  			lifecycle: "hold,purge,evac,drop",
    86  			state:     schema.PurgeTableGCState,
    87  			next:      schema.EvacTableGCState,
    88  		},
    89  		{
    90  			lifecycle: "hold,purge,evac,drop",
    91  			state:     schema.EvacTableGCState,
    92  			next:      schema.DropTableGCState,
    93  		},
    94  		{
    95  			lifecycle: "hold,purge,evac",
    96  			state:     schema.EvacTableGCState,
    97  			next:      schema.DropTableGCState,
    98  		},
    99  		{
   100  			lifecycle: "hold,purge",
   101  			state:     schema.HoldTableGCState,
   102  			next:      schema.PurgeTableGCState,
   103  		},
   104  		{
   105  			lifecycle: "hold,purge",
   106  			state:     schema.PurgeTableGCState,
   107  			next:      schema.DropTableGCState,
   108  		},
   109  		{
   110  			lifecycle: "hold",
   111  			state:     schema.HoldTableGCState,
   112  			next:      schema.DropTableGCState,
   113  		},
   114  		{
   115  			lifecycle: "evac,drop",
   116  			state:     schema.HoldTableGCState,
   117  			next:      schema.EvacTableGCState,
   118  		},
   119  		{
   120  			lifecycle: "evac,drop",
   121  			state:     schema.EvacTableGCState,
   122  			next:      schema.DropTableGCState,
   123  		},
   124  		{
   125  			lifecycle: "drop",
   126  			state:     schema.HoldTableGCState,
   127  			next:      schema.DropTableGCState,
   128  		},
   129  		{
   130  			lifecycle: "drop",
   131  			state:     schema.EvacTableGCState,
   132  			next:      schema.DropTableGCState,
   133  		},
   134  		{
   135  			lifecycle: "",
   136  			state:     schema.HoldTableGCState,
   137  			next:      schema.DropTableGCState,
   138  		},
   139  	}
   140  	for _, ts := range tt {
   141  		collector := &TableGC{}
   142  		var err error
   143  		collector.lifecycleStates, err = schema.ParseGCLifecycle(ts.lifecycle)
   144  		assert.NoError(t, err)
   145  		next := collector.nextState(ts.state)
   146  		assert.NotNil(t, next)
   147  		assert.Equal(t, ts.next, *next)
   148  
   149  		postDrop := collector.nextState(schema.DropTableGCState)
   150  		assert.Nil(t, postDrop)
   151  	}
   152  }
   153  
   154  func TestShouldTransitionTable(t *testing.T) {
   155  	tt := []struct {
   156  		table            string
   157  		state            schema.TableGCState
   158  		uuid             string
   159  		shouldTransition bool
   160  		isError          bool
   161  	}{
   162  		{
   163  			table:            "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_20200915120410",
   164  			state:            schema.PurgeTableGCState,
   165  			uuid:             "6ace8bcef73211ea87e9f875a4d24e90",
   166  			shouldTransition: true,
   167  		},
   168  		{
   169  			table:            "_vt_PURGE_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
   170  			state:            schema.PurgeTableGCState,
   171  			uuid:             "6ace8bcef73211ea87e9f875a4d24e90",
   172  			shouldTransition: false,
   173  		},
   174  		{
   175  			table:            "_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
   176  			state:            schema.DropTableGCState,
   177  			uuid:             "6ace8bcef73211ea87e9f875a4d24e90",
   178  			shouldTransition: false,
   179  		},
   180  		{
   181  			table:            "_vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20090915120410",
   182  			state:            schema.DropTableGCState,
   183  			uuid:             "6ace8bcef73211ea87e9f875a4d24e90",
   184  			shouldTransition: true,
   185  		},
   186  		{
   187  			table:            "_vt_EVAC_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
   188  			state:            schema.EvacTableGCState,
   189  			uuid:             "6ace8bcef73211ea87e9f875a4d24e90",
   190  			shouldTransition: false,
   191  		},
   192  		{
   193  			table:            "_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
   194  			state:            schema.HoldTableGCState,
   195  			uuid:             "6ace8bcef73211ea87e9f875a4d24e90",
   196  			shouldTransition: true,
   197  		},
   198  		{
   199  			table:            "_vt_SOMETHING_6ace8bcef73211ea87e9f875a4d24e90_29990915120410",
   200  			state:            "",
   201  			uuid:             "",
   202  			shouldTransition: false,
   203  		},
   204  	}
   205  	lifecycleStates, err := schema.ParseGCLifecycle("purge,evac,drop")
   206  	assert.NoError(t, err)
   207  	collector := &TableGC{
   208  		lifecycleStates: lifecycleStates,
   209  	}
   210  	for _, ts := range tt {
   211  		shouldTransition, state, uuid, err := collector.shouldTransitionTable(ts.table)
   212  		if ts.isError {
   213  			assert.Error(t, err)
   214  		} else {
   215  			assert.NoError(t, err)
   216  			assert.Equal(t, ts.shouldTransition, shouldTransition)
   217  			assert.Equal(t, ts.state, state)
   218  			assert.Equal(t, ts.uuid, uuid)
   219  		}
   220  	}
   221  }