Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 095c86a

Browse files
authored
Merge pull request #1021 from Starnop/supernode-priority
feature: add weight for node config
2 parents e51ade6 + dd99a67 commit 095c86a

11 files changed

+498
-54
lines changed

cmd/dfget/app/root.go

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ func init() {
7272
// runDfget does some init operations and starts to download.
7373
func runDfget() error {
7474
// get config from property files
75-
propResults := initProperties()
75+
propResults, err := initProperties()
76+
if err != nil {
77+
return err
78+
}
7679

7780
// initialize logger
7881
if err := initClientLog(); err != nil {
@@ -91,10 +94,6 @@ func runDfget() error {
9194

9295
cfg.Filter = transFilter(filter)
9396

94-
if err := handleNodes(); err != nil {
95-
return err
96-
}
97-
9897
if err := checkParameters(); err != nil {
9998
return err
10099
}
@@ -106,10 +105,10 @@ func runDfget() error {
106105
logrus.Infof("get init config:%v", cfg)
107106

108107
// enter the core process
109-
err := core.Start(cfg)
110-
printer.Println(resultMsg(cfg, time.Now(), err))
111-
if err != nil {
112-
os.Exit(err.Code)
108+
dfError := core.Start(cfg)
109+
printer.Println(resultMsg(cfg, time.Now(), dfError))
110+
if dfError != nil {
111+
os.Exit(dfError.Code)
113112
}
114113
return nil
115114
}
@@ -122,12 +121,12 @@ func checkParameters() error {
122121
}
123122

124123
// initProperties loads config from property files.
125-
func initProperties() []*propertiesResult {
124+
func initProperties() ([]*propertiesResult, error) {
126125
var results []*propertiesResult
127126
properties := config.NewProperties()
128127
for _, v := range cfg.ConfigFiles {
129-
var err error
130-
if err = properties.Load(v); err == nil {
128+
err := properties.Load(v)
129+
if err == nil {
131130
break
132131
}
133132
results = append(results, &propertiesResult{
@@ -137,8 +136,12 @@ func initProperties() []*propertiesResult {
137136
})
138137
}
139138

140-
if cfg.Nodes == nil {
141-
cfg.Nodes = properties.Nodes
139+
supernodes := cfg.Supernodes
140+
if supernodes == nil {
141+
supernodes = properties.Supernodes
142+
}
143+
if supernodes != nil {
144+
cfg.Nodes = config.NodeWightSlice2StringSlice(supernodes)
142145
}
143146

144147
if cfg.LocalLimit == 0 {
@@ -173,7 +176,7 @@ func initProperties() []*propertiesResult {
173176
cfg.RV.SystemDataDir = path.Join(cfg.WorkHome, "data")
174177
cfg.RV.FileLength = -1
175178

176-
return results
179+
return results, nil
177180
}
178181

179182
// initClientLog initializes dfget client's logger.
@@ -234,8 +237,8 @@ func initFlags() {
234237
"\nin this way, different but actually the same URLs can reuse the same downloading task")
235238
flagSet.StringSliceVar(&cfg.Header, "header", nil,
236239
"http header, eg: --header='Accept: *' --header='Host: abc'")
237-
flagSet.StringSliceVarP(&cfg.Nodes, "node", "n", nil,
238-
"specify the addresses(host:port) of supernodes")
240+
flagSet.VarP(config.NewSupernodesValue(&cfg.Supernodes, nil), "node", "n",
241+
"specify the addresses(host:port=weight) of supernodes where the host is necessary, the port(default: 8002) and the weight(default:1) are optional. And the type of weight must be integer")
239242
flagSet.BoolVar(&cfg.Notbs, "notbs", false,
240243
"disable back source downloading for requested file when p2p fails to download it")
241244
flagSet.BoolVar(&cfg.DFDaemon, "dfdaemon", false,
@@ -275,21 +278,6 @@ func transFilter(filter string) []string {
275278
return strings.Split(filter, "&")
276279
}
277280

278-
func handleNodes() error {
279-
nodes := make([]string, 0)
280-
281-
for _, v := range cfg.Nodes {
282-
// TODO: check the validity of v.
283-
if strings.IndexByte(v, ':') > 0 {
284-
nodes = append(nodes, v)
285-
continue
286-
}
287-
nodes = append(nodes, fmt.Sprintf("%s:%d", v, config.DefaultSupernodePort))
288-
}
289-
cfg.Nodes = nodes
290-
return nil
291-
}
292-
293281
func resultMsg(cfg *config.Config, end time.Time, e *errortypes.DfError) string {
294282
if e != nil {
295283
return fmt.Sprintf("download FAIL(%d) cost:%.3fs length:%d reason:%d error:%v",

cmd/dfget/app/root_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type dfgetSuit struct {
3737

3838
func (suit *dfgetSuit) Test_initFlagsNoArguments() {
3939
initProperties()
40-
suit.Equal(cfg.Nodes, []string{"127.0.0.1"})
40+
suit.Equal(cfg.Nodes, []string{"127.0.0.1:8002"})
4141
suit.Equal(cfg.LocalLimit, 20*rate.MB)
4242
suit.Equal(cfg.TotalLimit, 20*rate.MB)
4343
suit.Equal(cfg.Notbs, false)
@@ -69,11 +69,11 @@ func (suit *dfgetSuit) Test_initProperties() {
6969
{configs: nil,
7070
expected: config.NewProperties()},
7171
{configs: []string{iniFile, yamlFile},
72-
expected: newProp(0, 0, 0, "1.1.1.1")},
72+
expected: newProp(0, 0, 0, "1.1.1.1:8002")},
7373
{configs: []string{yamlFile, iniFile},
74-
expected: newProp(int(rate.KB*1000), int(rate.KB*1000), 0, "1.1.1.2")},
74+
expected: newProp(int(rate.KB*1000), int(rate.KB*1000), 0, "1.1.1.2:8002")},
7575
{configs: []string{filepath.Join(dirName, "x"), yamlFile},
76-
expected: newProp(int(rate.KB*1000), int(rate.KB*1000), 0, "1.1.1.2")},
76+
expected: newProp(int(rate.KB*1000), int(rate.KB*1000), 0, "1.1.1.2:8002")},
7777
}
7878

7979
for _, v := range cases {
@@ -84,7 +84,7 @@ func (suit *dfgetSuit) Test_initProperties() {
8484
"--locallimit", v.expected.LocalLimit.String(),
8585
"--totallimit", v.expected.TotalLimit.String()})
8686
initProperties()
87-
suit.EqualValues(cfg.Nodes, v.expected.Nodes)
87+
suit.EqualValues(cfg.Nodes, config.NodeWightSlice2StringSlice(v.expected.Supernodes))
8888
suit.Equal(cfg.LocalLimit, v.expected.LocalLimit)
8989
suit.Equal(cfg.TotalLimit, v.expected.TotalLimit)
9090
suit.Equal(cfg.ClientQueueSize, v.expected.ClientQueueSize)
@@ -134,7 +134,7 @@ func TestSuite(t *testing.T) {
134134
func newProp(local int, total int, size int, nodes ...string) *config.Properties {
135135
p := config.NewProperties()
136136
if nodes != nil {
137-
p.Nodes = nodes
137+
p.Supernodes, _ = config.ParseNodesSlice(nodes)
138138
}
139139
if local != 0 {
140140
p.LocalLimit = rate.Rate(local)

dfget/config/config.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/dragonflyoss/Dragonfly/pkg/printer"
3434
"github.com/dragonflyoss/Dragonfly/pkg/rate"
3535
"github.com/dragonflyoss/Dragonfly/pkg/stringutils"
36-
3736
"github.com/pkg/errors"
3837
"gopkg.in/gcfg.v1"
3938
"gopkg.in/warnings.v0"
@@ -50,14 +49,18 @@ import (
5049
// Since 0.2.0, the INI config is just to be compatible with previous versions.
5150
// The YAML config will have more properties:
5251
// nodes:
53-
// - 127.0.0.1
54-
// - 10.10.10.1
52+
// - 127.0.0.1=1
53+
// - 10.10.10.1:8002=2
5554
// localLimit: 20M
5655
// totalLimit: 20M
5756
// clientQueueSize: 6
5857
type Properties struct {
59-
// Nodes specify supernodes.
60-
Nodes []string `yaml:"nodes,omitempty" json:"nodes,omitempty"`
58+
// Supernodes specify supernodes with weight.
59+
// The type of weight must be integer.
60+
// All weights will be divided by the greatest common divisor in the end.
61+
//
62+
// E.g. ["192.168.33.21=1", "192.168.33.22=2"]
63+
Supernodes []*NodeWight `yaml:"nodes,omitempty" json:"nodes,omitempty"`
6164

6265
// LocalLimit rate limit about a single download task, format: G(B)/g/M(B)/m/K(B)/k/B
6366
// pure number will also be parsed as Byte.
@@ -85,7 +88,7 @@ type Properties struct {
8588
// NewProperties creates a new properties with default values.
8689
func NewProperties() *Properties {
8790
return &Properties{
88-
Nodes: []string{DefaultNode},
91+
Supernodes: GetDefaultSupernodesValue(),
8992
LocalLimit: DefaultLocalLimit,
9093
MinRate: DefaultMinRate,
9194
ClientQueueSize: DefaultClientQueueSize,
@@ -123,8 +126,13 @@ func (p *Properties) loadFromIni(path string) error {
123126
return fmt.Errorf("read ini config from %s error: %v", path, err)
124127
}
125128
}
126-
p.Nodes = strings.Split(oldConfig.Node.Address, ",")
127-
return nil
129+
130+
nodes, err := ParseNodesString(oldConfig.Node.Address)
131+
if err != nil {
132+
return errors.Wrapf(err, "failed to handle nodes")
133+
}
134+
p.Supernodes = nodes
135+
return err
128136
}
129137

130138
func (p *Properties) fileType(path string) string {
@@ -197,6 +205,9 @@ type Config struct {
197205
// If set true, log level will be 'debug'.
198206
Verbose bool `json:"verbose,omitempty"`
199207

208+
// Nodes specify supernodes.
209+
Nodes []string `json:"-"`
210+
200211
// Start time.
201212
StartTime time.Time `json:"-"`
202213

dfget/config/config_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,31 @@ func (suite *ConfigSuite) TestProperties_Load(c *check.C) {
173173
content: "nodes:\n\t- 10.10.10.1", errMsg: "yaml", expected: nil},
174174
{create: true, ext: "yaml",
175175
content: "nodes:\n - 10.10.10.1\n - 10.10.10.2\n",
176-
errMsg: "", expected: &Properties{Nodes: []string{"10.10.10.1", "10.10.10.2"}}},
176+
errMsg: "", expected: &Properties{Supernodes: []*NodeWight{
177+
{"10.10.10.1:8002", 1},
178+
{"10.10.10.2:8002", 1},
179+
}}},
177180
{create: true, ext: "yaml",
178181
content: "totalLimit: 10M",
179182
errMsg: "", expected: &Properties{TotalLimit: 10 * rate.MB}},
180183
{create: false, ext: "ini", content: "[node]\naddress=1.1.1.1", errMsg: "read ini config"},
181184
{create: true, ext: "ini", content: "[node]\naddress=1.1.1.1",
182-
expected: &Properties{Nodes: []string{"1.1.1.1"}}},
185+
expected: &Properties{Supernodes: []*NodeWight{
186+
{"1.1.1.1:8002", 1},
187+
}}},
183188
{create: true, ext: "conf", content: "[node]\naddress=1.1.1.1",
184-
expected: &Properties{Nodes: []string{"1.1.1.1"}}},
189+
expected: &Properties{Supernodes: []*NodeWight{
190+
{"1.1.1.1:8002", 1},
191+
}}},
185192
{create: true, ext: "conf", content: "[node]\naddress=1.1.1.1,1.1.1.2",
186-
expected: &Properties{Nodes: []string{"1.1.1.1", "1.1.1.2"}}},
193+
expected: &Properties{Supernodes: []*NodeWight{
194+
{"1.1.1.1:8002", 1},
195+
{"1.1.1.2:8002", 1},
196+
}}},
187197
{create: true, ext: "conf", content: "[node]\naddress=1.1.1.1\n[totalLimit]",
188-
expected: &Properties{Nodes: []string{"1.1.1.1"}}},
198+
expected: &Properties{Supernodes: []*NodeWight{
199+
{"1.1.1.1:8002", 1},
200+
}}},
189201
}
190202

191203
for idx, v := range cases {

dfget/config/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
DefaultMinRate = 64 * rate.KB
5555
DefaultTotalLimit = 20 * rate.MB
5656
DefaultClientQueueSize = 6
57+
DefaultSupernodeWeight = 1
5758
)
5859

5960
/* http headers */

0 commit comments

Comments
 (0)