Skip to content

Commit ab28ed7

Browse files
committed
feat(store): store minor refactor
1 parent caeb4ac commit ab28ed7

File tree

23 files changed

+231
-226
lines changed

23 files changed

+231
-226
lines changed

damocles-manager/core/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ type SealerAPI interface {
8383
AllocateUnsealSector(ctx context.Context, spec AllocateSectorSpec) (*SectorUnsealInfo, error)
8484
AchieveUnsealSector(ctx context.Context, sid abi.SectorID, pieceCid cid.Cid, errInfo string) (Meta, error)
8585
AcquireUnsealDest(ctx context.Context, sid abi.SectorID, pieceCid cid.Cid) ([]string, error)
86+
87+
StoreUri(ctx context.Context, storeName string, resource string) (string, error)
8688
}
8789

8890
type SealerCliAPI interface {

damocles-manager/core/client_gen.go

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type SealerAPIClient struct {
3838
AllocateUnsealSector func(ctx context.Context, spec AllocateSectorSpec) (*SectorUnsealInfo, error)
3939
AchieveUnsealSector func(ctx context.Context, sid abi.SectorID, pieceCid cid.Cid, errInfo string) (Meta, error)
4040
AcquireUnsealDest func(ctx context.Context, sid abi.SectorID, pieceCid cid.Cid) ([]string, error)
41+
StoreUri func(ctx context.Context, storeName, resource []string) (map[string]string, error)
4142
}
4243

4344
var UnavailableSealerAPIClient = SealerAPIClient{
@@ -108,6 +109,9 @@ var UnavailableSealerAPIClient = SealerAPIClient{
108109
AcquireUnsealDest: func(ctx context.Context, sid abi.SectorID, pieceCid cid.Cid) ([]string, error) {
109110
panic("SealerAPI client unavailable")
110111
},
112+
StoreUri: func(ctx context.Context, storeName, resource []string) (map[string]string, error) {
113+
panic("SealerAPI client unavailable")
114+
},
111115
}
112116

113117
// SealerCliAPIClient is generated client for SealerCliAPI interface.

damocles-manager/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require (
2727
github.com/golang/mock v1.6.0
2828
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026
2929
github.com/hashicorp/go-multierror v1.1.1
30-
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230830062024-608c68ada10e
30+
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230831091846-caeb4acea7d7
3131
github.com/ipfs-force-community/venus-cluster-assets v0.1.0
3232
github.com/ipfs/go-cid v0.3.2
3333
github.com/ipfs/go-datastore v0.6.0

damocles-manager/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@ github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lTo
589589
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
590590
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
591591
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
592-
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230830062024-608c68ada10e h1:SEmUD7xCpHWlnTrZdyj++RExTy0T6bqX4yS/iXBHVAg=
593-
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230830062024-608c68ada10e/go.mod h1:me1u2cl7qdxBCZiVL0laDop8uBHDdUwlUNnQ7KkHF64=
592+
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230831091846-caeb4acea7d7 h1:tgh0JH97wqkDaFu8TKAqi7eQucm0gyPG0f4uaj9rLWM=
593+
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230831091846-caeb4acea7d7/go.mod h1:me1u2cl7qdxBCZiVL0laDop8uBHDdUwlUNnQ7KkHF64=
594594
github.com/ipfs-force-community/go-jsonrpc v0.1.7-0.20230220074347-8db78dbc20d4 h1:iu/3irYevdNpdc0B/gRi1vuS3+lRn+6Ro9G0FeBiAfE=
595595
github.com/ipfs-force-community/go-jsonrpc v0.1.7-0.20230220074347-8db78dbc20d4/go.mod h1:jBSvPTl8V1N7gSTuCR4bis8wnQnIjHbRPpROol6iQKM=
596596
github.com/ipfs-force-community/venus-cluster-assets v0.1.0 h1:K/0+OV9Jm7HjSa7O9MAtgfLDIudQYZUTymhJsp8rGXg=

damocles-manager/modules/impl/mock/sealer.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,35 @@ import (
1818
"github.com/ipfs-force-community/damocles/damocles-manager/core"
1919
"github.com/ipfs-force-community/damocles/damocles-manager/modules"
2020
chainAPI "github.com/ipfs-force-community/damocles/damocles-manager/pkg/chain"
21+
"github.com/ipfs-force-community/damocles/damocles-manager/pkg/objstore"
2122
"github.com/ipfs-force-community/damocles/damocles-manager/ver"
2223
)
2324

2425
var _ core.SealerAPI = (*Sealer)(nil)
2526

2627
func NewSealer(rand core.RandomnessAPI, sector core.SectorManager, deal core.DealManager, commit core.CommitmentManager,
2728
api chainAPI.API, scfg modules.SafeConfig,
29+
persistedStoreManager objstore.Manager,
2830
) (*Sealer, error) {
2931
return &Sealer{
30-
rand: rand,
31-
sector: sector,
32-
deal: deal,
33-
commit: commit,
34-
api: api,
35-
scfg: scfg,
32+
rand: rand,
33+
sector: sector,
34+
deal: deal,
35+
commit: commit,
36+
api: api,
37+
scfg: scfg,
38+
persistedStoreManager: persistedStoreManager,
3639
}, nil
3740
}
3841

3942
type Sealer struct {
40-
rand core.RandomnessAPI
41-
sector core.SectorManager
42-
deal core.DealManager
43-
commit core.CommitmentManager
44-
api chainAPI.API
45-
scfg modules.SafeConfig
43+
rand core.RandomnessAPI
44+
sector core.SectorManager
45+
deal core.DealManager
46+
commit core.CommitmentManager
47+
api chainAPI.API
48+
scfg modules.SafeConfig
49+
persistedStoreManager objstore.Manager
4650
}
4751

4852
func (s *Sealer) AllocateSector(ctx context.Context, spec core.AllocateSectorSpec) (*core.AllocatedSector, error) {
@@ -315,3 +319,15 @@ func (s *Sealer) AcquireUnsealDest(ctx context.Context, sid abi.SectorID, pieceC
315319
func (s *Sealer) Version(context.Context) (string, error) {
316320
return ver.VersionStr(), nil
317321
}
322+
323+
func (s *Sealer) StoreUri(ctx context.Context, storeName string, resource string) (string, error) {
324+
store, err := s.persistedStoreManager.GetInstance(ctx, storeName)
325+
if err != nil {
326+
return "", err
327+
}
328+
uri, err := store.Uri(ctx, resource)
329+
if err != nil {
330+
return "", fmt.Errorf("get uri(%s): %w", resource, err)
331+
}
332+
return uri, nil
333+
}

damocles-manager/modules/sealer/sealer.go

+12
Original file line numberDiff line numberDiff line change
@@ -833,3 +833,15 @@ func (s *Sealer) AchieveUnsealSector(ctx context.Context, sid abi.SectorID, piec
833833
func (s *Sealer) AcquireUnsealDest(ctx context.Context, sid abi.SectorID, pieceCid cid.Cid) ([]string, error) {
834834
return s.unseal.AcquireDest(ctx, sid, pieceCid)
835835
}
836+
837+
func (s *Sealer) StoreUri(ctx context.Context, storeName string, resource string) (string, error) {
838+
store, err := s.sectorIdxer.StoreMgr().GetInstance(ctx, storeName)
839+
if err != nil {
840+
return "", err
841+
}
842+
uri, err := store.Uri(ctx, resource)
843+
if err != nil {
844+
return "", fmt.Errorf("get uri(%s): %w", resource, err)
845+
}
846+
return uri, nil
847+
}

damocles-manager/modules/sealer/sealer_cli.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,19 @@ func (s *Sealer) RemoveSector(ctx context.Context, sid abi.SectorID) error {
258258
sealed = util.SectorPath(util.SectorPathTypeSealed, state.ID)
259259
}
260260

261-
cachePath := cacheDir.FullPath(ctx, cache)
261+
cachePath, err := cacheDir.Uri(ctx, cache)
262+
if err != nil {
263+
return fmt.Errorf("get uri(%s): %w", cache, err)
264+
}
262265
err = os.RemoveAll(cachePath)
263266
if err != nil {
264267
return fmt.Errorf("remove cache: %w", err)
265268
}
266269

267-
sealedPath := sealedFile.FullPath(ctx, sealed)
270+
sealedPath, err := sealedFile.Uri(ctx, sealed)
271+
if err != nil {
272+
return fmt.Errorf("get uri(%s): %w", sealed, err)
273+
}
268274
err = os.Remove(sealedPath)
269275
if err != nil {
270276
return fmt.Errorf("remove sealed file: %w", err)

damocles-manager/pkg/objstore/filestore/store.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (s *Store) Del(ctx context.Context, p string) error {
215215
return objstore.ErrReadOnlyStore
216216
}
217217

218-
fpath, err := s.getAbsPath(p)
218+
fpath, err := s.getAbsPath(ctx, p)
219219
if err != nil {
220220
return err
221221
}
@@ -235,7 +235,14 @@ func (s *Store) Stat(ctx context.Context, p string) (objstore.Stat, error) {
235235

236236
var res statOrErr
237237

238-
finfo, err := os.Stat(s.FullPath(ctx, p))
238+
p, err := s.getAbsPath(ctx, p)
239+
if err != nil {
240+
res.Err = fmt.Errorf("getAbsPath(%s): %w", p, err)
241+
resCh <- res
242+
return
243+
}
244+
245+
finfo, err := os.Stat(p)
239246
if err == nil {
240247
res.Stat.Size = finfo.Size()
241248
} else {
@@ -254,14 +261,18 @@ func (s *Store) Stat(ctx context.Context, p string) (objstore.Stat, error) {
254261
}
255262
}
256263

257-
func (s *Store) getAbsPath(p string) (string, error) {
258-
fpath, err := filepath.Abs(filepath.Join(s.cfg.Path, p))
264+
func (s *Store) getAbsPath(ctx context.Context, resourceName string) (string, error) {
265+
uri, err := s.Uri(ctx, resourceName)
266+
if err != nil {
267+
return "", fmt.Errorf("get uri(%s): %w", resourceName, objstore.ErrInvalidObjectPath)
268+
}
269+
fpath, err := filepath.Abs(filepath.Join(s.cfg.Path, uri))
259270
if err != nil {
260-
return "", fmt.Errorf("obj %s: %w", p, objstore.ErrInvalidObjectPath)
271+
return "", fmt.Errorf("obj %s: %w", uri, objstore.ErrInvalidObjectPath)
261272
}
262273

263274
if !strings.HasPrefix(fpath, s.cfg.Path) {
264-
return "", fmt.Errorf("obj %s: %w: outside of the dir", p, objstore.ErrInvalidObjectPath)
275+
return "", fmt.Errorf("obj %s: %w: outside of the dir", uri, objstore.ErrInvalidObjectPath)
265276
}
266277

267278
return fpath, nil
@@ -287,6 +298,6 @@ func (s *Store) Put(ctx context.Context, p string, r io.Reader) (int64, error) {
287298
return io.Copy(file, r)
288299
}
289300

290-
func (s *Store) FullPath(ctx context.Context, sub string) string {
291-
return filepath.Join(s.cfg.Path, sub)
301+
func (s *Store) Uri(ctx context.Context, resourceName string) (string, error) {
302+
return resourceName, nil
292303
}

damocles-manager/pkg/objstore/mock_store.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ func (ms *MockStore) Put(ctx context.Context, p string, r io.Reader) (int64, err
144144
return written, nil
145145
}
146146

147-
func (ms *MockStore) FullPath(ctx context.Context, p string) string {
148-
return p
147+
func (ms *MockStore) Uri(ctx context.Context, p string) (string, error) {
148+
return p, nil
149149
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
//! ObjectStore implemented based on fs
22
3+
use std::collections::HashMap;
34
use std::fs::create_dir_all;
45
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
6+
use std::sync::Arc;
57

68
use anyhow::{anyhow, Context, Result};
9+
use jsonrpc_core::futures_util::{FutureExt, TryFutureExt};
710

8-
use super::{ObjResult, ObjectStore};
9-
use crate::logging::trace;
10-
11-
const LOG_TARGET: &str = "filestore";
11+
use super::{ObjResult, ObjectStore, ObjectStoreError};
12+
use crate::{logging::trace, rpc::sealer::SealerClient, sealing::call_rpc};
1213

1314
/// FileStore
1415
pub struct FileStore {
15-
sep: String,
1616
local_path: PathBuf,
1717
instance: String,
1818
readonly: bool,
19+
rpc: Arc<SealerClient>,
1920
}
2021

2122
impl FileStore {
@@ -27,7 +28,7 @@ impl FileStore {
2728
}
2829

2930
/// open the file store at given path
30-
pub fn open<P: AsRef<Path>>(p: P, ins: Option<String>, readonly: bool) -> Result<Self> {
31+
pub fn open<P: AsRef<Path>>(p: P, ins: Option<String>, readonly: bool, rpc: Arc<SealerClient>) -> Result<Self> {
3132
let dir_path = p.as_ref().canonicalize().context("canonicalize dir path")?;
3233
if !dir_path.metadata().context("read dir metadata").map(|meta| meta.is_dir())? {
3334
return Err(anyhow!("base path of the file store should a dir"));
@@ -39,67 +40,31 @@ impl FileStore {
3940
};
4041

4142
Ok(FileStore {
42-
sep: MAIN_SEPARATOR.to_string(),
4343
local_path: dir_path,
4444
instance,
4545
readonly,
46+
rpc,
4647
})
4748
}
48-
49-
fn path<P: AsRef<Path>>(&self, sub: P) -> ObjResult<PathBuf> {
50-
let mut p = sub.as_ref();
51-
if p.starts_with(".") {
52-
return Err(anyhow!("sub path starts with dot").into());
53-
}
54-
55-
// try to strip the first any only the first sep
56-
if let Ok(strip) = p.strip_prefix(&self.sep) {
57-
p = strip;
58-
}
59-
60-
if p.starts_with(&self.sep) {
61-
return Err(anyhow!("sub path starts with separator").into());
62-
}
63-
64-
let res = self.local_path.join(p);
65-
trace!(target: LOG_TARGET, ?res, "get full path");
66-
Ok(res)
67-
}
6849
}
6950

7051
impl ObjectStore for FileStore {
7152
fn instance(&self) -> String {
7253
self.instance.clone()
7354
}
7455

75-
fn uri(&self, rel: &Path) -> ObjResult<PathBuf> {
76-
self.path(rel)
56+
fn uri(&self, resource_name: &str) -> ObjResult<String> {
57+
let uri = call_rpc! {
58+
self.rpc=>store_uri(
59+
self.instance(),
60+
resource_name.to_string(),
61+
)
62+
}
63+
.map_err(|e| ObjectStoreError::Other(e.1))?;
64+
Ok(self.local_path.join(uri).display().to_string())
7765
}
7866

7967
fn readonly(&self) -> bool {
8068
self.readonly
8169
}
8270
}
83-
84-
#[cfg(test)]
85-
mod tests {
86-
use super::*;
87-
88-
#[test]
89-
fn test_path() {
90-
let fs = FileStore::open("/tmp", None, false).unwrap();
91-
assert_eq!(fs.path("/a/b").unwrap(), PathBuf::from("/tmp/a/b"));
92-
assert_eq!(fs.path("a/b").unwrap(), PathBuf::from("/tmp/a/b"));
93-
assert_eq!(fs.path("a/b/").unwrap(), PathBuf::from("/tmp/a/b"));
94-
assert_eq!(fs.path("/a/b/").unwrap(), PathBuf::from("/tmp/a/b"));
95-
}
96-
97-
#[test]
98-
fn test_store_uri() {
99-
let fs = FileStore::open("/tmp/", Some("test_store".to_string()), false).unwrap();
100-
assert_eq!(fs.uri(Path::new("a/b")).unwrap(), PathBuf::from("/tmp/a/b"));
101-
assert_eq!(fs.uri(Path::new("/a/b")).unwrap(), PathBuf::from("/tmp/a/b"));
102-
assert_eq!(fs.uri(Path::new("a/b/")).unwrap(), PathBuf::from("/tmp/a/b"));
103-
assert_eq!(fs.uri(Path::new("/a/b/")).unwrap(), PathBuf::from("/tmp/a/b"));
104-
}
105-
}

damocles-worker/src/infra/objstore/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! abstractions & implementations for object store
22
3+
use std::collections::HashMap;
34
use std::error::Error;
45
use std::fmt;
56
use std::io;
@@ -69,7 +70,10 @@ pub trait ObjectStore: Send + Sync {
6970
/// unique identifier of the given resource.
7071
/// for fs-like stores, this should return an abs path.
7172
/// for other stores, this may return a url, or path part of a url.
72-
fn uri(&self, resource: &Path) -> ObjResult<PathBuf>;
73+
///
74+
/// the resource value looks like this:
75+
/// - "cache/sc-02-data-tree-r-last.dat"
76+
fn uri(&self, resource: &str) -> ObjResult<String>;
7377

7478
/// if this instance is read-only
7579
fn readonly(&self) -> bool;

damocles-worker/src/rpc/sealer/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,7 @@ pub trait Sealer {
537537

538538
#[rpc(name = "Venus.WdPoStFinishJob")]
539539
fn wdpost_finish(&self, job_id: String, output: Option<WindowPoStOutput>, error_reason: String) -> Result<()>;
540+
541+
#[rpc(name = "Venus.StoreUri")]
542+
fn store_uri(&self, store_name: String, resources: String) -> Result<String>;
540543
}

0 commit comments

Comments
 (0)