Issue 260: fixed executors per kimchy's advice; also fixed regression in a lot of live tests

This commit is contained in:
Adrian Cole 2010-05-23 22:26:14 -07:00
parent d3d36dc879
commit c8cd0b0f2e
19 changed files with 513 additions and 112 deletions

View File

@ -51,6 +51,8 @@ import org.testng.annotations.Test;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.inject.Module; import com.google.inject.Module;
/** /**
@ -117,7 +119,7 @@ public class AtmosStorageClientLiveTest {
String uid = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user"); String uid = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user");
String key = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key"); String key = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key");
BlobStoreContext blobStoreContext = new BlobStoreContextFactory().createContext( BlobStoreContext blobStoreContext = new BlobStoreContextFactory().createContext(
"atmosstorage", uid, key, ImmutableSet.<Module> of(new Log4JLoggingModule())); "atmosonline", uid, key, ImmutableSet.<Module> of(new Log4JLoggingModule()));
RestContext<AtmosStorageAsyncClient, AtmosStorageClient> context = blobStoreContext RestContext<AtmosStorageAsyncClient, AtmosStorageClient> context = blobStoreContext
.getProviderSpecificContext(); .getProviderSpecificContext();
connection = context.getApi(); connection = context.getApi();
@ -166,18 +168,13 @@ public class AtmosStorageClientLiveTest {
createOrReplaceObject("object4", "here is my data!", "meta-value1"); createOrReplaceObject("object4", "here is my data!", "meta-value1");
BoundedSet<? extends DirectoryEntry> r2 = connection.listDirectory(privateDirectory, BoundedSet<? extends DirectoryEntry> r2 = connection.listDirectory(privateDirectory,
ListOptions.Builder.limit(1)); ListOptions.Builder.limit(1));
// test bug exists: assertEquals(r2.size(), 1);
assertEquals(r2.size(), 3); assert r2.getToken() != null;
// assertEquals(r2.size(), 1); assertEquals(Iterables.getLast(Sets.newTreeSet(r2)).getObjectName(), "object2");
// assert r2.getToken() != null; r2 = connection.listDirectory(privateDirectory, ListOptions.Builder.token(r2.getToken()));
// assertEquals(r2.last().getObjectName(),"object2"); assertEquals(r2.size(), 2);
// r2 = connection.listDirectory(privateDirectory, assert r2.getToken() == null;
// ListOptions.Builder.token(r2.getToken())).get(10, assertEquals(Iterables.getLast(Sets.newTreeSet(r2)).getObjectName(), "object4");
// TimeUnit.SECONDS);
// assertEquals(r2.size(), 2);
// assert r2.getToken() == null;
// assertEquals(r2.last().getObjectName(),"object4");
} }
@Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testListOptions" }) @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testListOptions" })

View File

@ -80,6 +80,8 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
String startedId = null; String startedId = null;
try { try {
cleanupExtendedStuff(securityGroupClient, keyPairClient, tag);
// create a security group that allows ssh in so that our scripts later will work // create a security group that allows ssh in so that our scripts later will work
securityGroupClient.createSecurityGroupInRegion(null, tag, tag); securityGroupClient.createSecurityGroupInRegion(null, tag, tag);
securityGroupClient.authorizeSecurityGroupIngressInRegion(null, tag, IpProtocol.TCP, 22, securityGroupClient.authorizeSecurityGroupIngressInRegion(null, tag, IpProtocol.TCP, 22,
@ -142,6 +144,8 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
String startedId = null; String startedId = null;
try { try {
cleanupExtendedStuff(securityGroupClient, keyPairClient, tag);
// create the security group // create the security group
securityGroupClient.createSecurityGroupInRegion(null, tag, tag); securityGroupClient.createSecurityGroupInRegion(null, tag, tag);

View File

@ -132,7 +132,7 @@ public class NodeMetadataImpl extends ComputeMetadataImpl implements NodeMetadat
+ getName() + ", location=" + getLocation() + ", uri=" + getUri() + ", image=" + getName() + ", location=" + getLocation() + ", uri=" + getUri() + ", image="
+ getImage() + ", userMetadata=" + getUserMetadata() + ", state=" + getState() + getImage() + ", userMetadata=" + getUserMetadata() + ", state=" + getState()
+ ", privateAddresses=" + privateAddresses + ", publicAddresses=" + publicAddresses + ", privateAddresses=" + privateAddresses + ", publicAddresses=" + publicAddresses
+ "]"; + ", extra=" + getExtra() + "]";
} }
@Override @Override

View File

@ -165,9 +165,9 @@ public abstract class BaseComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = "testImagesCache") @Test(enabled = true, dependsOnMethods = "testImagesCache")
public void testTemplateMatch() throws Exception { public void testTemplateMatch() throws Exception {
template = buildTemplate(client.templateBuilder()); template = buildTemplate(client.templateBuilder());
Template toMatch = client.templateBuilder().imageId(template.getImage().getProviderId()) Template toMatch = client.templateBuilder().imageId(template.getImage().getId())
.build(); .build();
assertEquals(toMatch, template); assertEquals(toMatch.getImage(), template.getImage());
} }
@Test(enabled = true, dependsOnMethods = "testTemplateMatch") @Test(enabled = true, dependsOnMethods = "testTemplateMatch")

View File

@ -0,0 +1,209 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.jclouds.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Factory and utility methods for handling {@link DynamicThreadPoolExecutor}.
*
* @author kimchy (shay.banon)
*/
public class DynamicExecutors {
/**
* Creates a thread pool that creates new threads as needed, but will reuse previously
* constructed threads when they are available. Calls to <tt>execute</tt> will reuse previously
* constructed threads if available. If no existing thread is available, a new thread will be
* created and added to the pool. No more than <tt>max</tt> threads will be created. Threads that
* have not been used for a <tt>keepAlive</tt> timeout are terminated and removed from the cache.
* Thus, a pool that remains idle for long enough will not consume any resources other than the
* <tt>min</tt> specified.
*
* @param min
* the number of threads to keep in the pool, even if they are idle.
* @param max
* the maximum number of threads to allow in the pool.
* @param keepAliveTime
* when the number of threads is greater than the min, this is the maximum time that
* excess idle threads will wait for new tasks before terminating (in milliseconds).
* @return the newly created thread pool
*/
public static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime) {
return newScalingThreadPool(min, max, keepAliveTime, Executors.defaultThreadFactory());
}
/**
* Creates a thread pool, same as in {@link #newScalingThreadPool(int, int, long)}, using the
* provided ThreadFactory to create new threads when needed.
*
* @param min
* the number of threads to keep in the pool, even if they are idle.
* @param max
* the maximum number of threads to allow in the pool.
* @param keepAliveTime
* when the number of threads is greater than the min, this is the maximum time that
* excess idle threads will wait for new tasks before terminating (in milliseconds).
* @param threadFactory
* the factory to use when creating new threads.
* @return the newly created thread pool
*/
public static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime,
ThreadFactory threadFactory) {
DynamicThreadPoolExecutor.DynamicQueue<Runnable> queue = new DynamicThreadPoolExecutor.DynamicQueue<Runnable>();
ThreadPoolExecutor executor = new DynamicThreadPoolExecutor(min, max, keepAliveTime,
TimeUnit.MILLISECONDS, queue, threadFactory);
executor.setRejectedExecutionHandler(new DynamicThreadPoolExecutor.ForceQueuePolicy());
queue.setThreadPoolExecutor(executor);
return executor;
}
/**
* Creates a thread pool similar to that constructed by
* {@link #newScalingThreadPool(int, int, long)}, but blocks the call to <tt>execute</tt> if the
* queue has reached it's capacity, and all <tt>max</tt> threads are busy handling requests.
* <p/>
* If the wait time of this queue has elapsed, a {@link RejectedExecutionException} will be
* thrown.
*
* @param min
* the number of threads to keep in the pool, even if they are idle.
* @param max
* the maximum number of threads to allow in the pool.
* @param keepAliveTime
* when the number of threads is greater than the min, this is the maximum time that
* excess idle threads will wait for new tasks before terminating (in milliseconds).
* @param capacity
* the fixed capacity of the underlying queue (resembles backlog).
* @param waitTime
* the wait time (in milliseconds) for space to become available in the queue.
* @return the newly created thread pool
*/
public static ExecutorService newBlockingThreadPool(int min, int max, long keepAliveTime,
int capacity, long waitTime) {
return newBlockingThreadPool(min, max, keepAliveTime, capacity, waitTime, Executors
.defaultThreadFactory());
}
/**
* Creates a thread pool, same as in {@link #newBlockingThreadPool(int, int, long, int, long)},
* using the provided ThreadFactory to create new threads when needed.
*
* @param min
* the number of threads to keep in the pool, even if they are idle.
* @param max
* the maximum number of threads to allow in the pool.
* @param keepAliveTime
* when the number of threads is greater than the min, this is the maximum time that
* excess idle threads will wait for new tasks before terminating (in milliseconds).
* @param capacity
* the fixed capacity of the underlying queue (resembles backlog).
* @param waitTime
* the wait time (in milliseconds) for space to become available in the queue.
* @param threadFactory
* the factory to use when creating new threads.
* @return the newly created thread pool
*/
public static ExecutorService newBlockingThreadPool(int min, int max, long keepAliveTime,
int capacity, long waitTime, ThreadFactory threadFactory) {
DynamicThreadPoolExecutor.DynamicQueue<Runnable> queue = new DynamicThreadPoolExecutor.DynamicQueue<Runnable>(
capacity);
ThreadPoolExecutor executor = new DynamicThreadPoolExecutor(min, max, keepAliveTime,
TimeUnit.MILLISECONDS, queue, threadFactory);
executor.setRejectedExecutionHandler(new DynamicThreadPoolExecutor.TimedBlockingPolicy(
waitTime));
queue.setThreadPoolExecutor(executor);
return executor;
}
/**
* A priority based thread factory, for all Thread priority constants:
* <tt>Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY</tt>;
* <p/>
* This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of
* priority and thread owner name.
*
* @param namePrefix
* a name prefix for this thread
* @return a thread factory based on given priority.
*/
public static ThreadFactory daemonThreadFactory(String namePrefix) {
final ThreadFactory f = Executors.defaultThreadFactory();
final String o = namePrefix + "-";
return new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = f.newThread(r);
/*
* Thread name: owner-pool-N-thread-M, where N is the sequence number of this factory,
* and M is the sequence number of the thread created by this factory.
*/
t.setName(o + t.getName());
/* override default definition t.setDaemon(false); */
t.setDaemon(true);
return t;
}
};
}
/**
* A priority based thread factory, for all Thread priority constants:
* <tt>Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY</tt>;
* <p/>
* This factory is used instead of Executers.DefaultThreadFactory to allow manipulation of
* priority and thread owner name.
*
* @param priority
* The priority to be assigned to each thread; can be either
* <tt>Thread.MIN_PRIORITY, Thread.NORM_PRIORITY</tt> or Thread.MAX_PRIORITY.
* @param namePrefix
* a name prefix for this thread
* @return a thread factory based on given priority.
*/
public static ThreadFactory priorityThreadFactory(int priority, String namePrefix) {
final ThreadFactory f = DynamicExecutors.daemonThreadFactory(namePrefix);
final int p = priority;
return new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = f.newThread(r);
/* override default thread priority of Thread.NORM_PRIORITY */
if (p != Thread.NORM_PRIORITY)
t.setPriority(p);
return t;
}
};
}
/**
* Cannot instantiate.
*/
private DynamicExecutors() {
}
}

View File

@ -0,0 +1,164 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.jclouds.concurrent;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* An {@link ExecutorService} that executes each submitted task using one of
* possibly several pooled threads, normally configured using
* {@link DynamicExecutors} factory methods.
*
* @author kimchy (shay.banon)
*/
public class DynamicThreadPoolExecutor extends ThreadPoolExecutor {
/**
* number of threads that are actively executing tasks
*/
private final AtomicInteger activeCount = new AtomicInteger();
public DynamicThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
@Override public int getActiveCount() {
return activeCount.get();
}
@Override protected void beforeExecute(Thread t, Runnable r) {
activeCount.incrementAndGet();
}
@Override protected void afterExecute(Runnable r, Throwable t) {
activeCount.decrementAndGet();
}
/**
* Much like a {@link SynchronousQueue} which acts as a rendezvous channel. It
* is well suited for handoff designs, in which a tasks is only queued if there
* is an available thread to pick it up.
* <p/>
* This queue is correlated with a thread-pool, and allows insertions to the
* queue only if there is a free thread that can poll this task. Otherwise, the
* task is rejected and the decision is left up to one of the
* {@link RejectedExecutionHandler} policies:
* <ol>
* <li> {@link ForceQueuePolicy} - forces the queue to accept the rejected task. </li>
* <li> {@link TimedBlockingPolicy} - waits for a given time for the task to be
* executed.</li>
* </ol>
*
* @author kimchy (Shay Banon)
*/
public static class DynamicQueue<E> extends LinkedBlockingQueue<E> {
private static final long serialVersionUID = 1L;
/**
* The executor this Queue belongs to
*/
private transient ThreadPoolExecutor executor;
/**
* Creates a <tt>DynamicQueue</tt> with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public DynamicQueue() {
super();
}
/**
* Creates a <tt>DynamicQueue</tt> with the given (fixed) capacity.
*
* @param capacity the capacity of this queue.
*/
public DynamicQueue(int capacity) {
super(capacity);
}
/**
* Sets the executor this queue belongs to.
*/
public void setThreadPoolExecutor(ThreadPoolExecutor executor) {
this.executor = executor;
}
/**
* Inserts the specified element at the tail of this queue if there is at
* least one available thread to run the current task. If all pool threads
* are actively busy, it rejects the offer.
*
* @param o the element to add.
* @return <tt>true</tt> if it was possible to add the element to this
* queue, else <tt>false</tt>
* @see ThreadPoolExecutor#execute(Runnable)
*/
@Override
public boolean offer(E o) {
int allWorkingThreads = executor.getActiveCount() + super.size();
return allWorkingThreads < executor.getPoolSize() && super.offer(o);
}
}
/**
* A handler for rejected tasks that adds the specified element to this queue,
* waiting if necessary for space to become available.
*/
public static class ForceQueuePolicy implements RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
//should never happen since we never wait
throw new RejectedExecutionException(e);
}
}
}
/**
* A handler for rejected tasks that inserts the specified element into this
* queue, waiting if necessary up to the specified wait time for space to become
* available.
*/
public static class TimedBlockingPolicy implements RejectedExecutionHandler {
private final long waitTime;
/**
* @param waitTime wait time in milliseconds for space to become available.
*/
public TimedBlockingPolicy(long waitTime) {
this.waitTime = waitTime;
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
boolean successful = executor.getQueue().offer(r, waitTime, TimeUnit.MILLISECONDS);
if (!successful)
throw new RejectedExecutionException("Rejected execution after waiting "
+ waitTime + " ms for task [" + r.getClass() + "] to be executed.");
} catch (InterruptedException e) {
throw new RejectedExecutionException(e);
}
}
}
}

View File

@ -18,14 +18,13 @@
*/ */
package org.jclouds.concurrent.config; package org.jclouds.concurrent.config;
import static org.jclouds.concurrent.DynamicExecutors.newScalingThreadPool;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
@ -120,14 +119,14 @@ public class ExecutorServiceModule extends AbstractModule {
} }
@VisibleForTesting @VisibleForTesting
static ExecutorService newThreadPoolNamed(String name, int count) { static ExecutorService newThreadPoolNamed(String name, int maxCount) {
return count == 0 ? newCachedThreadPoolNamed(name) : newFixedThreadPoolNamed(name, count); return maxCount == 0 ? newCachedThreadPoolNamed(name) : newScalingThreadPoolNamed(name,
maxCount);
} }
@VisibleForTesting @VisibleForTesting
static ExecutorService newFixedThreadPoolNamed(String name, int maxCount) { static ExecutorService newScalingThreadPoolNamed(String name, int maxCount) {
return new ThreadPoolExecutor(maxCount, maxCount, 60L, TimeUnit.SECONDS, return newScalingThreadPool(0, maxCount, 60L * 1000, new NamingThreadFactory(name));
new LinkedBlockingQueue<Runnable>(), new NamingThreadFactory(name));
} }
} }

View File

@ -37,7 +37,8 @@ import com.google.inject.Module;
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test @Test
public class JavaUrlHttpCommandExecutorServiceIntegrationTest extends BaseHttpCommandExecutorServiceIntegrationTest { public class JavaUrlHttpCommandExecutorServiceIntegrationTest extends
BaseHttpCommandExecutorServiceIntegrationTest {
protected Module createConnectionModule() { protected Module createConnectionModule() {
return new JavaUrlHttpCommandExecutorServiceModule(); return new JavaUrlHttpCommandExecutorServiceModule();

View File

@ -57,4 +57,9 @@ public class ErrorResponse implements Comparable<ErrorResponse> {
public int compareTo(ErrorResponse o) { public int compareTo(ErrorResponse o) {
return message.compareTo(o.getMessage()); return message.compareTo(o.getMessage());
} }
@Override
public String toString() {
return "[errorCode=" + errorCode + ", message=" + message + "]";
}
} }

View File

@ -33,6 +33,7 @@ import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpErrorHandler; import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException; import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.AuthorizationException;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -55,7 +56,8 @@ public class GoGridErrorHandler implements HttpErrorHandler {
Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response.getContent()); Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response.getContent());
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 403: case 403:
exception = new HttpResponseException(command, response); exception = new AuthorizationException(command.getRequest(), errors != null ? errors
.toString() : response.getStatusLine());
break; break;
default: default:
exception = errors != null ? new GoGridResponseException(command, response, errors) exception = errors != null ? new GoGridResponseException(command, response, errors)

View File

@ -48,7 +48,7 @@ public class GoGridComputeServiceLiveTest extends BaseComputeServiceLiveTest {
assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64); assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64);
assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.CENTOS); assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.CENTOS);
assertEquals(defaultTemplate.getLocation().getId(), "SANFRANCISCO"); assertEquals(defaultTemplate.getLocation().getId(), "SANFRANCISCO");
assertEquals(defaultTemplate.getSize().getCores(), 1.0d); assertEquals(defaultTemplate.getSize().getCores(), 0.5d);
} }
@Override @Override

View File

@ -128,7 +128,7 @@ public class RimuHostingComputeServiceContextModule extends RimuHostingContextMo
@Provides @Provides
@Named("DEFAULT") @Named("DEFAULT")
protected TemplateBuilder provideTemplate(TemplateBuilder template) { protected TemplateBuilder provideTemplate(TemplateBuilder template) {
return template.sizeId("MIRO1B").osFamily(UBUNTU); return template.sizeId("MIRO1B").osFamily(UBUNTU).architecture(Architecture.X86_32);
} }
@Provides @Provides

View File

@ -20,16 +20,20 @@ package org.jclouds.rimuhosting.miro.functions;
import static org.jclouds.util.Utils.propagateOrNull; import static org.jclouds.util.Utils.propagateOrNull;
import com.google.common.base.Function; import java.lang.reflect.Type;
import com.google.gson.Gson; import java.util.Map;
import com.google.gson.reflect.TypeToken;
import org.jclouds.http.HttpResponseException;
import org.jclouds.rimuhosting.miro.domain.internal.RimuHostingResponse;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.lang.reflect.Type;
import java.util.Map; import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rimuhosting.miro.domain.internal.RimuHostingResponse;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/** /**
* On non 2xx we have an error. RimuHosting using the same json base object. * On non 2xx we have an error. RimuHosting using the same json base object.
@ -52,13 +56,16 @@ public class ParseRimuHostingException implements Function<Exception, Object> {
if (e instanceof HttpResponseException) { if (e instanceof HttpResponseException) {
HttpResponseException responseException = (HttpResponseException) e; HttpResponseException responseException = (HttpResponseException) e;
if (responseException.getContent() != null) { if (responseException.getContent() != null) {
Type setType = new TypeToken<Map<String, RimuHostingResponse>>() { Type setType = new TypeToken<Map<String, RimuHostingResponse>>() {
}.getType(); }.getType();
String test = responseException.getContent(); String test = responseException.getContent();
Map<String, RimuHostingResponse> responseMap = gson.fromJson(test, setType); Map<String, RimuHostingResponse> responseMap = gson.fromJson(test, setType);
throw new RuntimeException(responseMap.values().iterator().next().getErrorInfo() RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
.getErrorClass()); String errorClass = firstResponse.getErrorInfo().getErrorClass();
if (errorClass.equals("PermissionException"))
throw new AuthorizationException(responseException.getCommand().getRequest(),
firstResponse.getErrorInfo().getErrorMessage());
throw new RuntimeException(firstResponse.getErrorInfo().getErrorMessage(), e);
} }
} }
return propagateOrNull(e); return propagateOrNull(e);

View File

@ -43,7 +43,7 @@ public class RimuHostingComputeServiceLiveTest extends BaseComputeServiceLiveTes
@Test @Test
public void testTemplateBuilder() { public void testTemplateBuilder() {
Template defaultTemplate = client.templateBuilder().build(); Template defaultTemplate = client.templateBuilder().build();
assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64); assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_32);
assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.UBUNTU); assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.UBUNTU);
assertEquals(defaultTemplate.getLocation().getId(), "DCDALLAS"); assertEquals(defaultTemplate.getLocation().getId(), "DCDALLAS");
assertEquals(defaultTemplate.getSize().getProviderId(), "MIRO1B"); assertEquals(defaultTemplate.getSize().getProviderId(), "MIRO1B");

View File

@ -58,7 +58,7 @@ public class BlueLockVCloudRestClientModule extends VCloudRestClientModule {
@Override @Override
public boolean apply(NamedResource input) { public boolean apply(NamedResource input) {
return input.getName().endsWith("Public"); return input.getName().equals("Internal In and Out");
} }
})).getLocation(); })).getLocation();

View File

@ -126,17 +126,21 @@ public class BaseVCloudComputeClient implements VCloudComputeClient {
public void stop(String id) { public void stop(String id) {
VApp vApp = client.getVApp(id); VApp vApp = client.getVApp(id);
if (vApp.getStatus() != VAppStatus.OFF) { vApp = powerOffVAppIfDeployed(vApp);
logger.debug(">> powering off vApp(%s), current status: %s", vApp.getId(), vApp vApp = undeployVAppIfDeployed(vApp);
.getStatus()); boolean successful = deleteVApp(vApp);
Task task = client.powerOffVApp(vApp.getId()); logger.debug("<< deleted vApp(%s) completed(%s)", vApp.getId(), successful);
if (!taskTester.apply(task.getId())) {
throw new TaskException("powerOff", vApp, task);
} }
vApp = client.getVApp(id);
logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getId()); private boolean deleteVApp(VApp vApp) {
logger.debug(">> deleting vApp(%s)", vApp.getId());
client.deleteVApp(vApp.getId());
boolean successful = notFoundTester.apply(vApp);
return successful;
} }
if (vApp.getStatus() != VAppStatus.UNRESOLVED) {
private VApp undeployVAppIfDeployed(VApp vApp) {
if (vApp.getStatus().compareTo(VAppStatus.RESOLVED) > 0) {
logger logger
.debug(">> undeploying vApp(%s), current status: %s", vApp.getId(), vApp .debug(">> undeploying vApp(%s), current status: %s", vApp.getId(), vApp
.getStatus()); .getStatus());
@ -144,13 +148,24 @@ public class BaseVCloudComputeClient implements VCloudComputeClient {
if (!taskTester.apply(task.getId())) { if (!taskTester.apply(task.getId())) {
throw new TaskException("undeploy", vApp, task); throw new TaskException("undeploy", vApp, task);
} }
vApp = client.getVApp(id); vApp = client.getVApp(vApp.getId());
logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getId()); logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getId());
} }
logger.debug(">> deleting vApp(%s)", vApp.getId()); return vApp;
client.deleteVApp(id); }
boolean successful = notFoundTester.apply(vApp);
logger.debug("<< deleted vApp(%s) completed(%s)", vApp.getId(), successful); private VApp powerOffVAppIfDeployed(VApp vApp) {
if (vApp.getStatus().compareTo(VAppStatus.OFF) > 0) {
logger.debug(">> powering off vApp(%s), current status: %s", vApp.getId(), vApp
.getStatus());
Task task = client.powerOffVApp(vApp.getId());
if (!taskTester.apply(task.getId())) {
throw new TaskException("powerOff", vApp, task);
}
vApp = client.getVApp(vApp.getId());
logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getId());
}
return vApp;
} }
public static class TaskException extends VAppException { public static class TaskException extends VAppException {

View File

@ -22,8 +22,10 @@ import java.net.InetAddress;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.annotation.Resource;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.compute.BaseVCloudComputeClient; import org.jclouds.vcloud.compute.BaseVCloudComputeClient;
import org.jclouds.vcloud.domain.ResourceAllocation; import org.jclouds.vcloud.domain.ResourceAllocation;
import org.jclouds.vcloud.domain.ResourceType; import org.jclouds.vcloud.domain.ResourceType;
@ -41,8 +43,13 @@ import com.google.common.collect.Maps;
*/ */
@Singleton @Singleton
public class GetExtra implements Function<VApp, Map<String, String>> { public class GetExtra implements Function<VApp, Map<String, String>> {
@Resource
protected Logger logger = Logger.NULL;
public Map<String, String> apply(VApp vApp) { public Map<String, String> apply(VApp vApp) {
Map<String, String> extra = Maps.newHashMap(); Map<String, String> extra = Maps.newHashMap();
try {
extra.put("memory/mb", Iterables.getOnlyElement( extra.put("memory/mb", Iterables.getOnlyElement(
vApp.getResourceAllocationByType().get(ResourceType.MEMORY)).getVirtualQuantity() vApp.getResourceAllocationByType().get(ResourceType.MEMORY)).getVirtualQuantity()
+ ""); + "");
@ -61,6 +68,9 @@ public class GetExtra implements Function<VApp, Map<String, String>> {
.put(String.format("network/%s/ip", net.getKey()), net.getValue() .put(String.format("network/%s/ip", net.getKey()), net.getValue()
.getHostAddress()); .getHostAddress());
} }
} catch (Exception e) {
logger.error(e, "error getting extra data for vApp: %s", vApp);
}
return extra; return extra;
} }
} }

View File

@ -28,6 +28,7 @@ import org.jclouds.http.functions.BaseHandlerTest;
import org.jclouds.vcloud.VCloudMediaType; import org.jclouds.vcloud.VCloudMediaType;
import org.jclouds.vcloud.domain.Task; import org.jclouds.vcloud.domain.Task;
import org.jclouds.vcloud.domain.TaskStatus; import org.jclouds.vcloud.domain.TaskStatus;
import org.jclouds.vcloud.domain.VAppStatus;
import org.jclouds.vcloud.domain.internal.NamedResourceImpl; import org.jclouds.vcloud.domain.internal.NamedResourceImpl;
import org.jclouds.vcloud.domain.internal.TaskImpl; import org.jclouds.vcloud.domain.internal.TaskImpl;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
@ -71,6 +72,12 @@ public class TaskHandlerTest extends BaseHandlerTest {
} }
public void testStates() {
assert VAppStatus.ON.compareTo(VAppStatus.OFF) > 0;
assert VAppStatus.SUSPENDED.compareTo(VAppStatus.OFF) > 0;
assert VAppStatus.OFF.compareTo(VAppStatus.OFF) == 0;
}
public void testSelf() { public void testSelf() {
InputStream is = getClass().getResourceAsStream("/task-self.xml"); InputStream is = getClass().getResourceAsStream("/task-self.xml");

View File

@ -50,23 +50,4 @@ public class HostingDotComVCloudComputeServiceLiveTest extends VCloudComputeServ
assertEquals(defaultTemplate.getSize().getCores(), 1.0d); assertEquals(defaultTemplate.getSize().getCores(), 1.0d);
} }
// Takes too long
@Override
@Test(enabled = false)
public void testCreateTwoNodesWithRunScript() throws Exception {
super.testCreateTwoNodesWithRunScript();
}
@Override
@Test(enabled = false)
public void testGet() throws Exception {
super.testGet();
}
@Override
@Test(enabled = false)
public void testReboot() throws Exception {
super.testReboot();
}
} }