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

Migrate to Swoole #171

Merged
merged 12 commits into from
Oct 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion APP_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.3
2.0.1
2 changes: 1 addition & 1 deletion bin/query
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ try {
'body' => '$query',
]
]);
\$task = QueryProcessor::process(\$request)->run(Task::createRuntime());
\$task = QueryProcessor::process(\$request)->run();
\$status = \$task->wait(true);
printf('Status code: %s' . PHP_EOL, \$status->name);
printf('Result: ' . PHP_EOL . '%s', \$task->getResult()->getStruct());
Expand Down
23 changes: 12 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
"react/socket": "^1.12",
"manticoresoftware/telemetry": "^0.1.9",
"symfony/dependency-injection": "^6.1",
"manticoresoftware/buddy-core": "^v0.1.0",
"manticoresoftware/buddy-plugin-empty-string": "^v0.1.0",
"manticoresoftware/buddy-plugin-backup": "^v0.1.0",
"manticoresoftware/buddy-plugin-emulate-elastic": "^v0.1.0",
"manticoresoftware/buddy-plugin-insert": "^v0.1.0",
"manticoresoftware/buddy-plugin-select": "^v0.1.0",
"manticoresoftware/buddy-plugin-show": "^v0.1.0",
"manticoresoftware/buddy-plugin-cli-table": "^v0.1.0",
"manticoresoftware/buddy-plugin-plugin": "^v0.1.0",
"manticoresoftware/buddy-plugin-test": "^v0.1.0",
"manticoresoftware/buddy-plugin-insert-mva": "^v0.1.0",
"manticoresoftware/buddy-core": "dev-main",
"manticoresoftware/buddy-plugin-empty-string": "dev-main",
"manticoresoftware/buddy-plugin-backup": "dev-main",
"manticoresoftware/buddy-plugin-emulate-elastic": "dev-main",
"manticoresoftware/buddy-plugin-insert": "dev-main",
"manticoresoftware/buddy-plugin-select": "dev-main",
"manticoresoftware/buddy-plugin-show": "dev-main",
"manticoresoftware/buddy-plugin-cli-table": "dev-main",
"manticoresoftware/buddy-plugin-plugin": "dev-main",
"manticoresoftware/buddy-plugin-test": "dev-main",
"manticoresoftware/buddy-plugin-insert-mva": "dev-main",
"manticoresoftware/buddy-plugin-create-table": "dev-main",
"php-ds/php-ds": "^1.4"
},
"require-dev": {
Expand Down
229 changes: 150 additions & 79 deletions composer.lock

Large diffs are not rendered by default.

49 changes: 17 additions & 32 deletions src/Lib/MetricThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

namespace Manticoresearch\Buddy\Base\Lib;

use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Tool\Buddy;
use Psr\Container\ContainerInterface;
use parallel\Channel;
use Swoole\Process;

// This is class that allows us to run separate thread for telemetry collection
/**
Expand All @@ -37,12 +36,7 @@ final class MetricThread {
// We set this on initialization (init.php) so we are sure we have it in class
protected static ContainerInterface $container;


/**
* @param Task $task
* @return void
*/
public function __construct(protected Task $task) {
public function __construct(public readonly Process $process) {
}

/**
Expand All @@ -65,7 +59,7 @@ public static function destroy(): void {
return;
}

static::$instance->task->destroy();
static::$instance->process->exit();
}

/**
Expand All @@ -87,36 +81,28 @@ public static function instance(): static {
* @return self
*/
public static function start(): self {
$task = Task::loopInRuntime(
Task::createRuntime(),
static function (Channel $ch, string $container) {
$container = unserialize($container);
/** @var ContainerInterface $container */
// This fix issue when we get "sh: 1: cd: can't cd to" error
// while running buddy inside directory that are not allowed for us
$process = new Process(
static function (Process $worker) {
chdir(sys_get_temp_dir());

Metric::setContainer($container);
Metric::setContainer(static::$container);
$metric = Metric::instance();
while ($msg = $ch->recv()) {

while ($msg = $worker->read()) {
if (!is_string($msg)) {
throw new \Exception('Incorrect data received');
}
$msg = unserialize($msg);
if (!is_array($msg)) {
throw new \Exception('Incorrect data received');
}
[$method, $args] = $msg;
$metric->$method(...$args);
}
}, [serialize(static::$container)]
}, true, 2
);

return (new self($task))->run();
}

/**
* @return self
*/
public function run(): self {
$this->task->run();
return $this;
$process->start();
return new self($process);
}

/**
Expand All @@ -129,9 +115,8 @@ public function run(): self {
* @return static
*/
public function execute(string $method, array $args = []): static {
$argsJson = json_encode($args);
Buddy::debug("metric: $method $argsJson");
$this->task->transmit([$method, $args]);
Buddy::debug("metric: $method " . json_encode($args));
$this->process->write(serialize([$method, $args]));
return $this;
}
}
19 changes: 8 additions & 11 deletions src/Lib/QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Exception;
use Manticoresearch\Buddy\Base\Exception\SQLQueryCommandNotSupported;
use Manticoresearch\Buddy\Base\Network\EventHandler;
use Manticoresearch\Buddy\Base\Sharding\Thread as ShardingThread;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client as HTTPClient;
use Manticoresearch\Buddy\Core\ManticoreSearch\Settings as ManticoreSettings;
use Manticoresearch\Buddy\Core\ManticoreSearch\Settings;
Expand Down Expand Up @@ -205,7 +205,6 @@ protected static function getHooks(): array {
'installed',
function () {
static::$extraPlugins = static::$pluggable->fetchExtraPlugins();
EventHandler::setShouldExit(true);
},
],
// Happens when we remove the plugin
Expand All @@ -214,18 +213,16 @@ function () {
'deleted',
function () {
static::$extraPlugins = static::$pluggable->fetchExtraPlugins();
EventHandler::setShouldExit(true);
},
],
// Happens when we run create table with shards in options
// TODO: uncomment and add plugin to composer.json
// [
// 'manticoresoftware/buddy-plugin-create-table',
// 'shard',
// function (array $args) {
// ShardingThread::instance()->execute('shard', $args);
// },
// ],
[
'manticoresoftware/buddy-plugin-create-table',
'shard',
function (array $args) {
ShardingThread::instance()->execute('shard', $args);
},
],
];
}
}
Loading