use of org.dcache.srm.util.JDC in project dcache by dCache.
the class AsynchronousSaveJobStorage method saveJob.
@Override
public void saveJob(final J job, final boolean force) {
UpdateState existingState;
if (force) {
existingState = states.put(job.getId(), UpdateState.QUEUED_FORCED);
} else {
existingState = states.putIfAbsent(job.getId(), UpdateState.QUEUED_NOT_FORCED);
while (existingState == UpdateState.PROCESSING && !states.replace(job.getId(), UpdateState.PROCESSING, UpdateState.QUEUED_NOT_FORCED)) {
existingState = states.putIfAbsent(job.getId(), UpdateState.QUEUED_NOT_FORCED);
}
}
if (existingState == null) {
boolean success = false;
Runnable task = new Runnable() {
@Override
public void run() {
try (JDC ignored = job.applyJdc()) {
UpdateState state = states.put(job.getId(), UpdateState.PROCESSING);
try {
storage.saveJob(job, state == UpdateState.QUEUED_FORCED);
} catch (TransactionException e) {
LOGGER.error("SQL statement failed: {}", e.getMessage());
} catch (Throwable e) {
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
} finally {
if (!states.remove(job.getId(), UpdateState.PROCESSING)) {
executor.execute(this);
}
}
}
}
};
try {
executor.execute(task);
success = true;
} catch (RejectedExecutionException e) {
if (force) {
// Execute forced save synchronously, thus creating back pressure.
task.run();
success = true;
} else {
LOGGER.warn("Persistence of request {} skipped, queue is too long.", job.getId());
}
} finally {
if (!success) {
states.remove(job.getId());
}
}
}
}
use of org.dcache.srm.util.JDC in project dcache by dCache.
the class SrmHandler method handleRequest.
public Object handleRequest(String requestName, Object request) throws RemoteException {
long startTimeStamp = System.currentTimeMillis();
// requestName values all start "srm". This is redundant, so may
// be removed when creating the session id. The initial character is
// converted to lowercase, so "srmPrepareToPut" becomes "prepareToPut".
String session = "srm2:" + Character.toLowerCase(requestName.charAt(3)) + requestName.substring(4);
try (JDC ignored = JDC.createSession(session)) {
for (RequestLogger logger : loggers) {
logger.request(requestName, request);
}
Subject user = Subject.getSubject(AccessController.getContext());
Object response;
if (requestName.equals("srmPing")) {
// Ping is special as it isn't authenticated and unable to return a failure
response = new SrmPingResponse("v2.2", pingExtraInfo);
} else {
try {
response = dispatch(user, requestName, request);
} catch (SRMInternalErrorException e) {
LOGGER.error(e.getMessage());
response = getFailedResponse(requestName, e.getStatusCode(), "Authentication failed (server log contains additional information).");
} catch (SRMAuthorizationException e) {
LOGGER.info(e.getMessage());
response = getFailedResponse(requestName, e.getStatusCode(), "Permission denied.");
} catch (SRMAuthenticationException e) {
LOGGER.warn(e.getMessage());
response = getFailedResponse(requestName, e.getStatusCode(), "Authentication failed (server log contains additional information).");
} catch (SRMException e) {
response = getFailedResponse(requestName, e.getStatusCode(), e.getMessage());
} catch (PermissionDeniedCacheException e) {
response = getFailedResponse(requestName, TStatusCode.SRM_AUTHORIZATION_FAILURE, e.getMessage());
} catch (CacheException e) {
response = getFailedResponse(requestName, TStatusCode.SRM_INTERNAL_ERROR, e.getMessage());
} catch (InterruptedException e) {
response = getFailedResponse(requestName, TStatusCode.SRM_FATAL_INTERNAL_ERROR, "Server shutdown.");
} catch (NoRouteToCellException e) {
LOGGER.error(e.getMessage());
response = getFailedResponse(requestName, TStatusCode.SRM_INTERNAL_ERROR, "SRM backend serving this request is currently offline.");
}
}
long time = System.currentTimeMillis() - startTimeStamp;
for (RequestLogger logger : loggers) {
logger.response(requestName, request, response, user, time);
}
return response;
}
}
use of org.dcache.srm.util.JDC in project dcache by dCache.
the class SrmPrepareToGet method srmPrepareToGet.
private SrmPrepareToGetResponse srmPrepareToGet() throws IllegalStateTransition, InterruptedException, SRMInvalidRequestException, SRMNotSupportedException, SRMInternalErrorException {
String[] protocols = getTransferProtocols(request);
String clientHost = getClientHost(request).orElse(this.clientHost);
long lifetime = Lifetimes.calculateLifetime(request.getDesiredTotalRequestTime(), configuration.getGetLifetime());
String[] supportedProtocols = storage.supportedGetProtocols();
URI[] surls = getSurls(request);
if (protocols != null && protocols.length > 0) {
boolean isAnyProtocolSupported = any(asList(protocols), in(asList(supportedProtocols)));
if (!isAnyProtocolSupported) {
throw new SRMNotSupportedException("Protocol(s) not supported: " + Arrays.toString(protocols));
}
}
GetRequest r = new GetRequest(srm.getSrmId(), user, surls, protocols, lifetime, configuration.getGetMaxPollPeriod(), request.getUserRequestDescription(), clientHost, isStagingAllowed());
try (JDC ignored = r.applyJdc()) {
srm.acceptNewJob(r);
return r.getSrmPrepareToGetResponse(configuration.getGetSwitchToAsynchronousModeDelay());
}
}
use of org.dcache.srm.util.JDC in project dcache by dCache.
the class SrmPrepareToPut method srmPrepareToPut.
private SrmPrepareToPutResponse srmPrepareToPut() throws IllegalStateTransition, InterruptedException, SRMNotSupportedException, SRMInvalidRequestException, SRMInternalErrorException {
checkFileStorageType(request, TFileStorageType.PERMANENT);
String[] protocols = getProtocols();
String clientHost = getClientHost(request).orElse(this.clientHost);
String spaceToken = request.getTargetSpaceToken();
TRetentionPolicy retentionPolicy = null;
TAccessLatency accessLatency = null;
if (request.getTargetFileRetentionPolicyInfo() != null) {
retentionPolicy = request.getTargetFileRetentionPolicyInfo().getRetentionPolicy();
accessLatency = request.getTargetFileRetentionPolicyInfo().getAccessLatency();
}
TPutFileRequest[] fileRequests = getFileRequests(request);
// assume transfers will take place in parallel
long effectiveSize = largestFileOf(fileRequests);
long lifetime = Lifetimes.calculateLifetime(request.getDesiredTotalRequestTime(), effectiveSize, configuration.getMaximumClientAssumedBandwidth(), configuration.getPutLifetime());
TOverwriteMode overwriteMode = getOverwriteMode(request);
String[] supportedProtocols = storage.supportedPutProtocols();
boolean isAnyProtocolSupported = any(asList(protocols), in(asList(supportedProtocols)));
if (!isAnyProtocolSupported) {
throw new SRMNotSupportedException("Protocol(s) not supported: " + Arrays.toString(protocols));
}
URI[] surls = new URI[fileRequests.length];
Long[] sizes = new Long[fileRequests.length];
boolean[] wantPermanent = new boolean[fileRequests.length];
for (int i = 0; i < fileRequests.length; ++i) {
TPutFileRequest fileRequest = fileRequests[i];
if (fileRequest == null) {
throw new SRMInvalidRequestException("file request #" + (i + 1) + " is null.");
}
if (fileRequest.getTargetSURL() == null) {
throw new SRMInvalidRequestException("surl of file request #" + (i + 1) + " is null.");
}
URI surl = URI.create(fileRequest.getTargetSURL().toString());
UnsignedLong knownSize = fileRequest.getExpectedFileSize();
if (knownSize != null) {
sizes[i] = knownSize.longValue();
if (sizes[i] < 0) {
throw new SRMInvalidRequestException("Negative file size is not allowed.");
}
}
// for now, extract type info from space token in the future
wantPermanent[i] = true;
/* nextRequest.getFileStorageType()==
TFileStorageType.PERMANENT;*/
surls[i] = surl;
}
PutRequest r = new PutRequest(srm.getSrmId(), user, surls, sizes, wantPermanent, protocols, lifetime, configuration.getPutMaxPollPeriod(), clientHost, spaceToken, retentionPolicy, accessLatency, request.getUserRequestDescription());
try (JDC ignored = r.applyJdc()) {
if (overwriteMode != null) {
r.setOverwriteMode(overwriteMode);
}
srm.acceptNewJob(r);
// Return the request status
return r.getSrmPrepareToPutResponse(configuration.getPutSwitchToAsynchronousModeDelay());
}
}
use of org.dcache.srm.util.JDC in project dcache by dCache.
the class SrmStatusOfCopyRequest method srmStatusOfCopyRequest.
private SrmStatusOfCopyRequestResponse srmStatusOfCopyRequest() throws SRMInvalidRequestException {
CopyRequest copyRequest = Request.getRequest(request.getRequestToken(), CopyRequest.class);
try (JDC ignored = copyRequest.applyJdc()) {
if (request.getArrayOfSourceSURLs() == null || request.getArrayOfTargetSURLs() == null) {
return copyRequest.getSrmStatusOfCopyRequest();
}
URI[] fromsurls = request.getArrayOfSourceSURLs().getUrlArray();
URI[] tosurls = request.getArrayOfTargetSURLs().getUrlArray();
if (fromsurls.length == 0 || tosurls.length == 0) {
return copyRequest.getSrmStatusOfCopyRequest();
}
if (tosurls.length != fromsurls.length) {
throw new SRMInvalidRequestException("Length of arrayOfSourceSURLs and arrayOfTargetSURLs differ.");
}
return copyRequest.getSrmStatusOfCopyRequest(fromsurls, tosurls);
}
}
Aggregations