Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Commit

Permalink
Merge pull request #260 from eric-haibin-lin/sid
Browse files Browse the repository at this point in the history
#246 Support database selection when selecting tables
  • Loading branch information
MattPerron authored Sep 20, 2016
2 parents 63e8b24 + 876973c commit fb6856a
Show file tree
Hide file tree
Showing 46 changed files with 655 additions and 317 deletions.
162 changes: 161 additions & 1 deletion script/testing/jdbc/src/PelotonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.postgresql.util.*;

public class PelotonTest {
private final String url = "jdbc:postgresql://localhost:5432/";
private final String url = "jdbc:postgresql://localhost:54321/";
private final String username = "postgres";
private final String pass = "postgres";

private final int STAT_INTERVAL_MS = 1000;

private final String DROP = "DROP TABLE IF EXISTS A;" +
"DROP TABLE IF EXISTS B;";
private final String DDL = "CREATE TABLE A (id INT PRIMARY KEY, data TEXT);";
Expand Down Expand Up @@ -95,6 +97,17 @@ public class PelotonTest {

private enum TABLE {A, B, AB}

// To test the database stats colleced
private final String SELECT_DB_METRIC = "SELECT * FROM catalog_db.database_metric;";

// To test the query stats colleced
private final String SELECT_QUERY_METRIC = "SELECT * FROM catalog_db.query_metric;";

// To test the index stats colleced
private final String SELECT_INDEX_METRIC = "SELECT * FROM catalog_db.index_metric;";

// To test the table stats colleced
private final String SELECT_TABLE_METRIC = "SELECT * FROM catalog_db.table_metric;";
;

public PelotonTest() throws SQLException {
Expand Down Expand Up @@ -241,6 +254,135 @@ public void Scan_Test() throws SQLException {
System.out.println("Scan test passed.");
}

/**
* Test SeqScan and IndexScan
*
* @throws SQLException
*/
public void Stat_Test() throws Exception {
conn.setAutoCommit(true);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
int txn_committed = 0;
int table_update = 0;
int table_delete = 0;
int table_insert = 0;
int table_read = 0;
int index_read = 0;
int index_insert = 0;

// Test Insert
stmt.execute(INSERT_A_1);
txn_committed++;
table_insert++;
index_insert++;
// Wait for stats to be aggregated
Thread.sleep(STAT_INTERVAL_MS * 2);
// Check query metric
stmt.execute(SELECT_QUERY_METRIC);
ResultSet rs = stmt.getResultSet();
rs.last();
int nums[] = new int[4];
// Read
nums[0] = rs.getInt(3);
// Update
nums[1] = rs.getInt(4);
// Delete
nums[2] = rs.getInt(5);
// Insert
nums[3] = rs.getInt(6);
// Query
String query = rs.getString(1);
String row1 = query + "\tread:" + nums[0] + "\tupdate:" + nums[1]
+ "\tdelete:" + nums[2] + "\tinsert:" + nums[3];
System.out.println(row1);
if (nums[3] != table_insert) {
throw new SQLException("Query insert doesn't match!");
}

// Test Update
UpdateByIndex(1);
txn_committed++;
table_update++;
table_read++;
// TODO this test doesn't examine index since update by index scan
// is not supported yet
//index_read++;

stmt.execute(INDEXSCAN);
table_read++;
txn_committed++;
index_read++;

// TODO this test doesn't examine index since delete by index
// scan is not supported
stmt.execute(DELETE_A);
table_read++;
table_delete++;
txn_committed++;
// index_read++;

// Wait for stats to be aggregated
Thread.sleep(STAT_INTERVAL_MS * 2);
// Check index metric
stmt.execute(SELECT_INDEX_METRIC);
rs = stmt.getResultSet();
rs.last();
nums = new int[3];
// Read
nums[0] = rs.getInt(4);
// Insert
nums[2] = rs.getInt(6);
if (nums[0] != index_read) {
throw new SQLException("Index read doesn't match: " + nums[0]);
}
if (nums[2] != index_insert) {
throw new SQLException("Index insert doesn't match: " + nums[2]);
}


// Check table metric
stmt.execute(SELECT_TABLE_METRIC);
rs = stmt.getResultSet();
rs.last();
nums = new int[4];
// Read
nums[0] = rs.getInt(3);
// Update
nums[1] = rs.getInt(4);
// Delete
nums[2] = rs.getInt(5);
// Insert
nums[3] = rs.getInt(6);
if (nums[1] != table_update) {
throw new SQLException("Table update doesn't match: " + nums[1]);
}
if (nums[2] != table_delete) {
throw new SQLException("Table delete doesn't match: " + nums[2]);
}
if (nums[3] != table_insert) {
throw new SQLException("Table insert doesn't match: " + nums[3]);
}
if (nums[0] != table_read) {
throw new SQLException("Table read doesn't match: " + nums[0]);
}

// Check database metric
stmt.execute(SELECT_DB_METRIC);
rs = stmt.getResultSet();
rs.last();
nums = new int[2];
// Commit
nums[0] = rs.getInt(2);
// Abort
nums[1] = rs.getInt(3);
if (nums[0] < txn_committed) {
throw new SQLException("Txn committed count doesn't match!");
}

System.out.println("Stat test all good!");

}

/**
* Insert a record
*
Expand Down Expand Up @@ -506,6 +648,15 @@ public void SelectParam() throws SQLException {
}

static public void main(String[] args) throws Exception {
if (args.length == 0 || args[0].equals("basic")) {
BasicTest();
} else if (args[0].equals("stats")) {
StatsTest();
}
}

static public void BasicTest() throws Exception {
System.out.println("Basic Tests");
PelotonTest pt = new PelotonTest();
pt.Init();
pt.ShowTable();
Expand All @@ -517,4 +668,13 @@ static public void main(String[] args) throws Exception {
pt.Batch_Delete();
pt.Close();
}

static public void StatsTest() throws Exception {
System.out.println("Stats Tests");
PelotonTest pt = new PelotonTest();
pt.Init();
pt.Stat_Test();
pt.Close();
}

}
2 changes: 1 addition & 1 deletion script/testing/jdbc/test_jdbc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ mkdir -p out
wget -nc -P lib https://jdbc.postgresql.org/download/postgresql-9.4.1209.jre6.jar
javac -classpath ./lib/postgresql-9.4.1209.jre6.jar -d out src/PelotonTest.java
jar -cvf out.jar -C out .
java -cp out.jar:./lib/postgresql-9.4.1209.jre6.jar PelotonTest
java -cp out.jar:./lib/postgresql-9.4.1209.jre6.jar PelotonTest $1
23 changes: 20 additions & 3 deletions script/validators/jdbc_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@

if __name__ == '__main__':
# Launch Peloton
peloton = subprocess.Popen(['exec ' + PELOTON_BIN], shell=True)
peloton = subprocess.Popen(['exec ' + PELOTON_BIN + ' -port 54321'], shell=True)

# start jdbc test
os.chdir(PELOTON_JDBC_SCRIPT_DIR)
jdbc = subprocess.Popen(['/bin/bash test_jdbc.sh'], shell=True, stdout=subprocess.PIPE,
jdbc = subprocess.Popen(['/bin/bash test_jdbc.sh basic'], shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
jdbc_out = ''.join(jdbc.stdout.readlines())
print jdbc_out
Expand All @@ -46,4 +46,21 @@
# any mention of the word 'exception'? Fail validator
if re.search('exception', jdbc_out, re.IGNORECASE):
sys.exit(EXIT_FAILURE)
sys.exit(EXIT_SUCCESS)

# Launch Peloton with Stat Option
peloton = subprocess.Popen(['exec ' + PELOTON_BIN + ' -port 54321 -stats_mode 1'], shell=True)

# start jdbc test
os.chdir(PELOTON_JDBC_SCRIPT_DIR)
jdbc = subprocess.Popen(['/bin/bash test_jdbc.sh stats'], shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
jdbc_out = ''.join(jdbc.stdout.readlines())
print jdbc_out

# kill peloton
peloton.kill()

# any mention of the word 'exception'? Fail validator
if re.search('exception', jdbc_out, re.IGNORECASE):
sys.exit(EXIT_FAILURE)
sys.exit(EXIT_SUCCESS)
19 changes: 8 additions & 11 deletions src/catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ Catalog *Catalog::GetInstance(void) {

Catalog::Catalog() {
CreateCatalogDatabase();
auto result = CreateDatabase(DEFAULT_DB_NAME, nullptr);
if (result == RESULT_SUCCESS) {
// create metrics table in default database
CreateMetricsCatalog();
}

// Create metrics table in default database
CreateMetricsCatalog();
}

void Catalog::CreateMetricsCatalog() {
auto default_db = GetDatabaseWithName(DEFAULT_DB_NAME);
auto default_db = GetDatabaseWithName(CATALOG_DATABASE_NAME);
auto default_db_oid = default_db->GetOid();

// Create table for database metrics
Expand Down Expand Up @@ -554,19 +552,19 @@ std::unique_ptr<catalog::Schema> Catalog::InitializeDatabaseMetricsSchema() {
auto id_column =
catalog::Column(integer_type, integer_type_size, "database_id", true);
id_column.AddConstraint(not_null_constraint);
auto txn_aborted_column =
catalog::Column(integer_type, integer_type_size, "txn_aborted", true);
txn_aborted_column.AddConstraint(not_null_constraint);
auto txn_committed_column =
catalog::Column(integer_type, integer_type_size, "txn_committed", true);
txn_committed_column.AddConstraint(not_null_constraint);
auto txn_aborted_column =
catalog::Column(integer_type, integer_type_size, "txn_aborted", true);
txn_aborted_column.AddConstraint(not_null_constraint);

auto timestamp_column =
catalog::Column(integer_type, integer_type_size, "time_stamp", true);
timestamp_column.AddConstraint(not_null_constraint);

std::unique_ptr<catalog::Schema> database_schema(new catalog::Schema(
{id_column, txn_aborted_column, txn_committed_column, timestamp_column}));
{id_column, txn_committed_column, txn_aborted_column, timestamp_column}));
return database_schema;
}

Expand Down Expand Up @@ -697,7 +695,6 @@ oid_t Catalog::GetDatabaseCount() { return databases_.size(); }
oid_t Catalog::GetNextOid() { return oid_++; }

Catalog::~Catalog() {
delete GetDatabaseWithName(DEFAULT_DB_NAME);
delete GetDatabaseWithName(CATALOG_DATABASE_NAME);

delete pool_;
Expand Down
8 changes: 5 additions & 3 deletions src/container/cuckoo_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

namespace peloton {

namespace storage{
namespace storage {
class TileGroup;
}

namespace stats {
class BackendStatsContext;
class IndexMetric;
}

CUCKOO_MAP_TEMPLATE_ARGUMENTS
Expand Down Expand Up @@ -60,8 +61,7 @@ bool CUCKOO_MAP_TYPE::Erase(const KeyType &key){
}

CUCKOO_MAP_TEMPLATE_ARGUMENTS
bool CUCKOO_MAP_TYPE::Find(const KeyType &key,
ValueType& value){
bool CUCKOO_MAP_TYPE::Find(const KeyType &key, ValueType &value) const {

auto status = cuckoo_map.find(key, value);
LOG_TRACE("find status : %d", status);
Expand Down Expand Up @@ -98,4 +98,6 @@ template class CuckooMap<oid_t, std::shared_ptr<oid_t>>;
template class CuckooMap<std::thread::id,
std::shared_ptr<stats::BackendStatsContext>>;

template class CuckooMap<oid_t, std::shared_ptr<stats::IndexMetric>>;

} // End peloton namespace
3 changes: 2 additions & 1 deletion src/executor/create_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ bool CreateExecutor::DExecute() {
// Check if query was for creating table
if (node.GetCreateType() == CreateType::CREATE_TYPE_TABLE) {
std::string table_name = node.GetTableName();
auto database_name = node.GetDatabaseName();
std::unique_ptr<catalog::Schema> schema(node.GetSchema());

Result result = catalog::Catalog::GetInstance()->CreateTable(
DEFAULT_DB_NAME, table_name, std::move(schema), current_txn);
database_name, table_name, std::move(schema), current_txn);
current_txn->SetResult(result);

if (current_txn->GetResult() == Result::RESULT_SUCCESS) {
Expand Down
1 change: 1 addition & 0 deletions src/include/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ enum ExpressionType {
EXPRESSION_TYPE_PLACEHOLDER = 701,
EXPRESSION_TYPE_COLUMN_REF = 702,
EXPRESSION_TYPE_FUNCTION_REF = 703,
EXPRESSION_TYPE_TABLE_REF = 704,

//===--------------------------------------------------------------------===//
// Misc
Expand Down
2 changes: 1 addition & 1 deletion src/include/container/cuckoo_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CuckooMap {
bool Update(const KeyType &key, ValueType value);

// Extracts the corresponding value
bool Find(const KeyType &key, ValueType& value);
bool Find(const KeyType &key, ValueType &value) const;

// Delete key from the cuckoo_map
bool Erase(const KeyType &key);
Expand Down
1 change: 1 addition & 0 deletions src/include/expression/abstract_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class AbstractExpression : public Printable {
char *name = nullptr;
char *column = nullptr;
char *alias = nullptr;
char *database = nullptr;

bool distinct = false;

Expand Down
Loading

0 comments on commit fb6856a

Please sign in to comment.