| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.gogrid.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.*; |
| 22 | |
| 23 | import java.security.SecureRandom; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.compute.domain.Hardware; |
| 30 | import org.jclouds.compute.domain.NodeMetadata; |
| 31 | import org.jclouds.compute.domain.Template; |
| 32 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 33 | import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; |
| 34 | import org.jclouds.gogrid.GoGridClient; |
| 35 | import org.jclouds.gogrid.domain.Ip; |
| 36 | import org.jclouds.gogrid.domain.IpType; |
| 37 | import org.jclouds.gogrid.domain.PowerCommand; |
| 38 | import org.jclouds.gogrid.domain.Server; |
| 39 | import org.jclouds.gogrid.options.GetIpListOptions; |
| 40 | import org.jclouds.gogrid.predicates.ServerLatestJobCompleted; |
| 41 | import org.jclouds.predicates.RetryablePredicate; |
| 42 | |
| 43 | import com.google.common.base.Function; |
| 44 | import com.google.common.base.Throwables; |
| 45 | import com.google.common.collect.Iterables; |
| 46 | |
| 47 | /** |
| 48 | * @author Oleksiy Yarmula |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class FindIpThenCreateNodeInGroup implements CreateNodeWithGroupEncodedIntoName { |
| 52 | private final GoGridClient client; |
| 53 | private final Function<Hardware, String> sizeToRam; |
| 54 | private final Function<Server, NodeMetadata> serverToNodeMetadata; |
| 55 | private RetryablePredicate<Server> serverLatestJobCompleted; |
| 56 | private RetryablePredicate<Server> serverLatestJobCompletedShort; |
| 57 | |
| 58 | @Inject |
| 59 | protected FindIpThenCreateNodeInGroup(GoGridClient client, |
| 60 | Function<Server, NodeMetadata> serverToNodeMetadata, Function<Hardware, String> sizeToRam, |
| 61 | Timeouts timeouts) { |
| 62 | this.client = client; |
| 63 | this.serverToNodeMetadata = serverToNodeMetadata; |
| 64 | this.sizeToRam = sizeToRam; |
| 65 | this.serverLatestJobCompleted = new RetryablePredicate<Server>( |
| 66 | new ServerLatestJobCompleted(client.getJobServices()), |
| 67 | timeouts.nodeRunning * 9l / 10l); |
| 68 | this.serverLatestJobCompletedShort = new RetryablePredicate<Server>( |
| 69 | new ServerLatestJobCompleted(client.getJobServices()), |
| 70 | timeouts.nodeRunning * 1l / 10l); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) { |
| 75 | Server addedServer = null; |
| 76 | boolean notStarted = true; |
| 77 | int numOfRetries = 20; |
| 78 | GetIpListOptions unassignedIps = new GetIpListOptions() |
| 79 | .onlyUnassigned() |
| 80 | .inDatacenter(template.getLocation().getId()) |
| 81 | .onlyWithType(IpType.PUBLIC); |
| 82 | // lock-free consumption of a shared resource: IP address pool |
| 83 | while (notStarted) { // TODO: replace with Predicate-based thread |
| 84 | // collision avoidance for simplicity |
| 85 | Set<Ip> availableIps = client.getIpServices().getIpList(unassignedIps); |
| 86 | if (availableIps.isEmpty()) |
| 87 | throw new RuntimeException("No IPs available on this identity."); |
| 88 | int ipIndex = new SecureRandom().nextInt(availableIps.size()); |
| 89 | Ip availableIp = Iterables.get(availableIps, ipIndex); |
| 90 | try { |
| 91 | addedServer = addServer(name, template, availableIp); |
| 92 | notStarted = false; |
| 93 | } catch (Exception e) { |
| 94 | if (--numOfRetries == 0) |
| 95 | Throwables.propagate(e); |
| 96 | notStarted = true; |
| 97 | } |
| 98 | } |
| 99 | if (template.getOptions().shouldBlockUntilRunning()) { |
| 100 | serverLatestJobCompleted.apply(addedServer); |
| 101 | client.getServerServices().power(addedServer.getName(), PowerCommand.START); |
| 102 | serverLatestJobCompletedShort.apply(addedServer); |
| 103 | addedServer = Iterables.getOnlyElement(client.getServerServices().getServersByName( |
| 104 | addedServer.getName())); |
| 105 | } |
| 106 | return serverToNodeMetadata.apply(addedServer); |
| 107 | } |
| 108 | |
| 109 | private Server addServer(String name, Template template, Ip availableIp) { |
| 110 | Server addedServer; |
| 111 | addedServer = client.getServerServices().addServer(name, |
| 112 | checkNotNull(template.getImage().getProviderId()), |
| 113 | sizeToRam.apply(template.getHardware()), availableIp.getIp()); |
| 114 | return addedServer; |
| 115 | } |
| 116 | } |