Search in sources :

Example 6 with StorageSite

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

the class JDBCatalogue method getStorageSites.

public Collection<StorageSite> getStorageSites(Connection connection, Boolean isCache, Boolean includePrivate) throws SQLException {
    try (Statement s = connection.createStatement()) {
        try (ResultSet rs = s.executeQuery("SELECT storageSiteId, resourceURI, " + "currentNum, currentSize, quotaNum, quotaSize, username, " + "password, encrypt, private FROM storage_site_table " + "JOIN credential_table ON credentialRef = credintialId " + "WHERE isCache = " + isCache + " AND removing = FALSE")) {
            ArrayList<StorageSite> res = new ArrayList<>();
            while (rs.next()) {
                StorageSite ss = new StorageSite();
                ss.setStorageSiteId(rs.getLong(1));
                ss.setResourceURI(rs.getString(2));
                ss.setCurrentNum(rs.getLong(3));
                ss.setCurrentSize(rs.getLong(4));
                ss.setQuotaNum(rs.getLong(5));
                ss.setQuotaSize(rs.getLong(6));
                Credential c = new Credential();
                c.setStorageSiteUsername(rs.getString(7));
                c.setStorageSitePassword(rs.getString(8));
                ss.setCredential(c);
                ss.setEncrypt(rs.getBoolean(9));
                ss.setCache(isCache);
                if (rs.getBoolean(10) && includePrivate) {
                    res.add(ss);
                } else if (!rs.getBoolean(10)) {
                    res.add(ss);
                }
            }
            return res;
        }
    }
}
Also used : Credential(nl.uva.cs.lobcder.resources.Credential) StorageSite(nl.uva.cs.lobcder.resources.StorageSite)

Example 7 with StorageSite

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

the class StorageSitesService method queryStorageSites.

// 
// @Path("set/")
// @PUT
// @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
// public void set(JAXBElement<StorageSiteWrapperList> jbSites) throws SQLException {
// MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
// if (mp.isAdmin()) {
// try (Connection connection = getCatalogue().getConnection()) {
// StorageSiteWrapperList sitesWL = jbSites.getValue();
// List<StorageSiteWrapper> sswl = sitesWL.getSites();
// if (sswl != null && sswl.size() > 0) {
// Collection<StorageSite> sites = new ArrayList<>();
// for (StorageSiteWrapper ssw : sswl) {
// StorageSite site = new StorageSite();
// Credential cred = new Credential();
// cred.setStorageSitePassword(ssw.getCredential().getStorageSitePassword());
// cred.setStorageSiteUsername(ssw.getCredential().getStorageSiteUsername());
// site.setCredential(cred);
// site.setCurrentNum(ssw.getCurrentNum());
// site.setCurrentSize(ssw.getCurrentSize());
// site.setResourceURI(ssw.getResourceURI());
// site.setEncrypt(ssw.isEncrypt());
// site.setCache(ssw.isCache());
// site.setQuotaNum(ssw.getQuotaNum());
// site.setQuotaSize(ssw.getQuotaSize());
// sites.add(site);
// }
// getCatalogue().insertOrUpdateStorageSites(sites, connection, mp.isAdmin());
// connection.commit();
// }
// 
// }
// }
// }
// 
// @Path("delete/")
// @PUT
// @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
// public void delete(JAXBElement<StorageSiteWrapperList> jbSites) throws SQLException {
// StorageSiteWrapperList sitesWL = jbSites.getValue();
// MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
// if (sitesWL != null && sitesWL.getSites() != null && sitesWL.getSites().size() > 0 && mp.isAdmin()) {
// List<Long> ids = new ArrayList<>();
// for (StorageSiteWrapper ssw : sitesWL.getSites()) {
// ids.add(ssw.getStorageSiteId());
// //                if(ssw.isSaveFilesOnDelete()){
// //                    getCatalogue().getPdriStorageSiteID(ssw.getStorageSiteId(), null);
// //                }
// }
// try (Connection connection = getCatalogue().getConnection()) {
// getCatalogue().deleteStorageSites(ids, connection);
// connection.commit();
// }
// }
// }
// 
private List<StorageSiteWrapper> queryStorageSites(@Nonnull Connection cn, Boolean includePrivate) throws SQLException {
    MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
    List<String> ids = queryParameters.get("id");
    if (ids != null && ids.size() > 0 && ids.get(0).equals("all")) {
        Collection<StorageSite> sites = getCatalogue().getStorageSites(cn, Boolean.FALSE, includePrivate);
        Collection<StorageSite> cachesites = getCatalogue().getStorageSites(cn, Boolean.TRUE, includePrivate);
        List<StorageSiteWrapper> sitesWarpper = new ArrayList<>();
        for (StorageSite s : sites) {
            StorageSiteWrapper sw = new StorageSiteWrapper();
            CredentialWrapped cw = new CredentialWrapped();
            cw.setStorageSitePassword(s.getCredential().getStorageSitePassword());
            // cw.setStorageSitePassword("************");
            cw.setStorageSiteUsername(s.getCredential().getStorageSiteUsername());
            sw.setCredential(cw);
            sw.setCurrentNum(s.getCurrentNum());
            sw.setEncrypt(s.isEncrypt());
            sw.setQuotaNum(s.getQuotaNum());
            sw.setQuotaSize(s.getQuotaSize());
            sw.setResourceURI(s.getResourceURI());
            sw.setStorageSiteId(s.getStorageSiteId());
            sw.setIsCache(false);
            sw.setCurrentSize(s.getCurrentSize());
            sitesWarpper.add(sw);
        }
        for (StorageSite s : cachesites) {
            StorageSiteWrapper sw = new StorageSiteWrapper();
            CredentialWrapped cw = new CredentialWrapped();
            cw.setStorageSitePassword(s.getCredential().getStorageSitePassword());
            // cw.setStorageSitePassword("************");
            cw.setStorageSiteUsername(s.getCredential().getStorageSiteUsername());
            sw.setCredential(cw);
            sw.setCurrentNum(s.getCurrentNum());
            sw.setEncrypt(s.isEncrypt());
            sw.setQuotaNum(s.getQuotaNum());
            sw.setQuotaSize(s.getQuotaSize());
            sw.setResourceURI(s.getResourceURI());
            sw.setStorageSiteId(s.getStorageSiteId());
            sw.setIsCache(true);
            sw.setCurrentSize(s.getCurrentSize());
            sitesWarpper.add(sw);
        }
        return sitesWarpper;
    }
    return null;
}
Also used : StorageSiteWrapper(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) ArrayList(java.util.ArrayList) CredentialWrapped(nl.uva.cs.lobcder.rest.wrappers.CredentialWrapped)

Example 8 with StorageSite

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

the class FastestSiteReplicationPolicy method getRandomStorageSite.

private Map<String, StorageSite> getRandomStorageSite(int number) {
    Map<String, StorageSite> res = new HashMap<>();
    List<StorageSite> copy = new LinkedList<>(availableStorage.values());
    Collections.shuffle(copy);
    if (number > availableStorage.size()) {
        number = availableStorage.size();
    }
    List<StorageSite> rand = copy.subList(0, number);
    for (StorageSite s : rand) {
        res.put(s.getResourceURI(), s);
    }
    return res;
}
Also used : HashMap(java.util.HashMap) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) LinkedList(java.util.LinkedList)

Example 9 with StorageSite

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

the class FastestSiteReplicationPolicy method getSitesToReplicate.

@Override
public Collection<Long> getSitesToReplicate(Connection connection) throws Exception {
    if (availableStorage == null || availableStorage.isEmpty()) {
        availableStorage = getStorageSites(connection);
        number = PropertiesHelper.getNumberOfSites();
    }
    Map<String, StorageSite> fastestMap = getFastestSites(connection, number);
    Collection<Long> fastest = new ArrayList<>();
    for (StorageSite s : fastestMap.values()) {
        fastest.add(s.getStorageSiteId());
    }
    return fastest;
}
Also used : StorageSite(nl.uva.cs.lobcder.resources.StorageSite) ArrayList(java.util.ArrayList)

Example 10 with StorageSite

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

the class FastestSiteReplicationPolicy method getFastestSites.

private Map<String, StorageSite> getFastestSites(Connection connection, int number) throws SQLException {
    // Every now and then (20/80) go random
    int[] numsToGenerate = new int[] { 1, 2 };
    double[] discreteProbabilities = new double[] { 0.85, 0.15 };
    EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(numsToGenerate, discreteProbabilities);
    int random = distribution.sample();
    Map<String, StorageSite> res = new HashMap<>();
    if (random <= 1) {
        try (Statement s = connection.createStatement()) {
            ResultSet rs = s.executeQuery("SELECT src, dst FROM speed_table " + "GROUP BY averageSpeed DESC LIMIT " + number);
            Map<String, StorageSite> tmp = new HashMap<>();
            while (rs.next()) {
                String src = rs.getString(1);
                String dst = rs.getString(2);
                Set<String> keys = availableStorage.keySet();
                for (String k : keys) {
                    if (k.contains(src) || k.contains(dst)) {
                        tmp.put(k, availableStorage.get(k));
                    }
                }
            }
            if (tmp.isEmpty()) {
                ArrayList<String> keysAsArray = new ArrayList<>(availableStorage.keySet());
                Random r = new Random();
                StorageSite randomValue = availableStorage.get(keysAsArray.get(r.nextInt(keysAsArray.size())));
                res.put(randomValue.getResourceURI(), randomValue);
            } else {
                ArrayList<String> keysAsArray = new ArrayList<>(tmp.keySet());
                Random r = new Random();
                StorageSite randomValue = tmp.get(keysAsArray.get(r.nextInt(keysAsArray.size())));
                res.put(randomValue.getResourceURI(), randomValue);
            }
        }
    } else {
        return getRandomStorageSite(number);
    }
    return res;
}
Also used : EnumeratedIntegerDistribution(org.apache.commons.math3.distribution.EnumeratedIntegerDistribution) HashMap(java.util.HashMap) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Random(java.util.Random) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) ResultSet(java.sql.ResultSet)

Aggregations

StorageSite (nl.uva.cs.lobcder.resources.StorageSite)12 ArrayList (java.util.ArrayList)6 Credential (nl.uva.cs.lobcder.resources.Credential)6 HashMap (java.util.HashMap)3 StorageSiteWrapper (nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper)3 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 CredentialWrapped (nl.uva.cs.lobcder.rest.wrappers.CredentialWrapped)2 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)1 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)1 StorageSiteWrapperList (nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapperList)1