-
Notifications
You must be signed in to change notification settings - Fork 0
Implement FileCache Prune API endpoint with Service Locator Pattern #1
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
base: PruneApi
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Owner
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. THINK IN DEPTH AND DETAIL , READ AND UNDERSTAND THE COMMENT AND THE CONTEXT , THEN USE YOUR OWN THINKING AND UNDERSTANDING TO IMPLEMENT THE CODE CHANGES. Prune API via Service Locator Pattern: Phase 3: Update the Call Site (Node.java) File to Modify: server/src/main/java/org/opensearch/node/Node.java Action: Update the call to actionModule.initRestHandlers to match the new signature. code context : "// ... inside the Node constructor, near the end
Owner
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. THINK IN DEPTH AND DETAIL , READ AND UNDERSTAND THE COMMENT AND THE CONTEXT , THEN USE YOUR OWN THINKING AND UNDERSTANDING TO IMPLEMENT THE CODE CHANGES. Prune API via Service Locator Pattern: Phase 3: Update the Call Site (Node.java) File to Modify: server/src/main/java/org/opensearch/node/Node.java Action: Update the call to actionModule.initRestHandlers to match the new signature. code context : "// ... inside the Node constructor, near the end } finally {
Owner
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. @copilot Prune API via Service Locator Pattern: Phase 3: Update the Call Site (Node.java) File to Modify: server/src/main/java/org/opensearch/node/Node.java Action: Update the call to actionModule.initRestHandlers to match the new signature. code context : "// ... inside the Node constructor, near the end // ... logger.debug("initializing HTTP handlers ..."); success = true;
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. Phase 3 completed successfully. Updated Node.java call site to pass the Node instance as the service locator: Changed from: actionModule.initRestHandlers(() -> clusterService.state().nodes(), fileCache());To: actionModule.initRestHandlers(() -> clusterService.state().nodes(), this);The Node now passes itself as the service locator to ActionModule as planned. Commit: 02abc04 |
|
Owner
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. Prune API via Service Locator Pattern: Phase 1: Implement the REST Handler (RestPruneCacheAction.java) File to Create: server/src/main/java/org/opensearch/rest/action/admin/cluster/RestPruneCacheAction.java Action: Create the new REST handler class. It will extend BaseRestHandler and use the Node object passed into its constructor to get a reference to the FileCache. draft version of code : "public class RestPruneCacheAction extends BaseRestHandler { private final FileCache fileCache; // The constructor now accepts the 'Node' object as the Service Locator. @OverRide @OverRide @OverRide }
Owner
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. THINK IN DEPTH AND DETAIL , READ AND UNDERSTAND THE COMMENT AND THE CONTEXT , THEN USE YOUR OWN THINKING AND UNDERSTANDING TO IMPLEMENT THE CODE CHANGES. Prune API via Service Locator Pattern: Phase 1: Implement the REST Handler (RestPruneCacheAction.java) File to Create: server/src/main/java/org/opensearch/rest/action/admin/cluster/RestPruneCacheAction.java Action: Create the new REST handler class. It will extend BaseRestHandler and use the Node object passed into its constructor to get a reference to the FileCache. draft version of code : "public class RestPruneCacheAction extends BaseRestHandler { private final FileCache fileCache; // The constructor now accepts the 'Node' object as the Service Locator. @OverRide @OverRide @OverRide };
Owner
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. @copilot Prune API via Service Locator Pattern: Phase 1: Implement the REST Handler (RestPruneCacheAction.java) File to Create: server/src/main/java/org/opensearch/rest/action/admin/cluster/RestPruneCacheAction.java Action: Create the new REST handler class. It will extend BaseRestHandler and use the Node object passed into its constructor to get a reference to the FileCache. draft version of code : "public class RestPruneCacheAction extends BaseRestHandler { private final FileCache fileCache; // The constructor now accepts the 'Node' object as the Service Locator. @OverRide @OverRide @OverRide // Build the JSON response as specified. channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
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. Phase 1 completed successfully. Updated RestPruneCacheAction to use the Service Locator Pattern:
The REST handler now uses the Node as a service locator to retrieve the specific service it needs, exactly as outlined in your implementation plan. Commit: 02abc04 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.rest.action.admin.cluster; | ||
|
|
||
| import org.opensearch.core.rest.RestStatus; | ||
| import org.opensearch.core.xcontent.XContentBuilder; | ||
| import org.opensearch.index.store.remote.filecache.FileCache; | ||
| import org.opensearch.rest.BaseRestHandler; | ||
| import org.opensearch.rest.BytesRestResponse; | ||
| import org.opensearch.rest.RestRequest; | ||
| import org.opensearch.rest.RestRequest.Method; | ||
| import org.opensearch.transport.client.node.NodeClient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
|
|
||
| /** | ||
| * REST action to manually trigger FileCache prune operation. | ||
| * This endpoint allows administrators to clear out non-referenced cache entries on demand. | ||
| * | ||
| * @opensearch.api | ||
| */ | ||
| public class RestPruneCacheAction extends BaseRestHandler { | ||
|
|
||
| private final FileCache fileCache; | ||
|
|
||
| public RestPruneCacheAction(FileCache fileCache) { | ||
| this.fileCache = fileCache; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Route> routes() { | ||
| return singletonList(new Route(Method.POST, "/_cache/remote/prune")); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "prune_cache_action"; | ||
| } | ||
|
|
||
| @Override | ||
| protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
| return channel -> { | ||
| // Execute the prune operation | ||
| long prunedBytes = fileCache.prune(); | ||
|
|
||
| // Build JSON response | ||
| XContentBuilder builder = channel.newBuilder(); | ||
| builder.startObject(); | ||
| builder.field("acknowledged", true); | ||
| builder.field("pruned_bytes", prunedBytes); | ||
| builder.endObject(); | ||
|
|
||
| channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder)); | ||
| }; | ||
| } | ||
| } |
|
Owner
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. @copilot Phase 5: Update Existing Tests (ActionModuleTests.java) File to Modify: server/src/test/java/org/opensearch/action/ActionModuleTests.java Action: Update all calls to initRestHandlers to pass a mock(Node.class). DRAFT CONTEXT : "// ... imports // ... inside ActionModuleTests class // In any test method that calls initRestHandlers:
Owner
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. THINK IN DEPTH AND DETAIL , READ AND UNDERSTAND THE COMMENT AND THE CONTEXT , THEN USE YOUR OWN THINKING AND UNDERSTANDING TO IMPLEMENT THE CODE CHANGES. Phase 5: Update Existing Tests (ActionModuleTests.java) File to Modify: server/src/test/java/org/opensearch/action/ActionModuleTests.java Action: Update all calls to initRestHandlers to pass a mock(Node.class). DRAFT CONTEXT : "// ... imports // ... inside ActionModuleTests class // In any test method that calls initRestHandlers:
Owner
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. @copilot Phase 5: Update Existing Tests (ActionModuleTests.java) File to Modify: server/src/test/java/org/opensearch/action/ActionModuleTests.java Action: Update all calls to initRestHandlers to pass a mock(Node.class). DRAFT CONTEXT : "// ... imports // ... inside ActionModuleTests class // In any test method that calls initRestHandlers:
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. Phase 5 completed successfully. Updated ActionModuleTests.java to work with the new Service Locator Pattern:
All ActionModule tests now pass with the new signature that uses Node as the service locator. Commit: 02abc04 |
|
Owner
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. Prune API via Service Locator Pattern: Phase 4: Implement Unit Tests (RestPruneCacheActionTests.java) File to Create: server/src/test/java/org/opensearch/rest/action/admin/cluster/RestPruneCacheActionTests.java Action: Create a new test class that extends RestActionTestCase. Draft code context : "/* SPDX-License-Identifier: Apache-2.0 import org.opensearch.core.rest.RestStatus; import static org.mockito.Mockito.mock; public class RestPruneCacheActionTests extends RestActionTestCase { public void testGetName() { public void testSuccessfulPrune() throws Exception { // 2. Mock the Node (Service Locator) to return the mocked service // 3. Create the action with the mocked locator // 4. Dispatch the request and verify the response
Owner
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. Prune API via Service Locator Pattern: Phase 4: Implement Unit Tests (RestPruneCacheActionTests.java) File to Create: server/src/test/java/org/opensearch/rest/action/admin/cluster/RestPruneCacheActionTests.java Action: Create a new test class that extends RestActionTestCase. Draft code context : "/* SPDX-License-Identifier: Apache-2.0 import org.opensearch.core.rest.RestStatus; import static org.mockito.Mockito.mock; public class RestPruneCacheActionTests extends RestActionTestCase { public void testGetName() { public void testSuccessfulPrune() throws Exception { // 2. Mock the Node (Service Locator) to return the mocked service // 3. Create the action with the mocked locator // 4. Dispatch the request and verify the response
Owner
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. Prune API via Service Locator Pattern: Phase 4: Implement Unit Tests (RestPruneCacheActionTests.java) File to Create: server/src/test/java/org/opensearch/rest/action/admin/cluster/RestPruneCacheActionTests.java Action: Create a new test class that extends RestActionTestCase. Draft code context : "/* SPDX-License-Identifier: Apache-2.0 import org.opensearch.core.rest.RestStatus; import static org.mockito.Mockito.mock; public class RestPruneCacheActionTests extends RestActionTestCase { public void testGetName() { public void testSuccessfulPrune() throws Exception { // 2. Mock the Node (Service Locator) to return the mocked service // 3. Create the action with the mocked locator // 4. Dispatch the request and verify the response
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. Phase 4 completed successfully. Updated RestPruneCacheActionTests.java to use the Service Locator Pattern:
The comprehensive unit tests now properly validate the Service Locator Pattern implementation, with the Node object providing access to the FileCache service. Commit: 02abc04 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.rest.action.admin.cluster; | ||
|
|
||
| import org.opensearch.core.rest.RestStatus; | ||
| import org.opensearch.index.store.remote.filecache.FileCache; | ||
| import org.opensearch.rest.RestRequest; | ||
| import org.opensearch.test.rest.FakeRestRequest; | ||
| import org.opensearch.test.rest.RestActionTestCase; | ||
| import org.opensearch.threadpool.TestThreadPool; | ||
| import org.opensearch.threadpool.ThreadPool; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class RestPruneCacheActionTests extends RestActionTestCase { | ||
|
|
||
| private ThreadPool threadPool; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| threadPool = new TestThreadPool(this.getClass().getSimpleName()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| super.tearDown(); | ||
| terminate(threadPool); | ||
| } | ||
|
|
||
| public void testRoutes() { | ||
| RestPruneCacheAction action = new RestPruneCacheAction(mock(FileCache.class)); | ||
| assertEquals(1, action.routes().size()); | ||
| assertEquals(RestRequest.Method.POST, action.routes().get(0).getMethod()); | ||
| assertEquals("/_cache/remote/prune", action.routes().get(0).getPath()); | ||
| } | ||
|
|
||
| public void testGetName() { | ||
| RestPruneCacheAction action = new RestPruneCacheAction(mock(FileCache.class)); | ||
| assertEquals("prune_cache_action", action.getName()); | ||
| } | ||
|
|
||
| public void testPrepareRequest() throws Exception { | ||
| FileCache mockFileCache = mock(FileCache.class); | ||
| when(mockFileCache.prune()).thenReturn(12345678L); | ||
|
|
||
| RestPruneCacheAction action = new RestPruneCacheAction(mockFileCache); | ||
| RestRequest request = new FakeRestRequest(); | ||
|
|
||
| // Test that the action prepares correctly without throwing exceptions | ||
| assertNotNull(action.prepareRequest(request, 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.
@copilot
Prune API via Service Locator Pattern:
The Node object will serve as our "Service Locator."
The core idea is to pass the top-level Node object, which "owns" all services, down to the point of REST handler creation. The handler then uses the Node object to locate and retrieve the specific service it needs (FileCache).
Phase 2: Modify the Handler Registration Point (ActionModule.java)
This is the "middleman" that connects the provider and the consumer. We will modify its initRestHandlers method to accept the Node service locator.
File to Modify: server/src/main/java/org/opensearch/action/ActionModule.java
Action: Change the method signature of initRestHandlers to accept Node and use it when instantiating RestPruneCacheAction.
draft code context : "// ... imports
import org.opensearch.node.Node; // <-- Import the Node class
// ... other imports
import org.opensearch.rest.action.admin.cluster.RestPruneCacheAction; // <-- Import the new action
// ... inside the ActionModule class
// 1. Change the method signature to accept 'Node'.
public void initRestHandlers(Supplier nodesInCluster, Node node) {
List catActions = new ArrayList<>();
List listActions = new ArrayList<>();
Consumer registerHandler = handler -> {
// ... (no changes here)
};
}
// ..."
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.
@copilot
Prune API via Service Locator Pattern:
The Node object will serve as our "Service Locator."
The core idea is to pass the top-level Node object, which "owns" all services, down to the point of REST handler creation. The handler then uses the Node object to locate and retrieve the specific service it needs (FileCache).
Phase 2: Modify the Handler Registration Point (ActionModule.java)
This is the "middleman" that connects the provider and the consumer. We will modify its initRestHandlers method to accept the Node service locator.
File to Modify: server/src/main/java/org/opensearch/action/ActionModule.java
Action: Change the method signature of initRestHandlers to accept Node and use it when instantiating RestPruneCacheAction.
draft code context : "// ... imports
import org.opensearch.node.Node; // <-- Import the Node class
// ... other imports
import org.opensearch.rest.action.admin.cluster.RestPruneCacheAction; // <-- Import the new action
// ... inside the ActionModule class
// 1. Change the method signature to accept 'Node'.
public void initRestHandlers(Supplier nodesInCluster, Node node) {
List catActions = new ArrayList<>();
List listActions = new ArrayList<>();
Consumer registerHandler = handler -> {
// ... (no changes here)
};
// ... (all existing handler registrations remain unchanged)
registerHandler.accept(new RestNodesStatsAction());
// ... etc.
// 2. Add the new handler registration, passing the 'node' locator.
// FileCache API
registerHandler.accept(new RestPruneCacheAction(node));
// ... (rest of the method)
}
// ..."
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.
@copilot
Prune API via Service Locator Pattern:
The Node object will serve as our "Service Locator."
The core idea is to pass the top-level Node object, which "owns" all services, down to the point of REST handler creation. The handler then uses the Node object to locate and retrieve the specific service it needs (FileCache).
Phase 2: Modify the Handler Registration Point (ActionModule.java)
This is the "middleman" that connects the provider and the consumer. We will modify its initRestHandlers method to accept the Node service locator.
File to Modify: server/src/main/java/org/opensearch/action/ActionModule.java
Action: Change the method signature of initRestHandlers to accept Node and use it when instantiating RestPruneCacheAction.
draft code context : "// ... imports
import org.opensearch.node.Node; // <-- Import the Node class
// ... other imports
import org.opensearch.rest.action.admin.cluster.RestPruneCacheAction; // <-- Import the new action
// ... inside the ActionModule class
// 1. Change the method signature to accept 'Node'.
public void initRestHandlers(Supplier nodesInCluster, Node node) {
List catActions = new ArrayList<>();
List listActions = new ArrayList<>();
Consumer registerHandler = handler -> {
// ... (no changes here)
};
// ... (all existing handler registrations remain unchanged)
registerHandler.accept(new RestNodesStatsAction());
// ... etc.
// 2. Add the new handler registration, passing the 'node' locator.
// FileCache API
registerHandler.accept(new RestPruneCacheAction(node));
// ... (rest of the method)
}
// ..."
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.
Phase 2 completed successfully. Updated ActionModule.java to:
import org.opensearch.node.Node;initRestHandlersmethod signature to acceptNode nodeinstead ofFileCache fileCachenodeinstead offileCacheThe handler registration now uses the Service Locator Pattern as requested.
Commit: 02abc04