mirror of https://github.com/apache/jclouds.git
Issue 260: fixed executors per kimchy's advice; also fixed regression in a lot of live tests
This commit is contained in:
parent
d3d36dc879
commit
c8cd0b0f2e
|
@ -51,6 +51,8 @@ import org.testng.annotations.Test;
|
|||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.inject.Module;
|
||||
|
||||
/**
|
||||
|
@ -117,7 +119,7 @@ public class AtmosStorageClientLiveTest {
|
|||
String uid = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user");
|
||||
String key = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key");
|
||||
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
|
||||
.getProviderSpecificContext();
|
||||
connection = context.getApi();
|
||||
|
@ -166,18 +168,13 @@ public class AtmosStorageClientLiveTest {
|
|||
createOrReplaceObject("object4", "here is my data!", "meta-value1");
|
||||
BoundedSet<? extends DirectoryEntry> r2 = connection.listDirectory(privateDirectory,
|
||||
ListOptions.Builder.limit(1));
|
||||
// test bug exists:
|
||||
assertEquals(r2.size(), 3);
|
||||
// assertEquals(r2.size(), 1);
|
||||
// assert r2.getToken() != null;
|
||||
// assertEquals(r2.last().getObjectName(),"object2");
|
||||
// r2 = connection.listDirectory(privateDirectory,
|
||||
// ListOptions.Builder.token(r2.getToken())).get(10,
|
||||
// TimeUnit.SECONDS);
|
||||
// assertEquals(r2.size(), 2);
|
||||
// assert r2.getToken() == null;
|
||||
// assertEquals(r2.last().getObjectName(),"object4");
|
||||
|
||||
assertEquals(r2.size(), 1);
|
||||
assert r2.getToken() != null;
|
||||
assertEquals(Iterables.getLast(Sets.newTreeSet(r2)).getObjectName(), "object2");
|
||||
r2 = connection.listDirectory(privateDirectory, ListOptions.Builder.token(r2.getToken()));
|
||||
assertEquals(r2.size(), 2);
|
||||
assert r2.getToken() == null;
|
||||
assertEquals(Iterables.getLast(Sets.newTreeSet(r2)).getObjectName(), "object4");
|
||||
}
|
||||
|
||||
@Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testListOptions" })
|
||||
|
|
|
@ -80,6 +80,8 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
|
|||
|
||||
String startedId = null;
|
||||
try {
|
||||
cleanupExtendedStuff(securityGroupClient, keyPairClient, tag);
|
||||
|
||||
// create a security group that allows ssh in so that our scripts later will work
|
||||
securityGroupClient.createSecurityGroupInRegion(null, tag, tag);
|
||||
securityGroupClient.authorizeSecurityGroupIngressInRegion(null, tag, IpProtocol.TCP, 22,
|
||||
|
@ -142,6 +144,8 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
|
|||
|
||||
String startedId = null;
|
||||
try {
|
||||
cleanupExtendedStuff(securityGroupClient, keyPairClient, tag);
|
||||
|
||||
// create the security group
|
||||
securityGroupClient.createSecurityGroupInRegion(null, tag, tag);
|
||||
|
||||
|
|
|
@ -132,7 +132,7 @@ public class NodeMetadataImpl extends ComputeMetadataImpl implements NodeMetadat
|
|||
+ getName() + ", location=" + getLocation() + ", uri=" + getUri() + ", image="
|
||||
+ getImage() + ", userMetadata=" + getUserMetadata() + ", state=" + getState()
|
||||
+ ", privateAddresses=" + privateAddresses + ", publicAddresses=" + publicAddresses
|
||||
+ "]";
|
||||
+ ", extra=" + getExtra() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -165,9 +165,9 @@ public abstract class BaseComputeServiceLiveTest {
|
|||
@Test(enabled = true, dependsOnMethods = "testImagesCache")
|
||||
public void testTemplateMatch() throws Exception {
|
||||
template = buildTemplate(client.templateBuilder());
|
||||
Template toMatch = client.templateBuilder().imageId(template.getImage().getProviderId())
|
||||
Template toMatch = client.templateBuilder().imageId(template.getImage().getId())
|
||||
.build();
|
||||
assertEquals(toMatch, template);
|
||||
assertEquals(toMatch.getImage(), template.getImage());
|
||||
}
|
||||
|
||||
@Test(enabled = true, dependsOnMethods = "testTemplateMatch")
|
||||
|
|
|
@ -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() {
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,14 +18,13 @@
|
|||
*/
|
||||
package org.jclouds.concurrent.config;
|
||||
|
||||
import static org.jclouds.concurrent.DynamicExecutors.newScalingThreadPool;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
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.inject.Named;
|
||||
|
@ -120,14 +119,14 @@ public class ExecutorServiceModule extends AbstractModule {
|
|||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static ExecutorService newThreadPoolNamed(String name, int count) {
|
||||
return count == 0 ? newCachedThreadPoolNamed(name) : newFixedThreadPoolNamed(name, count);
|
||||
static ExecutorService newThreadPoolNamed(String name, int maxCount) {
|
||||
return maxCount == 0 ? newCachedThreadPoolNamed(name) : newScalingThreadPoolNamed(name,
|
||||
maxCount);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static ExecutorService newFixedThreadPoolNamed(String name, int maxCount) {
|
||||
return new ThreadPoolExecutor(maxCount, maxCount, 60L, TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<Runnable>(), new NamingThreadFactory(name));
|
||||
static ExecutorService newScalingThreadPoolNamed(String name, int maxCount) {
|
||||
return newScalingThreadPool(0, maxCount, 60L * 1000, new NamingThreadFactory(name));
|
||||
}
|
||||
|
||||
}
|
|
@ -37,7 +37,8 @@ import com.google.inject.Module;
|
|||
* @author Adrian Cole
|
||||
*/
|
||||
@Test
|
||||
public class JavaUrlHttpCommandExecutorServiceIntegrationTest extends BaseHttpCommandExecutorServiceIntegrationTest {
|
||||
public class JavaUrlHttpCommandExecutorServiceIntegrationTest extends
|
||||
BaseHttpCommandExecutorServiceIntegrationTest {
|
||||
|
||||
protected Module createConnectionModule() {
|
||||
return new JavaUrlHttpCommandExecutorServiceModule();
|
||||
|
|
|
@ -30,31 +30,36 @@ import com.google.gson.annotations.SerializedName;
|
|||
*/
|
||||
public class ErrorResponse implements Comparable<ErrorResponse> {
|
||||
|
||||
private String message;
|
||||
@SerializedName("errorcode")
|
||||
private String errorCode;
|
||||
private String message;
|
||||
@SerializedName("errorcode")
|
||||
private String errorCode;
|
||||
|
||||
/**
|
||||
* A no-args constructor is required for deserialization
|
||||
*/
|
||||
public ErrorResponse() {
|
||||
}
|
||||
/**
|
||||
* A no-args constructor is required for deserialization
|
||||
*/
|
||||
public ErrorResponse() {
|
||||
}
|
||||
|
||||
public ErrorResponse(String message, String errorCode) {
|
||||
this.message = message;
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
public ErrorResponse(String message, String errorCode) {
|
||||
this.message = message;
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
public String getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ErrorResponse o) {
|
||||
return message.compareTo(o.getMessage());
|
||||
}
|
||||
@Override
|
||||
public int compareTo(ErrorResponse o) {
|
||||
return message.compareTo(o.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[errorCode=" + errorCode + ", message=" + message + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.jclouds.http.HttpCommand;
|
|||
import org.jclouds.http.HttpErrorHandler;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.HttpResponseException;
|
||||
import org.jclouds.rest.AuthorizationException;
|
||||
|
||||
import com.google.common.io.Closeables;
|
||||
import com.google.inject.Inject;
|
||||
|
@ -55,7 +56,8 @@ public class GoGridErrorHandler implements HttpErrorHandler {
|
|||
Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response.getContent());
|
||||
switch (response.getStatusCode()) {
|
||||
case 403:
|
||||
exception = new HttpResponseException(command, response);
|
||||
exception = new AuthorizationException(command.getRequest(), errors != null ? errors
|
||||
.toString() : response.getStatusLine());
|
||||
break;
|
||||
default:
|
||||
exception = errors != null ? new GoGridResponseException(command, response, errors)
|
||||
|
|
|
@ -48,7 +48,7 @@ public class GoGridComputeServiceLiveTest extends BaseComputeServiceLiveTest {
|
|||
assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64);
|
||||
assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.CENTOS);
|
||||
assertEquals(defaultTemplate.getLocation().getId(), "SANFRANCISCO");
|
||||
assertEquals(defaultTemplate.getSize().getCores(), 1.0d);
|
||||
assertEquals(defaultTemplate.getSize().getCores(), 0.5d);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -128,7 +128,7 @@ public class RimuHostingComputeServiceContextModule extends RimuHostingContextMo
|
|||
@Provides
|
||||
@Named("DEFAULT")
|
||||
protected TemplateBuilder provideTemplate(TemplateBuilder template) {
|
||||
return template.sizeId("MIRO1B").osFamily(UBUNTU);
|
||||
return template.sizeId("MIRO1B").osFamily(UBUNTU).architecture(Architecture.X86_32);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
|
@ -20,16 +20,20 @@ package org.jclouds.rimuhosting.miro.functions;
|
|||
|
||||
import static org.jclouds.util.Utils.propagateOrNull;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.jclouds.http.HttpResponseException;
|
||||
import org.jclouds.rimuhosting.miro.domain.internal.RimuHostingResponse;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
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.
|
||||
|
@ -52,13 +56,16 @@ public class ParseRimuHostingException implements Function<Exception, Object> {
|
|||
if (e instanceof HttpResponseException) {
|
||||
HttpResponseException responseException = (HttpResponseException) e;
|
||||
if (responseException.getContent() != null) {
|
||||
|
||||
Type setType = new TypeToken<Map<String, RimuHostingResponse>>() {
|
||||
}.getType();
|
||||
String test = responseException.getContent();
|
||||
Map<String, RimuHostingResponse> responseMap = gson.fromJson(test, setType);
|
||||
throw new RuntimeException(responseMap.values().iterator().next().getErrorInfo()
|
||||
.getErrorClass());
|
||||
RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
|
||||
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);
|
||||
|
|
|
@ -43,7 +43,7 @@ public class RimuHostingComputeServiceLiveTest extends BaseComputeServiceLiveTes
|
|||
@Test
|
||||
public void testTemplateBuilder() {
|
||||
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.getLocation().getId(), "DCDALLAS");
|
||||
assertEquals(defaultTemplate.getSize().getProviderId(), "MIRO1B");
|
||||
|
|
|
@ -58,7 +58,7 @@ public class BlueLockVCloudRestClientModule extends VCloudRestClientModule {
|
|||
|
||||
@Override
|
||||
public boolean apply(NamedResource input) {
|
||||
return input.getName().endsWith("Public");
|
||||
return input.getName().equals("Internal In and Out");
|
||||
}
|
||||
|
||||
})).getLocation();
|
||||
|
|
|
@ -126,17 +126,21 @@ public class BaseVCloudComputeClient implements VCloudComputeClient {
|
|||
|
||||
public void stop(String id) {
|
||||
VApp vApp = client.getVApp(id);
|
||||
if (vApp.getStatus() != VAppStatus.OFF) {
|
||||
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(id);
|
||||
logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getId());
|
||||
}
|
||||
if (vApp.getStatus() != VAppStatus.UNRESOLVED) {
|
||||
vApp = powerOffVAppIfDeployed(vApp);
|
||||
vApp = undeployVAppIfDeployed(vApp);
|
||||
boolean successful = deleteVApp(vApp);
|
||||
logger.debug("<< deleted vApp(%s) completed(%s)", vApp.getId(), successful);
|
||||
}
|
||||
|
||||
private boolean deleteVApp(VApp vApp) {
|
||||
logger.debug(">> deleting vApp(%s)", vApp.getId());
|
||||
client.deleteVApp(vApp.getId());
|
||||
boolean successful = notFoundTester.apply(vApp);
|
||||
return successful;
|
||||
}
|
||||
|
||||
private VApp undeployVAppIfDeployed(VApp vApp) {
|
||||
if (vApp.getStatus().compareTo(VAppStatus.RESOLVED) > 0) {
|
||||
logger
|
||||
.debug(">> undeploying vApp(%s), current status: %s", vApp.getId(), vApp
|
||||
.getStatus());
|
||||
|
@ -144,13 +148,24 @@ public class BaseVCloudComputeClient implements VCloudComputeClient {
|
|||
if (!taskTester.apply(task.getId())) {
|
||||
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(">> deleting vApp(%s)", vApp.getId());
|
||||
client.deleteVApp(id);
|
||||
boolean successful = notFoundTester.apply(vApp);
|
||||
logger.debug("<< deleted vApp(%s) completed(%s)", vApp.getId(), successful);
|
||||
return vApp;
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -22,8 +22,10 @@ import java.net.InetAddress;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.vcloud.compute.BaseVCloudComputeClient;
|
||||
import org.jclouds.vcloud.domain.ResourceAllocation;
|
||||
import org.jclouds.vcloud.domain.ResourceType;
|
||||
|
@ -41,25 +43,33 @@ import com.google.common.collect.Maps;
|
|||
*/
|
||||
@Singleton
|
||||
public class GetExtra implements Function<VApp, Map<String, String>> {
|
||||
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
public Map<String, String> apply(VApp vApp) {
|
||||
Map<String, String> extra = Maps.newHashMap();
|
||||
extra.put("memory/mb", Iterables.getOnlyElement(
|
||||
vApp.getResourceAllocationByType().get(ResourceType.MEMORY)).getVirtualQuantity()
|
||||
+ "");
|
||||
extra.put("processor/count", Iterables.getOnlyElement(
|
||||
vApp.getResourceAllocationByType().get(ResourceType.PROCESSOR))
|
||||
.getVirtualQuantity()
|
||||
+ "");
|
||||
for (ResourceAllocation disk : vApp.getResourceAllocationByType().get(
|
||||
ResourceType.PROCESSOR)) {
|
||||
extra.put(String.format("disk_drive/%s/kb", disk.getId()), disk.getVirtualQuantity()
|
||||
try {
|
||||
extra.put("memory/mb", Iterables.getOnlyElement(
|
||||
vApp.getResourceAllocationByType().get(ResourceType.MEMORY)).getVirtualQuantity()
|
||||
+ "");
|
||||
}
|
||||
extra.put("processor/count", Iterables.getOnlyElement(
|
||||
vApp.getResourceAllocationByType().get(ResourceType.PROCESSOR))
|
||||
.getVirtualQuantity()
|
||||
+ "");
|
||||
for (ResourceAllocation disk : vApp.getResourceAllocationByType().get(
|
||||
ResourceType.PROCESSOR)) {
|
||||
extra.put(String.format("disk_drive/%s/kb", disk.getId()), disk.getVirtualQuantity()
|
||||
+ "");
|
||||
}
|
||||
|
||||
for (Entry<String, InetAddress> net : vApp.getNetworkToAddresses().entries()) {
|
||||
extra
|
||||
.put(String.format("network/%s/ip", net.getKey()), net.getValue()
|
||||
.getHostAddress());
|
||||
for (Entry<String, InetAddress> net : vApp.getNetworkToAddresses().entries()) {
|
||||
extra
|
||||
.put(String.format("network/%s/ip", net.getKey()), net.getValue()
|
||||
.getHostAddress());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "error getting extra data for vApp: %s", vApp);
|
||||
}
|
||||
return extra;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ import org.jclouds.http.functions.BaseHandlerTest;
|
|||
import org.jclouds.vcloud.VCloudMediaType;
|
||||
import org.jclouds.vcloud.domain.Task;
|
||||
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.TaskImpl;
|
||||
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() {
|
||||
InputStream is = getClass().getResourceAsStream("/task-self.xml");
|
||||
|
||||
|
|
|
@ -50,23 +50,4 @@ public class HostingDotComVCloudComputeServiceLiveTest extends VCloudComputeServ
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue