You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

base_store.go 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package ops2
  2. import (
  3. "fmt"
  4. "io"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. "gitlink.org.cn/cloudream/common/utils/io2"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/dag"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
  11. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch2"
  12. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool"
  13. stgtypes "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
  14. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  15. )
  16. const (
  17. BaseReadStatsStoreKey = "Stats.BaseRead"
  18. )
  19. func init() {
  20. exec.UseOp[*BaseWrite]()
  21. exec.UseOp[*BaseRead]()
  22. exec.UseOp[*BaseReadDyn]()
  23. exec.UseVarValue[*BaseReadStatsValue]()
  24. }
  25. type BaseReadStatsValue struct {
  26. Length int64
  27. ElapsedTime time.Duration
  28. Location exec.Location
  29. }
  30. func (v *BaseReadStatsValue) Clone() exec.VarValue {
  31. return &BaseReadStatsValue{
  32. Length: v.Length,
  33. ElapsedTime: v.ElapsedTime,
  34. Location: v.Location,
  35. }
  36. }
  37. type BaseRead struct {
  38. Output exec.VarID
  39. UserSpace jcstypes.UserSpaceDetail
  40. Path jcstypes.JPath
  41. Option stgtypes.OpenOption
  42. }
  43. func (o *BaseRead) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  44. logger.
  45. WithField("Output", o.Output).
  46. WithField("UserSpace", o.UserSpace).
  47. WithField("Path", o.Path).
  48. Debug("base read")
  49. defer logger.Debug("base read end")
  50. stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
  51. if err != nil {
  52. return fmt.Errorf("getting storage pool: %w", err)
  53. }
  54. store, err := stgPool.GetBaseStore(&o.UserSpace)
  55. if err != nil {
  56. return fmt.Errorf("getting base store of storage %v: %w", o.UserSpace, err)
  57. }
  58. stream, err := store.Read(o.Path, o.Option)
  59. if err != nil {
  60. return fmt.Errorf("reading object %v: %w", o.Path, err)
  61. }
  62. startTime := time.Now()
  63. counter := io2.CounterCloser(stream, nil)
  64. fut := future.NewSetVoid()
  65. output := &exec.StreamValue{
  66. Stream: io2.AfterReadClosed(counter, func(closer io.ReadCloser) {
  67. fut.SetVoid()
  68. }),
  69. }
  70. e.PutVar(o.Output, output)
  71. err = fut.Wait(ctx.Context)
  72. e.Store(BaseReadStatsStoreKey, &BaseReadStatsValue{
  73. Length: counter.Count(),
  74. ElapsedTime: time.Since(startTime),
  75. Location: e.Location(),
  76. })
  77. return err
  78. }
  79. func (o *BaseRead) String() string {
  80. return fmt.Sprintf("BaseRead(opt=%v) %v:%v -> %v", o.Option, o.UserSpace, o.Path, o.Output)
  81. }
  82. type BaseReadDyn struct {
  83. UserSpace jcstypes.UserSpaceDetail
  84. Output exec.VarID
  85. FileInfo exec.VarID
  86. Option stgtypes.OpenOption
  87. }
  88. func (o *BaseReadDyn) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  89. logger.
  90. WithField("Output", o.Output).
  91. WithField("UserSpace", o.UserSpace).
  92. WithField("Path", o.FileInfo).
  93. Debug("base read dynamic")
  94. defer logger.Debug("base read dynamic end")
  95. stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
  96. if err != nil {
  97. return fmt.Errorf("getting storage pool: %w", err)
  98. }
  99. info, err := exec.BindVar[*FileInfoValue](e, ctx.Context, o.FileInfo)
  100. if err != nil {
  101. return err
  102. }
  103. store, err := stgPool.GetBaseStore(&o.UserSpace)
  104. if err != nil {
  105. return fmt.Errorf("getting base store of storage %v: %w", o.UserSpace, err)
  106. }
  107. stream, err := store.Read(info.Path, o.Option)
  108. if err != nil {
  109. logger.Warnf("reading file %v: %v", info.Path, err)
  110. return fmt.Errorf("reading object %v: %w", o.FileInfo, err)
  111. }
  112. startTime := time.Now()
  113. counter := io2.CounterCloser(stream, nil)
  114. fut := future.NewSetVoid()
  115. output := &exec.StreamValue{
  116. Stream: io2.AfterReadClosed(counter, func(closer io.ReadCloser) {
  117. fut.SetVoid()
  118. }),
  119. }
  120. e.PutVar(o.Output, output)
  121. err = fut.Wait(ctx.Context)
  122. e.Store(BaseReadStatsStoreKey, &BaseReadStatsValue{
  123. Length: counter.Count(),
  124. ElapsedTime: time.Since(startTime),
  125. Location: e.Location(),
  126. })
  127. return err
  128. }
  129. func (o *BaseReadDyn) String() string {
  130. return fmt.Sprintf("BaseReadDyn(opt=%v) %v:%v -> %v", o.Option, o.UserSpace, o.FileInfo, o.Output)
  131. }
  132. type BaseWrite struct {
  133. Input exec.VarID
  134. UserSpace jcstypes.UserSpaceDetail
  135. Path jcstypes.JPath
  136. FileInfo exec.VarID
  137. Option stgtypes.WriteOption
  138. }
  139. func (o *BaseWrite) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  140. logger.
  141. WithField("Input", o.Input).
  142. Debugf("write file to base store")
  143. defer logger.Debugf("write file to base store finished")
  144. stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
  145. if err != nil {
  146. return fmt.Errorf("getting storage pool: %w", err)
  147. }
  148. store, err := stgPool.GetBaseStore(&o.UserSpace)
  149. if err != nil {
  150. return fmt.Errorf("getting base store of storage %v: %w", o.UserSpace, err)
  151. }
  152. input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
  153. if err != nil {
  154. return err
  155. }
  156. defer input.Stream.Close()
  157. ret, err := store.Write(o.Path, input.Stream, o.Option)
  158. if err != nil {
  159. return err
  160. }
  161. e.PutVar(o.FileInfo, &FileInfoValue{
  162. FileInfo: ret,
  163. })
  164. return nil
  165. }
  166. func (o *BaseWrite) String() string {
  167. return fmt.Sprintf("BaseWrite %v -> %v:%v, %v", o.Input, o.UserSpace, o.Path, o.FileInfo)
  168. }
  169. type BaseReadNode struct {
  170. dag.NodeBase
  171. From ioswitch2.From
  172. UserSpace jcstypes.UserSpaceDetail
  173. Path jcstypes.JPath
  174. Option stgtypes.OpenOption
  175. }
  176. func (b *GraphNodeBuilder) NewBaseRead(from ioswitch2.From, userSpace jcstypes.UserSpaceDetail, path jcstypes.JPath, opt stgtypes.OpenOption) *BaseReadNode {
  177. node := &BaseReadNode{
  178. From: from,
  179. UserSpace: userSpace,
  180. Path: path,
  181. Option: opt,
  182. }
  183. b.AddNode(node)
  184. node.OutputStreams().Init(node, 1)
  185. return node
  186. }
  187. func (t *BaseReadNode) GetFrom() ioswitch2.From {
  188. return t.From
  189. }
  190. func (t *BaseReadNode) Output() dag.StreamOutputSlot {
  191. return dag.StreamOutputSlot{
  192. Node: t,
  193. Index: 0,
  194. }
  195. }
  196. func (t *BaseReadNode) GenerateOp() (exec.Op, error) {
  197. return &BaseRead{
  198. Output: t.Output().Var().VarID,
  199. UserSpace: t.UserSpace,
  200. Path: t.Path,
  201. Option: t.Option,
  202. }, nil
  203. }
  204. type BaseReadDynNode struct {
  205. dag.NodeBase
  206. From ioswitch2.From
  207. UserSpace jcstypes.UserSpaceDetail
  208. Option stgtypes.OpenOption
  209. }
  210. func (b *GraphNodeBuilder) NewBaseReadDyn(from ioswitch2.From, userSpace jcstypes.UserSpaceDetail, opt stgtypes.OpenOption) *BaseReadDynNode {
  211. node := &BaseReadDynNode{
  212. From: from,
  213. UserSpace: userSpace,
  214. Option: opt,
  215. }
  216. b.AddNode(node)
  217. node.OutputStreams().Init(node, 1)
  218. node.InputValues().Init(1)
  219. return node
  220. }
  221. func (t *BaseReadDynNode) GetFrom() ioswitch2.From {
  222. return t.From
  223. }
  224. func (t *BaseReadDynNode) FileInfoSlot() dag.ValueInputSlot {
  225. return dag.ValueInputSlot{
  226. Node: t,
  227. Index: 0,
  228. }
  229. }
  230. func (t *BaseReadDynNode) Output() dag.StreamOutputSlot {
  231. return dag.StreamOutputSlot{
  232. Node: t,
  233. Index: 0,
  234. }
  235. }
  236. func (t *BaseReadDynNode) GenerateOp() (exec.Op, error) {
  237. return &BaseReadDyn{
  238. UserSpace: t.UserSpace,
  239. Output: t.Output().Var().VarID,
  240. FileInfo: t.FileInfoSlot().Var().VarID,
  241. Option: t.Option,
  242. }, nil
  243. }
  244. type BaseWriteNode struct {
  245. dag.NodeBase
  246. To ioswitch2.To
  247. UserSpace jcstypes.UserSpaceDetail
  248. Path jcstypes.JPath
  249. Option stgtypes.WriteOption
  250. }
  251. func (b *GraphNodeBuilder) NewBaseWrite(to ioswitch2.To, userSpace jcstypes.UserSpaceDetail, path jcstypes.JPath, opt stgtypes.WriteOption) *BaseWriteNode {
  252. node := &BaseWriteNode{
  253. To: to,
  254. UserSpace: userSpace,
  255. Path: path,
  256. Option: opt,
  257. }
  258. b.AddNode(node)
  259. node.InputStreams().Init(1)
  260. node.OutputValues().Init(node, 1)
  261. return node
  262. }
  263. func (t *BaseWriteNode) GetTo() ioswitch2.To {
  264. return t.To
  265. }
  266. func (t *BaseWriteNode) Input() dag.StreamInputSlot {
  267. return dag.StreamInputSlot{
  268. Node: t,
  269. Index: 0,
  270. }
  271. }
  272. func (t *BaseWriteNode) FileInfoVar() dag.ValueOutputSlot {
  273. return dag.ValueOutputSlot{
  274. Node: t,
  275. Index: 0,
  276. }
  277. }
  278. func (t *BaseWriteNode) GenerateOp() (exec.Op, error) {
  279. return &BaseWrite{
  280. Input: t.InputStreams().Get(0).VarID,
  281. UserSpace: t.UserSpace,
  282. Path: t.Path,
  283. FileInfo: t.FileInfoVar().Var().VarID,
  284. Option: t.Option,
  285. }, nil
  286. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。