Updated Branches: refs/heads/master 11122cb8e -> e0bf6dc82
registry persitance for the Topology Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/e0bf6dc8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/e0bf6dc8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/e0bf6dc8 Branch: refs/heads/master Commit: e0bf6dc828d0460ac7df015d622abfb0974d1873 Parents: 11122cb Author: rekathiru <[email protected]> Authored: Wed Nov 6 14:44:30 2013 +0530 Committer: rekathiru <[email protected]> Committed: Wed Nov 6 14:44:30 2013 +0530 ---------------------------------------------------------------------- .../cloud/controller/persist/Serializer.java | 35 +++- .../controller/registry/RegistryManager.java | 193 ++++++++++++------- .../controller/topology/TopologyManager.java | 59 ++---- .../util/CloudControllerConstants.java | 2 + 4 files changed, 166 insertions(+), 123 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/e0bf6dc8/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/persist/Serializer.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/persist/Serializer.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/persist/Serializer.java index 57ae5e1..48ac4c7 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/persist/Serializer.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/persist/Serializer.java @@ -18,16 +18,12 @@ */ package org.apache.stratos.cloud.controller.persist; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectOutput; -import java.io.ObjectOutputStream; - -import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder; +import org.apache.stratos.messaging.domain.topology.Topology; + +import java.io.*; public class Serializer { @@ -83,4 +79,27 @@ public class Serializer { } + /** + * Serialize a {@link org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder} to a byte array. + * @param topology + * @return byte[] + * @throws IOException + */ + public static byte[] serializeToByteArray(Topology topology) throws IOException { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutput out = null; + try { + out = new ObjectOutputStream(bos); + out.writeObject(topology); + + return bos.toByteArray(); + + } finally { + out.close(); + bos.close(); + } + + } + } http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/e0bf6dc8/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/registry/RegistryManager.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/registry/RegistryManager.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/registry/RegistryManager.java index 711f88f..9c5c4b3 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/registry/RegistryManager.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/registry/RegistryManager.java @@ -21,13 +21,14 @@ package org.apache.stratos.cloud.controller.registry; */ +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.stratos.cloud.controller.exception.CloudControllerException; import org.apache.stratos.cloud.controller.persist.Serializer; import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder; import org.apache.stratos.cloud.controller.util.CloudControllerConstants; import org.apache.stratos.cloud.controller.util.ServiceReferenceHolder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.stratos.messaging.domain.topology.Topology; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Resource; import org.wso2.carbon.registry.core.exceptions.RegistryException; @@ -37,85 +38,127 @@ import org.wso2.carbon.registry.core.exceptions.ResourceNotFoundException; * */ public class RegistryManager { - private final static Log log = LogFactory.getLog(RegistryManager.class); - private static Registry registryService; - private static RegistryManager registryManager; - - public static RegistryManager getInstance() { - - registryService = ServiceReferenceHolder.getInstance().getRegistry(); - - if (registryManager == null) { - synchronized(RegistryManager.class){ - if (registryManager == null) { - if(registryService == null){ + private final static Log log = LogFactory.getLog(RegistryManager.class); + private static Registry registryService; + private static RegistryManager registryManager; + + public static RegistryManager getInstance() { + + registryService = ServiceReferenceHolder.getInstance().getRegistry(); + + if (registryManager == null) { + synchronized (RegistryManager.class) { + if (registryManager == null) { + if (registryService == null) { // log.warn("Registry Service is null. Hence unable to fetch data from registry."); - return registryManager; - } - registryManager = new RegistryManager(); - } - } - } - - return registryManager; - } - - private RegistryManager() { - try { - if (!registryService.resourceExists(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE)) { - registryService.put(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE, - registryService.newCollection()); - } - } catch (RegistryException e) { - String msg = - "Failed to create the registry resource " + - CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE; - log.error(msg, e); - throw new CloudControllerException(msg, e); - } - } - - /** + return registryManager; + } + registryManager = new RegistryManager(); + } + } + } + + return registryManager; + } + + private RegistryManager() { + try { + if (!registryService.resourceExists(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE)) { + registryService.put(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE, + registryService.newCollection()); + } + } catch (RegistryException e) { + String msg = + "Failed to create the registry resource " + + CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE; + log.error(msg, e); + throw new CloudControllerException(msg, e); + } + } + + /** * Persist an object in the local registry. + * * @param dataObj object to be persisted. */ - public void persist(FasterLookUpDataHolder dataObj) throws RegistryException { - try { + public void persist(FasterLookUpDataHolder dataObj) throws RegistryException { + try { + registryService.beginTransaction(); + + Resource nodeResource = registryService.newResource(); + + nodeResource.setContent(Serializer.serializeToByteArray(dataObj)); + + registryService.put(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE + CloudControllerConstants.DATA_RESOURCE, nodeResource); + + registryService.commitTransaction(); + + } catch (Exception e) { + String msg = "Failed to persist the cloud controller data in registry."; + registryService.rollbackTransaction(); + log.error(msg, e); + throw new CloudControllerException(msg, e); + + } + } + + public void persistTopology(Topology topology) throws RegistryException { + try { registryService.beginTransaction(); - - Resource nodeResource = registryService.newResource(); - - nodeResource.setContent(Serializer.serializeToByteArray(dataObj)); - - registryService.put(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE+ CloudControllerConstants.DATA_RESOURCE, nodeResource); - - registryService.commitTransaction(); - - } catch (Exception e) { - String msg = "Failed to persist the cloud controller data in registry."; - registryService.rollbackTransaction(); - log.error(msg, e); - throw new CloudControllerException(msg, e); - - } - } - - public Object retrieve(){ - - try { - Resource resource = registryService.get(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE+ CloudControllerConstants.DATA_RESOURCE); - - return resource.getContent(); - + + Resource nodeResource = registryService.newResource(); + + nodeResource.setContent(Serializer.serializeToByteArray(topology)); + + registryService.put(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE + CloudControllerConstants.TOPOLOGY_RESOURCE, nodeResource); + + registryService.commitTransaction(); + + } catch (Exception e) { + String msg = "Failed to persist the cloud controller data in registry."; + registryService.rollbackTransaction(); + log.error(msg, e); + throw new CloudControllerException(msg, e); + + } + } + + + public Object retrieve() { + + try { + Resource resource = registryService.get(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE + CloudControllerConstants.DATA_RESOURCE); + + return resource.getContent(); + + } catch (ResourceNotFoundException ignore) { + // this means, we've never persisted CC info in registry + return null; + } catch (RegistryException e) { + String msg = "Failed to retrieve cloud controller data from registry."; + log.error(msg, e); + throw new CloudControllerException(msg, e); + } + + } + + public Object retrieveTopology() { + + try { + Resource resource = registryService.get(CloudControllerConstants.CLOUD_CONTROLLER_RESOURCE + + CloudControllerConstants.TOPOLOGY_RESOURCE); + + return resource.getContent(); + } catch (ResourceNotFoundException ignore) { - // this means, we've never persisted CC info in registry - return null; - }catch (RegistryException e) { - String msg = "Failed to retrieve cloud controller data from registry."; - log.error(msg, e); - throw new CloudControllerException(msg, e); + // this means, we've never persisted CC info in registry + return null; + } catch (RegistryException e) { + String msg = "Failed to retrieve cloud controller data from registry."; + log.error(msg, e); + throw new CloudControllerException(msg, e); } - - } + + } } http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/e0bf6dc8/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyManager.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyManager.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyManager.java index 8d8d58a..e2fa512 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyManager.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/topology/TopologyManager.java @@ -19,16 +19,16 @@ package org.apache.stratos.cloud.controller.topology; import com.google.gson.Gson; -import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.stratos.cloud.controller.exception.CloudControllerException; +import org.apache.stratos.cloud.controller.registry.RegistryManager; import org.apache.stratos.cloud.controller.util.CloudControllerConstants; import org.apache.stratos.messaging.domain.topology.Topology; +import org.wso2.carbon.registry.core.exceptions.RegistryException; import javax.jms.TextMessage; import java.io.File; -import java.io.IOException; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -39,11 +39,11 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; public class TopologyManager { private static final Log log = LogFactory.getLog(TopologyManager.class); - private volatile ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private volatile ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private volatile ReentrantReadWriteLock.ReadLock readLock = lock.readLock(); private volatile ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock(); private File topologyFile = new File(CloudControllerConstants.TOPOLOGY_FILE_PATH); - private File backup = new File(CloudControllerConstants.TOPOLOGY_FILE_PATH + ".back"); + private File backup = new File(CloudControllerConstants.TOPOLOGY_FILE_PATH + ".back"); private volatile Topology topology; private static TopologyManager instance; private BlockingQueue<TextMessage> sharedTopologyDiffQueue = new LinkedBlockingQueue<TextMessage>(); @@ -76,31 +76,17 @@ public class TopologyManager { } public synchronized Topology getTopology() { - String currentContent = null; synchronized (TopologyManager.class) { - if(this.topology == null) { + if (this.topology == null) { //need to initialize the topology - if(this.topologyFile.exists()) { - try { - currentContent = FileUtils.readFileToString(this.topologyFile); - Gson gson = new Gson(); - this.topology = gson.fromJson(currentContent, Topology.class); - if(log.isDebugEnabled()) { - log.debug("The current topology is: " + currentContent); - } - } catch (IOException e) { - log.error(e.getMessage()); - throw new CloudControllerException(e.getMessage(), e); - } - } else { - if(log.isDebugEnabled()) { - log.debug("Creating new topology"); - } + this.topology = (Topology) RegistryManager.getInstance().retrieveTopology(); + if (this.topology == null) { + //we never persisted the topology before this.topology = new Topology(); } } } - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("The current topology is: " + toJson()); } return this.topology; @@ -108,25 +94,18 @@ public class TopologyManager { public synchronized void updateTopology(Topology topology) { synchronized (TopologyManager.class) { - this.topology = topology; - if (this.topologyFile.exists()) { - this.backup.delete(); - this.topologyFile.renameTo(backup); - } - Gson gson = new Gson(); - String message = gson.toJson(topology); - // overwrite the topology file + this.topology = topology; + //Persist data in registry. try { - FileUtils.writeStringToFile(this.topologyFile, message); - if(log.isDebugEnabled()) { - log.debug("The updated topology is: " + message); - } - } catch (IOException e) { - log.error(e.getMessage()); - throw new CloudControllerException(e.getMessage(), e); + RegistryManager.getInstance().persistTopology( + TopologyManager.getInstance().getTopology()); + } catch (RegistryException e) { + + String msg = "Failed to persist the Cloud Controller data in registry. Further, transaction roll back also failed."; + log.fatal(msg); + throw new CloudControllerException(msg, e); } } - } public void setTopology(Topology topology) { @@ -135,7 +114,7 @@ public class TopologyManager { public String toJson() { Gson gson = new Gson(); - return gson.toJson(topology); + return gson.toJson(topology); } http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/e0bf6dc8/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/util/CloudControllerConstants.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/util/CloudControllerConstants.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/util/CloudControllerConstants.java index 5dd80b0..13bf788 100644 --- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/util/CloudControllerConstants.java +++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/util/CloudControllerConstants.java @@ -220,5 +220,7 @@ public final class CloudControllerConstants { */ public static final String CLOUD_CONTROLLER_RESOURCE = "/cloud.controller"; public static final String DATA_RESOURCE = "/data"; + public static final String TOPOLOGY_RESOURCE = "/topology"; + }
