Search in sources :

Example 6 with CheckpointListener

use of org.apache.ignite.spi.checkpoint.CheckpointListener in project ignite by apache.

the class SharedFsTimeoutTask method body.

/**
 * {@inheritDoc}
 */
@Override
public void body() throws InterruptedException {
    long nextTime = 0;
    Collection<String> rmvKeys = new HashSet<>();
    while (!isInterrupted()) {
        rmvKeys.clear();
        synchronized (mux) {
            // nextTime is 0 only on first iteration.
            if (nextTime != 0) {
                long delay;
                if (nextTime == -1)
                    delay = 5000;
                else {
                    assert nextTime > 0;
                    delay = nextTime - U.currentTimeMillis();
                }
                while (delay > 0) {
                    mux.wait(delay);
                    delay = nextTime - U.currentTimeMillis();
                }
            }
            Map<File, SharedFsTimeData> snapshot = new HashMap<>(files);
            long now = U.currentTimeMillis();
            nextTime = -1;
            // if (now - last modification date) > expiration time
            for (Map.Entry<File, SharedFsTimeData> entry : snapshot.entrySet()) {
                File file = entry.getKey();
                SharedFsTimeData timeData = entry.getValue();
                try {
                    if (timeData.getLastAccessTime() != file.lastModified())
                        timeData.setExpireTime(SharedFsUtils.read(file, marshaller, log).getExpireTime());
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to marshal/unmarshal in checkpoint file: " + file.getAbsolutePath(), e);
                    continue;
                } catch (IOException e) {
                    if (!file.exists()) {
                        files.remove(file);
                        rmvKeys.add(timeData.getKey());
                    } else
                        U.error(log, "Failed to read checkpoint file: " + file.getAbsolutePath(), e);
                    continue;
                }
                if (timeData.getExpireTime() > 0)
                    if (timeData.getExpireTime() <= now)
                        if (!file.delete() && file.exists())
                            U.error(log, "Failed to delete check point file by timeout: " + file.getAbsolutePath());
                        else {
                            files.remove(file);
                            rmvKeys.add(timeData.getKey());
                            if (log.isDebugEnabled())
                                log.debug("File was deleted by timeout: " + file.getAbsolutePath());
                        }
                    else if (timeData.getExpireTime() < nextTime || nextTime == -1)
                        nextTime = timeData.getExpireTime();
            }
        }
        CheckpointListener lsnr = this.lsnr;
        if (lsnr != null)
            for (String key : rmvKeys) lsnr.onCheckpointRemoved(key);
    }
    synchronized (mux) {
        files.clear();
    }
}
Also used : HashMap(java.util.HashMap) CheckpointListener(org.apache.ignite.spi.checkpoint.CheckpointListener) IOException(java.io.IOException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 7 with CheckpointListener

use of org.apache.ignite.spi.checkpoint.CheckpointListener in project ignite by apache.

the class JdbcCheckpointSpi method removeExpiredCheckpoints.

/**
 * Removes expired checkpoints from the checkpoint table.
 *
 * @param conn Active database connection.
 * @return Number of removed expired checkpoints.
 * @throws SQLException Thrown in case of any errors.
 */
private int removeExpiredCheckpoints(Connection conn) throws SQLException {
    int delCnt = 0;
    PreparedStatement selSt = null;
    PreparedStatement delSt = null;
    ResultSet rs = null;
    Time time = new Time(U.currentTimeMillis());
    CheckpointListener tmp = lsnr;
    try {
        if (tmp != null) {
            selSt = conn.prepareStatement(selExpSql);
            selSt.setTime(1, time);
            rs = selSt.executeQuery();
            while (rs.next()) tmp.onCheckpointRemoved(rs.getString(1));
        }
        delSt = conn.prepareStatement(delExpSql);
        delSt.setTime(1, time);
        delCnt = delSt.executeUpdate();
    } finally {
        U.close(rs, log);
        U.close(selSt, log);
        U.close(delSt, log);
    }
    if (log.isDebugEnabled())
        log.debug("Successfully removed expired checkpoints from: " + tblName);
    return delCnt;
}
Also used : CheckpointListener(org.apache.ignite.spi.checkpoint.CheckpointListener) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time)

Aggregations

CheckpointListener (org.apache.ignite.spi.checkpoint.CheckpointListener)7 File (java.io.File)2 PreparedStatement (java.sql.PreparedStatement)2 CheckpointSpi (org.apache.ignite.spi.checkpoint.CheckpointSpi)2 AmazonClientException (com.amazonaws.AmazonClientException)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Time (java.sql.Time)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1