LEFT | RIGHT |
1 package org.apache.cassandra.locator; | 1 package org.apache.cassandra.locator; |
2 | 2 |
3 import java.net.InetAddress; | 3 import java.net.InetAddress; |
4 import java.util.ArrayList; | 4 import java.util.ArrayList; |
5 import java.util.Collection; | 5 import java.util.Collection; |
6 import java.util.Collections; | 6 import java.util.Collections; |
7 import java.util.Comparator; | 7 import java.util.Comparator; |
8 import java.util.HashMap; | 8 import java.util.HashMap; |
9 import java.util.List; | 9 import java.util.List; |
10 import java.util.Map; | 10 import java.util.Map; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 public List<InetAddress> getSortedListByProximity(final InetAddress address,
Collection<InetAddress> addresses) | 91 public List<InetAddress> getSortedListByProximity(final InetAddress address,
Collection<InetAddress> addresses) |
92 { | 92 { |
93 List<InetAddress> list = new ArrayList<InetAddress>(addresses); | 93 List<InetAddress> list = new ArrayList<InetAddress>(addresses); |
94 sortByProximity(address, list); | 94 sortByProximity(address, list); |
95 return list; | 95 return list; |
96 } | 96 } |
97 | 97 |
98 // TODO: what do we need: increasing or decreasing order? | 98 // We need an increasing order, the first address in the list that getSorted
ListbyProximit() returns should be the closest, |
| 99 // that is, the one with the lowest latency. This follows from the choice of
StorageService::getPreferredHintsStreamTarget(). |
99 private int compareEndpoints(InetAddress target, InetAddress hostA, InetAddr
ess hostB, Map<InetAddress, Integer> latencies) | 100 private int compareEndpoints(InetAddress target, InetAddress hostA, InetAddr
ess hostB, Map<InetAddress, Integer> latencies) |
100 { | 101 { |
101 // It is enough because our special topology | 102 // It is enough because our special topology |
102 Integer hostALatency = latencies.get(hostA); | 103 Integer hostALatency = latencies.get(hostA); |
103 Integer hostBLatency = latencies.get(hostB); | 104 Integer hostBLatency = latencies.get(hostB); |
104 if (hostALatency == null) { | 105 if (hostALatency == null) { |
105 hostALatency = Integer.MAX_VALUE; | 106 hostALatency = Integer.MAX_VALUE; |
106 } | 107 } |
107 if (hostBLatency == null) { | 108 if (hostBLatency == null) { |
108 hostBLatency = Integer.MAX_VALUE; | 109 hostBLatency = Integer.MAX_VALUE; |
109 } | 110 } |
110 if (hostALatency.equals(hostBLatency)) | 111 if (hostALatency.equals(hostBLatency)) |
111 return subsnitch.compareEndpoints(target, hostA, hostB); | 112 return subsnitch.compareEndpoints(target, hostA, hostB); |
112 if (hostALatency < hostBLatency) | 113 return hostALatency.compareTo(hostBLatency); |
113 return -1; | |
114 else | |
115 return 1; | |
116 } | 114 } |
117 | 115 |
118 @Override | 116 @Override |
119 public void sortByProximity(final InetAddress address, List<InetAddress> add
resses) | 117 public void sortByProximity(final InetAddress address, List<InetAddress> add
resses) |
120 { | 118 { |
121 // Same issue as in DynamicEndpointSnitch::sortByProximityWithScore() | 119 // Same issue as in DynamicEndpointSnitch::sortByProximityWithScore() |
122 final HashMap<InetAddress, Integer> latencies = this.switchToHostLatency
; | 120 final HashMap<InetAddress, Integer> latencies = this.switchToHostLatency
; |
123 Collections.sort(addresses, new Comparator<InetAddress>() | 121 Collections.sort(addresses, new Comparator<InetAddress>() |
124 { | 122 { |
125 public int compare(InetAddress a1, InetAddress a2) | 123 public int compare(InetAddress a1, InetAddress a2) |
126 { | 124 { |
127 return compareEndpoints(address, a1, a2, latencies); | 125 return compareEndpoints(address, a1, a2, latencies); |
128 } | 126 } |
129 }); | 127 }); |
130 } | 128 } |
131 } | 129 } |
LEFT | RIGHT |