Beispiel #1
0
def main(args=None):
    print("进行神经网络鲁棒性增强")
    pause()
    press_any_key_exit("任意键以继续\n")

    #模型准备
    model = model_select(FLAGS.model_path)  # 输入模型路径

    #如果重新训练模型

    #数据集准备
    # dataset_1=Dataset_select(dap.Setup_mnist_fashion())#数据集处理方法修改,输入数据集路径
    # dataset=Dataset_select(da.Setup_mnist())#选择数据集
    dataset = da.Setup_mnist(train_start=0,
                             train_end=60000,
                             test_start=0,
                             test_end=10000)
    #模型再训练
    # model_defend_display,model_up_time=model_robust_strengthen(model, FLAGS.model_path, dataset)#模型防御方法选择
    # model_defend,_=model_robust_strengthen(model, FLAGS.model_path, dataset)#模型防御方法选择

    #如果不训练,单独读取某个以训练好的模型
    model_defend = model_select(FLAGS.model_defend_path)

    #测试防御效果
    eval = evaluation(model, dataset, model_defend, BIM)

    origin_clean_acc = eval.model_clean_eval()

    origin_adv_acc = eval.model_adv_eval()

    strengthen_clean_acc = eval.model_defend_clean_eval()

    strengthen_adv_acc = eval.model_defend_adv_eval()
Beispiel #2
0
    def ask_employee(self, inputtxt=None):
        """Ask the title for the task"""

        while_breaker = 1

        while True:

            # This helps the testing, since these are kind of
            # infinite loops while testing, there's need for something
            # that can break them automatically
            if while_breaker > 100:
                break
            else:
                while_breaker += 1

            utils.cls()
            name = utils.get_input(inputtxt)

            if not name:
                utils.pause('Error: Blanks spaces are not allowed')
                continue

            try:
                employee = Employee.get(Employee.name == name)
            except Employee.DoesNotExist:
                employee = Employee.create(name=name)

            return employee.id_employee
Beispiel #3
0
    def search_by_regex(self):

        while_breaker = 1

        while True:

            # This helps the testing, since these are kind of
            # infinite loops while testing, there's need for something
            # that can break them automatically
            if while_breaker > 100:
                break
            else:
                while_breaker += 1

            utils.cls()
            regex = utils.get_input('Enter your regex here: ')

            if not regex:
                utils.pause('Error: Blank spaces are not allowed')
                continue
            else:
                break

        rows = (Task.select(Task, Employee).join(Employee).where(
            Task.title.regexp(regex)).naive())

        return rows
    def run(self):
        print("********************************\n" +
              "*                              *\n" +
              "*   ------------------------   *\n" +
              "*   | Welcome to the LS-8! |   *\n" +
              "*   ------------------------   *\n" +
              "*                              *\n" +
              "********************************\n")
        self.print_help()

        while self.cpu_is_active:
            # red in user input
            prog_name = input("Enter program name:\n>> ")

            program_has_run = False

            # handle 'exit' code
            if prog_name.lower() == "exit":
                self.cpu_is_active = False
            # handle 'help' code
            elif prog_name.lower() == "help":
                self.print_help()
            # handle 'list' code
            elif prog_name.lower() == "list":
                self.print_directory_list()
            # else load the program into the file
            else:
                try:
                    if len(prog_name) <= 4 or prog_name[-4:] != ".ls8":
                        prog_name += ".ls8"
                    self.load(f"programs/{prog_name}")
                    self.running = True
                except FileNotFoundError:
                    print(f"Couldn't open file {prog_name}.\n")

            # Initialize CPU properties for interrupts
            self.time = datetime.now()
            self.keyboard_listener_start()

            while self.running:
                program_has_run = True

                self.check_interrupt_status()
                self.timer_interrupt_check()

                # grab the IR
                ir = self.ram[self.PC]
                # read each instruction code then go to next loop iteration
                self.read_instruction(ir)

            # prompt user that run is complete
            if program_has_run:
                print("\nProgram execution complete.")
                pause()

            # clear listener after while loop ends
            self.keyboard_listener_stop()

            # clear the input buffer to handle presses in peyboard interrupt test
            flush_input()
Beispiel #5
0
def main(args):
    vm_name = args['<vm_name>']
    symbol = args['<symbol>']

    # register SIGINT
    signal.signal(signal.SIGINT, signal_handler)

    with Libvmi(vm_name, INIT_DOMAINNAME | INIT_EVENTS) as vmi:
        vaddr = vmi.translate_ksym2v(symbol)

        debug_event = DebugEvent(debug_callback)

        num_vcpus = vmi.get_num_vcpus()
        sstep_event = SingleStepEvent(range(num_vcpus), enable=False,
                                      callback=sstep_callback)
        with pause(vmi):
            vmi.register_event(debug_event)
            vmi.register_event(sstep_event)
            # set dr0 to our address
            vmi.set_vcpureg(vaddr, X86Reg.DR0.value, 0)
            toggle_dr0(vmi, True)

        # listen
        while not interrupted:
            print("Waiting for events")
            vmi.listen(1000)
        print("Stop listening")

        with pause(vmi):
            vmi.clear_event(debug_event)
            vmi.clear_event(sstep_event)
Beispiel #6
0
    def run(self):
#        oldTick = 0
        tickNumber = 0
        while 1:
            #            print( "Nb de messages : " + str(mail.getMailNumber()))
        #processing events
            #    print( "tick",)
            for event in pygame.event.get():
                if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or (event.type == pygame.KEYDOWN and event.key == pygame.K_q):
                    print("exiting...")
                    sys.exit()

                if event.type == utils.ARRIVED:
                    agent = event.dict['agent']
                    agent.globalGoal = lab.getCaseAt(random.randint(0, lab.width - 1), random.randint(0, lab.height - 1))
                    agent.pathToGoal = lab.computePath(lab.getCaseAt(agent.x / self.tileWidth, agent.y / self.tileHeight), agent.globalGoal)

                #                    print( tickNumber, agent.id, "arrive", "new path : [", utils.pathToString(agent.pathToGoal), "]")

                if event.type == utils.COLLISION:
                    agent = event.dict['agent']
                    #            print( tickNumber, agent.id,"collision : old path : [", utils.pathToString(agent.pathToGoal), "]")
                    newPath = lab.computePath(agent.currentCase, agent.globalGoal)
                    if len(newPath) == 0:
                        agent.globalGoal = None
                    agent.pathToGoal = newPath
                #                print( tickNumber, agent.id,"collision : new path : [", utils.pathToString(agent.pathToGoal), "]")
        #            else:
                #                print( tickNumber, "pas de chemin disponible")

                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    utils.pause()

            #updating agents positions
            for a in self.agents:
                a.currentCase.content = None
                a.update()
                a.currentCase = lab.getCaseAt(a.x / self.tileWidth, a.y / self.tileHeight)
                a.currentCase.content = a

            #printing
            self.screen.blit(self.labImage, (0, 0))
            for a in self.agents:
                self.screen.blit(self.agentImage[a.dir], pygame.Rect(a.x, a.y, self.tileWidth, self.tileHeight))
                if len(a.pathToGoal) != 0:
#                    for c in a.pathToGoal:
#                        self.screen.blit(self.dotImage, pygame.Rect(c.x * self.tileWidth, c.y * self.tileHeight, self.tileWidth, self.tileHeight));
                    start = (a.currentCase.x + 0.5) * self.tileWidth, (a.currentCase.y + 0.5) * self.tileHeight
                    end = (a.pathToGoal[0].x + 0.5) * self.tileWidth, (a.pathToGoal[0].y + 0.5) * self.tileHeight
                    pygame.draw.line(self.screen, (255, 0, 0), start, end)
#                       
                    for i in range(len(a.pathToGoal) - 1):
                        start = (a.pathToGoal[i].x + 0.5) * self.tileWidth, (a.pathToGoal[i].y + 0.5) * self.tileHeight
                        end = (a.pathToGoal[i + 1].x + 0.5) * self.tileWidth, (a.pathToGoal[i + 1].y + 0.5) * self.tileHeight
                        pygame.draw.line(self.screen, (255, 0, 0), start, end)
#                        self.screen.blit(self.dotImage, pygame.Rect(c.x * self.tileWidth, c.y * self.tileHeight, self.tileWidth, self.tileHeight));
                    self.screen.blit(self.goalImage[0], pygame.Rect(a.pathToGoal[len(a.pathToGoal) - 1].x * self.tileWidth, a.pathToGoal[len(a.pathToGoal) - 1].y * self.tileHeight, self.tileWidth, self.tileHeight))
            pygame.display.flip()
#            pygame.time.wait(20)
            tickNumber += 1
Beispiel #7
0
  def setUpClass(cls):
    """Set up two keyspaces: one unsharded, one with two shards."""
    topology = vttest_pb2.VTTestTopology()
    topology.cells.append('test')
    topology.cells.append('test2')
    keyspace = topology.keyspaces.add(name='test_keyspace')
    keyspace.replica_count = 2
    keyspace.rdonly_count = 2
    keyspace.shards.add(name='-80')
    keyspace.shards.add(name='80-')
    keyspace2 = topology.keyspaces.add(name='test_keyspace2')
    keyspace2.shards.add(name='0')
    keyspace2.replica_count = 2
    keyspace2.rdonly_count = 1

    cls.driver = environment.create_webdriver()

    port = environment.reserve_ports(1)
    vttest_environment.base_port = port

    environment.reset_mysql_flavor()

    cls.db = local_database.LocalDatabase(
        topology,
        os.path.join(environment.vttop, 'test/vttest_schema'),
        False, None,
        web_dir=os.path.join(environment.vttop, 'web/vtctld'),
        default_schema_dir=os.path.join(
            environment.vttop, 'test/vttest_schema/default'),
        web_dir2=os.path.join(environment.vttop, 'web/vtctld2/app'))
    cls.db.setup()
    cls.vtctld_addr = 'http://localhost:%d' % cls.db.config()['port']
    utils.pause('Paused test after vtcombo was started.\n'
                'For manual testing, connect to vtctld: %s' % cls.vtctld_addr)
Beispiel #8
0
    def test_reparent_cross_cell(self, shard_id="0"):
        utils.run_vtctl("CreateKeyspace test_keyspace")

        # create the database so vttablets start, as they are serving
        tablet_62344.create_db("vt_test_keyspace")
        tablet_62044.create_db("vt_test_keyspace")
        tablet_41983.create_db("vt_test_keyspace")
        tablet_31981.create_db("vt_test_keyspace")

        # Start up a master mysql and vttablet
        tablet_62344.init_tablet("master", "test_keyspace", shard_id, start=True)
        if environment.topo_server_implementation == "zookeeper":
            shard = utils.run_vtctl_json(["GetShard", "test_keyspace/" + shard_id])
            self.assertEqual(shard["Cells"], ["test_nj"], "wrong list of cell in Shard: %s" % str(shard["Cells"]))

        # Create a few slaves for testing reparenting.
        tablet_62044.init_tablet("replica", "test_keyspace", shard_id, start=True, wait_for_start=False)
        tablet_41983.init_tablet("replica", "test_keyspace", shard_id, start=True, wait_for_start=False)
        tablet_31981.init_tablet("replica", "test_keyspace", shard_id, start=True, wait_for_start=False)
        for t in [tablet_62044, tablet_41983, tablet_31981]:
            t.wait_for_vttablet_state("SERVING")
        if environment.topo_server_implementation == "zookeeper":
            shard = utils.run_vtctl_json(["GetShard", "test_keyspace/" + shard_id])
            self.assertEqual(
                shard["Cells"], ["test_nj", "test_ny"], "wrong list of cell in Shard: %s" % str(shard["Cells"])
            )

        # Recompute the shard layout node - until you do that, it might not be valid.
        utils.run_vtctl("RebuildShardGraph test_keyspace/" + shard_id)
        utils.validate_topology()

        # Force the slaves to reparent assuming that all the datasets are identical.
        for t in [tablet_62344, tablet_62044, tablet_41983, tablet_31981]:
            t.reset_replication()
        utils.pause("force ReparentShard?")
        utils.run_vtctl("ReparentShard -force test_keyspace/%s %s" % (shard_id, tablet_62344.tablet_alias))
        utils.validate_topology(ping_tablets=True)

        self._check_db_addr(shard_id, "master", tablet_62344.port)

        # Verify MasterCell is properly set
        srvShard = utils.run_vtctl_json(["GetSrvShard", "test_nj", "test_keyspace/%s" % (shard_id)])
        self.assertEqual(srvShard["MasterCell"], "test_nj")
        srvShard = utils.run_vtctl_json(["GetSrvShard", "test_ny", "test_keyspace/%s" % (shard_id)])
        self.assertEqual(srvShard["MasterCell"], "test_nj")

        # Perform a graceful reparent operation to another cell.
        utils.pause("graceful ReparentShard?")
        utils.run_vtctl("ReparentShard test_keyspace/%s %s" % (shard_id, tablet_31981.tablet_alias), auto_log=True)
        utils.validate_topology()

        self._check_db_addr(shard_id, "master", tablet_31981.port, cell="test_ny")

        # Verify MasterCell is set to new cell.
        srvShard = utils.run_vtctl_json(["GetSrvShard", "test_nj", "test_keyspace/%s" % (shard_id)])
        self.assertEqual(srvShard["MasterCell"], "test_ny")
        srvShard = utils.run_vtctl_json(["GetSrvShard", "test_ny", "test_keyspace/%s" % (shard_id)])
        self.assertEqual(srvShard["MasterCell"], "test_ny")

        tablet.kill_tablets([tablet_62344, tablet_62044, tablet_41983, tablet_31981])
Beispiel #9
0
def feasible(num_dims, prop, pwa_trace, solver=gopts.opt_engine):
    cons, Vars, vars_grouped_by_state = pwatrace2cons(pwa_trace, num_dims,
                                                      prop)
    cons = list(cons)
    #     for c in cons:
    #         print(c)

    #nvars = num_dims.x + num_dims.pi

    #A_ub, b_ub = truncate(A_ub, b_ub)

    #err.warn_severe('faking output of optimizer')
    #res = True
    #varval_map = {v: 0 for v in Vars}
    # TODO: Make choice of opt engine
    #res, varval_map = z3opt.nlinprog(obj, cons, Vars)

    #res, varval_map = nlpfun(solver)(obj, cons, Vars)
    #ret_val = optsoln2x([varval_map[v] for v in Vars], len(pwa_trace)) if res else None
    obj = 0
    res = nlpfun(solver)(obj, cons, Vars)
    ret_val = optsoln2x(res.x, len(pwa_trace)) if res.success else None
    print(res.success)
    #print(cons)
    print(ret_val)
    if res.success:
        U.pause()
    #embed()
    return ret_val
Beispiel #10
0
    def test(self, num_test_episodes, visualize, pause=False):
        rewards = []
        state_arr = []
        for episode in range(num_test_episodes):
            done = False
            state = self.env.reset(test=True)
            episode_reward = 0
            reward = 0
            frame = 0
            if not visualize:
                self.test_env.render()
            # while not done:
            while frame < 1000:
                if visualize:
                    self.env.render()
                action = self.get_action(state, epsilon=0)
                next_state, reward, done, info = self.env.step(action,
                                                               test=True)
                frame += 1
                state_arr.append(state[0])
                state = next_state
                print("Reward: {0} \t State: {1} \t Fixed Point: {2}".format(
                    reward, state, info['Fixed_Point']))
                episode_reward += reward
                done = info['true_done']
                if pause: utils.pause()
            rewards.append(episode_reward)
        f = open(self.model_path + '/states_track.txt', 'w')
        pickle.dump(state_arr, f)
        f.close()

        return np.mean(rewards), np.std(rewards), rewards
Beispiel #11
0
def run_test_reparent_lag_slave(shard_id='0'):
    utils.zk_wipe()

    utils.run_vtctl('CreateKeyspace -force test_keyspace')

    # create the database so vttablets start, as they are serving
    tablet_62344.create_db('vt_test_keyspace')
    tablet_62044.create_db('vt_test_keyspace')
    tablet_41983.create_db('vt_test_keyspace')
    tablet_31981.create_db('vt_test_keyspace')

    # Start up a master mysql and vttablet
    tablet_62344.init_tablet('master', 'test_keyspace', shard_id, start=True)

    # Create a few slaves for testing reparenting.
    tablet_62044.init_tablet('replica', 'test_keyspace', shard_id, start=True)
    tablet_31981.init_tablet('replica', 'test_keyspace', shard_id, start=True)
    tablet_41983.init_tablet('lag', 'test_keyspace', shard_id, start=True)

    # Recompute the shard layout node - until you do that, it might not be valid.
    utils.run_vtctl('RebuildShardGraph test_keyspace/' + shard_id)
    utils.validate_topology()

    # Force the slaves to reparent assuming that all the datasets are identical.
    for t in [tablet_62344, tablet_62044, tablet_41983, tablet_31981]:
        t.reset_replication()
    utils.run_vtctl('ReparentShard -force test_keyspace/%s %s' %
                    (shard_id, tablet_62344.tablet_alias))
    utils.validate_topology(ping_tablets=True)

    tablet_62344.create_db('vt_test_keyspace')
    tablet_62344.mquery('vt_test_keyspace', create_vt_insert_test)

    tablet_41983.mquery('', 'stop slave')
    for q in populate_vt_insert_test:
        tablet_62344.mquery('vt_test_keyspace', q, write=True)

    # Perform a graceful reparent operation.
    utils.run_vtctl('ReparentShard test_keyspace/%s %s' %
                    (shard_id, tablet_62044.tablet_alias))

    tablet_41983.mquery('', 'start slave')
    time.sleep(1)

    utils.pause("check orphan")

    utils.run_vtctl('ReparentTablet %s' % tablet_41983.tablet_alias)

    result = tablet_41983.mquery('vt_test_keyspace',
                                 'select msg from vt_insert_test where id=1')
    if len(result) != 1:
        raise utils.TestError('expected 1 row from vt_insert_test', result)

    utils.pause("check lag reparent")

    tablet_62344.kill_vttablet()
    tablet_62044.kill_vttablet()
    tablet_41983.kill_vttablet()
    tablet_31981.kill_vttablet()
Beispiel #12
0
 def print_help(self):
     print("*************************************\n" +
           "* exit  ->  power off the LS-8      *\n" +
           "* list  ->  list available programs *\n" +
           "* help  ->  display this message    *\n" +
           "* press 'esc' to exit program loops *\n" +
           "*************************************")
     pause()
Beispiel #13
0
def model_select(model_path):
    print("加载模型文件:")
    print('the model path is:%s' % (model_path))
    pause()
    model = load_model(model_path)
    print("加载完成")

    return model
Beispiel #14
0
def overapprox_x0(num_dims, prop, pwa_trace, solver=gopts.opt_engine):
    cons, Vars, vars_grouped_by_state = pwatrace2cons(pwa_trace, num_dims,
                                                      prop)
    cons = list(cons)

    num_opt_vars = len(Vars)
    nvars = num_dims.x + num_dims.pi

    #directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    I = np.eye(nvars)
    directions = np.vstack((I, -I))
    left_over_vars = num_opt_vars - nvars
    directions_ext = np.pad(directions, [(0, 0), (0, left_over_vars)],
                            'constant')

    var_array = np.array(Vars)

    for direction in directions_ext:
        print(np.dot(direction, var_array))

    #lambdafied = tuple(
    #        sym.lambdify(Vars, np.dot(direction, var_array), str('numpy')) for direction in directions_ext)
    #obj_f = tuple(lambda x, l=l: l(*x) for l in lambdafied)
    objs = tuple(np.dot(direction, var_array) for direction in directions_ext)

    x_arr = np.array(sym.symbols(['x{}'.format(i) for i in range(nvars)]))

    res = solve_mult_opt(nlpfun(solver), objs, cons, Vars)

    l = len(res)
    assert (l % 2 == 0)
    min_res, max_res = res[:l // 2], res[l // 2:]

    ranges = []

    for di, rmin, rmax in zip(directions, min_res, max_res):
        if (rmin.success and rmax.success):
            print('{} \in [{}, {}]'.format(np.dot(di, x_arr), rmin.fun,
                                           -rmax.fun))
            ranges.append([rmin.fun, -rmax.fun])
        else:
            if settings.debug:
                print('LP failed')
                print('rmin status:', rmin.status)
                print('rmax status:', rmax.status)
            return None

    r = np.asarray(ranges)
    # due to numerical errors, the interval can be malformed
    try:
        ret_val = IntervalCons(r[:, 0], r[:, 1])
    except ValueError:
        err.warn('linprog fp failure: Malformed Interval! Please fix.')
        return None

    print(ret_val)
    U.pause()
    return ret_val
Beispiel #15
0
def run_test_mysqlctl_split():
  utils.zk_wipe()

  # Start up a master mysql and vttablet
  utils.run_vtctl('CreateKeyspace -force test_keyspace')

  tablet_62344.init_tablet('master', 'test_keyspace', '0')
  utils.run_vtctl('RebuildShardGraph test_keyspace/0')
  utils.validate_topology()

  tablet_62344.populate('vt_test_keyspace', create_vt_insert_test,
                        populate_vt_insert_test)

  tablet_62344.start_vttablet()

  err = tablet_62344.mysqlctl('-port %u -mysql-port %u partialsnapshot -end=0000000000000003 vt_test_keyspace id' % (tablet_62344.port, tablet_62344.mysql_port)).wait()
  if err != 0:
    raise utils.TestError('mysqlctl partialsnapshot failed')


  utils.pause("partialsnapshot finished")

  tablet_62044.mquery('', 'stop slave')
  tablet_62044.create_db('vt_test_keyspace')
  call(["touch", "/tmp/vtSimulateFetchFailures"])
  err = tablet_62044.mysqlctl('-port %u -mysql-port %u partialrestore %s/snapshot/vt_0000062344/data/vt_test_keyspace-,0000000000000003/partial_snapshot_manifest.json' % (tablet_62044.port, tablet_62044.mysql_port, utils.vtdataroot)).wait()
  if err != 0:
    raise utils.TestError('mysqlctl partialrestore failed')

  tablet_62044.assert_table_count('vt_test_keyspace', 'vt_insert_test', 2)

  # change/add two values on the master, one in range, one out of range, make
  # sure the right one propagate and not the other
  utils.run_vtctl('SetReadWrite ' + tablet_62344.tablet_alias)
  tablet_62344.mquery('vt_test_keyspace', "insert into vt_insert_test (id, msg) values (5, 'test should not propagate')", write=True)
  tablet_62344.mquery('vt_test_keyspace', "update vt_insert_test set msg='test should propagate' where id=2", write=True)

  utils.pause("look at db now!")

  # wait until value that should have been changed is here
  timeout = 10
  while timeout > 0:
    result = tablet_62044.mquery('vt_test_keyspace', 'select msg from vt_insert_test where id=2')
    if result[0][0] == "test should propagate":
      break
    timeout -= 1
    time.sleep(1)
  if timeout == 0:
    raise utils.TestError("expected propagation to happen", result)

  # test value that should not propagate
  # this part is disabled now, as the replication pruning is only enabled
  # for row-based replication, but the mysql server is statement based.
  # will re-enable once we get statement-based pruning patch into mysql.
#  tablet_62044.assert_table_count('vt_test_keyspace', 'vt_insert_test', 0, 'where id=5')

  tablet_62344.kill_vttablet()
Beispiel #16
0
def showInf(lang):
	import netifaces
	clear()
	print (_("Interfaces"))
	def_gw_device = netifaces.gateways()['default'][netifaces.AF_INET][1]
	print def_gw_device
	macaddr = netifaces.ifaddresses('enp0s25')[netifaces.AF_LINK][0]['addr']
	print macaddr
	pause()
Beispiel #17
0
  def test_reparent_lag_slave(self, shard_id='0'):
    utils.run_vtctl('CreateKeyspace test_keyspace')

    # create the database so vttablets start, as they are serving
    tablet_62344.create_db('vt_test_keyspace')
    tablet_62044.create_db('vt_test_keyspace')
    tablet_41983.create_db('vt_test_keyspace')
    tablet_31981.create_db('vt_test_keyspace')

    # Start up a master mysql and vttablet
    tablet_62344.init_tablet('master', 'test_keyspace', shard_id, start=True, wait_for_start=False)

    # Create a few slaves for testing reparenting.
    tablet_62044.init_tablet('replica', 'test_keyspace', shard_id, start=True, wait_for_start=False)
    tablet_31981.init_tablet('replica', 'test_keyspace', shard_id, start=True, wait_for_start=False)
    tablet_41983.init_tablet('lag', 'test_keyspace', shard_id, start=True, wait_for_start=False)

    # wait for all tablets to start
    for t in [tablet_62344, tablet_62044, tablet_31981]:
      t.wait_for_vttablet_state("SERVING")
    tablet_41983.wait_for_vttablet_state("NOT_SERVING")

    # Recompute the shard layout node - until you do that, it might not be valid.
    utils.run_vtctl('RebuildShardGraph test_keyspace/' + shard_id)
    utils.validate_topology()

    # Force the slaves to reparent assuming that all the datasets are identical.
    for t in [tablet_62344, tablet_62044, tablet_41983, tablet_31981]:
      t.reset_replication()
    utils.run_vtctl('ReparentShard -force test_keyspace/%s %s' % (shard_id, tablet_62344.tablet_alias))
    utils.validate_topology(ping_tablets=True)

    tablet_62344.create_db('vt_test_keyspace')
    tablet_62344.mquery('vt_test_keyspace', self._create_vt_insert_test)

    tablet_41983.mquery('', 'stop slave')
    for q in self._populate_vt_insert_test:
      tablet_62344.mquery('vt_test_keyspace', q, write=True)

    # Perform a graceful reparent operation.
    utils.run_vtctl('ReparentShard test_keyspace/%s %s' % (shard_id, tablet_62044.tablet_alias))

    tablet_41983.mquery('', 'start slave')
    time.sleep(1)

    utils.pause("check orphan")

    utils.run_vtctl('ReparentTablet %s' % tablet_41983.tablet_alias)

    result = tablet_41983.mquery('vt_test_keyspace', 'select msg from vt_insert_test where id=1')
    if len(result) != 1:
      self.fail('expected 1 row from vt_insert_test: %s' % str(result))

    utils.pause("check lag reparent")

    tablet.kill_tablets([tablet_62344, tablet_62044, tablet_41983, tablet_31981])
def run_test_reparent_lag_slave(shard_id="0"):
    utils.zk_wipe()

    utils.run_vtctl("CreateKeyspace -force test_keyspace")

    # create the database so vttablets start, as they are serving
    tablet_62344.create_db("vt_test_keyspace")
    tablet_62044.create_db("vt_test_keyspace")
    tablet_41983.create_db("vt_test_keyspace")
    tablet_31981.create_db("vt_test_keyspace")

    # Start up a master mysql and vttablet
    tablet_62344.init_tablet("master", "test_keyspace", shard_id, start=True)

    # Create a few slaves for testing reparenting.
    tablet_62044.init_tablet("replica", "test_keyspace", shard_id, start=True)
    tablet_31981.init_tablet("replica", "test_keyspace", shard_id, start=True)
    tablet_41983.init_tablet("lag", "test_keyspace", shard_id, start=True)

    # Recompute the shard layout node - until you do that, it might not be valid.
    utils.run_vtctl("RebuildShardGraph test_keyspace/" + shard_id)
    utils.validate_topology()

    # Force the slaves to reparent assuming that all the datasets are identical.
    for t in [tablet_62344, tablet_62044, tablet_41983, tablet_31981]:
        t.reset_replication()
    utils.run_vtctl("ReparentShard -force test_keyspace/%s %s" % (shard_id, tablet_62344.tablet_alias))
    utils.validate_topology(ping_tablets=True)

    tablet_62344.create_db("vt_test_keyspace")
    tablet_62344.mquery("vt_test_keyspace", create_vt_insert_test)

    tablet_41983.mquery("", "stop slave")
    for q in populate_vt_insert_test:
        tablet_62344.mquery("vt_test_keyspace", q, write=True)

    # Perform a graceful reparent operation.
    utils.run_vtctl("ReparentShard test_keyspace/%s %s" % (shard_id, tablet_62044.tablet_alias))

    tablet_41983.mquery("", "start slave")
    time.sleep(1)

    utils.pause("check orphan")

    utils.run_vtctl("ReparentTablet %s" % tablet_41983.tablet_alias)

    result = tablet_41983.mquery("vt_test_keyspace", "select msg from vt_insert_test where id=1")
    if len(result) != 1:
        raise utils.TestError("expected 1 row from vt_insert_test", result)

    utils.pause("check lag reparent")

    tablet_62344.kill_vttablet()
    tablet_62044.kill_vttablet()
    tablet_41983.kill_vttablet()
    tablet_31981.kill_vttablet()
Beispiel #19
0
    def _test_vtctl_clone(self, server_mode):
        if server_mode:
            clone_flags = '-server-mode'
        else:
            clone_flags = ''

        # Start up a master mysql and vttablet
        utils.run_vtctl('CreateKeyspace snapshot_test')

        tablet_62344.init_tablet('master', 'snapshot_test', '0')
        utils.run_vtctl('RebuildShardGraph snapshot_test/0')
        utils.validate_topology()

        tablet_62344.populate('vt_snapshot_test', self._create_vt_insert_test,
                              self._populate_vt_insert_test)
        tablet_62344.start_vttablet()

        tablet_62044.create_db('vt_snapshot_test')
        tablet_62044.init_tablet('idle', start=True)

        # small test to make sure the directory validation works
        snapshot_dir = os.path.join(utils.vtdataroot, 'snapshot')
        utils.run("rm -rf %s" % snapshot_dir)
        utils.run("mkdir -p %s" % snapshot_dir)
        utils.run("chmod -w %s" % snapshot_dir)
        out, err = utils.run(
            utils.vtroot +
            '/bin/vtctl --alsologtostderr Clone -force %s %s %s' %
            (clone_flags, tablet_62344.tablet_alias,
             tablet_62044.tablet_alias),
            trap_output=True,
            raise_on_error=False)
        if "Cannot validate snapshot directory" not in err:
            raise utils.TestError("expected validation error", err)
        if "Un-reserved test_nj-0000062044" not in err:
            raise utils.TestError("expected Un-reserved", err)
        logging.debug("Failed Clone output: " + err)
        utils.run("chmod +w %s" % snapshot_dir)

        call(["touch", "/tmp/vtSimulateFetchFailures"])
        utils.run_vtctl('Clone -force %s %s %s' %
                        (clone_flags, tablet_62344.tablet_alias,
                         tablet_62044.tablet_alias),
                        auto_log=True)

        utils.pause("look at logs!")
        tablet_62044.assert_table_count('vt_snapshot_test', 'vt_insert_test',
                                        4)
        tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test',
                                        4)

        utils.validate_topology()

        tablet_62344.kill_vttablet()
        tablet_62044.kill_vttablet()
Beispiel #20
0
    def setUpClass(cls):
        """Set up two keyspaces: one unsharded, one with two shards."""
        topology = vttest_pb2.VTTestTopology()
        topology.cells.append('test')
        topology.cells.append('test2')
        keyspace = topology.keyspaces.add(name='test_keyspace')
        keyspace.replica_count = 2
        keyspace.rdonly_count = 2
        keyspace.shards.add(name='-80')
        keyspace.shards.add(name='80-')
        keyspace2 = topology.keyspaces.add(name='test_keyspace2')
        keyspace2.shards.add(name='0')
        keyspace2.replica_count = 2
        keyspace2.rdonly_count = 1

        if os.environ.get('CI') == 'true' and os.environ.get(
                'TRAVIS') == 'true':
            username = os.environ['SAUCE_USERNAME']
            access_key = os.environ['SAUCE_ACCESS_KEY']
            capabilities = {}
            capabilities['tunnel-identifier'] = os.environ['TRAVIS_JOB_NUMBER']
            capabilities['build'] = os.environ['TRAVIS_BUILD_NUMBER']
            capabilities['platform'] = 'Linux'
            capabilities['browserName'] = 'chrome'
            hub_url = '%s:%s@localhost:4445' % (username, access_key)
            cls.driver = webdriver.Remote(desired_capabilities=capabilities,
                                          command_executor='http://%s/wd/hub' %
                                          hub_url)
        else:
            os.environ['webdriver.chrome.driver'] = os.path.join(
                environment.vtroot, 'dist')
            # Only testing against Chrome for now
            cls.driver = webdriver.Chrome()
            cls.driver.set_window_position(0, 0)
            cls.driver.set_window_size(1920, 1280)

        port = environment.reserve_ports(1)
        vttest_environment.base_port = port
        mysql_flavor.set_mysql_flavor(None)

        cls.db = local_database.LocalDatabase(
            topology,
            os.path.join(environment.vttop, 'test/vttest_schema'),
            False,
            None,
            web_dir=os.path.join(environment.vttop, 'web/vtctld'),
            default_schema_dir=os.path.join(environment.vttop,
                                            'test/vttest_schema/default'),
            web_dir2=os.path.join(environment.vttop, 'web/vtctld2/app'))
        cls.db.setup()
        cls.vtctld_addr = 'http://localhost:%d' % cls.db.config()['port']
        utils.pause('Paused test after vtcombo was started.\n'
                    'For manual testing, connect to vtctld: %s' %
                    cls.vtctld_addr)
Beispiel #21
0
  def test_reparent_cross_cell(self, shard_id='0'):
    utils.run_vtctl(['CreateKeyspace', 'test_keyspace'])

    # create the database so vttablets start, as they are serving
    tablet_62344.create_db('vt_test_keyspace')
    tablet_62044.create_db('vt_test_keyspace')
    tablet_41983.create_db('vt_test_keyspace')
    tablet_31981.create_db('vt_test_keyspace')

    # Start up a master mysql and vttablet
    tablet_62344.init_tablet('master', 'test_keyspace', shard_id, start=True,
                             wait_for_start=False)
    shard = utils.run_vtctl_json(['GetShard', 'test_keyspace/' + shard_id])
    self.assertEqual(shard['cells'], ['test_nj'],
                     'wrong list of cell in Shard: %s' % str(shard['cells']))

    # Create a few slaves for testing reparenting. Won't be healthy
    # as replication is not running.
    tablet_62044.init_tablet('replica', 'test_keyspace', shard_id, start=True,
                             wait_for_start=False)
    tablet_41983.init_tablet('replica', 'test_keyspace', shard_id, start=True,
                             wait_for_start=False)
    tablet_31981.init_tablet('replica', 'test_keyspace', shard_id, start=True,
                             wait_for_start=False)
    tablet_62344.wait_for_vttablet_state('SERVING')
    for t in [tablet_62044, tablet_41983, tablet_31981]:
      t.wait_for_vttablet_state('NOT_SERVING')
    shard = utils.run_vtctl_json(['GetShard', 'test_keyspace/' + shard_id])
    self.assertEqual(
        shard['cells'], ['test_nj', 'test_ny'],
        'wrong list of cell in Shard: %s' % str(shard['cells']))

    utils.validate_topology()

    # Force the slaves to reparent assuming that all the datasets are
    # identical.
    for t in [tablet_62344, tablet_62044, tablet_41983, tablet_31981]:
      t.reset_replication()
    utils.run_vtctl(['InitShardMaster', 'test_keyspace/' + shard_id,
                     tablet_62344.tablet_alias], auto_log=True)
    utils.validate_topology(ping_tablets=True)

    self._check_master_tablet(tablet_62344)

    # Perform a graceful reparent operation to another cell.
    utils.pause('test_reparent_cross_cell PlannedReparentShard')
    utils.run_vtctl(['PlannedReparentShard', 'test_keyspace/' + shard_id,
                     tablet_31981.tablet_alias], auto_log=True)
    utils.validate_topology()

    self._check_master_tablet(tablet_31981)

    tablet.kill_tablets([tablet_62344, tablet_62044, tablet_41983,
                         tablet_31981])
Beispiel #22
0
    def _test_mysqlctl_clone(server_mode):
        if server_mode:
            snapshot_cmd = ['snapshotsourcestart', '-concurrency=8']
            restore_flags = ['-dont_wait_for_slave_start']
        else:
            snapshot_cmd = ['snapshot', '-concurrency=5']
            restore_flags = []

        # Start up a master mysql and vttablet
        utils.run_vtctl(['CreateKeyspace', 'snapshot_test'])

        tablet_62344.init_tablet('master', 'snapshot_test', '0')
        utils.run_vtctl(['RebuildShardGraph', 'snapshot_test/0'])
        utils.validate_topology()

        tablet_62344.populate('vt_snapshot_test', self._create_vt_insert_test,
                              self._populate_vt_insert_test)

        tablet_62344.start_vttablet()

        err = tablet_62344.mysqlctl(snapshot_cmd + ['vt_snapshot_test'],
                                    with_ports=True).wait()
        if err != 0:
            self.fail('mysqlctl %s failed' % str(snapshot_cmd))

        utils.pause("%s finished" % str(snapshot_cmd))

        call(["touch", "/tmp/vtSimulateFetchFailures"])
        err = tablet_31981.mysqlctl(
            ['restore', '-fetch_concurrency=2', '-fetch_retry_count=4'] +
            restore_flags + [
                environment.vtdataroot +
                '/snapshot/vt_0000062344/snapshot_manifest.json'
            ],
            with_ports=True).wait()
        if err != 0:
            self.fail('mysqlctl restore failed')

        tablet_31981.assert_table_count('vt_snapshot_test', 'vt_insert_test',
                                        4)

        if server_mode:
            err = tablet_62344.mysqlctl(
                ['snapshotsourceend', '-read_write', 'vt_snapshot_test'],
                with_ports=True).wait()
            if err != 0:
                self.fail('mysqlctl snapshotsourceend failed')

            # see if server restarted properly
            tablet_62344.assert_table_count('vt_snapshot_test',
                                            'vt_insert_test', 4)

        tablet_62344.kill_vttablet()
    def search(self):
        """ Search a task """

        while_breaker = 1

        while True:

            # This helps the testing, since these are kind of
            # infinite loops while testing, there's need for something
            # that can break them automatically
            if while_breaker > 100:
                break
            else:
                while_breaker += 1

            utils.cls()

            menu = ("Do you want to search by: \n"
                    "a) Exact Date \n"
                    "b) Range of Dates \n"
                    "c) Term \n"
                    "d) Regex Pattern \n"
                    "e) Employee \n"
                    "f) Return to menu \n"
                    ">")

            option = utils.get_input(menu).lower()

            # Handling the errors first
            if not option:
                utils.pause('Error: Blank spaces are not allowed')
                continue
            elif option not in 'abcdef':
                utils.pause("Error: {} isn't a valid option".format(option))
                continue

            tasksearch = TaskSearch()

            if option == 'a':
                rows = tasksearch.search_by_date()
            elif option == 'b':
                rows = tasksearch.search_by_dates_range()
            elif option == 'c':
                rows = tasksearch.search_by_term()
            elif option == 'd':
                rows = tasksearch.search_by_regex()
            elif option == 'e':
                rows = tasksearch.search_by_employee()
            else:
                # Get us back to the initial menu
                return self.menu()

            self.display_results(rows)
Beispiel #24
0
def _run_test_mysqlctl_clone(server_mode):
    if server_mode:
        snapshot_cmd = "snapshotsourcestart -concurrency=8"
        restore_flags = "-dont-wait-for-slave-start"
    else:
        snapshot_cmd = "snapshot -concurrency=5"
        restore_flags = ""

    utils.zk_wipe()

    # Start up a master mysql and vttablet
    utils.run_vtctl('CreateKeyspace -force snapshot_test')

    tablet_62344.init_tablet('master', 'snapshot_test', '0')
    utils.run_vtctl('RebuildShardGraph snapshot_test/0')
    utils.validate_topology()

    tablet_62344.populate('vt_snapshot_test', create_vt_insert_test,
                          populate_vt_insert_test)

    tablet_62344.start_vttablet()

    err = tablet_62344.mysqlctl(
        '-port %u -mysql-port %u %s vt_snapshot_test' %
        (tablet_62344.port, tablet_62344.mysql_port, snapshot_cmd)).wait()
    if err != 0:
        raise utils.TestError('mysqlctl %s failed' % snapshot_cmd)

    utils.pause("%s finished" % snapshot_cmd)

    call(["touch", "/tmp/vtSimulateFetchFailures"])
    err = tablet_62044.mysqlctl(
        '-port %u -mysql-port %u restore -fetch-concurrency=2 -fetch-retry-count=4 %s %s/snapshot/vt_0000062344/snapshot_manifest.json'
        % (tablet_62044.port, tablet_62044.mysql_port, restore_flags,
           utils.vtdataroot)).wait()
    if err != 0:
        raise utils.TestError('mysqlctl restore failed')

    tablet_62044.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

    if server_mode:
        err = tablet_62344.mysqlctl(
            '-port %u -mysql-port %u snapshotsourceend -read-write vt_snapshot_test'
            % (tablet_62344.port, tablet_62344.mysql_port)).wait()
        if err != 0:
            raise utils.TestError('mysqlctl snapshotsourceend failed')

        # see if server restarted properly
        tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test',
                                        4)

    tablet_62344.kill_vttablet()
Beispiel #25
0
    def _test_vtctl_clone(self, server_mode):
        if server_mode:
            clone_flags = ['-server-mode']
        else:
            clone_flags = []

        # Start up a master mysql and vttablet
        utils.run_vtctl(['CreateKeyspace', 'snapshot_test'])

        tablet_62344.init_tablet('master', 'snapshot_test', '0')
        utils.run_vtctl(['RebuildShardGraph', 'snapshot_test/0'])
        utils.validate_topology()

        tablet_62344.populate('vt_snapshot_test', self._create_vt_insert_test,
                              self._populate_vt_insert_test)
        tablet_62344.start_vttablet()

        tablet_31981.create_db('vt_snapshot_test')
        tablet_31981.init_tablet('idle', start=True)

        # small test to make sure the directory validation works
        snapshot_dir = os.path.join(environment.vtdataroot, 'snapshot')
        utils.run("rm -rf %s" % snapshot_dir)
        utils.run("mkdir -p %s" % snapshot_dir)
        utils.run("chmod -w %s" % snapshot_dir)
        out, err = utils.run_vtctl(
            ['Clone', '-force'] + clone_flags +
            [tablet_62344.tablet_alias, tablet_31981.tablet_alias],
            log_level='INFO',
            expect_fail=True)
        if "Cannot validate snapshot directory" not in err:
            self.fail("expected validation error: %s" % err)
        if "Un-reserved test_ny-0000031981" not in err:
            self.fail("expected Un-reserved: %s" % err)
        logging.debug("Failed Clone output: " + err)
        utils.run("chmod +w %s" % snapshot_dir)

        call(["touch", "/tmp/vtSimulateFetchFailures"])
        utils.run_vtctl(['Clone', '-force'] + clone_flags +
                        [tablet_62344.tablet_alias, tablet_31981.tablet_alias],
                        auto_log=True)
        self._check_shard()

        utils.pause("look at logs!")
        tablet_31981.assert_table_count('vt_snapshot_test', 'vt_insert_test',
                                        4)
        tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test',
                                        4)

        utils.validate_topology()

        tablet.kill_tablets([tablet_62344, tablet_31981])
Beispiel #26
0
    def setUpClass(cls):
        """Set up two keyspaces: one unsharded, one with two shards."""
        topology = vttest_pb2.VTTestTopology()
        topology.cells.append("test")
        topology.cells.append("test2")
        keyspace = topology.keyspaces.add(name="test_keyspace")
        keyspace.replica_count = 2
        keyspace.rdonly_count = 2
        keyspace.shards.add(name="-80")
        keyspace.shards.add(name="80-")
        keyspace2 = topology.keyspaces.add(name="test_keyspace2")
        keyspace2.shards.add(name="0")
        keyspace2.replica_count = 2
        keyspace2.rdonly_count = 1

        if os.environ.get("CI") == "true" and os.environ.get("TRAVIS") == "true":
            username = os.environ["SAUCE_USERNAME"]
            access_key = os.environ["SAUCE_ACCESS_KEY"]
            capabilities = {}
            capabilities["tunnel-identifier"] = os.environ["TRAVIS_JOB_NUMBER"]
            capabilities["build"] = os.environ["TRAVIS_BUILD_NUMBER"]
            capabilities["platform"] = "Linux"
            capabilities["browserName"] = "chrome"
            hub_url = "%s:%s@localhost:4445" % (username, access_key)
            cls.driver = webdriver.Remote(
                desired_capabilities=capabilities, command_executor="http://%s/wd/hub" % hub_url
            )
        else:
            os.environ["webdriver.chrome.driver"] = os.path.join(environment.vtroot, "dist")
            # Only testing against Chrome for now
            cls.driver = webdriver.Chrome()
            cls.driver.set_window_position(0, 0)
            cls.driver.set_window_size(1920, 1280)

        port = environment.reserve_ports(1)
        vttest_environment.base_port = port
        mysql_flavor.set_mysql_flavor(None)

        cls.db = local_database.LocalDatabase(
            topology,
            os.path.join(environment.vttop, "test/vttest_schema"),
            False,
            None,
            web_dir=os.path.join(environment.vttop, "web/vtctld"),
            default_schema_dir=os.path.join(environment.vttop, "test/vttest_schema/default"),
            web_dir2=os.path.join(environment.vttop, "web/vtctld2/app"),
        )
        cls.db.setup()
        cls.vtctld_addr = "http://localhost:%d" % cls.db.config()["port"]
        utils.pause(
            "Paused test after vtcombo was started.\n" "For manual testing, connect to vtctld: %s" % cls.vtctld_addr
        )
def _run_test_vtctl_clone(server_mode):
    if server_mode:
        clone_flags = "-server-mode"
    else:
        clone_flags = ""
    utils.zk_wipe()

    # Start up a master mysql and vttablet
    utils.run_vtctl("CreateKeyspace -force snapshot_test")

    tablet_62344.init_tablet("master", "snapshot_test", "0")
    utils.run_vtctl("RebuildShardGraph snapshot_test/0")
    utils.validate_topology()

    tablet_62344.populate("vt_snapshot_test", create_vt_insert_test, populate_vt_insert_test)
    tablet_62344.start_vttablet()

    tablet_62044.create_db("vt_snapshot_test")
    tablet_62044.init_tablet("idle", start=True)

    # small test to make sure the directory validation works
    snapshot_dir = os.path.join(utils.vtdataroot, "snapshot")
    utils.run("rm -rf %s" % snapshot_dir)
    utils.run("mkdir -p %s" % snapshot_dir)
    utils.run("chmod -w %s" % snapshot_dir)
    out, err = utils.run(
        utils.vtroot
        + "/bin/vtctl --alsologtostderr Clone -force %s %s %s"
        % (clone_flags, tablet_62344.tablet_alias, tablet_62044.tablet_alias),
        trap_output=True,
        raise_on_error=False,
    )
    if "Cannot validate snapshot directory" not in err:
        raise utils.TestError("expected validation error", err)
    if "Un-reserved test_nj-0000062044" not in err:
        raise utils.TestError("expected Un-reserved", err)
    utils.debug("Failed Clone output: " + err)
    utils.run("chmod +w %s" % snapshot_dir)

    call(["touch", "/tmp/vtSimulateFetchFailures"])
    utils.run_vtctl(
        "Clone -force %s %s %s" % (clone_flags, tablet_62344.tablet_alias, tablet_62044.tablet_alias), auto_log=True
    )

    utils.pause("look at logs!")
    tablet_62044.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)
    tablet_62344.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)

    utils.validate_topology()

    tablet_62344.kill_vttablet()
    tablet_62044.kill_vttablet()
Beispiel #28
0
    def ask_title(self, inputtxt=None):
        """Ask the title for the task"""

        while True:

            utils.cls()
            title = utils.get_input(inputtxt)

            if not title:
                utils.pause('Error: Blanks spaces are not allowed')
                continue

            return title
def _run_test_mysqlctl_clone(server_mode):
    if server_mode:
        snapshot_cmd = "snapshotsourcestart -concurrency=8"
        restore_flags = "-dont-wait-for-slave-start"
    else:
        snapshot_cmd = "snapshot -concurrency=5"
        restore_flags = ""

    utils.zk_wipe()

    # Start up a master mysql and vttablet
    utils.run_vtctl("CreateKeyspace -force snapshot_test")

    tablet_62344.init_tablet("master", "snapshot_test", "0")
    utils.run_vtctl("RebuildShardGraph snapshot_test/0")
    utils.validate_topology()

    tablet_62344.populate("vt_snapshot_test", create_vt_insert_test, populate_vt_insert_test)

    tablet_62344.start_vttablet()

    err = tablet_62344.mysqlctl(
        "-port %u -mysql-port %u %s vt_snapshot_test" % (tablet_62344.port, tablet_62344.mysql_port, snapshot_cmd)
    ).wait()
    if err != 0:
        raise utils.TestError("mysqlctl %s failed" % snapshot_cmd)

    utils.pause("%s finished" % snapshot_cmd)

    call(["touch", "/tmp/vtSimulateFetchFailures"])
    err = tablet_62044.mysqlctl(
        "-port %u -mysql-port %u restore -fetch-concurrency=2 -fetch-retry-count=4 %s %s/snapshot/vt_0000062344/snapshot_manifest.json"
        % (tablet_62044.port, tablet_62044.mysql_port, restore_flags, utils.vtdataroot)
    ).wait()
    if err != 0:
        raise utils.TestError("mysqlctl restore failed")

    tablet_62044.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)

    if server_mode:
        err = tablet_62344.mysqlctl(
            "-port %u -mysql-port %u snapshotsourceend -read-write vt_snapshot_test"
            % (tablet_62344.port, tablet_62344.mysql_port)
        ).wait()
        if err != 0:
            raise utils.TestError("mysqlctl snapshotsourceend failed")

        # see if server restarted properly
        tablet_62344.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)

    tablet_62344.kill_vttablet()
Beispiel #30
0
def cache(url, path):
	print 'Downloading: %s' % urlparse.urlsplit(url).path
	page = requests.get(url, proxies=PROX)
	if page.status_code == 404:
		return {'url': url, 'uid': 'Error: 404', 'time': gmt_now()}, []
	assert page.status_code == 200, 'Error: Status code error in page download, received %s @ %s' % (page.status_code, urlparse.urlsplit(url).path)
	tree = html.fromstring(page.content)

	links = list_links([str(x) for x in tree.xpath(r'//*[@href]/@href')], url=url)

	uid = dump(html.tostring(tree), path, verbose=True)

	pause()
	return {'url': url, 'uid': uid, 'time': gmt_now()}, links
Beispiel #31
0
  def _test_mysqlctl_clone(server_mode):
    if server_mode:
      snapshot_cmd = ['snapshotsourcestart', '-concurrency=8']
      restore_flags = ['-dont_wait_for_slave_start']
    else:
      snapshot_cmd = ['snapshot', '-concurrency=5']
      restore_flags = []

    # Start up a master mysql and vttablet
    utils.run_vtctl(['CreateKeyspace', 'snapshot_test'])

    tablet_62344.init_tablet('master', 'snapshot_test', '0')
    utils.run_vtctl(['RebuildShardGraph', 'snapshot_test/0'])
    utils.validate_topology()

    tablet_62344.populate('vt_snapshot_test', self._create_vt_insert_test,
                          self._populate_vt_insert_test)

    tablet_62344.start_vttablet()

    err = tablet_62344.mysqlctl(snapshot_cmd + ['vt_snapshot_test'],
                                with_ports=True).wait()
    if err != 0:
      self.fail('mysqlctl %s failed' % str(snapshot_cmd))

    utils.pause("%s finished" % str(snapshot_cmd))

    call(["touch", "/tmp/vtSimulateFetchFailures"])
    err = tablet_31981.mysqlctl(['restore',
                                 '-fetch_concurrency=2',
                                 '-fetch_retry_count=4'] +
                                restore_flags +
                                [environment.vtdataroot + '/snapshot/vt_0000062344/snapshot_manifest.json'],
                                with_ports=True).wait()
    if err != 0:
      self.fail('mysqlctl restore failed')

    tablet_31981.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

    if server_mode:
      err = tablet_62344.mysqlctl(['snapshotsourceend',
                                   '-read_write',
                                   'vt_snapshot_test'], with_ports=True).wait()
      if err != 0:
        self.fail('mysqlctl snapshotsourceend failed')

      # see if server restarted properly
      tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

    tablet_62344.kill_vttablet()
Beispiel #32
0
def _run_test_vtctl_clone(server_mode):
  if server_mode:
    clone_flags = '-server-mode'
  else:
    clone_flags = ''
  utils.zk_wipe()

  # Start up a master mysql and vttablet
  utils.run_vtctl('CreateKeyspace -force snapshot_test')

  tablet_62344.init_tablet('master', 'snapshot_test', '0')
  utils.run_vtctl('RebuildShardGraph snapshot_test/0')
  utils.validate_topology()

  tablet_62344.populate('vt_snapshot_test', create_vt_insert_test,
                        populate_vt_insert_test)
  tablet_62344.start_vttablet()

  tablet_62044.create_db('vt_snapshot_test')
  tablet_62044.init_tablet('idle', start=True)

  # small test to make sure the directory validation works
  snapshot_dir = os.path.join(utils.vtdataroot, 'snapshot')
  utils.run("rm -rf %s" % snapshot_dir)
  utils.run("mkdir -p %s" % snapshot_dir)
  utils.run("chmod -w %s" % snapshot_dir)
  out, err = utils.run(utils.vtroot+'/bin/vtctl -logfile=/dev/null Clone -force %s %s %s' %
                       (clone_flags, tablet_62344.tablet_alias,
                        tablet_62044.tablet_alias),
                       trap_output=True, raise_on_error=False)
  if "Cannot validate snapshot directory" not in err:
    raise utils.TestError("expected validation error", err)
  if "Un-reserved test_nj-0000062044" not in err:
    raise utils.TestError("expected Un-reserved", err)
  utils.debug("Failed Clone output: " + err)
  utils.run("chmod +w %s" % snapshot_dir)

  call(["touch", "/tmp/vtSimulateFetchFailures"])
  utils.run_vtctl('Clone -force %s %s %s' %
                  (clone_flags, tablet_62344.tablet_alias,
                   tablet_62044.tablet_alias))

  utils.pause("look at logs!")
  tablet_62044.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)
  tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

  utils.validate_topology()

  tablet_62344.kill_vttablet()
  tablet_62044.kill_vttablet()
Beispiel #33
0
  def test_vtgate(self):
    # do a few vtgate topology queries to prime the cache
    vtgate_client = zkocc.ZkOccConnection(utils.vtgate.addr(), "test_nj", 30.0)
    vtgate_client.dial()
    vtgate_client.get_srv_keyspace_names("test_nj")
    vtgate_client.get_srv_keyspace("test_nj", "test_keyspace")
    vtgate_client.get_end_points("test_nj", "test_keyspace", "-80", "master")
    vtgate_client.close()

    status = utils.vtgate.get_status()
    self.assertIn('</html>', status) # end of page
    self.assertIn('/serving_graph/test_nj">test_nj', status) # vtctld link

    utils.pause("You can now run a browser and connect to http://%s:%d%s to manually check vtgate status page" % (socket.getfqdn(), utils.vtgate.port, environment.status_url))
Beispiel #34
0
  def _test_vtctl_clone(self, server_mode):
    if server_mode:
      clone_flags = ['-server-mode']
    else:
      clone_flags = []

    # Start up a master mysql and vttablet
    utils.run_vtctl(['CreateKeyspace', 'snapshot_test'])

    tablet_62344.init_tablet('master', 'snapshot_test', '0')
    utils.run_vtctl(['RebuildShardGraph', 'snapshot_test/0'])
    utils.validate_topology()

    tablet_62344.populate('vt_snapshot_test', self._create_vt_insert_test,
                          self._populate_vt_insert_test)
    tablet_62344.start_vttablet()

    tablet_31981.create_db('vt_snapshot_test')
    tablet_31981.init_tablet('idle', start=True)

    # small test to make sure the directory validation works
    snapshot_dir = os.path.join(environment.vtdataroot, 'snapshot')
    utils.run("rm -rf %s" % snapshot_dir)
    utils.run("mkdir -p %s" % snapshot_dir)
    utils.run("chmod -w %s" % snapshot_dir)
    out, err = utils.run_vtctl(['Clone', '-force'] + clone_flags +
                               [tablet_62344.tablet_alias,
                                tablet_31981.tablet_alias],
                               log_level='INFO', expect_fail=True)
    if "Cannot validate snapshot directory" not in err:
      self.fail("expected validation error: %s" % err)
    if "Un-reserved test_ny-0000031981" not in err:
      self.fail("expected Un-reserved: %s" % err)
    logging.debug("Failed Clone output: " + err)
    utils.run("chmod +w %s" % snapshot_dir)

    call(["touch", "/tmp/vtSimulateFetchFailures"])
    utils.run_vtctl(['Clone', '-force'] + clone_flags +
                    [tablet_62344.tablet_alias, tablet_31981.tablet_alias],
                    auto_log=True)
    self._check_shard()

    utils.pause("look at logs!")
    tablet_31981.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)
    tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

    utils.validate_topology()

    tablet.kill_tablets([tablet_62344, tablet_31981])
Beispiel #35
0
  def test_vtgate(self):
    # do a few vtgate topology queries to prime the cache
    vtgate_client = zkocc.ZkOccConnection("localhost:%u" % vtgate_port,
                                          "test_nj", 30.0)
    vtgate_client.dial()
    vtgate_client.get_srv_keyspace_names("test_nj")
    vtgate_client.get_srv_keyspace("test_nj", "test_keyspace")
    vtgate_client.get_end_points("test_nj", "test_keyspace", "-80", "master")
    vtgate_client.close()

    status = utils.get_status(vtgate_port)
    self.assertIn('</html>', status) # end of page
    self.assertIn('/serving_graph/test_nj">test_nj', status) # vtctld link

    utils.pause("You can now run a browser and connect to http://localhost:%u%s to manually check vtgate status page" % (vtgate_port, environment.status_url))
Beispiel #36
0
    def _test_vtctl_clone(self, server_mode):
        if server_mode:
            clone_flags = ["-server-mode"]
        else:
            clone_flags = []

        # Start up a master mysql and vttablet
        utils.run_vtctl(["CreateKeyspace", "snapshot_test"])

        tablet_62344.init_tablet("master", "snapshot_test", "0")
        utils.run_vtctl(["RebuildShardGraph", "snapshot_test/0"])
        utils.validate_topology()

        tablet_62344.populate("vt_snapshot_test", self._create_vt_insert_test, self._populate_vt_insert_test)
        tablet_62344.start_vttablet()

        tablet_62044.create_db("vt_snapshot_test")
        tablet_62044.init_tablet("idle", start=True)

        # small test to make sure the directory validation works
        snapshot_dir = os.path.join(environment.vtdataroot, "snapshot")
        utils.run("rm -rf %s" % snapshot_dir)
        utils.run("mkdir -p %s" % snapshot_dir)
        utils.run("chmod -w %s" % snapshot_dir)
        out, err = utils.run_vtctl(
            ["Clone", "-force"] + clone_flags + [tablet_62344.tablet_alias, tablet_62044.tablet_alias],
            log_level="INFO",
            expect_fail=True,
        )
        if "Cannot validate snapshot directory" not in err:
            self.fail("expected validation error: %s" % err)
        if "Un-reserved test_nj-0000062044" not in err:
            self.fail("expected Un-reserved: %s" % err)
        logging.debug("Failed Clone output: " + err)
        utils.run("chmod +w %s" % snapshot_dir)

        call(["touch", "/tmp/vtSimulateFetchFailures"])
        utils.run_vtctl(
            ["Clone", "-force"] + clone_flags + [tablet_62344.tablet_alias, tablet_62044.tablet_alias], auto_log=True
        )

        utils.pause("look at logs!")
        tablet_62044.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)
        tablet_62344.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)

        utils.validate_topology()

        tablet.kill_tablets([tablet_62344, tablet_62044])
Beispiel #37
0
    def _test_mysqlctl_clone(server_mode):
        if server_mode:
            snapshot_cmd = ["snapshotsourcestart", "-concurrency=8"]
            restore_flags = ["-dont-wait-for-slave-start"]
        else:
            snapshot_cmd = ["snapshot", "-concurrency=5"]
            restore_flags = []

        # Start up a master mysql and vttablet
        utils.run_vtctl(["CreateKeyspace", "snapshot_test"])

        tablet_62344.init_tablet("master", "snapshot_test", "0")
        utils.run_vtctl(["RebuildShardGraph", "snapshot_test/0"])
        utils.validate_topology()

        tablet_62344.populate("vt_snapshot_test", self._create_vt_insert_test, self._populate_vt_insert_test)

        tablet_62344.start_vttablet()

        err = tablet_62344.mysqlctl(snapshot_cmd + ["vt_snapshot_test"], with_ports=True).wait()
        if err != 0:
            self.fail("mysqlctl %s failed" % str(snapshot_cmd))

        utils.pause("%s finished" % str(snapshot_cmd))

        call(["touch", "/tmp/vtSimulateFetchFailures"])
        err = tablet_62044.mysqlctl(
            ["restore", "-fetch-concurrency=2", "-fetch-retry-count=4"]
            + restore_flags
            + [environment.vtdataroot + "/snapshot/vt_0000062344/snapshot_manifest.json"],
            with_ports=True,
        ).wait()
        if err != 0:
            self.fail("mysqlctl restore failed")

        tablet_62044.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)

        if server_mode:
            err = tablet_62344.mysqlctl(
                ["snapshotsourceend", "-read-write", "vt_snapshot_test"], with_ports=True
            ).wait()
            if err != 0:
                self.fail("mysqlctl snapshotsourceend failed")

            # see if server restarted properly
            tablet_62344.assert_table_count("vt_snapshot_test", "vt_insert_test", 4)

        tablet_62344.kill_vttablet()
Beispiel #38
0
def main(args):
    logging.basicConfig(level=logging.INFO)
    vm_name = args['<vm_name>']

    # register SIGINT
    signal.signal(signal.SIGINT, signal_handler)

    # 1 - init LibVMI
    kvm_socket = {
        VMIInitData.KVMI_SOCKET: args['--kvmi-socket']
    } if args['--kvmi-socket'] else None
    with Libvmi(vm_name,
                INIT_DOMAINNAME | INIT_EVENTS,
                init_data=kvm_socket,
                partial=True) as vmi:
        counter = Counter()

        # 2 - define CR3 callback
        def cr3_callback(vmi, event):
            cr3_value = event.value
            logging.info("CR3 change: %s", hex(cr3_value))
            counter[hex(cr3_value)] += 1

        # 3 - define and register CR3-write event
        with pause(vmi):
            # register CR3-write event
            reg_event = RegEvent(X86Reg.CR3, RegAccess.W, cr3_callback)
            vmi.register_event(reg_event)

        # 4 - listen for events
        for i in range(0, 100):
            vmi.listen(500)
        logging.info(counter)
Beispiel #39
0
  def test_api(self):
    # for manual tests
    utils.pause('Now is a good time to look at vtctld UI at: '
                'http://localhost:%d/' % utils.vtctld.port)

    # test root works
    url = 'http://localhost:%d/api/topodata/' % utils.vtctld.port
    f = urllib2.urlopen(url)
    root = f.read()
    f.close()
    result = json.loads(root)
    self.assertEqual(result['Error'], '')
    self.assertIn('global', result['Children'])
    self.assertIn('test_ca', result['Children'])
    self.assertIn('test_nj', result['Children'])
    self.assertIn('test_ny', result['Children'])
Beispiel #40
0
def main(args):

    if len(args) != 3:
        print("./mem_vmi.py <process_name> <string_addr>")
        return 1

    vm_name = "ubuntu16.04"
    process_name = args[1]
    string_addr = args[2]

    with Libvmi(
            vm_name
    ) as vmi:  #class Libvmi,the default parameters calls vmi_init_complete
        #init offsets values, init libvmi library
        tasks_offset = vmi.get_offset("linux_tasks")
        name_offset = vmi.get_offset("linux_name")
        pid_offset = vmi.get_offset("linux_pid")

        #pause vm for consistent memory access
        with pause(vmi):
            pids = get_pid_by_name(vmi, process_name)
            if not pids:
                print("Cannot find process %s" % process_name)
            pid = pids[0]
            print("process %s:%d" % (process_name, pid))

            #get output string of exp
            string = vmi.read_str_va(int(string_addr, 16), pid)
            print("output:%s" % string)

            #alter output string of exp
            vmi.write_8_va(int(string_addr, 16), pid, 0x77)
            return 0
Beispiel #41
0
    def test_api(self):
        # for manual tests
        utils.pause('Now is a good time to look at vtctld UI at: '
                    'http://localhost:%d/' % utils.vtctld.port)

        # test root works
        url = 'http://localhost:%d/api/topodata/' % utils.vtctld.port
        f = urllib2.urlopen(url)
        root = f.read()
        f.close()
        result = json.loads(root)
        self.assertEqual(result['Error'], '')
        self.assertIn('global', result['Children'])
        self.assertIn('test_ca', result['Children'])
        self.assertIn('test_nj', result['Children'])
        self.assertIn('test_ny', result['Children'])
Beispiel #42
0
def main(args):
    logging.basicConfig(level=logging.INFO)
    vm_name = args['<vm_name>']

    # register SIGINT
    signal.signal(signal.SIGINT, signal_handler)

    kvm_socket = {VMIInitData.KVMI_SOCKET: args['--kvmi-socket']} if args['--kvmi-socket'] else None
    with Libvmi(vm_name, INIT_DOMAINNAME | INIT_EVENTS, init_data=kvm_socket, partial=True) as vmi:
        # init paging to translate virtual addresses
        vmi.init_paging(0)
        with pause(vmi):
            # get current RIP on VCPU 0
            rip = vmi.get_vcpureg(X86Reg.RIP.value, 0)
            # get DTB
            cr3 = vmi.get_vcpureg(X86Reg.CR3.value, 0)
            dtb = cr3 & ~0xfff
            # get gpa
            paddr = vmi.pagetable_lookup(dtb, rip)
            gfn = paddr >> 12

            # define callback
            def cb_mem_event(vmi, event):
                logging.info("Mem event at RIP: %s, frame: %s, offset: %s, permissions: %s",
                             hex(event.x86_regs.rip), hex(event.gla), hex(event.offset), event.out_access.name)

            mem_event = MemEvent(MemAccess.X, cb_mem_event, gfn=gfn)
            vmi.register_event(mem_event)
        # listen
        while not interrupted:
            vmi.listen(3000)
        logging.info("stop listening")
Beispiel #43
0
    def crawl(self, save = True):
        """Crawl necessary info from nntime.com
        Regular expression pattern and decrypt encrypted-port code
        maybe change depend on url source

        - if param save set to False, collected info will not be saved in memory

        Here's a simply explanation for decrypt encrypted-port:
            Ports at nntime are encrypted as fake random-string,
            as it random - String is really random
            as it fake - same number at per page is same random string
            So we can find regular pattern at those string
            Key:
                1.Per ip:port slot has specific string, and that string has
                port value at bottom.
                2.Each single number at port itself is encrypted as '+alpha'
                3.So that we can get len of encrypted port and capture the
                real port by Key.1
        """
        ProxySpider.crawl(self)
        p_port1 = re.compile(r'value="([0-9.]{0,36})" onclick', re.DOTALL)
        p_port2 = re.compile(r'\(":"([a-zA-Z+]{0,8})\)', re.DOTALL)
        p_ip = re.compile(r'</td><td>([0-9.]{0,16})<script', re.DOTALL)
        for link in self.links:
            self.view(link)
            #get decrypt port
            encrypt_ports1 = [
                port1.group(1) for port1 in re.finditer(p_port1, self.cont)]
            encrypt_ports2 = [
                port2.group(1) for port2 in re.finditer(p_port2, self.cont)]
            ports = []
            for i in range(len(encrypt_ports1)):
                if len(encrypt_ports2) != len(encrypt_ports1):
                    cprint(self, 'Port crawled odd,')
                    cprint(self, 'may occur some issues')
                    pause()
                ports.append(encrypt_ports1[i][-len(encrypt_ports2[i])/2:])
            #get ip    
            ips = [ip.group(1) for ip in re.finditer(p_ip, self.cont)]
            #exception tick
            if len(ips) != len(ports):
                cprint(self, 'Port&IP collected amount is different,')
                cprint(self, 'may occur some issues')
                pause()

            if not save is False:
                self.save_collected(ips, ports)
Beispiel #44
0
def search_menu() -> None:
    """The search menu for iTules.
    """
    # Whether or not to keep the menu alive.
    keep_alive: bool = True

    while keep_alive:
        # The valid options the user can pick.
        choices: Dict[str, str] = {
            'song': 'search for a song',
            'album': 'search for a album',
            'artist': 'search for a artist',
            'all': 'search for any of the above',
            'back': 'go back',
            'exit': 'exit the program'
        }
        # The option the user has chosen.
        choice: str
        # Any extra input the user has entered.
        args: List[str]
        # Actually retrieve the data.
        choice, args = get_menu_opt('SEARCH', choices)

        if choice == 'exit':
            sys.exit(0)
        elif choice == 'back':
            keep_alive = False
        else:
            search_term: str = ' '.join(args)
            if len(args) == 0:
                print_header()
                print_centered(f'{choice.upper()} SEARCH MENU')
                search_term = input('enter your search term:\n')
            entity_name: str = 'song,album,musicArtist' if choice == 'all' else \
                'musicArtist' if choice == 'artist' else choice
            search_results: List[Union[Artist, Album,
                                       Track]] = search_entities(
                                           search_term, entity_name)
            clear()
            print_centered(f'{choice.upper()} SEARCH RESULTS')
            if len(search_results) > 0:
                for _, result in enumerate(search_results):
                    print_result(result)
            else:
                print_centered('NO RESULTS')
            pause()
Beispiel #45
0
    def _test_standalone(self, use_vtcombo):
        """Sample test for run_local_database.py as a standalone process."""

        # launch a backend database based on the provided topology and schema
        port = environment.reserve_ports(1)
        args = [
            environment.run_local_database,
            '--port',
            str(port),
            '--topology',
            'test_keyspace/-80:test_keyspace_0,'
            'test_keyspace/80-:test_keyspace_1',
            '--schema_dir',
            os.path.join(environment.vttop, 'test', 'vttest_schema'),
        ]
        if use_vtcombo:
            args.append('--use_vtcombo')
        sp = subprocess.Popen(args,
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE)
        config = json.loads(sp.stdout.readline())

        # gather the vars for the vtgate process
        url = 'http://localhost:%d/debug/vars' % config['port']
        f = urllib.urlopen(url)
        data = f.read()
        f.close()
        json_vars = json.loads(data)
        process_name = 'vtgate'
        if use_vtcombo:
            process_name = 'vtcombo'
        self.assertIn(process_name, json_vars['cmdline'][0])

        # to test vtcombo:
        # ./vttest_sample_test.py -v -d
        # go install && vtcombo -port 15010 -grpc_port 15011 -service_map grpc-vtgateservice -topology test_keyspace/-80:test_keyspace_0,test_keyspace/80-:test_keyspace_1 -mycnf_server_id 1 -mycnf_socket_file $VTDATAROOT/vttest*/vt_0000000001/mysql.sock -db-config-dba-uname vt_dba -db-config-dba-charset utf8 -db-config-app-uname vt_app -db-config-app-charset utf8 -alsologtostderr
        # vtctl -vtgate_protocol grpc VtGateExecuteShards -server localhost:15011 -keyspace test_keyspace -shards -80 -tablet_type master "select 1 from dual"
        # vtctl -vtgate_protocol grpc VtGateExecuteKeyspaceIds -server localhost:15011 -keyspace test_keyspace -keyspace_ids 20 -tablet_type master "show tables"
        if use_vtcombo:
            utils.pause('good time to test vtcombo with database running')
        else:
            utils.pause('good time to test vtgate with database running')

        # and we're done, clean-up
        sp.stdin.write('\n')
        sp.wait()
Beispiel #46
0
def _run_test_vtctl_partial_clone(create, populate,
                                  start, end):
  utils.zk_wipe()

  # Start up a master mysql and vttablet
  utils.run_vtctl('CreateKeyspace -force snapshot_test')

  tablet_62344.init_tablet('master', 'snapshot_test', '0')
  utils.run_vtctl('RebuildShardGraph snapshot_test/0')
  utils.validate_topology()

  tablet_62344.populate('vt_snapshot_test', create, populate)

  tablet_62344.start_vttablet()

  tablet_62044.init_tablet('idle', start=True)

  # FIXME(alainjobart): not sure where the right place for this is,
  # but it doesn't seem it should right here. It should be either in
  # InitTablet (running an action on the vttablet), or in PartialClone
  # (instead of doing a 'USE dbname' it could do a 'CREATE DATABASE
  # dbname').
  tablet_62044.mquery('', 'stop slave')
  tablet_62044.create_db('vt_snapshot_test')
  call(["touch", "/tmp/vtSimulateFetchFailures"])
  utils.run_vtctl('PartialClone -force %s %s id %s %s' %
                  (tablet_62344.tablet_alias, tablet_62044.tablet_alias,
                   start, end))

  utils.pause("after PartialClone")

  # grab the new tablet definition from zk, make sure the start and
  # end keys are set properly
  out = utils.zk_cat(tablet_62044.zk_tablet_path)
  if '"Start": "%s"' % start not in out or '"End": "%s"' % end not in out:
    print "Tablet output:"
    print "out"
    raise utils.TestError('wrong Start or End')

  tablet_62044.assert_table_count('vt_snapshot_test', 'vt_insert_test', 2)

  utils.validate_topology()

  tablet_62344.kill_vttablet()
  tablet_62044.kill_vttablet()
Beispiel #47
0
def main():
  parser = optparse.OptionParser(usage='usage: %prog [options]')
  utils.add_options(parser)
  (options, args) = parser.parse_args()
  options.debug = True
  utils.set_options(options)
  env = keyspace_util.TestEnv()
  try:
    environment.topo_server().setup()
    env.launch(
        'user',
        shards=['-80', '80-'],
        ddls=[
            'create table user(user_id bigint, name varchar(128), '
            'primary key(user_id))',
            'create table user_extra(user_id bigint, extra varchar(128), '
            'primary key(user_id))',
            'create table music(user_id bigint, music_id bigint, '
            'primary key(user_id, music_id))',
            'create table music_extra(music_id bigint, '
            'keyspace_id bigint unsigned, primary key(music_id))',
            ],
        )
    env.launch(
        'lookup',
        ddls=[
            'create table user_idx(user_id bigint not null auto_increment, '
            'primary key(user_id))',
            'create table name_user_idx(name varchar(128), user_id bigint, '
            'primary key(name, user_id))',
            'create table music_user_idx(music_id bigint not null '
            'auto_increment, user_id bigint, primary key(music_id))',
            ],
        )
    utils.apply_vschema(vschema)
    utils.VtGate().start(cache_ttl='500s')
    utils.Vtctld().start()
    print 'vtgate:', utils.vtgate.port
    print 'vtctld:', utils.vtctld.port
    utils.pause('the cluster is up, press enter to shut it down...')
  finally:
    env.teardown()
    utils.kill_sub_processes()
    utils.remove_tmp_files()
    environment.topo_server().teardown()
Beispiel #48
0
  def setUpClass(cls):
    """Set up two keyspaces: one unsharded, one with two shards."""
    if os.environ.get('CI') == 'true' and os.environ.get('TRAVIS') == 'true':
      username = os.environ['SAUCE_USERNAME']
      access_key = os.environ['SAUCE_ACCESS_KEY']
      capabilities = {}
      capabilities['tunnel-identifier'] = os.environ['TRAVIS_JOB_NUMBER']
      capabilities['build'] = os.environ['TRAVIS_BUILD_NUMBER']
      capabilities['platform'] = 'Linux'
      capabilities['browserName'] = 'chrome'
      hub_url = '%s:%s@localhost:4445' % (username, access_key)
      cls.driver = webdriver.Remote(
          desired_capabilities=capabilities,
          command_executor='http://%s/wd/hub' % hub_url)
    else:
      os.environ['webdriver.chrome.driver'] = os.path.join(
          os.environ['VTROOT'], 'dist')
      # Only testing against Chrome for now
      cls.driver = webdriver.Chrome()

    topology = vttest_pb2.VTTestTopology()
    topology.cells.append('test')
    keyspace = topology.keyspaces.add(name='test_keyspace')
    keyspace.replica_count = 2
    keyspace.rdonly_count = 2
    keyspace.shards.add(name='-80')
    keyspace.shards.add(name='80-')
    keyspace2 = topology.keyspaces.add(name='test_keyspace2')
    keyspace2.shards.add(name='0')
    keyspace2.replica_count = 2
    keyspace2.rdonly_count = 1

    port = environment.reserve_ports(1)
    vttest_environment.base_port = port
    mysql_flavor.set_mysql_flavor(None)

    cls.db = local_database.LocalDatabase(
        topology, '', False, None,
        os.path.join(os.environ['VTTOP'], 'web/vtctld2/dist'),
        os.path.join(os.environ['VTTOP'], 'test/vttest_schema/default'))
    cls.db.setup()
    cls.vtctld_addr = 'http://localhost:%d' % cls.db.config()['port']
    utils.pause('Paused test after vtcombo was started.\n'
                'For manual testing, connect to vtctld: %s' % cls.vtctld_addr)
  def test_successful_resharding(self):
    """Reshard from 1 to 2 shards by running the workflow."""
    worker_proc, _, worker_rpc_port = utils.run_vtworker_bg(
        ['--cell', 'test_nj'], auto_log=True)
    vtworker_endpoint = 'localhost:%d' % worker_rpc_port

    stdout = utils.run_vtctl(['WorkflowCreate', 'horizontal_resharding',
                              '-keyspace=test_keyspace',
                              '-vtworkers=%s' % vtworker_endpoint],
                             auto_log=True)
    workflow_uuid = re.match(r'^uuid: (.*)$', stdout[0]).group(1)

    utils.pause('Now is a good time to look at vtctld UI at: '
                '%s, workflow uuid=%s' % (utils.vtctld.port, workflow_uuid))

    utils.run_vtctl(['WorkflowWait', workflow_uuid])

    self.verify()
    utils.kill_sub_process(worker_proc, soft=True)
Beispiel #50
0
  def _test_standalone(self, use_vtcombo):
    """Sample test for run_local_database.py as a standalone process."""

    # launch a backend database based on the provided topology and schema
    port = environment.reserve_ports(1)
    args = [environment.run_local_database,
            '--port', str(port),
            '--topology',
            'test_keyspace/-80:test_keyspace_0,'
            'test_keyspace/80-:test_keyspace_1',
            '--schema_dir', os.path.join(environment.vttop, 'test',
                                         'vttest_schema'),
    ]
    if use_vtcombo:
      args.append('--use_vtcombo')
    sp = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    config = json.loads(sp.stdout.readline())

    # gather the vars for the vtgate process
    url = 'http://localhost:%d/debug/vars' % config['port']
    f = urllib.urlopen(url)
    data = f.read()
    f.close()
    json_vars = json.loads(data)
    process_name = 'vtgate'
    if use_vtcombo:
      process_name = 'vtcombo'
    self.assertIn(process_name, json_vars['cmdline'][0])

    # to test vtcombo:
    # ./vttest_sample_test.py -v -d
    # go install && vtcombo -port 15010 -grpc_port 15011 -service_map grpc-vtgateservice -topology test_keyspace/-80:test_keyspace_0,test_keyspace/80-:test_keyspace_1 -mycnf_server_id 1 -mycnf_socket_file $VTDATAROOT/vttest*/vt_0000000001/mysql.sock -db-config-dba-uname vt_dba -db-config-dba-charset utf8 -db-config-app-uname vt_app -db-config-app-charset utf8 -alsologtostderr
    # vtctl -vtgate_protocol grpc VtGateExecuteShards -server localhost:15011 -keyspace test_keyspace -shards -80 -tablet_type master "select 1 from dual"
    # vtctl -vtgate_protocol grpc VtGateExecuteKeyspaceIds -server localhost:15011 -keyspace test_keyspace -keyspace_ids 20 -tablet_type master "show tables"
    if use_vtcombo:
      utils.pause('good time to test vtcombo with database running')
    else:
      utils.pause('good time to test vtgate with database running')

    # and we're done, clean-up
    sp.stdin.write('\n')
    sp.wait()
Beispiel #51
0
  def _test_mysqlctl_clone(server_mode):
    if server_mode:
      snapshot_cmd = "snapshotsourcestart -concurrency=8"
      restore_flags = "-dont-wait-for-slave-start"
    else:
      snapshot_cmd = "snapshot -concurrency=5"
      restore_flags = ""

    # Start up a master mysql and vttablet
    utils.run_vtctl('CreateKeyspace snapshot_test')

    tablet_62344.init_tablet('master', 'snapshot_test', '0')
    utils.run_vtctl('RebuildShardGraph snapshot_test/0')
    utils.validate_topology()

    tablet_62344.populate('vt_snapshot_test', self._create_vt_insert_test,
                          self._populate_vt_insert_test)

    tablet_62344.start_vttablet()

    err = tablet_62344.mysqlctl('-port %u -mysql-port %u %s vt_snapshot_test' % (tablet_62344.port, tablet_62344.mysql_port, snapshot_cmd)).wait()
    if err != 0:
      self.fail('mysqlctl %s failed' % snapshot_cmd)

    utils.pause("%s finished" % snapshot_cmd)

    call(["touch", "/tmp/vtSimulateFetchFailures"])
    err = tablet_62044.mysqlctl('-port %u -mysql-port %u restore -fetch-concurrency=2 -fetch-retry-count=4 %s %s/snapshot/vt_0000062344/snapshot_manifest.json' % (tablet_62044.port, tablet_62044.mysql_port, restore_flags, utils.vtdataroot)).wait()
    if err != 0:
      self.fail('mysqlctl restore failed')

    tablet_62044.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

    if server_mode:
      err = tablet_62344.mysqlctl('-port %u -mysql-port %u snapshotsourceend -read-write vt_snapshot_test' % (tablet_62344.port, tablet_62344.mysql_port)).wait()
      if err != 0:
        self.fail('mysqlctl snapshotsourceend failed')

      # see if server restarted properly
      tablet_62344.assert_table_count('vt_snapshot_test', 'vt_insert_test', 4)

    tablet_62344.kill_vttablet()
Beispiel #52
0
def main():
  parser = optparse.OptionParser(usage="usage: %prog [options]")
  utils.add_options(parser)
  (options, args) = parser.parse_args()
  options.debug = True
  utils.set_options(options)
  env = keyspace_util.TestEnv()
  vtgate_server=None
  try:
    environment.topo_server().setup()
    env.launch(
        "user",
        shards=["-80", "80-"],
        ddls=[
            'create table user(user_id bigint, name varchar(128), primary key(user_id))',
            'create table user_extra(user_id bigint, extra varchar(128), primary key(user_id))',
            'create table music(user_id bigint, music_id bigint, primary key(user_id, music_id))',
            'create table music_extra(music_id bigint, keyspace_id bigint unsigned, primary key(music_id))',
            ],
        )
    env.launch(
        "lookup",
        ddls=[
            'create table user_idx(user_id bigint not null auto_increment, primary key(user_id))',
            'create table name_user_idx(name varchar(128), user_id bigint, primary key(name, user_id))',
            'create table music_user_idx(music_id bigint not null auto_increment, user_id bigint, primary key(music_id))',
            ],
        )
    utils.apply_vschema(vschema)
    vtgate_server, vtgate_port = utils.vtgate_start(cache_ttl='500s')
    utils.Vtctld().start()
    print "vtgate:", vtgate_port
    print "vtctld:", utils.vtctld.port
    utils.pause("the cluster is up, press enter to shut it down...")
  finally:
    utils.vtgate_kill(vtgate_server)
    env.teardown()
    utils.kill_sub_processes()
    utils.remove_tmp_files()
    environment.topo_server().teardown()
Beispiel #53
0
 def test_partial(self):
   utils.pause(
       'You can now run a browser and connect to http://%s:%d to '
       'manually check topology' % (socket.getfqdn(), utils.vtctld.port))
   self.assertEqual(self.data['Partial'], True)
Beispiel #54
0
  def test_complex_schema(self):

    utils.run_vtctl(['CreateKeyspace', 'test_keyspace'])

    shard_0_master.init_tablet(  'master',  'test_keyspace', '0')
    shard_0_replica1.init_tablet('replica', 'test_keyspace', '0')
    shard_0_replica2.init_tablet('replica', 'test_keyspace', '0')
    shard_0_rdonly.init_tablet(  'rdonly',  'test_keyspace', '0')
    shard_0_backup.init_tablet(  'backup',  'test_keyspace', '0')
    shard_1_master.init_tablet(  'master',  'test_keyspace', '1')
    shard_1_replica1.init_tablet('replica', 'test_keyspace', '1')
    shard_2_master.init_tablet(  'master',  'test_keyspace', '2')
    shard_2_replica1.init_tablet('replica', 'test_keyspace', '2')

    utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'], auto_log=True)

    # run checks now before we start the tablets
    utils.validate_topology()

    # create databases, start the tablets
    for t in [shard_0_master, shard_0_replica1, shard_0_replica2,
              shard_0_rdonly, shard_0_backup, shard_1_master, shard_1_replica1]:
      t.create_db('vt_test_keyspace')
      t.start_vttablet(wait_for_state=None)

    # we intentionally don't want to create db on these tablets
    shard_2_master.start_vttablet(wait_for_state=None)
    shard_2_replica1.start_vttablet(wait_for_state=None)

    # wait for the tablets to start
    shard_0_master.wait_for_vttablet_state('SERVING')
    shard_0_replica1.wait_for_vttablet_state('SERVING')
    shard_0_replica2.wait_for_vttablet_state('SERVING')
    shard_0_rdonly.wait_for_vttablet_state('SERVING')
    shard_0_backup.wait_for_vttablet_state('NOT_SERVING')
    shard_1_master.wait_for_vttablet_state('SERVING')
    shard_1_replica1.wait_for_vttablet_state('SERVING')
    shard_2_master.wait_for_vttablet_state('NOT_SERVING')
    shard_2_replica1.wait_for_vttablet_state('NOT_SERVING')

    # make sure all replication is good
    for t in [shard_0_master, shard_0_replica1, shard_0_replica2,
              shard_0_rdonly, shard_0_backup, shard_1_master, shard_1_replica1, shard_2_master, shard_2_replica1]:
      t.reset_replication()
    utils.run_vtctl(['InitShardMaster', 'test_keyspace/0',
                     shard_0_master.tablet_alias], auto_log=True)
    utils.run_vtctl(['InitShardMaster', 'test_keyspace/1',
                     shard_1_master.tablet_alias], auto_log=True)
    utils.run_vtctl(['InitShardMaster', 'test_keyspace/2',
                     shard_2_master.tablet_alias], auto_log=True)
    utils.run_vtctl(['ValidateKeyspace', '-ping-tablets', 'test_keyspace'])

    # check after all tablets are here and replication is fixed
    utils.validate_topology(ping_tablets=True)

    # shard 0: apply the schema using a complex schema upgrade, no
    # reparenting yet
    utils.run_vtctl(['ApplySchemaShard',
                     '-sql='+create_vt_select_test[0],
                     'test_keyspace/0'],
                    auto_log=True)

    # check all expected hosts have the change:
    # - master won't have it as it's a complex change
    self._check_tables(shard_0_master, 0)
    self._check_tables(shard_0_replica1, 1)
    self._check_tables(shard_0_replica2, 1)
    self._check_tables(shard_0_rdonly, 1)
    self._check_tables(shard_0_backup, 1)
    self._check_tables(shard_1_master, 0)
    self._check_tables(shard_1_replica1, 0)

    # shard 0: apply schema change to just master directly
    # (to test its state is not changed)
    utils.run_vtctl(['ApplySchema',
                     '-stop-replication',
                     '-sql='+create_vt_select_test[0],
                     shard_0_master.tablet_alias],
                    auto_log=True)
    self._check_tables(shard_0_master, 1)

    # shard 0: apply new schema change, with reparenting
    utils.run_vtctl(['ApplySchemaShard',
                     '-new-parent='+shard_0_replica1.tablet_alias,
                     '-sql='+create_vt_select_test[1],
                     'test_keyspace/0'],
                    auto_log=True)
    self._check_tables(shard_0_master, 1)
    self._check_tables(shard_0_replica1, 2)
    self._check_tables(shard_0_replica2, 2)
    self._check_tables(shard_0_rdonly, 2)
    self._check_tables(shard_0_backup, 2)

    # verify GetSchema --tables works
    s = utils.run_vtctl_json(['GetSchema', '--tables=vt_select_test0',
                               shard_0_replica1.tablet_alias])
    self.assertEqual(len(s['TableDefinitions']), 1)
    self.assertEqual(s['TableDefinitions'][0]['Name'], 'vt_select_test0')

    # CopySchemaShard is responsible for creating the db; one shouldn't exist before
    # the command is run.
    self._check_db_not_created(shard_2_master)
    self._check_db_not_created(shard_2_replica1)

    utils.run_vtctl(['CopySchemaShard',
                     shard_0_replica1.tablet_alias,
                     'test_keyspace/2'],
                    auto_log=True)

    # shard_2_master should look the same as the replica we copied from
    self._check_tables(shard_2_master, 2)
    self._check_tables(shard_2_replica1, 2)
    # shard_2_replica1 should have gotten an identical schema applied to it via replication
    self.assertEqual(
      utils.run_vtctl_json(['GetSchema', shard_0_replica1.tablet_alias]),
      utils.run_vtctl_json(['GetSchema', shard_2_replica1.tablet_alias]),
    )

    # keyspace: try to apply a keyspace-wide schema change, should fail
    # as the preflight would be different in shard1 vs the others
    out, err = utils.run_vtctl(['ApplySchemaKeyspace',
                                '-sql='+create_vt_select_test[2],
                                'test_keyspace'],
                               trap_output=True,
                               log_level='INFO',
                               raise_on_error=False)
    if err.find('ApplySchemaKeyspace Shard 1 has inconsistent schema') == -1:
      self.fail('Unexpected ApplySchemaKeyspace output: %s' % err)

    # shard 1: catch it up with simple updates
    utils.run_vtctl(['ApplySchemaShard',
                     '-simple',
                     '-sql='+create_vt_select_test[0],
                     'test_keyspace/1'],
                    auto_log=True)
    utils.run_vtctl(['ApplySchemaShard',
                     '-simple',
                     '-sql='+create_vt_select_test[1],
                     'test_keyspace/1'],
                    auto_log=True)
    self._check_tables(shard_1_master, 2)
    self._check_tables(shard_1_replica1, 2)

    # keyspace: apply a keyspace-wide simple schema change, should work now
    utils.run_vtctl(['ApplySchemaKeyspace',
                     '-simple',
                     '-sql='+create_vt_select_test[2],
                     'test_keyspace'],
                    auto_log=True)

    # check all expected hosts have the change
    self._check_tables(shard_0_master, 1) # was stuck a long time ago as scrap
    self._check_tables(shard_0_replica1, 3) # current master
    self._check_tables(shard_0_replica2, 3)
    self._check_tables(shard_0_rdonly, 3)
    self._check_tables(shard_0_backup, 3)
    self._check_tables(shard_1_master, 3) # current master
    self._check_tables(shard_1_replica1, 3)
    self._check_tables(shard_2_master, 3) # current master
    self._check_tables(shard_2_replica1, 3)

    # keyspace: apply a keyspace-wide complex schema change, should work too
    utils.run_vtctl(['ApplySchemaKeyspace',
                     '-sql='+create_vt_select_test[3],
                     'test_keyspace'],
                    auto_log=True)

    # check all expected hosts have the change:
    # - master won't have it as it's a complex change
    # - backup won't have it as IsReplicatingType is false
    self._check_tables(shard_0_master, 1) # was stuck a long time ago as scrap
    self._check_tables(shard_0_replica1, 3) # current master
    self._check_tables(shard_0_replica2, 4)
    self._check_tables(shard_0_rdonly, 4)
    self._check_tables(shard_0_backup, 4)
    self._check_tables(shard_1_master, 3) # current master
    self._check_tables(shard_1_replica1, 4)
    self._check_tables(shard_2_master, 3) # current master
    self._check_tables(shard_2_replica1, 4)

    utils.pause("Look at schema now!")

    tablet.kill_tablets([shard_0_master, shard_0_replica1, shard_0_replica2,
                         shard_0_rdonly, shard_0_backup, shard_1_master,
                         shard_1_replica1, shard_2_master, shard_2_replica1])
Beispiel #55
0
    def test_resharding(self):
        utils.run_vtctl(
            [
                "CreateKeyspace",
                "--sharding_column_name",
                "bad_column",
                "--sharding_column_type",
                "bytes",
                "--split_shard_count",
                "2",
                "test_keyspace",
            ]
        )
        utils.run_vtctl(["SetKeyspaceShardingInfo", "test_keyspace", "keyspace_id", "uint64"], expect_fail=True)
        utils.run_vtctl(
            [
                "SetKeyspaceShardingInfo",
                "-force",
                "-split_shard_count",
                "4",
                "test_keyspace",
                "keyspace_id",
                keyspace_id_type,
            ]
        )

        shard_0_master.init_tablet("master", "test_keyspace", "-80")
        shard_0_replica.init_tablet("replica", "test_keyspace", "-80")
        shard_0_ny_slave.init_tablet("spare", "test_keyspace", "-80")
        shard_1_master.init_tablet("master", "test_keyspace", "80-")
        shard_1_slave1.init_tablet("replica", "test_keyspace", "80-")
        shard_1_slave2.init_tablet("spare", "test_keyspace", "80-")
        shard_1_ny_slave.init_tablet("spare", "test_keyspace", "80-")
        shard_1_rdonly.init_tablet("rdonly", "test_keyspace", "80-")

        utils.run_vtctl(["RebuildKeyspaceGraph", "test_keyspace"], auto_log=True)

        ks = utils.run_vtctl_json(["GetSrvKeyspace", "test_nj", "test_keyspace"])
        self.assertEqual(ks["SplitShardCount"], 4)

        # we set full_mycnf_args to True as a test in the KIT_BYTES case
        full_mycnf_args = keyspace_id_type == keyrange_constants.KIT_BYTES

        # create databases so vttablet can start behaving normally
        for t in [
            shard_0_master,
            shard_0_replica,
            shard_0_ny_slave,
            shard_1_master,
            shard_1_slave1,
            shard_1_slave2,
            shard_1_ny_slave,
            shard_1_rdonly,
        ]:
            t.create_db("vt_test_keyspace")
            t.start_vttablet(wait_for_state=None, full_mycnf_args=full_mycnf_args)

        # wait for the tablets
        shard_0_master.wait_for_vttablet_state("SERVING")
        shard_0_replica.wait_for_vttablet_state("SERVING")
        shard_0_ny_slave.wait_for_vttablet_state("NOT_SERVING")  # spare
        shard_1_master.wait_for_vttablet_state("SERVING")
        shard_1_slave1.wait_for_vttablet_state("SERVING")
        shard_1_slave2.wait_for_vttablet_state("NOT_SERVING")  # spare
        shard_1_ny_slave.wait_for_vttablet_state("NOT_SERVING")  # spare
        shard_1_rdonly.wait_for_vttablet_state("SERVING")

        # reparent to make the tablets work
        utils.run_vtctl(["ReparentShard", "-force", "test_keyspace/-80", shard_0_master.tablet_alias], auto_log=True)
        utils.run_vtctl(["ReparentShard", "-force", "test_keyspace/80-", shard_1_master.tablet_alias], auto_log=True)

        # create the tables
        self._create_schema()
        self._insert_startup_values()
        self._test_keyrange_constraints()

        # create the split shards
        shard_2_master.init_tablet("master", "test_keyspace", "80-c0")
        shard_2_replica1.init_tablet("spare", "test_keyspace", "80-c0")
        shard_2_replica2.init_tablet("spare", "test_keyspace", "80-c0")
        shard_3_master.init_tablet("master", "test_keyspace", "c0-")
        shard_3_replica.init_tablet("spare", "test_keyspace", "c0-")
        shard_3_rdonly.init_tablet("rdonly", "test_keyspace", "c0-")

        # start vttablet on the split shards (no db created,
        # so they're all not serving)
        shard_3_master.start_vttablet(wait_for_state=None, target_tablet_type="replica")
        for t in [shard_2_master, shard_2_replica1, shard_2_replica2, shard_3_replica, shard_3_rdonly]:
            t.start_vttablet(wait_for_state=None)
        for t in [shard_2_master, shard_2_replica1, shard_2_replica2, shard_3_master, shard_3_replica, shard_3_rdonly]:
            t.wait_for_vttablet_state("NOT_SERVING")

        utils.run_vtctl(["ReparentShard", "-force", "test_keyspace/80-c0", shard_2_master.tablet_alias], auto_log=True)
        utils.run_vtctl(["ReparentShard", "-force", "test_keyspace/c0-", shard_3_master.tablet_alias], auto_log=True)

        utils.run_vtctl(["RebuildKeyspaceGraph", "test_keyspace"], auto_log=True)
        utils.check_srv_keyspace(
            "test_nj",
            "test_keyspace",
            "Partitions(master): -80 80-\n"
            + "Partitions(rdonly): -80 80-\n"
            + "Partitions(replica): -80 80-\n"
            + "TabletTypes: master,rdonly,replica",
            keyspace_id_type=keyspace_id_type,
        )

        if use_clone_worker:
            # the worker will do everything. We test with source_reader_count=10
            # (down from default=20) as connection pool is not big enough for 20.
            # min_table_size_for_split is set to 1 as to force a split even on the
            # small table we have.
            utils.run_vtworker(
                [
                    "--cell",
                    "test_nj",
                    "--command_display_interval",
                    "10ms",
                    "SplitClone",
                    "--exclude_tables",
                    "unrelated",
                    "--strategy",
                    "populateBlpCheckpoint",
                    "--source_reader_count",
                    "10",
                    "--min_table_size_for_split",
                    "1",
                    "test_keyspace/80-c0",
                ],
                auto_log=True,
            )

            # TODO(alainjobart): experiment with the dontStartBinlogPlayer option

        else:
            # take the snapshot for the split
            utils.run_vtctl(
                ["MultiSnapshot", "--spec=80-c0-", "--exclude_tables=unrelated", shard_1_slave1.tablet_alias],
                auto_log=True,
            )

            # the snapshot_copy hook will copy the snapshot files to
            # VTDATAROOT/tmp/... as a test. We want to use these for one half,
            # but not for the other, so we test both scenarios.
            os.unlink(
                os.path.join(
                    environment.tmproot, "snapshot-from-%s-for-%s.tar" % (shard_1_slave1.tablet_alias, "80-c0")
                )
            )

            # wait for tablet's binlog server service to be enabled after snapshot
            shard_1_slave1.wait_for_binlog_server_state("Enabled")

            # perform the restores: first one from source tablet. We removed the
            # storage backup, so it's coming from the tablet itself.
            # we also delay starting the binlog player, then enable it.
            utils.run_vtctl(
                [
                    "ShardMultiRestore",
                    "-strategy=populateBlpCheckpoint,dontStartBinlogPlayer",
                    "test_keyspace/80-c0",
                    shard_1_slave1.tablet_alias,
                ],
                auto_log=True,
            )

            timeout = 10
            while True:
                shard_2_master_status = shard_2_master.get_status()
                if not "not starting because flag &#39;DontStart&#39; is set" in shard_2_master_status:
                    timeout = utils.wait_step("shard 2 master has not failed starting yet", timeout)
                    continue
                logging.debug("shard 2 master is waiting on flag removal, good")
                break

            qr = utils.run_vtctl_json(
                [
                    "ExecuteFetch",
                    shard_2_master.tablet_alias,
                    'update _vt.blp_checkpoint set flags="" where source_shard_uid=0',
                ]
            )
            self.assertEqual(qr["RowsAffected"], 1)

            timeout = 10
            while True:
                shard_2_master_status = shard_2_master.get_status()
                if "not starting because flag &#39;DontStart&#39; is set" in shard_2_master_status:
                    timeout = utils.wait_step("shard 2 master has not started replication yet", timeout)
                    continue
                logging.debug("shard 2 master has started replication, good")
                break

            # second restore from storage: to be sure, we stop vttablet, and restart
            # it afterwards
            shard_1_slave1.kill_vttablet()
            utils.run_vtctl(
                [
                    "ShardMultiRestore",
                    "-strategy=populateBlpCheckpoint",
                    "test_keyspace/c0-",
                    shard_1_slave1.tablet_alias,
                ],
                auto_log=True,
            )
            shard_1_slave1.start_vttablet(wait_for_state=None)
            shard_1_slave1.wait_for_binlog_server_state("Enabled")

        # check the startup values are in the right place
        self._check_startup_values()

        # check the schema too
        utils.run_vtctl(["ValidateSchemaKeyspace", "--exclude_tables=unrelated", "test_keyspace"], auto_log=True)

        # check the binlog players are running and exporting vars
        shard_2_master.wait_for_binlog_player_count(1)
        shard_3_master.wait_for_binlog_player_count(1)
        self._check_binlog_player_vars(shard_2_master)
        self._check_binlog_player_vars(shard_3_master)

        # check that binlog server exported the stats vars
        self._check_binlog_server_vars(shard_1_slave1)

        # testing filtered replication: insert a bunch of data on shard 1,
        # check we get most of it after a few seconds, wait for binlog server
        # timeout, check we get all of it.
        logging.debug("Inserting lots of data on source shard")
        self._insert_lots(1000)
        logging.debug("Checking 80 percent of data is sent quickly")
        self._check_lots_timeout(1000, 80, 5)
        logging.debug("Checking all data goes through eventually")
        self._check_lots_timeout(1000, 100, 20)
        logging.debug("Checking no data was sent the wrong way")
        self._check_lots_not_present(1000)
        self._check_binlog_player_vars(shard_2_master, seconds_behind_master_max=30)
        self._check_binlog_player_vars(shard_3_master, seconds_behind_master_max=30)

        # use the vtworker checker to compare the data
        logging.debug("Running vtworker SplitDiff")
        utils.run_vtworker(["-cell", "test_nj", "SplitDiff", "test_keyspace/c0-"], auto_log=True)
        utils.run_vtctl(["ChangeSlaveType", shard_1_rdonly.tablet_alias, "rdonly"], auto_log=True)
        utils.run_vtctl(["ChangeSlaveType", shard_3_rdonly.tablet_alias, "rdonly"], auto_log=True)

        utils.pause("Good time to test vtworker for diffs")

        # get status for a destination master tablet, make sure we have it all
        shard_2_master_status = shard_2_master.get_status()
        self.assertIn("Binlog player state: Running", shard_2_master_status)
        self.assertIn(
            "<td><b>All</b>: 6000<br><b>Query</b>: 4000<br><b>Transaction</b>: 2000<br></td>", shard_2_master_status
        )
        self.assertIn("</html>", shard_2_master_status)

        # start a thread to insert data into shard_1 in the background
        # with current time, and monitor the delay
        insert_thread_1 = InsertThread(shard_1_master, "insert_low", 10000, 0x9000000000000000)
        insert_thread_2 = InsertThread(shard_1_master, "insert_high", 10001, 0xD000000000000000)
        monitor_thread_1 = MonitorLagThread(shard_2_replica2, "insert_low")
        monitor_thread_2 = MonitorLagThread(shard_3_replica, "insert_high")

        # tests a failover switching serving to a different replica
        utils.run_vtctl(["ChangeSlaveType", shard_1_slave2.tablet_alias, "replica"])
        utils.run_vtctl(["ChangeSlaveType", shard_1_slave1.tablet_alias, "spare"])
        shard_1_slave2.wait_for_vttablet_state("SERVING")
        shard_1_slave1.wait_for_vttablet_state("NOT_SERVING")

        # test data goes through again
        logging.debug("Inserting lots of data on source shard")
        self._insert_lots(1000, base=1000)
        logging.debug("Checking 80 percent of data was sent quickly")
        self._check_lots_timeout(1000, 80, 5, base=1000)

        # check we can't migrate the master just yet
        utils.run_vtctl(["MigrateServedTypes", "test_keyspace/80-", "master"], expect_fail=True)

        # check query service is off on master 2 and master 3, as filtered
        # replication is enabled. Even health check that is enabled on
        # master 3 should not interfere.
        shard_2_master_vars = utils.get_vars(shard_2_master.port)
        self.assertEqual(shard_2_master_vars["TabletStateName"], "NOT_SERVING")
        shard_3_master_vars = utils.get_vars(shard_3_master.port)
        self.assertEqual(shard_3_master_vars["TabletStateName"], "NOT_SERVING")

        # now serve rdonly from the split shards
        utils.run_vtctl(["MigrateServedTypes", "test_keyspace/80-", "rdonly"], auto_log=True)
        utils.check_srv_keyspace(
            "test_nj",
            "test_keyspace",
            "Partitions(master): -80 80-\n"
            + "Partitions(rdonly): -80 80-c0 c0-\n"
            + "Partitions(replica): -80 80-\n"
            + "TabletTypes: master,rdonly,replica",
            keyspace_id_type=keyspace_id_type,
        )

        # then serve replica from the split shards
        utils.run_vtctl(["MigrateServedTypes", "test_keyspace/80-", "replica"], auto_log=True)
        utils.check_srv_keyspace(
            "test_nj",
            "test_keyspace",
            "Partitions(master): -80 80-\n"
            + "Partitions(rdonly): -80 80-c0 c0-\n"
            + "Partitions(replica): -80 80-c0 c0-\n"
            + "TabletTypes: master,rdonly,replica",
            keyspace_id_type=keyspace_id_type,
        )

        # move replica back and forth
        utils.run_vtctl(["MigrateServedTypes", "-reverse", "test_keyspace/80-", "replica"], auto_log=True)
        utils.check_srv_keyspace(
            "test_nj",
            "test_keyspace",
            "Partitions(master): -80 80-\n"
            + "Partitions(rdonly): -80 80-c0 c0-\n"
            + "Partitions(replica): -80 80-\n"
            + "TabletTypes: master,rdonly,replica",
            keyspace_id_type=keyspace_id_type,
        )
        utils.run_vtctl(["MigrateServedTypes", "test_keyspace/80-", "replica"], auto_log=True)
        utils.check_srv_keyspace(
            "test_nj",
            "test_keyspace",
            "Partitions(master): -80 80-\n"
            + "Partitions(rdonly): -80 80-c0 c0-\n"
            + "Partitions(replica): -80 80-c0 c0-\n"
            + "TabletTypes: master,rdonly,replica",
            keyspace_id_type=keyspace_id_type,
        )

        # reparent shard_2 to shard_2_replica1, then insert more data and
        # see it flow through still
        utils.run_vtctl(["ReparentShard", "test_keyspace/80-c0", shard_2_replica1.tablet_alias])
        logging.debug("Inserting lots of data on source shard after reparenting")
        self._insert_lots(3000, base=2000)
        logging.debug("Checking 80 percent of data was sent fairly quickly")
        self._check_lots_timeout(3000, 80, 10, base=2000)

        # use the vtworker checker to compare the data again
        logging.debug("Running vtworker SplitDiff")
        utils.run_vtworker(["-cell", "test_nj", "SplitDiff", "test_keyspace/c0-"], auto_log=True)
        utils.run_vtctl(["ChangeSlaveType", shard_1_rdonly.tablet_alias, "rdonly"], auto_log=True)
        utils.run_vtctl(["ChangeSlaveType", shard_3_rdonly.tablet_alias, "rdonly"], auto_log=True)

        # going to migrate the master now, check the delays
        monitor_thread_1.done = True
        monitor_thread_2.done = True
        insert_thread_1.done = True
        insert_thread_2.done = True
        logging.debug(
            "DELAY 1: %s max_lag=%u avg_lag=%u",
            monitor_thread_1.object_name,
            monitor_thread_1.max_lag,
            monitor_thread_1.lag_sum / monitor_thread_1.sample_count,
        )
        logging.debug(
            "DELAY 2: %s max_lag=%u avg_lag=%u",
            monitor_thread_2.object_name,
            monitor_thread_2.max_lag,
            monitor_thread_2.lag_sum / monitor_thread_2.sample_count,
        )

        # then serve master from the split shards
        utils.run_vtctl(["MigrateServedTypes", "test_keyspace/80-", "master"], auto_log=True)
        utils.check_srv_keyspace(
            "test_nj",
            "test_keyspace",
            "Partitions(master): -80 80-c0 c0-\n"
            + "Partitions(rdonly): -80 80-c0 c0-\n"
            + "Partitions(replica): -80 80-c0 c0-\n"
            + "TabletTypes: master,rdonly,replica",
            keyspace_id_type=keyspace_id_type,
        )

        # check the binlog players are gone now
        shard_2_master.wait_for_binlog_player_count(0)
        shard_3_master.wait_for_binlog_player_count(0)

        # get status for a destination master tablet, make sure it's good
        shard_2_master_status = shard_2_master.get_status()
        self.assertIn("No binlog player is running", shard_2_master_status)
        self.assertIn("</html>", shard_2_master_status)

        # scrap the original tablets in the original shard
        for t in [shard_1_master, shard_1_slave1, shard_1_slave2, shard_1_ny_slave, shard_1_rdonly]:
            utils.run_vtctl(["ScrapTablet", t.tablet_alias], auto_log=True)
        tablet.kill_tablets([shard_1_master, shard_1_slave1, shard_1_slave2, shard_1_ny_slave, shard_1_rdonly])
        for t in [shard_1_master, shard_1_slave1, shard_1_slave2, shard_1_ny_slave, shard_1_rdonly]:
            utils.run_vtctl(["DeleteTablet", t.tablet_alias], auto_log=True)

        # rebuild the serving graph, all mentions of the old shards shoud be gone
        utils.run_vtctl(["RebuildKeyspaceGraph", "test_keyspace"], auto_log=True)

        # test RemoveShardCell
        utils.run_vtctl(["RemoveShardCell", "test_keyspace/-80", "test_nj"], auto_log=True, expect_fail=True)
        utils.run_vtctl(["RemoveShardCell", "test_keyspace/80-", "test_nj"], auto_log=True)
        utils.run_vtctl(["RemoveShardCell", "test_keyspace/80-", "test_ny"], auto_log=True)
        shard = utils.run_vtctl_json(["GetShard", "test_keyspace/80-"])
        if shard["Cells"]:
            self.fail("Non-empty Cells record for shard: %s" % str(shard))

        # delete the original shard
        utils.run_vtctl(["DeleteShard", "test_keyspace/80-"], auto_log=True)

        # kill everything
        tablet.kill_tablets(
            [
                shard_0_master,
                shard_0_replica,
                shard_0_ny_slave,
                shard_2_master,
                shard_2_replica1,
                shard_2_replica2,
                shard_3_master,
                shard_3_replica,
                shard_3_rdonly,
            ]
        )
Beispiel #56
0
  def test_resharding(self):
    utils.run_vtctl(['CreateKeyspace',
                     '--sharding_column_name', 'bad_column',
                     '--sharding_column_type', 'bytes',
                     'test_keyspace'])
    utils.run_vtctl(['SetKeyspaceShardingInfo', 'test_keyspace',
                     'keyspace_id', 'uint64'], expect_fail=True)
    utils.run_vtctl(['SetKeyspaceShardingInfo', '-force', 'test_keyspace',
                     'keyspace_id', keyspace_id_type])

    shard_0_master.init_tablet( 'master',  'test_keyspace', '-80')
    shard_0_replica.init_tablet('replica', 'test_keyspace', '-80')
    shard_0_ny_slave.init_tablet('spare', 'test_keyspace', '-80')
    shard_1_master.init_tablet( 'master',  'test_keyspace', '80-')
    shard_1_slave1.init_tablet('replica', 'test_keyspace', '80-')
    shard_1_slave2.init_tablet('spare', 'test_keyspace', '80-')
    shard_1_ny_slave.init_tablet('spare', 'test_keyspace', '80-')
    shard_1_rdonly.init_tablet('rdonly', 'test_keyspace', '80-')

    utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'], auto_log=True)

    # we set full_mycnf_args to True as a test in the KIT_BYTES case
    full_mycnf_args = keyspace_id_type == keyrange_constants.KIT_BYTES

    # create databases so vttablet can start behaving normally
    for t in [shard_0_master, shard_0_replica, shard_0_ny_slave, shard_1_master,
              shard_1_slave1, shard_1_slave2, shard_1_ny_slave, shard_1_rdonly]:
      t.create_db('vt_test_keyspace')
      t.start_vttablet(wait_for_state=None, full_mycnf_args=full_mycnf_args)

    # wait for the tablets
    shard_0_master.wait_for_vttablet_state('SERVING')
    shard_0_replica.wait_for_vttablet_state('SERVING')
    shard_0_ny_slave.wait_for_vttablet_state('NOT_SERVING') # spare
    shard_1_master.wait_for_vttablet_state('SERVING')
    shard_1_slave1.wait_for_vttablet_state('SERVING')
    shard_1_slave2.wait_for_vttablet_state('NOT_SERVING') # spare
    shard_1_ny_slave.wait_for_vttablet_state('NOT_SERVING') # spare
    shard_1_rdonly.wait_for_vttablet_state('SERVING')

    # reparent to make the tablets work
    utils.run_vtctl(['ReparentShard', '-force', 'test_keyspace/-80',
                     shard_0_master.tablet_alias], auto_log=True)
    utils.run_vtctl(['ReparentShard', '-force', 'test_keyspace/80-',
                     shard_1_master.tablet_alias], auto_log=True)

    # create the tables
    self._create_schema()
    self._insert_startup_values()
    self._test_keyrange_constraints()

    # create the split shards
    shard_2_master.init_tablet(  'master', 'test_keyspace', '80-C0')
    shard_2_replica1.init_tablet('spare',  'test_keyspace', '80-C0')
    shard_2_replica2.init_tablet('spare',  'test_keyspace', '80-C0')
    shard_3_master.init_tablet(  'master', 'test_keyspace', 'C0-')
    shard_3_replica.init_tablet( 'spare',  'test_keyspace', 'C0-')
    shard_3_rdonly.init_tablet(  'rdonly', 'test_keyspace', 'C0-')

    # start vttablet on the split shards (no db created,
    # so they're all not serving)
    shard_3_master.start_vttablet(wait_for_state=None,
                                  target_tablet_type='replica')
    for t in [shard_2_master, shard_2_replica1, shard_2_replica2,
              shard_3_replica, shard_3_rdonly]:
      t.start_vttablet(wait_for_state=None)
    for t in [shard_2_master, shard_2_replica1, shard_2_replica2,
              shard_3_master, shard_3_replica, shard_3_rdonly]:
      t.wait_for_vttablet_state('NOT_SERVING')

    utils.run_vtctl(['ReparentShard', '-force', 'test_keyspace/80-C0',
                     shard_2_master.tablet_alias], auto_log=True)
    utils.run_vtctl(['ReparentShard', '-force', 'test_keyspace/C0-',
                     shard_3_master.tablet_alias], auto_log=True)

    utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'],
                    auto_log=True)
    utils.check_srv_keyspace('test_nj', 'test_keyspace',
                             'Partitions(master): -80 80-\n' +
                             'Partitions(rdonly): -80 80-\n' +
                             'Partitions(replica): -80 80-\n' +
                             'TabletTypes: master,rdonly,replica',
                             keyspace_id_type=keyspace_id_type)

    # take the snapshot for the split
    utils.run_vtctl(['MultiSnapshot', '--spec=80-C0-',
                     '--exclude_tables=unrelated',
                     shard_1_slave1.tablet_alias], auto_log=True)

    # the snapshot_copy hook will copy the snapshot files to
    # VTDATAROOT/tmp/... as a test. We want to use these for one half,
    # but not for the other, so we test both scenarios.
    os.unlink(os.path.join(environment.tmproot, "snapshot-from-%s-for-%s.tar" %
                           (shard_1_slave1.tablet_alias, "80-C0")))

    # wait for tablet's binlog server service to be enabled after snapshot
    shard_1_slave1.wait_for_binlog_server_state("Enabled")

    # perform the restores: first one from source tablet. We removed the
    # storage backup, so it's coming from the tablet itself.
    # we also delay starting the binlog player, then enable it.
    utils.run_vtctl(['ShardMultiRestore',
                     '-strategy=populateBlpCheckpoint,dontStartBinlogPlayer',
                     'test_keyspace/80-C0', shard_1_slave1.tablet_alias],
                    auto_log=True)

    timeout = 10
    while True:
      shard_2_master_status = shard_2_master.get_status()
      if not "not starting because flag &#39;DontStart&#39; is set" in shard_2_master_status:
        timeout = utils.wait_step('shard 2 master has not failed starting yet', timeout)
        continue
      logging.debug("shard 2 master is waiting on flag removal, good")
      break

    qr = utils.run_vtctl_json(['ExecuteFetch', shard_2_master.tablet_alias, 'update _vt.blp_checkpoint set flags="" where source_shard_uid=0'])
    self.assertEqual(qr['RowsAffected'], 1)

    timeout = 10
    while True:
      shard_2_master_status = shard_2_master.get_status()
      if "not starting because flag &#39;DontStart&#39; is set" in shard_2_master_status:
        timeout = utils.wait_step('shard 2 master has not started replication yet', timeout)
        continue
      logging.debug("shard 2 master has started replication, good")
      break

    # second restore from storage: to be sure, we stop vttablet, and restart
    # it afterwards
    shard_1_slave1.kill_vttablet()
    utils.run_vtctl(['ShardMultiRestore', '-strategy=populateBlpCheckpoint',
                     'test_keyspace/C0-', shard_1_slave1.tablet_alias],
                    auto_log=True)
    shard_1_slave1.start_vttablet(wait_for_state=None)
    shard_1_slave1.wait_for_binlog_server_state("Enabled")

    # check the startup values are in the right place
    self._check_startup_values()

    # check the schema too
    utils.run_vtctl(['ValidateSchemaKeyspace', '--exclude_tables=unrelated',
                     'test_keyspace'], auto_log=True)

    # check the binlog players are running and exporting vars
    shard_2_master.wait_for_binlog_player_count(1)
    shard_3_master.wait_for_binlog_player_count(1)
    self._check_binlog_player_vars(shard_2_master)
    self._check_binlog_player_vars(shard_3_master)

    # check that binlog server exported the stats vars
    self._check_binlog_server_vars(shard_1_slave1)

    # testing filtered replication: insert a bunch of data on shard 1,
    # check we get most of it after a few seconds, wait for binlog server
    # timeout, check we get all of it.
    logging.debug("Inserting lots of data on source shard")
    self._insert_lots(1000)
    logging.debug("Checking 80 percent of data is sent quickly")
    self._check_lots_timeout(1000, 80, 5)
    logging.debug("Checking all data goes through eventually")
    self._check_lots_timeout(1000, 100, 20)
    logging.debug("Checking no data was sent the wrong way")
    self._check_lots_not_present(1000)
    self._check_binlog_player_vars(shard_2_master, seconds_behind_master_max=30)
    self._check_binlog_player_vars(shard_3_master, seconds_behind_master_max=30)

    # use the vtworker checker to compare the data
    logging.debug("Running vtworker SplitDiff")
    utils.run_vtworker(['-cell', 'test_nj', 'SplitDiff', 'test_keyspace/C0-'],
                       auto_log=True)
    utils.run_vtctl(['ChangeSlaveType', shard_1_rdonly.tablet_alias, 'rdonly'],
                    auto_log=True)
    utils.run_vtctl(['ChangeSlaveType', shard_3_rdonly.tablet_alias, 'rdonly'],
                    auto_log=True)

    utils.pause("Good time to test vtworker for diffs")

    # get status for a destination master tablet, make sure we have it all
    shard_2_master_status = shard_2_master.get_status()
    self.assertIn('Binlog player state: Running', shard_2_master_status)
    self.assertIn('<td><b>All</b>: 6000<br><b>Query</b>: 4000<br><b>Transaction</b>: 2000<br></td>', shard_2_master_status)
    self.assertIn('</html>', shard_2_master_status)

    # start a thread to insert data into shard_1 in the background
    # with current time, and monitor the delay
    insert_thread_1 = InsertThread(shard_1_master, "insert_low", 10000,
                                   0x9000000000000000)
    insert_thread_2 = InsertThread(shard_1_master, "insert_high", 10001,
                                   0xD000000000000000)
    monitor_thread_1 = MonitorLagThread(shard_2_replica2, "insert_low")
    monitor_thread_2 = MonitorLagThread(shard_3_replica, "insert_high")

    # tests a failover switching serving to a different replica
    utils.run_vtctl(['ChangeSlaveType', shard_1_slave2.tablet_alias, 'replica'])
    utils.run_vtctl(['ChangeSlaveType', shard_1_slave1.tablet_alias, 'spare'])
    shard_1_slave2.wait_for_vttablet_state('SERVING')
    shard_1_slave1.wait_for_vttablet_state('NOT_SERVING')

    # test data goes through again
    logging.debug("Inserting lots of data on source shard")
    self._insert_lots(1000, base=1000)
    logging.debug("Checking 80 percent of data was sent quickly")
    self._check_lots_timeout(1000, 80, 5, base=1000)

    # check we can't migrate the master just yet
    utils.run_vtctl(['MigrateServedTypes', 'test_keyspace/80-', 'master'],
                    expect_fail=True)

    # check query service is off on master 2 and master 3, as filtered
    # replication is enabled. Even health check that is enabled on
    # master 3 should not interfere.
    shard_2_master_vars = utils.get_vars(shard_2_master.port)
    self.assertEqual(shard_2_master_vars['TabletStateName'], 'NOT_SERVING')
    shard_3_master_vars = utils.get_vars(shard_3_master.port)
    self.assertEqual(shard_3_master_vars['TabletStateName'], 'NOT_SERVING')

    # now serve rdonly from the split shards
    utils.run_vtctl(['MigrateServedTypes', 'test_keyspace/80-', 'rdonly'],
                    auto_log=True)
    utils.check_srv_keyspace('test_nj', 'test_keyspace',
                             'Partitions(master): -80 80-\n' +
                             'Partitions(rdonly): -80 80-C0 C0-\n' +
                             'Partitions(replica): -80 80-\n' +
                             'TabletTypes: master,rdonly,replica',
                             keyspace_id_type=keyspace_id_type)

    # then serve replica from the split shards
    utils.run_vtctl(['MigrateServedTypes', 'test_keyspace/80-', 'replica'],
                    auto_log=True)
    utils.check_srv_keyspace('test_nj', 'test_keyspace',
                             'Partitions(master): -80 80-\n' +
                             'Partitions(rdonly): -80 80-C0 C0-\n' +
                             'Partitions(replica): -80 80-C0 C0-\n' +
                             'TabletTypes: master,rdonly,replica',
                             keyspace_id_type=keyspace_id_type)

    # move replica back and forth
    utils.run_vtctl(['MigrateServedTypes', '-reverse', 'test_keyspace/80-', 'replica'],
                    auto_log=True)
    utils.check_srv_keyspace('test_nj', 'test_keyspace',
                             'Partitions(master): -80 80-\n' +
                             'Partitions(rdonly): -80 80-C0 C0-\n' +
                             'Partitions(replica): -80 80-\n' +
                             'TabletTypes: master,rdonly,replica',
                             keyspace_id_type=keyspace_id_type)
    utils.run_vtctl(['MigrateServedTypes', 'test_keyspace/80-', 'replica'],
                    auto_log=True)
    utils.check_srv_keyspace('test_nj', 'test_keyspace',
                             'Partitions(master): -80 80-\n' +
                             'Partitions(rdonly): -80 80-C0 C0-\n' +
                             'Partitions(replica): -80 80-C0 C0-\n' +
                             'TabletTypes: master,rdonly,replica',
                             keyspace_id_type=keyspace_id_type)

    # reparent shard_2 to shard_2_replica1, then insert more data and
    # see it flow through still
    utils.run_vtctl(['ReparentShard', 'test_keyspace/80-C0',
                    shard_2_replica1.tablet_alias])
    logging.debug("Inserting lots of data on source shard after reparenting")
    self._insert_lots(3000, base=2000)
    logging.debug("Checking 80 percent of data was sent fairly quickly")
    self._check_lots_timeout(3000, 80, 10, base=2000)

    # use the vtworker checker to compare the data again
    logging.debug("Running vtworker SplitDiff")
    utils.run_vtworker(['-cell', 'test_nj', 'SplitDiff', 'test_keyspace/C0-'],
                       auto_log=True)
    utils.run_vtctl(['ChangeSlaveType', shard_1_rdonly.tablet_alias, 'rdonly'],
                    auto_log=True)
    utils.run_vtctl(['ChangeSlaveType', shard_3_rdonly.tablet_alias, 'rdonly'],
                    auto_log=True)

    # going to migrate the master now, check the delays
    monitor_thread_1.done = True
    monitor_thread_2.done = True
    insert_thread_1.done = True
    insert_thread_2.done = True
    logging.debug("DELAY 1: %s max_lag=%u avg_lag=%u",
                  monitor_thread_1.object_name,
                  monitor_thread_1.max_lag,
                  monitor_thread_1.lag_sum / monitor_thread_1.sample_count)
    logging.debug("DELAY 2: %s max_lag=%u avg_lag=%u",
                  monitor_thread_2.object_name,
                  monitor_thread_2.max_lag,
                  monitor_thread_2.lag_sum / monitor_thread_2.sample_count)

    # then serve master from the split shards
    utils.run_vtctl(['MigrateServedTypes', 'test_keyspace/80-', 'master'],
                    auto_log=True)
    utils.check_srv_keyspace('test_nj', 'test_keyspace',
                             'Partitions(master): -80 80-C0 C0-\n' +
                             'Partitions(rdonly): -80 80-C0 C0-\n' +
                             'Partitions(replica): -80 80-C0 C0-\n' +
                             'TabletTypes: master,rdonly,replica',
                             keyspace_id_type=keyspace_id_type)

    # check the binlog players are gone now
    shard_2_master.wait_for_binlog_player_count(0)
    shard_3_master.wait_for_binlog_player_count(0)

    # get status for a destination master tablet, make sure it's good
    shard_2_master_status = shard_2_master.get_status()
    self.assertIn('No binlog player is running', shard_2_master_status)
    self.assertIn('</html>', shard_2_master_status)

    # scrap the original tablets in the original shard
    for t in [shard_1_master, shard_1_slave1, shard_1_slave2, shard_1_ny_slave,
              shard_1_rdonly]:
      utils.run_vtctl(['ScrapTablet', t.tablet_alias], auto_log=True)
    tablet.kill_tablets([shard_1_master, shard_1_slave1, shard_1_slave2,
                         shard_1_ny_slave, shard_1_rdonly])
    for t in [shard_1_master, shard_1_slave1, shard_1_slave2, shard_1_ny_slave,
              shard_1_rdonly]:
      utils.run_vtctl(['DeleteTablet', t.tablet_alias], auto_log=True)

    # rebuild the serving graph, all mentions of the old shards shoud be gone
    utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'], auto_log=True)

    # test RemoveShardCell
    utils.run_vtctl(['RemoveShardCell', 'test_keyspace/-80', 'test_nj'], auto_log=True, expect_fail=True)
    utils.run_vtctl(['RemoveShardCell', 'test_keyspace/80-', 'test_nj'], auto_log=True)
    utils.run_vtctl(['RemoveShardCell', 'test_keyspace/80-', 'test_ny'], auto_log=True)
    shard = utils.run_vtctl_json(['GetShard', 'test_keyspace/80-'])
    if shard['Cells']:
      self.fail("Non-empty Cells record for shard: %s" % str(shard))

    # delete the original shard
    utils.run_vtctl(['DeleteShard', 'test_keyspace/80-'], auto_log=True)

    # kill everything
    tablet.kill_tablets([shard_0_master, shard_0_replica, shard_0_ny_slave,
                         shard_2_master, shard_2_replica1, shard_2_replica2,
                         shard_3_master, shard_3_replica, shard_3_rdonly])
Beispiel #57
0
  def test_vertical_split(self):
    # the worker will do everything. We test with source_reader_count=10
    # (down from default=20) as connection pool is not big enough for 20.
    # min_table_size_for_split is set to 1 as to force a split even on the
    # small table we have.
    utils.run_vtctl(['CopySchemaShard', '--tables', 'moving.*,view1',
                     source_rdonly1.tablet_alias, 'destination_keyspace/0'],
                    auto_log=True)

    utils.run_vtworker(['--cell', 'test_nj',
                        '--command_display_interval', '10ms',
                        'VerticalSplitClone',
                        '--tables', 'moving.*,view1',
                        '--source_reader_count', '10',
                        '--min_table_size_for_split', '1',
                        'destination_keyspace/0'],
                       auto_log=True)
    # One of the two source rdonly tablets went spare after the clone.
    # Force a healthcheck on both to get them back to "rdonly".
    for t in [source_rdonly1, source_rdonly2]:
      utils.run_vtctl(['RunHealthCheck', t.tablet_alias, 'rdonly'])

    # check values are present
    self._check_values(destination_master, 'vt_destination_keyspace', 'moving1',
                       self.moving1_first, 100)
    self._check_values(destination_master, 'vt_destination_keyspace', 'moving2',
                       self.moving2_first, 100)
    self._check_values(destination_master, 'vt_destination_keyspace', 'view1',
                       self.moving1_first, 100)

    # check the binlog players is running
    destination_master.wait_for_binlog_player_count(1)

    # add values to source, make sure they're replicated
    moving1_first_add1 = self._insert_values('moving1', 100)
    _ = self._insert_values('staying1', 100)
    moving2_first_add1 = self._insert_values('moving2', 100)
    self._check_values_timeout(destination_master, 'vt_destination_keyspace',
                               'moving1', moving1_first_add1, 100)
    self._check_values_timeout(destination_master, 'vt_destination_keyspace',
                               'moving2', moving2_first_add1, 100)

    # use vtworker to compare the data
    for t in [destination_rdonly1, destination_rdonly2]:
      utils.run_vtctl(['RunHealthCheck', t.tablet_alias, 'rdonly'])
    logging.debug('Running vtworker VerticalSplitDiff')
    utils.run_vtworker(['-cell', 'test_nj', 'VerticalSplitDiff',
                        'destination_keyspace/0'], auto_log=True)
    # One of each source and dest rdonly tablet went spare after the diff.
    # Force a healthcheck on all four to get them back to "rdonly".
    for t in [source_rdonly1, source_rdonly2,
              destination_rdonly1, destination_rdonly2]:
      utils.run_vtctl(['RunHealthCheck', t.tablet_alias, 'rdonly'])

    utils.pause('Good time to test vtworker for diffs')

    # get status for destination master tablet, make sure we have it all
    destination_master_status = destination_master.get_status()
    self.assertIn('Binlog player state: Running', destination_master_status)
    self.assertIn('moving.*', destination_master_status)
    self.assertIn(
        '<td><b>All</b>: 1000<br><b>Query</b>: 700<br>'
        '<b>Transaction</b>: 300<br></td>', destination_master_status)
    self.assertIn('</html>', destination_master_status)

    # check query service is off on destination master, as filtered
    # replication is enabled. Even health check should not interfere.
    destination_master_vars = utils.get_vars(destination_master.port)
    self.assertEqual(destination_master_vars['TabletStateName'], 'NOT_SERVING')

    # check we can't migrate the master just yet
    utils.run_vtctl(['MigrateServedFrom', 'destination_keyspace/0', 'master'],
                    expect_fail=True)

    # migrate rdonly only in test_ny cell, make sure nothing is migrated
    # in test_nj
    utils.run_vtctl(['MigrateServedFrom', '--cells=test_ny',
                     'destination_keyspace/0', 'rdonly'],
                    auto_log=True)
    self._check_srv_keyspace('ServedFrom(master): source_keyspace\n'
                             'ServedFrom(rdonly): source_keyspace\n'
                             'ServedFrom(replica): source_keyspace\n')
    self._check_blacklisted_tables(source_master, None)
    self._check_blacklisted_tables(source_replica, None)
    self._check_blacklisted_tables(source_rdonly1, None)
    self._check_blacklisted_tables(source_rdonly2, None)

    # migrate test_nj only, using command line manual fix command,
    # and restore it back.
    keyspace_json = utils.run_vtctl_json(
        ['GetKeyspace', 'destination_keyspace'])
    found = False
    for ksf in keyspace_json['served_froms']:
      if ksf['tablet_type'] == topodata_pb2.RDONLY:
        found = True
        self.assertEqual(ksf['cells'], ['test_nj'])
    self.assertTrue(found)
    utils.run_vtctl(['SetKeyspaceServedFrom', '-source=source_keyspace',
                     '-remove', '-cells=test_nj', 'destination_keyspace',
                     'rdonly'], auto_log=True)
    keyspace_json = utils.run_vtctl_json(
        ['GetKeyspace', 'destination_keyspace'])
    found = False
    for ksf in keyspace_json['served_froms']:
      if ksf['tablet_type'] == topodata_pb2.RDONLY:
        found = True
    self.assertFalse(found)
    utils.run_vtctl(['SetKeyspaceServedFrom', '-source=source_keyspace',
                     'destination_keyspace', 'rdonly'],
                    auto_log=True)
    keyspace_json = utils.run_vtctl_json(
        ['GetKeyspace', 'destination_keyspace'])
    found = False
    for ksf in keyspace_json['served_froms']:
      if ksf['tablet_type'] == topodata_pb2.RDONLY:
        found = True
        self.assertNotIn('cells', ksf)
    self.assertTrue(found)

    # now serve rdonly from the destination shards
    utils.run_vtctl(['MigrateServedFrom', 'destination_keyspace/0', 'rdonly'],
                    auto_log=True)
    self._check_srv_keyspace('ServedFrom(master): source_keyspace\n'
                             'ServedFrom(replica): source_keyspace\n')
    self._check_blacklisted_tables(source_master, None)
    self._check_blacklisted_tables(source_replica, None)
    self._check_blacklisted_tables(source_rdonly1, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly2, ['moving.*', 'view1'])
    self._check_client_conn_redirection(
        'destination_keyspace',
        ['master', 'replica'], ['moving1', 'moving2'])

    # then serve replica from the destination shards
    utils.run_vtctl(['MigrateServedFrom', 'destination_keyspace/0', 'replica'],
                    auto_log=True)
    self._check_srv_keyspace('ServedFrom(master): source_keyspace\n')
    self._check_blacklisted_tables(source_master, None)
    self._check_blacklisted_tables(source_replica, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly1, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly2, ['moving.*', 'view1'])
    self._check_client_conn_redirection(
        'destination_keyspace',
        ['master'], ['moving1', 'moving2'])

    # move replica back and forth
    utils.run_vtctl(['MigrateServedFrom', '-reverse',
                     'destination_keyspace/0', 'replica'], auto_log=True)
    self._check_srv_keyspace('ServedFrom(master): source_keyspace\n'
                             'ServedFrom(replica): source_keyspace\n')
    self._check_blacklisted_tables(source_master, None)
    self._check_blacklisted_tables(source_replica, None)
    self._check_blacklisted_tables(source_rdonly1, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly2, ['moving.*', 'view1'])
    utils.run_vtctl(['MigrateServedFrom', 'destination_keyspace/0', 'replica'],
                    auto_log=True)
    self._check_srv_keyspace('ServedFrom(master): source_keyspace\n')
    self._check_blacklisted_tables(source_master, None)
    self._check_blacklisted_tables(source_replica, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly1, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly2, ['moving.*', 'view1'])
    self._check_client_conn_redirection(
        'destination_keyspace',
        ['master'], ['moving1', 'moving2'])

    # then serve master from the destination shards
    utils.run_vtctl(['MigrateServedFrom', 'destination_keyspace/0', 'master'],
                    auto_log=True)
    self._check_srv_keyspace('')
    self._check_blacklisted_tables(source_master, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_replica, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly1, ['moving.*', 'view1'])
    self._check_blacklisted_tables(source_rdonly2, ['moving.*', 'view1'])

    # check the binlog player is gone now
    destination_master.wait_for_binlog_player_count(0)

    # check the stats are correct
    self._check_stats()

    self._verify_vtctl_set_shard_tablet_control()