Search in sources :

Example 1 with OSDData

use of com.pspace.ifs.ksan.osd.OSDData in project ksan by infinistor.

the class OSDClient method receiveData.

private OSDData receiveData() throws IOException {
    DataInputStream si = new DataInputStream(socket.getInputStream());
    int size = si.readInt();
    byte[] buffer = new byte[size];
    si.read(buffer, 0, size);
    String result = new String(buffer, 0, size);
    String[] ArrayResult = result.split(GWConstants.COLON, -1);
    OSDData data = new OSDData();
    switch(ArrayResult[0]) {
        case OSDConstants.FILE:
            data.setETag(ArrayResult[1]);
            data.setFileSize(Long.parseLong(ArrayResult[2]));
            return data;
        default:
            logger.error(GWConstants.LOG_OSDCLIENT_UNKNOWN_RESULT, ArrayResult[1]);
    }
    return null;
}
Also used : OSDData(com.pspace.ifs.ksan.osd.OSDData) DataInputStream(java.io.DataInputStream)

Example 2 with OSDData

use of com.pspace.ifs.ksan.osd.OSDData in project ksan by infinistor.

the class S3ObjectOperation method uploadPartCopyLocal.

private OSDData uploadPartCopyLocal(String srcPath, String srcObjId, String srcVersionId, String copySourceRange, String path, String objId, String partNo) throws IOException, NoSuchAlgorithmException {
    OSDData osdData = new OSDData();
    MessageDigest md5er = MessageDigest.getInstance(GWConstants.MD5);
    byte[] buffer = new byte[GWConstants.MAXBUFSIZE];
    File srcFile = new File(makeObjPath(srcPath, srcObjId, srcVersionId));
    long remainLength = 0L;
    int readLength = 0;
    int readBytes;
    String eTag = "";
    long totalLength = 0L;
    OSDData data = null;
    try (FileInputStream fis = new FileInputStream(srcFile)) {
        File tmpFile = new File(makeTempPath(path, objId, partNo));
        com.google.common.io.Files.createParentDirs(tmpFile);
        try (FileOutputStream fos = new FileOutputStream(tmpFile, false)) {
            data = new OSDData();
            if (Strings.isNullOrEmpty(copySourceRange)) {
                remainLength = srcFile.length();
                data.setFileSize(remainLength);
                while (remainLength > 0) {
                    readBytes = 0;
                    if (remainLength < GWConstants.MAXBUFSIZE) {
                        readBytes = (int) remainLength;
                    } else {
                        readBytes = GWConstants.MAXBUFSIZE;
                    }
                    readLength = fis.read(buffer, 0, readBytes);
                    fos.write(buffer, 0, readLength);
                    md5er.update(buffer, 0, readLength);
                    remainLength -= readLength;
                    totalLength += readLength;
                }
                fos.flush();
            } else {
                String[] ranges = copySourceRange.split(GWConstants.SLASH);
                totalLength = 0L;
                for (String range : ranges) {
                    String[] rangeParts = range.split(GWConstants.COMMA);
                    long offset = Longs.tryParse(rangeParts[GWConstants.RANGE_OFFSET_INDEX]);
                    long length = Longs.tryParse(rangeParts[GWConstants.RANGE_LENGTH_INDEX]);
                    logger.debug(GWConstants.LOG_S3OBJECT_OPERATION_RANGE, offset, length);
                    if (offset > 0) {
                        fis.skip(offset);
                    }
                    remainLength = length;
                    totalLength += length;
                    while (remainLength > 0) {
                        readBytes = 0;
                        if (remainLength < GWConstants.MAXBUFSIZE) {
                            readBytes = (int) remainLength;
                        } else {
                            readBytes = GWConstants.MAXBUFSIZE;
                        }
                        readLength = fis.read(buffer, 0, readBytes);
                        fos.write(buffer, 0, readLength);
                        md5er.update(buffer, 0, readLength);
                        remainLength -= readLength;
                    }
                    fos.flush();
                    data.setFileSize(totalLength);
                }
            }
        }
        byte[] digest = md5er.digest();
        eTag = base16().lowerCase().encode(digest);
        osdData.setETag(eTag);
        osdData.setFileSize(totalLength);
    }
    return osdData;
}
Also used : OSDData(com.pspace.ifs.ksan.osd.OSDData) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with OSDData

use of com.pspace.ifs.ksan.osd.OSDData in project ksan by infinistor.

the class S3ObjectOperation method uploadPartCopy.

public S3Object uploadPartCopy(String path, Metadata srcObjMeta, S3Range s3Range) throws GWException {
    S3Object s3Object = new S3Object();
    try {
        String copySourceRange = "";
        if (s3Range != null && s3Range.getListRange().size() > 0) {
            for (S3Range.Range range : s3Range.getListRange()) {
                if (Strings.isNullOrEmpty(copySourceRange)) {
                    copySourceRange = String.valueOf(range.getOffset()) + GWConstants.COMMA + String.valueOf(range.getLength());
                } else {
                    copySourceRange += GWConstants.SLASH + String.valueOf(range.getOffset()) + GWConstants.COMMA + String.valueOf(range.getLength());
                }
            }
        }
        logger.debug(GWConstants.LOG_S3OBJECT_OPERATION_COPY_SOURCE_RANGE, copySourceRange);
        OSDData osdData = null;
        if (objMeta.getReplicaCount() > 1) {
            if (GWUtils.getLocalIP().equals(srcObjMeta.getPrimaryDisk().getOsdIp())) {
                osdData = uploadPartCopyLocal(srcObjMeta.getPrimaryDisk().getPath(), srcObjMeta.getObjId(), srcObjMeta.getVersionId(), copySourceRange, path, objMeta.getObjId(), s3Parameter.getPartNumber());
            } else if (GWUtils.getLocalIP().equals(srcObjMeta.getReplicaDisk().getOsdIp())) {
                osdData = uploadPartCopyLocal(srcObjMeta.getReplicaDisk().getPath(), srcObjMeta.getObjId(), srcObjMeta.getVersionId(), copySourceRange, path, objMeta.getObjId(), s3Parameter.getPartNumber());
            } else {
                try {
                    OSDClient client = OSDClientManager.getInstance().getOSDClient(srcObjMeta.getPrimaryDisk().getOsdIp());
                    osdData = client.partCopy(srcObjMeta.getPrimaryDisk().getPath(), srcObjMeta.getObjId(), srcObjMeta.getVersionId(), copySourceRange, path, objMeta.getObjId(), s3Parameter.getPartNumber());
                    OSDClientManager.getInstance().returnOSDClient(client);
                } catch (Exception e) {
                    OSDClient client = OSDClientManager.getInstance().getOSDClient(srcObjMeta.getReplicaDisk().getOsdIp());
                    osdData = client.partCopy(srcObjMeta.getReplicaDisk().getPath(), srcObjMeta.getObjId(), srcObjMeta.getVersionId(), copySourceRange, path, objMeta.getObjId(), s3Parameter.getPartNumber());
                    OSDClientManager.getInstance().returnOSDClient(client);
                }
            }
        } else {
            osdData = uploadPartCopyLocal(srcObjMeta.getPrimaryDisk().getPath(), srcObjMeta.getObjId(), srcObjMeta.getVersionId(), copySourceRange, path, objMeta.getObjId(), s3Parameter.getPartNumber());
        }
        if (osdData != null) {
            s3Object.setEtag(osdData.getETag());
            s3Object.setFileSize(osdData.getFileSize());
        } else {
            logger.error(GWConstants.LOG_S3OBJECT_OPERATION_OSD_ERROR);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        s3Object.setLastModified(new Date());
        s3Object.setDeleteMarker(GWConstants.OBJECT_TYPE_FILE);
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    return s3Object;
}
Also used : OSDClient(com.pspace.ifs.ksan.gw.object.osdclient.OSDClient) OSDData(com.pspace.ifs.ksan.osd.OSDData) GWException(com.pspace.ifs.ksan.gw.exception.GWException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) ResourceNotFoundException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Date(java.util.Date)

Aggregations

OSDData (com.pspace.ifs.ksan.osd.OSDData)3 GWException (com.pspace.ifs.ksan.gw.exception.GWException)1 OSDClient (com.pspace.ifs.ksan.gw.object.osdclient.OSDClient)1 ResourceNotFoundException (com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Date (java.util.Date)1