From 26b920343785b280f63619b5accded1770cd43e6 Mon Sep 17 00:00:00 2001 From: Martin Harris Date: Fri, 12 Sep 2014 11:57:33 +0100 Subject: [PATCH] Adds support for deploying the visitors example app, backed with Riak --- examples/webapps/hello-world-sql/pom.xml | 26 ++++ .../src/main/webapp/available.jsp | 8 +- .../hello-world-sql/src/main/webapp/riak.jsp | 147 ++++++++++++++++++ pom.xml | 1 + .../entity/nosql/riak/RiakCluster.java | 2 + .../entity/nosql/riak/RiakClusterImpl.java | 18 +++ .../entity/nosql/riak/riak-with-webapp.yaml | 43 +++++ 7 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 examples/webapps/hello-world-sql/src/main/webapp/riak.jsp create mode 100644 software/nosql/src/main/resources/brooklyn/entity/nosql/riak/riak-with-webapp.yaml diff --git a/examples/webapps/hello-world-sql/pom.xml b/examples/webapps/hello-world-sql/pom.xml index 9917d1e12e..d04974e1f7 100644 --- a/examples/webapps/hello-world-sql/pom.xml +++ b/examples/webapps/hello-world-sql/pom.xml @@ -31,6 +31,27 @@ + + + com.fasterxml.jackson.core + jackson-core + ${fasterxml.jackson.version} + + + com.fasterxml.jackson.core + jackson-annotations + ${fasterxml.jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${fasterxml.jackson.version} + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + ${fasterxml.jackson.version} + mysql mysql-connector-java @@ -41,6 +62,11 @@ mongo-java-driver ${mongodb.version} + + com.basho.riak + riak-client + ${riak.version} + diff --git a/examples/webapps/hello-world-sql/src/main/webapp/available.jsp b/examples/webapps/hello-world-sql/src/main/webapp/available.jsp index 0a39ada920..7c19d51337 100644 --- a/examples/webapps/hello-world-sql/src/main/webapp/available.jsp +++ b/examples/webapps/hello-world-sql/src/main/webapp/available.jsp @@ -64,8 +64,14 @@ if (hadoop!=null) { if (mongo!=null) { %>
  • MongoDB chatroom
  • +<% + } + String riak=System.getProperty("brooklyn.example.riak.nodes"); + if (riak != null) { +%> +
  • Riak chatroom
  • <% } -if (hadoop==null && url==null && mongo==null) { +if (hadoop==null && url==null && mongo==null && riak==null) { %>
  • None. Try one of the other Brooklyn examples to see SQL or Hadoop.
  • <% } %> diff --git a/examples/webapps/hello-world-sql/src/main/webapp/riak.jsp b/examples/webapps/hello-world-sql/src/main/webapp/riak.jsp new file mode 100644 index 0000000000..44934eef29 --- /dev/null +++ b/examples/webapps/hello-world-sql/src/main/webapp/riak.jsp @@ -0,0 +1,147 @@ +<%@ page language="java" import="com.basho.riak.client.IRiakClient,com.basho.riak.client.RiakFactory,com.basho.riak.client.bucket.Bucket" %> +<%@ page import="com.basho.riak.client.convert.RiakKey" %> +<%@ page import="com.basho.riak.client.raw.http.HTTPClientConfig" %> +<%@ page import="com.basho.riak.client.raw.http.HTTPClusterConfig" %> +<%@ page import="org.joda.time.DateTime" %> +<%@ page import="java.util.ArrayList" %> +<%@ page import="java.util.Collections" %> +<%@ page import="java.util.Comparator" %> +<%@ page import="java.util.List" %> + + + + +<%! + static class Message { + @RiakKey + public String id; + public String name; + public String message; + public DateTime dateTime; + + public Message() {} + + public Message(String name, String message) { + this.name = name; + this.message = message; + this.dateTime = DateTime.now(); + } + } +%> + + + Sample Application Riak JSP Page + + + + + + + + +
    + + +

    Sample Brooklyn Deployed WebApp (Riak JSP)

    + This is the output of a JSP page that is part of the Hello, World application, + deployed by brooklyn, to show Riak database interactivity. +
    + +<% + String url = System.getProperty("brooklyn.example.riak.url"); + if (url == null) { +%> +<% } else { %> +
    + +

    Visitors:

    + + +
    + +

    Please enter a message:

    + +
    + + + + + + + + + +
    Name:
    Message:
    + +
    + +
    + +

    Click here to go back to the main page.

    + + diff --git a/pom.xml b/pom.xml index 3b54f58b08..9cdef9dc88 100644 --- a/pom.xml +++ b/pom.xml @@ -143,6 +143,7 @@ 1.4 0.20 2.11.4 + 1.4.0 2.4 1.11 1.0.0.GA diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakCluster.java b/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakCluster.java index 32e8de7178..72b464e172 100644 --- a/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakCluster.java +++ b/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakCluster.java @@ -47,4 +47,6 @@ public interface RiakCluster extends DynamicCluster { ConfigKey DELAY_BEFORE_ADVERTISING_CLUSTER = ConfigKeys.newConfigKey(Duration.class, "riak.cluster.delayBeforeAdvertisingCluster", "Delay after cluster is started before checking and advertising its availability", Duration.seconds(2 * 60)); AttributeSensor IS_CLUSTER_INIT = Sensors.newBooleanSensor("riak.cluster.isClusterInit", "flag to determine if the cluster was already initialized"); + + AttributeSensor NODE_LIST = Sensors.newStringSensor("riak.cluster.nodeList", "List of nodes (including ports), comma separated"); } diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakClusterImpl.java b/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakClusterImpl.java index 80a9683af3..29c66ffcdf 100644 --- a/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakClusterImpl.java +++ b/software/nosql/src/main/java/brooklyn/entity/nosql/riak/RiakClusterImpl.java @@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory; import brooklyn.entity.Entity; +import brooklyn.entity.basic.Attributes; import brooklyn.entity.basic.Entities; import brooklyn.entity.basic.EntityInternal; import brooklyn.entity.basic.Lifecycle; @@ -172,6 +173,23 @@ public boolean apply(@Nullable Entity node) { ServiceNotUpLogic.updateNotUpIndicatorRequiringNonEmptyMap(this, RIAK_CLUSTER_NODES); if (log.isTraceEnabled()) log.trace("Done {} checkEntity {}", this, member); + + calculateClusterAddresses(); + } + + private void calculateClusterAddresses() { + String addresses = ""; + for (Entity entity : this.getMembers()) { + if (entity instanceof RiakNode && entity.getAttribute(Attributes.SERVICE_UP)) { + RiakNode riakNode = (RiakNode) entity; + addresses += riakNode.getAttribute(Attributes.HOSTNAME) + ":" + riakNode.getAttribute(RiakNode.RIAK_WEB_PORT) + ","; + } + } + if (addresses.length() > 0) { + setAttribute(RiakCluster.NODE_LIST, addresses.substring(0, addresses.length() -1)); + } else { + setAttribute(RiakCluster.NODE_LIST, null); + } } protected boolean belongsInServerPool(Entity member) { diff --git a/software/nosql/src/main/resources/brooklyn/entity/nosql/riak/riak-with-webapp.yaml b/software/nosql/src/main/resources/brooklyn/entity/nosql/riak/riak-with-webapp.yaml new file mode 100644 index 0000000000..92def6acf2 --- /dev/null +++ b/software/nosql/src/main/resources/brooklyn/entity/nosql/riak/riak-with-webapp.yaml @@ -0,0 +1,43 @@ +# +# 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. +# + +name: Riak Cluster with Webapp + + +location: softlayer:sjc01 + + +services: + +- type: brooklyn.entity.nosql.riak.RiakCluster + initialSize: 2 + id: mycluster + brooklyn.config: + install.version: 1.4.10 + provisioning.properties: + osFamily: centos + +- serviceType: brooklyn.entity.webapp.ControlledDynamicWebAppCluster + name: My Web + location: localhost + brooklyn.config: + initialSize: 2 + wars.root: "classpath://brooklyn-example-hello-world-sql-webapp.war" + java.sysprops: + brooklyn.example.riak.nodes: $brooklyn:component("mycluster").attributeWhenReady("riak.cluster.nodeList")