use of org.dcache.srm.v2_2.SrmPrepareToPutResponse in project dcache by dCache.
the class PutRequest method getSrmPrepareToPutResponse.
/**
* Waits for up to timeout milliseconds for the request to reach a non-queued state and then
* returns the current SrmPrepareToPutResponse for this PutRequest.
*/
public final SrmPrepareToPutResponse getSrmPrepareToPutResponse(long timeout) throws InterruptedException, SRMInvalidRequestException {
/* To avoid a race condition between us querying the current
* response and us waiting for a state change notification,
* the notification scheme is counter based. This guarantees
* that we do not loose any notifications. A simple lock
* around the whole loop would not have worked, as the call to
* getSrmPrepareToPutResponse may itself trigger a state
* change and thus cause a deadlock when the state change is
* signaled.
*/
Date deadline = getDateRelativeToNow(timeout);
int counter = _stateChangeCounter.get();
tryToReady();
SrmPrepareToPutResponse response = getSrmPrepareToPutResponse();
while (response.getReturnStatus().getStatusCode().isProcessing() && deadline.after(new Date()) && _stateChangeCounter.awaitChangeUntil(counter, deadline)) {
counter = _stateChangeCounter.get();
tryToReady();
response = getSrmPrepareToPutResponse();
}
response.setRemainingTotalRequestTime(getRemainingLifetimeIn(TimeUnit.SECONDS));
return response;
}
use of org.dcache.srm.v2_2.SrmPrepareToPutResponse 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.v2_2.SrmPrepareToPutResponse in project dcache by dCache.
the class SrmPrepareToPut method getFailedResponse.
public static final SrmPrepareToPutResponse getFailedResponse(String error, TStatusCode statusCode) {
SrmPrepareToPutResponse srmPrepareToPutResponse = new SrmPrepareToPutResponse();
srmPrepareToPutResponse.setReturnStatus(new TReturnStatus(statusCode, error));
return srmPrepareToPutResponse;
}
use of org.dcache.srm.v2_2.SrmPrepareToPutResponse in project dcache by dCache.
the class RemoteTurlPutterV2 method getInitialRequest.
@Override
public void getInitialRequest() throws SRMException {
if (number_of_file_reqs == 0) {
LOGGER.debug("number_of_file_reqs is 0, nothing to do");
return;
}
try {
java.net.URI srmUrl = URIs.createWithDefaultPort(SURLs[0]);
srmv2 = new SRMClientV2(srmUrl, Optional.of(credential.getDelegatedCredential()), Optional.empty(), retry_timout, retry_num, false, false, caCertificatePath, transport);
int len = SURLs.length;
TPutFileRequest[] fileRequests = new TPutFileRequest[len];
for (int i = 0; i < len; ++i) {
URI uri = new URI(SURLs[i]);
fileRequests[i] = new TPutFileRequest();
fileRequests[i].setTargetSURL(uri);
pendingSurlsToIndex.put(SURLs[i], i);
}
SrmPrepareToPutRequest srmPrepareToPutRequest = new SrmPrepareToPutRequest();
if (retentionPolicy != null || accessLatency != null) {
TRetentionPolicyInfo retentionPolicyInfo = new TRetentionPolicyInfo();
retentionPolicyInfo.setRetentionPolicy(retentionPolicy);
retentionPolicyInfo.setAccessLatency(accessLatency);
srmPrepareToPutRequest.setTargetFileRetentionPolicyInfo(retentionPolicyInfo);
}
TTransferParameters transferParameters = new TTransferParameters();
transferParameters.setAccessPattern(TAccessPattern.TRANSFER_MODE);
transferParameters.setConnectionType(TConnectionType.WAN);
transferParameters.setArrayOfTransferProtocols(new ArrayOfString(protocols));
srmPrepareToPutRequest.setTransferParameters(transferParameters);
srmPrepareToPutRequest.setArrayOfFileRequests(new ArrayOfTPutFileRequest(fileRequests));
srmPrepareToPutRequest.setDesiredFileStorageType(storageType);
srmPrepareToPutRequest.setDesiredTotalRequestTime((int) requestLifetime);
srmPrepareToPutRequest.setOverwriteOption(overwriteMode);
srmPrepareToPutRequest.setTargetSpaceToken(targetSpaceToken);
srmPrepareToPutResponse = srmv2.srmPrepareToPut(srmPrepareToPutRequest);
} catch (URISyntaxException | IOException | InterruptedException | ServiceException e) {
LOGGER.error("failed to connect to {} {}", SURLs[0], e.getMessage());
throw new SRMException("failed to connect to " + SURLs[0], e);
}
}
use of org.dcache.srm.v2_2.SrmPrepareToPutResponse in project dcache by dCache.
the class RemoteTurlPutterV2 method run.
@Override
public void run() {
if (number_of_file_reqs == 0) {
LOGGER.debug("number_of_file_reqs is 0, nothing to do");
return;
}
try {
int len = SURLs.length;
if (srmPrepareToPutResponse == null) {
throw new IOException(" null srmPrepareToPutResponse");
}
TReturnStatus status = srmPrepareToPutResponse.getReturnStatus();
if (status == null) {
throw new IOException(" null return status");
}
TStatusCode statusCode = status.getStatusCode();
if (statusCode == null) {
throw new IOException(" null status code");
}
if (RequestStatusTool.isFailedRequestStatus(status)) {
throw new IOException("srmPrepareToPut submission failed, unexpected or failed status : " + statusCode + " explanation=" + status.getExplanation());
}
requestToken = srmPrepareToPutResponse.getRequestToken();
LOGGER.debug(" srm returned requestToken = {} one of remote surls = {}", requestToken, SURLs[0]);
ArrayOfTPutRequestFileStatus arrayOfTPutRequestFileStatus = srmPrepareToPutResponse.getArrayOfFileStatuses();
if (arrayOfTPutRequestFileStatus == null) {
throw new IOException("returned PutRequestFileStatuses is an empty array");
}
TPutRequestFileStatus[] putRequestFileStatuses = arrayOfTPutRequestFileStatus.getStatusArray();
if (putRequestFileStatuses == null) {
throw new IOException("returned PutRequestFileStatuses is an empty array");
}
if (putRequestFileStatuses.length != len) {
throw new IOException("incorrect number of GetRequestFileStatuses" + "in RequestStatus expected " + len + " received " + putRequestFileStatuses.length);
}
boolean haveCompletedFileRequests = false;
while (!pendingSurlsToIndex.isEmpty()) {
long estimatedWaitInSeconds = Integer.MAX_VALUE;
for (TPutRequestFileStatus putRequestFileStatus : putRequestFileStatuses) {
URI surl = putRequestFileStatus.getSURL();
if (surl == null) {
LOGGER.error("invalid putRequestFileStatus, surl is null");
continue;
}
String surl_string = surl.toString();
if (!pendingSurlsToIndex.containsKey(surl_string)) {
LOGGER.error("invalid putRequestFileStatus, surl = {} not found", surl_string);
continue;
}
TReturnStatus fileStatus = putRequestFileStatus.getStatus();
if (fileStatus == null) {
throw new IOException(" null file return status");
}
TStatusCode fileStatusCode = fileStatus.getStatusCode();
if (fileStatusCode == null) {
throw new IOException(" null file status code");
}
if (RequestStatusTool.isFailedFileRequestStatus(fileStatus)) {
String error = "retreval of surl " + surl_string + " failed, status = " + fileStatusCode + " explanation=" + fileStatus.getExplanation();
LOGGER.error(error);
int indx = pendingSurlsToIndex.remove(surl_string);
notifyOfFailure(SURLs[indx], error, requestToken, null);
haveCompletedFileRequests = true;
continue;
}
if (putRequestFileStatus.getTransferURL() != null) {
String turl = putRequestFileStatus.getTransferURL().toString();
int indx = pendingSurlsToIndex.remove(surl_string);
// in case of put we do not need the size from the destination
notifyOfTURL(SURLs[indx], turl, requestToken, null, null);
continue;
}
if (putRequestFileStatus.getEstimatedWaitTime() != null && putRequestFileStatus.getEstimatedWaitTime() < estimatedWaitInSeconds && putRequestFileStatus.getEstimatedWaitTime() >= 1) {
estimatedWaitInSeconds = putRequestFileStatus.getEstimatedWaitTime();
}
}
if (pendingSurlsToIndex.isEmpty()) {
LOGGER.debug("no more pending transfers, breaking the loop");
break;
}
// do not wait longer then 60 seconds
if (estimatedWaitInSeconds > 60) {
estimatedWaitInSeconds = 60;
}
try {
LOGGER.debug("sleeping {} seconds ...", estimatedWaitInSeconds);
Thread.sleep(estimatedWaitInSeconds * 1000);
} catch (InterruptedException ie) {
}
SrmStatusOfPutRequestRequest srmStatusOfPutRequestRequest = new SrmStatusOfPutRequestRequest();
srmStatusOfPutRequestRequest.setRequestToken(requestToken);
// if we do not have completed file requests
// we want to get status for all files
// we do not need to specify any surls
int expectedResponseLength;
if (haveCompletedFileRequests) {
String[] pendingSurlStrings = pendingSurlsToIndex.keySet().toArray(String[]::new);
expectedResponseLength = pendingSurlStrings.length;
URI[] surlArray = new URI[expectedResponseLength];
for (int i = 0; i < expectedResponseLength; ++i) {
URI uri = new URI(pendingSurlStrings[i]);
surlArray[i] = uri;
}
srmStatusOfPutRequestRequest.setArrayOfTargetSURLs(new ArrayOfAnyURI(surlArray));
} else {
expectedResponseLength = SURLs.length;
URI[] surlArray = new URI[expectedResponseLength];
for (int i = 0; i < expectedResponseLength; ++i) {
URI surl = new URI(SURLs[i]);
surlArray[i] = surl;
}
srmStatusOfPutRequestRequest.setArrayOfTargetSURLs(new ArrayOfAnyURI(surlArray));
}
SrmStatusOfPutRequestResponse srmStatusOfPutRequestResponse = srmv2.srmStatusOfPutRequest(srmStatusOfPutRequestRequest);
if (srmStatusOfPutRequestResponse == null) {
throw new IOException(" null srmStatusOfPutRequestResponse");
}
arrayOfTPutRequestFileStatus = srmStatusOfPutRequestResponse.getArrayOfFileStatuses();
if (arrayOfTPutRequestFileStatus == null) {
throw new IOException("incorrect number of RequestFileStatuses");
}
putRequestFileStatuses = arrayOfTPutRequestFileStatus.getStatusArray();
if (putRequestFileStatuses == null || putRequestFileStatuses.length != expectedResponseLength) {
LOGGER.error("incorrect number of RequestFileStatuses");
throw new IOException("incorrect number of RequestFileStatuses");
}
status = srmStatusOfPutRequestResponse.getReturnStatus();
if (status == null) {
throw new IOException(" null return status");
}
statusCode = status.getStatusCode();
if (statusCode == null) {
throw new IOException(" null status code");
}
if (RequestStatusTool.isFailedRequestStatus(status)) {
throw new IOException("srmPrepareToPut update failed, unexpected or failed status : " + statusCode + " explanation=" + status.getExplanation());
}
}
} catch (IOException ioe) {
LOGGER.error(ioe.toString());
notifyOfFailure(ioe);
}
}
Aggregations