Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@
<exclude>**/*.java</exclude>
</excludes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>

<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler;
import org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheCommandHandler;
import org.apache.ignite.internal.processors.rest.handlers.datastructures.DataStructuresCommandHandler;
import org.apache.ignite.internal.processors.rest.handlers.log.GridLogCommandHandler;
import org.apache.ignite.internal.processors.rest.handlers.query.QueryCommandHandler;
import org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler;
import org.apache.ignite.internal.processors.rest.handlers.top.GridTopologyCommandHandler;
Expand Down Expand Up @@ -442,6 +443,8 @@ public GridRestProcessor(GridKernalContext ctx) {
addHandler(new GridVersionCommandHandler(ctx));
addHandler(new DataStructuresCommandHandler(ctx));
addHandler(new QueryCommandHandler(ctx));
addHandler(new GridLogCommandHandler(ctx));


// Start protocols.
startTcpProtocol();
Expand Down
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;
Copy link
Contributor

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:

  • if 'from' is set but 'to' is not then return an error;
  • if 'to' is set but 'from' is not then return an error as well;
  • if neither 'from' nor 'to' is set then return the first log line.
  • if both parameters are set then just check that 'from' is less than 'to' and use them as is

[1] https://apacheignite.readme.io/docs/rest-api#section-request-parameters

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Denis,

  • The validateRange method checks for the first 2 point.
  • The DEFAULT_FROM=0 and DEFAULT_TO=1 return first log line in absence of 'from' and 'to'
  • if both 'from' and 'to' is set then we are checking that 'from' is less than 'to'.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add additional test for the following cases:

  • 'to' and 'from' are not set;
  • 'path' is not set;
  • 'from' is bigger or equal to 'to'

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
Expand Up @@ -19,6 +19,7 @@

import junit.framework.TestSuite;
import org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheCommandHandlerSelfTest;
import org.apache.ignite.internal.processors.rest.handlers.log.GridLogCommandHandlerTest;

/**
* REST support tests.
Expand All @@ -32,7 +33,8 @@ public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite("REST Support Test Suite");

suite.addTestSuite(GridCacheCommandHandlerSelfTest.class);
suite.addTestSuite(GridLogCommandHandlerTest.class);

return suite;
}
}
}
4 changes: 4 additions & 0 deletions modules/core/src/test/resources/Test
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]
4 changes: 4 additions & 0 deletions modules/core/src/test/resources/work/log/ignite.log
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]