Merge pull request #16773 from yihuazhang/alts_tsi_handshaker_use_pollset_set
Use gRPC thread model (i.e., pollset_set) in ALTS TSI implementationpull/17084/head
commit
5e9c9792b5
28 changed files with 1027 additions and 816 deletions
@ -0,0 +1,73 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2018 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/tsi/alts/handshaker/alts_shared_resource.h" |
||||||
|
|
||||||
|
#include <grpc/support/log.h> |
||||||
|
|
||||||
|
#include "src/core/tsi/alts/handshaker/alts_handshaker_client.h" |
||||||
|
|
||||||
|
static alts_shared_resource_dedicated g_alts_resource_dedicated; |
||||||
|
static alts_shared_resource* g_shared_resources = alts_get_shared_resource(); |
||||||
|
|
||||||
|
alts_shared_resource_dedicated* grpc_alts_get_shared_resource_dedicated(void) { |
||||||
|
return &g_alts_resource_dedicated; |
||||||
|
} |
||||||
|
|
||||||
|
static void thread_worker(void* arg) { |
||||||
|
while (true) { |
||||||
|
grpc_event event = |
||||||
|
grpc_completion_queue_next(g_alts_resource_dedicated.cq, |
||||||
|
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
||||||
|
GPR_ASSERT(event.type != GRPC_QUEUE_TIMEOUT); |
||||||
|
if (event.type == GRPC_QUEUE_SHUTDOWN) { |
||||||
|
break; |
||||||
|
} |
||||||
|
GPR_ASSERT(event.type == GRPC_OP_COMPLETE); |
||||||
|
alts_handshaker_client* client = |
||||||
|
static_cast<alts_handshaker_client*>(event.tag); |
||||||
|
alts_handshaker_client_handle_response(client, event.success); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void grpc_alts_shared_resource_dedicated_init() { |
||||||
|
g_alts_resource_dedicated.cq = nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
void grpc_alts_shared_resource_dedicated_start() { |
||||||
|
g_alts_resource_dedicated.cq = grpc_completion_queue_create_for_next(nullptr); |
||||||
|
g_alts_resource_dedicated.thread = |
||||||
|
grpc_core::Thread("alts_tsi_handshaker", &thread_worker, nullptr); |
||||||
|
g_alts_resource_dedicated.interested_parties = grpc_pollset_set_create(); |
||||||
|
grpc_pollset_set_add_pollset(g_alts_resource_dedicated.interested_parties, |
||||||
|
grpc_cq_pollset(g_alts_resource_dedicated.cq)); |
||||||
|
g_alts_resource_dedicated.thread.Start(); |
||||||
|
} |
||||||
|
|
||||||
|
void grpc_alts_shared_resource_dedicated_shutdown() { |
||||||
|
if (g_alts_resource_dedicated.cq != nullptr) { |
||||||
|
grpc_pollset_set_del_pollset(g_alts_resource_dedicated.interested_parties, |
||||||
|
grpc_cq_pollset(g_alts_resource_dedicated.cq)); |
||||||
|
grpc_completion_queue_shutdown(g_alts_resource_dedicated.cq); |
||||||
|
g_alts_resource_dedicated.thread.Join(); |
||||||
|
grpc_pollset_set_destroy(g_alts_resource_dedicated.interested_parties); |
||||||
|
grpc_completion_queue_destroy(g_alts_resource_dedicated.cq); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,70 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2018 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_SHARED_RESOURCE_H |
||||||
|
#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_SHARED_RESOURCE_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include <grpc/grpc.h> |
||||||
|
#include <grpc/support/sync.h> |
||||||
|
|
||||||
|
#include "src/core/lib/gprpp/thd.h" |
||||||
|
#include "src/core/lib/iomgr/pollset_set.h" |
||||||
|
#include "src/core/lib/surface/completion_queue.h" |
||||||
|
|
||||||
|
/**
|
||||||
|
* Main struct containing ALTS shared resources used when |
||||||
|
* employing the dedicated completion queue and thread. |
||||||
|
*/ |
||||||
|
typedef struct alts_shared_resource_dedicated { |
||||||
|
grpc_core::Thread thread; |
||||||
|
grpc_completion_queue* cq; |
||||||
|
grpc_pollset_set* interested_parties; |
||||||
|
grpc_cq_completion storage; |
||||||
|
} alts_shared_resource_dedicated; |
||||||
|
|
||||||
|
/* This method returns the address of alts_shared_resource_dedicated
|
||||||
|
* object shared by all TSI handshakes. |
||||||
|
*/ |
||||||
|
alts_shared_resource_dedicated* grpc_alts_get_shared_resource_dedicated(void); |
||||||
|
|
||||||
|
/**
|
||||||
|
* This method destroys the alts_shared_resource_dedicated object |
||||||
|
* shared by all TSI handshakes. The applicaiton is responsible for |
||||||
|
* invoking the API before calling grpc_shutdown(). |
||||||
|
*/ |
||||||
|
void grpc_alts_shared_resource_dedicated_shutdown(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* This method initializes the alts_shared_resource_dedicated object |
||||||
|
* shared by all TSI handshakes. The application is responsible for |
||||||
|
* invoking the API after calling grpc_init(); |
||||||
|
*/ |
||||||
|
void grpc_alts_shared_resource_dedicated_init(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* This method populates various fields of the alts_shared_resource_dedicated |
||||||
|
* object shared by all TSI handshakes and start the dedicated thread. |
||||||
|
* The API will be invoked by the caller in a lazy manner. That is, |
||||||
|
* it will get invoked when ALTS TSI handshake occurs for the first time. |
||||||
|
*/ |
||||||
|
void grpc_alts_shared_resource_dedicated_start(); |
||||||
|
|
||||||
|
#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_SHARED_RESOURCE_H \ |
||||||
|
*/ |
@ -1,75 +0,0 @@ |
|||||||
/*
|
|
||||||
* |
|
||||||
* Copyright 2018 gRPC authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
#include <grpc/support/port_platform.h> |
|
||||||
|
|
||||||
#include "src/core/tsi/alts/handshaker/alts_tsi_event.h" |
|
||||||
|
|
||||||
#include <grpc/grpc.h> |
|
||||||
#include <grpc/support/alloc.h> |
|
||||||
#include <grpc/support/log.h> |
|
||||||
|
|
||||||
#include "src/core/lib/slice/slice_internal.h" |
|
||||||
|
|
||||||
tsi_result alts_tsi_event_create(alts_tsi_handshaker* handshaker, |
|
||||||
tsi_handshaker_on_next_done_cb cb, |
|
||||||
void* user_data, |
|
||||||
grpc_alts_credentials_options* options, |
|
||||||
grpc_slice target_name, |
|
||||||
alts_tsi_event** event) { |
|
||||||
if (event == nullptr || handshaker == nullptr || cb == nullptr) { |
|
||||||
gpr_log(GPR_ERROR, "Invalid arguments to alts_tsi_event_create()"); |
|
||||||
return TSI_INVALID_ARGUMENT; |
|
||||||
} |
|
||||||
alts_tsi_event* e = static_cast<alts_tsi_event*>(gpr_zalloc(sizeof(*e))); |
|
||||||
e->handshaker = handshaker; |
|
||||||
e->cb = cb; |
|
||||||
e->user_data = user_data; |
|
||||||
e->options = grpc_alts_credentials_options_copy(options); |
|
||||||
e->target_name = grpc_slice_copy(target_name); |
|
||||||
grpc_metadata_array_init(&e->initial_metadata); |
|
||||||
grpc_metadata_array_init(&e->trailing_metadata); |
|
||||||
*event = e; |
|
||||||
return TSI_OK; |
|
||||||
} |
|
||||||
|
|
||||||
void alts_tsi_event_dispatch_to_handshaker(alts_tsi_event* event, bool is_ok) { |
|
||||||
if (event == nullptr) { |
|
||||||
gpr_log( |
|
||||||
GPR_ERROR, |
|
||||||
"ALTS TSI event is nullptr in alts_tsi_event_dispatch_to_handshaker()"); |
|
||||||
return; |
|
||||||
} |
|
||||||
alts_tsi_handshaker_handle_response(event->handshaker, event->recv_buffer, |
|
||||||
event->status, &event->details, event->cb, |
|
||||||
event->user_data, is_ok); |
|
||||||
} |
|
||||||
|
|
||||||
void alts_tsi_event_destroy(alts_tsi_event* event) { |
|
||||||
if (event == nullptr) { |
|
||||||
return; |
|
||||||
} |
|
||||||
grpc_byte_buffer_destroy(event->send_buffer); |
|
||||||
grpc_byte_buffer_destroy(event->recv_buffer); |
|
||||||
grpc_metadata_array_destroy(&event->initial_metadata); |
|
||||||
grpc_metadata_array_destroy(&event->trailing_metadata); |
|
||||||
grpc_slice_unref_internal(event->details); |
|
||||||
grpc_slice_unref_internal(event->target_name); |
|
||||||
grpc_alts_credentials_options_destroy(event->options); |
|
||||||
gpr_free(event); |
|
||||||
} |
|
@ -1,93 +0,0 @@ |
|||||||
/*
|
|
||||||
* |
|
||||||
* Copyright 2018 gRPC authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_EVENT_H |
|
||||||
#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_EVENT_H |
|
||||||
|
|
||||||
#include <grpc/support/port_platform.h> |
|
||||||
|
|
||||||
#include <grpc/byte_buffer.h> |
|
||||||
#include <grpc/byte_buffer_reader.h> |
|
||||||
|
|
||||||
#include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h" |
|
||||||
#include "src/core/tsi/transport_security_interface.h" |
|
||||||
|
|
||||||
/**
|
|
||||||
* A ALTS TSI event interface. In asynchronous implementation of |
|
||||||
* tsi_handshaker_next(), the function will exit after scheduling a handshaker |
|
||||||
* request to ALTS handshaker service without waiting for response to return. |
|
||||||
* The event is used to link the scheduled handshaker request with the |
|
||||||
* corresponding response so that enough context information can be inferred |
|
||||||
* from it to handle the response. All APIs in the header are thread-compatible. |
|
||||||
*/ |
|
||||||
|
|
||||||
/**
|
|
||||||
* Main struct for ALTS TSI event. It retains ownership on send_buffer and |
|
||||||
* recv_buffer, but not on handshaker. |
|
||||||
*/ |
|
||||||
typedef struct alts_tsi_event { |
|
||||||
alts_tsi_handshaker* handshaker; |
|
||||||
grpc_byte_buffer* send_buffer; |
|
||||||
grpc_byte_buffer* recv_buffer; |
|
||||||
grpc_status_code status; |
|
||||||
grpc_slice details; |
|
||||||
grpc_metadata_array initial_metadata; |
|
||||||
grpc_metadata_array trailing_metadata; |
|
||||||
tsi_handshaker_on_next_done_cb cb; |
|
||||||
void* user_data; |
|
||||||
grpc_alts_credentials_options* options; |
|
||||||
grpc_slice target_name; |
|
||||||
} alts_tsi_event; |
|
||||||
|
|
||||||
/**
|
|
||||||
* This method creates a ALTS TSI event. |
|
||||||
* |
|
||||||
* - handshaker: ALTS TSI handshaker instance associated with the event to be |
|
||||||
* created. The created event does not own the handshaker instance. |
|
||||||
* - cb: callback function to be called when handling data received from ALTS |
|
||||||
* handshaker service. |
|
||||||
* - user_data: argument to callback function. |
|
||||||
* - options: ALTS credentials options. |
|
||||||
* - target_name: name of endpoint used for secure naming check. |
|
||||||
* - event: address of ALTS TSI event instance to be returned from the method. |
|
||||||
* |
|
||||||
* It returns TSI_OK on success and an error status code on failure. |
|
||||||
*/ |
|
||||||
tsi_result alts_tsi_event_create(alts_tsi_handshaker* handshaker, |
|
||||||
tsi_handshaker_on_next_done_cb cb, |
|
||||||
void* user_data, |
|
||||||
grpc_alts_credentials_options* options, |
|
||||||
grpc_slice target_name, |
|
||||||
alts_tsi_event** event); |
|
||||||
|
|
||||||
/**
|
|
||||||
* This method dispatches a ALTS TSI event received from the handshaker service, |
|
||||||
* and a boolean flag indicating if the event is valid to read to ALTS TSI |
|
||||||
* handshaker to process. It is called by TSI thread. |
|
||||||
* |
|
||||||
* - event: ALTS TSI event instance. |
|
||||||
* - is_ok: a boolean value indicating if the event is valid to read. |
|
||||||
*/ |
|
||||||
void alts_tsi_event_dispatch_to_handshaker(alts_tsi_event* event, bool is_ok); |
|
||||||
|
|
||||||
/**
|
|
||||||
* This method destroys the ALTS TSI event. |
|
||||||
*/ |
|
||||||
void alts_tsi_event_destroy(alts_tsi_event* event); |
|
||||||
|
|
||||||
#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_EVENT_H */ |
|
Loading…
Reference in new issue