~nobuto/charms/precise/memcached/fix-a-typo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh

set -ue

tfile=`mktemp /etc/.memcached.conf.XXXXXXX`
cat > $tfile <<EOF
################################
#
# This config file generated by the juju memcached charm. Changes
# may not be preserved.
#
################################
#
# memcached default config file
# 2003 - Jay Bonci <jaybonci@debian.org>
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution. 

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d

# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log

# Be verbose
# -v

# Be even more verbose (print client commands as well)
# -vv

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m `config-get size`

# Default connection port is 11211
-p `config-get tcp-port`

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache

# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 0.0.0.0

# Limit the number of simultaneous incoming connections. The daemon default is 1024
-c `config-get connection-limit`

# Lock down all paged memory. Consult with the README and homepage before you do this
# -k

# Return error when memory is exhausted (rather than removing items)
# -M

# Maximize core file limit
# -r
EOF

append_numeric() {
    local name=$1
    local opt=$2
    local target=$3
    val=`config-get $name`
    if [ "$val" != "-1" ] ; then
        cat >> $target <<EOF
# $name
$opt $val
EOF
    fi
}

append_bool() {
    local name=$1
    local opt=$2
    local target=$3
    val=`config-get $name`
    if [ "$val" = "yes" ] || [  "$val" = "Y" ] ; then
        cat >> $target <<EOF
# $name
$opt
EOF
    fi
}

append_numeric "request-limit" "-R" $tfile
append_numeric "min-item-size" "-n" $tfile
append_numeric "slab-page-size" "-I" $tfile
append_numeric "threads" "-t" $tfile
append_bool "disable-auto-cleanup" "-M" $tfile
append_bool "disable-cas" "-C" $tfile

factor=`config-get factor`
if [ "$factor" != "-1.0" ] ; then
    cat >> $tfile <<EOF
# factor
-f $factor
EOF
fi

size=`config-get size`
if [ $size -ge 2048 ] ; then
    disableLP=`config-get disable-large-pages`
    if [ "$disableLP" != "yes" ] && [ "$disableLP" != "Y" ] ; then
        juju-log "Memory >= 2GB, using large-pages to speed up access"
        # First try to reserve them
        save_hugepages=`sysctl -n vm.nr_hugepages`
        npages=$(($size/2))
        sysctl -w vm.nr_hugepages=$npages
        allocated=`sysctl -n vm.nr_hugepages`
        if [ $allocated -lt $npages ] ; then
            juju-log -l WARNING "Cannot allocate $npages contiguous pages for huge page allocation"
            juju-log -l WARNING "Setting vm.nr_hugepages back to $save_hugepages"
            sysctl -w vm.nr_hugepages=$save_hugepages
            juju-log -l WARNING "Will disable large pages and fall back to regular memory"
        else
            juju-log "Allocated $npages contiguous blocks only for huge pages."
            cat >> $tfile <<EOF
# large pages can be disabled with disable-large-pages
-L
EOF
        fi
    fi
fi

new_hash=`md5sum $tfile|cut -d' ' -f1`
old_hash=`md5sum /etc/memcached.conf|cut -d' ' -f1`
if [ "$new_hash" != "$old_hash" ] ; then
    juju-log "New config generated. hash=$new_hash"
    mv -f /etc/memcached.conf /etc/memcached.conf.$old_hash
    mv -f $tfile /etc/memcached.conf
    service memcached restart
else
    juju-log "Config not changed. hash=$new_hash"
fi

tcp_port=`config-get tcp-port`
udp_port=`config-get udp-port`

# Work around http://pad.lv/900517
[ -n "$tcp_port" ] || tcp_port=0
[ -n "$udp_port" ] || udp_port=0

if [ $tcp_port -gt 0 ] ; then
    open-port $tcp_port/TCP
fi

if [ $udp_port -gt 0 ] ; then
    open-port $udp_port/UDP
fi
# In case port changed, inform consumers
hooks/cache-relation-joined