feat: create table through GRPC interface (#224)

* feat: create table through GRPC interface

* move `CreateExpr` `oneof` expr of `AdminExpr` in `admin.proto`, and implement the admin GRPC interface

* add `table_options` and `partition_options` to `CreateExpr`

* resolve code review comments

Co-authored-by: luofucong <luofucong@greptime.com>
This commit is contained in:
LFC
2022-09-06 12:51:07 +08:00
committed by GitHub
parent 3f9144a2e3
commit 119ff2fc2e
26 changed files with 605 additions and 77 deletions

View File

@@ -12,21 +12,25 @@ use tonic::{Request, Response, Status};
use crate::error::{Result, StartGrpcSnafu, TcpBindSnafu};
use crate::grpc::handler::BatchHandler;
use crate::query_handler::GrpcQueryHandlerRef;
use crate::query_handler::{GrpcAdminHandlerRef, GrpcQueryHandlerRef};
use crate::server::Server;
pub struct GrpcServer {
query_handler: GrpcQueryHandlerRef,
admin_handler: GrpcAdminHandlerRef,
}
impl GrpcServer {
pub fn new(query_handler: GrpcQueryHandlerRef) -> Self {
Self { query_handler }
pub fn new(query_handler: GrpcQueryHandlerRef, admin_handler: GrpcAdminHandlerRef) -> Self {
Self {
query_handler,
admin_handler,
}
}
pub fn create_service(&self) -> greptime_server::GreptimeServer<GrpcService> {
let service = GrpcService {
handler: BatchHandler::new(self.query_handler.clone()),
handler: BatchHandler::new(self.query_handler.clone(), self.admin_handler.clone()),
};
greptime_server::GreptimeServer::new(service)
}

View File

@@ -1,22 +1,35 @@
use api::v1::{BatchRequest, BatchResponse, DatabaseResponse};
use api::v1::{AdminResponse, BatchRequest, BatchResponse, DatabaseResponse};
use crate::error::Result;
use crate::query_handler::GrpcQueryHandlerRef;
use crate::query_handler::{GrpcAdminHandlerRef, GrpcQueryHandlerRef};
#[derive(Clone)]
pub struct BatchHandler {
query_handler: GrpcQueryHandlerRef,
admin_handler: GrpcAdminHandlerRef,
}
impl BatchHandler {
pub fn new(query_handler: GrpcQueryHandlerRef) -> Self {
Self { query_handler }
pub fn new(query_handler: GrpcQueryHandlerRef, admin_handler: GrpcAdminHandlerRef) -> Self {
Self {
query_handler,
admin_handler,
}
}
pub async fn batch(&self, batch_req: BatchRequest) -> Result<BatchResponse> {
let mut batch_resp = BatchResponse::default();
let mut admin_resp = AdminResponse::default();
let mut db_resp = DatabaseResponse::default();
for admin_req in batch_req.admins {
for admin_expr in admin_req.exprs {
let admin_result = self.admin_handler.exec_admin_request(admin_expr).await?;
admin_resp.results.push(admin_result);
}
}
batch_resp.admins.push(admin_resp);
for db_req in batch_req.databases {
for obj_expr in db_req.exprs {
let object_resp = self.query_handler.do_query(obj_expr).await?;

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use api::v1::{ObjectExpr, ObjectResult};
use api::v1::{AdminExpr, AdminResult, ObjectExpr, ObjectResult};
use async_trait::async_trait;
use query::Output;
@@ -18,6 +18,7 @@ use crate::error::Result;
pub type SqlQueryHandlerRef = Arc<dyn SqlQueryHandler + Send + Sync>;
pub type GrpcQueryHandlerRef = Arc<dyn GrpcQueryHandler + Send + Sync>;
pub type GrpcAdminHandlerRef = Arc<dyn GrpcAdminHandler + Send + Sync>;
#[async_trait]
pub trait SqlQueryHandler {
@@ -29,3 +30,8 @@ pub trait SqlQueryHandler {
pub trait GrpcQueryHandler {
async fn do_query(&self, query: ObjectExpr) -> Result<ObjectResult>;
}
#[async_trait]
pub trait GrpcAdminHandler {
async fn exec_admin_request(&self, expr: AdminExpr) -> Result<AdminResult>;
}