Skip to content

Commit

Permalink
dcache-frontend: add support for qos policies (qos rule engine 8)
Browse files Browse the repository at this point in the history
Motivation:

Implement the rule engine extension to QoS services.

Modification:

Adds a REST resources for querying, adding
and removing policies, and for getting the
summary of file policy states or the policy
info of individual files.

Result:

The Rule engine extensions to QoS are now
complete.

Target: master
Patch: https://rb.dcache.org/r/14076
Depends-on: #14075
Acked-by: Tigran
  • Loading branch information
alrossi committed Sep 6, 2023
1 parent 13580d9 commit 8cb60b9
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 3 deletions.
Expand Up @@ -17,8 +17,10 @@
import org.dcache.restful.resources.migration.MigrationResources;
import org.dcache.restful.resources.namespace.FileResources;
import org.dcache.restful.resources.namespace.IdResources;
import org.dcache.restful.resources.namespace.QosPolicyResource;
import org.dcache.restful.resources.pool.PoolGroupInfoResources;
import org.dcache.restful.resources.pool.PoolInfoResources;
import org.dcache.restful.resources.qos.QoSPolicyResource;
import org.dcache.restful.resources.quota.QuotaResources;
import org.dcache.restful.resources.restores.RestoreResources;
import org.dcache.restful.resources.selection.LinkResources;
Expand All @@ -27,10 +29,10 @@
import org.dcache.restful.resources.selection.UnitResources;
import org.dcache.restful.resources.space.SpaceManagerResources;
import org.dcache.restful.resources.srr.SrrResource;
import org.dcache.restful.resources.transfers.TransferResources;
import org.dcache.restful.resources.tape.ArchiveInfoResources;
import org.dcache.restful.resources.tape.ReleaseResources;
import org.dcache.restful.resources.tape.StageResources;
import org.dcache.restful.resources.transfers.TransferResources;
import org.dcache.restful.util.RequestUser;
import org.glassfish.jersey.message.filtering.EntityFilteringFeature;
import org.glassfish.jersey.server.ResourceConfig;
Expand All @@ -51,8 +53,10 @@ public DcacheRestApplication() {
register(LabelsResources.class);
register(FileResources.class);
register(IdResources.class);
register(QosPolicyResource.class);
register(PoolGroupInfoResources.class);
register(PoolInfoResources.class);
register(QoSPolicyResource.class);
register(QuotaResources.class);
register(RestoreResources.class);
register(LinkResources.class);
Expand Down
Expand Up @@ -444,6 +444,8 @@ public Response cmrResources(
break;
case "qos":
String targetQos = reqPayload.getString("target");
String qosPolicy = reqPayload.getString("policy");
Integer qosState = (Integer)reqPayload.get("state");
Subject subject = RequestUser.isAdmin() ? Subjects.ROOT : RequestUser.getSubject();
if (!useQosService) {
new QoSTransitionEngine(poolmanager,
Expand All @@ -460,7 +462,8 @@ public Response cmrResources(
= pnfsHandler.getFileAttributes(path.toString(),
NamespaceUtils.getRequestedAttributes(false, false,
true, false, true));
FileQoSRequirements requirements = getBasicRequirements(targetQos, attr);
FileQoSRequirements requirements = getBasicRequirements(targetQos,
qosPolicy, qosState, attr);
RemoteQoSRequirementsClient client = new RemoteQoSRequirementsClient();
client.setRequirementsService(qosEngine);
client.fileQoSRequirementsModified(requirements, subject);
Expand Down Expand Up @@ -626,10 +629,17 @@ public void setUseQosService(boolean useQosService) {
this.useQosService = useQosService;
}

private FileQoSRequirements getBasicRequirements(String targetQos, FileAttributes attributes) {
private FileQoSRequirements getBasicRequirements(String targetQos, String qosPolicy,
Integer qosState, FileAttributes attributes) {
FileQoSRequirements requirements = new FileQoSRequirements(attributes.getPnfsId(),
attributes);

if (qosPolicy != null) {
requirements.setRequiredQoSPolicy(qosPolicy);
requirements.setRequiredQoSStateIndex( qosState == null ? 0 : qosState);
return requirements;
}

if (targetQos == null) {
throw new IllegalArgumentException("no target qos given.");
}
Expand Down
@@ -0,0 +1,217 @@
/*
COPYRIGHT STATUS:
Dec 1st 2001, Fermi National Accelerator Laboratory (FNAL) documents and
software are sponsored by the U.S. Department of Energy under Contract No.
DE-AC02-76CH03000. Therefore, the U.S. Government retains a world-wide
non-exclusive, royalty-free license to publish or reproduce these documents
and software for U.S. Government purposes. All documents and software
available from this server are protected under the U.S. and Foreign
Copyright Laws, and FNAL reserves all rights.
Distribution of the software available from this server is free of
charge subject to the user following the terms of the Fermitools
Software Legal Information.
Redistribution and/or modification of the software shall be accompanied
by the Fermitools Software Legal Information (including the copyright
notice).
The user is asked to feed back problems, benefits, and/or suggestions
about the software to the Fermilab Software Providers.
Neither the name of Fermilab, the URA, nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
DISCLAIMER OF LIABILITY (BSD):
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FERMILAB,
OR THE URA, OR THE U.S. DEPARTMENT of ENERGY, OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Liabilities of the Government:
This software is provided by URA, independent from its Prime Contract
with the U.S. Department of Energy. URA is acting independently from
the Government and in its own private capacity and is not acting on
behalf of the U.S. Government, nor as its contractor nor its agent.
Correspondingly, it is understood and agreed that the U.S. Government
has no connection to this software and in no manner whatsoever shall
be liable for nor assume any responsibility or obligation for any claim,
cost, or damages arising out of or resulting from the use of the software
available from this server.
Export Control:
All documents and software available from this server are subject to U.S.
export control laws. Anyone downloading information from this server is
obligated to secure any necessary Government licenses before exporting
documents or software obtained from this server.
*/
package org.dcache.restful.resources.namespace;

import diskCacheV111.util.CacheException;
import diskCacheV111.util.FsPath;
import diskCacheV111.util.PnfsHandler;
import diskCacheV111.util.PnfsId;
import dmg.util.Exceptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.dcache.cells.CellStub;
import org.dcache.http.PathMapper;
import org.dcache.qos.QoSPolicyStat;
import org.dcache.qos.data.FileQosPolicyInfo;
import org.dcache.restful.util.HandlerBuilders;
import org.dcache.vehicles.qos.FileQoSPolicyInfoMessage;
import org.dcache.vehicles.qos.PnfsManagerGetQoSPolicyStatsMessage;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* <p>RESTful API to which allows one to get information about a file's QoS policy and state,
* as well as current statistics on the number of files in each policy state.</p>
*
* @version v1.0
*/
@Component
@Api(value = "namespace", authorizations = {@Authorization("basicAuth")})
@Path("/qos-policy")
public class QosPolicyResource {

private static final Logger LOGGER = LoggerFactory.getLogger(
QosPolicyResource.class);

@Context
private HttpServletRequest request;

@Inject
@Named("pnfs-stub")
private CellStub pnfsmanager;

@Inject
private PathMapper pathMapper;

@Inject
@Named("qos-engine")
private CellStub qosEngine;

@GET
@ApiOperation(value = "Retrieve the current count of files in the namespace by policy and state.")
@ApiResponses({
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Internal Server Error"),
})
@Path("stats")
@Produces(MediaType.APPLICATION_JSON)
public List<QoSPolicyStat> getPolicyStats() {
PnfsManagerGetQoSPolicyStatsMessage message = new PnfsManagerGetQoSPolicyStatsMessage();

try {
message = pnfsmanager.sendAndWait(message);
} catch (CacheException e) {
throw new BadRequestException(e);
} catch (JSONException | IllegalArgumentException e) {
throw new BadRequestException(e);
} catch (Exception e) {
LOGGER.warn(Exceptions.meaningfulMessage(e));
throw new InternalServerErrorException(e);
}

return message.getPolicyStats();
}

@GET
@ApiOperation(value = "Retrieve the QoSPolicy name and status for this file pnfsid.")
@ApiResponses({
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Internal Server Error"),
})
@Path("id/{id}")
@Produces(MediaType.APPLICATION_JSON)
public FileQosPolicyInfo getPolicyInfoForPnfsId(@ApiParam("The pnfsid of the file for which to retrieve policy info.")
@PathParam("id")String id) {
FileQoSPolicyInfoMessage message = new FileQoSPolicyInfoMessage(new PnfsId(id));

try {
message = qosEngine.sendAndWait(message);
} catch (CacheException e) {
throw new BadRequestException(e);
} catch (JSONException | IllegalArgumentException e) {
throw new BadRequestException(e);
} catch (Exception e) {
LOGGER.warn(Exceptions.meaningfulMessage(e));
throw new InternalServerErrorException(e);
}

return message.getQosPolicyInfo();
}

@GET
@ApiOperation(value = "Retrieve the QoSPolicy name and status for this file path.")
@ApiResponses({
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Internal Server Error"),
})
@Path("path/{path : .*}")
@Produces(MediaType.APPLICATION_JSON)
public FileQosPolicyInfo getPolicyInfoForPath(@ApiParam("The path of the file for which to retrieve policy info.")
@PathParam("path")String requestPath) {
FileQoSPolicyInfoMessage message;
PnfsHandler handler = HandlerBuilders.unrestrictedPnfsHandler(pnfsmanager);
FsPath path;
try {
path = pathMapper.asDcachePath(request, requestPath, ForbiddenException::new, handler);
message = new FileQoSPolicyInfoMessage(path);
message = qosEngine.sendAndWait(message);
} catch (CacheException e) {
throw new BadRequestException(e);
} catch (JSONException | IllegalArgumentException e) {
throw new BadRequestException(e);
} catch (Exception e) {
LOGGER.warn(Exceptions.meaningfulMessage(e));
throw new InternalServerErrorException(e);
}

FileQosPolicyInfo stat = message.getQosPolicyInfo();
stat.setPath(path);
return stat;
}

}

0 comments on commit 8cb60b9

Please sign in to comment.