<s> """<STR_LIT>""" <EOL> import random <EOL> from zope . interface import implementer <EOL> from twisted . internet . defer import CancelledError <EOL> from flocker . common import timeout <EOL> from . . _interfaces import IRequest <EOL> from . _request_load import ( <EOL> RequestLoadScenario , NoNodesFound <EOL> ) <EOL> from . _rate_measurer import DEFAULT_SAMPLE_SIZE <EOL> class DatasetCreationTimeout ( Exception ) : <EOL> """<STR_LIT>""" <EOL> @ implementer ( IRequest ) <EOL> class WriteRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reactor , control_service , timeout = <NUM_LIT:10> ) : <EOL> self . control_service = control_service <EOL> self . reactor = reactor <EOL> self . timeout = timeout <EOL> self . nodes = None <EOL> self . dataset_node = None <EOL> self . dataset_id = "<STR_LIT>" <EOL> def _create_dataset ( self , node ) : <EOL> """<STR_LIT>""" <EOL> self . dataset_node = node <EOL> creating = self . control_service . create_dataset ( <EOL> primary = node . uuid ) <EOL> def handle_timeout_and_errors ( failure ) : <EOL> failure . trap ( CancelledError ) <EOL> raise DatasetCreationTimeout ( ) <EOL> timeout ( self . reactor , creating , self . timeout ) <EOL> creating . addErrback ( handle_timeout_and_errors ) <EOL> return creating <EOL> def _get_dataset_node ( self , nodes ) : <EOL> """<STR_LIT>""" <EOL> if not nodes : <EOL> raise NoNodesFound ( ) <EOL> return random . choice ( nodes ) <EOL> def _set_dataset_id ( self , dataset ) : <EOL> """<STR_LIT>""" <EOL> self . dataset_id = dataset . dataset_id <EOL> def run_setup ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . control_service . list_nodes ( ) <EOL> d . addCallback ( self . _get_dataset_node ) <EOL> d . addCallback ( self . _create_dataset ) <EOL> d . addCallback ( self . _set_dataset_id ) <EOL> return d <EOL> def make_request ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . dataset_node is None : <EOL> raise DatasetCreationTimeout ( ) <EOL> return self . control_service . move_dataset ( <EOL> self . dataset_node . uuid , <EOL> self . dataset_id <EOL> ) <EOL> def run_cleanup ( self ) : <EOL> return self . control_service . delete_dataset ( self . dataset_id ) <EOL> def write_request_load_scenario ( reactor , cluster , request_rate = <NUM_LIT:10> , <EOL> sample_size = DEFAULT_SAMPLE_SIZE , timeout = <NUM_LIT> , <EOL> tolerance_percentage = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return RequestLoadScenario ( <EOL> reactor , <EOL> WriteRequest ( reactor , cluster . get_control_service ( reactor ) ) , <EOL> request_rate = request_rate , <EOL> sample_size = sample_size , <EOL> timeout = timeout , <EOL> tolerance_percentage = tolerance_percentage , <EOL> ) </s>
