Skip to content

Commit

Permalink
Return HTTP 409 (conflict) if get parallel put request (opensearch-pr…
Browse files Browse the repository at this point in the history
…oject#1158)

(cherry picked from commit ea382b4)
  • Loading branch information
andy840314 committed Jul 27, 2021
1 parent dbf2ff2 commit 9648a28
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
Expand Down Expand Up @@ -313,7 +314,11 @@ public OnSucessActionListener(RestChannel channel) {

@Override
public final void onFailure(Exception e) {
internalErrorResponse(channel, "Error "+e.getMessage());
if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) {
conflict(channel, e.getMessage());
} else {
internalErrorResponse(channel, "Error "+e.getMessage());
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@

public class UserApiTest extends AbstractRestApiUnitTest {

@Test
public void testParallelPutRequests() throws Exception {

setup();

rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

HttpResponse[] responses = rh.executeMultipleAsyncPutRequest(10, "/_opendistro/_security/api/internalusers/test1", "{\"password\":\"test1\"}");
boolean created = false;
for (HttpResponse response : responses) {
int sc = response.getStatusCode();
if (sc == HttpStatus.SC_CREATED) {
Assert.assertFalse(created);
created = true;
} else {
Assert.assertEquals(HttpStatus.SC_CONFLICT, sc);
}
}
deleteUser("test1");
}

@Test
public void testUserApi() throws Exception {
setup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.net.ssl.SSLContext;

Expand Down Expand Up @@ -121,6 +124,18 @@ public String executeSimpleRequest(final String request) throws Exception {
}
}

public HttpResponse[] executeMultipleAsyncPutRequest(final int numOfRequests, final String request, String body) throws Exception {
final ExecutorService executorService = Executors.newFixedThreadPool(numOfRequests);
Future<HttpResponse>[] futures = new Future[numOfRequests];
for (int i = 0; i < numOfRequests; i++) {
futures[i] = executorService.submit(() -> executePutRequest(request, body, new Header[0]));
}
executorService.shutdown();
return Arrays.stream(futures)
.map(HttpResponse::from)
.toArray(s -> new HttpResponse[s]);
}

public HttpResponse executeGetRequest(final String request, Header... header) throws Exception {
return executeRequest(new HttpGet(getHttpServerUri() + "/" + request), header);
}
Expand Down Expand Up @@ -259,7 +274,7 @@ protected final CloseableHttpClient getHTTPClient() throws Exception {
}


public class HttpResponse {
public static class HttpResponse {
private final CloseableHttpResponse inner;
private final String body;
private final Header[] header;
Expand Down Expand Up @@ -327,6 +342,13 @@ public String toString() {
+ ", statusReason=" + statusReason + "]";
}

private static HttpResponse from(Future<HttpResponse> future) {
try {
return future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}


Expand Down

0 comments on commit 9648a28

Please sign in to comment.