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

[prototype] Compiled schema synchronization #113

Draft
wants to merge 3 commits into
base: 2.3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
"ezsystems/ezplatform-code-style": "^0.1.0",
"mikey179/vfsstream": "^1.6"
},
"suggest": {
"aws/aws-sdk-php": "For compiled schema synchronization over AWS-S3",
"ext-redis": "*"
},
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformGraphQL\\": "src",
Expand Down
60 changes: 60 additions & 0 deletions src/Command/PublishSchemaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\EzPlatformGraphQL\Command;

use EzSystems\EzPlatformGraphQL\Schema\Sync\SharedSchema;
use Redis;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;

class PublishSchemaCommand extends Command
{
/**
* @var string
*/
private $definitionsDirectory;

/**
* @var \EzSystems\EzPlatformGraphQL\Schema\Sync\SharedSchema
*/
private $sharedSchema;

public function __construct(SharedSchema $sharedSchema, string $definitionsDirectory, string $name = null)
{
parent::__construct($name);
$this->definitionsDirectory = $definitionsDirectory;
$this->sharedSchema = $sharedSchema;
}

public function configure()
{
$this->setName('ibexa:graphql:publish-schema');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

foreach ((new Finder())->files()->in($this->definitionsDirectory) as $file) {
$this->sharedSchema->addFile($file->getBasename(), $file->getContents());
}

try {
$this->sharedSchema->publish(
filemtime("$this->definitionsDirectory/__classes.map")
);
} catch (\Exception $e) {
$io->error($e->getMessage());
return self::FAILURE;
}

return self::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace EzSystems\EzPlatformGraphQL\DependencyInjection;

use Aws\S3\S3Client;
use EzSystems\EzPlatformGraphQL\DependencyInjection\GraphQL\YamlSchemaProvider;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -39,8 +40,16 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services/mutations.yaml');
$loader->load('services/resolvers.yaml');
$loader->load('services/schema.yaml');
$loader->load('services/schema_sync.yaml');
$loader->load('services/services.yaml');
$loader->load('default_settings.yaml');

if (extension_loaded('redis')) {
$loader->load('services/schema_sync.yaml');
if (class_exists(S3Client::class)) {
$loader->load('services/schema_sync_s3.yaml');
}
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/Resources/config/services/schema_sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
parameters:
ibexa_graphql.definitions_directory: '%kernel.cache_dir%/overblog/graphql-bundle/__definitions__'
ibexa_graphql.sync.shared_directory: '/tmp/graphql-schema'

services:
_defaults:
autoconfigure: true
autowire: true
public: false
bind:
EzSystems\EzPlatformGraphQL\Schema\Sync\TimestampHandler: '@EzSystems\EzPlatformGraphQL\Schema\Sync\RedisTimestampHandler'
Redis $graphQLSyncRedis: '@ibexa_graphql.sync.redis_client'

EzSystems\EzPlatformGraphQL\Command\PublishSchemaCommand:
arguments:
$definitionsDirectory: '%ibexa_graphql.definitions_directory%'

EzSystems\EzPlatformGraphQL\Schema\Sync\LocalFolderSharedSchema:
arguments:
$sharedDirectory: '%ibexa_graphql.sync.shared_directory%'

EzSystems\EzPlatformGraphQL\Schema\Sync\SharedSchema: '@EzSystems\EzPlatformGraphQL\Schema\Sync\LocalFolderSharedSchema'

EzSystems\EzPlatformGraphQL\Schema\Sync\AddTypesSolutions:
arguments:
$definitionsDirectory: '%ibexa_graphql.definitions_directory%'

EzSystems\EzPlatformGraphQL\Schema\Sync\UpdateSchemaIfNeeded:
arguments:
$definitionsDirectory: '%ibexa_graphql.definitions_directory%'

ibexa_graphql.sync.redis_client:
class: Redis
calls:
- connect: ['%env(REDIS_GRAPHQL_HOST)%', '%env(REDIS_GRAPHQL_PORT)%']
- select: ['%env(REDIS_GRAPHQL_DBINDEX)%']

EzSystems\EzPlatformGraphQL\Schema\Sync\RedisTimestampHandler: ~

17 changes: 17 additions & 0 deletions src/Resources/config/services/schema_sync_s3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
_defaults:
bind:
Aws\S3\S3Client $graphQLSyncS3Client: '@ibexa_graphql.sync.s3_client'
EzSystems\EzPlatformGraphQL\Schema\Sync\TimestampHandler: '@EzSystems\EzPlatformGraphQL\Schema\Sync\RedisTimestampHandler'

ibexa_graphql.sync.s3_client:
class: Aws\S3\S3Client
arguments:
- version: '2006-03-01'
region: '%env(GRAPHQL_SYNC_S3_REGION)%'

EzSystems\EzPlatformGraphQL\Schema\Sync\S3SharedSchema:
arguments:
$bucket: '%env(GRAPHQL_SYNC_S3_BUCKET)%'

EzSystems\EzPlatformGraphQL\Schema\Sync\SharedSchema: '@EzSystems\EzPlatformGraphQL\Schema\Sync\S3SharedSchema'
88 changes: 88 additions & 0 deletions src/Schema/Sync/AddTypesSolutions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace EzSystems\EzPlatformGraphQL\Schema\Sync;

use Overblog\GraphQLBundle\Definition\ConfigProcessor;
use Overblog\GraphQLBundle\Definition\GlobalVariables;
use Overblog\GraphQLBundle\Event\Events;
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
use Overblog\GraphQLBundle\Resolver\TypeResolver;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AddTypesSolutions implements EventSubscriberInterface
{
/**
* @var \Overblog\GraphQLBundle\Resolver\TypeResolver
*/
private $typeResolver;

/**
* @var \Overblog\GraphQLBundle\Definition\ConfigProcessor
*/
private $configProcessor;

/**
* @var \Overblog\GraphQLBundle\Definition\GlobalVariables
*/
private $globalVariables;

/**
* @var string
*/
private $definitionsDirectory;

/**
* @var \Psr\Log\LoggerInterface|null
*/
private $logger;

public function __construct(TypeResolver $typeResolver, ConfigProcessor $configProcessor, GlobalVariables $globalVariables, string $definitionsDirectory, ?LoggerInterface $logger)
{
$this->typeResolver = $typeResolver;
$this->configProcessor = $configProcessor;
$this->globalVariables = $globalVariables;
$this->definitionsDirectory = $definitionsDirectory;
$this->logger = $logger;
}

/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [Events::PRE_EXECUTOR => 'registerTypes'];
}

public function registerTypes(ExecutorArgumentsEvent $event)
{
$classMapFile = "$this->definitionsDirectory/__classes.map";
$map = include($classMapFile);
ksort($map);
foreach ($map as $class => $file) {
$typeName = str_replace('Overblog\\GraphQLBundle\\__DEFINITIONS__\\', '', $class);
$typeName = substr($typeName, 0, -4);
if ($this->typeResolver->hasSolution($class)) {
continue;
}
$this->typeResolver->addSolution(
$class,
[
[$this, 'build'],
[$class]
],
[$typeName],
[
'id' => $class,
'aliases' => [$typeName],
'alias' => $typeName,
'generated' => true
]
);
}
}

public function build($id)
{
return new $id($this->configProcessor, $this->globalVariables);
}
}
68 changes: 68 additions & 0 deletions src/Schema/Sync/LocalFolderSharedSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\EzPlatformGraphQL\Schema\Sync;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class LocalFolderSharedSchema implements SharedSchema
{
/**
* @var string
*/
private $sharedDirectory;

/**
* @var array Map of filename => file contents
*/
private $files = [];

/**
* @var \EzSystems\EzPlatformGraphQL\Schema\Sync\TimestampHandler
*/
private $timestampHandler;

public function __construct(TimestampHandler $timestampHandler, string $sharedDirectory)
{
$this->sharedDirectory = rtrim($sharedDirectory, '/');
$this->timestampHandler = $timestampHandler;
}

public function addFile(string $name, string $contents)
{
$this->files[$name] = $contents;
}

public function publish(int $timestamp)
{
$fs = new Filesystem();

$targetDirectory = "$this->sharedDirectory/$timestamp";
$fs->mkdir($targetDirectory);

foreach ($this->files as $name => $contents) {
$fs->dumpFile("$targetDirectory/$name", $contents);
}

$this->timestampHandler->set($timestamp);
}

public function getFiles(int $timestamp): array
{
$directory = "$this->sharedDirectory/$timestamp";
if (!file_exists($directory) || !is_dir($directory)) {
throw new \Exception("Directory not found");
}

$files = [];
foreach ((new Finder())->files()->in($directory) as $file) {
$files[$file->getBasename()] = $file->getContents();
}

return $files;
}
}
68 changes: 68 additions & 0 deletions src/Schema/Sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# GraphQL schema sync

An experimental mechanism for publishing a compiled schema so that secondary servers can pull it and install it without recompiling their own containe.

## Configuration

### Redis
The feature requires Redis for publishing the latest schema timestamp (it is suggested in `composer.json`).

The feature comes with a default redis client service, `ibexa_graphql.sync.redis_client`.
It is configured using the following environment variables:
```.dotenv
REDIS_GRAPHQL_HOST=1.2.3.4
REDIS_GRAPHQL_PORT=6379
REDIS_GRAPHQL_DBINDEX=0
```

If you want to use your own client, you can redefined the service with the same name in your
project's services definitions.

If you already have your own and want to re-use it, create an alias with that name:
```yaml
# config/services.yaml
services:
ibexa_graphql.sync.redis_client: '@app.redis_client'
```

### AWS S3
Amazon S3 can be used to publish the schema files. To enable it, make sure that `aws/aws-sdk-php`
is installed on your project.

It uses a default client service named `ibexa_graphql.sync.s3_client`, based on the default
environment variables expected by the SDK:

```dotenv
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
```

If you already have an s3 client service, alias it to `ibexa_graphql.sync.s3_client`:
```yaml
# config/services.yaml
services:
ibexa_graphql.sync.s3_client: '@app.s3_client'
```

The feature also requires two extra settings for the bucket and the region:
```dotenv
GRAPHQL_SYNC_S3_BUCKET=ibexa-graphql
GRAPHQL_SYNC_S3_REGION=eu-west-1
```

## Usage
One server will do the schema generation + compiling, and run a command
(`ibexa:graphql:publish-schema`) to publish the schema. The command can be executed during a deployment process,
or manually.

Publishing will:

1. push the compiled schema (`%kernel.cache_dir%/overblog/graphql-bundle/__definitions__/*`) to a SharedSchema
2. set the published schema timestamp using a TimestampHandler.

Secondary servers, when a GraphQL query is executed (`UpdateSchemaIfNeeded` subscriber), will compare the timestamp to
theirs (modification time of `__classes.map`). If the remote schema is newer, it will be pulled and installed on shutdown.

Since the graphql schema types are compiled into the container as services, types that were added to the published schema
(new content types, etc) need to be registered on runtime. This is done by the `Schema\Sync\AddTypesSolutions` subscriber.
It checks which of the type classes do not have a solution in the current schema, and adds them to it.
Loading