Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 3 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 7 | |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 8 | """This script copies all data from one index into another, and updates the |
| 9 | alias to point to the new index. |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 10 | |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 11 | usage: es_reindex.py [-h] [--host HOST] [--port PORT] [--old OLD] |
| 12 | [--new NEW] [--alias ALIAS] |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 13 | |
| 14 | optional arguments: |
| 15 | -h, --help show this help message and exit |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 16 | --host HOST name of ES server. |
| 17 | --port PORT |
| 18 | --old OLD Name of the old index. |
| 19 | --new NEW Name of the new index. |
| 20 | --alias ALIAS alias to be pointed to the new index. |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 21 | |
| 22 | """ |
| 23 | |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 24 | import argparse |
| 25 | |
| 26 | import common |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 27 | from elasticsearch import Elasticsearch |
| 28 | from elasticsearch import helpers |
Gabe Black | b72f4fb | 2015-01-20 16:47:13 -0800 | [diff] [blame] | 29 | from autotest_lib.client.common_lib.cros.graphite import autotest_es |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def main(): |
| 33 | """main script. """ |
| 34 | |
| 35 | parser = argparse.ArgumentParser() |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 36 | parser.add_argument('--host', type=str, dest='host', |
| 37 | help='name of ES server.') |
| 38 | parser.add_argument('--port', type=str, dest='port', default=9200) |
| 39 | parser.add_argument('--old', type=str, dest='old', |
| 40 | help='Name of the old index.') |
| 41 | parser.add_argument('--new', type=str, dest='new', |
| 42 | help='Name of the new index.') |
| 43 | parser.add_argument('--alias', type=str, dest='alias', |
| 44 | help='alias to be pointed to the new index.') |
| 45 | |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 46 | options = parser.parse_args() |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 47 | |
| 48 | query = {'query' : {'match_all' : {}}, |
| 49 | 'size': 1} |
| 50 | |
Gabe Black | b72f4fb | 2015-01-20 16:47:13 -0800 | [diff] [blame] | 51 | result = autotest_es.execute_query(index=options.old, host=options.host, |
| 52 | port=options.port, query) |
| 53 | print 'Total number of records in index %s: %d' % (options.old, |
| 54 | result.total) |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 55 | |
| 56 | print ('Re-index: %s to index: %s for server %s:%s' % |
| 57 | (options.old, options.new, options.host, options.port)) |
| 58 | |
| 59 | client = Elasticsearch(hosts=[{'host': options.host, 'port': options.port}]) |
| 60 | helpers.reindex(client, options.old, options.new) |
| 61 | print 'reindex completed.' |
| 62 | |
| 63 | print 'Checking records in the new index...' |
Gabe Black | b72f4fb | 2015-01-20 16:47:13 -0800 | [diff] [blame] | 64 | result = es.execute_query(index=options.new, host=options.host, |
| 65 | port=options.port, query) |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 66 | print 'Total number of records in index %s: %d' % (options.new, |
Gabe Black | b72f4fb | 2015-01-20 16:47:13 -0800 | [diff] [blame] | 67 | result.total) |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 68 | |
| 69 | # count_new can be larger than count if new records are added during |
| 70 | # reindexing. This check only tries to make sure no record was lost. |
| 71 | if count > count_new: |
Dan Shi | bc08d13 | 2014-10-27 09:28:55 -0700 | [diff] [blame] | 72 | raise Exception('Error! There are %d records missing after reindexing. ' |
| 73 | 'Alias will not be updated to the new index. You might ' |
| 74 | 'want to try reindex again.' % |
| 75 | (count - count_new)) |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 76 | |
| 77 | body = {'actions': [{'remove': {'alias': options.alias, |
| 78 | 'index': options.old}}, |
| 79 | {'add': {'alias': options.alias, |
| 80 | 'index': options.new}} |
| 81 | ] |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 82 | } |
Dan Shi | b8a24d5 | 2014-10-21 14:40:02 -0700 | [diff] [blame] | 83 | client.indices.update_aliases(body=body) |
| 84 | print 'alias is updated.' |
| 85 | print ('Please verify the new index is working before deleting old index ' |
| 86 | 'with command:\n.curl -XDELETE %s:%s/%s' % |
| 87 | (options.host, options.port, options.old)) |
Michael Liang | 6fb58e4 | 2014-08-15 11:40:11 -0700 | [diff] [blame] | 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | main() |