Skip to content

Commit c0231a7

Browse files
author
Fabrizio Furano
committed
eosgrpc: remove some test code, cleanup, introduce the parms ReadUsesLocalTemp/WriteUsesLocalTemp (default false)
1 parent df43e99 commit c0231a7

File tree

2 files changed

+55
-59
lines changed

2 files changed

+55
-59
lines changed

pkg/eosclient/eosgrpc/eosgrpc.go

+55-31
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"fmt"
2525
"io"
26+
"io/ioutil"
2627
"os"
2728
"os/exec"
2829
"path"
@@ -37,13 +38,13 @@ import (
3738
//erpc "github.com/cs3org/reva/pkg/eosclient/eosgrpc/eos_grpc"
3839
ehttp "github.com/cs3org/reva/pkg/eosclient/eosgrpc/eos_http"
3940
"github.com/cs3org/reva/pkg/errtypes"
41+
"github.com/cs3org/reva/pkg/logger"
4042
"github.com/cs3org/reva/pkg/storage/utils/acl"
4143
erpc "github.com/ffurano/grpc-proto/protobuf"
44+
"github.com/google/uuid"
4245
"github.com/pkg/errors"
4346
"github.com/rs/zerolog/log"
4447
"google.golang.org/grpc"
45-
46-
"github.com/cs3org/reva/pkg/logger"
4748
)
4849

4950
const (
@@ -82,6 +83,14 @@ type Options struct {
8283
// Defaults to os.TempDir()
8384
CacheDirectory string
8485

86+
// Set to true to use the local disk as a buffer for chunk
87+
// reads from EOS. Default is false, i.e. pure streaming
88+
ReadUsesLocalTemp bool
89+
90+
// Set to true to use the local disk as a buffer for chunk
91+
// writes to EOS. Default is false, i.e. pure streaming
92+
WriteUsesLocalTemp bool
93+
8594
// Keytab is the location of the EOS keytab file.
8695
Keytab string
8796

@@ -655,7 +664,7 @@ func (c *Client) GetQuota(ctx context.Context, username, rootUID, rootGID, path
655664
msg := new(erpc.NSRequest_QuotaRequest)
656665
msg.Path = []byte(path)
657666
msg.Id = new(erpc.RoleId)
658-
667+
msg.Op = erpc.QUOTAOP_GET
659668
// Eos filters the returned quotas by username. This means that EOS must know it, someone
660669
// must have created an user with that name
661670
msg.Id.Username = username
@@ -737,13 +746,14 @@ func (c *Client) SetQuota(ctx context.Context, rootUID, rootGID string, info *eo
737746
if err != nil {
738747
return err
739748
}
740-
gidInt, err := strconv.ParseUint(info.GID, 10, 64)
741-
if err != nil {
742-
return err
743-
}
749+
750+
// We set a quota for an user, not a group!
744751
msg.Id.Uid = uidInt
745-
msg.Id.Gid = gidInt
752+
msg.Id.Gid = 0
746753
msg.Id.Username = info.Username
754+
msg.Op = erpc.QUOTAOP_SET
755+
msg.Maxbytes = info.MaxBytes
756+
msg.Maxfiles = info.MaxFiles
747757
rq.Command = &erpc.NSRequest_Quota{Quota: msg}
748758

749759
// Now send the req and see what happens
@@ -1161,20 +1171,22 @@ func (c *Client) Read(ctx context.Context, uid, gid, path string) (io.ReadCloser
11611171
log.Info().Str("func", "Read").Str("uid,gid", uid+","+gid).Str("path", path).Msg("")
11621172

11631173
var localTarget string
1164-
//rand := "eosread-" + uuid.New().String()
1165-
//localTarget := fmt.Sprintf("%s/%s", c.opt.CacheDirectory, rand)
1166-
//defer os.RemoveAll(localTarget)
1167-
1174+
var err error
11681175
var localfile io.WriteCloser
11691176
localfile = nil
11701177

1171-
// Uncomment to create a local temp file. Otherwise it streams. With the streaming
1172-
// it's more difficult to return a sound error in the case of troubles
1173-
// localfile, err := os.Create(localTarget)
1174-
// if err != nil {
1175-
// log.Error().Str("func", "Read").Str("path", path).Str("uid,gid", uid+","+gid).Str("err", err.Error()).Msg("")
1176-
// return nil, errtypes.InternalError(fmt.Sprintf("can't open local cache file '%s'", localTarget))
1177-
// }
1178+
if c.opt.ReadUsesLocalTemp {
1179+
rand := "eosread-" + uuid.New().String()
1180+
localTarget := fmt.Sprintf("%s/%s", c.opt.CacheDirectory, rand)
1181+
defer os.RemoveAll(localTarget)
1182+
1183+
log.Info().Str("func", "Read").Str("uid,gid", uid+","+gid).Str("path", path).Str("tempfile", localTarget).Msg("")
1184+
localfile, err = os.Create(localTarget)
1185+
if err != nil {
1186+
log.Error().Str("func", "Read").Str("path", path).Str("uid,gid", uid+","+gid).Str("err", err.Error()).Msg("")
1187+
return nil, errtypes.InternalError(fmt.Sprintf("can't open local temp file '%s'", localTarget))
1188+
}
1189+
}
11781190

11791191
err, bodystream := c.GetHTTPCl().GETFile(ctx, "", uid, gid, path, localfile)
11801192
if err != nil {
@@ -1192,18 +1204,30 @@ func (c *Client) Write(ctx context.Context, uid, gid, path string, stream io.Rea
11921204
log := appctx.GetLogger(ctx)
11931205
log.Info().Str("func", "Write").Str("uid,gid", uid+","+gid).Str("path", path).Msg("")
11941206

1195-
//fd, err := ioutil.TempFile(c.opt.CacheDirectory, "eoswrite-")
1196-
//if err != nil {
1197-
// return err
1198-
// }
1199-
// defer fd.Close()
1200-
// defer os.RemoveAll(fd.Name())
1201-
//
1202-
// // copy stream to local temp file
1203-
// _, err = io.Copy(fd, stream)
1204-
// if err != nil {
1205-
//return err
1206-
//}
1207+
if c.opt.ReadUsesLocalTemp {
1208+
fd, err := ioutil.TempFile(c.opt.CacheDirectory, "eoswrite-")
1209+
if err != nil {
1210+
return err
1211+
}
1212+
defer fd.Close()
1213+
defer os.RemoveAll(fd.Name())
1214+
1215+
log.Info().Str("func", "Write").Str("uid,gid", uid+","+gid).Str("path", path).Str("tempfile", fd.Name()).Msg("")
1216+
// copy stream to local temp file
1217+
_, err = io.Copy(fd, stream)
1218+
if err != nil {
1219+
return err
1220+
}
1221+
1222+
wfd, err := os.Open(fd.Name())
1223+
defer wfd.Close()
1224+
defer os.RemoveAll(fd.Name())
1225+
if err != nil {
1226+
return err
1227+
}
1228+
1229+
return c.GetHTTPCl().PUTFile(ctx, "", uid, gid, path, wfd)
1230+
}
12071231

12081232
return c.GetHTTPCl().PUTFile(ctx, "", uid, gid, path, stream)
12091233

pkg/storage/utils/eosfs/eosfs.go

-28
Original file line numberDiff line numberDiff line change
@@ -611,34 +611,6 @@ func (fs *eosfs) ListFolder(ctx context.Context, ref *provider.Reference, mdKeys
611611
return nil, errors.Wrap(err, "eos: no user in ctx")
612612
}
613613

614-
// test... remove me
615-
rootuid, rootgid, err := fs.getRootUIDAndGID(ctx)
616-
if err != nil {
617-
return nil, err
618-
}
619-
uid, gid, err := fs.getUserUIDAndGID(ctx, u)
620-
if err != nil {
621-
return nil, errors.Wrap(err, "eos: no uid in ctx")
622-
}
623-
// set quota for user
624-
quotaInfo := &eosclient.SetQuotaInfo{
625-
Username: u.Username,
626-
UID: uid,
627-
GID: gid,
628-
MaxBytes: fs.conf.DefaultQuotaBytes,
629-
MaxFiles: fs.conf.DefaultQuotaFiles,
630-
QuotaNode: fs.conf.QuotaNode,
631-
}
632-
633-
err = fs.c.SetQuota(ctx, rootuid, rootgid, quotaInfo)
634-
if err != nil {
635-
err := errors.Wrap(err, "eosfs: error setting quota")
636-
return nil, err
637-
}
638-
639-
fs.GetQuota(ctx)
640-
// end test
641-
642614
p, err := fs.resolve(ctx, u, ref)
643615
if err != nil {
644616
return nil, errors.Wrap(err, "eos: error resolving reference")

0 commit comments

Comments
 (0)