Skip to content

Commit

Permalink
Refactored operation id and added support of old jwt-cpp (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gazizonoki authored Aug 7, 2024
1 parent d47c399 commit 20f092c
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 178 deletions.
1 change: 0 additions & 1 deletion cmake/protos_public_headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ src/api/protos/ydb_export.pb.h
src/api/protos/ydb_coordination.pb.h
src/api/protos/ydb_status_codes.pb.h
src/api/protos/draft/ydb_replication.pb.h
src/library/operation_id/protos/operation_id.pb.h
src/library/yql/public/issue/protos/issue_severity.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ struct TJwtTokenSourceParams {
class ISigningAlgorithm {
public:
virtual ~ISigningAlgorithm() = default;
#ifdef YDB_SDK_USE_NEW_JWT
virtual std::string sign(const std::string& data, std::error_code& ec) const = 0;
#else
virtual std::string sign(const std::string& data) const = 0;
#endif
virtual std::string name() const = 0;
};

Expand All @@ -48,9 +52,15 @@ struct TJwtTokenSourceParams {
{
}

#ifdef YDB_SDK_USE_NEW_JWT
std::string sign(const std::string& data, std::error_code& ec) const override {
return Alg.sign(data, ec);
}
#else
std::string sign(const std::string& data) const override {
return Alg.sign(data);
}
#endif

std::string name() const override {
return Alg.name();
Expand Down
49 changes: 40 additions & 9 deletions include/ydb-cpp-sdk/library/operation_id/operation_id.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
#pragma once

#include <src/library/operation_id/protos/operation_id.pb.h>

#include <memory>
#include <string>
#include <vector>
#include <unordered_map>

namespace NKikimr {
namespace NOperationId {

class TOperationId {
static constexpr int kEKindMinValue = 0;
static constexpr int kEKindMaxValue = 10;
public:
enum EKind : int {
UNUSED = 0,
OPERATION_DDL = 1,
OPERATION_DML = 2,
SESSION_YQL = 3,
PREPARED_QUERY_ID = 4,
CMS_REQUEST = 5,
EXPORT = 6,
BUILD_INDEX = 7,
IMPORT = 8,
SCRIPT_EXECUTION = 9,
SS_BG_TASKS = 10,
};

struct TData {
std::string Key;
std::string Value;
};

using TDataList = std::vector<std::unique_ptr<TData>>;

TOperationId();
explicit TOperationId(const std::string& string, bool allowEmpty = false);
EKind GetKind() const;
EKind& GetMutableKind();

const TDataList& GetData() const;
TDataList& GetMutableData();

const std::vector<const std::string*>& GetValue(const std::string& key) const;
std::string GetSubKind() const;
const Ydb::TOperationId& GetProto() const;
std::string ToString() const;

private:
Ydb::TOperationId Proto_;
std::unordered_map<std::string, std::vector<const std::string*>> Index_;
bool IsValidKind(int kind);

EKind Kind;
TDataList Data;
std::unordered_map<std::string, std::vector<const std::string*>> Index;
};

std::string ProtoToString(const Ydb::TOperationId& proto);
void AddOptionalValue(Ydb::TOperationId& proto, const std::string& key, const std::string& value);
void AddOptionalValue(Ydb::TOperationId& proto, const std::string& key, const char* value, size_t size);
Ydb::TOperationId::EKind ParseKind(const std::string_view value);
void AddOptionalValue(TOperationId& operarionId, const std::string& key, const std::string& value);
void AddOptionalValue(TOperationId& operarionId, const std::string& key, const char* value, size_t size);
TOperationId::EKind ParseKind(const std::string_view value);

std::string FormatPreparedQueryIdCompat(const std::string& str);
bool DecodePreparedQueryIdCompat(const std::string& in, std::string& out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ _ydb_sdk_add_library(impl-ydb_internal-kqp_session_common)
target_link_libraries(impl-ydb_internal-kqp_session_common PUBLIC
yutil
threading-future
lib-operation_id-protos
client-impl-ydb_endpoints
)

Expand Down
6 changes: 3 additions & 3 deletions src/client/operation/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,21 @@ TOperationClient::TOperationClient(const TDriver& driver, const TCommonClientSet
template <typename TOp>
TFuture<TOp> TOperationClient::Get(const TOperation::TOperationId& id) {
auto request = MakeRequest<GetOperationRequest>();
request.set_id(NKikimr::NOperationId::ProtoToString(id.GetProto()));
request.set_id(id.ToString());

return Impl_->Get<TOp>(std::move(request));
}

TAsyncStatus TOperationClient::Cancel(const TOperation::TOperationId& id) {
auto request = MakeRequest<CancelOperationRequest>();
request.set_id(NKikimr::NOperationId::ProtoToString(id.GetProto()));
request.set_id(id.ToString());

return Impl_->Cancel(std::move(request));
}

TAsyncStatus TOperationClient::Forget(const TOperation::TOperationId& id) {
auto request = MakeRequest<ForgetOperationRequest>();
request.set_id(NKikimr::NOperationId::ProtoToString(id.GetProto()));
request.set_id(id.ToString());

return Impl_->Forget(std::move(request));
}
Expand Down
1 change: 0 additions & 1 deletion src/client/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ target_link_libraries(client-ydb_proto PUBLIC
api-grpc
api-grpc-draft
api-protos
lib-operation_id-protos
client-ydb_params
client-ydb_value
yql-public-issue-protos
Expand Down
2 changes: 1 addition & 1 deletion src/client/query/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class TQueryClient::TImpl: public TClientImplCommon<TQueryClient::TImpl>, public

TAsyncFetchScriptResultsResult FetchScriptResults(const NKikimr::NOperationId::TOperationId& operationId, int64_t resultSetIndex, const TFetchScriptResultsSettings& settings) {
auto request = MakeRequest<Ydb::Query::FetchScriptResultsRequest>();
request.set_operation_id(NKikimr::NOperationId::ProtoToString(operationId.GetProto()));
request.set_operation_id(operationId.ToString());
request.set_result_set_index(resultSetIndex);
return FetchScriptResultsImpl(std::move(request), settings);
}
Expand Down
1 change: 0 additions & 1 deletion src/client/table/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ target_link_libraries(client-ydb_table-impl
threading-future
api-protos
api-grpc
lib-operation_id-protos
client-impl-ydb_endpoints
impl-ydb_internal-session_pool
client-ydb_table-query_stats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ target_link_libraries(client-ydb_types-credentials-oauth2 PUBLIC
client-ydb_types
)

target_compile_definitions(client-ydb_types-credentials-oauth2 PUBLIC
YDB_SDK_USE_NEW_JWT
)

target_sources(client-ydb_types-credentials-oauth2 PRIVATE
credentials.cpp
jwt_token_source.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

namespace NYdb {

#ifdef YDB_SDK_USE_NEW_JWT
using TJwtCppStorage = std::vector<picojson::value>;
#else
using TJwtCppStorage = std::set<std::string>;
#endif

static const std::string TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";

class TJwtTokenSource: public ITokenSource {
Expand Down Expand Up @@ -61,7 +67,7 @@ class TJwtTokenSource: public ITokenSource {
if (Params.Audience_.size() == 1) {
tokenBuilder.set_audience(Params.Audience_[0]);
} else if (Params.Audience_.size() > 1) {
std::vector<picojson::value> aud(Params.Audience_.begin(), Params.Audience_.end());
TJwtCppStorage aud(Params.Audience_.begin(), Params.Audience_.end());
tokenBuilder.set_audience(aud);
}

Expand Down
13 changes: 5 additions & 8 deletions src/library/operation_id/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
add_subdirectory(protos)

_ydb_sdk_add_library(library-operation_id)

target_link_libraries(library-operation_id PUBLIC
yutil
protobuf::libprotobuf
cgiparam
uri
lib-operation_id-protos
target_link_libraries(library-operation_id
PRIVATE
yutil
cgiparam
uri
)

target_sources(library-operation_id PRIVATE
Expand Down
Loading

0 comments on commit 20f092c

Please sign in to comment.