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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.dns;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.zone.ZoneId;
import java.util.Objects;
/**
* An implementation of {@link AliasTarget} where is requests are answered based on the weight assigned to the
* record, as a proportion of the total weight for all records having the same DNS name.
*
* The portion of received traffic is calculated as follows: (record weight / sum of the weights of all records).
*
* @author mpolden
*/
public class WeightedAliasTarget extends AliasTarget {
private final long weight;
public WeightedAliasTarget(HostName name, String dnsZone, ZoneId zone, long weight) {
super(name, dnsZone, zone.value());
this.weight = weight;
}
/** The weight of this target */
public long weight() {
return weight;
}
@Override
public RecordData pack() {
return RecordData.from("weighted/" + name().value() + "/" + dnsZone() + "/" + id() + "/" + weight);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
WeightedAliasTarget that = (WeightedAliasTarget) o;
return weight == that.weight;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), weight);
}
@Override
public String toString() {
return "weighted target for " + name() + "[id=" + id() + ",dnsZone=" + dnsZone() + ",weight=" + weight + "]";
}
/** Unpack weighted alias from given record data */
public static WeightedAliasTarget unpack(RecordData data) {
var parts = data.asString().split("/");
if (parts.length != 5) {
throw new IllegalArgumentException("Expected data to be on format type/name/DNS-zone/zone-id/weight, " +
"but got " + data.asString());
}
if (!"weighted".equals(parts[0])) {
throw new IllegalArgumentException("Unexpected type '" + parts[0] + "'");
}
return new WeightedAliasTarget(HostName.from(parts[1]), parts[2], ZoneId.from(parts[3]),
Long.parseLong(parts[4]));
}
}
|