Package com.youtube.vitess.vtgate

Examples of com.youtube.vitess.vtgate.VtGate


  @Override
  public List<InputSplit> getSplits(JobContext context) {
    try {
      VitessConf conf = new VitessConf(context.getConfiguration());
      VtGate vtgate = VtGate.connect(conf.getHosts(), conf.getTimeoutMs());
      List<String> columns = conf.getInputColumns();
      if (!columns.contains(KeyspaceId.COL_NAME)) {
        columns.add(KeyspaceId.COL_NAME);
      }
      String sql = "select " + StringUtils.join(columns, ',') + " from " + conf.getInputTable();
      Map<Query, Long> queries =
          vtgate.splitQuery(conf.getKeyspace(), sql, conf.getSplitsPerShard());
      List<InputSplit> splits = new LinkedList<>();
      for (Query query : queries.keySet()) {
        Long size = queries.get(query);
        InputSplit split = new VitessInputSplit(query, size);
        splits.add(split);
View Full Code Here


    Util.createTable(testEnv);
  }

  @Test
  public void testIntegrityException() throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String insertSql = "insert into vtgate_test(id, keyspace_id) values (:id, :keyspace_id)";
    KeyspaceId kid = testEnv.getAllKeyspaceIds().get(0);
    Query insertQuery = new QueryBuilder(insertSql, testEnv.keyspace, "master")
        .addBindVar(BindVariable.forULong("id", UnsignedLong.valueOf("1")))
        .addBindVar(BindVariable.forULong("keyspace_id", ((UnsignedLong) kid.getId())))
        .addKeyspaceId(kid).build();
    vtgate.begin();
    vtgate.execute(insertQuery);
    vtgate.commit();
    vtgate.begin();
    try {
      vtgate.execute(insertQuery);
      Assert.fail("failed to throw exception");
    } catch (IntegrityException e) {
    } finally {
      vtgate.rollback();
      vtgate.close();
    }
  }
View Full Code Here

    }
  }

  @Test
  public void testTimeout() throws ConnectionException, DatabaseException {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 200);
    // Check timeout error raised for slow query
    Query sleepQuery = new QueryBuilder("select sleep(0.5) from dual", testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).build();
    try {
      vtgate.execute(sleepQuery);
      Assert.fail("did not raise timeout exception");
    } catch (ConnectionException e) {
    }
    vtgate.close();
    vtgate = VtGate.connect("localhost:" + testEnv.port, 2000);
    // Check no timeout error for fast query
    sleepQuery = new QueryBuilder("select sleep(0.01) from dual", testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).build();
    vtgate.execute(sleepQuery);
    vtgate.close();
  }
View Full Code Here

    List<VtGate> vtgates = new ArrayList<>();
    boolean failed = false;
    try {
      // Transaction cap is 20
      for (int i = 0; i < 25; i++) {
        VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
        vtgates.add(vtgate);
        vtgate.begin();
        // Run a query to actually begin a transaction with the tablets
        Query query = new QueryBuilder("delete from vtgate_test", testEnv.keyspace, "master")
            .addKeyRange(KeyRange.ALL).build();
        vtgate.execute(query);
      }
    } catch (DatabaseException e) {
      if (e.getMessage().contains("tx_pool_full")) {
        failed = true;
      }
    } finally {
      for (VtGate vtgate : vtgates) {
        vtgate.close();
      }
    }
    if (!failed) {
      Assert.fail("failed to raise tx_pool_full exception");
    }
View Full Code Here

  }

  @Test
  public void testShutdownServerWhileStreaming() throws Exception {
    Util.insertRows(testEnv, 1, 2000);
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String selectSql = "select A.* from vtgate_test A join vtgate_test B";
    Query joinQuery = new QueryBuilder(selectSql, testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).setStreaming(true).build();
    Cursor cursor = vtgate.execute(joinQuery);

    int count = 0;
    try {
      while (cursor.hasNext()) {
        count++;
        if (count == 1) {
          Util.setupTestEnv(testEnv, false);
        }
        cursor.next();
      }
      vtgate.close();
      Assert.fail("failed to raise exception");
    } catch (RuntimeException e) {
      Assert.assertTrue(e.getMessage().contains("vtgate exception: connection exception"));
    }
  }
View Full Code Here

    Util.createTable(testEnv);
  }

  @Test
  public void testStreamCursorType() throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String selectSql = "select * from vtgate_test";
    Query allRowsQuery = new QueryBuilder(selectSql, testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).setStreaming(true).build();
    Cursor cursor = vtgate.execute(allRowsQuery);
    Assert.assertEquals(StreamCursor.class, cursor.getClass());
    vtgate.close();
  }
View Full Code Here

  public void testStreamExecuteKeyspaceIds() throws Exception {
    int rowCount = 10;
    for (String shardName : testEnv.shardKidMap.keySet()) {
      Util.insertRowsInShard(testEnv, shardName, rowCount);
    }
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    for (String shardName : testEnv.shardKidMap.keySet()) {
      String selectSql = "select A.* from vtgate_test A join vtgate_test B join vtgate_test C";
      Query query = new QueryBuilder(selectSql, testEnv.keyspace, "master")
          .setKeyspaceIds(testEnv.getKeyspaceIds(shardName)).setStreaming(true).build();
      Cursor cursor = vtgate.execute(query);
      int count = 0;
      while (cursor.hasNext()) {
        cursor.next();
        count++;
      }
      Assert.assertEquals((int) Math.pow(rowCount, 3), count);
    }
    vtgate.close();
  }
View Full Code Here

  /**
   * Same as testStreamExecuteKeyspaceIds but for StreamExecuteKeyRanges
   */
  @Test
  public void testStreamExecuteKeyRanges() throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    int rowCount = 10;
    for (String shardName : testEnv.shardKidMap.keySet()) {
      Util.insertRowsInShard(testEnv, shardName, rowCount);
    }
    for (String shardName : testEnv.shardKidMap.keySet()) {
      List<KeyspaceId> kids = testEnv.getKeyspaceIds(shardName);
      KeyRange kr = new KeyRange(Collections.min(kids), Collections.max(kids));
      String selectSql = "select A.* from vtgate_test A join vtgate_test B join vtgate_test C";
      Query query = new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(kr)
          .setStreaming(true).build();
      Cursor cursor = vtgate.execute(query);
      int count = 0;
      while (cursor.hasNext()) {
        cursor.next();
        count++;
      }
      Assert.assertEquals((int) Math.pow(rowCount, 3), count);
    }
    vtgate.close();
  }
View Full Code Here

      Util.insertRowsInShard(testEnv, shardName, rowCount);
    }
    String selectSql = "select A.* from vtgate_test A join vtgate_test B join vtgate_test C";
    Query query = new QueryBuilder(selectSql, testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).setStreaming(true).build();
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    Cursor cursor = vtgate.execute(query);
    int count = 0;
    for (Row row : cursor) {
      count++;
    }
    Assert.assertEquals(2 * (int) Math.pow(rowCount, 3), count);
    vtgate.close();
  }
View Full Code Here

  }

  @Test
  @Ignore("currently failing as vtgate doesn't set the error")
  public void testStreamingWrites() throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);

    vtgate.begin();
    String insertSql = "insert into vtgate_test " + "(id, name, age, percent, keyspace_id) "
        + "values (:id, :name, :age, :percent, :keyspace_id)";
    KeyspaceId kid = testEnv.getAllKeyspaceIds().get(0);
    Query query = new QueryBuilder(insertSql, testEnv.keyspace, "master")
        .addBindVar(BindVariable.forULong("id", UnsignedLong.valueOf("" + 1)))
        .addBindVar(BindVariable.forString("name", ("name_" + 1)))
        .addBindVar(BindVariable.forULong("keyspace_id", (UnsignedLong) kid.getId()))
        .addKeyspaceId(kid)
        .setStreaming(true)
        .build();
    vtgate.execute(query);
    vtgate.commit();
    vtgate.close();
  }
View Full Code Here

TOP

Related Classes of com.youtube.vitess.vtgate.VtGate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.