use of io.fabric8.insight.metrics.model.Request in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testDownload.
private void testDownload(Handler serverHandler) throws Exception {
final String old = System.getProperty("karaf.data");
System.setProperty("karaf.data", new File("target").getCanonicalPath());
FileUtils.deleteDirectory(new File("target/tmp"));
Server server = new Server(0);
server.setHandler(serverHandler);
server.start();
try {
int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central");
RuntimeProperties props = new MockRuntimeProperties();
// TODO: local repo should point to target/tmp
MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
AsyncContext context = EasyMock.createMock(AsyncContext.class);
EasyMock.makeThreadSafe(context, true);
final Map<String, Object> attributes = new HashMap<>();
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
@Override
public void setAttribute(String name, Object o) {
attributes.put(name, o);
}
};
EasyMock.makeThreadSafe(request, true);
EasyMock.expect(request.getMethod()).andReturn("GET");
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
EasyMock.makeThreadSafe(response, true);
response.setStatus(EasyMock.anyInt());
EasyMock.expectLastCall();
response.setContentLength(EasyMock.anyInt());
EasyMock.expectLastCall();
response.setContentType((String) EasyMock.anyObject());
EasyMock.expectLastCall();
response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
EasyMock.expectLastCall();
response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchDispatch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchDispatch.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.start();
servlet.doGet(requestWrapper, response);
latchDispatch.await();
EasyMock.verify(request, response, context);
//
// Subsequent call
//
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ServletOutputStream outputStream = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
};
while (true) {
long size = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".size");
long pos = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".position");
ByteBuffer buffer = (ByteBuffer) attributes.get(ByteBuffer.class.getName());
if (pos + buffer.position() >= size) {
break;
}
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final CountDownLatch latch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latch.countDown();
return null;
}
});
EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
latch.await();
EasyMock.verify(request, response, context);
}
//
// Last calls
//
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final CountDownLatch latchComplete = new CountDownLatch(1);
context.complete();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchComplete.countDown();
return null;
}
});
EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
response.flushBuffer();
EasyMock.expectLastCall();
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
latchComplete.await();
EasyMock.verify(request, response, context);
Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());
} finally {
server.stop();
if (old != null) {
System.setProperty("karaf.data", old);
}
}
}
use of io.fabric8.insight.metrics.model.Request in project fabric8 by jboss-fuse.
the class OpenShiftAutoScaler method createContainers.
@Override
public void createContainers(AutoScaleRequest request) throws Exception {
int count = request.getDelta();
String profile = request.getProfile();
String version = request.getVersion();
FabricService fabricService = request.getFabricService();
CreateOpenshiftContainerOptions.Builder builder = null;
if (fabricService != null) {
builder = createAutoScaleOptions(fabricService);
}
if (builder != null) {
// TODO this is actually generic to all providers! :)
for (int i = 0; i < count; i++) {
Container[] containers = fabricService.getContainers();
final CreateOpenshiftContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
NameValidator openShiftValidator = containerProvider.createNameValidator(configuredBuilder.build());
NameValidator fabricNameValidator = Containers.createNameValidator(fabricService.getContainers());
NameValidator nameValidator = Containers.joinNameValidators(openShiftValidator, fabricNameValidator);
String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator);
CreateOpenshiftContainerOptions options = configuredBuilder.name(name).build();
LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
fabricService.createContainers(options);
}
} else {
LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration");
}
}
use of io.fabric8.insight.metrics.model.Request in project fabric8 by jboss-fuse.
the class SshAutoScaler method chooseHostContainerOptions.
/**
* This method is public for easier testing
*/
public static CreateSshContainerOptions.Builder chooseHostContainerOptions(AutoScaleRequest request, HostProfileCounter hostProfileCounter) {
CreateSshContainerOptions.Builder builder = CreateSshContainerOptions.builder();
FabricRequirements requirements = request.getFabricRequirements();
ProfileRequirements profileRequirements = request.getProfileRequirements();
SshScalingRequirements sshScalingRequirements = profileRequirements.getSshScalingRequirements();
List<SshHostConfiguration> hosts = requirements.getSshHosts();
SortedSet<LoadSortedHostConfiguration<SshHostConfiguration>> sortedHostConfigurations = AutoScalers.filterHosts(profileRequirements, sshScalingRequirements, hostProfileCounter, hosts);
SshHostConfiguration sshHostConfig = null;
if (!sortedHostConfigurations.isEmpty()) {
LoadSortedHostConfiguration<SshHostConfiguration> first = sortedHostConfigurations.first();
sshHostConfig = first.getConfiguration();
}
if (sshHostConfig == null) {
LOG.warn("Could not create version " + request.getVersion() + " profile " + request.getProfile() + " as no matching hosts could be found for " + sshScalingRequirements);
request.getProfileAutoScaleStatus().noSuitableHost("" + sshScalingRequirements);
return null;
}
builder.configure(sshHostConfig, requirements, profileRequirements);
return builder;
}
use of io.fabric8.insight.metrics.model.Request in project fabric8 by jboss-fuse.
the class SshAutoScaler method createContainers.
@Override
public void createContainers(AutoScaleRequest request) throws Exception {
int count = request.getDelta();
String profile = request.getProfile();
String version = request.getVersion();
FabricService fabricService = request.getFabricService();
Container[] containers = fabricService.getContainers();
FabricRequirements requirements = request.getFabricRequirements();
List<? extends HostConfiguration> hostConfigurations = requirements.getSshHosts();
HostProfileCounter hostProfileCounter = new HostProfileCounter();
AutoScalers.createHostToProfileScaleMap(hostProfileCounter, hostConfigurations, containers);
for (int i = 0; i < count; i++) {
CreateSshContainerOptions.Builder builder = null;
NameValidator nameValidator = Containers.createNameValidator(containers);
String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator);
if (fabricService != null) {
builder = createAutoScaleOptions(request, fabricService, hostProfileCounter);
}
if (builder != null) {
final CreateSshContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
CreateSshContainerOptions options = configuredBuilder.name(name).build();
LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
fabricService.createContainers(options);
}
}
}
use of io.fabric8.insight.metrics.model.Request in project fabric8 by jboss-fuse.
the class DetectingGateway method route.
public void route(final SocketWrapper socket, ConnectionParameters params, final Buffer received) {
NetClient client = null;
if (params.protocolVirtualHost == null) {
params.protocolVirtualHost = defaultVirtualHost;
}
HashSet<String> schemes = new HashSet<String>(Arrays.asList(params.protocolSchemes));
if (params.protocolVirtualHost != null) {
List<ServiceDetails> services = serviceMap.getServices(params.protocolVirtualHost);
// Lets try again with the defaultVirtualHost
if (services.isEmpty() && !params.protocolVirtualHost.equals(defaultVirtualHost)) {
params.protocolVirtualHost = defaultVirtualHost;
services = serviceMap.getServices(params.protocolVirtualHost);
}
LOG.debug(String.format("%d services match the virtual host", services.size()));
if (!services.isEmpty()) {
ClientRequestFacade clientRequestFacade = clientRequestFacadeFactory.create(socket, params);
ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, clientRequestFacade);
if (serviceDetails != null) {
List<String> urlStrings = serviceDetails.getServices();
LOG.debug("Selected service exposes the following URLS: {}", urlStrings);
for (String urlString : urlStrings) {
if (Strings.notEmpty(urlString)) {
// lets create a client for this request...
try {
URI uri = new URI(urlString);
// URL url = new URL(urlString);
String urlProtocol = uri.getScheme();
if (schemes.contains(urlProtocol)) {
if (!socket.remoteAddress().toString().equals(clientRequestFacade.getClientRequestKey())) {
LOG.info(String.format("Connecting client from '%s' (with key '%s') requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), clientRequestFacade.getClientRequestKey(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
} else {
LOG.info(String.format("Connecting client from '%s' requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
}
client = createClient(params, socket, uri, received);
break;
}
} catch (URISyntaxException e) {
LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
}
}
}
}
}
}
if (client == null) {
// failed to route
handleConnectFailure(socket, String.format("No endpoint available for virtual host '%s' and protocol %s", params.protocolVirtualHost, params.protocol));
}
}
Aggregations