github.com/matrixorigin/matrixone@v1.2.0/pkg/util/status/logtail_server_test.go (about)

     1  // Copyright 2021 -2023 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 status
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/api"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/logtail"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    26  	taelogtail "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail/service"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  type logtailer struct {
    34  	tables []api.TableID
    35  }
    36  
    37  func mockLocktailer(tables ...api.TableID) taelogtail.Logtailer {
    38  	return &logtailer{
    39  		tables: tables,
    40  	}
    41  }
    42  
    43  func mockLogtail(table api.TableID, ts timestamp.Timestamp) logtail.TableLogtail {
    44  	return logtail.TableLogtail{
    45  		CkpLocation: "checkpoint",
    46  		Table:       &table,
    47  		Ts:          &ts,
    48  	}
    49  }
    50  
    51  func (m *logtailer) RangeLogtail(
    52  	ctx context.Context, from, to timestamp.Timestamp,
    53  ) ([]logtail.TableLogtail, []func(), error) {
    54  	tails := make([]logtail.TableLogtail, 0, len(m.tables))
    55  	for _, table := range m.tables {
    56  		tails = append(tails, mockLogtail(table, to))
    57  	}
    58  	return tails, nil, nil
    59  }
    60  
    61  func (m *logtailer) RegisterCallback(cb func(from, to timestamp.Timestamp, closeCB func(), tails ...logtail.TableLogtail) error) {
    62  }
    63  
    64  func (m *logtailer) TableLogtail(
    65  	ctx context.Context, table api.TableID, from, to timestamp.Timestamp,
    66  ) (logtail.TableLogtail, func(), error) {
    67  	for _, t := range m.tables {
    68  		if t.String() == table.String() {
    69  			return mockLogtail(table, to), nil, nil
    70  		}
    71  	}
    72  	return logtail.TableLogtail{CkpLocation: "checkpoint", Table: &table, Ts: &to}, nil, nil
    73  }
    74  
    75  func (m *logtailer) Now() (timestamp.Timestamp, timestamp.Timestamp) {
    76  	panic("not implemented")
    77  }
    78  
    79  func TestFillLogtail(t *testing.T) {
    80  	var status Status
    81  	rt := runtime.DefaultRuntime()
    82  	logtailer := mockLocktailer()
    83  	logtailServer, err := service.NewLogtailServer(
    84  		"", options.NewDefaultLogtailServerCfg(), logtailer, rt,
    85  		service.WithServerCollectInterval(20*time.Millisecond),
    86  		service.WithServerSendTimeout(5*time.Second),
    87  		service.WithServerEnableChecksum(true),
    88  		service.WithServerMaxMessageSize(32+7),
    89  	)
    90  	require.NoError(t, err)
    91  	assert.NoError(t, err)
    92  	mgr := logtailServer.SessionMgr()
    93  	for i := 0; i < 10; i++ {
    94  		mgr.AddSession(uint64(i))
    95  	}
    96  	for i := 0; i < 10; i++ {
    97  		mgr.AddDeletedSession(uint64(i))
    98  	}
    99  	status.LogtailServerStatus.fill(logtailServer)
   100  	assert.Equal(t, 10, len(status.LogtailServerStatus.Sessions))
   101  	assert.Equal(t, 10, len(status.LogtailServerStatus.DeletedSessions))
   102  }