github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/checkpoint/runner_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package checkpoint
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/objectio"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestCkpCheck(t *testing.T) {
    30  	defer testutils.AfterTest(t)()
    31  	r := NewRunner(context.Background(), nil, nil, nil, nil)
    32  
    33  	for i := 0; i < 100; i += 10 {
    34  		r.storage.entries.Set(&CheckpointEntry{
    35  			start:      types.BuildTS(int64(i), 0),
    36  			end:        types.BuildTS(int64(i+9), 0),
    37  			state:      ST_Finished,
    38  			cnLocation: objectio.Location(fmt.Sprintf("loc-%d", i)),
    39  			version:    1,
    40  		})
    41  	}
    42  
    43  	r.storage.entries.Set(&CheckpointEntry{
    44  		start:      types.BuildTS(int64(100), 0),
    45  		end:        types.BuildTS(int64(109), 0),
    46  		state:      ST_Running,
    47  		cnLocation: objectio.Location("loc-100"),
    48  		version:    1,
    49  	})
    50  
    51  	ctx := context.Background()
    52  
    53  	loc, e, err := r.CollectCheckpointsInRange(ctx, types.BuildTS(4, 0), types.BuildTS(5, 0))
    54  	assert.NoError(t, err)
    55  	assert.True(t, e.Equal(types.BuildTSForTest(9, 0)))
    56  	assert.Equal(t, "loc-0;1;[0-0_9-0]", loc)
    57  
    58  	loc, e, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(12, 0), types.BuildTS(25, 0))
    59  	assert.NoError(t, err)
    60  	assert.True(t, e.Equal(types.BuildTSForTest(29, 0)))
    61  	assert.Equal(t, "loc-10;1;loc-20;1;[10-0_29-0]", loc)
    62  }
    63  
    64  func TestGetCheckpoints1(t *testing.T) {
    65  	defer testutils.AfterTest(t)()
    66  	r := NewRunner(context.Background(), nil, nil, nil, nil)
    67  
    68  	// ckp0[0,10]
    69  	// ckp1[10,20]
    70  	// ckp2[20,30]
    71  	// ckp3[30,40]
    72  	// ckp4[40,50(unfinished)]
    73  	timestamps := make([]types.TS, 0)
    74  	for i := 0; i < 6; i++ {
    75  		ts := types.BuildTS(int64(i*10), 0)
    76  		timestamps = append(timestamps, ts)
    77  	}
    78  	for i := 0; i < 5; i++ {
    79  		entry := &CheckpointEntry{
    80  			start:      timestamps[i].Next(),
    81  			end:        timestamps[i+1],
    82  			state:      ST_Finished,
    83  			cnLocation: objectio.Location(fmt.Sprintf("ckp%d", i)),
    84  			version:    1,
    85  		}
    86  		if i == 4 {
    87  			entry.state = ST_Pending
    88  		}
    89  		r.storage.entries.Set(entry)
    90  	}
    91  
    92  	ctx := context.Background()
    93  	// [0,10]
    94  	location, checkpointed, err := r.CollectCheckpointsInRange(ctx, types.BuildTS(0, 1), types.BuildTS(10, 0))
    95  	assert.NoError(t, err)
    96  	t.Log(location)
    97  	t.Log(checkpointed.ToString())
    98  	assert.Equal(t, "ckp0;1;[0-1_10-0]", location)
    99  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(10, 0)))
   100  
   101  	// [45,50]
   102  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(45, 0), types.BuildTS(50, 0))
   103  	assert.NoError(t, err)
   104  	t.Log(location)
   105  	t.Log(checkpointed.ToString())
   106  	assert.Equal(t, "", location)
   107  	assert.True(t, checkpointed.IsEmpty())
   108  
   109  	// [30,45]
   110  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(30, 1), types.BuildTS(45, 0))
   111  	assert.NoError(t, err)
   112  	t.Log(location)
   113  	t.Log(checkpointed.ToString())
   114  	assert.Equal(t, "ckp3;1;[30-1_40-0]", location)
   115  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(40, 0)))
   116  
   117  	// [25,45]
   118  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(25, 1), types.BuildTS(45, 0))
   119  	assert.NoError(t, err)
   120  	t.Log(location)
   121  	t.Log(checkpointed.ToString())
   122  	assert.Equal(t, "ckp2;1;ckp3;1;[20-1_40-0]", location)
   123  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(40, 0)))
   124  
   125  	// [22,25]
   126  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(22, 1), types.BuildTS(25, 0))
   127  	assert.NoError(t, err)
   128  	t.Log(location)
   129  	t.Log(checkpointed.ToString())
   130  	assert.Equal(t, "ckp2;1;[20-1_30-0]", location)
   131  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(30, 0)))
   132  
   133  	// [22,35]
   134  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(22, 1), types.BuildTS(35, 0))
   135  	assert.NoError(t, err)
   136  	t.Log(location)
   137  	t.Log(checkpointed.ToString())
   138  	assert.Equal(t, "ckp2;1;ckp3;1;[20-1_40-0]", location)
   139  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(40, 0)))
   140  }
   141  func TestGetCheckpoints2(t *testing.T) {
   142  	defer testutils.AfterTest(t)()
   143  	r := NewRunner(context.Background(), nil, nil, nil, nil)
   144  
   145  	// ckp0[0,10]
   146  	// ckp1[10,20]
   147  	// ckp2[20,30]
   148  	// global3[0,30]
   149  	// ckp3[30,40]
   150  	// ckp4[40,50(unfinished)]
   151  	timestamps := make([]types.TS, 0)
   152  	for i := 0; i < 6; i++ {
   153  		ts := types.BuildTS(int64(i*10), 0)
   154  		timestamps = append(timestamps, ts)
   155  	}
   156  	for i := 0; i < 5; i++ {
   157  		addGlobal := false
   158  		if i == 3 {
   159  			addGlobal = true
   160  		}
   161  		if addGlobal {
   162  			entry := &CheckpointEntry{
   163  				start:      types.TS{},
   164  				end:        timestamps[i].Next(),
   165  				state:      ST_Finished,
   166  				cnLocation: objectio.Location(fmt.Sprintf("global%d", i)),
   167  				version:    100,
   168  			}
   169  			r.storage.globals.Set(entry)
   170  		}
   171  		start := timestamps[i].Next()
   172  		if addGlobal {
   173  			start = start.Next()
   174  		}
   175  		entry := &CheckpointEntry{
   176  			start:      start,
   177  			end:        timestamps[i+1],
   178  			state:      ST_Finished,
   179  			cnLocation: objectio.Location(fmt.Sprintf("ckp%d", i)),
   180  			version:    uint32(i),
   181  		}
   182  		if i == 4 {
   183  			entry.state = ST_Pending
   184  		}
   185  		r.storage.entries.Set(entry)
   186  	}
   187  
   188  	ctx := context.Background()
   189  	// [0,10]
   190  	location, checkpointed, err := r.CollectCheckpointsInRange(ctx, types.BuildTS(0, 1), types.BuildTS(10, 0))
   191  	assert.NoError(t, err)
   192  	t.Log(location)
   193  	t.Log(checkpointed.ToString())
   194  	assert.Equal(t, "global3;100;[30-1_30-1]", location)
   195  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(30, 1)))
   196  
   197  	// [45,50]
   198  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(45, 0), types.BuildTS(50, 0))
   199  	assert.NoError(t, err)
   200  	t.Log(location)
   201  	t.Log(checkpointed.ToString())
   202  	assert.Equal(t, "", location)
   203  	assert.True(t, checkpointed.IsEmpty())
   204  
   205  	// [30,45]
   206  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(30, 2), types.BuildTS(45, 0))
   207  	assert.NoError(t, err)
   208  	t.Log(location)
   209  	t.Log(checkpointed.ToString())
   210  	assert.Equal(t, "ckp3;3;[30-2_40-0]", location)
   211  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(40, 0)))
   212  
   213  	// [25,45]
   214  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(25, 1), types.BuildTS(45, 0))
   215  	assert.NoError(t, err)
   216  	t.Log(location)
   217  	t.Log(checkpointed.ToString())
   218  	assert.Equal(t, "global3;100;ckp3;3;[30-1_40-0]", location)
   219  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(40, 0)))
   220  
   221  	// [22,25]
   222  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(22, 1), types.BuildTS(25, 0))
   223  	assert.NoError(t, err)
   224  	t.Log(location)
   225  	t.Log(checkpointed.ToString())
   226  	assert.Equal(t, "global3;100;[30-1_30-1]", location)
   227  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(30, 1)))
   228  
   229  	// [22,35]
   230  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(22, 1), types.BuildTS(35, 0))
   231  	assert.NoError(t, err)
   232  	t.Log(location)
   233  	t.Log(checkpointed.ToString())
   234  	assert.Equal(t, "global3;100;ckp3;3;[30-1_40-0]", location)
   235  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(40, 0)))
   236  
   237  	// [22,29]
   238  	location, checkpointed, err = r.CollectCheckpointsInRange(ctx, types.BuildTS(22, 1), types.BuildTS(29, 0))
   239  	assert.NoError(t, err)
   240  	t.Log(location)
   241  	t.Log(checkpointed.ToString())
   242  	assert.Equal(t, "global3;100;[30-1_30-1]", location)
   243  	assert.True(t, checkpointed.Equal(types.BuildTSForTest(30, 1)))
   244  }
   245  func TestICKPSeekLT(t *testing.T) {
   246  	defer testutils.AfterTest(t)()
   247  	r := NewRunner(context.Background(), nil, nil, nil, nil)
   248  
   249  	// ckp0[0,10]
   250  	// ckp1[10,20]
   251  	// ckp2[20,30]
   252  	// ckp3[30,40]
   253  	// ckp4[40,50(unfinished)]
   254  	timestamps := make([]types.TS, 0)
   255  	for i := 0; i < 6; i++ {
   256  		ts := types.BuildTS(int64(i*10), 0)
   257  		timestamps = append(timestamps, ts)
   258  	}
   259  	for i := 0; i < 5; i++ {
   260  		entry := &CheckpointEntry{
   261  			start:      timestamps[i].Next(),
   262  			end:        timestamps[i+1],
   263  			state:      ST_Finished,
   264  			cnLocation: objectio.Location(fmt.Sprintf("ckp%d", i)),
   265  			version:    uint32(i),
   266  		}
   267  		if i == 4 {
   268  			entry.state = ST_Pending
   269  		}
   270  		r.storage.entries.Set(entry)
   271  	}
   272  
   273  	// 0, 1
   274  	ckps := r.ICKPSeekLT(types.BuildTS(0, 0), 1)
   275  	for _, e := range ckps {
   276  		t.Log(e.String())
   277  	}
   278  	assert.Equal(t, 1, len(ckps))
   279  	assert.Equal(t, "ckp0", ckps[0].cnLocation.String())
   280  
   281  	// 0, 0
   282  	ckps = r.ICKPSeekLT(types.BuildTS(0, 0), 0)
   283  	for _, e := range ckps {
   284  		t.Log(e.String())
   285  	}
   286  	assert.Equal(t, 0, len(ckps))
   287  
   288  	// 0, 2
   289  	ckps = r.ICKPSeekLT(types.BuildTS(0, 0), 4)
   290  	for _, e := range ckps {
   291  		t.Log(e.String())
   292  	}
   293  	assert.Equal(t, 4, len(ckps))
   294  	assert.Equal(t, "ckp0", ckps[0].cnLocation.String())
   295  	assert.Equal(t, "ckp1", ckps[1].cnLocation.String())
   296  
   297  	// 0, 4
   298  	ckps = r.ICKPSeekLT(types.BuildTS(0, 0), 4)
   299  	for _, e := range ckps {
   300  		t.Log(e.String())
   301  	}
   302  	assert.Equal(t, 4, len(ckps))
   303  	assert.Equal(t, "ckp0", ckps[0].cnLocation.String())
   304  	assert.Equal(t, "ckp1", ckps[1].cnLocation.String())
   305  	assert.Equal(t, "ckp2", ckps[2].cnLocation.String())
   306  	assert.Equal(t, "ckp3", ckps[3].cnLocation.String())
   307  
   308  	// 0,10
   309  	ckps = r.ICKPSeekLT(types.BuildTS(0, 0), 10)
   310  	for _, e := range ckps {
   311  		t.Log(e.String())
   312  	}
   313  	assert.Equal(t, 4, len(ckps))
   314  	assert.Equal(t, "ckp0", ckps[0].cnLocation.String())
   315  	assert.Equal(t, "ckp1", ckps[1].cnLocation.String())
   316  	assert.Equal(t, "ckp2", ckps[2].cnLocation.String())
   317  	assert.Equal(t, "ckp3", ckps[3].cnLocation.String())
   318  
   319  	// 5,1
   320  	ckps = r.ICKPSeekLT(types.BuildTS(5, 0), 1)
   321  	for _, e := range ckps {
   322  		t.Log(e.String())
   323  	}
   324  	assert.Equal(t, 1, len(ckps))
   325  	assert.Equal(t, "ckp1", ckps[0].cnLocation.String())
   326  
   327  	// 50,1
   328  	ckps = r.ICKPSeekLT(types.BuildTS(50, 0), 1)
   329  	for _, e := range ckps {
   330  		t.Log(e.String())
   331  	}
   332  	assert.Equal(t, 0, len(ckps))
   333  
   334  	// 55,3
   335  	ckps = r.ICKPSeekLT(types.BuildTS(55, 0), 3)
   336  	for _, e := range ckps {
   337  		t.Log(e.String())
   338  	}
   339  	assert.Equal(t, 0, len(ckps))
   340  
   341  	// 40,3
   342  	ckps = r.ICKPSeekLT(types.BuildTS(40, 0), 3)
   343  	for _, e := range ckps {
   344  		t.Log(e.String())
   345  	}
   346  	assert.Equal(t, 0, len(ckps))
   347  
   348  	// 35,3
   349  	ckps = r.ICKPSeekLT(types.BuildTS(35, 0), 3)
   350  	for _, e := range ckps {
   351  		t.Log(e.String())
   352  	}
   353  	assert.Equal(t, 0, len(ckps))
   354  
   355  	// 30,3
   356  	ckps = r.ICKPSeekLT(types.BuildTS(30, 0), 3)
   357  	for _, e := range ckps {
   358  		t.Log(e.String())
   359  	}
   360  	assert.Equal(t, 1, len(ckps))
   361  	assert.Equal(t, "ckp3", ckps[0].cnLocation.String())
   362  
   363  	// 30-2,3
   364  	ckps = r.ICKPSeekLT(types.BuildTS(30, 2), 3)
   365  	for _, e := range ckps {
   366  		t.Log(e.String())
   367  	}
   368  	assert.Equal(t, 0, len(ckps))
   369  }