forked from voldemort/voldemort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigureNodesByZone.java
150 lines (129 loc) · 5.49 KB
/
ConfigureNodesByZone.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2010 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. 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 voldemort.store.routed.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import voldemort.VoldemortException;
import voldemort.cluster.Node;
import voldemort.cluster.Zone;
import voldemort.cluster.failuredetector.FailureDetector;
import voldemort.routing.RoutingStrategy;
import voldemort.store.routed.BasicPipelineData;
import voldemort.store.routed.Pipeline;
import voldemort.store.routed.Pipeline.Event;
import voldemort.store.routed.Pipeline.Operation;
import voldemort.utils.ByteArray;
import voldemort.utils.ByteUtils;
/**
* Configure the Nodes obtained via the routing strategy based on the zone
* information. Local zone nodes first, followed by the corresponding nodes from
* each of the other zones, ordered by proximity.
*/
public class ConfigureNodesByZone<V, PD extends BasicPipelineData<V>> extends
AbstractConfigureNodes<ByteArray, V, PD> {
private final ByteArray key;
private final Zone clientZone;
public ConfigureNodesByZone(PD pipelineData,
Event completeEvent,
FailureDetector failureDetector,
int required,
RoutingStrategy routingStrategy,
ByteArray key,
Zone clientZone) {
super(pipelineData, completeEvent, failureDetector, required, routingStrategy);
this.key = key;
this.clientZone = clientZone;
}
public List<Node> getNodes(ByteArray key, Operation op) {
List<Node> nodes = null;
try {
nodes = super.getNodes(key);
} catch(VoldemortException e) {
pipelineData.setFatalError(e);
return null;
}
if(logger.isDebugEnabled())
logger.debug("Adding " + nodes.size() + " node(s) to preference list");
if(pipelineData.getZonesRequired() > this.clientZone.getProximityList().size()) {
throw new VoldemortException("Number of zones required should be less than the total number of zones");
}
if(pipelineData.getZonesRequired() > required) {
throw new VoldemortException("Number of zones required should be less than the required number of "
+ op.getSimpleName() + "s");
}
// Create zone id to node mapping
Map<Integer, List<Node>> zoneIdToNode = new HashMap<Integer, List<Node>>();
for(Node node: nodes) {
List<Node> nodesList = null;
if(zoneIdToNode.containsKey(node.getZoneId())) {
nodesList = zoneIdToNode.get(node.getZoneId());
} else {
nodesList = new ArrayList<Node>();
zoneIdToNode.put(node.getZoneId(), nodesList);
}
nodesList.add(node);
}
nodes = new ArrayList<Node>();
LinkedList<Integer> zoneProximityList = this.clientZone.getProximityList();
if(op != Operation.PUT) {
// GET, GET_VERSIONS, DELETE
// Add a node from every zone, upto a max of
// zoneCountReads/zoneCountWrites.
for(int index = 0; index < pipelineData.getZonesRequired(); index++) {
List<Node> zoneNodes = zoneIdToNode.get(zoneProximityList.get(index));
if(zoneNodes != null && zoneNodes.size() > 0) {
nodes.add(zoneNodes.remove(0));
}
}
}
// Add the rest, starting with client zone...
List<Node> clientZoneNodes = zoneIdToNode.get(clientZone.getId());
if(clientZoneNodes != null && clientZoneNodes.size() > 0)
nodes.addAll(clientZoneNodes);
// ...followed by other zones sorted by proximity list
// NOTE : its imperative that the proximity list does not contain the
// client zone. If this happens, we will add those nodes twice to the
// list
for(int index = 0; index < zoneProximityList.size(); index++) {
List<Node> zoneNodes = zoneIdToNode.get(zoneProximityList.get(index));
if(zoneNodes != null && zoneNodes.size() > 0) {
nodes.addAll(zoneNodes);
}
}
return nodes;
}
public void execute(Pipeline pipeline) {
List<Node> nodes = null;
nodes = getNodes(key, pipeline.getOperation());
if(nodes == null) {
pipeline.abort();
return;
}
if(logger.isDebugEnabled()) {
StringBuilder nodeStr = new StringBuilder();
for(Node node: nodes) {
nodeStr.append(node.getId() + ",");
}
logger.debug("Key " + ByteUtils.toHexString(key.get())
+ " final preference list to contact " + nodeStr);
}
pipelineData.setNodes(nodes);
pipeline.addEvent(completeEvent);
}
}