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