Beispiel #1
0
  def test_vtgate(self):
    # Start up a master mysql and vttablet
    utils.run_vtctl('CreateKeyspace -force test_keyspace')
    utils.run_vtctl('CreateShard -force test_keyspace/0')
    tablet_62344.init_tablet('master', 'test_keyspace', '0', parent=False)
    utils.run_vtctl('RebuildShardGraph test_keyspace/0')
    utils.run_vtctl('RebuildKeyspaceGraph test_keyspace')
    utils.validate_topology()

    # if these statements don't run before the tablet it will wedge waiting for the
    # db to become accessible. this is more a bug than a feature.
    tablet_62344.mquery("", ["set global read_only = off"])
    tablet_62344.populate('vt_test_keyspace', self._create_vt_select_test,
                          self._populate_vt_select_test)

    tablet_62344.start_vttablet()
    gate_proc = utils.vtgate_start()

    try:
      conn = vtgate.connect("localhost:%s"%(utils.vtgate_port_base), "master", "test_keyspace", "0", 2.0)

      # _execute
      (result, count, lastrow, fields) = conn._execute("select * from vt_select_test", {})
      self.assertEqual(count, 4, "want 4, got %d" % (count))
      self.assertEqual(len(fields), 2, "want 2, got %d" % (len(fields)))

      # _stream_execute
      (result, count, lastrow, fields) = conn._stream_execute("select * from vt_select_test", {})
      self.assertEqual(len(fields), 2, "want 2, got %d" % (len(fields)))
      count = 0
      while 1:
        r = conn._stream_next()
        if not r:
          break
        count += 1
      self.assertEqual(count, 4, "want 4, got %d" % (count))

      # begin-rollback
      conn.begin()
      conn._execute("insert into vt_select_test values(:id, :msg)", {"id": 5, "msg": "test4"})
      conn.rollback()
      (result, count, lastrow, fields) = conn._execute("select * from vt_select_test", {})
      self.assertEqual(count, 4, "want 4, got %d" % (count))

      # begin-commit
      conn.begin()
      conn._execute("insert into vt_select_test values(:id, :msg)", {"id": 5, "msg": "test4"})
      conn.commit()
      (result, count, lastrow, fields) = conn._execute("select * from vt_select_test", {})
      self.assertEqual(count, 5, "want 5, got %d" % (count))

      # close
      conn.close()
    finally:
      utils.vtgate_kill(gate_proc)
      tablet_62344.kill_vttablet()
Beispiel #2
0
  def test_vtgate(self):
    # Start up a master mysql and vttablet
    utils.run_vtctl(['CreateKeyspace', 'test_keyspace'])
    utils.run_vtctl(['CreateShard', 'test_keyspace/0'])
    tablet_62344.init_tablet('master', 'test_keyspace', '0', parent=False)
    utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'])
    utils.validate_topology()

    # if these statements don't run before the tablet it will wedge waiting for the
    # db to become accessible. this is more a bug than a feature.
    tablet_62344.mquery("", ["set global read_only = off"])
    tablet_62344.populate('vt_test_keyspace', self._create_vt_select_test,
                          self._populate_vt_select_test)

    tablet_62344.start_vttablet()
    gate_proc, gate_port = utils.vtgate_start()

    conn = vtgate.connect("localhost:%s"%(gate_port), "master", "test_keyspace", "0", 2.0)

    # _execute
    (result, count, lastrow, fields) = conn._execute("select * from vt_select_test", {})
    self.assertEqual(count, 4, "want 4, got %d" % (count))
    self.assertEqual(len(fields), 2, "want 2, got %d" % (len(fields)))

    # _execute_batch
    queries = [
      "select * from vt_select_test where id = :id",
      "select * from vt_select_test where id = :id",
      ]
    bindvars = [
      {"id": 1},
      {"id": 2},
      ]
    rowsets = conn._execute_batch(queries, bindvars)
    self.assertEqual(rowsets[0][0][0][0], 1)
    self.assertEqual(rowsets[1][0][0][0], 2)

    # _stream_execute
    (result, count, lastrow, fields) = conn._stream_execute("select * from vt_select_test", {})
    self.assertEqual(len(fields), 2, "want 2, got %d" % (len(fields)))
    count = 0
    while 1:
      r = conn._stream_next()
      if not r:
        break
      count += 1
    self.assertEqual(count, 4, "want 4, got %d" % (count))

    # begin-rollback
    conn.begin()
    conn._execute("insert into vt_select_test values(:id, :msg)", {"id": 5, "msg": "test4"})
    conn.rollback()
    (result, count, lastrow, fields) = conn._execute("select * from vt_select_test", {})
    self.assertEqual(count, 4, "want 4, got %d" % (count))

    # begin-commit
    conn.begin()
    conn._execute("insert into vt_select_test values(:id, :msg)", {"id": 5, "msg": "test4"})
    conn.commit()
    (result, count, lastrow, fields) = conn._execute("select * from vt_select_test", {})
    self.assertEqual(count, 5, "want 5, got %d" % (count))

    # error on dml. We still need to get a transaction id
    conn.begin()
    with self.assertRaises(dbexceptions.IntegrityError):
      conn._execute("insert into vt_select_test values(:id, :msg)", {"id": 5, "msg": "test4"})
    self.assertTrue(conn.session["ShardSessions"][0]["TransactionId"] != 0)
    conn.commit()

    # interleaving
    conn2 = vtgate.connect("localhost:%s"%(gate_port), "master", "test_keyspace", "0", 2.0)
    thd = threading.Thread(target=self._query_lots, args=(conn2,))
    thd.start()
    for i in xrange(250):
      (result, count, lastrow, fields) = conn._execute("select id from vt_select_test where id = 2", {})
      self.assertEqual(result, [(2,)])
      if i % 10 == 0:
        conn._stream_execute("select id from vt_select_test where id = 3", {})
        while 1:
          result = conn._stream_next()
          if not result:
            break
          self.assertEqual(result, (3,))
    thd.join()

    # close
    conn.close()

    utils.vtgate_kill(gate_proc)
    tablet_62344.kill_vttablet()
Beispiel #3
0
    def test_vtgate(self):
        # Start up a master mysql and vttablet
        utils.run_vtctl(['CreateKeyspace', 'test_keyspace'])
        utils.run_vtctl(['CreateShard', 'test_keyspace/0'])
        tablet_62344.init_tablet('master', 'test_keyspace', '0', parent=False)
        utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'])
        utils.validate_topology()
        srvShard = utils.run_vtctl_json(
            ['GetSrvShard', 'test_nj', 'test_keyspace/0'])
        self.assertEqual(srvShard['MasterCell'], 'test_nj')

        # if these statements don't run before the tablet it will wedge
        # waiting for the db to become accessible. this is more a bug than
        # a feature.
        tablet_62344.mquery("", ["set global read_only = off"])
        tablet_62344.populate('vt_test_keyspace', self._create_vt_select_test,
                              self._populate_vt_select_test)

        tablet_62344.start_vttablet()
        gate_proc, gate_port = utils.vtgate_start()

        conn = vtgate.connect("localhost:%s" % (gate_port), "master",
                              "test_keyspace", "0", 2.0)

        # _execute
        (result, count, lastrow,
         fields) = conn._execute("select * from vt_select_test", {})
        self.assertEqual(count, 4, "want 4, got %d" % (count))
        self.assertEqual(len(fields), 2, "want 2, got %d" % (len(fields)))

        # _execute_batch
        queries = [
            "select * from vt_select_test where id = :id",
            "select * from vt_select_test where id = :id",
        ]
        bindvars = [
            {
                "id": 1
            },
            {
                "id": 2
            },
        ]
        rowsets = conn._execute_batch(queries, bindvars)
        self.assertEqual(rowsets[0][0][0][0], 1)
        self.assertEqual(rowsets[1][0][0][0], 2)

        # _stream_execute
        (result, count, lastrow,
         fields) = conn._stream_execute("select * from vt_select_test", {})
        self.assertEqual(len(fields), 2, "want 2, got %d" % (len(fields)))
        count = 0
        while 1:
            r = conn._stream_next()
            if not r:
                break
            count += 1
        self.assertEqual(count, 4, "want 4, got %d" % (count))

        # begin-rollback
        conn.begin()
        conn._execute("insert into vt_select_test values(:id, :msg)", {
            "id": 5,
            "msg": "test4"
        })
        conn.rollback()
        (result, count, lastrow,
         fields) = conn._execute("select * from vt_select_test", {})
        self.assertEqual(count, 4, "want 4, got %d" % (count))

        # begin-commit
        conn.begin()
        conn._execute("insert into vt_select_test values(:id, :msg)", {
            "id": 5,
            "msg": "test4"
        })
        conn.commit()
        (result, count, lastrow,
         fields) = conn._execute("select * from vt_select_test", {})
        self.assertEqual(count, 5, "want 5, got %d" % (count))

        # error on dml. We still need to get a transaction id
        conn.begin()
        with self.assertRaises(dbexceptions.IntegrityError):
            conn._execute("insert into vt_select_test values(:id, :msg)", {
                "id": 5,
                "msg": "test4"
            })
        self.assertTrue(conn.session["ShardSessions"][0]["TransactionId"] != 0)
        conn.commit()

        # interleaving
        conn2 = vtgate.connect("localhost:%s" % (gate_port), "master",
                               "test_keyspace", "0", 2.0)
        thd = threading.Thread(target=self._query_lots, args=(conn2, ))
        thd.start()
        for i in xrange(250):
            (result, count, lastrow, fields) = conn._execute(
                "select id from vt_select_test where id = 2", {})
            self.assertEqual(result, [(2, )])
            if i % 10 == 0:
                conn._stream_execute(
                    "select id from vt_select_test where id = 3", {})
                while 1:
                    result = conn._stream_next()
                    if not result:
                        break
                    self.assertEqual(result, (3, ))
        thd.join()

        # close
        conn.close()

        utils.vtgate_kill(gate_proc)
        tablet_62344.kill_vttablet()
Beispiel #4
0
  def test_secure(self):
    zkocc_server = utils.zkocc_start()

    # start the tablets
    shard_0_master.start_vttablet(cert=cert_dir + "/vt-server-cert.pem",
                                  key=cert_dir + "/vt-server-key.pem")
    shard_0_slave.start_vttablet(cert=cert_dir + "/vt-server-cert.pem",
                                 key=cert_dir + "/vt-server-key.pem",
                                 repl_extra_flags={
        'flags': "2048",
        'ssl-ca': cert_dir + "/ca-cert.pem",
        'ssl-cert': cert_dir + "/client-cert.pem",
        'ssl-key': cert_dir + "/client-key.pem",
        })

    # Reparent using SSL
    for t in [shard_0_master, shard_0_slave]:
      t.reset_replication()
    utils.run_vtctl('ReparentShard -force test_keyspace/0 ' + shard_0_master.tablet_alias, auto_log=True)

    # then get the topology and check it
    zkocc_client = zkocc.ZkOccConnection("localhost:%u" % utils.zkocc_port_base,
                                         "test_nj", 30.0)
    topology.read_keyspaces(zkocc_client)

    shard_0_master_addrs = topology.get_host_port_by_name(zkocc_client, "test_keyspace.0.master:_vts")
    if len(shard_0_master_addrs) != 1:
      self.fail('topology.get_host_port_by_name failed for "test_keyspace.0.master:_vts", got: %s' % " ".join(["%s:%u(%s)" % (h, p, str(e)) for (h, p, e) in shard_0_master_addrs]))
    if shard_0_master_addrs[0][2] != True:
      self.fail('topology.get_host_port_by_name failed for "test_keyspace.0.master:_vts" is not encrypted')
    logging.debug("shard 0 master addrs: %s", " ".join(["%s:%u(%s)" % (h, p, str(e)) for (h, p, e) in shard_0_master_addrs]))

    # make sure asking for optionally secure connections works too
    auto_addrs = topology.get_host_port_by_name(zkocc_client, "test_keyspace.0.master:_vtocc", encrypted=True)
    if auto_addrs != shard_0_master_addrs:
      self.fail('topology.get_host_port_by_name doesn\'t resolve encrypted addresses properly: %s != %s' % (str(shard_0_master_addrs), str(auto_addrs)))

    # try to connect with regular client
    try:
      conn = tablet3.TabletConnection("%s:%u" % (shard_0_master_addrs[0][0], shard_0_master_addrs[0][1]),
                                      "test_keyspace", "0", 10.0)
      conn.dial()
      self.fail("No exception raised to secure port")
    except tablet3.FatalError as e:
      if not e.args[0][0].startswith('Unexpected EOF in handshake to'):
        self.fail("Unexpected exception: %s" % str(e))

    sconn = utils.get_vars(shard_0_master.port)["SecureConnections"]
    if sconn != 0:
      self.fail("unexpected conns %s" % sconn)

    # connect to encrypted port
    conn = tablet3.TabletConnection("%s:%u" % (shard_0_master_addrs[0][0], shard_0_master_addrs[0][1]),
                                    "test_keyspace", "0", 5.0, encrypted=True)
    conn.dial()
    (results, rowcount, lastrowid, fields) = conn._execute("select 1 from dual", {})
    self.assertEqual(results, [(1,),], 'wrong conn._execute output: %s' % str(results))

    sconn = utils.get_vars(shard_0_master.port)["SecureConnections"]
    if sconn != 1:
      self.fail("unexpected conns %s" % sconn)
    saccept = utils.get_vars(shard_0_master.port)["SecureAccepts"]
    if saccept == 0:
      self.fail("unexpected accepts %s" % saccept)

    # trigger a time out on a secure connection, see what exception we get
    try:
      conn._execute("select sleep(100) from dual", {})
      self.fail("No timeout exception")
    except tablet3.TimeoutError as e:
      logging.debug("Got the right exception for SSL timeout: %s", str(e))

    # start a vtgate to connect to that tablet
    gate_proc, gate_port = utils.vtgate_start(tablet_bson_encrypted=True)
    conn = vtgate.connect("localhost:%s"%(gate_port), "master", "test_keyspace", "0", 2.0)

    # _execute
    (result, count, lastrow, fields) = conn._execute("select 1 from dual", {})
    logging.debug("Got result: %s", str(result))
    self.assertEqual(count, 1, "want 1, got %d" % (count))
    self.assertEqual(len(fields), 1, "want 1, got %d" % (len(fields)))

    conn.close()
    utils.vtgate_kill(gate_proc)

    # kill everything
    utils.kill_sub_process(zkocc_server)