From 8447d011adcd791e580f942554df15a90f5a8f87 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Mon, 27 Mar 2023 13:24:06 +0200 Subject: [PATCH] Add version to job archive --- pkg/archive/archive.go | 9 +++++++-- pkg/archive/fsBackend.go | 26 +++++++++++++++++++++----- tools/archive-migration/main.go | 9 +++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index d4d1582..5c6a517 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -8,12 +8,15 @@ import ( "encoding/json" "fmt" + "github.com/ClusterCockpit/cc-backend/pkg/log" "github.com/ClusterCockpit/cc-backend/pkg/lrucache" "github.com/ClusterCockpit/cc-backend/pkg/schema" ) +const Version = 1 + type ArchiveBackend interface { - Init(rawConfig json.RawMessage) error + Init(rawConfig json.RawMessage) (int, error) LoadJobMeta(job *schema.Job) (*schema.JobMeta, error) @@ -50,9 +53,11 @@ func Init(rawConfig json.RawMessage) error { return fmt.Errorf("unkown archive backend '%s''", kind.Kind) } - if err := ar.Init(rawConfig); err != nil { + version, err := ar.Init(rawConfig) + if err != nil { return err } + log.Infof("Load archive version %d", version) return initClusterConfig() } diff --git a/pkg/archive/fsBackend.go b/pkg/archive/fsBackend.go index eceeaf4..1ee7394 100644 --- a/pkg/archive/fsBackend.go +++ b/pkg/archive/fsBackend.go @@ -61,31 +61,47 @@ func loadJobMeta(filename string) (*schema.JobMeta, error) { return DecodeJobMeta(bufio.NewReader(f)) } -func (fsa *FsArchive) Init(rawConfig json.RawMessage) error { +func (fsa *FsArchive) Init(rawConfig json.RawMessage) (int, error) { var config FsArchiveConfig if err := json.Unmarshal(rawConfig, &config); err != nil { log.Errorf("fsBackend Init()- %v", err) - return err + return 0, err } if config.Path == "" { err := fmt.Errorf("fsBackend Init()- empty path") log.Errorf("fsBackend Init()- %v", err) - return err + return 0, err } fsa.path = config.Path + b, err := os.ReadFile(fmt.Sprintf("%s/version.txt", fsa.path)) + if err != nil { + fmt.Println("Err") + return 0, err + } + + version, err := strconv.Atoi(string(b)) + if err != nil { + log.Errorf("fsBackend Init()- %v", err) + return 0, err + } + + if version != Version { + return version, fmt.Errorf("Unsupported version %d, need %d", version, Version) + } + entries, err := os.ReadDir(fsa.path) if err != nil { log.Errorf("fsBackend Init()- %v", err) - return err + return 0, err } for _, de := range entries { fsa.clusters = append(fsa.clusters, de.Name()) } - return nil + return version, nil } func (fsa *FsArchive) LoadJobData(job *schema.Job) (schema.JobData, error) { diff --git a/tools/archive-migration/main.go b/tools/archive-migration/main.go index 70e4df2..30bca15 100644 --- a/tools/archive-migration/main.go +++ b/tools/archive-migration/main.go @@ -7,6 +7,7 @@ package main import ( "bufio" "encoding/json" + "errors" "flag" "fmt" "log" @@ -17,6 +18,8 @@ import ( "github.com/ClusterCockpit/cc-backend/pkg/units" ) +const Version = 1 + var ar FsArchive func loadJobData(filename string) (*JobData, error) { @@ -169,6 +172,10 @@ func main() { flag.StringVar(&srcPath, "s", "./var/job-archive", "Specify the source job archive path. Default is ./var/job-archive") flag.StringVar(&dstPath, "d", "./var/job-archive-new", "Specify the destination job archive path. Default is ./var/job-archive-new") + if _, err := os.Stat(fmt.Sprintf("%s/version.txt", srcPath)); !errors.Is(err, os.ErrNotExist) { + log.Fatal("Archive version exists!") + } + srcConfig := fmt.Sprintf("{\"path\": \"%s\"}", srcPath) err := ar.Init(json.RawMessage(srcConfig)) if err != nil { @@ -248,4 +255,6 @@ func main() { } }() } + + os.WriteFile(fmt.Sprintf("%s/version.txt", dstPath), []byte(fmt.Sprintf("%d", Version)), 0644) }