diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-28 09:50:21 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-28 09:50:21 +0000 |
commit | 9e52793370e243c647f6df181402a827e1948c67 (patch) | |
tree | b45fc568df85769e4281a01b84f45bf141d4dabb /sca-cpp | |
parent | fe93d86e5572870b2e4004c7788da8320a28de3d (diff) |
Change sample to use a pool of three memcached servers. Add a property to the memcached component to list multiple memcached servers. Replace spaces by tabs in memcached keys as spaces are not allowed in keys.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@980010 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp')
21 files changed, 112 insertions, 49 deletions
diff --git a/sca-cpp/trunk/components/cache/memcache-test.cpp b/sca-cpp/trunk/components/cache/memcache-test.cpp index 04c4b7d347..289037a179 100644 --- a/sca-cpp/trunk/components/cache/memcache-test.cpp +++ b/sca-cpp/trunk/components/cache/memcache-test.cpp @@ -33,7 +33,7 @@ namespace tuscany { namespace memcache { bool testMemCached() { - MemCached ch("localhost", 11211); + MemCached ch(mklist<string>("localhost:11211", "localhost:11212", "localhost:11213")); const value k = mklist<value>("a"); assert(hasContent(post(k, string("AAA"), ch))); @@ -59,7 +59,7 @@ struct getLoop { bool testGetPerf() { const value k = mklist<value>("c"); - MemCached ch("localhost", 11211); + MemCached ch(mklist<string>("localhost:11211", "localhost:11212", "localhost:11213")); assert(hasContent(post(k, string("CCC"), ch))); const lambda<bool()> gl = getLoop(k, ch); diff --git a/sca-cpp/trunk/components/cache/memcache.composite b/sca-cpp/trunk/components/cache/memcache.composite index 654df6abbf..7c160172f0 100644 --- a/sca-cpp/trunk/components/cache/memcache.composite +++ b/sca-cpp/trunk/components/cache/memcache.composite @@ -27,6 +27,7 @@ <service name="memcache"> <t:binding.http uri="memcache"/> </service> + <property name="servers">localhost,localhost:11212,localhost:11213</property> </component> </composite> diff --git a/sca-cpp/trunk/components/cache/memcache.cpp b/sca-cpp/trunk/components/cache/memcache.cpp index 246b6137b3..ec61ae9a92 100644 --- a/sca-cpp/trunk/components/cache/memcache.cpp +++ b/sca-cpp/trunk/components/cache/memcache.cpp @@ -112,7 +112,8 @@ private: */ const failable<value> start(unused const list<value>& params) { // Connect to memcached - memcache::MemCached& ch = *(new (gc_new<memcache::MemCached>()) memcache::MemCached("localhost", 11211)); + const value servers = ((lambda<value(list<value>)>)car(params))(list<value>()); + memcache::MemCached& ch = *(new (gc_new<memcache::MemCached>()) memcache::MemCached(tokenize(",", servers))); // Return the component implementation lambda function return value(lambda<value(const list<value>&)>(applyCache(ch))); diff --git a/sca-cpp/trunk/components/cache/memcache.hpp b/sca-cpp/trunk/components/cache/memcache.hpp index d410b90767..213120891f 100644 --- a/sca-cpp/trunk/components/cache/memcache.hpp +++ b/sca-cpp/trunk/components/cache/memcache.hpp @@ -54,7 +54,13 @@ public: MemCached(const string host, const int port) : owner(true) { apr_pool_create(&pool, NULL); apr_memcache_create(pool, 1, 0, &mc); - init(host, port); + addServer(host, port); + } + + MemCached(const list<string>& servers) : owner(true) { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + addServers(servers); } MemCached(const MemCached& c) : owner(false) { @@ -79,9 +85,9 @@ private: friend const failable<bool> del(const value& key, const MemCached& cache); /** - * Initialize the memcached context. + * Add servers to the memcached context. */ - const failable<bool> init(const string& host, const int port) { + const failable<bool> addServer(const string& host, const int port) { apr_memcache_server_t *server; const apr_status_t sc = apr_memcache_server_create(pool, c_str(host), (apr_port_t)port, 0, 1, 1, 60, &server); if (sc != APR_SUCCESS) @@ -91,9 +97,30 @@ private: return mkfailure<bool>("Could not add server"); return true; } + + const failable<bool> addServers(const list<string>& servers) { + if (isNil(servers)) + return true; + const list<string> toks = tokenize(":", car(servers)); + const failable<bool> r = addServer(car(toks), isNil(cdr(toks))? 11211 : atoi(c_str(cadr(toks)))); + if (!hasContent(r)) + return r; + return addServers(cdr(servers)); + } }; /** + * Replace spaces by tabs (as spaces are not allowed in memcached keys). + */ +const char* nospaces(const char* s) { + char* c = const_cast<char*>(s); + for (; *c; c++) + if (*c == ' ') + *c = '\t'; + return s; +} + +/** * Post a new item to the cache. */ const failable<bool> post(const value& key, const value& val, const MemCached& cache) { @@ -102,7 +129,7 @@ const failable<bool> post(const value& key, const value& val, const MemCached& c const string ks(scheme::writeValue(key)); const string vs(scheme::writeValue(val)); - const apr_status_t rc = apr_memcache_add(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); + const apr_status_t rc = apr_memcache_add(cache.mc, nospaces(c_str(ks)), const_cast<char*>(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) return mkfailure<bool>("Could not add entry"); @@ -119,7 +146,7 @@ const failable<bool> put(const value& key, const value& val, const MemCached& ca const string ks(scheme::writeValue(key)); const string vs(scheme::writeValue(val)); - const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); + const apr_status_t rc = apr_memcache_set(cache.mc, nospaces(c_str(ks)), const_cast<char*>(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) return mkfailure<bool>("Could not set entry"); @@ -141,7 +168,7 @@ const failable<value> get(const value& key, const MemCached& cache) { char *data; apr_size_t size; - const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, c_str(ks), &data, &size, NULL); + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, nospaces(c_str(ks)), &data, &size, NULL); if (rc != APR_SUCCESS) { apr_pool_destroy(vpool); return mkfailure<value>("Could not get entry"); @@ -161,7 +188,7 @@ const failable<bool> del(const value& key, const MemCached& cache) { debug(key, "memcache::delete::key"); const string ks(scheme::writeValue(key)); - const apr_status_t rc = apr_memcache_delete(cache.mc, c_str(ks), 0); + const apr_status_t rc = apr_memcache_delete(cache.mc, nospaces(c_str(ks)), 0); if (rc != APR_SUCCESS) return mkfailure<bool>("Could not delete entry"); diff --git a/sca-cpp/trunk/components/cache/memcached-start b/sca-cpp/trunk/components/cache/memcached-start index b10d7f3fe8..a3cecc29bf 100755 --- a/sca-cpp/trunk/components/cache/memcached-start +++ b/sca-cpp/trunk/components/cache/memcached-start @@ -19,7 +19,11 @@ # Start memcached here=`readlink -f $0`; here=`dirname $here` +port=$1 +if [ "$port" = "" ]; then + port="11211" +fi memcached_prefix=`cat $here/memcached.prefix` -$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p 11211 +$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p $port diff --git a/sca-cpp/trunk/components/cache/memcached-stop b/sca-cpp/trunk/components/cache/memcached-stop index 80801cdfbf..6522e1d36e 100755 --- a/sca-cpp/trunk/components/cache/memcached-stop +++ b/sca-cpp/trunk/components/cache/memcached-stop @@ -19,8 +19,12 @@ # Stop memcached here=`readlink -f $0`; here=`dirname $here` +port=$1 +if [ "$port" = "" ]; then + port="11211" +fi memcached_prefix=`cat $here/memcached.prefix` -mc="$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p 11211" +mc="$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p $port" kill `ps -ef | grep -v grep | grep "${mc}" | awk '{ print $2 }'` diff --git a/sca-cpp/trunk/components/cache/memcached-test b/sca-cpp/trunk/components/cache/memcached-test index 842e8d2030..993d63ba1b 100755 --- a/sca-cpp/trunk/components/cache/memcached-test +++ b/sca-cpp/trunk/components/cache/memcached-test @@ -18,7 +18,9 @@ # under the License. # Setup -./memcached-start +./memcached-start 11211 +./memcached-start 11212 +./memcached-start 11213 sleep 1 # Test @@ -26,5 +28,7 @@ sleep 1 rc=$? # Cleanup -./memcached-stop +./memcached-stop 11211 +./memcached-stop 11212 +./memcached-stop 11213 return $rc diff --git a/sca-cpp/trunk/components/cache/server-test b/sca-cpp/trunk/components/cache/server-test index 5c86b75ebc..598d4bce5b 100755 --- a/sca-cpp/trunk/components/cache/server-test +++ b/sca-cpp/trunk/components/cache/server-test @@ -26,7 +26,9 @@ SCAContribution `pwd`/ SCAComposite memcache.composite EOF -./memcached-start +./memcached-start 11211 +./memcached-start 11212 +./memcached-start 11213 ../../modules/http/httpd-start tmp sleep 2 @@ -36,6 +38,8 @@ rc=$? # Cleanup ../../modules/http/httpd-stop tmp -./memcached-stop +./memcached-stop 11211 +./memcached-stop 11212 +./memcached-stop 11213 sleep 2 return $rc diff --git a/sca-cpp/trunk/samples/store-cluster/domains/jane/shopping-cart.py b/sca-cpp/trunk/samples/store-cluster/domains/jane/shopping-cart.py index 405adb85bf..e315b2f1ca 100644 --- a/sca-cpp/trunk/samples/store-cluster/domains/jane/shopping-cart.py +++ b/sca-cpp/trunk/samples/store-cluster/domains/jane/shopping-cart.py @@ -19,21 +19,23 @@ import uuid import sys -cartId = "1234" +# Convert a particular host and user email to a cart id +def cartid(host, email): + return ("cart", host(), email()) # Get the shopping cart from the cache # Return an empty cart if not found def getcart(id, cache): - cart = cache("get", (id,)) + cart = cache("get", id) if cart is None: return () return cart # Post a new item to the cart, create a new cart if necessary -def post(collection, item, cache): +def post(collection, item, cache, host, email): id = str(uuid.uuid1()) - cart = ((item[0], id, item[2]),) + getcart(cartId, cache) - cache("put", (cartId,), cart) + cart = ((item[0], id, item[2]),) + getcart(cartid(host, email), cache) + cache("put", cartid(host, email), cart) return (id,) @@ -47,15 +49,15 @@ def find(id, cart): return find(id, cart[1:]) # Get items from the cart -def get(id, cache): +def get(id, cache, host, email): if id == (): - return ("Your Cart", cartId) + getcart(cartId, cache) - return find(id[0], getcart(cartId, cache)) + return ("Your Cart", email()) + getcart(cartid(host, email), cache) + return find(id[0], getcart(cartid(host, email), cache)) # Delete items from the cart -def delete(id, cache): +def delete(id, cache, host, email): if id == (): - return cache("delete", (cartId,)) + return cache("delete", cartid(host, email)) return True # Return the price of an item @@ -69,7 +71,6 @@ def sum(items): return price(items[0]) + sum(items[1:]) # Return the total price of the items in the cart -def gettotal(cache): - cart = getcart(cartId, cache) - return sum(cart) +def gettotal(cache, host, email): + return sum(getcart(cartid(host, email), cache)) diff --git a/sca-cpp/trunk/samples/store-cluster/domains/jane/store.composite b/sca-cpp/trunk/samples/store-cluster/domains/jane/store.composite index 893b4f0ed4..396970a6e6 100644 --- a/sca-cpp/trunk/samples/store-cluster/domains/jane/store.composite +++ b/sca-cpp/trunk/samples/store-cluster/domains/jane/store.composite @@ -43,6 +43,8 @@ <component name="ShoppingCart"> <t:implementation.python script="shopping-cart.py"/> + <property name="host">localhost</property> + <property name="email">anonymous@localhost</property> <service name="ShoppingCart"> <t:binding.atom uri="shoppingCart"/> </service> @@ -64,6 +66,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211,localhost:11212,localhost:11213</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-cluster/domains/joe/shopping-cart.py b/sca-cpp/trunk/samples/store-cluster/domains/joe/shopping-cart.py index 405adb85bf..e315b2f1ca 100644 --- a/sca-cpp/trunk/samples/store-cluster/domains/joe/shopping-cart.py +++ b/sca-cpp/trunk/samples/store-cluster/domains/joe/shopping-cart.py @@ -19,21 +19,23 @@ import uuid import sys -cartId = "1234" +# Convert a particular host and user email to a cart id +def cartid(host, email): + return ("cart", host(), email()) # Get the shopping cart from the cache # Return an empty cart if not found def getcart(id, cache): - cart = cache("get", (id,)) + cart = cache("get", id) if cart is None: return () return cart # Post a new item to the cart, create a new cart if necessary -def post(collection, item, cache): +def post(collection, item, cache, host, email): id = str(uuid.uuid1()) - cart = ((item[0], id, item[2]),) + getcart(cartId, cache) - cache("put", (cartId,), cart) + cart = ((item[0], id, item[2]),) + getcart(cartid(host, email), cache) + cache("put", cartid(host, email), cart) return (id,) @@ -47,15 +49,15 @@ def find(id, cart): return find(id, cart[1:]) # Get items from the cart -def get(id, cache): +def get(id, cache, host, email): if id == (): - return ("Your Cart", cartId) + getcart(cartId, cache) - return find(id[0], getcart(cartId, cache)) + return ("Your Cart", email()) + getcart(cartid(host, email), cache) + return find(id[0], getcart(cartid(host, email), cache)) # Delete items from the cart -def delete(id, cache): +def delete(id, cache, host, email): if id == (): - return cache("delete", (cartId,)) + return cache("delete", cartid(host, email)) return True # Return the price of an item @@ -69,7 +71,6 @@ def sum(items): return price(items[0]) + sum(items[1:]) # Return the total price of the items in the cart -def gettotal(cache): - cart = getcart(cartId, cache) - return sum(cart) +def gettotal(cache, host, email): + return sum(getcart(cartid(host, email), cache)) diff --git a/sca-cpp/trunk/samples/store-cluster/domains/joe/store.composite b/sca-cpp/trunk/samples/store-cluster/domains/joe/store.composite index 893b4f0ed4..396970a6e6 100644 --- a/sca-cpp/trunk/samples/store-cluster/domains/joe/store.composite +++ b/sca-cpp/trunk/samples/store-cluster/domains/joe/store.composite @@ -43,6 +43,8 @@ <component name="ShoppingCart"> <t:implementation.python script="shopping-cart.py"/> + <property name="host">localhost</property> + <property name="email">anonymous@localhost</property> <service name="ShoppingCart"> <t:binding.atom uri="shoppingCart"/> </service> @@ -64,6 +66,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211,localhost:11212,localhost:11213</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-cluster/ssl-start b/sca-cpp/trunk/samples/store-cluster/ssl-start index 4acd41bc2d..d9d0fec67d 100755 --- a/sca-cpp/trunk/samples/store-cluster/ssl-start +++ b/sca-cpp/trunk/samples/store-cluster/ssl-start @@ -99,9 +99,10 @@ cp `../../modules/http/ssl-ls tmp/ssl` tmp/proxy2/conf ../../modules/http/proxy-ssl-member-conf tmp/proxy2 localhost 8443 ../../modules/http/httpd-start tmp/proxy2 -# Start a shared memcached -# Todo change to one per server -../../components/cache/memcached-start +# Start three memcached servers +../../components/cache/memcached-start 11211 +../../components/cache/memcached-start 11212 +../../components/cache/memcached-start 11213 # Redirect traffic from port 80 to 8091 and use proxy1 #../../ubuntu/ip-redirect-all 80 8091 diff --git a/sca-cpp/trunk/samples/store-cluster/start b/sca-cpp/trunk/samples/store-cluster/start index 809e9edad9..1cd5825471 100755 --- a/sca-cpp/trunk/samples/store-cluster/start +++ b/sca-cpp/trunk/samples/store-cluster/start @@ -71,9 +71,10 @@ EOF ../../modules/http/proxy-member-conf tmp/proxy2 localhost 8103 ../../modules/http/httpd-start tmp/proxy2 -# Start a shared memcached -# Todo change to one per server -../../components/cache/memcached-start +# Start three memcached servers +../../components/cache/memcached-start 11211 +../../components/cache/memcached-start 11212 +../../components/cache/memcached-start 11213 # Redirect traffic from port 80 to 8091 and use proxy1 #../../ubuntu/ip-redirect-all 80 8091 diff --git a/sca-cpp/trunk/samples/store-cluster/stop b/sca-cpp/trunk/samples/store-cluster/stop index 375e5de698..9df37dc948 100755 --- a/sca-cpp/trunk/samples/store-cluster/stop +++ b/sca-cpp/trunk/samples/store-cluster/stop @@ -22,4 +22,6 @@ ../../modules/http/httpd-stop tmp/server3 ../../modules/http/httpd-stop tmp/proxy1 ../../modules/http/httpd-stop tmp/proxy2 -../../components/cache/memcached-stop +../../components/cache/memcached-stop 11211 +../../components/cache/memcached-stop 11212 +../../components/cache/memcached-stop 11213 diff --git a/sca-cpp/trunk/samples/store-cpp/store.composite b/sca-cpp/trunk/samples/store-cpp/store.composite index c9039a1d58..8b5edede99 100644 --- a/sca-cpp/trunk/samples/store-cpp/store.composite +++ b/sca-cpp/trunk/samples/store-cpp/store.composite @@ -64,6 +64,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-java/store.composite b/sca-cpp/trunk/samples/store-java/store.composite index b93e2dbfbf..4b0db9d05c 100644 --- a/sca-cpp/trunk/samples/store-java/store.composite +++ b/sca-cpp/trunk/samples/store-java/store.composite @@ -64,6 +64,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-python/store.composite b/sca-cpp/trunk/samples/store-python/store.composite index 045ebe6ec5..912898123b 100644 --- a/sca-cpp/trunk/samples/store-python/store.composite +++ b/sca-cpp/trunk/samples/store-python/store.composite @@ -64,6 +64,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-scheme/store.composite b/sca-cpp/trunk/samples/store-scheme/store.composite index e461637b2b..fd58ae6c9d 100644 --- a/sca-cpp/trunk/samples/store-scheme/store.composite +++ b/sca-cpp/trunk/samples/store-scheme/store.composite @@ -64,6 +64,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-vhost/domains/jane/store.composite b/sca-cpp/trunk/samples/store-vhost/domains/jane/store.composite index 893b4f0ed4..0c94bd81d2 100644 --- a/sca-cpp/trunk/samples/store-vhost/domains/jane/store.composite +++ b/sca-cpp/trunk/samples/store-vhost/domains/jane/store.composite @@ -64,6 +64,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211</property> </component> </composite> diff --git a/sca-cpp/trunk/samples/store-vhost/domains/joe/store.composite b/sca-cpp/trunk/samples/store-vhost/domains/joe/store.composite index 893b4f0ed4..0c94bd81d2 100644 --- a/sca-cpp/trunk/samples/store-vhost/domains/joe/store.composite +++ b/sca-cpp/trunk/samples/store-vhost/domains/joe/store.composite @@ -64,6 +64,7 @@ <service name="Cache"> <t:binding.atom uri="cache"/> </service> + <property name="servers">localhost:11211</property> </component> </composite> |