Github user andreaturli commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/723#discussion_r120736287
--- Diff:
locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/AbstractComputeServiceRegistry.java
---
@@ -0,0 +1,312 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.brooklyn.location.jclouds;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.brooklyn.util.JavaGroovyEquivalents.groovyTruth;
+import static
org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
+import static
org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY;
+
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.brooklyn.core.config.Sanitizer;
+import org.apache.brooklyn.core.location.cloud.CloudLocationConfig;
+import
org.apache.brooklyn.core.mgmt.persist.DeserializingJcloudsRenamesProvider;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.config.ConfigBag;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.time.Duration;
+import org.jclouds.Constants;
+import org.jclouds.ContextBuilder;
+import org.jclouds.azurecompute.arm.config.AzureComputeRateLimitModule;
+import org.jclouds.compute.ComputeService;
+import org.jclouds.compute.ComputeServiceContext;
+import org.jclouds.domain.Credentials;
+import org.jclouds.ec2.reference.EC2Constants;
+import org.jclouds.encryption.bouncycastle.config.BouncyCastleCryptoModule;
+import org.jclouds.location.reference.LocationConstants;
+import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
+import org.jclouds.sshj.config.SshjSshClientModule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Predicates;
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import com.google.inject.Module;
+
+public abstract class AbstractComputeServiceRegistry implements
ComputeServiceRegistry, JcloudsLocationConfig {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(AbstractComputeServiceRegistry.class);
+
+ private final Map<Map<?, ?>, ComputeService> cachedComputeServices =
new ConcurrentHashMap<>();
+
+ @Override
+ public ComputeService findComputeService(ConfigBag conf, boolean
allowReuse) {
+ Properties properties = new Properties();
+ setCommonProperties(conf, properties);
+
+ Iterable<Module> modules = getCommonModules();
+
+ // Enable aws-ec2 lazy image fetching, if given a specific
imageId; otherwise customize for specific owners; or all as a last resort
+ // See https://issues.apache.org/jira/browse/WHIRR-416
+ String provider = getProviderFromConfig(conf);
+ if ("aws-ec2".equals(provider)) {
+ setAWSEC2Properties(conf, properties);
+ } else if ("azurecompute-arm".equals(provider)) {
+ setAzureComputeArmProperties(conf, properties);
+ // jclouds 2.0.0 does not include the rate limit module for
Azure ARM. This quick fix enables this which will
+ // avoid provisioning to fail due to rate limit exceeded
+ // See https://issues.apache.org/jira/browse/JCLOUDS-1229
+ modules = ImmutableSet.<Module>builder()
+ .addAll(modules)
+ .add(new AzureComputeRateLimitModule())
+ .build();
+ }
+
+ addJCloudsProperties(conf, properties);
+ addEndpointProperty(conf, properties);
+
+ Supplier<ComputeService> computeServiceSupplier = allowReuse
+ ? new ReusableComputeServiceSupplier(conf, modules,
properties)
+ : new ComputeServiceSupplierImpl(conf, modules,
properties);
+
+ return computeServiceSupplier.get();
+ }
+
+ public abstract class ComputeServiceSupplier implements
Supplier<ComputeService> {
+
+ private final String provider;
+ private final ConfigBag conf;
+ private final Iterable<? extends Module> modules;
+ private final Properties properties;
+
+ private final Object createComputeServicesMutex = new Object();
+
+ public ComputeServiceSupplier(ConfigBag conf, Iterable<? extends
Module> modules, Properties properties) {
+ this.provider = getProviderFromConfig(conf);
+ this.conf = conf;
+ this.modules = modules;
+ this.properties = properties;
+ }
+
+ public ComputeService get() {
+ // Synchronizing to avoid deadlock from
sun.reflect.annotation.AnnotationType.
+ // See https://github.com/brooklyncentral/brooklyn/issues/974
+ synchronized (createComputeServicesMutex) {
+ ComputeServiceContext computeServiceContext =
ContextBuilder.newBuilder(provider)
+ .modules(modules)
+
.credentialsSupplier(AbstractComputeServiceRegistry.this.makeCredentials(conf))
+ .overrides(properties)
+ .build(ComputeServiceContext.class);
+ return computeServiceContext.getComputeService();
+ }
+ }
+
+ protected ConfigBag getConf() {
+ return conf;
+ }
+
+ protected Properties getProperties() {
+ return properties;
+ }
+ }
+
+ public class ComputeServiceSupplierImpl extends ComputeServiceSupplier
{
+
+ public ComputeServiceSupplierImpl(ConfigBag conf, Iterable<?
extends Module> modules, Properties properties) {
+ super(conf, modules, properties);
+ }
+ }
+
+ public class ReusableComputeServiceSupplier extends
ComputeServiceSupplier {
+
+ private Map<?, ?> cacheKey;
+
+ public ReusableComputeServiceSupplier(ConfigBag conf, Iterable<?
extends Module> modules, Properties properties) {
+ super(conf, modules, properties);
+ this.cacheKey = makeCacheKey();
+ }
+
+ @Override
+ public ComputeService get() {
+ ComputeService result = cachedComputeServices.get(cacheKey);
+ if (result != null) {
+ LOG.trace("jclouds ComputeService cache hit for compute
service, for " + Sanitizer.sanitize(getProperties()));
+ return result;
+ }
+ LOG.debug("jclouds ComputeService cache miss for compute
service, creating, for " + Sanitizer.sanitize(getProperties()));
+ final ComputeService computeService = super.get();
+ synchronized (cachedComputeServices) {
+ result = cachedComputeServices.get(cacheKey);
+ if (result != null) {
+ LOG.debug("jclouds ComputeService cache recovery for
compute service, for " + Sanitizer.sanitize(cacheKey));
+ //keep the old one, discard the new one
+ computeService.getContext().close();
+ return result;
+ }
+ LOG.debug("jclouds ComputeService created " +
computeService + ", adding to cache, for " +
Sanitizer.sanitize(getProperties()));
+ cachedComputeServices.put(cacheKey, computeService);
+ }
+ return result;
+ }
+
+ private Map<?, ?> makeCacheKey() {
+ String provider = getProviderFromConfig(getConf());
+ String identity =
checkNotNull(getConf().get(CloudLocationConfig.ACCESS_IDENTITY), "identity must
not be null");
+ String credential =
checkNotNull(getConf().get(CloudLocationConfig.ACCESS_CREDENTIAL), "credential
must not be null");
+ String endpoint =
getProperties().getProperty(Constants.PROPERTY_ENDPOINT);
+ return MutableMap.builder()
+ .putAll(getProperties())
+ .put("provider", provider)
+ .put("identity", identity)
+ .put("credential", credential)
+ .putIfNotNull("endpoint", endpoint)
+ .build()
+ .asUnmodifiable();
+ }
+ }
+
+ protected String getProviderFromConfig(ConfigBag conf) {
+ String rawProvider = checkNotNull(conf.get(CLOUD_PROVIDER),
"provider must not be null");
+ return
DeserializingJcloudsRenamesProvider.INSTANCE.applyJcloudsRenames(rawProvider);
+ }
+
+ private String addEndpointProperty(ConfigBag conf, Properties
properties) {
--- End diff --
[minor] maybe this method is can be splitted into:
- getEndpointFromConfig(conf)
- setEndpoint(properties)
?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---