Search in sources :

Example 1 with JDC

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());
            }
        }
    }
}
Also used : TransactionException(org.springframework.transaction.TransactionException) JDC(org.dcache.srm.util.JDC) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with JDC

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;
    }
}
Also used : SRMAuthorizationException(org.dcache.srm.SRMAuthorizationException) CacheException(diskCacheV111.util.CacheException) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) JDC(org.dcache.srm.util.JDC) ArrayOfString(org.dcache.srm.v2_2.ArrayOfString) SrmPingResponse(org.dcache.srm.v2_2.SrmPingResponse) Subject(javax.security.auth.Subject) SRMInternalErrorException(org.dcache.srm.SRMInternalErrorException) SRMAuthenticationException(org.dcache.srm.SRMAuthenticationException) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) SRMException(org.dcache.srm.SRMException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException)

Example 3 with JDC

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());
    }
}
Also used : SRMNotSupportedException(org.dcache.srm.SRMNotSupportedException) GetRequest(org.dcache.srm.request.GetRequest) SrmPrepareToGetRequest(org.dcache.srm.v2_2.SrmPrepareToGetRequest) JDC(org.dcache.srm.util.JDC) URI(java.net.URI)

Example 4 with JDC

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());
    }
}
Also used : SRMNotSupportedException(org.dcache.srm.SRMNotSupportedException) UnsignedLong(org.apache.axis.types.UnsignedLong) TRetentionPolicy(org.dcache.srm.v2_2.TRetentionPolicy) JDC(org.dcache.srm.util.JDC) PutRequest(org.dcache.srm.request.PutRequest) SrmPrepareToPutRequest(org.dcache.srm.v2_2.SrmPrepareToPutRequest) TOverwriteMode(org.dcache.srm.v2_2.TOverwriteMode) TPutFileRequest(org.dcache.srm.v2_2.TPutFileRequest) URI(java.net.URI) UnsignedLong(org.apache.axis.types.UnsignedLong) TAccessLatency(org.dcache.srm.v2_2.TAccessLatency) SRMInvalidRequestException(org.dcache.srm.SRMInvalidRequestException)

Example 5 with JDC

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);
    }
}
Also used : CopyRequest(org.dcache.srm.request.CopyRequest) JDC(org.dcache.srm.util.JDC) URI(org.apache.axis.types.URI) SRMInvalidRequestException(org.dcache.srm.SRMInvalidRequestException)

Aggregations

JDC (org.dcache.srm.util.JDC)18 URI (java.net.URI)7 SRMInvalidRequestException (org.dcache.srm.SRMInvalidRequestException)6 URI (org.apache.axis.types.URI)4 SRMInternalErrorException (org.dcache.srm.SRMInternalErrorException)4 SRMAuthorizationException (org.dcache.srm.SRMAuthorizationException)3 SRMNotSupportedException (org.dcache.srm.SRMNotSupportedException)3 GetRequest (org.dcache.srm.request.GetRequest)3 PutRequest (org.dcache.srm.request.PutRequest)3 IllegalStateTransition (org.dcache.srm.scheduler.IllegalStateTransition)3 ArrayOfAnyURI (org.dcache.srm.v2_2.ArrayOfAnyURI)3 ArrayOfTSURLReturnStatus (org.dcache.srm.v2_2.ArrayOfTSURLReturnStatus)3 TReturnStatus (org.dcache.srm.v2_2.TReturnStatus)3 TSURLReturnStatus (org.dcache.srm.v2_2.TSURLReturnStatus)3 BringOnlineRequest (org.dcache.srm.request.BringOnlineRequest)2 CopyRequest (org.dcache.srm.request.CopyRequest)2 TAccessLatency (org.dcache.srm.v2_2.TAccessLatency)2 TOverwriteMode (org.dcache.srm.v2_2.TOverwriteMode)2 TRetentionPolicy (org.dcache.srm.v2_2.TRetentionPolicy)2 CacheException (diskCacheV111.util.CacheException)1