generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 49
Add wrapper class of job scheduler lock service #290
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
Merged
heemin32
merged 1 commit into
opensearch-project:feature/ip2geo
from
heemin32:ip2geo-lock
May 5, 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
98 changes: 98 additions & 0 deletions
98
src/main/java/org/opensearch/geospatial/ip2geo/common/Ip2GeoLockService.java
This file contains hidden or 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,98 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.geospatial.ip2geo.common; | ||
|
|
||
| import static org.opensearch.geospatial.ip2geo.jobscheduler.DatasourceExtension.JOB_INDEX_NAME; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| 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 { | ||
|
Member
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. Are you planning to add an interface for the service that this class will implement?
Collaborator
Author
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. I don't have such plan. |
||
| private final ClusterService clusterService; | ||
| private final Client client; | ||
| private final LockService lockService; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @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); | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper method of LockService#acquireLockWithId | ||
| * | ||
| * Datasource use its name as doc id in job scheduler. Therefore, we can use datasource name to acquire | ||
| * a lock on a datasource. | ||
| * | ||
| * @param datasourceName datasourceName to acquire lock on | ||
| * @param lockDurationSeconds the lock duration in seconds | ||
| * @param listener the listener | ||
| */ | ||
| public void acquireLock(final String datasourceName, final Long lockDurationSeconds, final ActionListener<LockModel> listener) { | ||
| lockService.acquireLockWithId(JOB_INDEX_NAME, lockDurationSeconds, datasourceName, listener); | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper method of LockService#release | ||
| * | ||
| * @param lockModel the lock model | ||
| * @param listener the listener | ||
| */ | ||
| public void releaseLock(final LockModel lockModel, final ActionListener<Boolean> listener) { | ||
| lockService.release(lockModel, listener); | ||
| } | ||
|
|
||
| /** | ||
| * 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) { | ||
| AtomicReference<LockModel> lockReference = new AtomicReference(); | ||
| CountDownLatch countDownLatch = new CountDownLatch(1); | ||
| lockService.renewLock(lockModel, new ActionListener<>() { | ||
| @Override | ||
| public void onResponse(final LockModel lockModel) { | ||
| lockReference.set(lockModel); | ||
| countDownLatch.countDown(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(final Exception e) { | ||
| lockReference.set(null); | ||
| countDownLatch.countDown(); | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| countDownLatch.await(timeout.getMillis(), TimeUnit.MILLISECONDS); | ||
| return lockReference.get(); | ||
| } catch (InterruptedException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
src/test/java/org/opensearch/geospatial/ip2geo/common/Ip2GeoLockServiceTests.java
This file contains hidden or 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,46 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
heemin32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| package org.opensearch.geospatial.ip2geo.common; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import org.junit.Before; | ||
| import org.opensearch.action.ActionListener; | ||
| import org.opensearch.common.unit.TimeValue; | ||
| import org.opensearch.geospatial.GeospatialTestHelper; | ||
| import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase; | ||
|
|
||
| public class Ip2GeoLockServiceTests extends Ip2GeoTestCase { | ||
| private Ip2GeoLockService ip2GeoLockService; | ||
|
|
||
| @Before | ||
| public void init() { | ||
| ip2GeoLockService = new Ip2GeoLockService(clusterService, client); | ||
| } | ||
|
|
||
| public void testAcquireLock_whenValidInput_thenSucceed() { | ||
| // Cannot test because LockService is final class | ||
| // Simply calling method to increase coverage | ||
| ip2GeoLockService.acquireLock(GeospatialTestHelper.randomLowerCaseString(), randomPositiveLong(), mock(ActionListener.class)); | ||
| } | ||
|
|
||
| public void testReleaseLock_whenValidInput_thenSucceed() { | ||
| // Cannot test because LockService is final class | ||
| // Simply calling method to increase coverage | ||
| ip2GeoLockService.releaseLock(null, mock(ActionListener.class)); | ||
| } | ||
|
|
||
| public void testRenewLock_whenCalled_thenNotBlocked() { | ||
| long timeoutInMillis = 10000; | ||
| long expectedDurationInMillis = 1000; | ||
| Instant before = Instant.now(); | ||
| assertNull(ip2GeoLockService.renewLock(null, TimeValue.timeValueMillis(timeoutInMillis))); | ||
| Instant after = Instant.now(); | ||
| assertTrue(after.toEpochMilli() - before.toEpochMilli() < expectedDurationInMillis); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.