github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/logtail.go (about)

     1  // Copyright 2022 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 disttae
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	v2 "github.com/matrixorigin/matrixone/pkg/util/metric/v2"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/disttae/cache"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/catalog"
    25  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    26  	"github.com/matrixorigin/matrixone/pkg/container/types"
    27  	"github.com/matrixorigin/matrixone/pkg/pb/api"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/disttae/logtailreplay"
    29  )
    30  
    31  func consumeEntry(
    32  	ctx context.Context,
    33  	primarySeqnum int,
    34  	engine *Engine,
    35  	cache *cache.CatalogCache,
    36  	state *logtailreplay.PartitionState,
    37  	e *api.Entry,
    38  ) error {
    39  	start := time.Now()
    40  	defer func() {
    41  		v2.LogtailUpdatePartitonConsumeLogtailOneEntryDurationHistogram.Observe(time.Since(start).Seconds())
    42  	}()
    43  
    44  	var packer *types.Packer
    45  	put := engine.packerPool.Get(&packer)
    46  	defer put.Put()
    47  
    48  	if state != nil {
    49  		t0 := time.Now()
    50  		state.HandleLogtailEntry(ctx, engine.fs, e, primarySeqnum, packer)
    51  		v2.LogtailUpdatePartitonConsumeLogtailOneEntryLogtailReplayDurationHistogram.Observe(time.Since(t0).Seconds())
    52  	}
    53  
    54  	if logtailreplay.IsMetaTable(e.TableName) {
    55  		return nil
    56  	}
    57  
    58  	t0 := time.Now()
    59  	if e.EntryType == api.Entry_Insert {
    60  		switch e.TableId {
    61  		case catalog.MO_TABLES_ID:
    62  			bat, _ := batch.ProtoBatchToBatch(e.Bat)
    63  			if cache != nil {
    64  				cache.InsertTable(bat)
    65  			}
    66  		case catalog.MO_DATABASE_ID:
    67  			bat, _ := batch.ProtoBatchToBatch(e.Bat)
    68  			if cache != nil {
    69  				cache.InsertDatabase(bat)
    70  			}
    71  		case catalog.MO_COLUMNS_ID:
    72  			bat, _ := batch.ProtoBatchToBatch(e.Bat)
    73  			if cache != nil {
    74  				cache.InsertColumns(bat)
    75  			}
    76  		}
    77  		v2.LogtailUpdatePartitonConsumeLogtailOneEntryUpdateCatalogCacheDurationHistogram.Observe(time.Since(t0).Seconds())
    78  		return nil
    79  	}
    80  
    81  	switch e.TableId {
    82  	case catalog.MO_TABLES_ID:
    83  		bat, _ := batch.ProtoBatchToBatch(e.Bat)
    84  		if cache != nil {
    85  			cache.DeleteTable(bat)
    86  		}
    87  	case catalog.MO_DATABASE_ID:
    88  		bat, _ := batch.ProtoBatchToBatch(e.Bat)
    89  		if cache != nil {
    90  			cache.DeleteDatabase(bat)
    91  		}
    92  	}
    93  	v2.LogtailUpdatePartitonConsumeLogtailOneEntryUpdateCatalogCacheDurationHistogram.Observe(time.Since(t0).Seconds())
    94  
    95  	return nil
    96  }