Skip to content

Commit 3acb171

Browse files
authored
[ycable] cleanup logic for creating grpc future ready (sonic-net#289)
Signed-off-by: vaibhav-dahiya [email protected] This PR attempts to remove this call channel_ready = grpc.channel_ready_future for the ycabled gRPC implementation. The reason to do this is incase of channel not being connected ycabled tries to reattempt the channel creation again with each RPC request from linkmgrd. This is not the right way to maintain the channels/stubs, and actually adds unrequired overhead for ycabled. This PR supports creating channel/stub in ycabled in correct manner. Description Motivation and Context Required to reduce CPU usage for ycabled How Has This Been Tested? Deploying the changes on Arista testbed and UT
1 parent ce3b6db commit 3acb171

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

sonic-ycabled/tests/test_y_cable_helper.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -4962,10 +4962,18 @@ def test_setup_grpc_channel_for_port(self):
49624962
with patch('ycable.ycable_utilities.y_cable_helper.y_cable_platform_sfputil') as patched_util:
49634963

49644964
patched_util.get_asic_id_for_logical_port.return_value = 0
4965-
rc = setup_grpc_channel_for_port("Ethernet0", "192.168.0.1")
4965+
(channel, stub) = setup_grpc_channel_for_port("Ethernet0", "192.168.0.1")
49664966

4967-
assert(rc == (None, None))
4967+
assert(stub == True)
4968+
assert(channel != None)
49684969

4970+
def test_connect_channel(self):
4971+
4972+
with patch('grpc.channel_ready_future') as patched_util:
4973+
4974+
patched_util.result.return_value = 0
4975+
rc = connect_channel(patched_util, None, None)
4976+
assert(rc == None)
49694977

49704978
def test_setup_grpc_channels(self):
49714979

sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py

+48-22
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ def wrapper(*args, **kwargs):
332332

333333
return wrapper
334334

335-
336335
def retry_setup_grpc_channel_for_port(port, asic_index):
337336

338337
global grpc_port_stubs
@@ -409,34 +408,61 @@ def get_grpc_credentials(type, kvp):
409408

410409
return credential
411410

412-
def create_channel(type,level, kvp, soc_ip):
411+
def connect_channel(channel, stub, port):
413412

413+
channel_ready = grpc.channel_ready_future(channel)
414414
retries = 3
415+
415416
for _ in range(retries):
417+
try:
418+
channel_ready.result(timeout=2)
419+
except grpc.FutureTimeoutError:
420+
helper_logger.log_warning("gRPC port {} state changed to SHUTDOWN".format(port))
421+
else:
422+
break
416423

417-
if type == "secure":
418-
credential = get_grpc_credentials(level, kvp)
419-
target_name = kvp.get("grpc_ssl_credential", None)
420-
if credential is None or target_name is None:
421-
return (None, None)
424+
def create_channel(type, level, kvp, soc_ip, port):
422425

423-
GRPC_CLIENT_OPTIONS.append(('grpc.ssl_target_name_override', '{}'.format(target_name)))
424426

425-
channel = grpc.secure_channel("{}:{}".format(soc_ip, GRPC_PORT), credential, options=GRPC_CLIENT_OPTIONS)
426-
else:
427-
channel = grpc.insecure_channel("{}:{}".format(soc_ip, GRPC_PORT), options=GRPC_CLIENT_OPTIONS)
427+
#Helper callback to get an channel connectivity state
428+
def wait_for_state_change(channel_connectivity):
429+
if channel_connectivity == grpc.ChannelConnectivity.TRANSIENT_FAILURE:
430+
helper_logger.log_notice("gRPC port {} state changed to TRANSIENT_FAILURE".format(port))
431+
if channel_connectivity == grpc.ChannelConnectivity.CONNECTING:
432+
helper_logger.log_notice("gRPC port {} state changed to CONNECTING".format(port))
433+
if channel_connectivity == grpc.ChannelConnectivity.READY:
434+
helper_logger.log_notice("gRPC port {} state changed to READY".format(port))
435+
if channel_connectivity == grpc.ChannelConnectivity.IDLE:
436+
helper_logger.log_notice("gRPC port {} state changed to IDLE".format(port))
437+
if channel_connectivity == grpc.ChannelConnectivity.SHUTDOWN:
438+
helper_logger.log_notice("gRPC port {} state changed to SHUTDOWN".format(port))
428439

429-
stub = linkmgr_grpc_driver_pb2_grpc.DualToRActiveStub(channel)
430440

431-
channel_ready = grpc.channel_ready_future(channel)
441+
if type == "secure":
442+
credential = get_grpc_credentials(level, kvp)
443+
target_name = kvp.get("grpc_ssl_credential", None)
444+
if credential is None or target_name is None:
445+
return (None, None)
432446

433-
try:
434-
channel_ready.result(timeout=2)
435-
except grpc.FutureTimeoutError:
436-
channel = None
437-
stub = None
438-
else:
439-
break
447+
GRPC_CLIENT_OPTIONS.append(('grpc.ssl_target_name_override', '{}'.format(target_name)))
448+
449+
channel = grpc.secure_channel("{}:{}".format(soc_ip, GRPC_PORT), credential, options=GRPC_CLIENT_OPTIONS)
450+
else:
451+
channel = grpc.insecure_channel("{}:{}".format(soc_ip, GRPC_PORT), options=GRPC_CLIENT_OPTIONS)
452+
453+
454+
stub = linkmgr_grpc_driver_pb2_grpc.DualToRActiveStub(channel)
455+
456+
457+
if channel is not None:
458+
channel.subscribe(wait_for_state_change)
459+
460+
#connect_channel(channel, stub, port)
461+
"""
462+
Comment the connect channel call for now, since it is not required for normal gRPC I/O
463+
and all use cases work without it.
464+
TODO: check if this subroutine call can be ommitted for all use cases in future enhancements
465+
"""
440466

441467
return channel, stub
442468

@@ -490,12 +516,12 @@ def setup_grpc_channel_for_port(port, soc_ip):
490516
kvp = dict(fvs)
491517

492518

493-
channel, stub = create_channel(type, level, kvp, soc_ip)
519+
channel, stub = create_channel(type, level, kvp, soc_ip, port)
494520

495521
if stub is None:
496522
helper_logger.log_warning("stub was not setup for gRPC soc ip {} port {}, no gRPC soc server running ?".format(soc_ip, port))
497523
if channel is None:
498-
helper_logger.log_warning("channel was not setup for gRPC soc ip {} port {}, no gRPC server running ?".format(soc_ip, port))
524+
helper_logger.log_warning("channel was not setup for gRPC soc ip {} port {}, no gRPC soc server running ?".format(soc_ip, port))
499525

500526
return channel, stub
501527

0 commit comments

Comments
 (0)