forked from couchbase/indexing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation_manager.go
1139 lines (880 loc) · 31.4 KB
/
mutation_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package indexer
import (
"errors"
"sync"
"sync/atomic"
"github.com/couchbase/indexing/secondary/common"
"github.com/couchbase/indexing/secondary/logging"
)
//MutationManager handles messages from Indexer to manage Mutation Streams
//and flush mutations from mutation queues.
type MutationManager interface {
}
//Map from bucket name to mutation queue
type BucketQueueMap map[string]IndexerMutationQueue
//Map from bucket name to flusher stop channel
type BucketStopChMap map[string]StopChannel
type mutationMgr struct {
memUsed int64 //memory used by queue
maxMemory int64 //max memory to be used
streamBucketQueueMap map[common.StreamId]BucketQueueMap
streamIndexQueueMap map[common.StreamId]IndexQueueMap
streamReaderMap map[common.StreamId]MutationStreamReader
streamReaderCmdChMap map[common.StreamId]MsgChannel //Command msg channel for StreamReader
streamReaderExitChMap map[common.StreamId]DoneChannel //Channel to indicate stream reader exited
streamFlusherStopChMap map[common.StreamId]BucketStopChMap //stop channels for flusher
mutMgrRecvCh MsgChannel //Receive msg channel for Mutation Manager
internalRecvCh MsgChannel //Buffered channel to queue worker messages
supvCmdch MsgChannel //supervisor sends commands on this channel
supvRespch MsgChannel //channel to send any message to supervisor
shutdownCh DoneChannel //internal channel indicating shutdown
indexInstMap common.IndexInstMap
indexPartnMap IndexPartnMap
numVbuckets uint16 //number of vbuckets
flusherWaitGroup sync.WaitGroup
indexerState common.IndexerState
lock sync.Mutex //lock to protect this structure
flock sync.Mutex //fine-grain lock for streamFlusherStopChMap
config common.Config
stats IndexerStatsHolder
}
//NewMutationManager creates a new Mutation Manager which listens for commands from
//Indexer. In case returned MutationManager is nil, Message will have the error msg.
//supvCmdch is a synchronous channel and every request on this channel is followed
//by a response on the same channel. Supervisor is expected to wait for the response
//before issuing a new request on this channel.
//supvRespch will be used by Mutation Manager to send any async error/info messages
//that may happen due to any downstream error or its own processing.
//Additionally, for Flush commands, a sync response is sent on supvCmdch to indicate
//flush has been initiated and once flush completes, another message is sent on
//supvRespch to indicate its completion or any error that may have happened.
//If supvRespch or supvCmdch is closed, mutation manager will termiate its loop.
func NewMutationManager(supvCmdch MsgChannel, supvRespch MsgChannel,
config common.Config) (MutationManager, Message) {
//Init the mutationMgr struct
m := &mutationMgr{
streamBucketQueueMap: make(map[common.StreamId]BucketQueueMap),
streamIndexQueueMap: make(map[common.StreamId]IndexQueueMap),
streamReaderMap: make(map[common.StreamId]MutationStreamReader),
streamReaderCmdChMap: make(map[common.StreamId]MsgChannel),
streamReaderExitChMap: make(map[common.StreamId]DoneChannel),
streamFlusherStopChMap: make(map[common.StreamId]BucketStopChMap),
mutMgrRecvCh: make(MsgChannel),
internalRecvCh: make(MsgChannel, WORKER_MSG_QUEUE_LEN),
shutdownCh: make(DoneChannel),
supvCmdch: supvCmdch,
supvRespch: supvRespch,
numVbuckets: uint16(config["numVbuckets"].Int()),
config: config,
memUsed: 0,
maxMemory: 0,
}
//start Mutation Manager loop which listens to commands from its supervisor
go m.run()
return m, &MsgSuccess{}
}
//run starts the mutation manager loop which listens to messages
//from its workers(stream_reader and flusher) and
//supervisor(indexer)
func (m *mutationMgr) run() {
defer m.panicHandler()
go m.handleWorkerMsgs()
go m.listenWorkerMsgs()
//main Mutation Manager loop
loop:
for {
select {
case cmd, ok := <-m.supvCmdch:
if ok {
if cmd.GetMsgType() == MUT_MGR_SHUTDOWN {
//shutdown and exit the mutation manager loop
msg := m.shutdown()
m.supvCmdch <- msg
break loop
} else {
m.handleSupervisorCommands(cmd)
}
} else {
logging.Fatalf("Supervisor Channel Closed Unexpectedly." +
"Mutation Manager Shutting Itself Down.")
m.shutdown()
break loop
}
}
}
}
func (m *mutationMgr) handleWorkerMsgs() {
for {
select {
// internalRecvCh is a big buffer to bridge a fast sender (mutMgrRecvCh)
// and a slow receiver (supvRespch). This is used for stream management.
// Use a separate go-routine for this so that it wouldn't block the admin
// operation (in main go-routine).
case msg, ok := <-m.internalRecvCh:
if ok {
m.handleWorkerMessage(msg)
}
case _, ok := <-m.shutdownCh:
if !ok {
//shutdown signalled. exit this loop.
return
}
}
}
}
func (m *mutationMgr) listenWorkerMsgs() {
//listen to worker
for {
select {
case msg, ok := <-m.mutMgrRecvCh:
if ok {
//handle high priority messages
switch msg.GetMsgType() {
case STREAM_READER_ERROR:
err := msg.(*MsgStreamError).GetError()
streamId := msg.(*MsgStreamError).GetStreamId()
//if panic, stream reader cannot be sent any more commands
//cleanup any blocked requests and internals structs
if err.code == ERROR_STREAM_READER_PANIC {
logging.Errorf("MutationMgr::listenWorkerMsgs Stream Reader "+
"Received for StreamId: %v, Err: %v", streamId, err)
//signal to any blocked request that stream reader has exited
close(m.streamReaderExitChMap[streamId])
//cleanup entry for the stream reader
func() {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.streamReaderCmdChMap, streamId)
delete(m.streamReaderMap, streamId)
delete(m.streamReaderExitChMap, streamId)
}()
}
}
//all messages need to go to supervisor
m.internalRecvCh <- msg
}
case _, ok := <-m.shutdownCh:
if !ok {
//shutdown signalled. exit this loop.
return
}
}
}
}
//panicHandler handles the panic from underlying stream library
func (r *mutationMgr) panicHandler() {
//panic recovery
if rc := recover(); rc != nil {
var err error
switch x := rc.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("Unknown panic")
}
logging.Fatalf("MutationManager Panic Err %v", err)
logging.Fatalf("%s", logging.StackTrace())
//shutdown the mutation manager
select {
case <-r.shutdownCh:
//if the shutdown channel is closed, this means shutdown was in progress
//when panic happened, skip calling shutdown again
default:
r.shutdown()
}
//panic, propagate to supervisor
msg := &MsgError{
err: Error{code: ERROR_MUT_MGR_PANIC,
severity: FATAL,
category: MUTATION_MANAGER,
cause: err}}
r.supvRespch <- msg
}
}
//handleSupervisorCommands handles the messages from Supervisor
//Each operation acquires the mutex to make the itself atomic.
func (m *mutationMgr) handleSupervisorCommands(cmd Message) {
switch cmd.GetMsgType() {
case OPEN_STREAM:
m.handleOpenStream(cmd)
case ADD_INDEX_LIST_TO_STREAM:
m.handleAddIndexListToStream(cmd)
case REMOVE_INDEX_LIST_FROM_STREAM:
m.handleRemoveIndexListFromStream(cmd)
case REMOVE_BUCKET_FROM_STREAM:
m.handleRemoveBucketFromStream(cmd)
case CLOSE_STREAM:
m.handleCloseStream(cmd)
case CLEANUP_STREAM:
m.handleCleanupStream(cmd)
case MUT_MGR_PERSIST_MUTATION_QUEUE:
m.handlePersistMutationQueue(cmd)
case MUT_MGR_DRAIN_MUTATION_QUEUE:
m.handleDrainMutationQueue(cmd)
case MUT_MGR_GET_MUTATION_QUEUE_HWT:
m.handleGetMutationQueueHWT(cmd)
case MUT_MGR_GET_MUTATION_QUEUE_LWT:
m.handleGetMutationQueueLWT(cmd)
case UPDATE_INDEX_INSTANCE_MAP:
m.handleUpdateIndexInstMap(cmd)
case UPDATE_INDEX_PARTITION_MAP:
m.handleUpdateIndexPartnMap(cmd)
case MUT_MGR_ABORT_PERSIST:
m.handleAbortPersist(cmd)
case CONFIG_SETTINGS_UPDATE:
m.handleConfigUpdate(cmd)
case INDEXER_PAUSE:
m.handleIndexerPause(cmd)
case INDEXER_RESUME:
m.handleIndexerResume(cmd)
default:
logging.Fatalf("MutationMgr::handleSupervisorCommands Received Unknown Command %v", cmd)
common.CrashOnError(errors.New("Unknown Command On Supervisor Channel"))
}
}
//handleWorkerMessage handles messages from workers
func (m *mutationMgr) handleWorkerMessage(cmd Message) {
switch cmd.GetMsgType() {
case STREAM_READER_STREAM_DROP_DATA,
STREAM_READER_STREAM_BEGIN,
STREAM_READER_STREAM_END,
STREAM_READER_ERROR,
STREAM_READER_CONN_ERROR,
STREAM_READER_HWT:
//send message to supervisor to take decision
logging.Tracef("MutationMgr::handleWorkerMessage Received %v from worker", cmd)
m.supvRespch <- cmd
default:
logging.Fatalf("MutationMgr::handleWorkerMessage Received unhandled "+
"message from worker %v", cmd)
common.CrashOnError(errors.New("Unknown Message On Worker Channel"))
}
}
//handleOpenStream creates a new MutationStreamReader and
//initializes it with the mutation queue to store the
//mutations in.
func (m *mutationMgr) handleOpenStream(cmd Message) {
logging.Infof("MutationMgr::handleOpenStream %v", cmd)
streamId := cmd.(*MsgStreamUpdate).GetStreamId()
indexList := cmd.(*MsgStreamUpdate).GetIndexList()
bucket := cmd.(*MsgStreamUpdate).GetBucket()
restartTs := cmd.(*MsgStreamUpdate).GetRestartTs()
bucketFilter := make(map[string]*common.TsVbuuid)
bucketFilter[bucket] = restartTs
m.lock.Lock()
defer m.lock.Unlock()
//if this stream is already open, add to existing stream
if _, ok := m.streamReaderMap[streamId]; ok {
respMsg := m.addIndexListToExistingStream(streamId, indexList, bucketFilter)
m.supvCmdch <- respMsg
return
}
bucketQueueMap := make(BucketQueueMap)
indexQueueMap := make(IndexQueueMap)
//create a new mutation queue for the stream reader
//for each bucket, a separate queue is required
for _, i := range indexList {
//if there is no mutation queue for this index, allocate a new one
if _, ok := bucketQueueMap[i.Defn.Bucket]; !ok {
//init mutation queue
var queue MutationQueue
if queue = NewAtomicMutationQueue(i.Defn.Bucket, m.numVbuckets, &m.maxMemory, &m.memUsed, m.config); queue == nil {
m.supvCmdch <- &MsgError{
err: Error{code: ERROR_MUTATION_QUEUE_INIT,
severity: FATAL,
category: MUTATION_QUEUE}}
return
}
bucketQueueMap[i.Defn.Bucket] = IndexerMutationQueue{
queue: queue}
}
indexQueueMap[i.InstId] = bucketQueueMap[i.Defn.Bucket]
}
cmdCh := make(MsgChannel)
reader, errMsg := CreateMutationStreamReader(streamId, bucketQueueMap, bucketFilter,
cmdCh, m.mutMgrRecvCh, getNumStreamWorkers(m.config), m.stats.Get(),
m.config, m.indexerState)
if reader == nil {
//send the error back on supv channel
m.supvCmdch <- errMsg
} else {
//update internal structs
m.streamReaderMap[streamId] = reader
m.streamBucketQueueMap[streamId] = bucketQueueMap
m.streamIndexQueueMap[streamId] = indexQueueMap
m.streamReaderCmdChMap[streamId] = cmdCh
m.streamReaderExitChMap[streamId] = make(DoneChannel)
func() {
m.flock.Lock()
defer m.flock.Unlock()
m.streamFlusherStopChMap[streamId] = make(BucketStopChMap)
}()
//send success on supv channel
m.supvCmdch <- &MsgSuccess{}
}
}
//handleAddIndexListToStream adds a list of indexes to an
//already running MutationStreamReader. If the list has index
//for a bucket for which there is no mutation queue, it will
//be created.
func (m *mutationMgr) handleAddIndexListToStream(cmd Message) {
logging.Infof("MutationMgr::handleAddIndexListToStream %v", cmd)
streamId := cmd.(*MsgStreamUpdate).GetStreamId()
indexList := cmd.(*MsgStreamUpdate).GetIndexList()
m.lock.Lock()
defer m.lock.Unlock()
//return error if this stream is already closed
if _, ok := m.streamReaderMap[streamId]; !ok {
logging.Errorf("MutationMgr::handleAddIndexListToStream \n\tStream "+
"Already Closed %v", streamId)
m.supvCmdch <- &MsgError{
err: Error{code: ERROR_MUT_MGR_STREAM_ALREADY_CLOSED,
severity: NORMAL,
category: MUTATION_MANAGER}}
return
}
respMsg := m.addIndexListToExistingStream(streamId, indexList, nil)
//send the message back on supv channel
m.supvCmdch <- respMsg
}
func (m *mutationMgr) addIndexListToExistingStream(streamId common.StreamId,
indexList []common.IndexInst, bucketFilter map[string]*common.TsVbuuid) Message {
bucketQueueMap := m.streamBucketQueueMap[streamId]
indexQueueMap := m.streamIndexQueueMap[streamId]
var bucketMapDirty bool
for _, i := range indexList {
//if there is no mutation queue for this index, allocate a new one
if _, ok := bucketQueueMap[i.Defn.Bucket]; !ok {
//init mutation queue
var queue MutationQueue
if queue = NewAtomicMutationQueue(i.Defn.Bucket, m.numVbuckets, &m.maxMemory, &m.memUsed, m.config); queue == nil {
return &MsgError{
err: Error{code: ERROR_MUTATION_QUEUE_INIT,
severity: FATAL,
category: MUTATION_QUEUE}}
}
bucketQueueMap[i.Defn.Bucket] = IndexerMutationQueue{
queue: queue}
bucketMapDirty = true
}
indexQueueMap[i.InstId] = bucketQueueMap[i.Defn.Bucket]
}
if bucketMapDirty {
respMsg := m.sendMsgToStreamReader(streamId,
m.newUpdateBucketQueuesMsg(bucketQueueMap, bucketFilter))
if respMsg.GetMsgType() == MSG_SUCCESS {
//update internal structures
m.streamBucketQueueMap[streamId] = bucketQueueMap
m.streamIndexQueueMap[streamId] = indexQueueMap
}
return respMsg
}
return &MsgSuccess{}
}
func (m *mutationMgr) newUpdateBucketQueuesMsg(bucketQueueMap BucketQueueMap,
bucketFilter map[string]*common.TsVbuuid) *MsgUpdateBucketQueue {
return &MsgUpdateBucketQueue{bucketQueueMap: bucketQueueMap,
bucketFilter: bucketFilter,
stats: m.stats.Get()}
}
//handleRemoveIndexListFromStream removes a list of indexes from an
//already running MutationStreamReader. If all the indexes for a
//bucket get deleted, its mutation queue is dropped.
func (m *mutationMgr) handleRemoveIndexListFromStream(cmd Message) {
logging.Infof("MutationMgr::handleRemoveIndexListFromStream %v", cmd)
streamId := cmd.(*MsgStreamUpdate).GetStreamId()
m.lock.Lock()
defer m.lock.Unlock()
//return error if this stream is already closed
if _, ok := m.streamReaderMap[streamId]; !ok {
logging.Errorf("MutationMgr::handleRemoveIndexListFromStream "+
"Stream Already Closed %v", streamId)
m.supvCmdch <- &MsgError{
err: Error{code: ERROR_MUT_MGR_STREAM_ALREADY_CLOSED,
severity: NORMAL,
category: MUTATION_MANAGER}}
return
}
indexList := cmd.(*MsgStreamUpdate).GetIndexList()
bucketQueueMap := m.streamBucketQueueMap[streamId]
indexQueueMap := m.streamIndexQueueMap[streamId]
//delete the given indexes from map
for _, i := range indexList {
delete(indexQueueMap, i.InstId)
}
var bucketMapDirty bool
//if all indexes for a bucket have been removed, drop the mutation queue
for b, bq := range bucketQueueMap {
dropBucket := true
for _, iq := range indexQueueMap {
//bad check: if the queues match, it is still being used
//better way is to check with bucket
if bq == iq {
dropBucket = false
break
}
}
if dropBucket == true {
//destroy the queue explicitly so that
//any pending mutations in queue get freed
mq := bucketQueueMap[b].queue
mq.Destroy()
delete(bucketQueueMap, b)
bucketMapDirty = true
}
}
if bucketMapDirty {
respMsg := m.sendMsgToStreamReader(streamId, m.newUpdateBucketQueuesMsg(bucketQueueMap, nil))
if respMsg.GetMsgType() == MSG_SUCCESS {
//update internal structures
m.streamBucketQueueMap[streamId] = bucketQueueMap
m.streamIndexQueueMap[streamId] = indexQueueMap
}
//send the message back on supv channel
m.supvCmdch <- respMsg
} else {
m.supvCmdch <- &MsgSuccess{}
}
}
func (m *mutationMgr) handleRemoveBucketFromStream(cmd Message) {
logging.Infof("MutationMgr::handleRemoveBucketFromStream %v", cmd)
streamId := cmd.(*MsgStreamUpdate).GetStreamId()
bucket := cmd.(*MsgStreamUpdate).GetBucket()
m.lock.Lock()
defer m.lock.Unlock()
//return error if this stream is already closed
if _, ok := m.streamReaderMap[streamId]; !ok {
logging.Errorf("MutationMgr::handleRemoveIndexListFromStream "+
"Stream Already Closed %v", streamId)
m.supvCmdch <- &MsgError{
err: Error{code: ERROR_MUT_MGR_STREAM_ALREADY_CLOSED,
severity: NORMAL,
category: MUTATION_MANAGER}}
return
}
bucketQueueMap := m.streamBucketQueueMap[streamId]
indexQueueMap := m.streamIndexQueueMap[streamId]
var bucketMapDirty bool
//delete all indexes for given bucket from map
for _, inst := range m.indexInstMap {
if inst.Defn.Bucket == bucket {
bucketMapDirty = true
delete(indexQueueMap, inst.InstId)
}
}
if _, ok := bucketQueueMap[bucket]; ok {
bucketMapDirty = true
//destroy the queue explicitly so that
//any pending mutations in queue get freed
mq := bucketQueueMap[bucket].queue
mq.Destroy()
delete(bucketQueueMap, bucket)
}
if len(bucketQueueMap) == 0 {
m.sendMsgToStreamReader(streamId,
&MsgGeneral{mType: STREAM_READER_SHUTDOWN})
m.cleanupStream(streamId)
m.supvCmdch <- &MsgSuccess{}
} else if bucketMapDirty {
respMsg := m.sendMsgToStreamReader(streamId, m.newUpdateBucketQueuesMsg(bucketQueueMap, nil))
if respMsg.GetMsgType() == MSG_SUCCESS {
//update internal structures
m.streamBucketQueueMap[streamId] = bucketQueueMap
m.streamIndexQueueMap[streamId] = indexQueueMap
}
//send the message back on supv channel
m.supvCmdch <- respMsg
} else {
m.supvCmdch <- &MsgSuccess{}
}
}
//handleCloseStream closes MutationStreamReader for the specified stream.
func (m *mutationMgr) handleCloseStream(cmd Message) {
logging.Infof("MutationMgr::handleCloseStream %v", cmd)
streamId := cmd.(*MsgStreamUpdate).GetStreamId()
m.lock.Lock()
defer m.lock.Unlock()
//return error if this stream is already closed
if _, ok := m.streamReaderMap[streamId]; !ok {
logging.Errorf("MutationMgr::handleCloseStream Stream "+
"Already Closed %v", streamId)
m.supvCmdch <- &MsgError{
err: Error{code: ERROR_MUT_MGR_STREAM_ALREADY_CLOSED,
severity: NORMAL,
category: MUTATION_MANAGER}}
return
}
respMsg := m.sendMsgToStreamReader(streamId,
&MsgGeneral{mType: STREAM_READER_SHUTDOWN})
if respMsg.GetMsgType() == MSG_SUCCESS {
//update internal data structures
m.cleanupStream(streamId)
}
//send the message back on supv channel
m.supvCmdch <- respMsg
}
//handleCleanupStream cleans up an already closed stream.
//This handles the case when a MutationStreamReader closes
//abruptly. This method can be used to clean up internal
//mutation manager structures.
func (m *mutationMgr) handleCleanupStream(cmd Message) {
logging.Infof("MutationMgr::handleCleanupStream %v", cmd)
streamId := cmd.(*MsgStreamUpdate).GetStreamId()
m.lock.Lock()
defer m.lock.Unlock()
//clean up internal structs for this stream
m.cleanupStream(streamId)
//TODO Send response to supervisor
}
//shutdown shuts down all stream readers and flushers
//This call doesn't return till shutdown is complete.
func (m *mutationMgr) shutdown() Message {
logging.Infof("MutationMgr::shutdown Shutting Down")
//signal shutdown
close(m.shutdownCh)
var uncleanShutdown bool
donech := make(DoneChannel)
go func(donech DoneChannel) {
m.lock.Lock()
defer m.lock.Unlock()
//send shutdown message to all stream readers
for streamId, _ := range m.streamReaderCmdChMap {
respMsg := m.sendMsgToStreamReader(streamId,
&MsgGeneral{mType: STREAM_READER_SHUTDOWN})
if respMsg.GetMsgType() == MSG_SUCCESS {
//update internal data structures
m.cleanupStream(streamId)
} else {
uncleanShutdown = true
}
}
donech <- true
}(donech)
//wait for all readers to shutdown
<-donech
logging.Infof("MutationMgr:shutdown All Stream Readers Shutdown")
//stop flushers
go func() {
m.flock.Lock()
defer m.flock.Unlock()
//send stop signal to all flushers
for _, bucketStopChMap := range m.streamFlusherStopChMap {
for _, stopch := range bucketStopChMap {
close(stopch)
}
}
}()
//wait for all flushers to finish before returning
m.flusherWaitGroup.Wait()
logging.Infof("MutationMgr::shutdown All Flushers Shutdown")
//return error in case of unclean shutdown
if uncleanShutdown {
logging.Errorf("MutationMgr::shutdown Unclean Shutdown")
return &MsgError{
err: Error{code: ERROR_MUT_MGR_UNCLEAN_SHUTDOWN,
severity: NORMAL,
category: MUTATION_MANAGER}}
} else {
logging.Infof("MutationMgr::shutdown Shutdown Complete")
return &MsgSuccess{}
}
}
//sendMsgToStreamReader sends the provided message to the stream reader
//and sends the response back. In case the stream reader panics during the
//communication, error is captured and returned back.
func (m *mutationMgr) sendMsgToStreamReader(streamId common.StreamId, msg Message) Message {
//use select to send message to stream reader,
//in case stream reader has exited, a message on streamReaderExitCh
//will unblock this call
select {
case m.streamReaderCmdChMap[streamId] <- msg:
case <-m.streamReaderExitChMap[streamId]:
logging.Fatalf("MutationMgr::sendMsgToStreamReader Unexpected Stream Reader Exit")
return &MsgError{
err: Error{code: ERROR_STREAM_READER_PANIC,
severity: FATAL,
category: MUTATION_MANAGER}}
}
//use select to wait for stream reader response.
//incase stream reader has exited, a message on streamReaderExitCh
//will unblock this call
select {
case respMsg, ok := <-m.streamReaderCmdChMap[streamId]:
if ok {
return respMsg
} else {
logging.Fatalf("MutationMgr:sendMsgToStreamReader Internal Error. Unexpected" +
"Close for Stream Reader Command Channel")
return &MsgError{
err: Error{code: ERROR_MUT_MGR_INTERNAL_ERROR,
severity: FATAL,
category: MUTATION_MANAGER}}
}
case <-m.streamReaderExitChMap[streamId]:
logging.Fatalf("MutationMgr::sendMsgToStreamReader Unexpected Stream Reader Exit")
return &MsgError{
err: Error{code: ERROR_STREAM_READER_PANIC,
severity: FATAL,
category: MUTATION_MANAGER}}
}
}
//cleanupStream cleans up internal structs for the given stream
func (m *mutationMgr) cleanupStream(streamId common.StreamId) {
//cleanup internal maps for this stream
delete(m.streamReaderMap, streamId)
delete(m.streamBucketQueueMap, streamId)
delete(m.streamIndexQueueMap, streamId)
delete(m.streamReaderCmdChMap, streamId)
delete(m.streamReaderExitChMap, streamId)
m.flock.Lock()
defer m.flock.Unlock()
delete(m.streamFlusherStopChMap, streamId)
}
//handlePersistMutationQueue handles persist queue message from
//Indexer. Success is sent on the supervisor Cmd channel
//if the flush can be processed. Once the queue gets persisted,
//status is sent on the supervisor Response channel.
func (m *mutationMgr) handlePersistMutationQueue(cmd Message) {
logging.Tracef("MutationMgr::handlePersistMutationQueue %v", cmd)
bucket := cmd.(*MsgMutMgrFlushMutationQueue).GetBucket()
streamId := cmd.(*MsgMutMgrFlushMutationQueue).GetStreamId()
ts := cmd.(*MsgMutMgrFlushMutationQueue).GetTimestamp()
changeVec := cmd.(*MsgMutMgrFlushMutationQueue).GetChangeVector()
m.lock.Lock()
defer m.lock.Unlock()
q := m.streamBucketQueueMap[streamId][bucket]
stats := m.stats.Get()
go m.persistMutationQueue(q, streamId, bucket, ts, changeVec, stats)
m.supvCmdch <- &MsgSuccess{}
}
//persistMutationQueue implements the actual persist for the queue
func (m *mutationMgr) persistMutationQueue(q IndexerMutationQueue,
streamId common.StreamId, bucket string, ts *common.TsVbuuid,
changeVec []bool, stats *IndexerStats) {
m.flock.Lock()
defer m.flock.Unlock()
stopch := make(StopChannel)
m.streamFlusherStopChMap[streamId][bucket] = stopch
m.flusherWaitGroup.Add(1)
go func(config common.Config) {
defer m.flusherWaitGroup.Done()
flusher := NewFlusher(config, stats)
sts := getSeqTsFromTsVbuuid(ts)
msgch := flusher.PersistUptoTS(q.queue, streamId, ts.Bucket,
m.indexInstMap, m.indexPartnMap, sts, changeVec, stopch)
//wait for flusher to finish
msg := <-msgch
//update map and free lock before blocking on the supv channel
func() {
m.flock.Lock()
defer m.flock.Unlock()
//delete the stop channel from the map
delete(m.streamFlusherStopChMap[streamId], bucket)
}()
stats.memoryUsedQueue.Set(atomic.LoadInt64(&m.memUsed))
//send the response to supervisor
if msg.GetMsgType() == MSG_SUCCESS {
m.supvRespch <- &MsgMutMgrFlushDone{mType: MUT_MGR_FLUSH_DONE,
streamId: streamId,
bucket: bucket,
ts: ts}
} else {
m.supvRespch <- &MsgMutMgrFlushDone{mType: MUT_MGR_FLUSH_DONE,
streamId: streamId,
bucket: bucket,
ts: ts,
aborted: true}
}
}(m.config)
}
//handleDrainMutationQueue handles drain queue message from
//supervisor. Success is sent on the supervisor Cmd channel
//if the flush can be processed. Once the queue gets drained,
//status is sent on the supervisor Response channel.
func (m *mutationMgr) handleDrainMutationQueue(cmd Message) {
logging.Tracef("MutationMgr::handleDrainMutationQueue %v", cmd)
bucket := cmd.(*MsgMutMgrFlushMutationQueue).GetBucket()
streamId := cmd.(*MsgMutMgrFlushMutationQueue).GetStreamId()
ts := cmd.(*MsgMutMgrFlushMutationQueue).GetTimestamp()
changeVec := cmd.(*MsgMutMgrFlushMutationQueue).GetChangeVector()
m.lock.Lock()
defer m.lock.Unlock()
q := m.streamBucketQueueMap[streamId][bucket]
stats := m.stats.Get()
go m.drainMutationQueue(q, streamId, bucket, ts, changeVec, stats)
m.supvCmdch <- &MsgSuccess{}
}
//drainMutationQueue implements the actual drain for the queue
func (m *mutationMgr) drainMutationQueue(q IndexerMutationQueue,
streamId common.StreamId, bucket string, ts *common.TsVbuuid,
changeVec []bool, stats *IndexerStats) {
m.flock.Lock()
defer m.flock.Unlock()
stopch := make(StopChannel)
m.streamFlusherStopChMap[streamId][bucket] = stopch
m.flusherWaitGroup.Add(1)
go func(config common.Config) {
defer m.flusherWaitGroup.Done()
flusher := NewFlusher(config, stats)
sts := getSeqTsFromTsVbuuid(ts)
msgch := flusher.DrainUptoTS(q.queue, streamId, ts.Bucket,
sts, changeVec, stopch)
//wait for flusher to finish
msg := <-msgch
//update map and free lock before blocking on the supv channel
func() {
m.flock.Lock()
defer m.flock.Unlock()
//delete the stop channel from the map
delete(m.streamFlusherStopChMap[streamId], bucket)
}()
//send the response to supervisor
m.supvRespch <- msg
}(m.config)
}
//handleGetMutationQueueHWT calculates HWT for a mutation queue
//for a given stream and bucket
func (m *mutationMgr) handleGetMutationQueueHWT(cmd Message) {
logging.Tracef("MutationMgr::handleGetMutationQueueHWT %v", cmd)
bucket := cmd.(*MsgMutMgrGetTimestamp).GetBucket()
streamId := cmd.(*MsgMutMgrGetTimestamp).GetStreamId()
m.lock.Lock()
defer m.lock.Unlock()
q := m.streamBucketQueueMap[streamId][bucket]
stats := m.stats.Get()
go func(config common.Config) {
flusher := NewFlusher(config, stats)
ts := flusher.GetQueueHWT(q.queue)
m.supvCmdch <- &MsgTimestamp{ts: ts}
}(m.config)
}
//handleGetMutationQueueLWT calculates LWT for a mutation queue
//for a given stream and bucket
func (m *mutationMgr) handleGetMutationQueueLWT(cmd Message) {
logging.Tracef("MutationMgr::handleGetMutationQueueLWT %v", cmd)
bucket := cmd.(*MsgMutMgrGetTimestamp).GetBucket()
streamId := cmd.(*MsgMutMgrGetTimestamp).GetStreamId()
m.lock.Lock()
defer m.lock.Unlock()
q := m.streamBucketQueueMap[streamId][bucket]
stats := m.stats.Get()
go func(config common.Config) {
flusher := NewFlusher(config, stats)
ts := flusher.GetQueueLWT(q.queue)
m.supvCmdch <- &MsgTimestamp{ts: ts}
}(m.config)
}
//handleUpdateIndexInstMap updates the indexInstMap
func (m *mutationMgr) handleUpdateIndexInstMap(cmd Message) {
logging.Infof("MutationMgr::handleUpdateIndexInstMap %v", cmd)
m.lock.Lock()
defer m.lock.Unlock()
req := cmd.(*MsgUpdateInstMap)
indexInstMap := req.GetIndexInstMap()