'Performs a new rebalancing operation with the master server.'

def _DoRebalance(self, new_mapping):

    print 'Contacting    master    server    to    start    re-sharding...',
    pool = None
    try:
        pool = urllib3.connectionpool.HTTPConnectionPool(self.addr, port=self.port)
    except urllib3.exceptions.MaxRetryError:
        print 'Unable    to    contact    master...'
        return
    body = new_mapping.SerializeToString()
    headers = {'Content-Length': len(body)}
    res = None
    try:
        res = pool.urlopen('POST', '/rebalance/phase1', headers=headers, body=body)
    except urllib3.exceptions.MaxRetryError:
        print 'Unable    to    talk    with    master...'
        pool.close()
        return
    if (res.status != constants.RESPONSE_OK):
        print 'Re-sharding    cannot    be    done!'
        return
    rebalance = rdf_data_server.DataServerRebalance.FromSerializedString(res.data)
    print 'OK'
    print
    print 'The    following    servers    will    need    to    move    data:'
    for (i, move) in enumerate(list(rebalance.moving)):
        print ('Server    %d    moves    %dKB' % (i, (move / 1024)))
    answer = raw_input('Proceed    with    re-sharding?    (y/n)    ')
    if (answer != 'y'):
        return
    body = rebalance.SerializeToString()
    headers = {'Content-Length': len(body)}
    try:
        res = pool.urlopen('POST', '/rebalance/phase2', headers=headers, body=body)
    except urllib3.exceptions.MaxRetryError:
        print 'Unable    to    contact    server    for    re-sharding.'
        print 'Make    sure    the    data    servers    are    up    and    try    again.'
        return
    if (res.status != constants.RESPONSE_OK):
        print 'Could    not    start    copying    files    for    re-sharding'
        print 'Make    sure    the    data    servers    are    up    and    try    again.'
        return
    try:
        res = pool.urlopen('POST', '/rebalance/commit', headers=headers, body=body)
    except urllib3.exceptions.MaxRetryError:
        print ('Could    not    commit    the    re-sharding    transaction    with    id    %s' % rebalance.id)
        print 'Make    sure    the    data    servers    are    up    and    then    run:'
        print ("'recover    %s'    in    order    to    re-run    transaction" % rebalance.id)
        return
    if (res.status != constants.RESPONSE_OK):
        print ('Could    not    commit    the    transaction    %s' % rebalance.id)
        print 'Make    sure    the    data    servers    are    up    and    then    run:'
        print ("'recover    %s'    in    order    to    re-run    transaction" % rebalance.id)
        return
    self.mapping = rdf_data_server.DataServerMapping.FromSerializedString(res.data)
    print ('Rebalance    with    id    %s    fully    performed.' % rebalance.id)
