Search in sources :

Example 1 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class Sampler method dumpToFile.

/**
 * Dumps the sampled stacks to file. the collected samples are reset
 *
 * @param id - id will be added to file name returns the name of the file.
 * @return - the file name where the data was persisted or null if there was no data to persist.
 * @throws IOException - io issues while persisting data.
 */
@JmxExport(value = "dumpToSpecificFile", description = "save stack samples to file")
@Nullable
// not possible the provided ID is validated for path separators.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public synchronized File dumpToFile(@JmxExport(value = "fileID", description = "the ID that will be part of the file name") @Nullable final String id) throws IOException {
    String fileName = filePrefix + CharSequences.validatedFileName(((id == null) ? "" : '_' + id) + '_' + DateTimeFormats.TS_FORMAT.format(Timing.getCurrentTiming().fromNanoTimeToInstant(lastDumpTimeNanos)) + '_' + DateTimeFormats.TS_FORMAT.format(Instant.now()));
    File file = new File(fileName);
    return dumpToFile(file);
}
Also used : File(java.io.File) JmxExport(org.spf4j.jmx.JmxExport) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Nullable(javax.annotation.Nullable)

Example 2 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class JdbcHeartBeat method getLastRunDB.

@JmxExport(description = "The last run time recorded in the DB by this process")
public long getLastRunDB() throws SQLException, InterruptedException {
    return jdbc.transactOnConnection((final Connection conn, final long deadlineNanos) -> {
        try (PreparedStatement stmt = conn.prepareStatement(selectLastRunSql)) {
            stmt.setQueryTimeout(JdbcTemplate.getTimeoutToDeadlineSeconds(deadlineNanos));
            stmt.setNString(1, org.spf4j.base.Runtime.PROCESS_ID);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    long result = rs.getLong(1);
                    if (rs.next()) {
                        throw new IllegalStateException("Multible beats for same owner " + org.spf4j.base.Runtime.PROCESS_ID);
                    }
                    return result;
                } else {
                    return 0L;
                }
            }
        }
    }, jdbcTimeoutSeconds, TimeUnit.SECONDS);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JmxExport(org.spf4j.jmx.JmxExport)

Example 3 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class JdbcSemaphore method releaseDeadOwnerPermits.

/**
 * Attempts to release permits for this semaphore owned by dead owners.
 *
 * @param wishPermits - How many permits we would like to get released.
 * @return - the number of permits we actually released.
 * @throws SQLException - something went wrong with the db.
 * @throws InterruptedException - thrown if thread is interrupted.
 */
@JmxExport(description = "release dead owner permits")
@CheckReturnValue
public int releaseDeadOwnerPermits(@JmxExport(value = "wishPermits", description = "how many we whish to release") final int wishPermits) throws InterruptedException, SQLException {
    return jdbc.transactOnConnection((final Connection conn, final long deadlineNanos) -> {
        List<OwnerPermits> deadOwnerPermits = getDeadOwnerPermits(conn, deadlineNanos, wishPermits);
        int released = 0;
        for (OwnerPermits permit : deadOwnerPermits) {
            try (PreparedStatement stmt = conn.prepareStatement(deleteDeadOwerRecordSql)) {
                String owner = permit.getOwner();
                stmt.setNString(1, owner);
                stmt.setNString(2, semName);
                int nrPermits = permit.getNrPermits();
                stmt.setInt(3, nrPermits);
                stmt.setQueryTimeout(JdbcTemplate.getTimeoutToDeadlineSeconds(deadlineNanos));
                if (stmt.executeUpdate() == 1) {
                    // I can release! if not somebody else is doing it.
                    released += nrPermits;
                    releaseReservations(conn, deadlineNanos, nrPermits);
                    LOG.warn("Released {} reservations from dead owner {}", nrPermits, owner);
                }
            }
        }
        return released;
    }, jdbcTimeoutSeconds, TimeUnit.SECONDS);
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CheckReturnValue(javax.annotation.CheckReturnValue) JmxExport(org.spf4j.jmx.JmxExport)

Example 4 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class JdbcSemaphore method availablePermits.

@JmxExport(description = "Get the available semaphore permits")
public int availablePermits() throws SQLException, InterruptedException {
    return jdbc.transactOnConnection((final Connection conn, final long deadlineNanos) -> {
        try (PreparedStatement stmt = conn.prepareStatement(permitsSql)) {
            stmt.setNString(1, semName);
            stmt.setQueryTimeout(JdbcTemplate.getTimeoutToDeadlineSeconds(deadlineNanos));
            try (ResultSet rs = stmt.executeQuery()) {
                if (!rs.next()) {
                    throw new IllegalStateException();
                } else {
                    int result = rs.getInt(1);
                    if (rs.next()) {
                        throw new IllegalStateException();
                    }
                    return result;
                }
            }
        }
    }, jdbcTimeoutSeconds, TimeUnit.SECONDS);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JmxExport(org.spf4j.jmx.JmxExport)

Example 5 with JmxExport

use of org.spf4j.jmx.JmxExport in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorderSource method getMeasurementsAsString.

@JmxExport(description = "measurements as csv")
public String getMeasurementsAsString() {
    StringWriter sw = new StringWriter(128);
    Map<Object, MeasurementAccumulator> entitiesMeasurements = getEntitiesMeasurements();
    MeasurementsInfo info = this.processorTemplate.getInfo();
    try {
        Csv.writeCsvRow2(sw, "Measured", (Object[]) info.getMeasurementNames());
        Csv.writeCsvRow2(sw, "string", (Object[]) info.getMeasurementUnits());
        for (Map.Entry<Object, MeasurementAccumulator> entry : entitiesMeasurements.entrySet()) {
            Csv.writeCsvElement(entry.getKey().toString(), sw);
            sw.write(',');
            final long[] measurements = entry.getValue().get();
            if (measurements != null) {
                Csv.writeCsvRow(sw, measurements);
            }
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
    return sw.toString();
}
Also used : MeasurementAccumulator(org.spf4j.perf.MeasurementAccumulator) StringWriter(java.io.StringWriter) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MeasurementsInfo(org.spf4j.perf.MeasurementsInfo) HashMap(java.util.HashMap) Map(java.util.Map) TObjectLongMap(gnu.trove.map.TObjectLongMap) TObjectLongHashMap(gnu.trove.map.hash.TObjectLongHashMap) JmxExport(org.spf4j.jmx.JmxExport)

Aggregations

JmxExport (org.spf4j.jmx.JmxExport)14 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)4 AbstractRunnable (org.spf4j.base.AbstractRunnable)4 IOException (java.io.IOException)3 MeasurementRecorder (org.spf4j.perf.MeasurementRecorder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 TObjectLongMap (gnu.trove.map.TObjectLongMap)2 TObjectLongHashMap (gnu.trove.map.hash.TObjectLongHashMap)2 StringWriter (java.io.StringWriter)2 UncheckedIOException (java.io.UncheckedIOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MeasurementsInfo (org.spf4j.perf.MeasurementsInfo)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1