roadrunner-server / roadrunner-plugins

📦 Home for the roadrunner plugins
MIT License
25 stars 9 forks source link

[BUG] I cant run grpc server #100

Closed revilon1991 closed 2 years ago

revilon1991 commented 2 years ago

Thank you for your work. I have a problem. Maybe, it isn't a bug, but I seem this kind of issue more matches my problem.

I got the rr by documentation, then I ran rr but I didn't see that grpc server is running. And my client code can't connect to grpc server. What's wrong?

I tried this code:

composer require spiral/roadrunner:v2.0 nyholm/psr7
./vendor/bin/rr get-binary
./rr serve
# DEBUG rpc Started RPC service {"address": "tcp://127.0.0.1:6001", "plugins": ["informer", "resetter"]}

I expected to see this happen:

{"address": "tcp://127.0.0.1:6001", "plugins": ["informer", "resetter", "grpc"]}

Instead, this happened:

{"address": "tcp://127.0.0.1:6001", "plugins": ["informer", "resetter"]}

The version of RR used: v2.5.3

The operation system used: linux

RR configuration file content:

# .rr.yaml
rpc:
  enable: true
  listen: tcp://127.0.0.1:6001

grpc:
  listen: "tcp://:9090"
  proto: "service.proto"
  tls:
    key:  "server.key"
    cert: "server.crt"
  workers:
    command: "php GrpcWorker.php"
    pool:
      numWorkers: 1

My worker for grpc server:

// GrpcWorker.php
<?php

/**
 * Sample GRPC PHP server.
 */

use Service\EchoInterface;
use Spiral\RoadRunner\GRPC\Server;
use Spiral\RoadRunner\Worker;

require __DIR__ . '/vendor/autoload.php';

$server = new Server(null, [
    'debug' => true,
]);

$server->registerService(EchoInterface::class, new EchoService());

$server->serve(Worker::create());

My client code:

<?php

include "vendor/autoload.php";

$client = new \Service\EchoClient(
    'localhost:9090',
    [
        'credentials' => \Grpc\ChannelCredentials::createSsl(file_get_contents("server.crt")),
    ]
);

$result = $client->Ping(
    new \Service\Message()
)->wait();

var_dump($result[1]->code, $result[1]->details);
// int(14)
// string(34) "failed to connect to all addresses"

Errortrace, Backtrace or Panictrace: none

rustatian commented 2 years ago

Hey @revilon1991 . Documentation is located here: https://github.com/spiral/roadrunner-docs/blob/master/beep-beep/grpc.md You used the wrong configuration (from RR1). There is no workers configuration section. Please, use the configuration from the link above.

rustatian commented 2 years ago

This is the correct output: {"address": "tcp://127.0.0.1:6001", "plugins": ["informer", "resetter"]} This output is from the RPC plugin with registered RPC endpoints. GRPC plugin doesn't have any RPC endpoints, thus, didn't appear in this log message.

revilon1991 commented 2 years ago

@rustatian yes, I saw this doc. So, I changed my .rr.yaml:

grpc:
  listen: "tcp://localhost:9001"
  proto: "service.proto"
  tls:
    key: "server.crt"
    cert: "server.key"
    client_auth_type: "no_client_certs"

  max_send_msg_size: 50
  max_recv_msg_size: 50
  max_connection_idle: 0s
  max_connection_age: 0s
  max_connection_age_grace: 0s
  max_concurrent_streams: 10
  ping_time: 1s
  timeout: 200s

  pool:
    num_workers: 2
    max_jobs: 0
    allocate_timeout: 60s
    destroy_timeout: 60

but I got a connection error from the client again.

Maybe we have something else?

I would be glad if I can debug that.

rustatian commented 2 years ago

but I got a connection error from the client again.

Could you please share this error?

revilon1991 commented 2 years ago

Yes. My client code:

<?php

include "vendor/autoload.php";

$client = new \Service\EchoClient(
    'localhost:9001',
    [
        'credentials' => \Grpc\ChannelCredentials::createSsl(file_get_contents("server.crt")),
    ]
);

$result = $client->Ping(
    new \Service\Message()
)->wait();

var_dump($result[1]->code, $result[1]->details);

and I get:

// int(14)
// string(34) "failed to connect to all addresses"

Also I checked with nmap what's port is open when the server is running.

# nmap localhost -p9001
PORT     STATE  SERVICE
9001/tcp closed tor-orport

I guess that directive grpc not handled because I tried to change keys to a wrong (for ex listen => lusten) and it doesn't give effect (I didn't see any error).

rustatian commented 2 years ago

Could you please share your full configuration? (rr.yaml)

revilon1991 commented 2 years ago

yes, of course. cat .rr.yaml =>

rpc:
  enable: true
  listen: tcp://127.0.0.1:6001

grpc:
  lusten: "tcp://localhost:9001"
  workers.command: "php GrpcWorker.php"
  proto: "service.proto"
  tls:
    key: "server.crt"
    cert: "server.key"
    client_auth_type: "no_client_certs"

  max_send_msg_size: 50
  max_recv_msg_size: 50
  max_connection_idle: 0s
  max_connection_age: 0s
  max_connection_age_grace: 0s
  max_concurrent_streams: 10
  ping_time: 1s
  timeout: 200s

  pool:
    num_workers: 2
    max_jobs: 0
    allocate_timeout: 60s
    destroy_timeout: 60
rustatian commented 2 years ago

@revilon1991 Where did you find this option: workers.command: "php GrpcWorker.php" ?

revilon1991 commented 2 years ago

I saw this here https://github.com/spiral/app-grpc#running-grpc-server

It's all my tries. Also, I tried like that:

rpc:
  enable: true
  listen: tcp://127.0.0.1:6001

grpc:
  listen: "tcp://localhost:9001"
#  workers.command: "php GrpcWorker.php"
  proto: "service.proto"
  tls:
    key: "server.crt"
    cert: "server.key"
    client_auth_type: "no_client_certs"

  max_send_msg_size: 50
  max_recv_msg_size: 50
  max_connection_idle: 0s
  max_connection_age: 0s
  max_connection_age_grace: 0s
  max_concurrent_streams: 10
  ping_time: 1s
  timeout: 200s

  pool:
    num_workers: 2
    max_jobs: 0
    allocate_timeout: 60s
    destroy_timeout: 60

no difference :(

rustatian commented 2 years ago

You used RRv1 configuration on the RRv2 binary (I mentioned this).

  1. grpc configuration is correct.
  2. rpc doesn't have enable option.
  3. https://github.com/spiral/roadrunner-plugins/blob/master/tests/plugins/grpc/configs/.rr-grpc-init.yaml sample config (attention at the server plugin)
revilon1991 commented 2 years ago

You are right. I have progress, but now I see error about CRC:

# ./rr serve
handle_serve_command: Serve error:
        endure_start:
        endure_serve_internal: Function call error:
        endure_call_serve_fn: got initial serve error from the Vertex grpc.Plugin, stopping execution, error: grpc_plugin_serve: WorkerAllocate:
        goridge_frame_receive: CRC verification failed

Though I copied config from example.

Do I have to do somthing else?

PS Now my config looks like that:

rpc:
    listen: "tcp://127.0.0.1:6001"

server:
    command: "php GrpcWorker.php"
    relay: "pipes"
    relay_timeout: "20s"

grpc:
    listen: "tcp://localhost:9001"
    proto: "service.proto"
    max_send_msg_size: 50
    max_recv_msg_size: 50
    max_connection_idle: 0s
    max_connection_age: 0s
    max_connection_age_grace: 0s
    max_concurrent_streams: 10
    ping_time: 1s
    timeout: 200s
    pool:
        num_workers: 2
        max_jobs: 0
        allocate_timeout: 60s
        destroy_timeout: 60
rustatian commented 2 years ago

This is the sample worker: https://github.com/spiral/roadrunner-plugins/blob/master/tests/worker-grpc.php CRC error means, that your worker responded incorrectly.

rustatian commented 2 years ago

Keep in mind, that you should use the new package: https://github.com/spiral/roadrunner-grpc

revilon1991 commented 2 years ago

Yes, I use the package spiral/roadrunner-grpc and content of my worker is identical with https://github.com/spiral/roadrunner-plugins/blob/master/tests/worker-grpc.php

I can show it: # cat GrpcWorker.php

<?php

/**
 * Sample GRPC PHP server.
 */

use Service\EchoInterface;
use Spiral\RoadRunner\GRPC\Server;
use Spiral\RoadRunner\Worker;

require __DIR__ . '/vendor/autoload.php';

$server = new Server(null, [
    'debug' => false, // optional (default: false)
]);

$server->registerService(EchoInterface::class, new EchoService());

$server->serve(Worker::create());
rustatian commented 2 years ago

The problem might be in some PHP warnings before RR starts for example.

revilon1991 commented 2 years ago

Yes. I saw it when I launch php GrpcWorker.php

Fatal error: Uncaught Error: Class "EchoService" not found in /app/GrpcWorker.php:17
Stack trace:
#0 {main}
  thrown in /app/GrpcWorker.php on line 17

I should copy the example files to the project namespace and use those in the worker.

Now I have a strange problem with the port.

handle_serve_command: Serve error:
        endure_start:
        endure_serve_internal: Function call error:
        endure_call_serve_fn: got initial serve error from the Vertex grpc.Plugin, stopping execution, error: grpc_plugin_serve: cannot bind to "localhost:9001": cannot assign requested address

But my port 9001 is not busy. I checked it with nmap utils and I tried other ports.

If I can increase verbose maybe I will be able to repair it.

rustatian commented 2 years ago

@revilon1991 Yes, you can use:

logs:
  mode: development
  level: debug

ref: https://github.com/spiral/roadrunner-binary/blob/master/.rr.yaml#L57

revilon1991 commented 2 years ago

I add this to my config:

logs:
      mode: development
      level: debug
      output: /var/log/rr_errors.log

It got me such little... :(

/app # ./rr serve
/app # cat /var/log/rr_errors.log 
2021-10-31T17:12:37.547Z        DEBUG   server          worker constructed      {"pid": 1491}
2021-10-31T17:12:37.594Z        DEBUG   server          worker constructed      {"pid": 1495}
rustatian commented 2 years ago

That means, that workers started successfully.

revilon1991 commented 2 years ago

It starts and stops at once... I can show you my full flow work: road runner cant start

rustatian commented 2 years ago

The RR clearly says that it cannot bind to this address. Check the RR permissions or other running software.

revilon1991 commented 2 years ago

I could resolve this problem. I just changed the directive listen:

# ...
grpc:
    listen: "tcp://localhost:9001"
# ...

=>

# ...
grpc:
    listen: "tcp://:9001"
# ...

Now it works.

Thank you so much @rustatian. 🤗 I'm very glad.

rustatian commented 2 years ago

You are welcome ❤️ Feel free to close the issue 👍🏻