github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/sm/safeq.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 "context" 19 "runtime" 20 "sync" 21 "sync/atomic" 22 23 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 24 ) 25 26 const ( 27 Created int32 = iota 28 Running 29 ReceiverStopped 30 PrepareStop 31 Stopped 32 ) 33 34 type safeQueue struct { 35 queue chan any 36 ctx context.Context 37 cancel context.CancelFunc 38 wg sync.WaitGroup 39 state atomic.Int32 40 pending atomic.Int64 41 batchSize int 42 onItemsCB OnItemsCB 43 } 44 45 func NewSafeQueue(queueSize, batchSize int, onItem OnItemsCB) *safeQueue { 46 q := &safeQueue{ 47 queue: make(chan any, queueSize), 48 batchSize: batchSize, 49 onItemsCB: onItem, 50 } 51 q.state.Store(Created) 52 q.ctx, q.cancel = context.WithCancel(context.Background()) 53 return q 54 } 55 56 func (q *safeQueue) Start() { 57 q.state.Store(Running) 58 q.wg.Add(1) 59 items := make([]any, 0, q.batchSize) 60 go func() { 61 defer q.wg.Done() 62 for { 63 select { 64 case <-q.ctx.Done(): 65 return 66 case item := <-q.queue: 67 if q.onItemsCB == nil { 68 continue 69 } 70 items = append(items, item) 71 Left: 72 for i := 0; i < q.batchSize-1; i++ { 73 select { 74 case item = <-q.queue: 75 items = append(items, item) 76 default: 77 break Left 78 } 79 } 80 cnt := len(items) 81 q.onItemsCB(items...) 82 items = items[:0] 83 q.pending.Add(-1 * int64(cnt)) 84 } 85 } 86 }() 87 } 88 89 func (q *safeQueue) Stop() { 90 q.stopReceiver() 91 q.waitStop() 92 close(q.queue) 93 } 94 95 func (q *safeQueue) stopReceiver() { 96 state := q.state.Load() 97 if state >= ReceiverStopped { 98 return 99 } 100 q.state.CompareAndSwap(state, ReceiverStopped) 101 } 102 103 func (q *safeQueue) waitStop() { 104 if q.state.Load() <= Running { 105 panic("logic error") 106 } 107 if q.state.Load() == Stopped { 108 return 109 } 110 if q.state.CompareAndSwap(ReceiverStopped, PrepareStop) { 111 for q.pending.Load() != 0 { 112 runtime.Gosched() 113 } 114 q.cancel() 115 } 116 q.wg.Wait() 117 } 118 119 func (q *safeQueue) Enqueue(item any) (any, error) { 120 if q.state.Load() != Running { 121 return item, common.ErrClose 122 } 123 q.pending.Add(1) 124 if q.state.Load() != Running { 125 q.pending.Add(-1) 126 return item, common.ErrClose 127 } 128 q.queue <- item 129 return item, nil 130 }