Problem/Motivation

The method Drupal\Core\DrupalKernel::handle() does more then only handle the user request. When Drupal is run with Apache or NginX this is not a problem. The Swoole module boot Drupal core, keeps it in memory and then feeds it requests at supersonic speeds. The more that is preloaded, the less that has to be done when the user request is to be executed. The Swoole module requires that the site_path is set in advance. The Swoole module wants to skip calling: static::bootEnvironment();, $this->initializeSettings($request); and $this->boot(); on every user request. It is just not needed and slows down the handling of a user request. The problem is that the Swoole module now needs to call the method Drupal\Core\DrupalKernel::handleException() directly and the method is protected.

Proposed resolution

Change the method Drupal\Core\DrupalKernel::handle() to make it work with the Swoole module.

Remaining tasks

TBD

User interface changes

None

API changes

See proposed solution.

Data model changes

None

Release notes snippet

TBD

Comments

daffie created an issue. See original summary.

daffie’s picture

Status: Active » Needs review
StatusFileSize
new1.82 KB

The patch with testing.

geek-merlin’s picture

Version: 10.0.x-dev » 9.5.x-dev
Status: Needs review » Needs work

The rationale makes sense.
Code-wise, the patch trivially does what it announces.
It contains a test that is green.
No BC breaking API change, so no change record needed.

1) Technically a red test-only patch is needed. So NW for that.

I don't see a reason this can not go to 9.x (but may overlook sth) so changing to 9.5.x.

THX for the terrific work daffie!

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new1.06 KB
new1.84 KB

Added a change record, because we are doing a API addition.

Added a tests only patch, which should fail the testbot.

@geek-merlin: Thanks for the review!

Status: Needs review » Needs work

The last submitted patch, 4: 3279192-4.patch, failed testing. View results

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new1.07 KB
new1.84 KB

The constant Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST in Symfony 6 was named Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST in Symfony 4, which is used by Drupal 9.

daffie’s picture

StatusFileSize
new754 bytes

Forgot the interdiff file.

The last submitted patch, 6: 3279192-9.5-6-test-only-should-fail.patch, failed testing. View results

geek-merlin’s picture

Status: Needs review » Reviewed & tested by the community

All from #3 resolved, red test plus CR, so RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 3279192-9.5-6.patch, failed testing. View results

daffie’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC

geek-merlin’s picture

@daffie: The UI says it "re-tests (only the red patch) every 2 days". Maybe uploading first green then red patch might prevent that bumping back and forth.

alexpott’s picture

Given that \Drupal\Core\DrupalKernel::bootEnvironment() has protection and so does ::boot() in terms of calling again wouldn't it be better to change ::handle?

If we change the try{} block to

    try {
      if (!$this->booted) {
        $this->initializeSettings($request);

        // Redirect the user to the installation script if Drupal has not been
        // installed yet (i.e., if no $databases array has been defined in the
        // settings.php file) and we are not already installing.
        if (!Database::getConnectionInfo() && !InstallerKernel::installationAttempted() && PHP_SAPI !== 'cli') {
          $catch = TRUE;
          throw new DatabaseNotFoundException();
        }
        $this->boot();
      }
      $response = $this->getHttpKernel()->handle($request, $type, $catch);
    }

I think this will mean the Swoole doesn't have to know so much about the internals of getting a response whilst making it easier for ::handle() to be re-entrant without doing unnecessary work.

daffie’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.61 KB

@alexpott: Totally not a solution that I had thought of, but works for me and the Swoole module. The Swoole module can call static::bootEnvironment() and $this->boot(), before calling $this->handle() when an user request is made. Thank you very much for this solution!

daffie’s picture

Title: Make the method Drupal\Core\DrupalKernel::handleException() public » Change the method Drupal\Core\DrupalKernel::handle() to make it work for the Swoole module
Issue summary: View changes
andypost’s picture

Yay, that looks cleaner!

Btw not sure check for CLI is handy, Octane has roadrunner's implementation as well + reactphp with fibers also will be blocked as it running via CLI

alexpott’s picture

Status: Needs review » Needs work

I think we can do even better here tbh...

I we change \Drupal\Core\Installer\InstallerRedirectTrait::shouldRedirectToInstaller to do:

    // If the database wasn't found, assume the user hasn't entered it properly
    // and redirect to the installer. This check needs to come first because a
    // DatabaseNotFoundException is also an instance of DatabaseException.
    if ($exception instanceof DatabaseNotFoundException || $exception instanceof ConnectionNotDefinedException) {
      return TRUE;
    }

Note we'll need to add ConnectionNotDefinedException to the use statements. Then \Drupal\Core\DrupalKernel::handle() can be...

  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
    // Ensure sane PHP environment variables.
    static::bootEnvironment();

    try {
      if (!$this->booted) {
        $this->initializeSettings($request);
        $this->boot();
      }
      $response = $this->getHttpKernel()->handle($request, $type, $catch);
    }
    catch (\Exception $e) {
      if ($catch === FALSE) {
        throw $e;
      }

      $response = $this->handleException($e, $request, $type);
    }

    // Adapt response headers to the current request.
    $response->prepare($request);

    return $response;
  }

It's tempting to move the

    // Ensure sane PHP environment variables.
    static::bootEnvironment();

inside the if (!$this->booted) { too. Not sure about that... maybe a follow-up.

andypost’s picture

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new2.28 KB
new2.05 KB

Made changes as per comment #17, please review.

andypost’s picture

Status: Needs review » Needs work
daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new2.64 KB
new582 bytes

Fixing the style guide violation.

@alexpott: Thank you for your input in this issue!

geek-merlin’s picture

Code-wise i can confirm that #17 review is done in #19 / #21 (+cs-fix).

Dunno why the php7 tests fail:

pingwin4eg’s picture

Re PHP 7.3: I see the same 43 errors in PhpRequirementsTest locally without this patch, on bare Drupal 9.5.x, so it's not related. Not sure if there's an open issue for that though.

Update: There is an open issue for that - #3261447-55: Add an API for dynamically setting recommended and supported PHP versions based on known and predicted PHP release schedules.

pingwin4eg’s picture

Re PHP 7.4 & PgSQL 9.5:

The database server version 9.5.19 is less than the minimum required version 10.

So I requeued PHP 7.3 & 8.1 with PgSQL 10 - there's no option for PHP 7.4 with PgSQL 10 for some reason.

pingwin4eg’s picture

Tests passed.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The database server version 9.5.19 is less than the minimum required version 10.

@pingwin4eg: You are right. I used the wrong PostgreSQL version for the testbot.

The code in the patch from comment #21 is the same as the patch from comment #19 with a small guide style fix. The code from the patch is from @alexpott fro the comments #13 and #17. Therefor I can review the patch.
All the code changes looks good to me. The code changes are only a small refacturing to make it easier for the swoole module to use it.
For me it is RTBC.

alexpott’s picture

Category: Feature request » Task
Status: Reviewed & tested by the community » Fixed

Committed and pushed 61e4c24929 to 10.0.x and dcca2f9041 to 9.5.x. Thanks!

  • alexpott committed 61e4c24 on 10.0.x
    Issue #3279192 by daffie, ravi.shankar, geek-merlin, alexpott: Change...

  • alexpott committed dcca2f9 on 9.5.x
    Issue #3279192 by daffie, ravi.shankar, geek-merlin, alexpott: Change...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.