@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(); }
/** 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; }
/** 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(); }
/** 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."); }