| @@ -0,0 +1,26 @@ | |||||
| package consts | |||||
| const ( | |||||
| IPFSStateOK = "OK" | |||||
| StorageDirectoryStateOK = "OK" | |||||
| NodeStateNormal = "Normal" | |||||
| NodeStateUnavailable = "Unavailable" | |||||
| ) | |||||
| const ( | |||||
| ObjectStateNormal = "Normal" | |||||
| ObjectStateDeleted = "Deleted" | |||||
| ) | |||||
| const ( | |||||
| StorageObjectStateNormal = "Normal" | |||||
| StorageObjectStateDeleted = "Deleted" | |||||
| StorageObjectStateOutdated = "Outdated" | |||||
| ) | |||||
| const ( | |||||
| CacheStatePinned = "Pinned" | |||||
| CacheStateTemp = "Temp" | |||||
| ) | |||||
| @@ -2,22 +2,6 @@ package models | |||||
| /// TODO 将分散在各处的公共结构体定义集中到这里来 | /// TODO 将分散在各处的公共结构体定义集中到这里来 | ||||
| const ( | |||||
| RedundancyRep = "rep" | |||||
| RedundancyEC = "ec" | |||||
| ) | |||||
| type RedundancyConfigTypes interface{} | |||||
| type RedundancyConfigTypesConst interface { | |||||
| RepRedundancyConfig | ECRedundancyConfig | |||||
| } | |||||
| type RepRedundancyConfig struct { | |||||
| RepCount int `json:"repCount"` | |||||
| } | |||||
| type ECRedundancyConfig struct { | |||||
| } | |||||
| type RedundancyDataTypes interface{} | type RedundancyDataTypes interface{} | ||||
| type RedundancyDataTypesConst interface { | type RedundancyDataTypesConst interface { | ||||
| RepRedundancyData | ECRedundancyData | RepRedundancyData | ECRedundancyData | ||||
| @@ -33,22 +17,22 @@ func NewRedundancyRepData(fileHash string) RepRedundancyData { | |||||
| } | } | ||||
| type ECRedundancyData struct { | type ECRedundancyData struct { | ||||
| Ec EC `json:"ec"` | |||||
| Ec EC `json:"ec"` | |||||
| Blocks []ObjectBlock `json:"blocks"` | Blocks []ObjectBlock `json:"blocks"` | ||||
| } | } | ||||
| func NewRedundancyEcData(ec EC, blocks []ObjectBlock) ECRedundancyData { | func NewRedundancyEcData(ec EC, blocks []ObjectBlock) ECRedundancyData { | ||||
| return ECRedundancyData{ | return ECRedundancyData{ | ||||
| Ec: ec, | |||||
| Ec: ec, | |||||
| Blocks: blocks, | Blocks: blocks, | ||||
| } | } | ||||
| } | } | ||||
| type EC struct { | type EC struct { | ||||
| ID int `json:"id"` | |||||
| Name string `json:"name"` | |||||
| EcK int `json:"ecK"` | |||||
| EcN int `json:"ecN"` | |||||
| ID int `json:"id"` | |||||
| Name string `json:"name"` | |||||
| EcK int `json:"ecK"` | |||||
| EcN int `json:"ecN"` | |||||
| } | } | ||||
| type ObjectBlock struct { | type ObjectBlock struct { | ||||
| @@ -63,11 +47,11 @@ func NewObjectBlock(index int, fileHash string) ObjectBlock { | |||||
| } | } | ||||
| } | } | ||||
| func NewEc(id int, name string, ecK int, ecN int) EC{ | |||||
| func NewEc(id int, name string, ecK int, ecN int) EC { | |||||
| return EC{ | return EC{ | ||||
| ID: id, | |||||
| ID: id, | |||||
| Name: name, | Name: name, | ||||
| EcK: ecK, | |||||
| EcN: ecN, | |||||
| EcK: ecK, | |||||
| EcN: ecN, | |||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -4,7 +4,7 @@ import ( | |||||
| "time" | "time" | ||||
| "github.com/jmoiron/sqlx" | "github.com/jmoiron/sqlx" | ||||
| "gitlink.org.cn/cloudream/common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -9,7 +9,7 @@ import ( | |||||
| _ "github.com/go-sql-driver/mysql" | _ "github.com/go-sql-driver/mysql" | ||||
| "github.com/jmoiron/sqlx" | "github.com/jmoiron/sqlx" | ||||
| "gitlink.org.cn/cloudream/common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/config" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/config" | ||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -7,8 +7,8 @@ import ( | |||||
| "github.com/jmoiron/sqlx" | "github.com/jmoiron/sqlx" | ||||
| "github.com/samber/lo" | "github.com/samber/lo" | ||||
| "gitlink.org.cn/cloudream/common/consts" | |||||
| "gitlink.org.cn/cloudream/common/models" | "gitlink.org.cn/cloudream/common/models" | ||||
| "gitlink.org.cn/cloudream/storage-common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -4,7 +4,7 @@ import ( | |||||
| "database/sql" | "database/sql" | ||||
| "github.com/jmoiron/sqlx" | "github.com/jmoiron/sqlx" | ||||
| "gitlink.org.cn/cloudream/common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -5,7 +5,7 @@ import ( | |||||
| "fmt" | "fmt" | ||||
| "github.com/jmoiron/sqlx" | "github.com/jmoiron/sqlx" | ||||
| "gitlink.org.cn/cloudream/common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -4,7 +4,7 @@ import ( | |||||
| "fmt" | "fmt" | ||||
| "github.com/jmoiron/sqlx" | "github.com/jmoiron/sqlx" | ||||
| "gitlink.org.cn/cloudream/common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/consts" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -1,8 +1,8 @@ | |||||
| package agent | package agent | ||||
| import ( | import ( | ||||
| "gitlink.org.cn/cloudream/common/models" | |||||
| "gitlink.org.cn/cloudream/common/pkg/mq" | "gitlink.org.cn/cloudream/common/pkg/mq" | ||||
| "gitlink.org.cn/cloudream/storage-common/models" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -1,8 +1,8 @@ | |||||
| package coordinator | package coordinator | ||||
| import ( | import ( | ||||
| "gitlink.org.cn/cloudream/common/models" | |||||
| "gitlink.org.cn/cloudream/common/pkg/mq" | "gitlink.org.cn/cloudream/common/pkg/mq" | ||||
| "gitlink.org.cn/cloudream/storage-common/models" | |||||
| "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" | ||||
| ) | ) | ||||
| @@ -4,6 +4,7 @@ import ( | |||||
| "gitlink.org.cn/cloudream/common/models" | "gitlink.org.cn/cloudream/common/models" | ||||
| "gitlink.org.cn/cloudream/common/pkg/mq" | "gitlink.org.cn/cloudream/common/pkg/mq" | ||||
| myreflect "gitlink.org.cn/cloudream/common/utils/reflect" | myreflect "gitlink.org.cn/cloudream/common/utils/reflect" | ||||
| mymodels "gitlink.org.cn/cloudream/storage-common/models" | |||||
| ) | ) | ||||
| type Node struct { | type Node struct { | ||||
| @@ -44,13 +45,13 @@ type RespRedundancyDataTypesConst interface { | |||||
| type RespRedundancyDataTypes interface{} | type RespRedundancyDataTypes interface{} | ||||
| type RespRepRedundancyData struct { | type RespRepRedundancyData struct { | ||||
| models.RepRedundancyData | |||||
| mymodels.RepRedundancyData | |||||
| Nodes []RespNode `json:"nodes"` | Nodes []RespNode `json:"nodes"` | ||||
| } | } | ||||
| func NewRespRepRedundancyData(fileHash string, nodes []RespNode) RespRepRedundancyData { | func NewRespRepRedundancyData(fileHash string, nodes []RespNode) RespRepRedundancyData { | ||||
| return RespRepRedundancyData{ | return RespRepRedundancyData{ | ||||
| RepRedundancyData: models.RepRedundancyData{ | |||||
| RepRedundancyData: mymodels.RepRedundancyData{ | |||||
| FileHash: fileHash, | FileHash: fileHash, | ||||
| }, | }, | ||||
| Nodes: nodes, | Nodes: nodes, | ||||
| @@ -72,14 +73,14 @@ func NewRespEcRedundancyData(ec Ec, blocks []RespObjectBlock, nodes [][]RespNode | |||||
| } | } | ||||
| type RespObjectBlock struct { | type RespObjectBlock struct { | ||||
| models.ObjectBlock | |||||
| mymodels.ObjectBlock | |||||
| //Node RespNode `json:"node"` | //Node RespNode `json:"node"` | ||||
| } | } | ||||
| // func NewRespObjectBlock(index int, fileHash string, node RespNode) RespObjectBlock { | // func NewRespObjectBlock(index int, fileHash string, node RespNode) RespObjectBlock { | ||||
| func NewRespObjectBlock(index int, fileHash string) RespObjectBlock { | func NewRespObjectBlock(index int, fileHash string) RespObjectBlock { | ||||
| return RespObjectBlock{ | return RespObjectBlock{ | ||||
| ObjectBlock: models.ObjectBlock{ | |||||
| ObjectBlock: mymodels.ObjectBlock{ | |||||
| Index: index, | Index: index, | ||||
| FileHash: fileHash, | FileHash: fileHash, | ||||
| }, | }, | ||||
| @@ -106,7 +107,7 @@ func NewEc(id int, name string, k int, n int) Ec { | |||||
| func init() { | func init() { | ||||
| mq.RegisterTypeSet[models.RedundancyConfigTypes](myreflect.TypeOf[models.RepRedundancyConfig](), myreflect.TypeOf[models.ECRedundancyConfig]()) | mq.RegisterTypeSet[models.RedundancyConfigTypes](myreflect.TypeOf[models.RepRedundancyConfig](), myreflect.TypeOf[models.ECRedundancyConfig]()) | ||||
| mq.RegisterTypeSet[models.RedundancyDataTypes](myreflect.TypeOf[models.RepRedundancyData](), myreflect.TypeOf[models.ECRedundancyData]()) | |||||
| mq.RegisterTypeSet[mymodels.RedundancyDataTypes](myreflect.TypeOf[mymodels.RepRedundancyData](), myreflect.TypeOf[mymodels.ECRedundancyData]()) | |||||
| mq.RegisterTypeSet[RespRedundancyDataTypes](myreflect.TypeOf[RespRepRedundancyData](), myreflect.TypeOf[RespEcRedundancyData]()) | mq.RegisterTypeSet[RespRedundancyDataTypes](myreflect.TypeOf[RespRepRedundancyData](), myreflect.TypeOf[RespEcRedundancyData]()) | ||||
| } | } | ||||
| @@ -0,0 +1,74 @@ | |||||
| package utils | |||||
| import ( | |||||
| "fmt" | |||||
| "regexp" | |||||
| "strconv" | |||||
| "github.com/beevik/etree" | |||||
| ) | |||||
| type EcConfig struct { | |||||
| ecid string `xml:"ecid"` | |||||
| class string `xml:"class"` | |||||
| n int `xml:"n"` | |||||
| k int `xml:"k"` | |||||
| w int `xml:"w"` | |||||
| opt int `xml:"opt"` | |||||
| } | |||||
| func (r *EcConfig) GetK() int { | |||||
| return r.k | |||||
| } | |||||
| func (r *EcConfig) GetN() int { | |||||
| return r.n | |||||
| } | |||||
| func GetEcPolicy() *map[string]EcConfig { | |||||
| doc := etree.NewDocument() | |||||
| if err := doc.ReadFromFile("../conf/sysSetting.xml"); err != nil { | |||||
| panic(err) | |||||
| } | |||||
| ecMap := make(map[string]EcConfig, 20) | |||||
| root := doc.SelectElement("setting") | |||||
| for _, attr := range root.SelectElements("attribute") { | |||||
| if name := attr.SelectElement("name"); name.Text() == "ec.policy" { | |||||
| for _, eci := range attr.SelectElements("value") { | |||||
| tt := EcConfig{} | |||||
| tt.ecid = eci.SelectElement("ecid").Text() | |||||
| tt.class = eci.SelectElement("class").Text() | |||||
| tt.n, _ = strconv.Atoi(eci.SelectElement("n").Text()) | |||||
| tt.k, _ = strconv.Atoi(eci.SelectElement("k").Text()) | |||||
| tt.w, _ = strconv.Atoi(eci.SelectElement("w").Text()) | |||||
| tt.opt, _ = strconv.Atoi(eci.SelectElement("opt").Text()) | |||||
| ecMap[tt.ecid] = tt | |||||
| } | |||||
| } | |||||
| } | |||||
| fmt.Println(ecMap) | |||||
| return &ecMap | |||||
| // | |||||
| } | |||||
| func GetAgentIps() []string { | |||||
| doc := etree.NewDocument() | |||||
| if err := doc.ReadFromFile("../conf/sysSetting.xml"); err != nil { | |||||
| panic(err) | |||||
| } | |||||
| root := doc.SelectElement("setting") | |||||
| var ips []string // 定义存储 IP 的字符串切片 | |||||
| for _, attr := range root.SelectElements("attribute") { | |||||
| if name := attr.SelectElement("name"); name.Text() == "agents.addr" { | |||||
| for _, ip := range attr.SelectElements("value") { | |||||
| ipRegex := regexp.MustCompile(`\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b`) | |||||
| match := ipRegex.FindString(ip.Text()) | |||||
| print(match) | |||||
| ips = append(ips, match) | |||||
| } | |||||
| } | |||||
| } | |||||
| return ips | |||||
| } | |||||
| @@ -0,0 +1,82 @@ | |||||
| package utils | |||||
| import ( | |||||
| //"fmt" | |||||
| "github.com/go-ping/ping" | |||||
| //"net" | |||||
| "io/ioutil" | |||||
| "net/http" | |||||
| "strings" | |||||
| "time" | |||||
| ) | |||||
| type ConnStatus struct { | |||||
| Addr string | |||||
| IsReachable bool | |||||
| Delay time.Duration | |||||
| TTL int | |||||
| } | |||||
| // 获取本地主机 IP 地址 | |||||
| func getLocalIP() string { | |||||
| resp, err := http.Get("https://api.ipify.org") | |||||
| if err != nil { | |||||
| panic(err) | |||||
| } | |||||
| defer resp.Body.Close() | |||||
| body, err := ioutil.ReadAll(resp.Body) | |||||
| if err != nil { | |||||
| panic(err) | |||||
| } | |||||
| ip := strings.TrimSpace(string(body)) | |||||
| return ip | |||||
| } | |||||
| func GetConnStatus(remoteIP string) (*ConnStatus, error) { | |||||
| // 本地主机 IP 地址 | |||||
| //localIP := getLocalIP() | |||||
| //print("!@#@#!") | |||||
| //print(localIP) | |||||
| conn := ConnStatus{ | |||||
| Addr: remoteIP, | |||||
| IsReachable: false, | |||||
| } | |||||
| pinger, err := ping.NewPinger(remoteIP) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| pinger.Count = 5 // 设置 ping 次数为 5 | |||||
| // pinger.Interval = 1 // 设置 ping 时间间隔为 1 秒 | |||||
| //pinger.Timeout = 2 // 设置 ping 超时时间为 2 秒 | |||||
| //pinger.SetPrivileged(true) // 设置使用特权模式以获取 TTL 值 | |||||
| pinger.OnRecv = func(pkt *ping.Packet) { | |||||
| //fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n", | |||||
| // pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl) | |||||
| conn.TTL = pkt.Ttl | |||||
| } | |||||
| /*pinger.OnDuplicateRecv = func(pkt *ping.Packet) { | |||||
| fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n", | |||||
| pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl) | |||||
| }*/ | |||||
| pinger.OnFinish = func(stats *ping.Statistics) { | |||||
| //fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr) | |||||
| //fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n", | |||||
| // stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss) | |||||
| //fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", | |||||
| // stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt) | |||||
| if stats.PacketLoss == 0.0 { | |||||
| conn.IsReachable = true | |||||
| } | |||||
| conn.Delay = stats.AvgRtt | |||||
| } | |||||
| err = pinger.Run() // Blocks until finished. | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| return &conn, nil | |||||
| } | |||||
| @@ -0,0 +1,21 @@ | |||||
| package utils | |||||
| import ( | |||||
| "fmt" | |||||
| "strings" | |||||
| ) | |||||
| // MakeMoveOperationFileName Move操作时,写入的文件的名称 | |||||
| func MakeMoveOperationFileName(objectID int64, userID int64) string { | |||||
| return fmt.Sprintf("%d-%d", objectID, userID) | |||||
| } | |||||
| // GetDirectoryName 根据objectName获取所属的文件夹名 | |||||
| func GetDirectoryName(objectName string) string { | |||||
| parts := strings.Split(objectName, "/") | |||||
| //若为文件,dirName设置为空 | |||||
| if len(parts) == 1 { | |||||
| return "" | |||||
| } | |||||
| return parts[0] | |||||
| } | |||||