-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add thread to perform pending cache maintenance every minute
Signed-off-by: owenhalpert <[email protected]>
- Loading branch information
1 parent
dc369e6
commit 85b1782
Showing
10 changed files
with
141 additions
and
3 deletions.
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
46 changes: 46 additions & 0 deletions
46
src/main/java/org/opensearch/knn/index/util/ScheduledExecutor.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,46 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.util; | ||
|
||
import lombok.Getter; | ||
|
||
import java.io.Closeable; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Executes a task periodically | ||
*/ | ||
public class ScheduledExecutor implements Closeable { | ||
final ScheduledExecutorService executor; | ||
@Getter | ||
private final Runnable task; | ||
|
||
/** | ||
* @param task task to be completed | ||
* @param scheduleMillis time in milliseconds to wait before executing the task again | ||
*/ | ||
public ScheduledExecutor(Runnable task, long scheduleMillis) { | ||
this.task = task; | ||
this.executor = Executors.newSingleThreadScheduledExecutor(); | ||
executor.scheduleAtFixedRate(task, 0, scheduleMillis, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
executor.shutdown(); | ||
try { | ||
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) { | ||
executor.shutdownNow(); | ||
} | ||
} catch (InterruptedException e) { | ||
executor.shutdownNow(); | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
} |
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
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
35 changes: 35 additions & 0 deletions
35
src/test/java/org/opensearch/knn/index/CacheMaintainerTests.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,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import org.junit.Test; | ||
import org.opensearch.knn.index.util.ScheduledExecutor; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CacheMaintainerTests { | ||
@Test | ||
public void testCacheEviction() throws InterruptedException { | ||
Cache<String, String> testCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.SECONDS).build(); | ||
|
||
ScheduledExecutor executor = new ScheduledExecutor(testCache::cleanUp, 60 * 1000); | ||
|
||
testCache.put("key1", "value1"); | ||
assertEquals(testCache.size(), 1); | ||
|
||
Thread.sleep(1500); | ||
|
||
executor.getTask().run(); | ||
|
||
assertEquals(testCache.size(), 0); | ||
|
||
executor.close(); | ||
} | ||
} |
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