-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve datanode snapshot creation
As of now, the snapshot are created in a sequential and blocking way in the datanode. This means that while a snapshot is being taken, no block can be processed. The following approach is made: - the database is locked with a transaction - queries are generated - one by one the query are: - executed - the result piped into the file system - finally the lock is released, and later the files are added to ipfs. The bottle neck here is that the results are being save on the fs as they arrive, which is unecessary and amount for 95% of the time spent snapshoting (and so blocking anything else). To prevent this, we keep those results from the database in buffers, and only save them to file via a worker go routine. Signed-off-by: Jeremy Letang <[email protected]>
- Loading branch information
1 parent
eb07bfd
commit 7b3aa5f
Showing
8 changed files
with
220 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (C) 2023 Gobalsky Labs Limited | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package snapshot | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"sync" | ||
) | ||
|
||
type bufAndPath struct { | ||
buf *bytes.Buffer | ||
isProgressFile bool | ||
path string | ||
} | ||
|
||
type FileWorker struct { | ||
mu sync.Mutex | ||
queue []*bufAndPath | ||
} | ||
|
||
func NewFileWorker() *FileWorker { | ||
return &FileWorker{ | ||
queue: []*bufAndPath{}, | ||
} | ||
} | ||
|
||
func (fw *FileWorker) Add(buf *bytes.Buffer, path string) { | ||
fw.mu.Lock() | ||
defer fw.mu.Unlock() | ||
|
||
fw.queue = append(fw.queue, &bufAndPath{buf, false, path}) | ||
} | ||
|
||
func (fw *FileWorker) AddLockFile(path string) { | ||
fw.mu.Lock() | ||
defer fw.mu.Unlock() | ||
|
||
fw.queue = append(fw.queue, &bufAndPath{nil, true, path}) | ||
} | ||
|
||
func (fw *FileWorker) peek() (bp *bufAndPath) { | ||
fw.mu.Lock() | ||
defer fw.mu.Unlock() | ||
|
||
if len(fw.queue) <= 0 { | ||
return | ||
} | ||
|
||
bp, fw.queue = fw.queue[0], fw.queue[1:] | ||
|
||
return | ||
} | ||
|
||
func (fw *FileWorker) Empty() bool { | ||
fw.mu.Lock() | ||
defer fw.mu.Unlock() | ||
|
||
return len(fw.queue) <= 0 | ||
} | ||
|
||
func (fw *FileWorker) Consume() error { | ||
bp := fw.peek() | ||
if bp == nil { | ||
return nil // nothing to do | ||
} | ||
|
||
if bp.isProgressFile { | ||
return fw.removeLockFile(bp.path) | ||
} | ||
|
||
return fw.writeSegment(bp) | ||
} | ||
|
||
func (fw *FileWorker) removeLockFile(path string) error { | ||
return os.Remove(path) | ||
} | ||
|
||
func (fw *FileWorker) writeSegment(bp *bufAndPath) error { | ||
file, err := os.Create(bp.path) | ||
if err != nil { | ||
return fmt.Errorf("failed to create file %s : %w", bp.path, err) | ||
} | ||
|
||
defer file.Close() | ||
|
||
_, err = bp.buf.WriteTo(file) | ||
if err != nil { | ||
return fmt.Errorf("couldn't writer to file %v : %w", bp.path, err) | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.