#!/usr/bin/bash

# Nothing to do if gluster's not installed
command -v gluster &>/dev/null || exit 0

MEMMB=$(
cat /proc/meminfo <(ulimit -m) <(ulimit -v) \
 | awk '/^[0-9]+$/{print int($1/1024)}; /^MemTotal:/{print int($2/1024)}' \
 | sort -n \
 | head -1
)

# Note that setting the cache too high may hit https://bugzilla.redhat.com/show_bug.cgi?id=1040494
# how high is too high? >= the total available memory 
CACHE_SIZE=$[MEMMB*15/100]

# To tune this properly we need to know the head-end instance sizing, which is tricky so for
# now we'll just guess that 512MiB is reasonable
#  - TC 2016-05-31
CACHE_SIZE=512

for volume in $(gluster volume list)
do
	gluster volume set $volume performance.cache-size "${CACHE_SIZE}MB" >/dev/null
	gluster volume set $volume performance.stat-prefetch on >/dev/null
	gluster volume set $volume performance.io-cache on >/dev/null
	gluster volume set $volume performance.quick-read on >/dev/null

	# Disable self-heal
	gluster volume set $volume cluster.data-self-heal off >/dev/null
	gluster volume set $volume cluster.entry-self-heal off >/dev/null
	gluster volume set $volume cluster.metadata-self-heal off >/dev/null
done
