Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/ledger/kvledger/kv_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func (l *kvLedger) initSnapshotMgr(initializer *lgrInitializer) error {
events: make(chan *event),
commitProceed: make(chan struct{}),
requestResponses: make(chan *requestResponse),
stopCh: make(chan struct{}),
}

bcInfo, err := l.blockStore.GetBlockchainInfo()
Expand Down
22 changes: 16 additions & 6 deletions core/ledger/kvledger/snapshot_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type snapshotMgr struct {
requestResponses chan *requestResponse
stopped bool
shutdownLock sync.Mutex
stopCh chan struct{}
wg sync.WaitGroup
}

type event struct {
Expand Down Expand Up @@ -134,14 +136,19 @@ func (l *kvLedger) processSnapshotMgmtEvents(lastCommittedBlockNumber uint64) {
continue
}
snapshotInProgress = true
l.snapshotMgr.wg.Add(1)
go func() {
defer l.snapshotMgr.wg.Done()
logger.Infow("Generating snapshot", "channelID", l.ledgerID, "lastCommittedBlockNumber", lastCommittedBlockNumber)
if err := l.generateSnapshot(); err != nil {
logger.Errorw("Failed to generate snapshot", "channelID", l.ledgerID, "lastCommittedBlockNumber", lastCommittedBlockNumber, "error", err)
} else {
logger.Infow("Generated snapshot", "channelID", l.ledgerID, "lastCommittedBlockNumber", lastCommittedBlockNumber)
}
events <- &event{snapshotDone, lastCommittedBlockNumber}
select {
case events <- &event{snapshotDone, lastCommittedBlockNumber}:
case <-l.snapshotMgr.stopCh:
}
}()

case snapshotDone:
Expand Down Expand Up @@ -194,14 +201,19 @@ func (l *kvLedger) processSnapshotMgmtEvents(lastCommittedBlockNumber uint64) {

if committerStatus == idle && requestedBlockNum == lastCommittedBlockNumber {
snapshotInProgress = true
l.snapshotMgr.wg.Add(1)
go func() {
defer l.snapshotMgr.wg.Done()
logger.Infow("Generating snapshot", "channelID", l.ledgerID, "lastCommittedBlockNumber", lastCommittedBlockNumber)
if err := l.generateSnapshot(); err != nil {
logger.Errorw("Failed to generate snapshot", "channelID", l.ledgerID, "lastCommittedBlockNumber", lastCommittedBlockNumber, "error", err)
} else {
logger.Infow("Generated snapshot", "channelID", l.ledgerID, "lastCommittedBlockNumber", lastCommittedBlockNumber)
}
events <- &event{snapshotDone, requestedBlockNum}
select {
case events <- &event{snapshotDone, requestedBlockNum}:
case <-l.snapshotMgr.stopCh:
}
}()
}
requestResponses <- &requestResponse{}
Expand Down Expand Up @@ -248,10 +260,6 @@ func (l *kvLedger) snapshotExists(blockNum uint64) (bool, error) {
return stat != nil, nil
}

// shutdown sends a snapshotMgrShutdown event and close all the channels, which is called
// when the ledger is closed. For simplicity, this function does not consider in-progress commit
// or snapshot generation. The caller should make sure there is no in-progress commit or
// snapshot generation. Otherwise, it may cause panic because the channels have been closed.
func (m *snapshotMgr) shutdown() {
m.shutdownLock.Lock()
defer m.shutdownLock.Unlock()
Expand All @@ -261,6 +269,8 @@ func (m *snapshotMgr) shutdown() {
}

m.stopped = true
close(m.stopCh)
m.wg.Wait()
m.events <- &event{typ: snapshotMgrShutdown}
close(m.events)
close(m.commitProceed)
Expand Down