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.

bypass.go 6.8 kB

11 months ago
11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package ops2
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  5. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  6. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/agtpool"
  8. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/types"
  9. )
  10. func init() {
  11. exec.UseOp[*BypassToShardStore]()
  12. exec.UseVarValue[*BypassUploadedFileValue]()
  13. exec.UseVarValue[*BypassHandleResultValue]()
  14. exec.UseOp[*BypassFromShardStore]()
  15. exec.UseVarValue[*BypassFilePathValue]()
  16. exec.UseOp[*BypassFromShardStoreHTTP]()
  17. exec.UseVarValue[*HTTPRequestValue]()
  18. }
  19. type BypassUploadedFileValue struct {
  20. types.BypassUploadedFile
  21. }
  22. func (v *BypassUploadedFileValue) Clone() exec.VarValue {
  23. return &BypassUploadedFileValue{
  24. BypassUploadedFile: v.BypassUploadedFile,
  25. }
  26. }
  27. type BypassHandleResultValue struct {
  28. Commited bool
  29. }
  30. func (r *BypassHandleResultValue) Clone() exec.VarValue {
  31. return &BypassHandleResultValue{
  32. Commited: r.Commited,
  33. }
  34. }
  35. type BypassToShardStore struct {
  36. StorageID cdssdk.StorageID
  37. BypassFileInfo exec.VarID
  38. BypassCallback exec.VarID
  39. FileHash exec.VarID
  40. }
  41. func (o *BypassToShardStore) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  42. stgAgts, err := exec.GetValueByType[*agtpool.AgentPool](ctx)
  43. if err != nil {
  44. return err
  45. }
  46. shardStore, err := stgAgts.GetShardStore(o.StorageID)
  47. if err != nil {
  48. return err
  49. }
  50. br, ok := shardStore.(types.BypassWrite)
  51. if !ok {
  52. return fmt.Errorf("shard store %v not support bypass write", o.StorageID)
  53. }
  54. fileInfo, err := exec.BindVar[*BypassUploadedFileValue](e, ctx.Context, o.BypassFileInfo)
  55. if err != nil {
  56. return err
  57. }
  58. err = br.BypassUploaded(fileInfo.BypassUploadedFile)
  59. if err != nil {
  60. return err
  61. }
  62. e.PutVar(o.BypassCallback, &BypassHandleResultValue{Commited: true})
  63. e.PutVar(o.FileHash, &FileHashValue{Hash: fileInfo.Hash})
  64. return nil
  65. }
  66. func (o *BypassToShardStore) String() string {
  67. return fmt.Sprintf("BypassToShardStore[StorageID:%v] Info: %v, Callback: %v", o.StorageID, o.BypassFileInfo, o.BypassCallback)
  68. }
  69. type BypassFilePathValue struct {
  70. types.BypassFilePath
  71. }
  72. func (v *BypassFilePathValue) Clone() exec.VarValue {
  73. return &BypassFilePathValue{
  74. BypassFilePath: v.BypassFilePath,
  75. }
  76. }
  77. type BypassFromShardStore struct {
  78. StorageID cdssdk.StorageID
  79. FileHash cdssdk.FileHash
  80. Output exec.VarID
  81. }
  82. func (o *BypassFromShardStore) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  83. stgAgts, err := exec.GetValueByType[*agtpool.AgentPool](ctx)
  84. if err != nil {
  85. return err
  86. }
  87. shardStore, err := stgAgts.GetShardStore(o.StorageID)
  88. if err != nil {
  89. return err
  90. }
  91. br, ok := shardStore.(types.BypassRead)
  92. if !ok {
  93. return fmt.Errorf("shard store %v not support bypass read", o.StorageID)
  94. }
  95. path, err := br.BypassRead(o.FileHash)
  96. if err != nil {
  97. return err
  98. }
  99. e.PutVar(o.Output, &BypassFilePathValue{BypassFilePath: path})
  100. return nil
  101. }
  102. func (o *BypassFromShardStore) String() string {
  103. return fmt.Sprintf("BypassFromShardStore[StorageID:%v] FileHash: %v, Output: %v", o.StorageID, o.FileHash, o.Output)
  104. }
  105. // 旁路Http读取
  106. type BypassFromShardStoreHTTP struct {
  107. StorageID cdssdk.StorageID
  108. FileHash cdssdk.FileHash
  109. Output exec.VarID
  110. }
  111. type HTTPRequestValue struct {
  112. types.HTTPRequest
  113. }
  114. func (v *HTTPRequestValue) Clone() exec.VarValue {
  115. return &HTTPRequestValue{
  116. HTTPRequest: v.HTTPRequest,
  117. }
  118. }
  119. func (o *BypassFromShardStoreHTTP) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  120. stgAgts, err := exec.GetValueByType[*agtpool.AgentPool](ctx)
  121. if err != nil {
  122. return err
  123. }
  124. shardStore, err := stgAgts.GetShardStore(o.StorageID)
  125. if err != nil {
  126. return err
  127. }
  128. br, ok := shardStore.(types.HTTPBypassRead)
  129. if !ok {
  130. return fmt.Errorf("shard store %v not support bypass read", o.StorageID)
  131. }
  132. req, err := br.HTTPBypassRead(o.FileHash)
  133. if err != nil {
  134. return err
  135. }
  136. e.PutVar(o.Output, &HTTPRequestValue{HTTPRequest: req})
  137. return nil
  138. }
  139. func (o *BypassFromShardStoreHTTP) String() string {
  140. return fmt.Sprintf("BypassFromShardStoreHTTP[StorageID:%v] FileHash: %v, Output: %v", o.StorageID, o.FileHash, o.Output)
  141. }
  142. // 旁路写入
  143. type BypassToShardStoreNode struct {
  144. dag.NodeBase
  145. StorageID cdssdk.StorageID
  146. FileHashStoreKey string
  147. }
  148. func (b *GraphNodeBuilder) NewBypassToShardStore(storageID cdssdk.StorageID, fileHashStoreKey string) *BypassToShardStoreNode {
  149. node := &BypassToShardStoreNode{
  150. StorageID: storageID,
  151. FileHashStoreKey: fileHashStoreKey,
  152. }
  153. b.AddNode(node)
  154. node.InputValues().Init(1)
  155. node.OutputValues().Init(node, 2)
  156. return node
  157. }
  158. func (n *BypassToShardStoreNode) BypassFileInfoSlot() dag.ValueInputSlot {
  159. return dag.ValueInputSlot{
  160. Node: n,
  161. Index: 0,
  162. }
  163. }
  164. func (n *BypassToShardStoreNode) BypassCallbackVar() dag.ValueOutputSlot {
  165. return dag.ValueOutputSlot{
  166. Node: n,
  167. Index: 0,
  168. }
  169. }
  170. func (n *BypassToShardStoreNode) FileHashVar() dag.ValueOutputSlot {
  171. return dag.ValueOutputSlot{
  172. Node: n,
  173. Index: 1,
  174. }
  175. }
  176. func (t *BypassToShardStoreNode) GenerateOp() (exec.Op, error) {
  177. return &BypassToShardStore{
  178. StorageID: t.StorageID,
  179. BypassFileInfo: t.BypassFileInfoSlot().Var().VarID,
  180. BypassCallback: t.BypassCallbackVar().Var().VarID,
  181. FileHash: t.FileHashVar().Var().VarID,
  182. }, nil
  183. }
  184. // 旁路读取
  185. type BypassFromShardStoreNode struct {
  186. dag.NodeBase
  187. StorageID cdssdk.StorageID
  188. FileHash cdssdk.FileHash
  189. }
  190. func (b *GraphNodeBuilder) NewBypassFromShardStore(storageID cdssdk.StorageID, fileHash cdssdk.FileHash) *BypassFromShardStoreNode {
  191. node := &BypassFromShardStoreNode{
  192. StorageID: storageID,
  193. FileHash: fileHash,
  194. }
  195. b.AddNode(node)
  196. node.OutputValues().Init(node, 1)
  197. return node
  198. }
  199. func (n *BypassFromShardStoreNode) FilePathVar() dag.ValueOutputSlot {
  200. return dag.ValueOutputSlot{
  201. Node: n,
  202. Index: 0,
  203. }
  204. }
  205. func (n *BypassFromShardStoreNode) GenerateOp() (exec.Op, error) {
  206. return &BypassFromShardStore{
  207. StorageID: n.StorageID,
  208. FileHash: n.FileHash,
  209. Output: n.FilePathVar().Var().VarID,
  210. }, nil
  211. }
  212. // 旁路Http读取
  213. type BypassFromShardStoreHTTPNode struct {
  214. dag.NodeBase
  215. StorageID cdssdk.StorageID
  216. FileHash cdssdk.FileHash
  217. }
  218. func (b *GraphNodeBuilder) NewBypassFromShardStoreHTTP(storageID cdssdk.StorageID, fileHash cdssdk.FileHash) *BypassFromShardStoreHTTPNode {
  219. node := &BypassFromShardStoreHTTPNode{
  220. StorageID: storageID,
  221. FileHash: fileHash,
  222. }
  223. b.AddNode(node)
  224. node.OutputValues().Init(node, 1)
  225. return node
  226. }
  227. func (n *BypassFromShardStoreHTTPNode) HTTPRequestVar() dag.ValueOutputSlot {
  228. return dag.ValueOutputSlot{
  229. Node: n,
  230. Index: 0,
  231. }
  232. }
  233. func (n *BypassFromShardStoreHTTPNode) GenerateOp() (exec.Op, error) {
  234. return &BypassFromShardStoreHTTP{
  235. StorageID: n.StorageID,
  236. FileHash: n.FileHash,
  237. Output: n.HTTPRequestVar().Var().VarID,
  238. }, nil
  239. }

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