Skip to content

Commit

Permalink
fix object meta search, add object delete, fix routing redirect, updt…
Browse files Browse the repository at this point in the history
… nginx config
  • Loading branch information
killua-eu committed Feb 24, 2024
1 parent 7bd9b61 commit 648b2d8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- migrate:up

ALTER TABLE `t_stor_objects_replicas`
ADD `desired` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Object is desired on device (1 = desired, 0 = not desired). By default the desired state is set to 1, in case that the object is to be deleted, this is to be set to 0. Objects marked with 0 are to be expunged (and once this happens, the particular line in this table deleted)';

-- migrate:down

ALTER TABLE `t_stor_objects_replicas`
DROP `desired`;
1 change: 1 addition & 0 deletions glued/Config/Nginx/sites-available/glued-stor
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ server {
root /var/www/html/glued-stor/public;
listen 8008 ssl default_server;
listen [::]:8008 ssl default_server;
http2 on;
include snippets/server/common.conf;

##########################
Expand Down
1 change: 1 addition & 0 deletions glued/Config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ routes:
pattern: /api/stor
label: stor
dscr: A routegroup, no methods here.
provides: ingress

#########################################################
# STOR / HEALTH #
Expand Down
64 changes: 56 additions & 8 deletions glued/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use Exception;
use mysqli_sql_exception;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Nyholm\Psr7\Factory\Psr17Factory;
use Ramsey\Uuid\Uuid;



class ServiceController extends AbstractController
{

Expand Down Expand Up @@ -702,8 +701,9 @@ public function objects_r1(Request $request, Response $response, array $args = [
foreach ($qp as $k=>$v) {
$kp = explode('.', str_replace('_', '.', $k), 2);
if ($kp[0] != 'meta') { continue; }
$path = $kp[1] ?? '';
$wm .= "AND CAST(JSON_EXTRACT(om.data, ?) as CHAR) = ?";
$patharr = explode('.', $kp[1] ?? '');
$path = '"'.implode('"."',$patharr).'"';
$wm .= 'AND CAST(JSON_UNQUOTE(JSON_EXTRACT(om.data, ?)) as CHAR) = ?';
$pa[] = "$.{$path}";
$pa[] = $v;
}
Expand Down Expand Up @@ -752,12 +752,59 @@ public function objects_r1(Request $request, Response $response, array $args = [

public function objects_d1(Request $request, Response $response, array $args = []): Response
{
$candidate = false;
if (!array_key_exists('bucket', $args)) { throw new Exception('Bucket UUID missing in request uri `/api/stor/v1/buckets/{bucket}/objects/{object}/{element}`.', 400); }
if (!array_key_exists('object', $args)) { throw new Exception('Object UUID missing in request uri `/api/stor/v1/buckets/{bucket}/objects/{object}/{element}`.', 400); }
if (!array_key_exists('element', $args)) { throw new Exception('Element UUID missing in request uri `/api/stor/v1/buckets/{bucket}/objects/{object}/{element}`.', 400); }
if ($args['element'] == 'refs') {
//if (!array_key_exists('element', $args)) { throw new Exception('Element UUID missing in request uri `/api/stor/v1/buckets/{bucket}/objects/{object}/{element}`.', 400); }

if (!isset($args['element'])) {
$pa[] = $args['bucket'];
$pa[] = $args['object'];
$q = "
SELECT
bin_to_uuid(o.`bucket`,1) AS `bucket`,
HEX(o.`hash`) AS `hash`,
bin_to_uuid(o.`object`,1) AS `object`,
f.c_size as size,
f.c_mime as mime,
o.name as name,
f.c_ext as ext,
o.ts_created as created,
bin_to_uuid(bdgs.dg,1) as bucket_dg,
status.dev_uuid as bucket_dev
FROM `t_stor_objects` o
LEFT JOIN t_stor_files f ON f.c_hash = o.hash
LEFT JOIN t_stor_bucket_dgs bdgs ON bdgs.bucket = o.bucket
LEFT JOIN v_stor_status status ON bdgs.dg = uuid_to_bin(status.dg_uuid,1)
WHERE o.bucket = uuid_to_bin(? ,1) AND o.object = uuid_to_bin(? ,1)
";

$handle = $this->mysqli->execute_query($q, $pa);
$candidate = $handle->fetch_all(MYSQLI_ASSOC) ?? [];
if (!$candidate) { throw new \Exception("Trying to delete {$args['object']} object that doesn't exist"); };

$this->mysqli->begin_transaction();
$q1 = "delete from t_stor_objects where object = uuid_to_bin(?,1)";
$this->mysqli->execute_query($q1, [$args['object']]);
$q2 = "delete from t_stor_objects_meta where uuid = uuid_to_bin(?,1)";
$this->mysqli->execute_query($q2, [$args['object']]);
$q3 = "delete from t_stor_objects_refs where obj = uuid_to_bin(?,1) or ref_val = uuid_to_bin(?, 1)";
$this->mysqli->execute_query($q3, [$args['object'],$args['object']]);
$q4 = "INSERT INTO t_stor_objects_replicas (object, device, desired) VALUES (uuid_to_bin(?, 1), uuid_to_bin(?, 1), 0) ON DUPLICATE KEY UPDATE desired = VALUES(desired)";
foreach ($candidate as $replica) {
$this->mysqli->execute_query($q4, [$replica['object'],$replica['bucket_dev']]);
}
$this->mysqli->commit();
$data['status'] = 200;
$data['message'] = "Deleted object {$args['object']}, queued up for expunge.";
return $response->withJson($data);


} elseif ($args['element'] == 'refs') {
$body = $request->getParsedBody();
if (is_null($body)) { throw new \Exception('Request body must be a valid json', 400); }
if (is_null($body)) {
throw new \Exception('Request body must be a valid json', 400);
}
$this->object_refs($args['object'], $body, 'del');
$data = [
'timestamp' => microtime(),
Expand All @@ -767,8 +814,9 @@ public function objects_d1(Request $request, Response $response, array $args = [
];
return $response->withJson($data)->withStatus(200);
} else {
throw new Exception('DELETE request supported elements are: `refs`.', 400);
throw new Exception('DELETE request supported elements are: `refs` and NULL.', 400);
}

}


Expand Down
18 changes: 15 additions & 3 deletions glued/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@
// in TrailingSlash controls the inclusion of the port the Location header
// of the redirect. Not including the port is wanted as this microservice
// is supposed to run behind the nginx+glued-core as its auth proxy, otherwise
// users would get redirected directly to the backend port.
// TODO: fix glued-lib trailingSlash to do only path-based redirects (no full url)
$trailingSlash = new TrailingSlash(false, false);
// users would get redirected directly to the backend port. Finally, the third
// parameter is populated by all routes marked with providing `ingress`.
// These routes are considered the entry points to the api and in regard to
// the frontend/backend setup of the microservices, the trailing slash
// should not be removed on ingress path.

$ingressPaths = array_filter($settings['routes'], function ($route) {
return isset($route['provides']) && $route['provides'] === 'ingress';
});
$ingressPaths = array_map(function ($route) {
return rtrim($route['path'], '/') . '/'; // Append a '/' if the path does not already end with one
}, $ingressPaths);
$ingressPaths = array_values($ingressPaths);

$trailingSlash = new TrailingSlash(false, false, $ingressPaths);
$trailingSlash->redirect();
$app->add($trailingSlash);

Expand Down

0 comments on commit 648b2d8

Please sign in to comment.