Ejemplo n.º 1
0
  /** Loads stats from /proc/[pid]/stat and /proc/[pid]/statm files */
  public static ProcessStatus getProcStats(String pid) {

    // using a space as the delimeter.
    String data;

    // get the /proc/[pid]/stat as a string and them split into string array
    // using a space as the delimeter.
    try {
      data = FileReadUtil.readFirstLine(String.format(STAT_FILE, pid));
      String[] procStatData = data.split(SPACE_VALUE);
      long startTimeSecs =
          UnsignedLong.valueOf(procStatData[STAT_STARTTIME])
              .dividedBy(UnsignedLong.fromLongBits(HZ))
              .longValue();
      long currTimeSecs = new Date().getTime() / 1000;
      long upTimeSecs = currTimeSecs - (getBootTime() + startTimeSecs);
      return new ProcessStatus(
          upTimeSecs,
          Long.parseLong(procStatData[STAT_NUM_THREADS]),
          startTimeSecs,
          Integer.parseInt(procStatData[STAT_PID]),
          Long.parseLong(procStatData[STAT_RSS]) * getPageSize(),
          Long.parseLong(procStatData[STAT_VSIZE]));
    } catch (Exception e) {
      _log.error("Error occurred while getting service stats from /stats file: {}", e);
    }

    return null;
  }
Ejemplo n.º 2
0
  /** Test selects using ExecuteKeyspaceIds */
  @Test
  public void testExecuteKeyspaceIds() throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);

    // Ensure empty table
    String selectSql = "select * from vtgate_test";
    Query allRowsQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master")
            .setKeyspaceIds(testEnv.getAllKeyspaceIds())
            .build();
    Cursor cursor = vtgate.execute(allRowsQuery);
    Assert.assertEquals(CursorImpl.class, cursor.getClass());
    Assert.assertEquals(0, cursor.getRowsAffected());
    Assert.assertEquals(0, cursor.getLastRowId());
    Assert.assertFalse(cursor.hasNext());
    vtgate.close();

    // Insert 10 rows
    Util.insertRows(testEnv, 1000, 10);

    vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    cursor = vtgate.execute(allRowsQuery);
    Assert.assertEquals(10, cursor.getRowsAffected());
    Assert.assertEquals(0, cursor.getLastRowId());
    Assert.assertTrue(cursor.hasNext());

    // Fetch all rows from the first shard
    KeyspaceId firstKid = testEnv.getAllKeyspaceIds().get(0);
    Query query =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyspaceId(firstKid).build();
    cursor = vtgate.execute(query);

    // Check field types and values
    Row row = cursor.next();
    Cell idCell = row.next();
    Assert.assertEquals("id", idCell.getName());
    Assert.assertEquals(UnsignedLong.class, idCell.getType());
    UnsignedLong id = row.getULong(idCell.getName());

    Cell nameCell = row.next();
    Assert.assertEquals("name", nameCell.getName());
    Assert.assertEquals(byte[].class, nameCell.getType());
    Assert.assertTrue(
        Arrays.equals(("name_" + id.toString()).getBytes(), row.getBytes(nameCell.getName())));

    Cell ageCell = row.next();
    Assert.assertEquals("age", ageCell.getName());
    Assert.assertEquals(Integer.class, ageCell.getType());
    Assert.assertEquals(Integer.valueOf(2 * id.intValue()), row.getInt(ageCell.getName()));

    vtgate.close();
  }
Ejemplo n.º 3
0
 @Test
 public void testBatchExecuteKeyspaceIds() throws Exception {
   int rowsPerShard = 5;
   for (String shardName : testEnv.shardKidMap.keySet()) {
     Util.insertRowsInShard(testEnv, shardName, rowsPerShard);
   }
   VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
   BatchQuery query =
       new BatchQueryBuilder(testEnv.keyspace, "master")
           .addSqlAndBindVars("select * from vtgate_test where id = 3", null)
           .addSqlAndBindVars(
               "select * from vtgate_test where id = :id",
               Lists.newArrayList(BindVariable.forULong("id", UnsignedLong.valueOf("4"))))
           .withKeyspaceIds(testEnv.getAllKeyspaceIds())
           .build();
   List<Long> expected = Lists.newArrayList(3L, 3L, 4L, 4L);
   List<Cursor> cursors = vtgate.execute(query);
   List<Long> actual = new ArrayList<>();
   for (Cursor cursor : cursors) {
     for (Row row : cursor) {
       actual.add(row.getULong("id").longValue());
     }
   }
   Assert.assertTrue(expected.equals(actual));
   vtgate.close();
 }
Ejemplo n.º 4
0
  /*
   * (non-Javadoc)
   *
   * @see com.dianping.puma.parser.mysql.event.AbstractBinlogEvent#doParse(java .nio.ByteBuffer,
   * com.dianping.puma.common.bo.PumaContext)
   */
  @Override
  public void doParse(ByteBuffer buf, PumaContext context) throws IOException {
    tableId = PacketUtils.readLong(buf, 6);
    reserved = PacketUtils.readInt(buf, 2);
    databaseNameLength = buf.get();
    databaseName = PacketUtils.readNullTerminatedString(buf);
    tableNameLength = buf.get();
    tableName = PacketUtils.readNullTerminatedString(buf);
    columnCount = PacketUtils.readLengthCodedUnsignedLong(buf);
    columnTypes = PacketUtils.readBytes(buf, columnCount.intValue());
    columnMetadataCount = PacketUtils.readLengthCodedUnsignedLong(buf);

    columnMetadata =
        Metadata.valueOf(columnTypes, PacketUtils.readBytes(buf, columnMetadataCount.intValue()));
    columnNullabilities = PacketUtils.readBitSet(buf, columnCount.intValue());

    context.getTableMaps().put(tableId, this);
  }
Ejemplo n.º 5
0
  /** Test inserts using KeyRange query */
  @Test
  public void testKeyRangeWrites() throws Exception {
    Random random = new Random();
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    String sql =
        "insert into vtgate_test "
            + "(id, name, keyspace_id, age) "
            + "values (:id, :name, :keyspace_id, :age)";
    int count = 20;
    // Insert 20 rows per shard
    for (String shardName : testEnv.shardKidMap.keySet()) {
      List<KeyspaceId> kids = testEnv.getKeyspaceIds(shardName);
      KeyspaceId minKid = Collections.min(kids);
      KeyspaceId maxKid = Collections.max(kids);
      KeyRange kr = new KeyRange(minKid, maxKid);
      for (int i = 0; i < count; i++) {
        KeyspaceId kid = kids.get(i % kids.size());
        Query query =
            new QueryBuilder(sql, testEnv.keyspace, "master")
                .addBindVar(
                    BindVariable.forULong(
                        "id", UnsignedLong.valueOf("" + Math.abs(random.nextInt()))))
                .addBindVar(BindVariable.forString("name", ("name_" + i)))
                .addBindVar(BindVariable.forULong("keyspace_id", (UnsignedLong) kid.getId()))
                .addBindVar(BindVariable.forNull("age"))
                .addKeyRange(kr)
                .build();
        vtgate.execute(query);
      }
    }
    vtgate.commit();
    vtgate.close();

    // Check 40 rows exist in total
    vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String selectSql = "select * from vtgate_test";
    Query allRowsQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master")
            .setKeyspaceIds(testEnv.getAllKeyspaceIds())
            .build();
    Cursor cursor = vtgate.execute(allRowsQuery);
    Assert.assertEquals(count * 2, cursor.getRowsAffected());

    // Check 20 rows exist per shard
    for (String shardName : testEnv.shardKidMap.keySet()) {
      Query shardRows =
          new QueryBuilder(selectSql, testEnv.keyspace, "master")
              .setKeyspaceIds(testEnv.getKeyspaceIds(shardName))
              .build();
      cursor = vtgate.execute(shardRows);
      Assert.assertEquals(count, cursor.getRowsAffected());
    }

    vtgate.close();
  }
Ejemplo n.º 6
0
  /** Retrieves CPU stats from /proc/stat file. */
  public static CPUStats getCPUStats() throws IOException, SyssvcInternalException {
    String[] fileData = FileReadUtil.readLines(PROC_STAT);
    for (String line : fileData) {
      if (line != null && line.startsWith("cpu")) {
        String[] stats = line.trim().split(SPACE_VALUE);
        UnsignedLong systemMode = UnsignedLong.valueOf(stats[3]);
        if (stats.length > 7) {
          systemMode =
              systemMode.plus(UnsignedLong.valueOf(stats[6]).plus(UnsignedLong.valueOf(stats[7])));
        }
        return new CPUStats(
            UnsignedLong.valueOf(stats[1]).plus(UnsignedLong.valueOf(stats[2])),
            systemMode,
            UnsignedLong.valueOf(stats[4]),
            UnsignedLong.valueOf(stats[5]));
      }
    }

    throw SyssvcException.syssvcExceptions.syssvcInternalError("CPU stats not found.");
  }
Ejemplo n.º 7
0
 @Test
 public void testGetULong() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(
                   Field.newBuilder()
                       .setName("col0")
                       .setType(Field.Type.TYPE_LONGLONG)
                       .setFlags(Field.Flag.VT_UNSIGNED_FLAG_VALUE)
                       .build())
               .addRows(
                   Row.newBuilder().addValues(ByteString.copyFromUtf8("18446744073709551615")))
               .build())) {
     cursor.next();
     Assert.assertEquals(UnsignedLong.fromLongBits(-1), cursor.getULong("col0"));
   }
 }
Ejemplo n.º 8
0
 @Test
 public void testGetULong() throws Exception {
   try (Cursor cursor =
       new SimpleCursor(
           QueryResult.newBuilder()
               .addFields(Field.newBuilder().setName("col1").setType(Query.Type.UINT64).build())
               .addFields(Field.newBuilder().setName("null").setType(Query.Type.UINT64).build())
               .addRows(
                   Query.Row.newBuilder()
                       .addLengths("18446744073709551615".length())
                       .addLengths(-1) // SQL NULL
                       .setValues(ByteString.copyFromUtf8("18446744073709551615")))
               .build())) {
     Row row = cursor.next();
     Assert.assertNotNull(row);
     Assert.assertEquals(UnsignedLong.fromLongBits(-1), row.getULong("col1"));
     Assert.assertFalse(row.wasNull());
     Assert.assertEquals(null, row.getULong("null"));
     Assert.assertTrue(row.wasNull());
   }
 }