Skip to content

Commit ff6d40d

Browse files
committed
Add Golang storage implementation and update configuration for deployment
1 parent 43b4729 commit ff6d40d

File tree

5 files changed

+135
-4
lines changed

5 files changed

+135
-4
lines changed

.dockerignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@ cache
77
python-venv
88
regression-*
99
*_code
10-
11-
# ignore, while building docker image
12-
scylladb-volume/
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package golang
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
"strconv"
11+
"strings"
12+
13+
"github.com/google/uuid"
14+
"github.com/minio/minio-go/v7"
15+
"github.com/minio/minio-go/v7/pkg/credentials"
16+
)
17+
18+
type Storage struct {
19+
client *minio.Client
20+
}
21+
22+
var instance *Storage
23+
24+
func UniqueName(name string) string {
25+
ext := filepath.Ext(name)
26+
basename := name[:len(name)-len(ext)]
27+
uuidStr := strings.Split(uuid.New().String(), "-")[0]
28+
return fmt.Sprintf("%s.%s%s", basename, uuidStr, ext)
29+
}
30+
31+
func NewStorage() (*Storage, error) {
32+
address, exists := os.LookupEnv("MINIO_ADDRESS")
33+
if !exists {
34+
return nil, nil
35+
}
36+
37+
accessKey := os.Getenv("MINIO_ACCESS_KEY")
38+
secretKey := os.Getenv("MINIO_SECRET_KEY")
39+
40+
parts := strings.Split(address, ":")
41+
endpoint := parts[0]
42+
var port int = 9000 // Default port
43+
44+
if len(parts) > 1 {
45+
var err error
46+
port, err = strconv.Atoi(parts[1])
47+
if err != nil {
48+
return nil, fmt.Errorf("invalid port in MINIO_ADDRESS: %v", err)
49+
}
50+
}
51+
52+
client, err := minio.New(endpoint, &minio.Options{
53+
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
54+
Secure: false,
55+
Port: uint16(port),
56+
})
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return &Storage{client: client}, nil
62+
}
63+
64+
func (s *Storage) Upload(bucket, file, filepath string) (string, error) {
65+
keyName := UniqueName(file)
66+
_, err := s.client.FPutObject(context.Background(), bucket, keyName, filepath, minio.PutObjectOptions{})
67+
if err != nil {
68+
return "", err
69+
}
70+
return keyName, nil
71+
}
72+
73+
func (s *Storage) Download(bucket, file, filepath string) error {
74+
return s.client.FGetObject(context.Background(), bucket, file, filepath, minio.GetObjectOptions{})
75+
}
76+
77+
func (s *Storage) DownloadDirectory(bucket, prefix, path string) error {
78+
ctx := context.Background()
79+
objects := s.client.ListObjects(ctx, bucket, minio.ListObjectsOptions{
80+
Prefix: prefix,
81+
Recursive: true,
82+
})
83+
84+
for object := range objects {
85+
if object.Err != nil {
86+
return object.Err
87+
}
88+
89+
objectPath := filepath.Join(path, object.Key)
90+
if err := os.MkdirAll(filepath.Dir(objectPath), 0755); err != nil {
91+
return err
92+
}
93+
94+
if err := s.Download(bucket, object.Key, objectPath); err != nil {
95+
return err
96+
}
97+
}
98+
99+
return nil
100+
}
101+
102+
func (s *Storage) UploadStream(bucket, file string, data []byte) (string, error) {
103+
keyName := UniqueName(file)
104+
reader := bytes.NewReader(data)
105+
_, err := s.client.PutObject(context.Background(), bucket, keyName, reader, int64(len(data)), minio.PutObjectOptions{})
106+
if err != nil {
107+
return "", err
108+
}
109+
return keyName, nil
110+
}
111+
112+
func (s *Storage) DownloadStream(bucket, file string) ([]byte, error) {
113+
obj, err := s.client.GetObject(context.Background(), bucket, file, minio.GetObjectOptions{})
114+
if err != nil {
115+
return nil, err
116+
}
117+
defer obj.Close()
118+
119+
return io.ReadAll(obj)
120+
}
121+
122+
func GetInstance() (*Storage, error) {
123+
if instance == nil {
124+
var err error
125+
instance, err = NewStorage()
126+
if err != nil {
127+
return nil, err
128+
}
129+
}
130+
return instance, nil
131+
}

config/systems.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"username": "docker_user",
8080
"deployment": {
8181
"files": [
82-
"handler.go",
8382
"storage.go"
8483
],
8584
"packages": []

dockerfiles/golang_installer.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

33
cd /mnt/function
4+
go mod init github.com/spcl/serverless-benchmarks
5+
go get github.com/minio/minio-go/v7
6+
go get github.com/google/uuid
47

58
go mod tidy

sebs/local/local.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def package_code(
124124
CONFIG_FILES = {
125125
"python": ["handler.py", "requirements.txt", ".python_packages"],
126126
"nodejs": ["handler.js", "package.json", "node_modules"],
127+
"golang": ["handler.go", "go.mod", "go.sum"],
127128
}
128129
package_config = CONFIG_FILES[language_name]
129130
function_dir = os.path.join(directory, "function")

0 commit comments

Comments
 (0)