EMMA Coverage Report (generated Mon Oct 17 05:41:20 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/233)0%   (0/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FindIpThenCreateNodeInGroup0%   (0/1)0%   (0/3)0%   (0/233)0%   (0/39)
FindIpThenCreateNodeInGroup (GoGridClient, Map, Function, Function, ComputeSe... 0%   (0/1)0%   (0/62)0%   (0/9)
addServer (String, Template, Ip): Server 0%   (0/1)0%   (0/23)0%   (0/2)
createNodeWithGroupEncodedIntoName (String, String, Template): NodeMetadata 0%   (0/1)0%   (0/148)0%   (0/28)

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

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