Added Main, StatisticsReader and Context classes.
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/5f79aea8 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/5f79aea8 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/5f79aea8 Branch: refs/heads/gsoc-projects-2015 Commit: 5f79aea8ea3aae9eca22b006d4cb5615053ddc3c Parents: 4eca3d4 Author: swapnilpatilRajaram <[email protected]> Authored: Sun Jun 14 15:59:26 2015 +0000 Committer: swapnilpatilRajaram <[email protected]> Committed: Sun Jun 14 15:59:26 2015 +0000 ---------------------------------------------------------------------- .../aws/extension/AWSExtensionContext.java | 101 +++++++++++++++++++ .../aws/extension/AWSStatisticsReader.java | 62 ++++++++++++ .../apache/stratos/aws/extension/Constants.java | 32 ++++++ .../org/apache/stratos/aws/extension/Main.java | 90 +++++++++++++++++ 4 files changed, 285 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/5f79aea8/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSExtensionContext.java ---------------------------------------------------------------------- diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSExtensionContext.java b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSExtensionContext.java new file mode 100644 index 0000000..bd55e34 --- /dev/null +++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSExtensionContext.java @@ -0,0 +1,101 @@ +/* + * 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.stratos.aws.extension; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * HAProxy context to read and store system properties. + */ +public class AWSExtensionContext { + private static final Log log = LogFactory.getLog(AWSExtensionContext.class); + private static volatile AWSExtensionContext context; + + private boolean cepStatsPublisherEnabled; + private String thriftReceiverIp; + private String thriftReceiverPort; + private String networkPartitionId; + private String clusterId; + private String serviceName; + + private AWSExtensionContext() { + this.cepStatsPublisherEnabled = Boolean.getBoolean(Constants.CEP_STATS_PUBLISHER_ENABLED); + this.thriftReceiverIp = System.getProperty(Constants.THRIFT_RECEIVER_IP); + this.thriftReceiverPort = System.getProperty(Constants.THRIFT_RECEIVER_PORT); + this.networkPartitionId = System.getProperty(Constants.NETWORK_PARTITION_ID); + this.clusterId = System.getProperty(Constants.CLUSTER_ID); + this.serviceName = System.getProperty(Constants.SERVICE_NAME); + + if (log.isDebugEnabled()) { + log.debug(Constants.CEP_STATS_PUBLISHER_ENABLED + " = " + cepStatsPublisherEnabled); + log.debug(Constants.THRIFT_RECEIVER_IP + " = " + thriftReceiverIp); + log.debug(Constants.THRIFT_RECEIVER_PORT + " = " + thriftReceiverPort); + log.debug(Constants.NETWORK_PARTITION_ID + " = " + networkPartitionId); + log.debug(Constants.CLUSTER_ID + " = " + clusterId); + } + } + + public static AWSExtensionContext getInstance() { + if (context == null) { + synchronized (AWSExtensionContext.class) { + if (context == null) { + context = new AWSExtensionContext(); + } + } + } + return context; + } + + public void validate() { + validateSystemProperty(Constants.CEP_STATS_PUBLISHER_ENABLED); + validateSystemProperty(Constants.CLUSTER_ID); + + if (cepStatsPublisherEnabled) { + validateSystemProperty(Constants.THRIFT_RECEIVER_IP); + validateSystemProperty(Constants.THRIFT_RECEIVER_PORT); + validateSystemProperty(Constants.NETWORK_PARTITION_ID); + } + } + + private void validateSystemProperty(String propertyName) { + String value = System.getProperty(propertyName); + if (StringUtils.isEmpty(value)) { + throw new RuntimeException("System property was not found: " + propertyName); + } + } + + public boolean isCEPStatsPublisherEnabled() { + return cepStatsPublisherEnabled; + } + + public String getNetworkPartitionId() { + return networkPartitionId; + } + + public String getClusterId() { + return clusterId; + } + + public String getServiceName() { + return serviceName; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/5f79aea8/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java ---------------------------------------------------------------------- diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java new file mode 100644 index 0000000..f7afc3a --- /dev/null +++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSStatisticsReader.java @@ -0,0 +1,62 @@ +/* + * 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.stratos.aws.extension; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.common.constants.StratosConstants; +import org.apache.stratos.common.util.CommandUtils; +import org.apache.stratos.load.balancer.common.domain.Cluster; +import org.apache.stratos.load.balancer.common.domain.Member; +import org.apache.stratos.load.balancer.common.domain.Port; +import org.apache.stratos.load.balancer.common.domain.Service; +import org.apache.stratos.load.balancer.common.statistics.LoadBalancerStatisticsReader; +import org.apache.stratos.load.balancer.common.topology.TopologyProvider; + +import java.io.IOException; + +/** + * AWS statistics reader. + */ +public class AWSStatisticsReader implements LoadBalancerStatisticsReader { + + private static final Log log = LogFactory.getLog(AWSStatisticsReader.class); + + private TopologyProvider topologyProvider; + private String clusterInstanceId; + + public AWSStatisticsReader(TopologyProvider topologyProvider) { + this.topologyProvider = topologyProvider; + this.clusterInstanceId = System.getProperty( + StratosConstants.CLUSTER_INSTANCE_ID, + StratosConstants.NOT_DEFINED); + } + + @Override + public String getClusterInstanceId() { + return clusterInstanceId; + } + + @Override + public int getInFlightRequestCount(String clusterId) { + // Find out logic + return 0; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/5f79aea8/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java ---------------------------------------------------------------------- diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java new file mode 100644 index 0000000..0e501f2 --- /dev/null +++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java @@ -0,0 +1,32 @@ +/* + * 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.stratos.aws.extension; + +/** + * HA proxy extension constants. + */ +public class Constants { + public static final String CEP_STATS_PUBLISHER_ENABLED = "cep.stats.publisher.enabled"; + public static final String THRIFT_RECEIVER_IP = "thrift.receiver.ip"; + public static final String THRIFT_RECEIVER_PORT = "thrift.receiver.port"; + public static final String NETWORK_PARTITION_ID = "network.partition.id"; + public static final String CLUSTER_ID = "cluster.id"; + public static final String SERVICE_NAME = "service.name"; +} http://git-wip-us.apache.org/repos/asf/stratos/blob/5f79aea8/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Main.java ---------------------------------------------------------------------- diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Main.java b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Main.java new file mode 100644 index 0000000..ee2bdb7 --- /dev/null +++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Main.java @@ -0,0 +1,90 @@ +/* + * 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.stratos.aws.extension; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.PropertyConfigurator; +import org.apache.stratos.common.threading.StratosThreadPool; +import org.apache.stratos.load.balancer.common.topology.TopologyProvider; +import org.apache.stratos.load.balancer.extension.api.LoadBalancerExtension; + +import java.util.concurrent.ExecutorService; + +/** + * AWS extension main class. + */ + +public class Main { + private static final Log log = LogFactory.getLog(Main.class); + private static ExecutorService executorService; + + public static void main(String[] args) { + + LoadBalancerExtension extension = null; + try { + // Configure log4j properties + PropertyConfigurator.configure(System + .getProperty("log4j.properties.file.path")); + + if (log.isInfoEnabled()) { + log.info("AWS extension started"); + } + + // Add shutdown hook + final Thread mainThread = Thread.currentThread(); + final LoadBalancerExtension finalExtension = extension; + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + try { + if (finalExtension != null) { + log.info("Shutting aws extension..."); + finalExtension.stop(); + } + mainThread.join(); + } catch (Exception e) { + log.error(e); + } + } + }); + + executorService = StratosThreadPool.getExecutorService( + "aws.extension.thread.pool", 10); + // Validate runtime parameters + AWSExtensionContext.getInstance().validate(); + TopologyProvider topologyProvider = new TopologyProvider(); + AWSStatisticsReader statisticsReader = AWSExtensionContext + .getInstance().isCEPStatsPublisherEnabled() ? new AWSStatisticsReader( + topologyProvider) : null; + extension = new LoadBalancerExtension(new AWSLoadBalancer(), + statisticsReader, topologyProvider); + extension.setExecutorService(executorService); + extension.execute(); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error(e); + } + if (extension != null) { + log.info("Shutting aws extension..."); + extension.stop(); + } + } + } +}
