Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public long getBackedupSnapZxid() {

public synchronized void initialize() throws IOException {
try {
backupStats = new BackupStats();
backupStats = new BackupStats(backupConfig.getStatusDir());
backupBean = new BackupBean(backupStats, serverId);
MBeanRegistry.getInstance().register(backupBean, null);
LOG.info("Registered BackupBean {} with JMX.", backupBean.getName());
Expand Down Expand Up @@ -640,7 +640,7 @@ public synchronized void initialize() throws IOException {
}

// create metric-related classes for timetable backup
TimetableBackupStats timetableBackupStats = new TimetableBackupStats();
TimetableBackupStats timetableBackupStats = new TimetableBackupStats(backupConfig.getStatusDir());
timetableBackupBean = new TimetableBackupBean(timetableBackupStats);
try {
// Put timetable backup bean under backup bean, so that they are coupled. this is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public long getMinutesSinceLastSuccessfulSnapshotIteration() {
return backupStats.getMinutesSinceLastSuccessfulSnapshotIteration();
}

@Override
public long getMinutesSinceLastSuccessfulSnapshotIterationAcrossRestarts() {
return backupStats.getMinutesSinceLastSuccessfulSnapshotIterationAcrossRestarts();
}

@Override
public boolean getSnapshotBackupActiveStatus() {
return backupStats.getSnapshotBackupActiveStatus();
Expand All @@ -79,6 +84,11 @@ public long getMinutesSinceLastSuccessfulTxnLogIteration() {
return backupStats.getMinutesSinceLastSuccessfulTxnLogIteration();
}

@Override
public long getMinutesSinceLastSuccessfulTxnLogIterationAcrossRestarts() {
return backupStats.getMinutesSinceLastSuccessfulTxnLogIterationAcrossRestarts();
}

@Override
public boolean getTxnLogBackupActiveStatus() {
return backupStats.getTxnLogBackupActiveStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public interface BackupMXBean {
*/
long getMinutesSinceLastSuccessfulSnapshotIteration();

/**
* Gauge, kept in the backup status directory so that it continues after a server restart.
* Unlike getMinutesSinceLastSuccessfulSnapshotIteration(), a backup that never succeeds while
* the server keeps restarting still shows a growing value. This metric can be used for alerts.
* @return Time passed (minutes) since last successful snapshot backup iteration, including
* iterations that finished before the latest server restart
*/
long getMinutesSinceLastSuccessfulSnapshotIterationAcrossRestarts();

/**
* Gauge
* @return If snapshot backup is currently actively ongoing
Expand Down Expand Up @@ -74,6 +83,15 @@ public interface BackupMXBean {
*/
long getMinutesSinceLastSuccessfulTxnLogIteration();

/**
* Gauge, kept in the backup status directory so that it continues after a server restart.
* Unlike getMinutesSinceLastSuccessfulTxnLogIteration(), a backup that never succeeds while
* the server keeps restarting still shows a growing value. This metric can be used for alerts.
* @return Time passed (minutes) since last successful txn log backup iteration, including
* iterations that finished before the latest server restart
*/
long getMinutesSinceLastSuccessfulTxnLogIterationAcrossRestarts();

/**
* Gauge
* @return If txn log backup is currently actively ongoing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.zookeeper.server.backup.monitoring;

import java.io.File;
import java.util.function.LongSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,25 +29,68 @@
public class BackupStats {
private static final Logger LOG = LoggerFactory.getLogger(BackupStats.class);

/**
* Files in the backup status directory holding the finish time of the last successful iteration.
*/
static final String LAST_SUCCESSFUL_SNAPSHOT_ITERATION_FILENAME = "zkBackupLastSuccessfulSnapshotIteration";
static final String LAST_SUCCESSFUL_TXNLOG_ITERATION_FILENAME = "zkBackupLastSuccessfulTxnLogIteration";

private final LongSupplier clock;
private final PersistedTimestamp persistedSnapshotSuccess;
private final PersistedTimestamp persistedTxnLogSuccess;

/*
Snapshot backup metric declarations
*/
private int failedSnapshotIterationCount = 0;
private long lastSuccessfulSnapshotBackupIterationFinishTime = System.currentTimeMillis();
private long lastSuccessfulSnapshotBackupIterationFinishTime;
private volatile long lastSuccessfulSnapshotIterationFinishTimeAcrossRestarts;
private boolean snapshotBackupActive = false;
private long snapshotIterationDuration = 0L;
private int numberOfSnapshotFilesBackedUpLastIteration = 0;
private int numberOfSnapshotFilesBackedUpThisIteration = 0;
private long lastSnapshotBackupIterationStartTime = System.currentTimeMillis();
private long lastSnapshotBackupIterationStartTime;

/*
TxLog backup metric declarations
*/
private int failedTxnLogIterationCount = 0;
private long lastSuccessfulTxnLogBackupIterationFinishTime = System.currentTimeMillis();
private long lastSuccessfulTxnLogBackupIterationFinishTime;
private volatile long lastSuccessfulTxnLogIterationFinishTimeAcrossRestarts;
private boolean txnLogBackupActive = false;
private long txnLogIterationDuration = 0L;
private long lastTxnLogBackupIterationStartTime = System.currentTimeMillis();
private long lastTxnLogBackupIterationStartTime;

/**
* Create stats that start over with the process; nothing is persisted.
*/
public BackupStats() {
this(null, System::currentTimeMillis);
}

/**
* Create stats that keep the last successful iteration times in the backup status directory, so
* the "AcrossRestarts" gauges continue after a restart.
* @param statusDir the backup status directory; null disables persistence
*/
public BackupStats(File statusDir) {
this(statusDir, System::currentTimeMillis);
}

BackupStats(File statusDir, LongSupplier clock) {
this.clock = clock;
long now = clock.getAsLong();
lastSuccessfulSnapshotBackupIterationFinishTime = now;
lastSnapshotBackupIterationStartTime = now;
lastSuccessfulTxnLogBackupIterationFinishTime = now;
lastTxnLogBackupIterationStartTime = now;
persistedSnapshotSuccess = new PersistedTimestamp(statusDir, LAST_SUCCESSFUL_SNAPSHOT_ITERATION_FILENAME);
persistedTxnLogSuccess = new PersistedTimestamp(statusDir, LAST_SUCCESSFUL_TXNLOG_ITERATION_FILENAME);
lastSuccessfulSnapshotIterationFinishTimeAcrossRestarts =
Math.min(now, persistedSnapshotSuccess.readOrDefault(now));
lastSuccessfulTxnLogIterationFinishTimeAcrossRestarts =
Math.min(now, persistedTxnLogSuccess.readOrDefault(now));
}

// Snapshot backup metrics

Expand All @@ -64,10 +109,19 @@ public int getNumConsecutiveFailedSnapshotIterations() {
* @return Time passed (minutes) since last successful snapshot backup iteration
*/
public long getMinutesSinceLastSuccessfulSnapshotIteration() {
return (System.currentTimeMillis() - lastSuccessfulSnapshotBackupIterationFinishTime) / (60
return (clock.getAsLong() - lastSuccessfulSnapshotBackupIterationFinishTime) / (60
* 1000);
}

/**
* Gauge
* @return Time passed (minutes) since last successful snapshot backup iteration, including
* iterations that finished before the latest server restart
*/
public long getMinutesSinceLastSuccessfulSnapshotIterationAcrossRestarts() {
return (clock.getAsLong() - lastSuccessfulSnapshotIterationFinishTimeAcrossRestarts) / (60 * 1000);
}

/**
* Gauge
* @return If snapshot backup is currently actively ongoing
Expand Down Expand Up @@ -96,7 +150,7 @@ public int getNumberOfSnapshotFilesBackedUpLastIteration() {
* Record the status and timestamp when a snapshot backup iteration starts
*/
public void setSnapshotBackupIterationStart() {
lastSnapshotBackupIterationStartTime = System.currentTimeMillis();
lastSnapshotBackupIterationStartTime = clock.getAsLong();
snapshotBackupActive = true;
numberOfSnapshotFilesBackedUpThisIteration = 0;
}
Expand All @@ -106,13 +160,15 @@ public void setSnapshotBackupIterationStart() {
* @param errorFree If this iteration finishes without error
*/
public void setSnapshotBackupIterationDone(boolean errorFree) {
long finishTime = System.currentTimeMillis();
long finishTime = clock.getAsLong();
snapshotIterationDuration = finishTime - lastSnapshotBackupIterationStartTime;
snapshotBackupActive = false;
numberOfSnapshotFilesBackedUpLastIteration = numberOfSnapshotFilesBackedUpThisIteration;
numberOfSnapshotFilesBackedUpThisIteration = 0;
if (errorFree) {
lastSuccessfulSnapshotBackupIterationFinishTime = finishTime;
lastSuccessfulSnapshotIterationFinishTimeAcrossRestarts = finishTime;
persistedSnapshotSuccess.write(finishTime);
failedSnapshotIterationCount = 0;
} else {
failedSnapshotIterationCount++;
Expand Down Expand Up @@ -144,10 +200,19 @@ public int getNumConsecutiveFailedTxnLogIterations() {
* @return Time passed (minutes) since last successful txn log backup iteration
*/
public long getMinutesSinceLastSuccessfulTxnLogIteration() {
return (System.currentTimeMillis() - lastSuccessfulTxnLogBackupIterationFinishTime) / (60
return (clock.getAsLong() - lastSuccessfulTxnLogBackupIterationFinishTime) / (60
* 1000);
}

/**
* Gauge
* @return Time passed (minutes) since last successful txn log backup iteration, including
* iterations that finished before the latest server restart
*/
public long getMinutesSinceLastSuccessfulTxnLogIterationAcrossRestarts() {
return (clock.getAsLong() - lastSuccessfulTxnLogIterationFinishTimeAcrossRestarts) / (60 * 1000);
}

/**
* Gauge
* @return If txn log backup is currently actively ongoing
Expand All @@ -168,7 +233,7 @@ public long getTxnLogIterationDuration() {
* Record the status and timestamp when a txn log backup iteration starts
*/
public void setTxnLogBackupIterationStart() {
lastTxnLogBackupIterationStartTime = System.currentTimeMillis();
lastTxnLogBackupIterationStartTime = clock.getAsLong();
txnLogBackupActive = true;
}

Expand All @@ -177,11 +242,13 @@ public void setTxnLogBackupIterationStart() {
* @param errorFree If this iteration finishes without error
*/
public void setTxnLogBackupIterationDone(boolean errorFree) {
long finishTime = System.currentTimeMillis();
long finishTime = clock.getAsLong();
txnLogBackupActive = false;
txnLogIterationDuration = finishTime - lastTxnLogBackupIterationStartTime;
if (errorFree) {
lastSuccessfulTxnLogBackupIterationFinishTime = finishTime;
lastSuccessfulTxnLogIterationFinishTimeAcrossRestarts = finishTime;
persistedTxnLogSuccess.write(finishTime);
failedTxnLogIterationCount = 0;
} else {
failedTxnLogIterationCount++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zookeeper.server.backup.monitoring;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A wall-clock timestamp kept in a small file in the backup status directory so that it survives
* server restarts. Failures to read or write are logged and never interrupt the backup.
*/
class PersistedTimestamp {
private static final Logger LOG = LoggerFactory.getLogger(PersistedTimestamp.class);

private final Path dir;
private final String fileName;

/**
* @param dir directory holding the file; null disables persistence
* @param fileName name of the file inside {@code dir}
*/
PersistedTimestamp(File dir, String fileName) {
this.dir = dir == null ? null : dir.toPath();
this.fileName = fileName;
}

/**
* @return the stored timestamp, or {@code defaultValue} if none is stored or it cannot be read
*/
long readOrDefault(long defaultValue) {
if (dir == null) {
return defaultValue;
}
Path file = dir.resolve(fileName);
if (!Files.exists(file)) {
return defaultValue;
}
try {
return Long.parseLong(new String(Files.readAllBytes(file), StandardCharsets.UTF_8).trim());
} catch (IOException | NumberFormatException e) {
LOG.warn("Ignoring unreadable backup timestamp file {}", file, e);
return defaultValue;
}
}

void write(long timestamp) {
if (dir == null) {
return;
}
Path file = dir.resolve(fileName);
Path tmp = dir.resolve(fileName + ".tmp");
try {
Files.createDirectories(dir);
Files.write(tmp, Long.toString(timestamp).getBytes(StandardCharsets.UTF_8));
try {
Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
LOG.warn("Failed to persist backup timestamp to {}", file, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public long getMinutesSinceLastSuccessfulTimetableIteration() {
return backupStats.getMinutesSinceLastSuccessfulTimetableIteration();
}

@Override
public long getMinutesSinceLastSuccessfulTimetableIterationAcrossRestarts() {
return backupStats.getMinutesSinceLastSuccessfulTimetableIterationAcrossRestarts();
}

@Override
public boolean getTimetableBackupActiveStatus() {
return backupStats.getTimetableBackupActiveStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public interface TimetableBackupMXBean {
*/
long getMinutesSinceLastSuccessfulTimetableIteration();

/**
* Gauge, kept in the backup status directory so that it continues after a server restart.
* Unlike getMinutesSinceLastSuccessfulTimetableIteration(), a backup that never succeeds while
* the server keeps restarting still shows a growing value. This metric can be used for alerts.
* @return Time passed (minutes) since last successful timetable backup iteration, including
* iterations that finished before the latest server restart
*/
long getMinutesSinceLastSuccessfulTimetableIterationAcrossRestarts();

/**
* Gauge
* @return If timetable backup is currently in progress
Expand Down
Loading
Loading