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