Commit 74d6f030 authored by Xiubo Li's avatar Xiubo Li Committed by Ilya Dryomov

ceph: fix geting random mds from mdsmap

For example, if we have 5 mds in the mdsmap and the states are:
m_info[5] --> [-1, 1, -1, 1, 1]

If we get a random number 1, then we should get the mds index 3 as
expected, but actually we will get index 2, which the state is -1.

The issue is that the for loop increment will advance past any "up"
MDS that was found during the while loop search.
Signed-off-by: default avatarXiubo Li <xiubli@redhat.com>
Reviewed-by: default avatarJeff Layton <jlayton@kernel.org>
Signed-off-by: default avatarIlya Dryomov <idryomov@gmail.com>
parent 6b0a8774
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m) int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
{ {
int n = 0; int n = 0;
int i; int i, j;
/* special case for one mds */ /* special case for one mds */
if (1 == m->m_num_mds && m->m_info[0].state > 0) if (1 == m->m_num_mds && m->m_info[0].state > 0)
...@@ -35,9 +35,12 @@ int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m) ...@@ -35,9 +35,12 @@ int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
/* pick */ /* pick */
n = prandom_u32() % n; n = prandom_u32() % n;
for (i = 0; n > 0; i++, n--) for (j = 0, i = 0; i < m->m_num_mds; i++) {
while (m->m_info[i].state <= 0) if (m->m_info[i].state > 0)
i++; j++;
if (j > n)
break;
}
return i; return i;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment