Skip to content

Commit

Permalink
Backport several additions / fixes to PHP / Ruby xDS Interop tests (#…
Browse files Browse the repository at this point in the history
…26037)

* PHP: allow xDS interop client to start RPCs asynchronously (#25696)

* PHP: allow xDS interop client to start RPCs asynchronously

* Address review comments

* Remove adhoc test config

* PHP: enable fault_injection xds interop test case (#25943)

* PHP: enable fault_injection xds interop test case

* sanity check yaph_code.sh

* some mysterious SIGTERM is being observed

* Remove adhoc test cfg

* Ruby: Fix xds fault_injection test (#26006)

* Fix header_matching for PHP and Ruby (#26017)
  • Loading branch information
stanley-cheung authored Apr 21, 2021
1 parent 02e2afe commit 4bbeb37
Show file tree
Hide file tree
Showing 10 changed files with 594 additions and 66 deletions.
63 changes: 63 additions & 0 deletions src/php/bin/run_xds_client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash
# Copyright 2021 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.


# This script is being launched from the run_xds_tests.py runner and is
# responsible for managing child PHP processes.

cleanup () {
echo "Trapped SIGTERM. Cleaning up..."
set -x
kill -9 $PID1
kill -9 $PID2
running=false
set +x
}

trap cleanup SIGTERM

set -e
cd $(dirname $0)/../../..
root=$(pwd)

# tmp_file1 contains the list of RPCs (and their spec) the parent PHP
# process want executed
tmp_file1=$(mktemp)
# tmp_file2 contains the RPC result of each key initiated
tmp_file2=$(mktemp)

set -x
# This is the PHP parent process, who is primarily responding to the
# run_xds_tests.py runner's stats requests
php -d extension=grpc.so -d extension=pthreads.so \
src/php/tests/interop/xds_client.php $1 $2 $3 $4 $5 $6 \
--tmp_file1=$tmp_file1 --tmp_file2=$tmp_file2 &
PID1=$!

# This script watches RPCs written to tmp_file1, spawn off more PHP
# child processes to execute them, and writes the result to tmp_file2
python3 -u src/php/bin/xds_manager.py \
--tmp_file1=$tmp_file1 --tmp_file2=$tmp_file2 \
--bootstrap_path=$GRPC_XDS_BOOTSTRAP &
PID2=$!
set +x

# This will be killed by a SIGTERM signal from the run_xds_tests.py
# runner
running=true
while $running
do
sleep 1
done
98 changes: 98 additions & 0 deletions src/php/bin/xds_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python
# Copyright 2021 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.
"""Manage PHP child processes for the main PHP xDS Interop client"""

import argparse
import fcntl
import os
import subprocess

# This script is being launched from src/php/bin/run_xds_client.sh
# to manage PHP child processes which will send 1 RPC each
# asynchronously. This script keeps track of all those open
# processes and reports back to the main PHP interop client each
# of the child RPCs' status code.

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--tmp_file1', nargs='?', default='')
parser.add_argument('--tmp_file2', nargs='?', default='')
parser.add_argument('--bootstrap_path', nargs='?', default='')
args = parser.parse_args()
server_address = ''
rpcs_started = []
open_processes = {}
client_env = dict(os.environ)
client_env['GRPC_XDS_BOOTSTRAP'] = args.bootstrap_path
while True:
# tmp_file1 contains a list of RPCs (and their spec) the parent process
# wants executed
f1 = open(args.tmp_file1, 'r+')
fcntl.flock(f1, fcntl.LOCK_EX)
while True:
key = f1.readline()
if not key:
break
key = key.strip()
if key.startswith('server_address'):
if not server_address:
server_address = key[15:]
elif not key in rpcs_started:
# format here needs to be in sync with
# src/php/tests/interop/xds_client.php
items = key.split('|')
num = items[0]
metadata = items[2]
timeout_sec = items[3]
if items[1] == 'UnaryCall':
p = subprocess.Popen([
'php', '-d', 'extension=grpc.so', '-d',
'extension=pthreads.so',
'src/php/tests/interop/xds_unary_call.php',
'--server=' + server_address, '--num=' + str(num),
'--metadata=' + metadata, '--timeout_sec=' + timeout_sec
],
env=client_env)
elif items[1] == 'EmptyCall':
p = subprocess.Popen([
'php', '-d', 'extension=grpc.so', '-d',
'extension=pthreads.so',
'src/php/tests/interop/xds_empty_call.php',
'--server=' + server_address, '--num=' + str(num),
'--metadata=' + metadata, '--timeout=' + timeout_sec
],
env=client_env)
else:
continue
rpcs_started.append(key)
open_processes[key] = p
f1.truncate(0)
fcntl.flock(f1, fcntl.LOCK_UN)
f1.close()
# tmp_file2 contains the RPC result of each key received from tmp_file1
f2 = open(args.tmp_file2, 'a')
fcntl.flock(f2, fcntl.LOCK_EX)
keys_to_delete = []
for key, process in open_processes.items():
result = process.poll()
if result is not None:
# format here needs to be in sync with
# src/php/tests/interop/xds_client.php
f2.write(key + ',' + str(process.returncode) + "\n")
keys_to_delete.append(key)
for key in keys_to_delete:
del open_processes[key]
fcntl.flock(f2, fcntl.LOCK_UN)
f2.close()
5 changes: 5 additions & 0 deletions src/php/ext/grpc/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ PHP_METHOD(Call, startBatch) {
goto cleanup;
}

// c-core may call rand(). If we don't call srand() here, all the
// random numbers being returned would be the same.
gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
srand(now.tv_nsec);

array_hash = Z_ARRVAL_P(array);

char *key = NULL;
Expand Down
4 changes: 1 addition & 3 deletions src/php/lib/Grpc/RpcServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ private function loadRequest($request) {
$event = $this->call->startBatch([
OP_RECV_MESSAGE => true,
]);
if (!$event->message) {
throw new Exception("Did not receive a proper message");
}
$request->clear();
$request->mergeFromString($event->message);
return $request;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// DO NOT EDIT
namespace Grpc\Testing;
class XdsUpdateClientConfigureServiceStub {
}

Loading

0 comments on commit 4bbeb37

Please sign in to comment.