<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import re <EOL> import optparse <EOL> import random <EOL> import time <EOL> import signal <EOL> import socket <EOL> from subprocess import call <EOL> CURRENT_DIR = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> def main ( ) : <EOL> nodes = get_nodes ( ) <EOL> leader = True if len ( sys . argv ) == <NUM_LIT:2> and sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' else False <EOL> while True : <EOL> if leader : <EOL> n = find_leader ( nodes ) <EOL> else : <EOL> n = random . choice ( nodes ) <EOL> kill_node ( n ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> start_node ( n ) <EOL> wait_join_cluster ( n ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> def find_leader ( nodes ) : <EOL> for host , port in nodes : <EOL> try : <EOL> s = socket . socket ( ) <EOL> s . connect ( ( host , int ( port ) ) ) <EOL> s . send ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in s . recv ( <NUM_LIT> ) : <EOL> return ( host , port ) <EOL> finally : <EOL> s . close ( ) <EOL> def kill_node ( n ) : <EOL> print '<STR_LIT>' % ( n , ) <EOL> host , port = n <EOL> call ( [ '<STR_LIT>' % port ] , shell = True ) <EOL> def start_node ( n ) : <EOL> print '<STR_LIT>' % ( n , ) <EOL> host , port = n <EOL> call ( [ '<STR_LIT>' % port ] , shell = True ) <EOL> def wait_join_cluster ( n ) : <EOL> print '<STR_LIT>' <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> def get_nodes ( ) : <EOL> nodes = [ ] <EOL> for f in os . listdir ( CURRENT_DIR ) : <EOL> if os . path . isdir ( f ) : <EOL> m = re . match ( '<STR_LIT>' , f ) <EOL> if m is not None : <EOL> nodes . append ( m . group ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> return nodes <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
