Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe VIEW for YDB CLI #9513

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ydb/core/driver_lib/run/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
#include <ydb/services/ydb/ydb_table.h>
#include <ydb/services/ydb/ydb_object_storage.h>
#include <ydb/services/tablet/ydb_tablet.h>
#include <ydb/services/view/grpc_service.h>

#include <ydb/core/fq/libs/init/init.h>

Expand Down Expand Up @@ -610,6 +611,8 @@ void TKikimrRunner::InitializeGRpc(const TKikimrRunConfig& runConfig) {
names["replication"] = &hasReplication;
TServiceCfg hasTabletService = services.empty();
names["tablet_service"] = &hasTabletService;
TServiceCfg hasView = services.empty();
names["view"] = &hasView;

std::unordered_set<TString> enabled;
for (const auto& name : services) {
Expand Down Expand Up @@ -895,6 +898,11 @@ void TKikimrRunner::InitializeGRpc(const TKikimrRunConfig& runConfig) {
hasTabletService.IsRlAllowed(), grpcConfig.GetHandlersPerCompletionQueue()));
}

if (hasView) {
server.AddService(new NGRpcService::TGRpcViewService(ActorSystem.Get(), Counters,
grpcRequestProxies[0], hasView.IsRlAllowed()));
}

if (ModuleFactories) {
for (const auto& service : ModuleFactories->GrpcServiceFactory.Create(enabled, disabled, ActorSystem.Get(), Counters, grpcRequestProxies[0])) {
server.AddService(service);
Expand Down
1 change: 1 addition & 0 deletions ydb/core/driver_lib/run/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ PEERDIR(
ydb/services/rate_limiter
ydb/services/replication
ydb/services/tablet
ydb/services/view
ydb/services/ydb
)

Expand Down
94 changes: 94 additions & 0 deletions ydb/core/grpc_services/rpc_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "rpc_scheme_base.h"
#include "service_view.h"

#include <ydb/core/grpc_services/base/base.h>
#include <ydb/core/tx/schemeshard/schemeshard.h>
#include <ydb/core/ydb_convert/ydb_convert.h>
#include <ydb/library/actors/core/actor.h>
#include <ydb/library/actors/core/hfunc.h>
#include <ydb/public/api/protos/draft/ydb_view.pb.h>

namespace NKikimr::NGRpcService {

using namespace Ydb;

using TEvDescribeView = TGrpcRequestOperationCall<View::DescribeViewRequest, View::DescribeViewResponse>;

class TDescribeViewRPC : public TRpcSchemeRequestActor<TDescribeViewRPC, TEvDescribeView> {
using TBase = TRpcSchemeRequestActor<TDescribeViewRPC, TEvDescribeView>;

public:
using TBase::TBase;

void Bootstrap() {
DescribeScheme();
}

void PassAway() override {
TBase::PassAway();
}

private:
void DescribeScheme() {
auto ev = std::make_unique<TEvTxUserProxy::TEvNavigate>();
SetAuthToken(ev, *Request_);
SetDatabase(ev.get(), *Request_);
ev->Record.MutableDescribePath()->SetPath(GetProtoRequest()->path());

Send(MakeTxProxyID(), ev.release());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you are wondering, all describe requests sent to tx proxy are going through both:

  • scheme cache (with sync version flag) to retrieve the path's SchemeShard id
  • SchemeShard to retrieve the most up-to-date info about the path

Become(&TDescribeViewRPC::StateDescribeScheme);
}

STATEFN(StateDescribeScheme) {
switch (ev->GetTypeRewrite()) {
HFunc(NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult, Handle);
default:
return TBase::StateWork(ev);
}
}

void Handle(NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult::TPtr& ev, const TActorContext& ctx) {
const auto& record = ev->Get()->GetRecord();
const auto& desc = record.GetPathDescription();

if (record.HasReason()) {
Request_->RaiseIssue(NYql::TIssue(record.GetReason()));
}

switch (record.GetStatus()) {
case NKikimrScheme::StatusSuccess:
if (desc.GetSelf().GetPathType() != NKikimrSchemeOp::EPathTypeView) {
auto message = TStringBuilder() << "Expected a view, but got: " << desc.GetSelf().GetPathType();
Request_->RaiseIssue(NYql::TIssue(message));
return Reply(StatusIds::SCHEME_ERROR, ctx);
}

ConvertDirectoryEntry(desc.GetSelf(), Result_.mutable_self(), true);
Result_.set_query_text(desc.GetViewDescription().GetQueryText());

return ReplyWithResult(StatusIds::SUCCESS, Result_, ctx);

case NKikimrScheme::StatusPathDoesNotExist:
case NKikimrScheme::StatusSchemeError:
return Reply(StatusIds::SCHEME_ERROR, ctx);

case NKikimrScheme::StatusAccessDenied:
return Reply(StatusIds::UNAUTHORIZED, ctx);

case NKikimrScheme::StatusNotAvailable:
return Reply(StatusIds::UNAVAILABLE, ctx);

default:
return Reply(StatusIds::GENERIC_ERROR, ctx);
}
}

private:
View::DescribeViewResult Result_;
};

void DoDescribeView(std::unique_ptr<IRequestOpCtx> p, const IFacilityProvider& f) {
f.RegisterActor(new TDescribeViewRPC(p.release()));
}

}
12 changes: 12 additions & 0 deletions ydb/core/grpc_services/service_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <memory>

namespace NKikimr::NGRpcService {

class IRequestOpCtx;
class IFacilityProvider;

void DoDescribeView(std::unique_ptr<IRequestOpCtx> p, const IFacilityProvider& f);

}
1 change: 1 addition & 0 deletions ydb/core/grpc_services/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ SRCS(
rpc_stream_execute_yql_script.cpp
rpc_whoami.cpp
rpc_object_storage.cpp
rpc_view.cpp
table_settings.cpp

rpc_common/rpc_common_kqp_session.cpp
Expand Down
1 change: 1 addition & 0 deletions ydb/public/api/grpc/draft/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SRCS(
ydb_object_storage_v1.proto
ydb_replication_v1.proto
ydb_tablet_v1.proto
ydb_view_v1.proto
ydb_ymq_v1.proto
)

Expand Down
10 changes: 10 additions & 0 deletions ydb/public/api/grpc/draft/ydb_view_v1.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package Ydb.View.V1;
option java_package = "com.yandex.ydb.view.v1";

import "ydb/public/api/protos/draft/ydb_view.proto";

service ViewService {
rpc DescribeView(View.DescribeViewRequest) returns (View.DescribeViewResponse);
}
28 changes: 28 additions & 0 deletions ydb/public/api/protos/draft/ydb_view.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";
option cc_enable_arenas = true;

package Ydb.View;
option java_package = "com.yandex.ydb.view";

import "ydb/public/api/protos/annotations/validation.proto";
import "ydb/public/api/protos/ydb_operation.proto";
import "ydb/public/api/protos/ydb_scheme.proto";

message DescribeViewRequest {
Ydb.Operations.OperationParams operation_params = 1;
// The path to the view.
string path = 2 [(required) = true];
}

message DescribeViewResponse {
// The result of the request will be inside the operation proto.
Ydb.Operations.Operation operation = 1;
}

message DescribeViewResult {
// Description of a generic scheme object.
Ydb.Scheme.Entry self = 1;

// View-specific fields.
string query_text = 2;
}
1 change: 1 addition & 0 deletions ydb/public/api/protos/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SRCS(
draft/ydb_object_storage.proto
draft/ydb_replication.proto
draft/ydb_tablet.proto
draft/ydb_view.proto
draft/ymq.proto
draft/field_transformation.proto
ydb_federation_discovery.proto
Expand Down
15 changes: 15 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_service_scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ int TCommandDescribe::PrintPathResponse(TDriver& driver, const NScheme::TDescrib
return DescribeCoordinationNode(driver);
case NScheme::ESchemeEntryType::Replication:
return DescribeReplication(driver);
case NScheme::ESchemeEntryType::View:
return DescribeView(driver);
default:
return DescribeEntryDefault(entry);
}
Expand Down Expand Up @@ -579,6 +581,19 @@ int TCommandDescribe::DescribeReplication(const TDriver& driver) {
return PrintDescription(this, OutputFormat, result, &TCommandDescribe::PrintReplicationResponsePretty);
}

int TCommandDescribe::PrintViewResponsePretty(const NYdb::NView::TDescribeViewResult& result) const {
Cout << "\nQuery text:\n" << result.GetViewDescription().GetQueryText() << Endl;
return EXIT_SUCCESS;
}

int TCommandDescribe::DescribeView(const TDriver& driver) {
NView::TViewClient client(driver);
auto result = client.DescribeView(Path, {}).ExtractValueSync();
ThrowOnError(result);

return PrintDescription(this, OutputFormat, result, &TCommandDescribe::PrintViewResponsePretty);
}

namespace {
void PrintColumns(const NTable::TTableDescription& tableDescription) {
if (!tableDescription.GetTableColumns().size()) {
Expand Down
4 changes: 4 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_service_scheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ydb/public/lib/ydb_cli/common/print_utils.h>
#include <ydb/public/lib/ydb_cli/common/recursive_remove.h>
#include <ydb/public/sdk/cpp/client/draft/ydb_replication.h>
#include <ydb/public/sdk/cpp/client/draft/ydb_view.h>
#include <ydb/public/sdk/cpp/client/ydb_coordination/coordination.h>
#include <ydb/public/sdk/cpp/client/ydb_proto/accessor.h>
#include <ydb/public/sdk/cpp/client/ydb_scheme/scheme.h>
Expand Down Expand Up @@ -100,6 +101,9 @@ class TCommandDescribe : public TYdbOperationCommand, public TCommandWithPath, p
int DescribeReplication(const TDriver& driver);
int PrintReplicationResponsePretty(const NYdb::NReplication::TDescribeReplicationResult& result) const;

int DescribeView(const TDriver& driver);
int PrintViewResponsePretty(const NYdb::NView::TDescribeViewResult& result) const;

int TryTopicConsumerDescribeOrFail(NYdb::TDriver& driver, const NScheme::TDescribePathResult& result);
std::pair<TString, TString> ParseTopicConsumer() const;
int PrintConsumerResponsePretty(const NYdb::NTopic::TConsumerDescription& description) const;
Expand Down
16 changes: 16 additions & 0 deletions ydb/public/sdk/cpp/client/draft/ut/helpers/grpc_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>

namespace NYdb {

template<class TService>
std::unique_ptr<grpc::Server> StartGrpcServer(const TString& address, TService& service) {
grpc::ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
return builder.BuildAndStart();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "scripting.h"

namespace NYdb::NScripting {

grpc::Status TMockSlyDbProxy::ExecuteYql(
grpc::ServerContext* context,
const Ydb::Scripting::ExecuteYqlRequest* request,
Ydb::Scripting::ExecuteYqlResponse* response
) {
context->AddInitialMetadata("key", "value");
Y_UNUSED(request);

// Just to make sdk core happy
auto* op = response->mutable_operation();
op->set_ready(true);
op->set_status(Ydb::StatusIds::SUCCESS);
op->mutable_result();

return grpc::Status::OK;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <ydb/public/api/grpc/ydb_scripting_v1.grpc.pb.h>

namespace NYdb::NScripting {

class TMockSlyDbProxy : public Ydb::Scripting::V1::ScriptingService::Service
{
public:
grpc::Status ExecuteYql(
grpc::ServerContext* context,
const Ydb::Scripting::ExecuteYqlRequest* request,
Ydb::Scripting::ExecuteYqlResponse* response) override;
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "view.h"

namespace NYdb::NView {

grpc::Status TViewDummyService::DescribeView(
grpc::ServerContext* context,
const Ydb::View::DescribeViewRequest* request,
Ydb::View::DescribeViewResponse* response
) {
Y_UNUSED(context);
Y_UNUSED(request);

auto* op = response->mutable_operation();
op->set_ready(true);
op->set_status(Ydb::StatusIds::SUCCESS);

Ydb::View::DescribeViewResult describeResult;
describeResult.set_query_text(DummyQueryText);
op->mutable_result()->PackFrom(describeResult);

return grpc::Status::OK;
}

}
18 changes: 18 additions & 0 deletions ydb/public/sdk/cpp/client/draft/ut/helpers/grpc_services/view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <ydb/public/api/grpc/draft/ydb_view_v1.grpc.pb.h>

namespace NYdb::NView {

constexpr const char* DummyQueryText = "select 42";

class TViewDummyService : public Ydb::View::V1::ViewService::Service
{
public:
grpc::Status DescribeView(
grpc::ServerContext* context,
const Ydb::View::DescribeViewRequest* request,
Ydb::View::DescribeViewResponse* response) override;
};

}
13 changes: 13 additions & 0 deletions ydb/public/sdk/cpp/client/draft/ut/helpers/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LIBRARY()

PEERDIR(
ydb/public/api/grpc
ydb/public/api/grpc/draft
)

SRCS(
grpc_services/scripting.cpp
grpc_services/view.cpp
)

END()
5 changes: 5 additions & 0 deletions ydb/public/sdk/cpp/client/draft/ut/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ ENDIF()

FORK_SUBTESTS()

PEERDIR(
ydb/public/sdk/cpp/client/draft/ut/helpers
)

SRCS(
ydb_scripting_response_headers_ut.cpp
ydb_view_ut.cpp
)

END()
Loading
Loading