Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4DE811925F for ; Thu, 24 Mar 2016 14:41:37 +0000 (UTC) Received: (qmail 31429 invoked by uid 500); 24 Mar 2016 14:41:37 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 31401 invoked by uid 500); 24 Mar 2016 14:41:37 -0000 Mailing-List: contact dev-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list dev@brooklyn.apache.org Received: (qmail 31379 invoked by uid 99); 24 Mar 2016 14:41:36 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Mar 2016 14:41:36 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D5071DFC70; Thu, 24 Mar 2016 14:41:36 +0000 (UTC) From: grkvlt To: dev@brooklyn.apache.org Reply-To: dev@brooklyn.apache.org References: In-Reply-To: Subject: [GitHub] brooklyn-server pull request: SoftLayer VLAN customizer Content-Type: text/plain Message-Id: <20160324144136.D5071DFC70@git1-us-west.apache.org> Date: Thu, 24 Mar 2016 14:41:36 +0000 (UTC) Github user grkvlt commented on a diff in the pull request: https://github.com/apache/brooklyn-server/pull/53#discussion_r57326425 --- Diff: locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/softlayer/SoftLayerSameVlanLocationCustomizer.java --- @@ -0,0 +1,398 @@ +/* + * 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.softlayer; + +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import javax.annotation.concurrent.ThreadSafe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableMap; +import com.google.common.reflect.TypeToken; +import com.google.common.util.concurrent.Uninterruptibles; + +import org.jclouds.compute.ComputeService; +import org.jclouds.compute.options.TemplateOptions; +import org.jclouds.softlayer.SoftLayerApi; +import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions; +import org.jclouds.softlayer.domain.VirtualGuest; +import org.jclouds.softlayer.features.VirtualGuestApi; +import org.jclouds.softlayer.reference.SoftLayerConstants; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.sensor.AttributeSensor; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.location.LocationConfigKeys; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.location.jclouds.BasicJcloudsLocationCustomizer; +import org.apache.brooklyn.location.jclouds.JcloudsLocation; +import org.apache.brooklyn.location.jclouds.JcloudsLocationCustomizer; +import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation; +import org.apache.brooklyn.location.jclouds.JcloudsSshMachineLocation; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.core.flags.SetFromFlag; +import org.apache.brooklyn.util.core.task.Tasks; +import org.apache.brooklyn.util.text.Strings; +import org.apache.brooklyn.util.time.Duration; + +/** + * Customizes {@link JcloudsSshMachineLocation machines} in SoftLayer to use + * the same VLANs across an application, or other named scope + *

+ * Define the scope by setting the {@link #SCOPE_UID scopeUid} ({@code softlayer.vlan.scopeUid}) + * option. If unset or blank, will default to the {@link Entity#getApplicationId() application ID} + * of the entity being deployed on the machine. Set {@link #SCOPE_TIMEOUT scopeTimeout} + * ({@code softlayer.vlan.timeout}) to change the length of time the customizer + * will wait for VLAN information; normally ten minutes. + *

+ * The VLAN IDs and latches are stored as {@link ConfigKey configuration} on + * the {@link JcloudsLocation location} provisioning the VMs, in a map keyed + * on the scope. + */ +@ThreadSafe +public class SoftLayerSameVlanLocationCustomizer extends BasicJcloudsLocationCustomizer { + + private static final Logger LOG = LoggerFactory.getLogger(SoftLayerSameVlanLocationCustomizer.class); + + @SetFromFlag("scopeUid") + public static final ConfigKey SCOPE_UID = ConfigKeys.newStringConfigKey( + "softlayer.vlan.scopeUid", + "The unique identifier for a Softlayer location scope that will have VMs created in the same VLAN"); + + @SetFromFlag("scopeTimeout") + public static final ConfigKey SCOPE_TIMEOUT = ConfigKeys.newDurationConfigKey( + "softlayer.vlan.timeout", + "The length of time to wait for a Softlayer VLAN ID", + Duration.minutes(15)); + + public static final ConfigKey> COUNTDOWN_LATCH_MAP = ConfigKeys.newConfigKey( + new TypeToken>() { }, + "softLayerSameVlanLocationCustomizer.map.latches", + "A mapping from scope identifiers to CountDownLatches; used to synchronize threads", + MutableMap.of()); + public static final ConfigKey> PUBLIC_VLAN_ID_MAP = ConfigKeys.newConfigKey( + new TypeToken>() { }, + "softLayerSameVlanLocationCustomizer.map.publicVlanIds", + "A mapping from scope identifiers to public VLAN numbers", + MutableMap.of()); + public static final ConfigKey> PRIVATE_VLAN_ID_MAP = ConfigKeys.newConfigKey( + new TypeToken>() { }, + "softLayerSameVlanLocationCustomizer.map.privateVlanIds", + "A mapping from scope identifiers to private VLAN numbers", + MutableMap.of()); + + public static final AttributeSensor PUBLIC_VLAN_ID = Sensors.newIntegerSensor( + "softLayer.vlan.publicId", "The public VLAN ID for this entity"); + public static final AttributeSensor PRIVATE_VLAN_ID = Sensors.newIntegerSensor( + "softLayer.vlan.privateId", "The private VLAN ID for this entity"); + + /* Flags passed in on object creation. */ + private final Map flags; + + /* Lock object for synchronization. */ + private static final Object lock = new Object[0]; + + /** Convenience creation method. */ + public static SoftLayerSameVlanLocationCustomizer forScope(String scopeUid) { + SoftLayerSameVlanLocationCustomizer customizer = new SoftLayerSameVlanLocationCustomizer(ImmutableMap.of(SCOPE_UID.getName(), scopeUid)); + return customizer; + } + + public SoftLayerSameVlanLocationCustomizer() { + this(ImmutableMap.of()); + } + + public SoftLayerSameVlanLocationCustomizer(Map flags) { + this.flags = ImmutableMap.copyOf(flags); + } + + /** + * Used to obtain the VLANs being used by the first created {@link JcloudsMachineLocation}. + * + * @see {@link JcloudsLocationCustomizer#customize(JcloudsLocation, ComputeService, JcloudsMachineLocation)} + */ + @Override + public void customize(JcloudsLocation location, ComputeService computeService, JcloudsMachineLocation machine) { + // Check we are customising a SoftLayer location + String provider = location.getProvider(); + if (!(provider.equals(SoftLayerConstants.SOFTLAYER_PROVIDER_NAME))) { + String message = String.format("Invalid location provider: %s", provider); + LOG.warn(message); + throw new IllegalArgumentException(message); + } + + // Lookup the latch for this scope + String scopeUid = getScopeUid(location); + CountDownLatch latch = null; + synchronized (lock) { + latch = lookupCountDownLatch(location, scopeUid); + if (latch == null) { + throw new IllegalStateException("No latch available for scope: " + scopeUid); + } + } + + try { + // Check if VLAN IDs have been set for this scope + LOG.debug("Looking up saved VLAN details {}", scopeUid); + Integer publicVlanId = lookupPublicVlanId(location, scopeUid); + Integer privateVlanId = lookupPrivateVlanId(location, scopeUid); + if (privateVlanId != null || publicVlanId != null) { + LOG.debug("SoftLayer VLANs private {} and public {} already configured for scope: {}", + new Object[] { privateVlanId, publicVlanId, scopeUid }); + return; + } + + // No VLAN info yet known, we are probably the first VM and we should + // set the VLAN info for others to then learn about. + + // Ask SoftLayer API for the VLAN details for this VM + LOG.debug("Requesting VLAN details from API for scope: {}", scopeUid); + VirtualGuestApi api = computeService.getContext().unwrapApi(SoftLayerApi.class).getVirtualGuestApi(); + Long serverId = Long.parseLong(machine.getJcloudsId()); + VirtualGuest guest = api.getVirtualGuestFiltered(serverId, + "primaryNetworkComponent;" + + "primaryNetworkComponent.networkVlan;" + + "primaryBackendNetworkComponent;" + + "primaryBackendNetworkComponent.networkVlan"); + publicVlanId = guest.getPrimaryNetworkComponent().getNetworkVlan().getId(); + privateVlanId = guest.getPrimaryBackendNetworkComponent().getNetworkVlan().getId(); + + // Save the VLAN ids + LOG.debug("Saving VLAN details private {} and public {} for scope: {}", + new Object[] { privateVlanId, publicVlanId, scopeUid }); + savePublicVlanId(location, scopeUid, publicVlanId); + savePrivateVlanId(location, scopeUid, privateVlanId); + } finally { + // Release the latch + latch.countDown(); + } + } + + /** + * Update the {@link org.jclouds.compute.options.TemplateOptions} that will + * be used by {@link JcloudsLocation} to obtain machines. Uses the VLAN + * numbers configured on an existing machine, as saved in the configuration + * maps for {@link #PUBLIC_VLAN_ID_MAP public} and {@link #PRIVATE_VLAN_ID_MAP private} + * VLAN numbers. + * + * @see {@link JcloudsLocationCustomizer#customize(JcloudsLocation, ComputeService, TemplateOptions)} + */ + @Override + public void customize(JcloudsLocation location, ComputeService computeService, TemplateOptions templateOptions) { + // Check we are customising a SoftLayer location + String provider = location.getProvider(); + if (!(provider.equals(SoftLayerConstants.SOFTLAYER_PROVIDER_NAME) && + templateOptions instanceof SoftLayerTemplateOptions)) { + String message = String.format("Invalid location provider or template options: %s/%s", + provider, templateOptions.getClass().getSimpleName()); + LOG.warn(message); + throw new IllegalArgumentException(message); + } + + // Check template options for VLAN configuration and save them if set + String scopeUid = getScopeUid(location); + SoftLayerTemplateOptions softLayerOptions = (SoftLayerTemplateOptions) templateOptions; + Integer publicVlanId = softLayerOptions.getPrimaryNetworkComponentNetworkVlanId(); + Integer privateVlanId = softLayerOptions.getPrimaryBackendNetworkComponentNetworkVlanId(); + if (publicVlanId != null || privateVlanId != null) { + LOG.debug("SoftLayer VLANs private {} and public {} already configured in template options for scope: {}", + new Object[] { privateVlanId, publicVlanId, scopeUid }); + savePublicVlanId(location, scopeUid, publicVlanId); + savePrivateVlanId(location, scopeUid, privateVlanId); + return; + } + + // Create a new latch if we are first + CountDownLatch latch = null; + synchronized (lock) { + latch = lookupCountDownLatch(location, scopeUid); + if (latch == null) { + // We are first to try obtaining a VM; create a latch and return + LOG.debug("Creating new latch for scope: {}", scopeUid); + latch = createCountDownLatch(location, scopeUid); + return; + } + } + + // Try and acquire the latch, or time out + Duration timeout = getTimeout(location); + Tasks.setBlockingDetails("Waiting for VLAN details"); + try { + LOG.debug("Waiting for VLAN details for scope: {}", scopeUid); + if (!Uninterruptibles.awaitUninterruptibly(latch, timeout.toMilliseconds(), TimeUnit.MILLISECONDS)) { + // Release the latch and throw an exception to prevent others blocking forever + latch.countDown(); --- End diff -- Or: 1. A creates the latch and starts to do work 2. BCD all wait on it 3. A crashes and the `finally` block executes, calling `countDown()` on the latch, setting the latch to zero 4. B, C and D all drop out of their `await()` calls and discover there's no VLANS and fail --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---