github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/sm/sm.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 sm
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    21  )
    22  
    23  type stateMachine struct {
    24  	closed          common.Closable
    25  	wg              *sync.WaitGroup
    26  	receiveQueue    Queue
    27  	checkpointQueue Queue
    28  }
    29  
    30  func NewStateMachine(wg *sync.WaitGroup, closed common.Closable, rQueue, ckpQueue Queue) *stateMachine {
    31  	return &stateMachine{
    32  		closed:          closed,
    33  		wg:              wg,
    34  		receiveQueue:    rQueue,
    35  		checkpointQueue: ckpQueue,
    36  	}
    37  }
    38  
    39  func (sm *stateMachine) EnqueueRecevied(item any) (any, error) {
    40  	return sm.receiveQueue.Enqueue(item)
    41  }
    42  
    43  func (sm *stateMachine) EnqueueCheckpoint(item any) (any, error) {
    44  	return sm.checkpointQueue.Enqueue(item)
    45  }
    46  
    47  func (sm *stateMachine) Start() {
    48  	if sm.checkpointQueue != nil {
    49  		sm.checkpointQueue.Start()
    50  	}
    51  	if sm.receiveQueue != nil {
    52  		sm.receiveQueue.Start()
    53  	}
    54  }
    55  
    56  func (sm *stateMachine) Stop() {
    57  	if !sm.closed.TryClose() {
    58  		return
    59  	}
    60  	if sm.receiveQueue != nil {
    61  		sm.receiveQueue.Stop()
    62  	}
    63  	if sm.checkpointQueue != nil {
    64  		sm.checkpointQueue.Stop()
    65  	}
    66  	sm.wg.Wait()
    67  }