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

Feature/soci #1505

Merged
merged 27 commits into from
Jul 13, 2018
Merged

Feature/soci #1505

merged 27 commits into from
Jul 13, 2018

Conversation

vdrobnyi
Copy link
Contributor

@vdrobnyi vdrobnyi commented Jun 28, 2018

Description of the Change

SOCI instead of pqxx.

Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
@l4l l4l added needs-correction pr/rfc is not completed and might be updated ametsuchi labels Jun 28, 2018
vdrobnyi and others added 5 commits June 29, 2018 09:26
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
* Initial SOCI integration to CMake build

* Add download step for SOCI

* Replace pqxx with soci in dockerfiles

* Enable packaging for SOCI

* Fix new directory path when downloading

* Fix pg_config and include not found by SOCI when pq not installed

* Fix docs, snapcraft; remove pqxx include

* Remove extra ; and add missing link library

Signed-off-by: Andrei Lebedev <[email protected]>
@vdrobnyi vdrobnyi changed the title Feature/soci WIP Feature/soci Jul 2, 2018
+ transaction_.quote(command.assetId()) + ", "
+ transaction_.quote(index) + ");");
status &= res != boost::none;
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to put all SQL queries into a separate function to avoid try-catch on any query

@@ -68,18 +65,19 @@ namespace iroha {
auto ids = {account_id,
command.srcAccountId(),
command.destAccountId()};
auto asset_id = command.assetId();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use reference?

sql_ << "SELECT height FROM height_by_hash WHERE hash = :hash",
soci::into(block_str), soci::use(hash_str);
if (block_str) {
blockId = std::stoull(block_str.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use row::get to retrieve?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails. Height is stored as a string in postgres. Maybe it's better to store it as an integer?

Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
st.exchange(soci::use(account_id, "account_id"));
st.exchange(soci::use(perm_str, "perm"));

auto msg =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make that into a lambda. We do not want to instantiate error message in the successful path.

st.exchange(soci::use(account_id, "account_id"));
st.exchange(soci::use(perm_str, "perm"));

auto msg =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make that into a lambda so it is evaluated only when we fail to perform the query.

PostgresWsvQuery(std::unique_ptr<pqxx::lazyconnection> connection,
std::unique_ptr<pqxx::nontransaction> transaction);
explicit PostgresWsvQuery(soci::session &sql);
PostgresWsvQuery(std::unique_ptr<soci::session> sql_ptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it explicit since it accepts single parameter now

Signed-off-by: Victor Drobny <[email protected]>
.template as<shared_model::interface::types::HeightType>();
});
};
std::vector<shared_model::interface::types::HeightType> result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sequence of statements is repeated when you select several rows from the database, maybe refactor it to a separate function?

PostgresBlockQuery(std::unique_ptr<pqxx::lazyconnection> connection,
std::unique_ptr<pqxx::nontransaction> transaction,
PostgresBlockQuery(soci::session &sql, KeyValueStorage &file_store);
PostgresBlockQuery(std::unique_ptr<soci::session> sql_ptr,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make explicit

return (boost::format("failed to insert role permissions, role "
"id: '%s', permissions: [%s]")
% role_id % perm_debug_str)
% role_id % perm_str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to print perm_debug_str here

* @param transform_func - function which transforms result row to T
* @return vector of target type
*/
template <typename T, typename Operator>
std::vector<T> transform(const pqxx::result &result,
std::vector<T> transform(const soci::rowset<soci::row> &result,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move these functions out of command executor

}

template <typename T, typename Operator>
std::vector<T> transform(const std::vector<soci::row> &result,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this function is supposed to do?

@@ -36,6 +36,8 @@ pghost=127.0.0.1

pgid=$(docker run -p $pghost:$pgport:$pgport --rm --name $pgname -e POSTGRES_USER=$user -e POSTGRES_PASSSWORD=$password -d postgres:9.5)

docker run -p 127.0.0.1:5432:5432 --rm --name postgrestestimage -e POSTGRES_USER=postgres -e POSTGRES_PASSSWORD=mysecretpassword -d postgres:9.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be here?

l4l
l4l previously requested changes Jul 4, 2018
Copy link
Contributor

@l4l l4l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the operator , isn't used for statements? E.g
sql << "SELECT * FROM table", into(val);

+ transaction_.quote(command.assetId()) + ", "
+ transaction_.quote(index) + ");");
status &= res != boost::none;
soci::statement st =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn' the = operator have the lowest priority? I mean the braces around prepare seems redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, comma has lower priority

@@ -94,26 +106,31 @@ namespace iroha {
block.transactions() | boost::adaptors::indexed(0),
[&](const auto &tx) {
const auto &creator_id = tx.value().creatorAccountId();
const auto &hash = tx.value().hash().blob();
const auto &hash = tx.value().hash().hex();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using soci::blob instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blob in postgres is designed for large files. I'm not sure that it is necessary here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That fixes changes schema and should be considered (if necessary) in other pr at least. It better to minimize the fixes here

: transaction_(transaction),
log_(logger::log("PostgresBlockIndex")),
execute_{makeExecuteOptional(transaction_, log_)} {}
bool executeSOCI(soci::statement &st) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

executeSoci please

@@ -106,43 +80,81 @@ namespace iroha {
}
}

template <typename Function, typename ParamType>
void processSOCI(soci::statement &st,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, processSoci

*sql_ << "CREATE TABLE IF NOT EXISTS ordering_service_state "
"(proposal_height bigserial)";
*sql_ << "INSERT INTO ordering_service_state VALUES (2)";
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about false case? Same for the following

.build();
});
}

static inline shared_model::builder::BuilderResult<
shared_model::interface::AccountAsset>
makeAccountAsset(const pqxx::row &row) noexcept {
makeAccountAsset(const soci::row &row) noexcept {
Copy link
Contributor

@l4l l4l Jul 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better change the signature as well: pass 3 strings
Or maybe all similar function can use soci::row
Anyway that inconsistency should be resolved

processSOCI(st, ind, row, [&assets, &account_id](soci::row &row) {
auto result = fromResult(makeAccountAsset(
account_id, row.get<std::string>(1), row.get<std::string>(2)));
if (result) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

monadic bind?

soci::into(size), soci::use(name);

if (size == 0) {
std::string query = "CREATE DATABASE ";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CREATE DATABASE :dbname?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason it does not work

@@ -57,6 +57,5 @@ target_link_libraries(postgres_options_test

add_library(ametsuchi_fixture INTERFACE)
target_link_libraries(ametsuchi_fixture INTERFACE
pqxx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems soci still need to be here

@@ -20,9 +20,10 @@

#include "ametsuchi/storage.hpp"

#include <soci/postgresql/soci-postgresql.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same

Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
sql_ << "SELECT height FROM height_by_hash WHERE hash = :hash",
soci::into(block_str), soci::use(hash_str);
if (block_str) {
blockId = std::stoull(block_str.get());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails. Height is stored as a string in postgres. Maybe it's better to store it as an integer?

+ transaction_.quote(command.assetId()) + ", "
+ transaction_.quote(index) + ");");
status &= res != boost::none;
soci::statement st =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, comma has lower priority

@@ -94,26 +106,31 @@ namespace iroha {
block.transactions() | boost::adaptors::indexed(0),
[&](const auto &tx) {
const auto &creator_id = tx.value().creatorAccountId();
const auto &hash = tx.value().hash().blob();
const auto &hash = tx.value().hash().hex();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blob in postgres is designed for large files. I'm not sure that it is necessary here.

@@ -151,24 +163,24 @@ namespace iroha {

static inline shared_model::builder::BuilderResult<
shared_model::interface::Peer>
makePeer(const pqxx::row &row) noexcept {
makePeer(const soci::row &row) noexcept {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It simplifies code in getPeers() in wsv query, but it is not possible to use soci::row everywhere here.

soci::into(size), soci::use(name);

if (size == 0) {
std::string query = "CREATE DATABASE ";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason it does not work

Signed-off-by: Victor Drobny <[email protected]>
@lebdron lebdron dismissed stale reviews from l4l and nickaleks July 5, 2018 07:19

Issues addressed.

@lebdron lebdron requested a review from nickaleks July 5, 2018 07:20
Signed-off-by: Victor Drobny <[email protected]>
l4l
l4l previously requested changes Jul 5, 2018
const auto &str =
shared_model::proto::permissions::toString(permissions);
const auto perm_debug_str =
std::accumulate(str.begin(), str.end(), std::string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be barely readable because it doesn't have spaces, e.g would look like "can_add_peercan_create_domaincan_create_user"

st.exchange(soci::use(empty_json));
st.exchange(soci::use(filled_json));
st.exchange(soci::use(value));
st.exchange(soci::use(std::string("{" + creator_account_id + "}")));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator""s might be handy, anyway for now is also ok

return result;
*sql_ <<"SAVEPOINT savepoint_";

return apply_function(tx, *wsv_) | [this, &execute_command,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks unformatted for me

[&cmd_error](
expected::Error<validation::CommandError>
&error) {
cmd_error = error.error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use std::move

return false;
});
*sql_ << "DROP TABLE IF EXISTS ordering_service_state";
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False case again

*sql_ << "DELETE FROM ordering_service_state";
*sql_ << "INSERT INTO ordering_service_state VALUES (:height)",
soci::use(height);
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False case again

Signed-off-by: Victor Drobny <[email protected]>
@vdrobnyi vdrobnyi dismissed l4l’s stale review July 6, 2018 08:29

issues fixed

@@ -17,22 +17,40 @@

#include "ametsuchi/impl/postgres_ordering_service_persistent_state.hpp"

#include <soci/postgresql/soci-postgresql.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be below :(

std::accumulate(str.begin(), str.end(), std::string());
std::string perm_debug_str;
std::for_each(
str.begin(), str.end(), [&perm_debug_str](const auto &elem) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use std::string perm_debug_str = std::acumulate(str.begin(), str.end(), std::string(), [](auto acc, const auto &elem) { return acc + " " + elem; }); instead

@l4l l4l removed the needs-correction pr/rfc is not completed and might be updated label Jul 6, 2018
l4l
l4l previously requested changes Jul 6, 2018
Copy link
Contributor

@l4l l4l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have failing test, please update the branch, maybe it will help

The following tests FAILED:
3 - module_wsv_query_command_test (Failed)
7 - module_kv_storage_test (Failed)
118 - integration_grant_permission_test (Failed)
126 - integration_set_account_detail_test (Failed)

Signed-off-by: Victor Drobny <[email protected]>
Signed-off-by: Victor Drobny <[email protected]>
@vdrobnyi vdrobnyi dismissed l4l’s stale review July 6, 2018 10:30

Issues fixed

Copy link
Contributor

@l4l l4l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Signed-off-by: Andrei Lebedev <[email protected]>
@sorabot
Copy link

sorabot commented Jul 13, 2018

SonarQube analysis reported 7 issues

  1. MINOR postgres_block_index.hpp#L54: Unused private function: 'PostgresBlockIndex::indexAccountAssets' rule
  2. MINOR postgres_block_query.hpp#L78: Unused private function: 'PostgresBlockQuery::getBlockIds' rule
  3. MINOR postgres_block_query.hpp#L86: Unused private function: 'PostgresBlockQuery::getBlockId' rule
  4. MINOR postgres_block_query.hpp#L96: Unused private function: 'PostgresBlockQuery::callback' rule
  5. MINOR ametsuchi_fixture.hpp#L79: The class 'BlockQueryTransferTest' defines member variable with name 'sql' also defined in its parent class 'AmetsuchiTest'. rule
  6. MINOR ametsuchi_fixture.hpp#L79: The class 'WsvQueryCommandTest' defines member variable with name 'sql' also defined in its parent class 'AmetsuchiTest'. rule
  7. MINOR storage_init_test.cpp#L21: Variable 'pg_opt_without_dbname_' is assigned in constructor body. Consider performing initialization in initialization list. rule

@vdrobnyi vdrobnyi merged commit 061a3a2 into develop Jul 13, 2018
@vdrobnyi vdrobnyi deleted the feature/soci branch July 13, 2018 09:52
l4l pushed a commit that referenced this pull request Jul 25, 2018
Pqxx replaced by SOCI. Some bugs are fixed by Andrei

Signed-off-by: Victor Drobny <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Pqxx replaced by SOCI. Some bugs are fixed by Andrei

Signed-off-by: Victor Drobny <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Pqxx replaced by SOCI. Some bugs are fixed by Andrei

Signed-off-by: Victor Drobny <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Pqxx replaced by SOCI. Some bugs are fixed by Andrei

Signed-off-by: Victor Drobny <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Pqxx replaced by SOCI. Some bugs are fixed by Andrei

Signed-off-by: Victor Drobny <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Pqxx replaced by SOCI. Some bugs are fixed by Andrei

Signed-off-by: victordrobny <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants