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

GCP library for channel management #12

Merged
merged 1 commit into from
Jul 9, 2018

Conversation

ZhouyihaiDing
Copy link
Contributor

@ZhouyihaiDing ZhouyihaiDing commented Jun 25, 2018

Code sample:

        $conf = new \Grpc\Gcp\ApiConfig();
        $conf->mergeFromJsonString($string);

        $config = new \Grpc\GCP\Config($hostname, $conf);
        $credentials = \Grpc\ChannelCredentials::createSsl();
        $auth = ApplicationDefaultCredentials::getCredentials();
        $opts = [
            'credentials' => $credentials,
            'update_metadata' => $auth->getUpdateMetadataFunc(),
            'grpc_call_invoker' => $config->callInvoker(),
        ];

        $this->stub = new SpannerGrpcClient($hostname, $opts);

Reference: implementation in python

return $this->real_call;
}

// Public funtions are rewriting all methods inside UnaryCall
Copy link
Member

Choose a reason for hiding this comment

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

This GCPUnaryCall seems only rewriting partial public methods. The original Grpc\UnaryCall extends Grpc\AbstractCall (https://github.com/grpc/grpc/blob/master/src/php/lib/Grpc/AbstractCall.php), which implements some common methods including getMetadata, getTrailingMetadata, getPeer, cancel, setCallCredentials. These methods are shared by all the 4 subclass type calls.

If we want to rewrite all the public functions of the original calls, should we also need to overwrite these common functions? We can either implement them in the superclass GcpBaseCall, or respectively in the four subclasses. Another option is to make the GcpBaseCall inherit from Grpc\AbstractCall.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. I will overwrite those functions inside GCPBaseCall. Thanks!

@ZhouyihaiDing ZhouyihaiDing force-pushed the gcp_extension branch 4 times, most recently from 5a9117f to a2c29ff Compare June 28, 2018 16:24
@TeBoring
Copy link

LGTM for php style only

Copy link
Member

@chingor13 chingor13 left a comment

Choose a reason for hiding this comment

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

A few notes on style. The cloud libraries have used the PSR-2 coding style guide.

  • Each class should be in its own separate file and it should allow the composer autoloader to require the files. If you do this, you won't need the extra requires in the test files.
  • Variables should be camel-cased
  • Private classes should not be prefixed with _. You should annotate them in the class descriptions.
  • Private variables should be camelcased and not be prefixed with _. Access modifiers is enough.
  • src/lib/ => src/

*/
namespace Grpc\GCP;

use Psr\Cache\CacheItemPoolInterface;
Copy link
Member

Choose a reason for hiding this comment

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

Need to add psr/cache to the dependencies in composer.json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

$this->affinity_key = null;
if($this->_affinity) {
$command = $this->_affinity['command'];
if ($command == 'BOUND' || $command == 'UNBIND') {
Copy link
Member

Choose a reason for hiding this comment

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

These should probably be constants. A few other spots below too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks!
I create new files for each class defined and use php-cs-fixer to help me update the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added more comments; removed methods start with '_', and also removed unnecessary comments/codes. Thank you!

composer.json Outdated
@@ -0,0 +1,25 @@
{
"name": "grpc/grpc-gcp",
Copy link
Collaborator

Choose a reason for hiding this comment

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

alternatively this could be google/cloud-grpc

This would only make sense if we saw this as possibly going into the google-cloud-php repository eventually, however.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Brent,
We think it it better to be named with gcp like grpc-gcp, because it is designed to make gcp better and solve the gcp specific issues. OSS users probably won't use this library.

*/
namespace Grpc\GCP;

class _ChannelRef
Copy link
Collaborator

Choose a reason for hiding this comment

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

starting classes with underscores is not idiomatic to PHP. use final instead?

* limitations under the License.
*
*/
namespace Grpc\GCP;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Namespace would be better as Google\Cloud\Grpc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

// If not, we can use $real_channel directly instead of creating a new one.
// It is useful to handle 'force_new' channel option.
// TODO(ddyihai): remove it once the serialzer handler for \Grpc\Channel is implemented.
class _HasDeserialized implements \Serializable {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Classes should each get their own project file and not share a file with other classes. This is to follow PSR-0 autoloading standards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I create a new file for each class defined now. Thanks!

@ZhouyihaiDing ZhouyihaiDing force-pushed the gcp_extension branch 2 times, most recently from 1ef8b2d to 2f2a95e Compare June 29, 2018 15:40
@@ -0,0 +1,15 @@
-----BEGIN CERTIFICATE-----
Copy link
Member

Choose a reason for hiding this comment

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

Just making sure these aren't real credentials.

Copy link
Contributor Author

@ZhouyihaiDing ZhouyihaiDing Jun 29, 2018

Choose a reason for hiding this comment

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

It is only used for testing. I copied it from the grpc/grpc's php unit tests. I didn't find out how I can specify certain files uploaded to the packagist because only files under src/ are needed.

}
}

abstract class GcpBaseCall
Copy link
Member

Choose a reason for hiding this comment

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

These other classes should move to separate files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Sorry about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add more comments for those classes and methods.

composer.json Outdated
"": ["src/generated/"]
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is super picky but most of our composer.json files use 4 spaces instead of two

class ChannelRef
{
// $opts has all information except Credentials for creating a Grpc\Channel
// except Grpc\Credentials.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit but this line can be removed!

putenv("GOOGLE_APPLICATION_CREDENTIALS=./../grpc-gcp.json");

require_once(dirname(__FILE__) . '/../vendor/autoload.php');
require_once(dirname(__FILE__) . '/../../vendor/autoload.php');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of dirname(__FILE__) you can use simply __DIR__

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thank you!
I am trying to run the spanner tests under google-cloud-php repo so I can add more tests later.
However, I cannot get any test passed when I am following exactly what travis does. I noticed that all files under keys/ are empty. Do you know are there extra steps I should do to run the tests?
Thanks!

@ZhouyihaiDing ZhouyihaiDing force-pushed the gcp_extension branch 2 times, most recently from a2fe458 to 62ad011 Compare June 29, 2018 18:05
@ZhouyihaiDing ZhouyihaiDing changed the title GCP extension via call invoker GCP library for connection management Jun 29, 2018
@ZhouyihaiDing ZhouyihaiDing force-pushed the gcp_extension branch 3 times, most recently from c9b20d1 to f4a5631 Compare June 29, 2018 20:52
@ZhouyihaiDing ZhouyihaiDing changed the title GCP library for connection management GCP library for channel management Jun 29, 2018
@ZhouyihaiDing
Copy link
Contributor Author

Hi all,
I create a simple PR to google-cloud-php, which describes my idea about how to enable the channel management support in the cloud API, which looks like:

$client = new SpannerClient(['enable_gcp' => true]);

It seems SpannerClient and SpannerGapicClient are both generated according to the comment, thus I need some help to find where the generator is to insert the gcp support.
Please take a look and provide suggestions, thank you!

change SESSION to apcu; Add CacheItemPoolInterface

add composer.json

update code style

create file BaseCall/UnaryCall/StreamCall

update composer.json, __DIR__ and nit

fix typo, add more comments, remove unnecessary things
@WeiranFang
Copy link
Member

This repo needs to be published. I'll merge this PR and clean some commit history before it gets public. However the package itself is not ready to be released yet. More fixes can be done in future PRs before release.

@WeiranFang WeiranFang merged commit 4903679 into GoogleCloudPlatform:master Jul 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants