-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWMETRICS-570] initial support for automatic lock renewal
The idea is to prevent a job execution lock from expiring while the job is running without placing too much extra load on Cassandra. job execution locks will automatically renew once more than half the time to their expiration has passed. If a lock is set to expire in one hour for example, then it will renew in 30 minutes. Single execution jobs locks have a hard coded timeout of 30 minutes. That value is completely arbitrary. I did not want a value to small resulting in lots of lock renewal queries. Repepating job lock timeouts are based on the job interval. The timeout is 25% more than the interval. The compression job runs every two hours, so the lock timeout will be 2.5 hours and will renew in 75 minutes.
- Loading branch information
John Sanda
committed
Jan 31, 2017
1 parent
ddfc5cc
commit 6a6fe70
Showing
3 changed files
with
301 additions
and
33 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
job-scheduler/src/main/java/org/hawkular/metrics/scheduler/impl/Lock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed 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.hawkular.metrics.scheduler.impl; | ||
|
||
import java.util.Objects; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
/** | ||
* Represents a lock stored in the locks table in Cassandra. Locks expire using Cassandra's TTL. See | ||
* {@link LockManager} for more details. | ||
* | ||
* @author jsanda | ||
*/ | ||
public class Lock { | ||
|
||
private final String name; | ||
|
||
private final String value; | ||
|
||
/** | ||
* This should be considered an approximation since we cannot guarantee clock synchronization, and the value is | ||
* a timestamp with millisecond precision. | ||
*/ | ||
private final long expirationTime; | ||
|
||
/** | ||
* This is a duration in seconds that serves two purposes. First, it used for the lock's TTL. Secondly, it used | ||
* threshold to determine when to renew the lock. If the amount of time left before the lock expires is less than | ||
* the renewal rate, then an atempt will be made to renew it. | ||
*/ | ||
private final int renewalRate; | ||
|
||
private final boolean locked; | ||
|
||
public Lock(String name, String value, long expirationTime, int renewalRate) { | ||
this.name = name; | ||
this.value = value; | ||
this.expirationTime = expirationTime; | ||
this.renewalRate = renewalRate; | ||
this.locked = false; | ||
} | ||
|
||
public Lock(String name, String value, long expirationTime, int renewalRate, boolean locked) { | ||
this.name = name; | ||
this.value = value; | ||
this.expirationTime = expirationTime; | ||
this.renewalRate = renewalRate; | ||
this.locked = locked; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public long getExpiration() { | ||
return expirationTime; | ||
} | ||
|
||
public int getRenewalRate() { | ||
return renewalRate; | ||
} | ||
|
||
public boolean isLocked() { | ||
return locked; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Lock lock = (Lock) o; | ||
return expirationTime == lock.expirationTime && | ||
renewalRate == lock.renewalRate && | ||
locked == lock.locked && | ||
Objects.equals(name, lock.name) && | ||
Objects.equals(value, lock.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, value, expirationTime, renewalRate, locked); | ||
} | ||
|
||
@Override public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("name", name) | ||
.add("value", value) | ||
.add("expirationTime", expirationTime) | ||
.add("renewalRate", renewalRate) | ||
.add("locked", locked) | ||
.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.