Skip to content

Commit

Permalink
IGNITE-18980 Fixed REST status code in case of failed request (apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
NSAmelchev authored Mar 10, 2023
1 parent a8e1689 commit eaabf74
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,17 @@ protected JsonNode validateJsonResponse(String content, boolean errorExpected, b
else
assertTrue("Unexpected error: " + errNode.asText(), errNode.isNull());

assertEquals(STATUS_SUCCESS, node.get("successStatus").asInt());
if (errorExpected)
assertEquals(STATUS_FAILED, node.get("successStatus").asInt());
else
assertEquals(STATUS_SUCCESS, node.get("successStatus").asInt());

if (!canBeUnauthenticated)
assertNotSame(securityEnabled(), node.get("sessionToken").isNull());
if (!canBeUnauthenticated) {
if (errorExpected)
assertTrue(node.get("sessionToken").isNull());
else
assertNotSame(securityEnabled(), node.get("sessionToken").isNull());
}

return node.get(errorExpected ? "error" : "response");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.ignite.internal.processors.rest;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand Down Expand Up @@ -322,11 +320,19 @@ private IgniteInternalFuture<GridRestResponse> handleRequest0(GridRestRequest re

GridRestCommandHandler hnd = handlers.get(req.command());

IgniteInternalFuture<GridRestResponse> res = hnd == null ? null : hnd.handleAsync(req);

if (res == null)
if (hnd == null) {
return new GridFinishedFuture<>(
new IgniteCheckedException("Failed to find registered handler for command: " + req.command()));
}

IgniteInternalFuture<GridRestResponse> res;

try {
res = hnd.handleAsync(req);
}
catch (Exception e) {
res = new GridFinishedFuture<>(e);
}

return res.chain(new C1<IgniteInternalFuture<GridRestResponse>, GridRestResponse>() {
@Override public GridRestResponse apply(IgniteInternalFuture<GridRestResponse> f) {
Expand All @@ -340,7 +346,9 @@ private IgniteInternalFuture<GridRestResponse> handleRequest0(GridRestRequest re
catch (Exception e) {
failed = true;

if (X.hasCause(e, IllegalArgumentException.class)) {
if (X.hasCause(e, SecurityException.class))
res = new GridRestResponse(STATUS_SECURITY_CHECK_FAILED, e.getMessage());
else if (X.hasCause(e, IllegalArgumentException.class)) {
IllegalArgumentException iae = X.cause(e, IllegalArgumentException.class);

res = new GridRestResponse(STATUS_ILLEGAL_ARGUMENT, iae.getMessage());
Expand All @@ -367,7 +375,7 @@ private IgniteInternalFuture<GridRestResponse> handleRequest0(GridRestRequest re
sb.a(", err=")
.a(e.getMessage() != null ? e.getMessage() : e.getClass().getName())
.a(", trace=")
.a(getErrorMessage(e))
.a(X.getFullStackTrace(e))
.a(']');

res = new GridRestResponse(STATUS_FAILED, sb.toString());
Expand All @@ -386,21 +394,6 @@ private IgniteInternalFuture<GridRestResponse> handleRequest0(GridRestRequest re
});
}

/**
* @param th Th.
* @return Stack trace
*/
private String getErrorMessage(Throwable th) {
if (th == null)
return "";

StringWriter writer = new StringWriter();

th.printStackTrace(new PrintWriter(writer));

return writer.toString();
}

/**
* @param req Request.
* @return Not null session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.rest.handlers;

import java.util.Collection;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.rest.GridRestCommand;
import org.apache.ignite.internal.processors.rest.GridRestResponse;
Expand All @@ -36,5 +37,5 @@ public interface GridRestCommandHandler {
* @param req Request.
* @return Future.
*/
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req);
public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) throws IgniteCheckedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.util.typedef.X;
import org.apache.ignite.internal.util.typedef.internal.SB;

/**
* Abstract command handler.
Expand Down Expand Up @@ -50,21 +48,4 @@ protected GridRestCommandHandlerAdapter(GridKernalContext ctx) {
protected static String missingParameter(String param) {
return "Failed to find mandatory parameter in request: " + param;
}

/**
* Converts exception to string representation for error in response.
*
* @param e Exception.
* @return String representation of exception for error in response.
*/
protected static String errorMessage(Exception e) {
SB sb = new SB();

sb.a(e.getMessage()).a("\n").a("suppressed: \n");

for (Throwable t : X.getSuppressedList(e))
sb.a(t.getMessage()).a("\n");

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public GridBaselineCommandHandler(GridKernalContext ctx) {
}

/** {@inheritDoc} */
@Override public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req) {
@Override public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest req)
throws IgniteCheckedException {
assert req != null;

assert SUPPORTED_COMMANDS.contains(req.command());
Expand All @@ -75,74 +76,65 @@ public GridBaselineCommandHandler(GridKernalContext ctx) {
log.debug("Handling baseline REST request: " + req);

GridRestBaselineRequest req0 = (GridRestBaselineRequest)req;

try {
IgniteClusterEx cluster = ctx.grid().cluster();

List<Object> consistentIds = req0.consistentIds();
IgniteClusterEx cluster = ctx.grid().cluster();

switch (req0.command()) {
case BASELINE_CURRENT_STATE: {
// No-op.
List<Object> consistentIds = req0.consistentIds();

break;
}
switch (req0.command()) {
case BASELINE_CURRENT_STATE: {
// No-op.

case BASELINE_SET: {
Long topVer = req0.topologyVersion();
break;
}

if (topVer == null && consistentIds == null)
throw new IgniteCheckedException("Failed to handle request (either topVer or consistentIds should be specified).");
case BASELINE_SET: {
Long topVer = req0.topologyVersion();

if (topVer != null)
cluster.setBaselineTopology(topVer);
else
cluster.setBaselineTopology(filterServerNodesByConsId(consistentIds));
if (topVer == null && consistentIds == null)
throw new IgniteCheckedException("Failed to handle request (either topVer or consistentIds should be specified).");

break;
}
if (topVer != null)
cluster.setBaselineTopology(topVer);
else
cluster.setBaselineTopology(filterServerNodesByConsId(consistentIds));

case BASELINE_ADD: {
if (consistentIds == null)
throw new IgniteCheckedException(missingParameter("consistentIds"));
break;
}

Set<BaselineNode> baselineTop = new HashSet<>(currentBaseLine());
case BASELINE_ADD: {
if (consistentIds == null)
throw new IgniteCheckedException(missingParameter("consistentIds"));

baselineTop.addAll(filterServerNodesByConsId(consistentIds));
Set<BaselineNode> baselineTop = new HashSet<>(currentBaseLine());

cluster.setBaselineTopology(baselineTop);
baselineTop.addAll(filterServerNodesByConsId(consistentIds));

break;
}
cluster.setBaselineTopology(baselineTop);

case BASELINE_REMOVE: {
if (consistentIds == null)
throw new IgniteCheckedException(missingParameter("consistentIds"));
break;
}

Collection<BaselineNode> baseline = currentBaseLine();
case BASELINE_REMOVE: {
if (consistentIds == null)
throw new IgniteCheckedException(missingParameter("consistentIds"));

Set<BaselineNode> baselineTop = new HashSet<>(baseline);
Collection<BaselineNode> baseline = currentBaseLine();

baselineTop.removeAll(filterNodesByConsId(baseline, consistentIds));
Set<BaselineNode> baselineTop = new HashSet<>(baseline);

cluster.setBaselineTopology(baselineTop);
baselineTop.removeAll(filterNodesByConsId(baseline, consistentIds));

break;
}
cluster.setBaselineTopology(baselineTop);

default:
assert false : "Invalid command for baseline handler: " + req;
break;
}

return new GridFinishedFuture<>(new GridRestResponse(currentState()));
}
catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
finally {
if (log.isDebugEnabled())
log.debug("Handled baseline REST request: " + req);
default:
assert false : "Invalid command for baseline handler: " + req;
}

return new GridFinishedFuture<>(new GridRestResponse(currentState()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
package org.apache.ignite.internal.processors.rest.handlers.cluster;

import java.util.Collection;
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.GridRestClusterStateRequest;
import org.apache.ignite.internal.processors.rest.request.GridRestRequest;
import org.apache.ignite.internal.util.future.GridFutureAdapter;
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.CLUSTER_SET_STATE;
Expand All @@ -51,43 +52,26 @@ public GridChangeClusterStateCommandHandler(GridKernalContext ctx) {
}

/** {@inheritDoc} */
@Override public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest restReq) {
@Override public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest restReq)
throws IgniteCheckedException {
GridRestClusterStateRequest req = (GridRestClusterStateRequest)restReq;

final GridFutureAdapter<GridRestResponse> fut = new GridFutureAdapter<>();
switch (req.command()) {
case CLUSTER_STATE:
assert req.isReqCurrentMode() : req;

final GridRestResponse res = new GridRestResponse();
return new GridFinishedFuture<>(new GridRestResponse(ctx.grid().cluster().state()));

try {
switch (req.command()) {
case CLUSTER_STATE:
assert req.isReqCurrentMode() : req;
default:
assert req.state() != null : req;

res.setResponse(ctx.grid().cluster().state());
U.log(log, "Received cluster state change request to " + req.state() +
" state from client node with ID: " + req.clientId());

break;
ctx.state().changeGlobalState(req.state(), req.forceDeactivation(),
ctx.cluster().get().forServers().nodes(), false).get();

default:
assert req.state() != null : req;

U.log(log, "Received cluster state change request to " + req.state() +
" state from client node with ID: " + req.clientId());

ctx.state().changeGlobalState(req.state(), req.forceDeactivation(),
ctx.cluster().get().forServers().nodes(), false).get();

res.setResponse(req.command().key() + " done");

break;
}

fut.onDone(res);
}
catch (Exception e) {
res.setError(errorMessage(e));

fut.onDone(res);
return new GridFinishedFuture<>(new GridRestResponse(req.command().key() + " done"));
}
return fut;
}
}
Loading

0 comments on commit eaabf74

Please sign in to comment.