Skip to content

Commit

Permalink
fix(plugin): update plugin deps
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5459 committed Sep 14, 2023
1 parent 68b14f0 commit 19f7965
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 160 deletions.
32 changes: 16 additions & 16 deletions docs/dev/damocles-manager插件框架.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ damocles-manager 插件机制基于 [Go plugin](https://pkg.go.dev/plugin#sectio
name = "memdb"
# 插件描述
description = "kvstore in memory"
# 插件类型,当前支持: ObjStore | KVStore | SyncSectorState
# 插件类型,当前支持: FileStore | KVStore | SyncSectorState
kind = "KVStore"
# 指定插件的初始化函数
onInit = "OnInit"
Expand All @@ -41,35 +41,35 @@ damocles-manager 插件机制基于 [Go plugin](https://pkg.go.dev/plugin#sectio
启动 damocles-manager 后会以日志的形式输出所有成功加载的插件。
```
2023-01-13T11:33:09.658+0800 INFO dep dep/sealer_constructor.go:132 loaded plugin 'KVStore/memdb', build time: '2023.01.13 11:32:43'.
2023-01-13T11:33:09.658+0800 INFO dep dep/sealer_constructor.go:132 loaded plugin 'ObjStore/fsstore', build time: '2023.01.13 11:32:43'.
2023-01-13T11:33:09.658+0800 INFO dep dep/sealer_constructor.go:132 loaded plugin 'FileStore/examplefstore', build time: '2023.01.13 11:32:43'.
...
```
---
### 当前支持的插件类型
#### ObjStore 插件
#### FileStore 插件
`ObjStore` 允许用户创建自定的存储类型,例如 s3, fs 等
`FileStore` 允许用户创建自定的存储类型,受 filecoin 封装算法限制,目前只支持文件系统,但是 FileStore 插件支持用户自定义扇区文件的存储路径
##### Manifest:
struct 定义:
```go
type ObjStoreManifest struct {
type FileStoreManifest struct {
Manifest
Constructor func(cfg objstore.Config) (objstore.Store, error)
Constructor func(cfg filestore.Config) (filestore.Store, error)
}
```

manifest.toml 示例:
```toml
# manifest.toml

name = "s3store"
description = "s3 plugin"
# 插件类型设置为: ObjStore
kind = "ObjStore"
name = "mystore"
description = "my filestore plugin"
# 插件类型设置为: FileStore
kind = "FileStore"
onInit = "OnInit"
onShutdown = "OnShutdown"

Expand All @@ -79,9 +79,9 @@ export = [
]
```

`ObjStore` 插件只需要额外提供一个 `Constructor` 函数返回实现了 [objstore.Store](https://github.com/ipfs-force-community/venus-objstore/blob/00ad77fcbfed1df5c1613176521bce3ba3041fc7/objstore.go#L50-L61) 接口的对象即可。
`FileStore` 插件只需要额外提供一个 `Constructor` 函数返回实现了 [filestore.Store](https://github.com/ipfs-force-community/damocles/blob/68b14f09025eab2ed7adb5b86ebb0b098fb3063f/manager-plugin/filestore/filestore.go#L75-L103) 接口的对象即可。

`ObjStore` 目前用于 Piece 文件存储 (PieceStore) 与封装后的扇区数据存储 (PersistStores), 当正确配置 ObjStore 插件后, damocles-manager 会调用插件返回的 `objstore.Store` 进行文件读写。
`FileStore` 目前用于 Piece 文件存储 (PieceStore) 与封装后的扇区数据存储 (PersistStores), 当正确配置 FileStore 插件后, damocles-manager 会调用插件返回的 `filesotre.Store` 进行文件读写。

##### PieceStore 插件配置样例

Expand All @@ -94,17 +94,17 @@ Dir = "path/to/damocles-manager-plugins-dir"

# damocles-manager 可以配置多个 PieceStore, 每个 PieceStore 都可以使用不同的插件。
[[Common.PieceStores]]
Name = "my-s3-store"
Name = "my-file-store"
Path = "/path"

# 指定插件名称
# 注意: PluginName 不是插件程序的文件名,而是在 manifest.toml 中配置的名称
PluginName = "s3store"
PluginName = "mystore"

[[Common.PieceStores.Meta]]
# Your plugin config here
Bucket = "mybucket"
# ConfigPath = "path/to/my-s3-store-config.toml"
SubDirPattern = "sub-%d-%d"
# ConfigPath = "path/to/my-store-config.toml"
```

PersistStores 的插件配置与 PieceStore 类似,详细请参考 damocles-manager 配置文件说明。
Expand Down
File renamed without changes.
104 changes: 104 additions & 0 deletions manager-plugin/examples/examplefstore/examplefstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/filecoin-project/go-state-types/abi"
plugin "github.com/ipfs-force-community/damocles/manager-plugin"
pluginfilestore "github.com/ipfs-force-community/damocles/manager-plugin/filestore"
"github.com/shirou/gopsutil/disk"
)

var _ pluginfilestore.Store = (*Store)(nil)

func OnInit(ctx context.Context, pluginsDir string, manifest *plugin.Manifest) error {
return nil
}

func Open(cfg pluginfilestore.Config) (pluginfilestore.Store, error) { // nolint: deadcode
dirPath, err := filepath.Abs(cfg.Path)
if err != nil {
return nil, fmt.Errorf("abs path for %s: %w", cfg.Path, err)
}

stat, err := os.Stat(dirPath)
if err != nil {
return nil, fmt.Errorf("stat for %s: %w", dirPath, err)
}

if !stat.IsDir() {
return nil, fmt.Errorf("%s is not a dir", dirPath)
}

cfg.Path = dirPath
if cfg.Name == "" {
cfg.Name = dirPath
}

return &Store{
cfg: cfg,
}, nil
}

type Store struct {
cfg pluginfilestore.Config
}

func (s *Store) Type() string {
return "builtin-filesotre"
}

func (*Store) Version() string {
return "example"
}

func (s *Store) Instance(context.Context) string { return s.cfg.Name }

func (s *Store) InstanceConfig(_ context.Context) pluginfilestore.Config {
return s.cfg
}

func (s *Store) InstanceInfo(ctx context.Context) (pluginfilestore.InstanceInfo, error) {
usage, err := disk.UsageWithContext(ctx, s.cfg.Path)
if err != nil {
return pluginfilestore.InstanceInfo{}, fmt.Errorf("get disk usage: %w", err)
}

return pluginfilestore.InstanceInfo{
Config: s.cfg,
Type: usage.Fstype,
Total: usage.Total,
Free: usage.Free,
Used: usage.Used,
UsedPercent: usage.UsedPercent,
}, nil
}

func (s *Store) SubPath(ctx context.Context, pathType pluginfilestore.PathType, sectorID *pluginfilestore.SectorID, custom *string) (subPath string, err error) {
if pathType == pluginfilestore.PathTypeCustom {
if custom == nil {
return "", fmt.Errorf("sectorID cannot be nil")
}
return *custom, nil
}

if sectorID == nil {
return "", fmt.Errorf("sectorID cannot be nil")
}

sid := abi.SectorID{
Miner: abi.ActorID(sectorID.Miner),
Number: abi.SectorNumber(sectorID.Number),
}

return filepath.Join(string(pathType), FormatSectorID(sid)), nil
}

const sectorIDFormat = "s-t0%d-%d"

func FormatSectorID(sid abi.SectorID) string {
return fmt.Sprintf(sectorIDFormat, sid.Miner, sid.Number)
}
36 changes: 36 additions & 0 deletions manager-plugin/examples/examplefstore/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module github.com/ipfs-force-community/damocles/manager-plugin/examples/examplefstore

go 1.20

require (
github.com/filecoin-project/go-state-types v0.11.1
github.com/ipfs-force-community/damocles/manager-plugin v0.0.0-20230913074304-520ac6d716fa
github.com/shirou/gopsutil v2.18.12+incompatible
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/filecoin-project/go-address v1.1.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/ipfs/go-block-format v0.1.2 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
github.com/ipfs/go-ipld-cbor v0.0.6 // indirect
github.com/ipfs/go-ipld-format v0.5.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
)
Loading

0 comments on commit 19f7965

Please sign in to comment.