-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-944 HTTP REST log command doesn't work #383
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
Changes from all commits
0beb728
b0bc1e3
a06ecbf
86b8cac
e7640ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.ignite.internal.processors.rest.handlers.log; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import org.apache.ignite.IgniteCheckedException; | ||
| import org.apache.ignite.internal.GridKernalContext; | ||
| import org.apache.ignite.internal.IgniteInternalFuture; | ||
| import org.apache.ignite.internal.processors.rest.GridRestCommand; | ||
| import org.apache.ignite.internal.processors.rest.GridRestResponse; | ||
| import org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandlerAdapter; | ||
| import org.apache.ignite.internal.processors.rest.request.GridRestLogRequest; | ||
| import org.apache.ignite.internal.processors.rest.request.GridRestRequest; | ||
| import org.apache.ignite.internal.util.future.GridFinishedFuture; | ||
| import org.apache.ignite.internal.util.typedef.internal.U; | ||
|
|
||
| import static org.apache.ignite.internal.processors.rest.GridRestCommand.LOG; | ||
|
|
||
| /** | ||
| * Handler for {@link org.apache.ignite.internal.processors.rest.GridRestCommand#LOG} command. | ||
| */ | ||
| public class GridLogCommandHandler extends GridRestCommandHandlerAdapter { | ||
| /** | ||
| * Supported commands. | ||
| */ | ||
| private static final Collection<GridRestCommand> SUPPORTED_COMMANDS = U.sealList(LOG); | ||
|
|
||
| /** | ||
| * Default log file name * | ||
| */ | ||
| private static final String DEFAULT_LOG_PATH = "work/log/ignite.log"; | ||
|
|
||
| /** | ||
| * Default log file start line number * | ||
| */ | ||
| private static final int DEFAULT_FROM = 0; | ||
|
|
||
| /** | ||
| * Default log file end line number* | ||
| */ | ||
| private static final int DEFAULT_TO = 1; | ||
|
|
||
| /** | ||
| * @param ctx Context. | ||
| */ | ||
| public GridLogCommandHandler(GridKernalContext ctx) { | ||
| super(ctx); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Collection<GridRestCommand> supportedCommands() { | ||
| return SUPPORTED_COMMANDS; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) { | ||
| assert req != null; | ||
| if (req.command() == LOG) { | ||
| if (log.isDebugEnabled()) | ||
| log.debug("Handling log REST request: " + req); | ||
| GridRestLogRequest req0 = (GridRestLogRequest) req; | ||
| try { | ||
| validateRange(req0); | ||
| } catch (IgniteCheckedException e){ | ||
| return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage())); | ||
| } | ||
| int from = DEFAULT_FROM; | ||
| if (req0.from() != -1) { | ||
| from = req0.from(); | ||
| } | ||
| int to = DEFAULT_TO; | ||
| if (req0.to() != -1) { | ||
| to = req0.to(); | ||
| } | ||
| String path = DEFAULT_LOG_PATH; | ||
| if (from >= to) { | ||
|
Contributor
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. Check this condition before and only after that calculate the path - Path filePath = getPath(req0, path);
Member
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. updated the check |
||
| return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, | ||
| "Log file start from cannot be greater than to")); | ||
| } | ||
| Path filePath = getPath(req0, path); | ||
| try { | ||
| String content = getContent(from, to, filePath); | ||
| return new GridFinishedFuture<>(new GridRestResponse(content)); | ||
| } catch (IgniteCheckedException e) { | ||
| return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED, e.getMessage())); | ||
| } | ||
| } | ||
| return new GridFinishedFuture<>(); | ||
| } | ||
|
|
||
| private void validateRange(GridRestLogRequest req0) throws IgniteCheckedException { | ||
| if((req0.from() != -1 && req0.to() == -1)|| | ||
| (req0.from() == -1 && req0.to() != -1)){ | ||
| throw new IgniteCheckedException("Both from and to need to be provided"); | ||
| } | ||
| } | ||
|
|
||
| private Path getPath(GridRestLogRequest req0, String path) { | ||
| Path filePath; | ||
| if (req0.path() != null) { | ||
| path = req0.path(); | ||
| filePath = Paths.get(path); | ||
| } else { | ||
| filePath = Paths.get(ctx.config().getIgniteHome() + "/" + path); | ||
|
|
||
| } | ||
| return filePath; | ||
| } | ||
|
|
||
| private String getContent(int from, int to, Path filePath) throws IgniteCheckedException { | ||
| StringBuilder content = new StringBuilder(); | ||
| try (BufferedReader reader = Files.newBufferedReader(filePath, Charset.defaultCharset())) { | ||
| String line = null; | ||
| int start = from; | ||
| while ((line = reader.readLine()) != null | ||
| && from < to) { | ||
| if (start >= from) { | ||
| content.append(line); | ||
| start++; | ||
| } | ||
| from++; | ||
| } | ||
| } catch (IOException e) { | ||
| throw new IgniteCheckedException(e); | ||
| } | ||
| return content.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.ignite.internal.processors.rest.handlers.log; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| import org.apache.ignite.internal.IgniteInternalFuture; | ||
| import org.apache.ignite.internal.processors.rest.GridRestCommand; | ||
| import org.apache.ignite.internal.processors.rest.GridRestResponse; | ||
| import org.apache.ignite.internal.processors.rest.request.GridRestLogRequest; | ||
| import org.apache.ignite.internal.processors.rest.request.GridRestRequest; | ||
| import org.apache.ignite.testframework.junits.GridTestKernalContext; | ||
| import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; | ||
|
|
||
| public class GridLogCommandHandlerTest extends GridCommonAbstractTest { | ||
|
|
||
| public void testSupportedCommands() throws Exception { | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(super.newContext()); | ||
| Collection<GridRestCommand> commandCollection = gridLogCommandHandler.supportedCommands(); | ||
| assertTrue(commandCollection.contains(GridRestCommand.LOG)); | ||
| } | ||
|
|
||
| public void testUnSupportedCommands() throws Exception { | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(super.newContext()); | ||
| Collection<GridRestCommand> commandCollection = gridLogCommandHandler.supportedCommands(); | ||
| assertFalse(commandCollection.contains(GridRestCommand.VERSION)); | ||
| } | ||
|
|
||
| public void testHandleAsync() throws Exception { | ||
| GridTestKernalContext ctx = super.newContext(); | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(ctx); | ||
| GridRestLogRequest gridLogRestRequest = new GridRestLogRequest(); | ||
| gridLogRestRequest.to(5); | ||
|
Contributor
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. Please add additional test for the following cases:
|
||
| gridLogRestRequest.from(2); | ||
| gridLogRestRequest.path(getClass().getResource("/Test").getFile()); | ||
| GridRestRequest gridRestRequest = (GridRestLogRequest) gridLogRestRequest; | ||
| gridRestRequest.command(GridRestCommand.LOG); | ||
| IgniteInternalFuture<GridRestResponse> gridRestResponse = gridLogCommandHandler.handleAsync(gridRestRequest); | ||
| assertEquals(0, gridRestResponse.result().getSuccessStatus()); | ||
| assertNotNull(gridRestResponse.result().getResponse()); | ||
| assertEquals(null, gridRestResponse.result().getError()); | ||
| } | ||
|
|
||
| public void testHandleAsyncFromAndToNotSet() throws Exception { | ||
| GridTestKernalContext ctx = super.newContext(); | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(ctx); | ||
| GridRestLogRequest gridLogRestRequest = new GridRestLogRequest(); | ||
| gridLogRestRequest.path(getClass().getResource("/Test").getFile()); | ||
| GridRestRequest gridRestRequest = (GridRestLogRequest) gridLogRestRequest; | ||
| gridRestRequest.command(GridRestCommand.LOG); | ||
| IgniteInternalFuture<GridRestResponse> gridRestResponse = gridLogCommandHandler.handleAsync(gridRestRequest); | ||
| assertEquals(0, gridRestResponse.result().getSuccessStatus()); | ||
| assertNotNull(gridRestResponse.result().getResponse()); | ||
| assertEquals(null, gridRestResponse.result().getError()); | ||
| } | ||
|
|
||
| public void testHandleAsyncPathNotSet() throws Exception { | ||
| GridTestKernalContext ctx = super.newContext(); | ||
| ctx.config().setIgniteHome(getClass().getResource("/").getFile()); | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(ctx); | ||
| GridRestLogRequest gridLogRestRequest = new GridRestLogRequest(); | ||
| gridLogRestRequest.to(5); | ||
| gridLogRestRequest.from(2); | ||
| GridRestRequest gridRestRequest = (GridRestLogRequest) gridLogRestRequest; | ||
| gridRestRequest.command(GridRestCommand.LOG); | ||
| IgniteInternalFuture<GridRestResponse> gridRestResponse = gridLogCommandHandler.handleAsync(gridRestRequest); | ||
| assertEquals(0, gridRestResponse.result().getSuccessStatus()); | ||
| assertNotNull(gridRestResponse.result().getResponse()); | ||
| assertEquals(null, gridRestResponse.result().getError()); | ||
| } | ||
|
|
||
| public void testHandleAsyncFromGreaterThanTo() throws Exception { | ||
| GridTestKernalContext ctx = super.newContext(); | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(ctx); | ||
| GridRestLogRequest gridLogRestRequest = new GridRestLogRequest(); | ||
| gridLogRestRequest.to(2); | ||
| gridLogRestRequest.from(5); | ||
| gridLogRestRequest.path(getClass().getResource("/Test").getFile()); | ||
| GridRestRequest gridRestRequest = (GridRestLogRequest) gridLogRestRequest; | ||
| gridRestRequest.command(GridRestCommand.LOG); | ||
| IgniteInternalFuture<GridRestResponse> gridRestResponse = gridLogCommandHandler.handleAsync(gridRestRequest); | ||
| assertEquals(1, gridRestResponse.result().getSuccessStatus()); | ||
| assertNull(gridRestResponse.result().getResponse()); | ||
| assertEquals("Log file start from cannot be greater than to", gridRestResponse.result().getError()); | ||
| } | ||
|
|
||
| public void testHandleAsyncFromEqualTo() throws Exception { | ||
| GridTestKernalContext ctx = super.newContext(); | ||
| GridLogCommandHandler gridLogCommandHandler = new GridLogCommandHandler(ctx); | ||
| GridRestLogRequest gridLogRestRequest = new GridRestLogRequest(); | ||
| gridLogRestRequest.to(2); | ||
| gridLogRestRequest.from(2); | ||
| gridLogRestRequest.path(getClass().getResource("/Test").getFile()); | ||
| GridRestRequest gridRestRequest = (GridRestLogRequest) gridLogRestRequest; | ||
| gridRestRequest.command(GridRestCommand.LOG); | ||
| IgniteInternalFuture<GridRestResponse> gridRestResponse = gridLogCommandHandler.handleAsync(gridRestRequest); | ||
| assertEquals(1, gridRestResponse.result().getSuccessStatus()); | ||
| assertNull(gridRestResponse.result().getResponse()); | ||
| assertEquals("Log file start from cannot be greater than to", gridRestResponse.result().getError()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [22:01:30,329][INFO ][grid-load-test-thread-12][GridDeploymentLocalStore] Task locally undeployed: class org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask | ||
| [22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Removed undeployed class: GridDeployment [ts=1451061090319, depMode=SHARED, clsLdr=IsolatedClassLoader{roleName='test'}, clsLdrId=df3586ad151-725cfdfb-3344-419d-9719-df3995800000, userVer=0, loc=true, sampleClsName=org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask, pendingUndeploy=false, undeployed=true, usage=0] | ||
| [22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Task locally undeployed: class org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask | ||
| [22:01:30,330][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Removed undeployed class: GridDeployment [ts=1451061090329, depMode=SHARED, clsLdr=IsolatedClassLoader{roleName='test'}, clsLdrId=004586ad151-725cfdfb-3344-419d-9719-df3995800000, userVer=0, loc=true, sampleClsName=org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask, pendingUndeploy=false, undeployed=true, usage=0] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [22:01:30,329][INFO ][grid-load-test-thread-12][GridDeploymentLocalStore] Task locally undeployed: class org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask | ||
| [22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Removed undeployed class: GridDeployment [ts=1451061090319, depMode=SHARED, clsLdr=IsolatedClassLoader{roleName='test'}, clsLdrId=df3586ad151-725cfdfb-3344-419d-9719-df3995800000, userVer=0, loc=true, sampleClsName=org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask, pendingUndeploy=false, undeployed=true, usage=0] | ||
| [22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Task locally undeployed: class org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask | ||
| [22:01:30,330][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Removed undeployed class: GridDeployment [ts=1451061090329, depMode=SHARED, clsLdr=IsolatedClassLoader{roleName='test'}, clsLdrId=004586ad151-725cfdfb-3344-419d-9719-df3995800000, userVer=0, loc=true, sampleClsName=org.apache.ignite.internal.GridDeploymentMultiThreadedSelfTest$GridDeploymentTestTask, pendingUndeploy=false, undeployed=true, usage=0] |
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.
Saikat,
Lets follow the Rest API docs [1] and implement support of 'from' and 'to' parameters the following way:
[1] https://apacheignite.readme.io/docs/rest-api#section-request-parameters
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.
Denis,