Skip to content

Commit

Permalink
Update CacheMaintainer to be a generic ScheduledExecutor class
Browse files Browse the repository at this point in the history
Signed-off-by: owenhalpert <[email protected]>
  • Loading branch information
owenhalpert committed Dec 19, 2024
1 parent 601a24a commit b0e7ee0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.opensearch.knn.common.exception.OutOfNativeMemoryException;
import org.opensearch.knn.common.featureflags.KNNFeatureFlags;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.ScheduledExecutor;
import org.opensearch.knn.index.util.ScheduledExecutor;
import org.opensearch.knn.plugin.stats.StatNames;

import java.io.Closeable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index;
package org.opensearch.knn.index.util;

import java.io.Closeable;
import java.util.concurrent.Executors;
Expand All @@ -12,11 +12,10 @@

/**
* Executes a task periodically
*/
public class ScheduledExecutor implements Closeable {
private final ScheduledExecutorService executor;
final Runnable task;
final ScheduledExecutorService executor;
public final Runnable task;

/**
* @param task task to be completed
Expand All @@ -25,12 +24,7 @@ public class ScheduledExecutor implements Closeable {
public ScheduledExecutor(Runnable task, long scheduleMillis) {
this.task = task;
this.executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(
task,
0,
scheduleMillis,
TimeUnit.MILLISECONDS
);
executor.scheduleAtFixedRate(task, 0, scheduleMillis, TimeUnit.MILLISECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.ScheduledExecutor;
import org.opensearch.knn.index.util.ScheduledExecutor;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -141,6 +141,8 @@ public void clear() {

@Override
public void close() throws IOException {
cacheMaintainer.close();
if (cacheMaintainer != null) {
cacheMaintainer.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;

Expand Down

0 comments on commit b0e7ee0

Please sign in to comment.