-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce lock duration and renew the lock during update #299
Merged
heemin32
merged 1 commit into
opensearch-project:feature/ip2geo
from
heemin32:ip2geo-create
May 10, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -7,24 +7,25 @@ | |
|
||
import static org.opensearch.geospatial.ip2geo.jobscheduler.DatasourceExtension.JOB_INDEX_NAME; | ||
|
||
import java.time.Instant; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.action.ActionListener; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.jobscheduler.spi.LockModel; | ||
import org.opensearch.jobscheduler.spi.utils.LockService; | ||
|
||
/** | ||
* A wrapper of job scheduler's lock service for datasource | ||
*/ | ||
public class Ip2GeoLockService { | ||
public static final long LOCK_DURATION_IN_SECONDS = 300l; | ||
public static final long RENEW_AFTER_IN_SECONDS = 120l; | ||
private final ClusterService clusterService; | ||
private final Client client; | ||
private final LockService lockService; | ||
|
||
/** | ||
|
@@ -33,10 +34,8 @@ public class Ip2GeoLockService { | |
* @param clusterService the cluster service | ||
* @param client the client | ||
*/ | ||
@Inject | ||
public Ip2GeoLockService(final ClusterService clusterService, final Client client) { | ||
this.clusterService = clusterService; | ||
this.client = client; | ||
this.lockService = new LockService(client, clusterService); | ||
} | ||
|
||
|
@@ -68,10 +67,9 @@ public void releaseLock(final LockModel lockModel, final ActionListener<Boolean> | |
* Synchronous method of LockService#renewLock | ||
* | ||
* @param lockModel lock to renew | ||
* @param timeout timeout in milliseconds precise | ||
* @return renewed lock if renew succeed and null otherwise | ||
*/ | ||
public LockModel renewLock(final LockModel lockModel, final TimeValue timeout) { | ||
public LockModel renewLock(final LockModel lockModel) { | ||
AtomicReference<LockModel> lockReference = new AtomicReference(); | ||
CountDownLatch countDownLatch = new CountDownLatch(1); | ||
lockService.renewLock(lockModel, new ActionListener<>() { | ||
|
@@ -89,10 +87,34 @@ public void onFailure(final Exception e) { | |
}); | ||
|
||
try { | ||
countDownLatch.await(timeout.getMillis(), TimeUnit.MILLISECONDS); | ||
countDownLatch.await(clusterService.getClusterSettings().get(Ip2GeoSettings.TIMEOUT).getSeconds(), TimeUnit.SECONDS); | ||
return lockReference.get(); | ||
} catch (InterruptedException e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Return a runnable which can renew the given lock model | ||
* | ||
* The runnable renews the lock and store the renewed lock in the AtomicReference. | ||
* It only renews the lock when it passed {@code RENEW_AFTER_IN_SECONDS} since | ||
* the last time the lock was renewed to avoid resource abuse. | ||
* | ||
* @param lockModel lock model to renew | ||
* @return runnable which can renew the given lock for every call | ||
*/ | ||
public Runnable getRenewLockRunnable(final AtomicReference<LockModel> lockModel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a JavaDoc for this public method? |
||
return () -> { | ||
LockModel preLock = lockModel.get(); | ||
if (Instant.now().isBefore(preLock.getLockTime().plusSeconds(RENEW_AFTER_IN_SECONDS))) { | ||
return; | ||
} | ||
|
||
lockModel.set(renewLock(lockModel.get())); | ||
if (lockModel.get() == null) { | ||
new OpenSearchException("failed to renew a lock [{}]", preLock); | ||
} | ||
}; | ||
} | ||
} |
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: check for non-null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry i meant during runtime as well.