-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.go
110 lines (94 loc) · 4.3 KB
/
structure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
*
* Copyright 2021 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package builderfile
import (
"encoding/json"
"fmt"
"time"
)
type DeviceInfo struct {
ID uint64
Region uint64
Zone uint64
NodeIP string `yaml:"ip" mapstructure:"ip"`
Port uint64
ReplicationIP string `yaml:"replication_ip" mapstructure:"replication_ip"`
ReplicationPort uint64 `yaml:"replication_port" mapstructure:"replication_port"`
Name string `mapstructure:"device"`
Weight float64
Partitions uint64 `mapstructure:"parts"`
Balance float64
Meta *map[string]string `yaml:"meta,omitempty"`
//nolint:unused
flags struct{} // TODO: figure out how the field looks like
}
// RingInfo contains the meta data about the ring file
type RingInfo struct {
FileName string `yaml:"file_name"`
Version uint64
ID string
Partitions uint64
Replicas float64
Regions uint64
Zones uint64
DeviceCount uint64 `yaml:"device_count"`
Balance float64
Dispersion float64
ReassignedCooldown uint64 `yaml:"reassigned_cooldown"`
ReassignedRemaining time.Time `yaml:"reassigned_remaining"`
OverloadFactorPercent float64 `yaml:"overload_factor_Percent"`
OverloadFactorDecimal float64 `yaml:"overload_factor_decimal"`
Devices []DeviceInfo
}
func (device DeviceInfo) IPAddressPort() string {
return fmt.Sprintf("%s:%d", device.NodeIP, device.Port)
}
func (ring RingInfo) CommandSetOverload(ringFilename string, desiredOverload float64) string {
return fmt.Sprintf("swift-ring-builder %s set_overload %f", ringFilename, desiredOverload)
}
func (device DeviceInfo) CommandAdd(ringFilename string) string {
if device.Meta != nil {
var meta []byte
meta, _ = json.Marshal(device.Meta) //nolint:errcheck
return fmt.Sprintf("swift-ring-builder %s add --region %d --zone %d --ip %s --port %d --device %s --weight %g --meta %s",
ringFilename, device.Region, device.Zone, device.NodeIP, device.Port, device.Name, device.Weight, meta)
}
return fmt.Sprintf("swift-ring-builder %s add --region %d --zone %d --ip %s --port %d --device %s --weight %g",
ringFilename, device.Region, device.Zone, device.NodeIP, device.Port, device.Name, device.Weight)
}
func (device DeviceInfo) CommandSetMeta(ringFilename string, desiredMeta map[string]string) string {
var meta []byte
meta, _ = json.Marshal(desiredMeta) //nolint:errcheck
return fmt.Sprintf("swift-ring-builder %s set_info --region %d --zone %d --ip %s --port %d --device %s --change-meta %s",
ringFilename, device.Region, device.Zone, device.NodeIP, device.Port, device.Name, meta)
}
func (device DeviceInfo) CommandSetMetaNode(ringFilename string, desiredMeta map[string]string) string {
var meta []byte
meta, _ = json.Marshal(desiredMeta) //nolint:errcheck
return fmt.Sprintf("swift-ring-builder %s set_info --region %d --zone %d --ip %s --port %d --change-meta %s --yes",
ringFilename, device.Region, device.Zone, device.NodeIP, device.Port, meta)
}
func (device DeviceInfo) CommandSetWeight(ringFilename string, desiredWeight float64) string {
return fmt.Sprintf("swift-ring-builder %s set_weight --region %d --zone %d --ip %s --port %d --device %s --weight %g %g",
ringFilename, device.Region, device.Zone, device.NodeIP, device.Port, device.Name, device.Weight, desiredWeight)
}
func (device DeviceInfo) CommandRemove(ringFilename string) string {
return fmt.Sprintf("swift-ring-builder %s remove --region %d --zone %d --ip %s --port %d --device %s --weight %g",
ringFilename, device.Region, device.Zone, device.NodeIP, device.Port, device.Name, device.Weight)
}