Add metric for lock life duration

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177070996
This commit is contained in:
guyben 2017-11-27 14:20:36 -08:00 committed by jianglai
parent 25b49c57cd
commit 38b2cb13bf
4 changed files with 70 additions and 12 deletions

View file

@ -74,6 +74,16 @@ public class Lock extends ImmutableObject {
/** When the lock can be considered implicitly released. */
DateTime expirationTime;
/** When was the lock acquired. Used for logging. */
DateTime acquiredTime;
/** The resource name used to create lockId. */
String resourceName;
/** The tld used to create lockId. */
@Nullable
String tld;
/**
* Create a new {@link Lock} for the given resource name in the specified tld (which can be
* null for cross-tld locks).
@ -82,14 +92,18 @@ public class Lock extends ImmutableObject {
String resourceName,
@Nullable String tld,
String requestLogId,
DateTime expirationTime) {
DateTime acquiredTime,
Duration leaseLength) {
checkArgument(!Strings.isNullOrEmpty(resourceName), "resourceName cannot be null or empty");
Lock instance = new Lock();
// Add the tld to the Lock's id so that it is unique for locks acquiring the same resource
// across different TLDs.
instance.lockId = makeLockId(resourceName, tld);
instance.requestLogId = requestLogId;
instance.expirationTime = expirationTime;
instance.expirationTime = acquiredTime.plus(leaseLength);
instance.acquiredTime = acquiredTime;
instance.resourceName = resourceName;
instance.tld = tld;
return instance;
}
@ -195,7 +209,8 @@ public class Lock extends ImmutableObject {
resourceName,
tld,
requestStatusChecker.getLogId(),
now.plus(leaseLength));
now,
leaseLength);
// Locks are not parented under an EntityGroupRoot (so as to avoid write contention) and
// don't need to be backed up.
ofy().saveWithoutBackup().entity(newLock);
@ -203,7 +218,7 @@ public class Lock extends ImmutableObject {
});
logAcquireResult(acquireResult);
lockMetrics.record(resourceName, tld, acquireResult.lockState());
lockMetrics.recordAcquire(resourceName, tld, acquireResult.lockState());
return Optional.ofNullable(acquireResult.newLock());
}
@ -221,6 +236,8 @@ public class Lock extends ImmutableObject {
// Use noBackupOfy() so that we don't create a commit log entry for deleting the lock.
logger.infofmt("Deleting lock: %s", lockId);
ofy().deleteWithoutBackup().entity(Lock.this);
lockMetrics.recordRelease(
resourceName, tld, new Duration(acquiredTime, ofy().getTransactionTime()));
} else {
logger.severefmt(
"The lock we acquired was transferred to someone else before we"

View file

@ -16,30 +16,57 @@ package google.registry.model.server;
import com.google.common.collect.ImmutableSet;
import google.registry.model.server.Lock.LockState;
import google.registry.monitoring.metrics.DistributionFitter;
import google.registry.monitoring.metrics.EventMetric;
import google.registry.monitoring.metrics.ExponentialFitter;
import google.registry.monitoring.metrics.IncrementableMetric;
import google.registry.monitoring.metrics.LabelDescriptor;
import google.registry.monitoring.metrics.MetricRegistryImpl;
import javax.annotation.Nullable;
import org.joda.time.Duration;
/** Metrics for lock contention. */
class LockMetrics {
private static final ImmutableSet<LabelDescriptor> LABEL_DESCRIPTORS =
private static final ImmutableSet<LabelDescriptor> REQUEST_LABEL_DESCRIPTORS =
ImmutableSet.of(
LabelDescriptor.create("tld", "TLD"),
LabelDescriptor.create("resource", "resource name"),
LabelDescriptor.create(
"state", "The existing lock state (before attempting to acquire)."));
private static final ImmutableSet<LabelDescriptor> RELEASE_LABEL_DESCRIPTORS =
ImmutableSet.of(
LabelDescriptor.create("tld", "TLD"),
LabelDescriptor.create("resource", "resource name"));
// Finer-grained fitter than the DEFAULT_FITTER, allows values between 10 and 10*2^20, which
// gives almost 3 hours.
private static final DistributionFitter EXPONENTIAL_FITTER =
ExponentialFitter.create(20, 2.0, 10.0);
private static final IncrementableMetric lockRequestsMetric =
MetricRegistryImpl.getDefault()
.newIncrementableMetric(
"/lock/acquire_lock_requests",
"Count of lock acquisition attempts",
"count",
LABEL_DESCRIPTORS);
REQUEST_LABEL_DESCRIPTORS);
void record(String resourceName, @Nullable String tld, LockState state) {
private static final EventMetric lockLifetimeMetric =
MetricRegistryImpl.getDefault()
.newEventMetric(
"/lock/lock_duration",
"Lock lifetime",
"milliseconds",
RELEASE_LABEL_DESCRIPTORS,
EXPONENTIAL_FITTER);
void recordAcquire(String resourceName, @Nullable String tld, LockState state) {
lockRequestsMetric.increment(String.valueOf(tld), resourceName, state.name());
}
void recordRelease(String resourceName, @Nullable String tld, Duration duration) {
lockLifetimeMetric.record(duration.getMillis(), String.valueOf(tld), resourceName);
}
}