Skip to content

Commit

Permalink
Catching AccessDeniedException for Personalize calls (#196)
Browse files Browse the repository at this point in the history
* Catching AccessDeniedException for Personalize calls

Signed-off-by: Prashant Mishra <[email protected]>

* Using status code in place of status message

* Using generic error message in place of custom error message

---------

Signed-off-by: Prashant Mishra <[email protected]>
(cherry picked from commit e8f153d)
  • Loading branch information
mishprs authored and github-actions[bot] committed Aug 30, 2023
1 parent c693311 commit 6316599
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.opensearch.search.relevance.transformer.personalizeintelligentranking.reranker.impl;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.personalizeruntime.model.GetPersonalizedRankingRequest;
import com.amazonaws.services.personalizeruntime.model.GetPersonalizedRankingResult;
import com.amazonaws.services.personalizeruntime.model.PredictedItem;
Expand All @@ -20,6 +21,7 @@
import org.opensearch.search.relevance.transformer.personalizeintelligentranking.configuration.PersonalizeIntelligentRankerConfiguration;
import org.opensearch.search.relevance.transformer.personalizeintelligentranking.requestparameter.PersonalizeRequestParameters;
import org.opensearch.search.relevance.transformer.personalizeintelligentranking.reranker.PersonalizedRanker;
import org.opensearch.search.relevance.transformer.personalizeintelligentranking.utils.ValidationUtil;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -36,6 +38,7 @@ public class AmazonPersonalizedRankerImpl implements PersonalizedRanker {
private static final Logger logger = LogManager.getLogger(AmazonPersonalizedRankerImpl.class);
private final PersonalizeIntelligentRankerConfiguration rankerConfig;
private final PersonalizeClient personalizeClient;

public AmazonPersonalizedRankerImpl(PersonalizeIntelligentRankerConfiguration config,
PersonalizeClient client) {
this.rankerConfig = config;
Expand Down Expand Up @@ -96,7 +99,15 @@ public SearchHits rerank(SearchHits hits, PersonalizeRequestParameters requestPa

SearchHits personalizedHits = combineScores(hits, result);
return personalizedHits;
} catch (Exception ex) {
} catch (AmazonServiceException e) {
logger.error("Exception while calling personalize campaign: {}", e.getMessage());
int statusCode = e.getStatusCode();
if (ValidationUtil.is4xxError(statusCode)) {
throw new IllegalArgumentException(e);
}
throw e;
}
catch (Exception ex) {
logger.error("Failed to re rank with Personalize.", ex);
throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ private static boolean isValidCampaignOrRoleArn(String arn, String expectedServi
return false;
}
}

public static boolean is4xxError(int statusCode){
if (statusCode >= 400 && statusCode < 500) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.opensearch.search.relevance.transformer.personalizeintelligentranking.ranker.impl;

import com.amazonaws.AmazonServiceException;
import org.junit.Assert;
import org.mockito.Mockito;
import org.opensearch.OpenSearchParseException;
import org.opensearch.search.SearchHit;
Expand Down Expand Up @@ -303,4 +305,39 @@ public void testReRankWithWeightAsNeitherZeroOrOneWithNullItemIdField() throws I
assertNotEquals(rerankedDocumentIdsWhenWeightIsOne, rerankedDocumentIds);
assertNotEquals(rerankedDocumentIdsWhenWeightIsZero, rerankedDocumentIds);
}

public void testReRankWithaccessDeniedExceptionWithStatusCode400() throws IOException {

PersonalizeIntelligentRankerConfiguration rankerConfig =
new PersonalizeIntelligentRankerConfiguration(personalizeCampaign, iamRoleArn, recipe, itemIdField, region, weight);
PersonalizeClient client = Mockito.mock(PersonalizeClient.class);
Mockito.when(client.getPersonalizedRanking(any())).thenThrow(buildErrorWithStatusCode(400));

PersonalizeRequestParameters requestParameters = new PersonalizeRequestParameters();
requestParameters.setUserId("28");
SearchHits responseHits = SearchTestUtil.getSampleSearchHitsForPersonalize(numOfHits);
AmazonPersonalizedRankerImpl ranker = new AmazonPersonalizedRankerImpl(rankerConfig, client);
Assert.assertThrows(IllegalArgumentException.class, () -> ranker.rerank(responseHits, requestParameters));
}

public void testReRankWithaccessDeniedExceptionWithStatusCode500() throws IOException {

PersonalizeIntelligentRankerConfiguration rankerConfig =
new PersonalizeIntelligentRankerConfiguration(personalizeCampaign, iamRoleArn, recipe, itemIdField, region, weight);
PersonalizeClient client = Mockito.mock(PersonalizeClient.class);
Mockito.when(client.getPersonalizedRanking(any())).thenThrow(buildErrorWithStatusCode(500));

PersonalizeRequestParameters requestParameters = new PersonalizeRequestParameters();
requestParameters.setUserId("28");
SearchHits responseHits = SearchTestUtil.getSampleSearchHitsForPersonalize(numOfHits);
AmazonPersonalizedRankerImpl ranker = new AmazonPersonalizedRankerImpl(rankerConfig, client);
Assert.assertThrows(AmazonServiceException.class, () -> ranker.rerank(responseHits, requestParameters));
}


private AmazonServiceException buildErrorWithStatusCode(int statusCode) {
AmazonServiceException amazonServiceException = new AmazonServiceException("Error");
amazonServiceException.setStatusCode(statusCode);
return amazonServiceException;
}
}

0 comments on commit 6316599

Please sign in to comment.