EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.gogrid.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [FindIpThenCreateNodeInGroup.java]

nameclass, %method, %block, %line, %
FindIpThenCreateNodeInGroup.java0%   (0/1)0%   (0/3)0%   (0/178)0%   (0/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FindIpThenCreateNodeInGroup0%   (0/1)0%   (0/3)0%   (0/178)0%   (0/33)
FindIpThenCreateNodeInGroup (GoGridClient, Function, Function, ComputeService... 0%   (0/1)0%   (0/44)0%   (0/7)
addServer (String, Template, Ip): Server 0%   (0/1)0%   (0/23)0%   (0/2)
createNodeWithGroupEncodedIntoName (String, String, Template): NodeMetadata 0%   (0/1)0%   (0/111)0%   (0/24)

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 */
19package org.jclouds.gogrid.compute.strategy;
20 
21import static com.google.common.base.Preconditions.*;
22 
23import java.security.SecureRandom;
24import java.util.Set;
25 
26import javax.inject.Inject;
27import javax.inject.Singleton;
28 
29import org.jclouds.compute.domain.Hardware;
30import org.jclouds.compute.domain.NodeMetadata;
31import org.jclouds.compute.domain.Template;
32import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
33import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName;
34import org.jclouds.gogrid.GoGridClient;
35import org.jclouds.gogrid.domain.Ip;
36import org.jclouds.gogrid.domain.IpType;
37import org.jclouds.gogrid.domain.PowerCommand;
38import org.jclouds.gogrid.domain.Server;
39import org.jclouds.gogrid.options.GetIpListOptions;
40import org.jclouds.gogrid.predicates.ServerLatestJobCompleted;
41import org.jclouds.predicates.RetryablePredicate;
42 
43import com.google.common.base.Function;
44import com.google.common.base.Throwables;
45import com.google.common.collect.Iterables;
46 
47/**
48 * @author Oleksiy Yarmula
49 */
50@Singleton
51public 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}

[all classes][org.jclouds.gogrid.compute.strategy]
EMMA 2.0.5312 (C) Vladimir Roubtsov