<commit_before>#!/bin/bash

# common functions used by postgres backup scripts
echo ""   # blank line in log file helps scroll btwn instances
source /root/.bashrc # source in the manta configs such as the url and credentials
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
set -o pipefail

PATH=/opt/smartdc/manatee/build/node/bin:/opt/local/bin:/usr/sbin/:/usr/bin:/usr/sbin:/usr/bin:/opt/smartdc/registrar/build/node/bin:/opt/smartdc/manatee/node_modules/.bin:/opt/smartdc/registrar/node_modules/.bin:/opt/smartdc/manatee/lib/tools:/opt/smartdc/manatee/pg_dump/

FATAL=
CFG=/opt/smartdc/manatee/etc/backup.json
DATASET=
DATE=
DUMP_DATASET=
DUMP_DIR=/var/tmp/upload/$(uuid)
MANATEE_LOCK=/opt/smartdc/manatee/node_modules/node-manatee/bin/manatee-lock
MANATEE_STAT=manatee-stat
MANTA_DIR_PREFIX=/poseidon/stor/manatee_backups
MMKDIR=mmkdir
MPUT=mput
MY_IP=
LOCK_PATH=/pg_dump_lock
PG_DIR=
PG_DIR_SIZE=
PG_PID=
SHARD_NAME=
PG_START_TIMEOUT=$2 || 10
PG_START_MAX_TRIES=50
PG_START_TRIES=0
UPLOAD_SNAPSHOT=
ZFS_CFG=/opt/smartdc/manatee/etc/snapshotter.json
ZFS_SNAPSHOT=$1
ZK_IP=

function finish {
    if [[ $FATAL -ne 1 ]]; then
        rm -rf $DUMP_DIR
    fi
    kill -9 $PG_PID
    zfs destroy -R $DUMP_DATASET
}
trap finish EXIT

function fatal
{
    FATAL=1
    echo "$(basename $0): fatal error: $*"
    kill -9 $PG_PID
    zfs destroy -R $DUMP_DATASET
    exit 1
}

function check_lock
{
    $MANATEE_LOCK $LOCK_PATH $ZK_IP
    [[ $? -eq 0 ]] || fatal "lock either exists or unable to check lock"
}

function take_zfs_snapshot
{
    echo "take a snapshot"
    ZFS_SNAPSHOT=$DATASET@$(date +%s)000
    zfs snapshot $ZFS_SNAPSHOT
    [[ $? -eq 0 ]] || fatal "Unable to create a snapshot"
}

function upload_zfs_snapshot
{
    # only upload the snapshot if the flag is set
    if [[ $UPLOAD_SNAPSHOT -eq 1 ]]; then
        local snapshot_size=$(zfs list -Hp -o refer -t snapshot $ZFS_SNAPSHOT)
        [[ $? -eq 0 ]] || return "Unable to retrieve snapshot size"
        # pad the snapshot_size by 5% since there's some zfs overhead, note the
        # last bit just takes the floor of the floating point value
        local snapshot_size=$(echo "$snapshot_size * 1.05" | bc | cut -d '.' -f1)
        [[ -n "$snapshot_size" ]] || return "Unable to calculate snapshot size"
        local dir=$MANTA_DIR_PREFIX/$SHARD_NAME/$(date -u +%Y/%m/%d/%H)
        $MMKDIR -p -u $MANTA_URL -a $MANTA_USER -k $MANTA_KEY_ID $dir
        [[ $? -eq 0 ]] || return "unable to create backup dir"
        echo "sending snapshot $ZFS_SNAPSHOT to manta"
        local snapshot_manta_name=$(echo $ZFS_SNAPSHOT | gsed -e 's|\/|\-|g')
        zfs send $ZFS_SNAPSHOT | $MPUT $dir/$snapshot_manta_name -H "max-content-length: $snapshot_size"
        [[ $? -eq 0 ]] || return "unable to send snapshot $ZFS_SNAPSHOT"

        echo "successfully backed up snapshot $ZFS_SNAPSHOT to manta file $dir/$snapshot_manta_name"
    fi

    return 0
}

function mount_data_set
{
    # destroy the dump dataset if it already exists
    zfs destroy -R $DUMP_DATASET
    # clone the current snapshot
    zfs clone $ZFS_SNAPSHOT $DUMP_DATASET
    [[ $? -eq 0 ]] || fatal "unable to clone snapshot"
    echo "successfully mounted dataset"
    # remove recovery.conf so this pg instance does not become a slave
    rm -f $PG_DIR/recovery.conf
    # remove postmaster.pid
    rm -f $PG_DIR/postmaster.pid
    # get pg dir size
    PG_DIR_SIZE=$(du -s $PG_DIR | cut -f1)

    ctrun -o noorphan sudo -u postgres postgres -D $PG_DIR -p 23456 -c logging_collector=off &
    PG_PID=$!
    [[ $? -eq 0 ]] || fatal "unable to start postgres"

    wait_for_pg_start
}

function wait_for_pg_start<commit_msg>MANATEE-222 increase pg dump performance by tuning filesystem and postgres<commit_after>#!/bin/bash

# common functions used by postgres backup scripts
echo ""   # blank line in log file helps scroll btwn instances
source /root/.bashrc # source in the manta configs such as the url and credentials
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
set -o pipefail

PATH=/opt/smartdc/manatee/build/node/bin:/opt/local/bin:/usr/sbin/:/usr/bin:/usr/sbin:/usr/bin:/opt/smartdc/registrar/build/node/bin:/opt/smartdc/manatee/node_modules/.bin:/opt/smartdc/registrar/node_modules/.bin:/opt/smartdc/manatee/lib/tools:/opt/smartdc/manatee/pg_dump/

FATAL=
CFG=/opt/smartdc/manatee/etc/backup.json
DATASET=
DATE=
DUMP_DATASET=
DUMP_DIR=/var/tmp/upload/$(uuid)
MANATEE_LOCK=/opt/smartdc/manatee/node_modules/node-manatee/bin/manatee-lock
MANATEE_STAT=manatee-stat
MANTA_DIR_PREFIX=/poseidon/stor/manatee_backups
MMKDIR=mmkdir
MPUT=mput
MY_IP=
LOCK_PATH=/pg_dump_lock
PG_DIR=
PG_DIR_SIZE=
PG_PID=
SHARD_NAME=
PG_START_TIMEOUT=$2 || 10
PG_START_MAX_TRIES=50
PG_START_TRIES=0
UPLOAD_SNAPSHOT=
ZFS_CFG=/opt/smartdc/manatee/etc/snapshotter.json
ZFS_SNAPSHOT=$1
ZK_IP=

function finish {
    if [[ $FATAL -ne 1 ]]; then
        rm -rf $DUMP_DIR
    fi
    kill -9 $PG_PID
    zfs destroy -R $DUMP_DATASET
}
trap finish EXIT

function fatal
{
    FATAL=1
    echo "$(basename $0): fatal error: $*"
    kill -9 $PG_PID
    zfs destroy -R $DUMP_DATASET
    exit 1
}

function check_lock
{
    $MANATEE_LOCK $LOCK_PATH $ZK_IP
    [[ $? -eq 0 ]] || fatal "lock either exists or unable to check lock"
}

function take_zfs_snapshot
{
    echo "take a snapshot"
    ZFS_SNAPSHOT=$DATASET@$(date +%s)000
    zfs snapshot $ZFS_SNAPSHOT
    [[ $? -eq 0 ]] || fatal "Unable to create a snapshot"
}

function upload_zfs_snapshot
{
    # only upload the snapshot if the flag is set
    if [[ $UPLOAD_SNAPSHOT -eq 1 ]]; then
        local snapshot_size=$(zfs list -Hp -o refer -t snapshot $ZFS_SNAPSHOT)
        [[ $? -eq 0 ]] || return "Unable to retrieve snapshot size"
        # pad the snapshot_size by 5% since there's some zfs overhead, note the
        # last bit just takes the floor of the floating point value
        local snapshot_size=$(echo "$snapshot_size * 1.05" | bc | cut -d '.' -f1)
        [[ -n "$snapshot_size" ]] || return "Unable to calculate snapshot size"
        local dir=$MANTA_DIR_PREFIX/$SHARD_NAME/$(date -u +%Y/%m/%d/%H)
        $MMKDIR -p -u $MANTA_URL -a $MANTA_USER -k $MANTA_KEY_ID $dir
        [[ $? -eq 0 ]] || return "unable to create backup dir"
        echo "sending snapshot $ZFS_SNAPSHOT to manta"
        local snapshot_manta_name=$(echo $ZFS_SNAPSHOT | gsed -e 's|\/|\-|g')
        zfs send $ZFS_SNAPSHOT | $MPUT $dir/$snapshot_manta_name -H "max-content-length: $snapshot_size"
        [[ $? -eq 0 ]] || return "unable to send snapshot $ZFS_SNAPSHOT"

        echo "successfully backed up snapshot $ZFS_SNAPSHOT to manta file $dir/$snapshot_manta_name"
    fi

    return 0
}

function mount_data_set
{
    # destroy the dump dataset if it already exists
    zfs destroy -R $DUMP_DATASET
    # clone the current snapshot. Disable sync for faster performance.
    zfs clone -o sync=disabled $ZFS_SNAPSHOT $DUMP_DATASET
    [[ $? -eq 0 ]] || fatal "unable to clone snapshot"
    echo "successfully mounted dataset"
    # remove recovery.conf so this pg instance does not become a slave
    rm -f $PG_DIR/recovery.conf
    # remove postmaster.pid
    rm -f $PG_DIR/postmaster.pid
    # get pg dir size
    PG_DIR_SIZE=$(du -s $PG_DIR | cut -f1)

    ctrun -o noorphan sudo -u postgres postgres -D $PG_DIR -p 23456 \
        -c logging_collector=off -c fsync=off -c synchronous_commit=off \
        -c checkpoint_segments=100 -c checkpoint_timeout=1h &
    PG_PID=$!
    [[ $? -eq 0 ]] || fatal "unable to start postgres"

    wait_for_pg_start
}

function wait_for_pg_start
{'fraction_non_alphanumeric': 0.12047089601046436, 'fraction_numerical': 0.011118378024852845, 'mean_word_length': 3.3816618911174787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1976494', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2892, 'n_words': 741}