github.com/matrixorigin/matrixone@v0.7.0/pkg/pb/pipeline/pipeline.go (about)

     1  // Copyright 2021 - 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 pipeline
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  )
    23  
    24  const (
    25  	// Basic message type
    26  	UnknowType = iota
    27  	PipelineMessage
    28  	BatchMessage
    29  	PrepareDoneNotifyMessage // for dispatch
    30  
    31  	// Status type
    32  	BatchEnd
    33  	BatchWaitingNext
    34  	WaitingNext
    35  	MessageEnd
    36  )
    37  
    38  func (m *Message) Size() int {
    39  	return m.ProtoSize()
    40  }
    41  
    42  func (m *Message) GetID() uint64 {
    43  	return m.Id
    44  }
    45  
    46  func (m *Message) SetID(id uint64) {
    47  	m.Id = id
    48  }
    49  
    50  func (m *Message) SetSid(sid uint64) {
    51  	m.Sid = sid
    52  }
    53  
    54  func (m *Message) SetCheckSum(sum uint32) {
    55  	m.Checksum = sum
    56  }
    57  
    58  func (m *Message) SetSequence(s uint64) {
    59  	m.Sequence = s
    60  }
    61  
    62  func (m *Message) SetMoError(ctx context.Context, err error) {
    63  	m.Err = EncodedMessageError(ctx, err)
    64  }
    65  
    66  func (m *Message) SetAnalysis(data []byte) {
    67  	m.Analyse = data
    68  }
    69  
    70  func (m *Message) TryToGetMoErr() (error, bool) {
    71  	errData := m.GetErr()
    72  	if len(errData) > 0 {
    73  		err := &moerr.Error{}
    74  		if errUnmarshal := err.UnmarshalBinary(errData); errUnmarshal != nil {
    75  			return errUnmarshal, true
    76  		}
    77  		return err, true
    78  	}
    79  	return nil, false
    80  }
    81  
    82  func (m *Message) SetMessageType(cmd uint64) {
    83  	m.Cmd = cmd
    84  }
    85  
    86  func (m *Message) SetData(data []byte) {
    87  	m.Data = data
    88  }
    89  
    90  func (m *Message) SetProcData(data []byte) {
    91  	m.ProcInfoData = data
    92  }
    93  
    94  func (m *Message) DebugString() string {
    95  	errInfo := "none"
    96  	if len(m.Err) > 0 {
    97  		me := moerr.Error{}
    98  		errInfo = me.UnmarshalBinary(m.Err).Error()
    99  	}
   100  	return fmt.Sprintf("MessageSize: %d, sid: %d, ErrInfo: %s, batchSize: %d", m.Size(), m.Sid, errInfo, len(m.Data))
   101  }
   102  
   103  func (m *Message) IsBatchMessage() bool {
   104  	return m.GetCmd() == BatchMessage
   105  }
   106  
   107  func (m *Message) IsNotifyMessage() bool {
   108  	return m.GetCmd() == PrepareDoneNotifyMessage
   109  }
   110  
   111  func (m *Message) IsPipelineMessage() bool {
   112  	return m.GetCmd() == PipelineMessage
   113  }
   114  
   115  func (m *Message) IsEndMessage() bool {
   116  	return m.Sid == MessageEnd
   117  }
   118  
   119  func (m *Message) WaitingNextToMerge() bool {
   120  	return m.Sid == WaitingNext
   121  }
   122  
   123  func (m *Message) BatcWaitingNextToMerge() bool {
   124  	return m.Sid == BatchWaitingNext
   125  }
   126  
   127  func EncodedMessageError(ctx context.Context, err error) []byte {
   128  	var errData []byte
   129  	if err == nil {
   130  		return nil
   131  	}
   132  	if me, ok := err.(*moerr.Error); ok {
   133  		if ed, err1 := me.MarshalBinary(); err1 == nil {
   134  			errData = ed
   135  		} else {
   136  			errData, _ = err1.(*moerr.Error).MarshalBinary()
   137  		}
   138  	} else {
   139  		// XXXXX It's so bad that if we still received non mo err here. Just convert all them to be mo err now.
   140  		// once we eliminate all the hidden dangers brought by non mo err, should delete these code.
   141  		errData, _ = moerr.ConvertGoError(ctx, err).(*moerr.Error).MarshalBinary()
   142  	}
   143  	return errData
   144  }
   145  
   146  func GetMessageErrorInfo(m *Message) error {
   147  	errData := m.GetErr()
   148  	if len(errData) > 0 {
   149  		err := &moerr.Error{}
   150  		if errUnmarshal := err.UnmarshalBinary(errData); errUnmarshal != nil {
   151  			return errUnmarshal
   152  		}
   153  		return err
   154  	}
   155  	return nil
   156  }