cc-backend/pkg/archive/s3Backend.go

319 lines
7.6 KiB
Go
Raw Normal View History

2024-01-25 15:43:06 +01:00
// Copyright (C) 2023 NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package archive
2023-11-09 06:19:53 +01:00
import (
2024-01-25 15:43:06 +01:00
"context"
2023-11-09 06:19:53 +01:00
"encoding/json"
2024-01-25 15:43:06 +01:00
"fmt"
"io"
2024-01-26 17:22:14 +01:00
"path/filepath"
2024-01-25 15:43:06 +01:00
"strconv"
"strings"
"time"
2024-01-26 17:22:14 +01:00
"unsafe"
2023-11-09 06:19:53 +01:00
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/ClusterCockpit/cc-backend/pkg/schema"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
2024-01-26 17:22:14 +01:00
"github.com/pkg/errors"
2023-11-09 06:19:53 +01:00
)
type S3ArchiveConfig struct {
2023-11-09 06:19:53 +01:00
Endpoint string `json:"endpoint"`
AccessKeyID string `json:"accessKeyID"`
SecretAccessKey string `json:"secretAccessKey"`
2024-01-25 15:43:06 +01:00
Bucket string `json:"bucket"`
2023-11-09 06:19:53 +01:00
UseSSL bool `json:"useSSL"`
}
type S3Archive struct {
2024-01-25 15:43:06 +01:00
client *minio.Client
bucket string
clusters []string
}
2023-11-09 06:19:53 +01:00
2024-01-26 17:22:14 +01:00
func (s3a *S3Archive) stat(object string) (*minio.ObjectInfo, error) {
objectStat, e := s3a.client.StatObject(context.Background(),
s3a.bucket,
object, minio.GetObjectOptions{})
if e != nil {
errResponse := minio.ToErrorResponse(e)
if errResponse.Code == "AccessDenied" {
return nil, errors.Wrap(e, "AccessDenied")
}
if errResponse.Code == "NoSuchBucket" {
return nil, errors.Wrap(e, "NoSuchBucket")
}
if errResponse.Code == "InvalidBucketName" {
return nil, errors.Wrap(e, "InvalidBucketName")
}
if errResponse.Code == "NoSuchKey" {
return nil, errors.Wrap(e, "NoSuchKey")
}
return nil, e
}
return &objectStat, nil
}
2023-11-09 06:19:53 +01:00
func (s3a *S3Archive) Init(rawConfig json.RawMessage) (uint64, error) {
var config S3ArchiveConfig
2024-01-25 15:43:06 +01:00
var err error
if err = json.Unmarshal(rawConfig, &config); err != nil {
2023-11-09 06:19:53 +01:00
log.Warnf("Init() > Unmarshal error: %#v", err)
return 0, err
}
2024-01-25 15:43:06 +01:00
fmt.Printf("Endpoint: %s Bucket: %s\n", config.Endpoint, config.Bucket)
s3a.client, err = minio.New(config.Endpoint, &minio.Options{
2023-11-09 06:19:53 +01:00
Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
Secure: config.UseSSL,
})
if err != nil {
2024-01-25 15:43:06 +01:00
err = fmt.Errorf("Init() : Initialize minio client failed")
return 0, err
2023-11-09 06:19:53 +01:00
}
2024-01-25 15:43:06 +01:00
s3a.bucket = config.Bucket
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
found, err := s3a.client.BucketExists(context.Background(), s3a.bucket)
if err != nil {
err = fmt.Errorf("Init() : %v", err)
return 0, err
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
if found {
log.Infof("Bucket found.")
} else {
log.Infof("Bucket not found.")
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
r, err := s3a.client.GetObject(context.Background(),
s3a.bucket, "version.txt", minio.GetObjectOptions{})
if err != nil {
err = fmt.Errorf("Init() : Get version object failed")
return 0, err
}
defer r.Close()
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
b, err := io.ReadAll(r)
if err != nil {
log.Errorf("Init() : %v", err)
return 0, err
}
version, err := strconv.ParseUint(strings.TrimSuffix(string(b), "\n"), 10, 64)
if err != nil {
log.Errorf("Init() : %v", err)
return 0, err
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
if version != Version {
return 0, fmt.Errorf("unsupported version %d, need %d", version, Version)
}
for object := range s3a.client.ListObjects(
context.Background(),
s3a.bucket, minio.ListObjectsOptions{
Recursive: false,
}) {
if object.Err != nil {
log.Errorf("listObject: %v", object.Err)
}
if strings.HasSuffix(object.Key, "/") {
s3a.clusters = append(s3a.clusters, strings.TrimSuffix(object.Key, "/"))
}
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
return version, err
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
func (s3a *S3Archive) Info() {
fmt.Printf("Job archive %s\n", s3a.bucket)
var clusters []string
for object := range s3a.client.ListObjects(
context.Background(),
s3a.bucket, minio.ListObjectsOptions{
Recursive: false,
}) {
if object.Err != nil {
log.Errorf("listObject: %v", object.Err)
}
if strings.HasSuffix(object.Key, "/") {
clusters = append(clusters, object.Key)
}
}
ci := make(map[string]*clusterInfo)
for _, cluster := range clusters {
ci[cluster] = &clusterInfo{dateFirst: time.Now().Unix()}
for d := range s3a.client.ListObjects(
context.Background(),
s3a.bucket, minio.ListObjectsOptions{
Recursive: true,
Prefix: cluster,
}) {
log.Errorf("%s", d.Key)
ci[cluster].diskSize += (float64(d.Size) * 1e-6)
}
}
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
// func (s3a *S3Archive) Exists(job *schema.Job) bool {
// return true
// }
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
func (s3a *S3Archive) LoadJobMeta(job *schema.Job) (*schema.JobMeta, error) {
filename := getPath(job, "/", "meta.json")
log.Infof("Init() : %s", filename)
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
r, err := s3a.client.GetObject(context.Background(),
s3a.bucket, filename, minio.GetObjectOptions{})
if err != nil {
err = fmt.Errorf("Init() : Get version object failed")
return nil, err
}
2024-01-26 17:22:14 +01:00
defer r.Close()
2024-01-25 15:43:06 +01:00
b, err := io.ReadAll(r)
if err != nil {
log.Errorf("Init() : %v", err)
return nil, err
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
return loadJobMeta(b)
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
func (s3a *S3Archive) LoadJobData(job *schema.Job) (schema.JobData, error) {
2024-01-26 17:22:14 +01:00
isCompressed := true
key := getPath(job, "./", "data.json.gz")
_, err := s3a.stat(key)
if err != nil {
if err.Error() == "NoSuchKey" {
key = getPath(job, "./", "data.json")
isCompressed = false
}
}
r, err := s3a.client.GetObject(context.Background(),
s3a.bucket, key, minio.GetObjectOptions{})
if err != nil {
err = fmt.Errorf("Init() : Get version object failed")
return nil, err
}
defer r.Close()
return loadJobData(r, key, isCompressed)
2024-01-25 15:43:06 +01:00
}
2023-11-09 06:19:53 +01:00
2024-01-26 17:22:14 +01:00
func (s3a *S3Archive) LoadClusterCfg(name string) (*schema.Cluster, error) {
key := filepath.Join("./", name, "cluster.json")
r, err := s3a.client.GetObject(context.Background(),
s3a.bucket, key, minio.GetObjectOptions{})
if err != nil {
err = fmt.Errorf("Init() : Get version object failed")
return nil, err
}
defer r.Close()
return DecodeCluster(r)
}
func (s3a *S3Archive) ImportJob(
jobMeta *schema.JobMeta,
jobData *schema.JobData,
) error {
job := schema.Job{
BaseJob: jobMeta.BaseJob,
StartTime: time.Unix(jobMeta.StartTime, 0),
StartTimeUnix: jobMeta.StartTime,
}
r, w := io.Pipe()
if err := EncodeJobMeta(w, jobMeta); err != nil {
log.Error("Error while encoding job metadata to meta.json file")
return err
}
key := getPath(&job, "./", "meta.json")
s3a.client.PutObject(context.Background(),
s3a.bucket, key, r,
int64(unsafe.Sizeof(job)), minio.PutObjectOptions{})
if err := w.Close(); err != nil {
log.Warn("Error while closing meta.json file")
return err
}
//
// f, err = os.Create(path.Join(dir, "data.json"))
// if err != nil {
// log.Error("Error while creating filepath for data.json")
// return err
// }
// if err := EncodeJobData(f, jobData); err != nil {
// log.Error("Error while encoding job metricdata to data.json file")
// return err
// }
// if err := f.Close(); err != nil {
// log.Warn("Error while closing data.json file")
// }
// return err
//
return nil
}
func (s3a *S3Archive) StoreJobMeta(jobMeta *schema.JobMeta) error {
job := schema.Job{
BaseJob: jobMeta.BaseJob,
StartTime: time.Unix(jobMeta.StartTime, 0),
StartTimeUnix: jobMeta.StartTime,
}
r, w := io.Pipe()
if err := EncodeJobMeta(w, jobMeta); err != nil {
log.Error("Error while encoding job metadata to meta.json file")
return err
}
key := getPath(&job, "./", "meta.json")
s3a.client.PutObject(context.Background(),
s3a.bucket, key, r,
int64(unsafe.Sizeof(job)), minio.PutObjectOptions{})
if err := w.Close(); err != nil {
log.Warn("Error while closing meta.json file")
return err
}
return nil
}
func (s3a *S3Archive) GetClusters() []string {
return s3a.clusters
2024-01-25 15:43:06 +01:00
}
2023-11-09 06:19:53 +01:00
2024-01-25 15:43:06 +01:00
//
// func (s3a *S3Archive) CleanUp(jobs []*schema.Job)
//
// func (s3a *S3Archive) Move(jobs []*schema.Job, path string)
//
// func (s3a *S3Archive) Clean(before int64, after int64)
//
// func (s3a *S3Archive) Compress(jobs []*schema.Job)
//
// func (s3a *S3Archive) CompressLast(starttime int64) int64
//
// func (s3a *S3Archive) Iter(loadMetricData bool) <-chan JobContainer