Search in sources :

Example 16 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class StorageSitesService method updateLogicalDataAndPdri.

@PUT
@Path("update_pdri/{uid}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void updateLogicalDataAndPdri(PDRIDescrWrapperList pdris, @PathParam("uid") Long uid) throws SQLException, IOException {
    MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
    if (mp.isAdmin()) {
        try (Connection cn = getCatalogue().getConnection()) {
            for (PDRIDescr pdriDescr : pdris.getPdris()) {
                LogicalData logicalData = getCatalogue().getLogicalDataByUid(uid, cn);
                PDRI pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
                logicalData.setLength(pdri.getLength());
                getCatalogue().updatePdri(logicalData, pdri, cn);
            }
            cn.commit();
            cn.close();
        }
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) Connection(java.sql.Connection) PDRI(nl.uva.cs.lobcder.resources.PDRI) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 17 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class Catalogue method getPDRI.

public PDRI getPDRI(String fileUID) throws InterruptedException, IOException, URISyntaxException {
    LogicalDataWrapped logicalData = getLogicalDataWrapped(fileUID);
    // new PDRIDescr();
    PDRIDescr pdriDesc = null;
    pdriDesc = selectBestPDRI(logicalData.getPdriList());
    Logger.getLogger(Catalogue.class.getName()).log(Level.FINE, "Selected pdri: {0}", pdriDesc.getResourceUrl());
    return new VPDRI(pdriDesc.getName(), pdriDesc.getId(), pdriDesc.getResourceUrl(), pdriDesc.getUsername(), pdriDesc.getPassword(), pdriDesc.getEncrypt(), pdriDesc.getKey(), false);
}
Also used : VPDRI(nl.uva.cs.lobcder.resources.VPDRI) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) LogicalDataWrapped(nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped)

Example 18 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class Catalogue method selectBestPDRI.

private PDRIDescr selectBestPDRI(List<PDRIDescr> pdris) throws URISyntaxException, UnknownHostException, SocketException {
    if (!pdris.isEmpty()) {
        Iterator<PDRIDescr> iter = pdris.iterator();
        while (iter.hasNext()) {
            PDRIDescr p = iter.next();
            URI uri = new URI(p.getResourceUrl());
            if (uri.getScheme().equals("file")) {
                return p;
            }
            if (Catalogue.isPDRIOnWorker(uri)) {
                String resURL = p.getResourceUrl().replaceFirst(uri.getScheme(), "file");
                p.setResourceUrl(resURL);
                return p;
            }
        }
    }
    if (weightPDRIMap.isEmpty() || weightPDRIMap.size() < pdris.size()) {
        // Just return one at random;
        int index = new Random().nextInt(pdris.size());
        PDRIDescr[] array = pdris.toArray(new PDRIDescr[pdris.size()]);
        Logger.getLogger(Catalogue.class.getName()).log(Level.FINE, "Selecting Random: {0}", array[index].getResourceUrl());
        return array[index];
    }
    long sumOfSpeed = 0;
    for (PDRIDescr p : pdris) {
        URI uri = new URI(p.getResourceUrl());
        String host = null;
        if (uri.getScheme().equals("file") || StringUtil.isEmpty(uri.getHost()) || uri.getHost().equals("localhost") || uri.getHost().equals("127.0.0.1")) {
            try {
                host = InetAddress.getLocalHost().getHostName();
            } catch (Exception ex) {
                List<String> ips = Util.getAllIPs();
                for (String ip : ips) {
                    if (ip.contains(".")) {
                        host = ip;
                        break;
                    }
                }
            }
        } else {
            host = uri.getHost();
        }
        Double speed = weightPDRIMap.get(host);
        if (speed == null) {
            speed = Double.valueOf(0);
        }
        Logger.getLogger(Catalogue.class.getName()).log(Level.FINE, "Speed: {0}", speed);
        sumOfSpeed += speed;
    }
    if (sumOfSpeed <= 0) {
        int index = new Random().nextInt(pdris.size());
        PDRIDescr[] array = pdris.toArray(new PDRIDescr[pdris.size()]);
        return array[index];
    }
    int itemIndex = new Random().nextInt((int) sumOfSpeed);
    for (PDRIDescr p : pdris) {
        Double speed = weightPDRIMap.get(new URI(p.getResourceUrl()).getHost());
        if (speed == null) {
            speed = Double.valueOf(0);
        }
        if (itemIndex < speed) {
            Logger.getLogger(Catalogue.class.getName()).log(Level.FINE, "Selecting:{0}  with speed: {1}", new Object[] { p.getResourceUrl(), speed });
            return p;
        }
        itemIndex -= speed;
    }
    int index = new Random().nextInt(pdris.size());
    PDRIDescr[] array = pdris.toArray(new PDRIDescr[pdris.size()]);
    PDRIDescr res = array[index];
    return res;
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) StorageSiteWrapperList(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapperList) List(java.util.List) ArrayList(java.util.ArrayList) PDRIDescrWrapperList(nl.uva.cs.lobcder.rest.wrappers.PDRIDescrWrapperList) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException)

Example 19 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class Catalogue method addCacheFileToPDRIDescr.

private LogicalDataWrapped addCacheFileToPDRIDescr(LogicalDataWrapped logicalData) throws IOException, URISyntaxException {
    List<PDRIDescr> pdris = logicalData.getPdriList();
    cacheFile = new File(Util.getCacheDir(), pdris.get(0).getName());
    if (!isFileOnWorker(logicalData) && cacheFile.exists() && !isCacheInPdriList(cacheFile, logicalData)) {
        String fileName = cacheFile.getName();
        long ssID = -1;
        String resourceURI = "file:///" + cacheFile.getAbsoluteFile().getParentFile().getParent();
        String uName = "fake";
        String passwd = "fake";
        boolean encrypt = false;
        long key = -1;
        long pdriId = -1;
        Long groupId = Long.valueOf(-1);
        boolean isCache = false;
        pdris.add(new PDRIDescr(fileName, ssID, resourceURI, uName, passwd, encrypt, BigInteger.valueOf(key), groupId, pdriId, isCache));
        switch(Util.getCacheEvictionPolicy()) {
            case LRU:
            case MRU:
            case RR:
                fileAccessMap.put(cacheFile.getAbsolutePath(), System.currentTimeMillis());
                break;
            case LFU:
            case MFU:
                Long count = fileAccessMap.get(cacheFile.getAbsolutePath());
                if (count == null) {
                    count = Long.valueOf(0);
                }
                fileAccessMap.put(cacheFile.getAbsolutePath(), count++);
                break;
        }
        logicalData.setPdriList(pdris);
    }
    return logicalData;
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) File(java.io.File)

Example 20 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class Catalogue method replicate.

boolean replicate(Pair<File, String> p) throws IOException {
    boolean result = true;
    PDRIDescr sourceDescr = createPdriDescr(p.getLeft());
    PDRI sourcePdri = PDRIFactory.getFactory().createInstance(sourceDescr);
    String[] sites = p.getRight().split("-");
    Long fileUID = Long.valueOf(sites[0]);
    Long pdriGroupId = Long.valueOf(sites[1]);
    List<PDRIDescr> destinationDescrList = new ArrayList<>();
    for (int i = 2; i < sites.length; i++) {
        try {
            StorageSiteWrapper ss = storageSiteCache.get(Long.valueOf(sites[i]));
            if (ss == null) {
                getStorageSites(String.valueOf(Long.valueOf(sites[i])));
                ss = storageSiteCache.get(Long.valueOf(sites[i]));
            }
            BigInteger pdriKey = nl.uva.cs.lobcder.util.DesEncrypter.generateKey();
            PDRIDescr destinationDescr = new PDRIDescr(sourceDescr.getName(), ss.getStorageSiteId(), ss.getResourceURI(), ss.getCredential().getStorageSiteUsername(), ss.getCredential().getStorageSitePassword(), ss.isEncrypt(), pdriKey, pdriGroupId, null, ss.isCache());
            PDRI destinationPdri = PDRIFactory.getFactory().createInstance(destinationDescr);
            destinationPdri.replicate(sourcePdri);
            result = destinationPdri.exists(destinationPdri.getFileName());
            long srcLen = sourcePdri.getLength();
            if (result == false || destinationPdri.getLength() != srcLen) {
                result = false;
            }
            destinationDescrList.add(destinationDescr);
        } catch (Exception e) {
            result = false;
        } catch (Throwable e) {
            result = false;
        }
    }
    result = updateMaster(destinationDescrList, fileUID);
    return result;
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) ArrayList(java.util.ArrayList) VPDRI(nl.uva.cs.lobcder.resources.VPDRI) PDRI(nl.uva.cs.lobcder.resources.PDRI) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) StorageSiteWrapper(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper) BigInteger(java.math.BigInteger)

Aggregations

PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)22 IOException (java.io.IOException)8 PDRI (nl.uva.cs.lobcder.resources.PDRI)7 SQLException (java.sql.SQLException)6 Permissions (nl.uva.cs.lobcder.auth.Permissions)5 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 LogicalDataWrapped (nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped)4 BadRequestException (io.milton.http.exceptions.BadRequestException)3 NotFoundException (io.milton.http.exceptions.NotFoundException)3 InputStream (java.io.InputStream)3 Path (javax.ws.rs.Path)3 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)3 ConflictException (io.milton.http.exceptions.ConflictException)2 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SocketException (java.net.SocketException)2