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