Search in sources :

Example 11 with PDRIDescr

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

the class DeleteSweep method run.

@Override
public void run() {
    PDRIFactory factory;
    try (Connection connection = datasource.getConnection()) {
        // This query interferes with the assimilator
        try {
            connection.setAutoCommit(false);
            try (Statement s1 = connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE)) {
                ResultSet rs1 = s1.executeQuery("SELECT pdriGroupId FROM pdrigroup_table WHERE refCount = 0 ");
                while (rs1.next()) {
                    Long groupId = rs1.getLong(1);
                    try (PreparedStatement ps2 = connection.prepareStatement("SELECT pdriId, fileName, storageSiteRef FROM pdri_table WHERE pdriGroupRef = ?", java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE)) {
                        ps2.setLong(1, groupId);
                        ResultSet rs2 = ps2.executeQuery();
                        while (rs2.next()) {
                            // here better to use a kind a local cache, i.e. hashmap
                            try (PreparedStatement ps3 = connection.prepareStatement("SELECT resourceUri, username, password FROM storage_site_table " + "JOIN credential_table ON credentialRef = credintialId " + "WHERE storageSiteId = ?", java.sql.ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
                                String fileName = rs2.getString(2);
                                Long storageSiteRef = rs2.getLong(3);
                                ps3.setLong(1, storageSiteRef);
                                ResultSet rs3 = ps3.executeQuery();
                                if (rs3.next()) {
                                    String resourceUri = rs3.getString(1);
                                    String username = rs3.getString(2);
                                    String password = rs3.getString(3);
                                    // For deleteing we don't care for encryption
                                    PDRIDescr pdriDescr = new PDRIDescr(fileName, storageSiteRef, resourceUri, username, password, false, null, null, null);
                                    DeleteSweep.log.log(Level.FINE, "PDRI Description: {0}, {1}, {2}, {3}, {4}", new Object[] { fileName, storageSiteRef, resourceUri, username, password });
                                    DeleteSweep.log.log(Level.FINE, "PDRI pdriDescr: {0}", new Object[] { pdriDescr });
                                    factory = PDRIFactory.getFactory();
                                    DeleteSweep.log.log(Level.FINE, "PDRIFactory: {0}", factory);
                                    PDRI pdri = factory.createInstance(pdriDescr, false);
                                    DeleteSweep.log.log(Level.FINE, "pdri: {0}", pdri);
                                    DeleteSweep.log.log(Level.FINE, "PDRI Instance file name: {0}", new Object[] { pdri.getFileName() });
                                    pdri.delete();
                                    DeleteSweep.log.log(Level.FINE, "DELETE:", pdri.getURI());
                                    rs2.deleteRow();
                                    connection.commit();
                                }
                            }
                        }
                        rs1.deleteRow();
                        connection.commit();
                    }
                }
            }
        } catch (SQLException | IOException e) {
            DeleteSweep.log.log(Level.SEVERE, null, e);
            connection.rollback();
        }
    } catch (SQLException e) {
        DeleteSweep.log.log(Level.SEVERE, null, e);
    }
}
Also used : PDRIFactory(nl.uva.cs.lobcder.resources.PDRIFactory) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) IOException(java.io.IOException) PDRI(nl.uva.cs.lobcder.resources.PDRI)

Example 12 with PDRIDescr

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

the class JDBCatalogue method getPdriDescrByGroupId.

public List<PDRIDescr> getPdriDescrByGroupId(Long groupId, @Nonnull Connection connection) throws SQLException {
    List<PDRIDescr> res = getFromPDRIDescrCache(groupId);
    if (res != null) {
        return res;
    }
    res = new ArrayList<>();
    long pdriGroupRef;
    long pdriId;
    try (PreparedStatement ps = connection.prepareStatement("SELECT fileName, " + "storageSiteRef, storage_site_table.resourceUri, " + "username, password, isEncrypted, encryptionKey, pdri_table.pdriId, storage_site_table.isCache " + "FROM pdri_table " + "JOIN storage_site_table ON storageSiteRef = storageSiteId " + "JOIN credential_table ON credentialRef = credintialId " + "WHERE pdri_table.pdriGroupRef = ? ")) {
        ps.setLong(1, groupId);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String fileName = rs.getString(1);
            long ssID = rs.getLong(2);
            String resourceURI = rs.getString(3);
            String uName = rs.getString(4);
            String passwd = rs.getString(5);
            boolean encrypt = rs.getBoolean(6);
            long key = rs.getLong(7);
            pdriId = rs.getLong(8);
            boolean isCache = rs.getBoolean(9);
            // if (resourceURI.startsWith("lfc") || resourceURI.startsWith("srm")
            // || resourceURI.startsWith("gftp")) {
            // try {
            // passwd = getProxyAsBase64String();
            // } catch (FileNotFoundException ex) {
            // Logger.getLogger(JDBCatalogue.class.getName()).log(Level.SEVERE, null, ex);
            // } catch (IOException ex) {
            // Logger.getLogger(JDBCatalogue.class.getName()).log(Level.SEVERE, null, ex);
            // }
            // }
            res.add(new PDRIDescr(fileName, ssID, resourceURI, uName, passwd, encrypt, BigInteger.valueOf(key), Long.valueOf(groupId), Long.valueOf(pdriId), isCache));
        }
        putToPDRIDescrCache(groupId, res);
        return res;
    }
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr)

Example 13 with PDRIDescr

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

the class JDBCatalogue method updatePdris.

// public List<String> setLocationPreferences(Connection connection, Long uid, List<String> locationPreferences, Boolean includePrivate) throws SQLException {
// List<String> dataPref = getDataLocationPreferace(connection, uid);
// 
// if (dataPref != null && !dataPref.isEmpty() && locationPreferences != null && !locationPreferences.isEmpty()) {
// locationPreferences.removeAll(dataPref);
// }
// 
// String storageSitesQuery = "select storageSiteId,resourceUri,private from storage_site_table "
// + "WHERE resourceUri LIKE ";
// List<String> sites = new ArrayList<>();
// 
// for (String site : locationPreferences) {
// try {
// new VRL(site);
// storageSitesQuery += "'" + site + "' OR resourceUri LIKE ";
// } catch (VRLSyntaxException ex) {
// Logger.getLogger(JDBCatalogue.class.getName()).log(Level.WARNING, "Wrong VRL", ex);
// }
// }
// if (storageSitesQuery.endsWith(" OR resourceUri LIKE ")) {
// storageSitesQuery = storageSitesQuery.substring(0, storageSitesQuery.lastIndexOf("OR")) + "";
// }
// if (locationPreferences != null && !locationPreferences.isEmpty()) {
// storageSitesQuery += " AND isCache = false";
// List<Long> ids = new ArrayList<>();
// try (Statement s = connection.createStatement()) {
// try (ResultSet rs = s.executeQuery(storageSitesQuery)) {
// while (rs.next()) {
// if (rs.getBoolean(3) && includePrivate) {
// sites.add(rs.getString(2));
// ids.add(rs.getLong(1));
// } else if (!rs.getBoolean(3)) {
// sites.add(rs.getString(2));
// ids.add(rs.getLong(1));
// }
// }
// }
// for (Long id : ids) {
// String query = "INSERT INTO pref_table (ld_uid, storageSiteRef) VALUES ('"
// + uid + "', '" + id + "')";
// s.addBatch(query);
// }
// s.executeBatch();
// }
// }
// LogicalData ldata = getFromLDataCache(uid, null);
// if (ldata != null) {
// ldata.setDataLocationPreferences(dataPref);
// putToLDataCache(ldata, null);
// }
// connection.commit();
// return getDataLocationPreferace(connection, uid);
// }
public void updatePdris(List<PDRIDescr> pdrisToUpdate, Connection connection) throws SQLException {
    for (PDRIDescr d : pdrisToUpdate) {
        try (PreparedStatement ps = connection.prepareStatement("UPDATE pdri_table SET isEncrypted = ?, storageSiteRef = ? WHERE pdriId = ?")) {
            ps.setBoolean(1, d.getEncrypt());
            ps.setLong(2, d.getStorageSiteId());
            ps.setLong(3, d.getId());
            ps.executeUpdate();
            List<PDRIDescr> res = getPdriDescrByGroupId(d.getPdriGroupRef(), connection);
            putToPDRIDescrCache(d.getPdriGroupRef(), res);
        }
    }
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr)

Example 14 with PDRIDescr

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

the class WebDataFileResource method transfer.

private PDRI transfer(List<PDRIDescr> pdris, OutputStream out, int tryCount, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
    InputStream in = null;
    PDRI pdri = null;
    double start = 0;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "Start for {0}", getLogicalData().getName());
    try {
        PDRIDescr pdriDescr = selectBestPDRI(pdris);
        pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "pdri: {0}", pdri.getURI());
        if (pdri != null) {
            start = System.currentTimeMillis();
            in = pdri.getData();
            if (!pdri.getEncrypted()) {
                if (doCircularStreamBufferTransferer) {
                    CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
                    cBuff.startTransfer((long) -1);
                } else {
                    int read;
                    byte[] copyBuffer = new byte[Constants.BUF_SIZE];
                    while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
                        out.write(copyBuffer, 0, read);
                    // tos.write(copyBuffer, 0, read);
                    }
                }
            } else {
                DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
                encrypter.decrypt(in, out);
            }
        } else {
            sleepTime = 5;
            throw new NotFoundException("Physical resource not found");
        }
    } catch (Exception ex) {
        if (ex instanceof NotFoundException) {
            throw (NotFoundException) ex;
        }
        if (ex.getMessage().contains("Resource not found")) {
            throw new NotFoundException(ex.getMessage());
        }
        if (pdri != null) {
            Double speed = weightPDRIMap.get(pdri.getHost());
            if (speed != null) {
                speed = speed - 10;
            } else {
                speed = Double.valueOf(-10);
            }
            weightPDRIMap.put(pdri.getHost(), speed);
        }
        try {
            sleepTime = sleepTime + 5;
            Thread.sleep(sleepTime);
            if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
                transfer(pdris, out, tryCount, false);
            } else if (++tryCount < Constants.RECONNECT_NTRY) {
                transfer(pdris, out, tryCount, false);
            } else {
                transfer(pdris, out, 0, false);
            }
        } catch (InterruptedException ex1) {
            sleepTime = 5;
            throw new IOException(ex1);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    sleepTime = 5;
    double elapsed = System.currentTimeMillis() - start;
    double speed = ((pdri.getLength() * 8.0) * 1000.0) / (elapsed * 1000.0);
    try {
        String msg = "File: " + pdri.getFileName() + " Destination: " + new URI(pdri.getURI()).getScheme() + "://" + pdri.getHost() + " Rx_Speed: " + speed + " Kbites/sec Rx_Size: " + (pdri.getLength()) + " bytes Elapsed_Time: " + elapsed + " ms";
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.INFO, msg);
    } catch (URISyntaxException ex) {
    }
    return pdri;
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) InputStream(java.io.InputStream) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PDRI(nl.uva.cs.lobcder.resources.PDRI) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 15 with PDRIDescr

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

the class WebDataFileResource method sendContent.

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException, NotFoundException {
    double start = System.currentTimeMillis();
    PDRI pdri;
    Iterator<PDRIDescr> it;
    try {
        List<PDRIDescr> pdris = getCatalogue().getPdriDescrByGroupId(getLogicalData().getPdriGroupId());
        if (pdris.size() <= 0) {
            throw new NotFoundException("File inconsistency! Could not find physical file!");
        }
        // it = getCatalogue().getPdriDescrByGroupId(getLogicalData().getPdriGroupId()).iterator();
        if (range != null) {
            if (range.getFinish() == null) {
                range = new Range(range.getStart(), (getLogicalData().getLength() - 1));
            }
            it = pdris.iterator();
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "Start: {0} end: {1} range: {2}", new Object[] { range.getStart(), range.getFinish(), range.getRange() });
            pdri = transfererRange(it, out, 0, null, range);
        } else {
            // pdri = transfer(it, out, 0, null, false);
            pdri = transfer(pdris, out, 0, false);
        }
    } catch (SQLException ex) {
        throw new BadRequestException(this, ex.getMessage());
    } catch (IOException ex) {
        if (ex.getMessage().contains("Resource not found") || ex.getMessage().contains("Couldn't locate path")) {
            throw new NotFoundException(ex.getMessage());
        } else {
            throw new BadRequestException(this, ex.getMessage());
        }
    } finally {
    // Don't close the output, we need it to send back the response
    // if (out != null) {
    // out.flush();
    // out.close();
    // }
    }
    double elapsed = System.currentTimeMillis() - start;
    long len;
    if (range != null) {
        len = range.getFinish() - range.getStart() + 1;
    } else {
        len = this.getLogicalData().getLength();
    }
    double speed = ((len * 8.0) * 1000.0) / (elapsed * 1000.0);
    Double oldSpeed = weightPDRIMap.get(pdri.getHost());
    if (oldSpeed == null) {
        oldSpeed = speed;
    }
    Integer numOfGets = numOfGetsMap.get(pdri.getHost());
    if (numOfGets == null) {
        numOfGets = 1;
    }
    double averagre = (speed + oldSpeed) / (double) numOfGets;
    numOfGetsMap.put(pdri.getHost(), numOfGets++);
    weightPDRIMap.put(pdri.getHost(), averagre);
    getCatalogue().addViewForRes(getLogicalData().getUid());
    Stats stats = new Stats();
    stats.setSource(pdri.getHost());
    stats.setDestination(fromAddress);
    stats.setSpeed(speed);
    stats.setSize(getLogicalData().getLength());
    String msg = "Source: " + stats.getSource() + " Destination: " + stats.getDestination() + " Tx_Speed: " + speed + " Kbites/sec Tx_Size: " + getLogicalData().getLength() + " bytes Elapsed_Time: " + elapsed + " ms";
    try {
        if (!pdri.isCahce()) {
            getCatalogue().setSpeed(stats);
        }
    } catch (SQLException ex) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.INFO, msg);
}
Also used : SQLException(java.sql.SQLException) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) IOException(java.io.IOException) Range(io.milton.http.Range) PDRI(nl.uva.cs.lobcder.resources.PDRI) Stats(nl.uva.cs.lobcder.rest.wrappers.Stats) BadRequestException(io.milton.http.exceptions.BadRequestException)

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