From 891cee4997bb0a269d4b2f35311bab104bdc3283 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 28 Feb 2022 17:14:39 +0300 Subject: [PATCH] NNCPNOSYNC environment variable --- doc/cmd/index.texi | 6 ++++++ doc/news.ru.texi | 18 ++++++++++++++++-- doc/news.texi | 17 +++++++++++++++-- src/cfg.go | 1 + src/cmd/nncp-bundle/main.go | 6 ++++-- src/cmd/nncp-reass/main.go | 6 ++++-- src/cmd/nncp-xfer/main.go | 16 +++++++++------- src/sp.go | 16 +++++++++------- src/tmp.go | 17 ++++++++++++++--- src/toss.go | 22 ++++++++++++---------- 10 files changed, 90 insertions(+), 35 deletions(-) diff --git a/doc/cmd/index.texi b/doc/cmd/index.texi index 56ea8e9..9f3c887 100644 --- a/doc/cmd/index.texi +++ b/doc/cmd/index.texi @@ -49,6 +49,12 @@ Nearly all commands have the following common options: @vindex TMPDIR All commands respect @env{$TMPDIR} environment variable. +@vindex NNCPNOSYNC +If you set @env{$NNCPNOSYNC=1}, then all @code{fsync} operations will be +skipped. That is dangerous option, but if there there are abilities to +rollback possibly corrupted state to some stable snapshot, then disabled +@code{fsync} can give considerable increase in performance. + @menu Configuration file commands diff --git a/doc/news.ru.texi b/doc/news.ru.texi index 161708e..76ebe57 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -1,6 +1,20 @@ @node Новости @section Новости +@node Релиз 8.6.0 +@subsection Релиз 8.6.0 +@itemize + +@item +Появилась возможность отключения @code{fsync} операции +@env{$NNCPNOSYNC=1} переменной окружения. + +@item +Добавлены разнообразные индексы в документации, что должно помочь при +поиске в ней. + +@end itemize + @node Релиз 8.5.0 @subsection Релиз 8.5.0 @itemize @@ -303,7 +317,7 @@ NNCP собирается на NetBSD. @itemize @item -Исправлено падение при fsync директорий после создания @file{.seen} файлов. +Исправлено падение при @code{fsync} директорий после создания @file{.seen} файлов. @end itemize @@ -717,7 +731,7 @@ SP протокол порождает меньше вызовов записе невозможности online аутентификации нод. @item -Явная синхронизация (fsync) директорий для гарантированного +Явная синхронизация (@code{fsync}) директорий для гарантированного переименования файлов. @end itemize diff --git a/doc/news.texi b/doc/news.texi index 861ba7f..93edf29 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -4,6 +4,19 @@ See also this page @ref{Новости, on russian}. +@node Release 8_6_0 +@section Release 8.6.0 +@itemize + +@item +Ability to turn @code{fsync} operation off using @env{$NNCPNOSYNC=1} +environment variable. + +@item +Added various indices in documentation, that should help searching in it. + +@end itemize + @node Release 8_5_0 @section Release 8.5.0 @itemize @@ -298,7 +311,7 @@ command. @itemize @item -Fixed failing directories fsync after @file{.seen} file creation. +Fixed failing directories @code{fsync} after @file{.seen} file creation. @end itemize @@ -700,7 +713,7 @@ Bugfix: private and public Noise keys were swapped in newly created configuration files, that lead to inability to authenticate online peers. @item -Explicit directories fsync-ing for guaranteed files renaming. +Explicit directories @code{fsync}-ing for guaranteed files renaming. @end itemize diff --git a/src/cfg.go b/src/cfg.go index 0937ab0..98bc80c 100644 --- a/src/cfg.go +++ b/src/cfg.go @@ -38,6 +38,7 @@ const ( CfgPathEnv = "NNCPCFG" CfgSpoolEnv = "NNCPSPOOL" CfgLogEnv = "NNCPLOG" + CfgNoSync = "NNCPNOSYNC" ) var ( diff --git a/src/cmd/nncp-bundle/main.go b/src/cmd/nncp-bundle/main.go index d5603a9..071ecea 100644 --- a/src/cmd/nncp-bundle/main.go +++ b/src/cmd/nncp-bundle/main.go @@ -483,8 +483,10 @@ func main() { if err = bufTmp.Flush(); err != nil { log.Fatalln("Error during flushing:", err) } - if err = tmp.Sync(); err != nil { - log.Fatalln("Error during syncing:", err) + if !nncp.NoSync { + if err = tmp.Sync(); err != nil { + log.Fatalln("Error during syncing:", err) + } } if err = tmp.Close(); err != nil { log.Fatalln("Error during closing:", err) diff --git a/src/cmd/nncp-reass/main.go b/src/cmd/nncp-reass/main.go index 0882d28..d095f95 100644 --- a/src/cmd/nncp-reass/main.go +++ b/src/cmd/nncp-reass/main.go @@ -237,8 +237,10 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo log.Fatalln("Can not flush:", err) } if tmp != nil { - if err = tmp.Sync(); err != nil { - log.Fatalln("Can not sync:", err) + if !nncp.NoSync { + if err = tmp.Sync(); err != nil { + log.Fatalln("Can not sync:", err) + } } if err = tmp.Close(); err != nil { log.Fatalln("Can not close:", err) diff --git a/src/cmd/nncp-xfer/main.go b/src/cmd/nncp-xfer/main.go index 41e8c61..68aa102 100644 --- a/src/cmd/nncp-xfer/main.go +++ b/src/cmd/nncp-xfer/main.go @@ -477,13 +477,15 @@ Tx: isBad = true continue } - if err = tmp.Sync(); err != nil { - tmp.Close() - ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string { - return logMsg(les) + ": syncing" - }) - isBad = true - continue + if !nncp.NoSync { + if err = tmp.Sync(); err != nil { + tmp.Close() + ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string { + return logMsg(les) + ": syncing" + }) + isBad = true + continue + } } if err = tmp.Close(); err != nil { ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string { diff --git a/src/sp.go b/src/sp.go index e8e477c..772d92c 100644 --- a/src/sp.go +++ b/src/sp.go @@ -1461,13 +1461,15 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { humanize.IBytes(uint64(fullsize)), ) } - err = fd.Sync() - if err != nil { - state.Ctx.LogE("sp-file-sync", lesp, err, func(les LEs) string { - return logMsg(les) + ": syncing" - }) - state.closeFd(filePathPart) - continue + if !NoSync { + err = fd.Sync() + if err != nil { + state.Ctx.LogE("sp-file-sync", lesp, err, func(les LEs) string { + return logMsg(les) + ": syncing" + }) + state.closeFd(filePathPart) + continue + } } if hasherAndOffset != nil { delete(state.fileHashers, filePath) diff --git a/src/tmp.go b/src/tmp.go index aee5867..4a43eaa 100644 --- a/src/tmp.go +++ b/src/tmp.go @@ -28,6 +28,12 @@ import ( "time" ) +var NoSync bool + +func init() { + NoSync = os.Getenv(CfgNoSync) != "" +} + func TempFile(dir, prefix string) (*os.File, error) { // Assume that probability of suffix collision is negligible suffix := strconv.FormatInt(time.Now().UnixNano()+int64(os.Getpid()), 16) @@ -77,6 +83,9 @@ func (tmp *TmpFileWHash) Cancel() { } func DirSync(dirPath string) error { + if NoSync { + return nil + } fd, err := os.Open(dirPath) if err != nil { return err @@ -102,9 +111,11 @@ func (tmp *TmpFileWHash) Commit(dir string) error { tmp.Fd.Close() return err } - if err = tmp.Fd.Sync(); err != nil { - tmp.Fd.Close() - return err + if !NoSync { + if err = tmp.Fd.Sync(); err != nil { + tmp.Fd.Close() + return err + } } if err = tmp.Fd.Close(); err != nil { return err diff --git a/src/toss.go b/src/toss.go index bdfcb31..e383257 100644 --- a/src/toss.go +++ b/src/toss.go @@ -329,16 +329,18 @@ func jobProcess( }) return err } - if err = tmp.Sync(); err != nil { - tmp.Close() - ctx.LogE("rx-sync", les, err, func(les LEs) string { - return fmt.Sprintf( - "Tossing file %s/%s (%s): %s: syncing", - sender.Name, pktName, - humanize.IBytes(pktSize), dst, - ) - }) - return err + if !NoSync { + if err = tmp.Sync(); err != nil { + tmp.Close() + ctx.LogE("rx-sync", les, err, func(les LEs) string { + return fmt.Sprintf( + "Tossing file %s/%s (%s): %s: syncing", + sender.Name, pktName, + humanize.IBytes(pktSize), dst, + ) + }) + return err + } } if err = tmp.Close(); err != nil { ctx.LogE("rx-close", les, err, func(les LEs) string { -- 2.44.0