diff --git a/consts/consts.go b/consts/consts.go new file mode 100644 index 0000000..dd9f224 --- /dev/null +++ b/consts/consts.go @@ -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" +) diff --git a/models/models.go b/models/models.go index e61e011..37aa7d5 100644 --- a/models/models.go +++ b/models/models.go @@ -2,22 +2,6 @@ package models /// 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 RedundancyDataTypesConst interface { RepRedundancyData | ECRedundancyData @@ -33,22 +17,22 @@ func NewRedundancyRepData(fileHash string) RepRedundancyData { } type ECRedundancyData struct { - Ec EC `json:"ec"` + Ec EC `json:"ec"` Blocks []ObjectBlock `json:"blocks"` } func NewRedundancyEcData(ec EC, blocks []ObjectBlock) ECRedundancyData { return ECRedundancyData{ - Ec: ec, + Ec: ec, Blocks: blocks, } } 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 { @@ -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{ - ID: id, + ID: id, Name: name, - EcK: ecK, - EcN: ecN, + EcK: ecK, + EcN: ecN, } -} \ No newline at end of file +} diff --git a/pkgs/db/cache.go b/pkgs/db/cache.go index f518424..6f0bdd3 100644 --- a/pkgs/db/cache.go +++ b/pkgs/db/cache.go @@ -4,7 +4,7 @@ import ( "time" "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" ) diff --git a/pkgs/db/db.go b/pkgs/db/db.go index f1e582f..a09099a 100644 --- a/pkgs/db/db.go +++ b/pkgs/db/db.go @@ -9,7 +9,7 @@ import ( _ "github.com/go-sql-driver/mysql" "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/model" ) diff --git a/pkgs/db/object.go b/pkgs/db/object.go index e205a6b..ee2a172 100644 --- a/pkgs/db/object.go +++ b/pkgs/db/object.go @@ -7,8 +7,8 @@ import ( "github.com/jmoiron/sqlx" "github.com/samber/lo" - "gitlink.org.cn/cloudream/common/consts" "gitlink.org.cn/cloudream/common/models" + "gitlink.org.cn/cloudream/storage-common/consts" "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" ) diff --git a/pkgs/db/object_block.go b/pkgs/db/object_block.go index fa91643..e671cc0 100644 --- a/pkgs/db/object_block.go +++ b/pkgs/db/object_block.go @@ -4,7 +4,7 @@ import ( "database/sql" "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" ) diff --git a/pkgs/db/object_rep.go b/pkgs/db/object_rep.go index f637e59..6764a33 100644 --- a/pkgs/db/object_rep.go +++ b/pkgs/db/object_rep.go @@ -5,7 +5,7 @@ import ( "fmt" "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" ) diff --git a/pkgs/db/storage_object.go b/pkgs/db/storage_object.go index 728e6e1..7f6014c 100644 --- a/pkgs/db/storage_object.go +++ b/pkgs/db/storage_object.go @@ -4,7 +4,7 @@ import ( "fmt" "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" ) diff --git a/pkgs/mq/message/agent/storage.go b/pkgs/mq/message/agent/storage.go index 058cf29..5246970 100644 --- a/pkgs/mq/message/agent/storage.go +++ b/pkgs/mq/message/agent/storage.go @@ -1,8 +1,8 @@ package agent import ( - "gitlink.org.cn/cloudream/common/models" "gitlink.org.cn/cloudream/common/pkg/mq" + "gitlink.org.cn/cloudream/storage-common/models" "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" ) diff --git a/pkgs/mq/message/coordinator/storage.go b/pkgs/mq/message/coordinator/storage.go index f1824e8..881e17b 100644 --- a/pkgs/mq/message/coordinator/storage.go +++ b/pkgs/mq/message/coordinator/storage.go @@ -1,8 +1,8 @@ package coordinator import ( - "gitlink.org.cn/cloudream/common/models" "gitlink.org.cn/cloudream/common/pkg/mq" + "gitlink.org.cn/cloudream/storage-common/models" "gitlink.org.cn/cloudream/storage-common/pkgs/db/model" ) diff --git a/pkgs/mq/message/publics.go b/pkgs/mq/message/publics.go index 66be48e..cbc6fdd 100644 --- a/pkgs/mq/message/publics.go +++ b/pkgs/mq/message/publics.go @@ -4,6 +4,7 @@ import ( "gitlink.org.cn/cloudream/common/models" "gitlink.org.cn/cloudream/common/pkg/mq" myreflect "gitlink.org.cn/cloudream/common/utils/reflect" + mymodels "gitlink.org.cn/cloudream/storage-common/models" ) type Node struct { @@ -44,13 +45,13 @@ type RespRedundancyDataTypesConst interface { type RespRedundancyDataTypes interface{} type RespRepRedundancyData struct { - models.RepRedundancyData + mymodels.RepRedundancyData Nodes []RespNode `json:"nodes"` } func NewRespRepRedundancyData(fileHash string, nodes []RespNode) RespRepRedundancyData { return RespRepRedundancyData{ - RepRedundancyData: models.RepRedundancyData{ + RepRedundancyData: mymodels.RepRedundancyData{ FileHash: fileHash, }, Nodes: nodes, @@ -72,14 +73,14 @@ func NewRespEcRedundancyData(ec Ec, blocks []RespObjectBlock, nodes [][]RespNode } type RespObjectBlock struct { - models.ObjectBlock + mymodels.ObjectBlock //Node RespNode `json:"node"` } // func NewRespObjectBlock(index int, fileHash string, node RespNode) RespObjectBlock { func NewRespObjectBlock(index int, fileHash string) RespObjectBlock { return RespObjectBlock{ - ObjectBlock: models.ObjectBlock{ + ObjectBlock: mymodels.ObjectBlock{ Index: index, FileHash: fileHash, }, @@ -106,7 +107,7 @@ func NewEc(id int, name string, k int, n int) Ec { func init() { 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]()) } diff --git a/utils/config.go b/utils/config.go new file mode 100644 index 0000000..d69134e --- /dev/null +++ b/utils/config.go @@ -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 +} diff --git a/utils/ping.go b/utils/ping.go new file mode 100644 index 0000000..85532d3 --- /dev/null +++ b/utils/ping.go @@ -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 +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..1717250 --- /dev/null +++ b/utils/utils.go @@ -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] +}