summaryrefslogtreecommitdiffstats
path: root/extras/ec-heal-script/gfid_needing_heal_parallel.sh
blob: d7f53c97c3379174a9ed8986a1fbc2f044a4b42f (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
#  Copyright (c) 2019-2020 Red Hat, Inc. <http://www.redhat.com>
#  This file is part of GlusterFS.
#
#  This file is licensed to you under your choice of the GNU Lesser
#  General Public License, version 3 or any later version (LGPLv3 or
#  later), or the GNU General Public License, version 2 (GPLv2), in all
#  cases as published by the Free Software Foundation.

# This script provides a list of all the files which can be healed or not healed.
# It also generates two files, potential_heal and can_not_heal, which contains the information
# of all theose files. These files could be used by correct_pending_heals.sh to correct
# the fragmnets so that files could be healed by shd.

CAN_NOT_HEAL="can_not_heal"
CAN_HEAL="potential_heal"
LINE_SEP="==================================================="
LOG_DIR="/var/log/glusterfs"
LOG_FILE="$LOG_DIR/ec-heal-script.log"

function heal_log()
{
    echo "$1" >> "$LOG_FILE"
}

function _init ()
{
    if [ $# -ne 1 ]; then
    echo "usage: $0 <gluster volume name>";
    echo "This script provides a list of all the files which can be healed or not healed.
It also generates two files, potential_heal and can_not_heal, which contains the information
of all theose files. These files could be used by correct_pending_heals.sh to correct
the fragmnets so that files could be healed by shd."
    exit 2;
    fi

    volume=$1;
}

function get_pending_entries ()
{
    local volume_name=$1

    gluster volume heal "$volume_name" info | grep -v ":/" | grep -v "Number of entries" | grep -v "Status:" | sort -u | sed '/^$/d'
}

function get_entry_path_on_brick()
{
    local path="$1"
    local gfid_string=""
    if [[ "${path:0:1}" == "/" ]];
    then
        echo "$path"
    else
        gfid_string="$(echo "$path" | cut -f2 -d':' | cut -f1 -d '>')"
        echo "/.glusterfs/${gfid_string:0:2}/${gfid_string:2:2}/$gfid_string"
    fi
}

function run_command_on_server()
{
	local subvolume="$1"
	local host="$2"
	local cmd="$3"
	local output
        output=$(ssh -n "${host}" "${cmd}")
	if [ -n "$output" ]
	then
            echo "$subvolume:$output"
	fi
}

function get_entry_path_all_bricks ()
{
    local entry="$1"
    local bricks="$2"
    local cmd=""
    for brick in $bricks
    do
	    echo "${brick}#$(get_entry_path_on_brick "$entry")"
    done | tr '\n' ','
}

function get_stat_for_entry_from_all_bricks ()
{
    local entry="$1"
    local bricks="$2"
    local subvolume=0
    local host=""
    local bpath=""
    local cmd=""

    for brick in $bricks
    do
        if [[ "$((subvolume % 6))" == "0" ]]
        then
            subvolume=$((subvolume+1))
        fi
        host=$(echo "$brick" | cut -f1 -d':')
        bpath=$(echo "$brick" | cut -f2 -d':')

	cmd="stat --format=%F:%B:%s $bpath$(get_entry_path_on_brick "$entry") 2>/dev/null"
    run_command_on_server "$subvolume" "${host}" "${cmd}" &
    done | sort | uniq -c | sort -rnk1
}

function get_bricks_from_volume()
{
    local v=$1
    gluster volume info "$v" | grep -E "^Brick[0-9][0-9]*:" | cut -f2- -d':'
}

function print_entry_gfid()
{
    local host="$1"
    local dirpath="$2"
    local entry="$3"
    local gfid
    gfid="$(ssh -n "${host}" "getfattr -d -m. -e hex $dirpath/$entry 2>/dev/null | grep trusted.gfid=|cut -f2 -d'='")"
    echo "$entry" - "$gfid"
}

function print_brick_directory_info()
{
    local h="$1"
    local dirpath="$2"
    while read -r e
    do
        print_entry_gfid "${h}" "${dirpath}" "${e}"
    done < <(ssh -n "${h}" "ls $dirpath 2>/dev/null")
}

function print_directory_info()
{
    local entry="$1"
    local bricks="$2"
    local h
    local b
    local gfid
    for brick in $bricks;
    do
        h="$(echo "$brick" | cut -f1 -d':')"
        b="$(echo "$brick" | cut -f2 -d':')"
        dirpath="$b$(get_entry_path_on_brick "$entry")"
	print_brick_directory_info "${h}" "${dirpath}" &
    done | sort | uniq -c
}

function print_entries_needing_heal()
{
    local quorum=0
    local entry="$1"
    local bricks="$2"
    while read -r line
    do
        quorum=$(echo "$line" | awk '{print $1}')
        if [[ "$quorum" -lt 4 ]]
        then
            echo "$line - Not in Quorum"
        else
            echo "$line - In Quorum"
        fi
    done < <(print_directory_info "$entry" "$bricks")
}

function find_file_paths ()
{
    local bpath=$1
    local file_entry=""
    local file_host=""
    local cmd=""
    local brick_root=""
    local gfid=""
    local actual_path=""
    local gfid_path=""

    file_host=$(echo "$bpath" | cut -d ":" -f 1)
    file_entry=$(echo "$bpath" | cut -d ":" -f 2)
    brick_root=$(echo "$file_entry" | cut -d "#" -f 1)

    gfid=$(echo "${file_entry}" | grep ".glusterfs")

    if [[ -n "$gfid" ]]
    then
        gfid_path=$(echo "$file_entry" | cut -d "#" -f 2)
        file_entry=${file_entry//#}
        cmd="find -L '$brick_root' -samefile '$file_entry' 2>/dev/null | grep -v '.glusterfs' "
        actual_path=$(ssh -n "${file_host}" "${cmd}")
        #removing absolute path so that user can refer this from mount point
        actual_path=${actual_path#"$brick_root"}
    else
        actual_path=$(echo "$file_entry" | cut -d "#" -f 2)
        file_entry=${file_entry//#}
        cmd="find -L '$brick_root' -samefile '$file_entry' 2>/dev/null | grep '.glusterfs' "
        gfid_path=$(ssh -n "${file_host}" "${cmd}")
        gfid_path=${gfid_path#"$brick_root"}
    fi

    echo "${gfid_path}|${actual_path}"
}

function log_can_not_heal ()
{
    local gfid_actual_paths=$1
    local file_paths=$2
    file_paths=${file_paths//#}

    echo "${LINE_SEP}" >> "$CAN_NOT_HEAL"
    echo "Can Not Heal : $(echo "$gfid_actual_paths" | cut -d '|' -f 2)" >> "$CAN_NOT_HEAL"
    for bpath in ${file_paths//,/ }
    do
        echo "${bpath}" >> "$CAN_NOT_HEAL"
    done
}

function main ()
{
    local bricks=""
    local quorum=0
    local stat_info=""
    local file_type=""
    local gfid_actual_paths=""
    local bpath=""
    local file_paths=""
    local good=0
    local bad=0
    bricks=$(get_bricks_from_volume "$volume")
    rm -f "$CAN_HEAL"
    rm -f "$CAN_NOT_HEAL"
    mkdir "$LOG_DIR" -p

    heal_log "Started $(basename "$BASH_SOURCE") on $(date) "
    while read -r heal_entry
    do
        heal_log "------------------------------------------------------------------"
        heal_log "$heal_entry"

        gfid_actual_paths=""
        file_paths="$(get_entry_path_all_bricks "$heal_entry" "$bricks")"
        stat_info="$(get_stat_for_entry_from_all_bricks "$heal_entry" "$bricks")"
        heal_log "$stat_info"

        quorum=$(echo "$stat_info" | head -1 | awk '{print $1}')
        good_stat=$(echo "$stat_info" | head -1 | awk '{print $3}')
        file_type="$(echo "$stat_info" | head -1 | cut -f2 -d':')"
        if [[ "$file_type" == "directory" ]]
        then
            print_entries_needing_heal "$heal_entry" "$bricks"
        else
            if [[ "$quorum" -ge 4 ]]
            then
                good=$((good + 1))
                heal_log "Verdict: Healable"

                echo "${good_stat}|$file_paths" >> "$CAN_HEAL"
            else
                bad=$((bad + 1))
                heal_log "Verdict: Not Healable"
                for bpath in ${file_paths//,/ }
                do
                    if [[ -z "$gfid_actual_paths" ]]
                    then
                        gfid_actual_paths=$(find_file_paths "$bpath")
                    else
                        break
                    fi
                done
                log_can_not_heal "$gfid_actual_paths" "${file_paths}"
            fi
        fi
    done < <(get_pending_entries "$volume")
    heal_log "========================================="
    heal_log "Total number of  potential heal : ${good}"
    heal_log "Total number of can not heal    : ${bad}"
    heal_log "========================================="
}

_init "$@" && main "$@"