You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ni...@apache.org on 2014/10/09 15:37:35 UTC
[5/6] git commit: Implementing updateContainers method and fixing a
minor sonar issue (removing redundant public modifier )
Implementing updateContainers method and fixing a minor sonar issue (removing redundant public modifier )
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/7596b464
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/7596b464
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/7596b464
Branch: refs/heads/container-autoscaling
Commit: 7596b46413db02df312cfc0b5810e7a523c5a38c
Parents: be7e36b
Author: Nirmal Fernando <ni...@gmail.com>
Authored: Thu Oct 9 19:01:17 2014 +0530
Committer: Nirmal Fernando <ni...@gmail.com>
Committed: Thu Oct 9 19:07:04 2014 +0530
----------------------------------------------------------------------
.../impl/CloudControllerServiceImpl.java | 135 ++++++++++++++++++-
.../interfaces/CloudControllerService.java | 33 ++---
2 files changed, 146 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/stratos/blob/7596b464/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
index da96ab0..a3fed80 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
@@ -1408,8 +1408,9 @@ public class CloudControllerServiceImpl implements CloudControllerService {
kubApi.createService(service);
- // set host port
+ // set host port and update
ctxt.addProperty(StratosConstants.ALLOCATED_SERVICE_HOST_PORT, service.getPort());
+ dataHolder.addClusterContext(ctxt);
if (log.isDebugEnabled()) {
log.debug("Cloud Controller successfully started the service "
@@ -1426,7 +1427,10 @@ public class CloudControllerServiceImpl implements CloudControllerService {
for (int i = 0; i < expectedCount ; i++) {
newlyCreatedPods = kubApi.getSelectedPods(new Label[]{l});
- log.info("Pods "+newlyCreatedPods.length);
+ if (log.isDebugEnabled()) {
+
+ log.debug("Pods Count: "+newlyCreatedPods.length+" for cluster: "+clusterId);
+ }
if(newlyCreatedPods.length == expectedCount) {
break;
}
@@ -1456,7 +1460,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
// trigger topology
// update the topology with the newly spawned member
TopologyBuilder.handleMemberSpawned(cartridgeType, clusterId, null,
- kubernetesMasterIp, kubernetesMasterIp, context);
+ pod.getCurrentState().getPodIP(), pod.getCurrentState().getHostIP(), context);
// publish data
// TODO
// CartridgeInstanceDataPublisher.publish(context.getMemberId(), null, null, context.getClusterId(), cartridgeType, MemberStatus.Created.toString(), node);
@@ -1626,10 +1630,129 @@ public class CloudControllerServiceImpl implements CloudControllerService {
}
@Override
- public void updateKubernetesController(String clusterId, int replicas)
- throws InvalidClusterException {
- // TODO Auto-generated method stub
+ public MemberContext[] updateContainers(String clusterId, int replicas)
+ throws UnregisteredCartridgeException {
+ if(log.isDebugEnabled()) {
+ log.debug("CloudControllerServiceImpl:updateContainers for cluster : "+clusterId);
+ }
+
+ ClusterContext ctxt = dataHolder.getClusterContext(clusterId);
+
+ if (ctxt == null) {
+ String msg = "Instance start-up failed. Invalid cluster id. " + clusterId;
+ log.error(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ String cartridgeType = ctxt.getCartridgeType();
+
+ Cartridge cartridge = dataHolder.getCartridge(cartridgeType);
+
+ if (cartridge == null) {
+ String msg =
+ "Instance start-up failed. No matching Cartridge found [type] "+cartridgeType
+ +". [cluster id] "+ clusterId;
+ log.error(msg);
+ throw new UnregisteredCartridgeException(msg);
+ }
+
+ try {
+ String kubernetesClusterId = validateProperty(StratosConstants.KUBERNETES_CLUSTER_ID, ctxt);
+
+ KubernetesClusterContext kubClusterContext = dataHolder.getKubernetesClusterContext(kubernetesClusterId);
+
+ if (kubClusterContext == null) {
+ String msg =
+ "Instance start-up failed. No matching Kubernetes Context Found for [id] "+kubernetesClusterId
+ +". [cluster id] "+ clusterId;
+ log.error(msg);
+ throw new UnregisteredCartridgeException(msg);
+ }
+
+ KubernetesApiClient kubApi = kubClusterContext.getKubApi();
+
+ // update the replication controller - cluster id = replication controller id
+ if (log.isDebugEnabled()) {
+ log.debug("Cloud Controller is delegating request to update a replication controller "+clusterId+
+ " to Kubernetes layer.");
+ }
+
+ kubApi.updateReplicationController(clusterId, replicas);
+
+ if (log.isDebugEnabled()) {
+ log.debug("Cloud Controller successfully updated the controller "
+ + clusterId + " via Kubernetes layer.");
+ }
+
+ // create a label query
+ Label l = new Label();
+ l.setName(clusterId);
+ // execute the label query
+ Pod[] allPods = new Pod[0];
+
+ // wait replicas*5s time in the worst case ; best case = 0s
+ for (int i = 0; i < replicas ; i++) {
+ allPods = kubApi.getSelectedPods(new Label[]{l});
+
+ if (log.isDebugEnabled()) {
+
+ log.debug("Pods Count: "+allPods.length+" for cluster: "+clusterId);
+ }
+ if(allPods.length == replicas) {
+ break;
+ }
+ Thread.sleep(5000);
+ }
+
+ if (log.isDebugEnabled()) {
+
+ log.debug(String.format("Pods created : %s for cluster : %s",allPods.length, clusterId));
+ }
+
+ List<MemberContext> memberContexts = new ArrayList<MemberContext>();
+
+ PodToMemberContext podToMemberContextFunc = new PodToMemberContext();
+ // generate Member Contexts
+ for (Pod pod : allPods) {
+ MemberContext context;
+ if ((context = dataHolder.getMemberContextOfMemberId(pod.getId())) == null) {
+
+ context = podToMemberContextFunc.apply(pod);
+ context.setCartridgeType(cartridgeType);
+ context.setClusterId(clusterId);
+
+ context.setProperties(CloudControllerUtil.addProperty(context
+ .getProperties(), StratosConstants.ALLOCATED_SERVICE_HOST_PORT,
+ ctxt.getProperties().getProperty(StratosConstants.ALLOCATED_SERVICE_HOST_PORT)));
+
+ dataHolder.addMemberContext(context);
+
+ // trigger topology
+ // update the topology with the newly spawned member
+ TopologyBuilder.handleMemberSpawned(cartridgeType, clusterId, null,
+ pod.getCurrentState().getPodIP(), pod.getCurrentState().getHostIP(), context);
+
+ memberContexts.add(context);
+ }
+ // publish data
+ // TODO
+// CartridgeInstanceDataPublisher.publish(context.getMemberId(), null, null, context.getClusterId(), cartridgeType, MemberStatus.Created.toString(), node);
+
+ }
+
+ // persist in registry
+ persist();
+
+ log.info("Kubernetes entities are successfully starting up. "+memberContexts);
+
+ return memberContexts.toArray(new MemberContext[0]);
+
+ } catch (Exception e) {
+ String msg = "Failed to update containers belong to cluster " + clusterId+". Cause: "+e.getMessage();
+ log.error(msg, e);
+ throw new IllegalStateException(msg, e);
+ }
}
}
http://git-wip-us.apache.org/repos/asf/stratos/blob/7596b464/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/interfaces/CloudControllerService.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/interfaces/CloudControllerService.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/interfaces/CloudControllerService.java
index 5725a2a..7117c1c 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/interfaces/CloudControllerService.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/interfaces/CloudControllerService.java
@@ -43,7 +43,7 @@ public interface CloudControllerService {
* @throws InvalidIaasProviderException if the iaas providers configured are not valid.
* @throws IllegalArgumentException if the provided argument is not valid.
*/
- public void deployCartridgeDefinition(CartridgeConfig cartridgeConfig)
+ void deployCartridgeDefinition(CartridgeConfig cartridgeConfig)
throws InvalidCartridgeDefinitionException, InvalidIaasProviderException;
/**
@@ -51,7 +51,7 @@ public interface CloudControllerService {
* @param cartridgeType type of the cartridge to be undeployed.
* @throws InvalidCartridgeTypeException if the cartridge type specified is not a deployed cartridge.
*/
- public void undeployCartridgeDefinition(String cartridgeType) throws InvalidCartridgeTypeException;
+ void undeployCartridgeDefinition(String cartridgeType) throws InvalidCartridgeTypeException;
/**
* Validate a given {@link Partition} for basic property existence.
@@ -59,7 +59,7 @@ public interface CloudControllerService {
* @return whether the partition is a valid one.
* @throws InvalidPartitionException if the partition is invalid.
*/
- public boolean validatePartition(Partition partition) throws InvalidPartitionException;
+ boolean validatePartition(Partition partition) throws InvalidPartitionException;
/**
* Validate a given {@link DeploymentPolicy} against a Cartridge.
@@ -69,7 +69,7 @@ public interface CloudControllerService {
* @throws InvalidPartitionException if the policy contains at least one invalid partition.
* @throws InvalidCartridgeTypeException if the given Cartridge type is not a valid one.
*/
- public boolean validateDeploymentPolicy(String cartridgeType, Partition[] partitions)
+ boolean validateDeploymentPolicy(String cartridgeType, Partition[] partitions)
throws InvalidPartitionException, InvalidCartridgeTypeException;
/**
@@ -85,7 +85,7 @@ public interface CloudControllerService {
* when the cartridge type requested by this service is
* not a registered one.
*/
- public boolean registerService(Registrant registrant) throws UnregisteredCartridgeException;
+ boolean registerService(Registrant registrant) throws UnregisteredCartridgeException;
/**
* Calling this method will result in an instance startup, which is belong
@@ -97,7 +97,7 @@ public interface CloudControllerService {
* @throws UnregisteredCartridgeException if the requested Cartridge type is not a registered one.
* @throws InvalidIaasProviderException if the iaas requested is not valid.
*/
- public MemberContext startInstance(MemberContext member) throws UnregisteredCartridgeException, InvalidIaasProviderException;
+ MemberContext startInstance(MemberContext member) throws UnregisteredCartridgeException, InvalidIaasProviderException;
/**
* Create a container cluster.
@@ -105,7 +105,7 @@ public interface CloudControllerService {
* @return a list of {@link MemberContext}s correspond to each Pod created.
* @throws UnregisteredCartridgeException if the requested Cartridge type is not a registered one.
*/
- public MemberContext[] startContainers(ContainerClusterContext clusterContext) throws UnregisteredCartridgeException;
+ MemberContext[] startContainers(ContainerClusterContext clusterContext) throws UnregisteredCartridgeException;
/**
* Calling this method will result in termination of the instance with given member id in the given Partition.
@@ -117,7 +117,7 @@ public interface CloudControllerService {
* an instance need to be terminated..
* @return whether an instance terminated successfully or not.
*/
- public void terminateInstance(String memberId) throws InvalidMemberException, InvalidCartridgeTypeException;
+ void terminateInstance(String memberId) throws InvalidMemberException, InvalidCartridgeTypeException;
/**
* Calling this method will result in termination of all instances belong
@@ -127,36 +127,37 @@ public interface CloudControllerService {
* cluster ID of the instance to be terminated.
* @return whether an instance terminated successfully or not.
*/
- public void terminateAllInstances(String clusterId) throws InvalidClusterException;
+ void terminateAllInstances(String clusterId) throws InvalidClusterException;
/**
* Terminate all containers of the given cluster.
* @param clusterId id of the subjected cluster.
* @throws InvalidClusterException
*/
- public void terminateAllContainers(String clusterId) throws InvalidClusterException;
+ void terminateAllContainers(String clusterId) throws InvalidClusterException;
/**
* Update the Kubernetes controller created for the given cluster with the specified number of replicas.
* @param clusterId id of the subjected cluster.
* @param replicas total number of replicas to be set to the controller.
+ * @return newly created Members if any.
* @throws InvalidClusterException
*/
- public void updateKubernetesController(String clusterId, int replicas) throws InvalidClusterException;
+ MemberContext[] updateContainers(String clusterId, int replicas) throws UnregisteredCartridgeException;
/**
* Unregister a docker service identified by the given cluster id.
* @param clusterId service cluster id.
* @throws UnregisteredClusterException if the service cluster requested is not a registered one.
*/
- public void unregisterDockerService(String clusterId) throws UnregisteredClusterException;
+ void unregisterDockerService(String clusterId) throws UnregisteredClusterException;
/**
* Unregister the service cluster identified by the given cluster id.
* @param clusterId service cluster id.
* @throws UnregisteredClusterException if the service cluster requested is not a registered one.
*/
- public void unregisterService(String clusterId) throws UnregisteredClusterException;
+ void unregisterService(String clusterId) throws UnregisteredClusterException;
/**
* This method will return the information regarding the given cartridge, if present.
@@ -167,7 +168,7 @@ public interface CloudControllerService {
* @return {@link org.apache.stratos.cloud.controller.pojo.CartridgeInfo} of the given cartridge type or <code>null</code>.
* @throws UnregisteredCartridgeException if there is no registered cartridge with this type.
*/
- public CartridgeInfo getCartridgeInfo(String cartridgeType) throws UnregisteredCartridgeException;
+ CartridgeInfo getCartridgeInfo(String cartridgeType) throws UnregisteredCartridgeException;
/**
* Calling this method will result in returning the types of {@link org.apache.stratos.cloud.controller.pojo.Cartridge}s
@@ -175,7 +176,7 @@ public interface CloudControllerService {
*
* @return String array containing types of registered {@link org.apache.stratos.cloud.controller.pojo.Cartridge}s.
*/
- public String[] getRegisteredCartridges();
+ String[] getRegisteredCartridges();
/**
* Returns the {@link org.apache.stratos.cloud.controller.pojo.ClusterContext} object associated with the given cluster id, or null if not found
@@ -183,6 +184,6 @@ public interface CloudControllerService {
* @param clusterId cluster id
* @return {@link org.apache.stratos.cloud.controller.pojo.ClusterContext} object associated with the given cluster id, or null
*/
- public ClusterContext getClusterContext (String clusterId);
+ ClusterContext getClusterContext (String clusterId);
}