forked from apache/whirr
/
CassandraService.java
175 lines (152 loc) · 6.42 KB
/
CassandraService.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.whirr.service.cassandra;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.whirr.service.RunUrlBuilder.runUrls;
import static org.jclouds.compute.options.TemplateOptions.Builder.runScript;
import static org.jclouds.io.Payloads.newStringPayload;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.whirr.service.Cluster;
import org.apache.whirr.service.Cluster.Instance;
import org.apache.whirr.service.ClusterSpec;
import org.apache.whirr.service.ClusterSpec.InstanceTemplate;
import org.apache.whirr.service.ComputeServiceContextBuilder;
import org.apache.whirr.service.Service;
import org.apache.whirr.service.jclouds.FirewallSettings;
import org.apache.whirr.service.jclouds.TemplateBuilderStrategy;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.predicates.NodePredicates;
import org.jclouds.io.Payload;
import org.jclouds.ssh.ExecResponse;
public class CassandraService extends Service {
public static final String CASSANDRA_ROLE = "cassandra";
public static final int CLIENT_PORT = 9160;
@Override
public String getName() {
return "cassandra";
}
@Override
public Cluster launchCluster(ClusterSpec clusterSpec) throws IOException {
ComputeServiceContext computeServiceContext =
ComputeServiceContextBuilder.build(clusterSpec);
ComputeService computeService = computeServiceContext.getComputeService();
Payload bootScript = newStringPayload(runUrls(clusterSpec.getRunUrlBase(),
"sun/java/install",
"apache/cassandra/install"));
TemplateBuilder templateBuilder = computeService.templateBuilder()
.options(
runScript(bootScript).installPrivateKey(
clusterSpec.getPrivateKey()).authorizePublicKey(
clusterSpec.getPublicKey()));
new TemplateBuilderStrategy().configureTemplateBuilder(clusterSpec,
templateBuilder);
Template template = templateBuilder.build();
InstanceTemplate instanceTemplate = clusterSpec
.getInstanceTemplate(CASSANDRA_ROLE);
checkNotNull(instanceTemplate);
int clusterSize = instanceTemplate.getNumberOfInstances();
Set<? extends NodeMetadata> nodeMap;
try {
nodeMap = computeService.runNodesWithTag(clusterSpec.getClusterName(),
clusterSize, template);
} catch (RunNodesException e) {
// TODO: can we do better here
throw new IOException(e);
}
FirewallSettings.authorizeIngress(computeServiceContext, nodeMap, clusterSpec, CLIENT_PORT);
List<NodeMetadata> nodes = Lists.newArrayList(nodeMap);
List<NodeMetadata> seeds = getSeeds(nodes);
// Pass list of all servers in cluster to configure script.
String servers = Joiner.on(' ').join(getPrivateIps(seeds));
Payload configureScript = newStringPayload(runUrls(clusterSpec.getRunUrlBase(),
"apache/cassandra/post-configure " + servers));
try {
Map<? extends NodeMetadata, ExecResponse> responses = computeService
.runScriptOnNodesMatching(
NodePredicates.runningWithTag(clusterSpec.getClusterName()),
configureScript);
assert responses.size() > 0 : "no nodes matched "
+ clusterSpec.getClusterName();
} catch (RunScriptOnNodesException e) {
// TODO: retry
throw new IOException(e);
}
return new Cluster(getInstances(nodes));
}
/**
* Pick a selection of the nodes that are to become seeds. TODO improve
* selection method. Right now it picks 20% of the nodes as seeds, or a
* minimum of one node if it is a small cluster.
*
* @param nodes
* all nodes in cluster
* @return list of seeds
*/
protected List<NodeMetadata> getSeeds(List<NodeMetadata> nodes) {
int seeds = (int) Math.ceil(Math.max(1, nodes.size() * 0.2));
List<NodeMetadata> rv = new ArrayList<NodeMetadata>();
for (int i = 0; i < seeds; i++) {
rv.add(nodes.get(i));
}
return rv;
}
private List<String> getPrivateIps(List<NodeMetadata> nodes) {
return Lists.transform(Lists.newArrayList(nodes),
new Function<NodeMetadata, String>() {
@Override
public String apply(NodeMetadata node) {
return Iterables.get(node.getPrivateAddresses(), 0);
}
});
}
private Set<Instance> getInstances(List<NodeMetadata> nodes) {
return Sets.newHashSet(Collections2.transform(Sets.newHashSet(nodes),
new Function<NodeMetadata, Instance>() {
@Override
public Instance apply(NodeMetadata node) {
try {
return new Instance(node.getCredentials(), Collections
.singleton(CASSANDRA_ROLE), InetAddress.getByName(Iterables
.get(node.getPublicAddresses(), 0)), InetAddress
.getByName(Iterables.get(node.getPrivateAddresses(), 0)));
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}));
}
}