9 // Coordinate is a specialized structure for holding network coordinates for the
10 // Vivaldi-based coordinate mapping algorithm. All of the fields should be public
11 // to enable this to be serialized. All values in here are in units of seconds.
12 type Coordinate struct {
13 // Vec is the Euclidean portion of the coordinate. This is used along
14 // with the other fields to provide an overall distance estimate. The
15 // units here are seconds.
18 // Err reflects the confidence in the given coordinate and is updated
19 // dynamically by the Vivaldi Client. This is dimensionless.
22 // Adjustment is a distance offset computed based on a calculation over
23 // observations from all other nodes over a fixed window and is updated
24 // dynamically by the Vivaldi Client. The units here are seconds.
27 // Height is a distance offset that accounts for non-Euclidean effects
28 // which model the access links from nodes to the core Internet. The access
29 // links are usually set by bandwidth and congestion, and the core links
30 // usually follow distance based on geography.
35 // secondsToNanoseconds is used to convert float seconds to nanoseconds.
36 secondsToNanoseconds = 1.0e9
38 // zeroThreshold is used to decide if two coordinates are on top of each
40 zeroThreshold = 1.0e-6
43 // ErrDimensionalityConflict will be panic-d if you try to perform operations
44 // with incompatible dimensions.
45 type DimensionalityConflictError struct{}
47 // Adds the error interface.
48 func (e DimensionalityConflictError) Error() string {
49 return "coordinate dimensionality does not match"
52 // NewCoordinate creates a new coordinate at the origin, using the given config
53 // to supply key initial values.
54 func NewCoordinate(config *Config) *Coordinate {
56 Vec: make([]float64, config.Dimensionality),
57 Error: config.VivaldiErrorMax,
59 Height: config.HeightMin,
63 // Clone creates an independent copy of this coordinate.
64 func (c *Coordinate) Clone() *Coordinate {
65 vec := make([]float64, len(c.Vec))
70 Adjustment: c.Adjustment,
75 // IsCompatibleWith checks to see if the two coordinates are compatible
76 // dimensionally. If this returns true then you are guaranteed to not get
77 // any runtime errors operating on them.
78 func (c *Coordinate) IsCompatibleWith(other *Coordinate) bool {
79 return len(c.Vec) == len(other.Vec)
82 // ApplyForce returns the result of applying the force from the direction of the
84 func (c *Coordinate) ApplyForce(config *Config, force float64, other *Coordinate) *Coordinate {
85 if !c.IsCompatibleWith(other) {
86 panic(DimensionalityConflictError{})
90 unit, mag := unitVectorAt(c.Vec, other.Vec)
91 ret.Vec = add(ret.Vec, mul(unit, force))
92 if mag > zeroThreshold {
93 ret.Height = (ret.Height+other.Height)*force/mag + ret.Height
94 ret.Height = math.Max(ret.Height, config.HeightMin)
99 // DistanceTo returns the distance between this coordinate and the other
100 // coordinate, including adjustments.
101 func (c *Coordinate) DistanceTo(other *Coordinate) time.Duration {
102 if !c.IsCompatibleWith(other) {
103 panic(DimensionalityConflictError{})
106 dist := c.rawDistanceTo(other)
107 adjustedDist := dist + c.Adjustment + other.Adjustment
108 if adjustedDist > 0.0 {
111 return time.Duration(dist * secondsToNanoseconds)
114 // rawDistanceTo returns the Vivaldi distance between this coordinate and the
115 // other coordinate in seconds, not including adjustments. This assumes the
116 // dimensions have already been checked to be compatible.
117 func (c *Coordinate) rawDistanceTo(other *Coordinate) float64 {
118 return magnitude(diff(c.Vec, other.Vec)) + c.Height + other.Height
121 // add returns the sum of vec1 and vec2. This assumes the dimensions have
122 // already been checked to be compatible.
123 func add(vec1 []float64, vec2 []float64) []float64 {
124 ret := make([]float64, len(vec1))
125 for i, _ := range ret {
126 ret[i] = vec1[i] + vec2[i]
131 // diff returns the difference between the vec1 and vec2. This assumes the
132 // dimensions have already been checked to be compatible.
133 func diff(vec1 []float64, vec2 []float64) []float64 {
134 ret := make([]float64, len(vec1))
135 for i, _ := range ret {
136 ret[i] = vec1[i] - vec2[i]
141 // mul returns vec multiplied by a scalar factor.
142 func mul(vec []float64, factor float64) []float64 {
143 ret := make([]float64, len(vec))
144 for i, _ := range vec {
145 ret[i] = vec[i] * factor
150 // magnitude computes the magnitude of the vec.
151 func magnitude(vec []float64) float64 {
153 for i, _ := range vec {
154 sum += vec[i] * vec[i]
156 return math.Sqrt(sum)
159 // unitVectorAt returns a unit vector pointing at vec1 from vec2. If the two
160 // positions are the same then a random unit vector is returned. We also return
161 // the distance between the points for use in the later height calculation.
162 func unitVectorAt(vec1 []float64, vec2 []float64) ([]float64, float64) {
163 ret := diff(vec1, vec2)
165 // If the coordinates aren't on top of each other we can normalize.
166 if mag := magnitude(ret); mag > zeroThreshold {
167 return mul(ret, 1.0/mag), mag
170 // Otherwise, just return a random unit vector.
171 for i, _ := range ret {
172 ret[i] = rand.Float64() - 0.5
174 if mag := magnitude(ret); mag > zeroThreshold {
175 return mul(ret, 1.0/mag), 0.0
178 // And finally just give up and make a unit vector along the first
179 // dimension. This should be exceedingly rare.
180 ret = make([]float64, len(ret))