Browse Source

拆分common库

gitlink
Sydonian 2 years ago
parent
commit
bd58f2c899
14 changed files with 228 additions and 40 deletions
  1. +26
    -0
      consts/consts.go
  2. +11
    -27
      models/models.go
  3. +1
    -1
      pkgs/db/cache.go
  4. +1
    -1
      pkgs/db/db.go
  5. +1
    -1
      pkgs/db/object.go
  6. +1
    -1
      pkgs/db/object_block.go
  7. +1
    -1
      pkgs/db/object_rep.go
  8. +1
    -1
      pkgs/db/storage_object.go
  9. +1
    -1
      pkgs/mq/message/agent/storage.go
  10. +1
    -1
      pkgs/mq/message/coordinator/storage.go
  11. +6
    -5
      pkgs/mq/message/publics.go
  12. +74
    -0
      utils/config.go
  13. +82
    -0
      utils/ping.go
  14. +21
    -0
      utils/utils.go

+ 26
- 0
consts/consts.go View File

@@ -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"
)

+ 11
- 27
models/models.go View File

@@ -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,
}
}
}

+ 1
- 1
pkgs/db/cache.go View File

@@ -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"
)



+ 1
- 1
pkgs/db/db.go View File

@@ -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"
)


+ 1
- 1
pkgs/db/object.go View File

@@ -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"
)



+ 1
- 1
pkgs/db/object_block.go View File

@@ -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"
)



+ 1
- 1
pkgs/db/object_rep.go View File

@@ -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"
)



+ 1
- 1
pkgs/db/storage_object.go View File

@@ -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"
)



+ 1
- 1
pkgs/mq/message/agent/storage.go View File

@@ -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"
)



+ 1
- 1
pkgs/mq/message/coordinator/storage.go View File

@@ -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"
)



+ 6
- 5
pkgs/mq/message/publics.go View File

@@ -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]())
}

+ 74
- 0
utils/config.go View File

@@ -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
}

+ 82
- 0
utils/ping.go View File

@@ -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
}

+ 21
- 0
utils/utils.go View File

@@ -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]
}

Loading…
Cancel
Save