Skip to content

Commit

Permalink
Added the error support in Sensei
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhabiuk committed Apr 18, 2012
1 parent 2f30e96 commit c6ed00c
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 90 deletions.
Expand Up @@ -50,9 +50,7 @@ public static SenseiClientRequest.Builder basicWithSelections(SenseiClientReques
.addSelection(Selection.path("field", "value", true, 1))
.addSelection(Selection.range("color", "*", "*"))
.addFacet("facet1", Facet.builder().max(2).minHit(1).orderByVal().build())
.addFacetInit("name", "parameter", FacetInit.build(FacetType.type_double, "val1", "val2"))
.addSort(Sort.desc("color"))
.addSort(Sort.asc("year"))
.addFacetInit("name", "parameter", FacetInit.build(FacetType.type_double, "val1", "val2"))
.addTermVector("Term1")
.explain(true)
.partitions(Arrays.asList(1,2)) ;
Expand Down
Expand Up @@ -31,6 +31,8 @@
import com.senseidb.metrics.MetricsConstants;
import com.senseidb.search.req.AbstractSenseiRequest;
import com.senseidb.search.req.AbstractSenseiResult;
import com.senseidb.search.req.ErrorType;
import com.senseidb.search.req.SenseiError;
import com.senseidb.svc.api.SenseiException;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Meter;
Expand Down Expand Up @@ -173,6 +175,7 @@ protected RESULT doBrowse(PartitionedNetworkClient<String> networkClient, final
final long time = System.currentTimeMillis();

final List<RESULT> resultList = new ArrayList<RESULT>();

try {
resultList.addAll(ScatterTimer.time(new Callable<List<RESULT>>() {
@Override
Expand All @@ -182,14 +185,19 @@ public List<RESULT> call() throws Exception {
}));
} catch (Exception e) {
ErrorMeter.mark();
RESULT emptyResult = getEmptyResultInstance();
logger.error("Error running scatter/gather", e);
return getEmptyResultInstance();
emptyResult.addError(new SenseiError("Error gathering the results" + e.getMessage(), ErrorType.BrokerGatherError));
return emptyResult;
}

if (resultList.size() == 0)
{
logger.error("no result received at all return empty result");
RESULT emptyResult = getEmptyResultInstance();
emptyResult.addError(new SenseiError("Error gathering the results. " + "no result received at all return empty result", ErrorType.BrokerGatherError));
EmptyMeter.mark();
return emptyResult;
}

RESULT result = null;
Expand All @@ -203,6 +211,7 @@ public RESULT call() throws Exception {
} catch (Exception e) {
result = getEmptyResultInstance();
logger.error("Error gathering the results", e);
result.addError(new SenseiError("Error gathering the results" + e.getMessage(), ErrorType.BrokerGatherError));
ErrorMeter.mark();
}

Expand Down
Expand Up @@ -12,6 +12,8 @@
import com.linkedin.norbert.javacompat.network.ScatterGatherHandler;
import com.senseidb.search.req.AbstractSenseiRequest;
import com.senseidb.search.req.AbstractSenseiResult;
import com.senseidb.search.req.ErrorType;
import com.senseidb.search.req.SenseiError;

public abstract class AbstractSenseiScatterGatherHandler<REQUEST extends AbstractSenseiRequest, RESULT extends AbstractSenseiResult>
implements ScatterGatherHandler<REQUEST, RESULT, RESULT, Integer>
Expand Down Expand Up @@ -64,21 +66,22 @@ public long getTimeoutMillis()
@Override
public RESULT gatherResponses(ResponseIterator<RESULT> iter) throws Exception {
boolean debugmode = logger.isDebugEnabled();

int timeOuts = 0;;
List<RESULT> boboBrowseList = new ArrayList<RESULT>();
while (iter.hasNext())
{
RESULT result = iter.next(_timeoutMillis > 0 ? _timeoutMillis : Long.MAX_VALUE, TimeUnit.MILLISECONDS);

if (result == null)
{
timeOuts++;
logger.error("Request Timed Out");
} else
{
boboBrowseList.add(result);
}
}
RESULT res = mergeResults(_request, boboBrowseList);
res.addError(new SenseiError("Request timeout", ErrorType.BrokerTimeout));
if (debugmode)
{
logger.debug("merged results: " + res);
Expand Down
Expand Up @@ -39,6 +39,8 @@
import com.browseengine.bobo.sort.SortCollector;
import com.browseengine.bobo.sort.SortCollector.CollectorContext;
import com.browseengine.bobo.util.ListMerger;
import com.senseidb.search.req.ErrorType;
import com.senseidb.search.req.SenseiError;
import com.senseidb.search.req.SenseiHit;
import com.senseidb.search.req.SenseiRequest;
import com.senseidb.search.req.SenseiResult;
Expand Down Expand Up @@ -1148,7 +1150,7 @@ else if (rawGroupValueType[j] == 0)

time += (end-start);
merged.setTime(time);
merged.setParsedQuery(parsedQuery);
mergerErrors(merged, req, results, parsedQuery);
if (req.getMapReduceFunction() != null) {
if (onSearchNode) {
merged.setMapReduceResult(SenseiReduceFunctionWrapper.combine(req.getMapReduceFunction(), SenseiReduceFunctionWrapper.extractMapReduceResults(results)));
Expand All @@ -1159,4 +1161,16 @@ else if (rawGroupValueType[j] == 0)
}
return merged;
}
private static void mergerErrors(SenseiResult merged, final SenseiRequest req, Collection<SenseiResult> results, String parsedQuery) {
merged.setParsedQuery(parsedQuery);
merged.getErrors().addAll(req.getErrors());
for (SenseiResult res : results) {
merged.getErrors().addAll(res.getErrors());
if (res.getBoboErrors().size() > 0) {
for (String boboError : res.getBoboErrors()) {
merged.addError(new SenseiError(boboError, ErrorType.BoboExecutionError));
}
}
}
}
}
Expand Up @@ -18,6 +18,8 @@
import com.linkedin.norbert.javacompat.network.PartitionedNetworkClient;
import com.senseidb.conf.SenseiSchema;
import com.senseidb.indexing.DefaultJsonSchemaInterpreter;
import com.senseidb.search.req.ErrorType;
import com.senseidb.search.req.SenseiError;
import com.senseidb.search.req.SenseiHit;
import com.senseidb.search.req.SenseiRequest;
import com.senseidb.search.req.SenseiResult;
Expand Down Expand Up @@ -46,7 +48,7 @@ public SenseiBroker(PartitionedNetworkClient<String> networkClient, ClusterClien
logger.info("created broker instance " + networkClient + " " + clusterClient);
}

private void recoverSrcData(SenseiHit[] hits, boolean isFetchStoredFields)
private void recoverSrcData(SenseiResult res, SenseiHit[] hits, boolean isFetchStoredFields)
{
if (hits != null)
{
Expand Down Expand Up @@ -89,6 +91,7 @@ private void recoverSrcData(SenseiHit[] hits, boolean isFetchStoredFields)
}
catch(Exception ex)
{

data = dataBytes;
}
hit.setSrcData(new String(data, "UTF-8"));
Expand All @@ -97,9 +100,10 @@ private void recoverSrcData(SenseiHit[] hits, boolean isFetchStoredFields)
catch(Exception e)
{
logger.error(e.getMessage(),e);
res.getErrors().add(new SenseiError(e.getMessage(), ErrorType.BrokerGatherError));
}

recoverSrcData(hit.getSenseiGroupHits(), isFetchStoredFields);
recoverSrcData(res, hit.getSenseiGroupHits(), isFetchStoredFields);

// Remove stored fields since the user is not requesting:
if (!isFetchStoredFields)
Expand All @@ -113,9 +117,9 @@ public SenseiResult mergeResults(SenseiRequest request, List<SenseiResult> resul
{
request.restoreState();
SenseiResult res = ResultMerger.merge(request, resultList, false);

if (request.isFetchStoredFields() || request.isFetchStoredValue())
recoverSrcData(res.getSenseiHits(), request.isFetchStoredFields());
recoverSrcData(res, res.getSenseiHits(), request.isFetchStoredFields());

return res;
}
Expand Down
Expand Up @@ -13,6 +13,8 @@
import org.apache.lucene.document.Document;

import com.senseidb.conf.SenseiSchema;
import com.senseidb.search.req.ErrorType;
import com.senseidb.search.req.SenseiError;
import com.senseidb.search.req.SenseiHit;
import com.senseidb.search.req.SenseiRequest;
import com.senseidb.search.req.SenseiResult;
Expand Down Expand Up @@ -79,6 +81,7 @@ public SenseiResult mergeResults(SenseiRequest request, List<SenseiResult> resul
}
catch(Exception e)
{
res.addError(new SenseiError(e.getMessage(),ErrorType.BrokerGatherError));
logger.error(e.getMessage(),e);
}
}
Expand Down
Expand Up @@ -10,4 +10,5 @@ public interface AbstractSenseiRequest extends Serializable
public String getRouteParam();
public void saveState();
public void restoreState();
public void addError(SenseiError error);
}
Expand Up @@ -6,4 +6,5 @@ public interface AbstractSenseiResult extends Serializable
{
public abstract long getTime();
public abstract void setTime(long searchTimeMillis);
public void addError(SenseiError error);
}
@@ -1,5 +1,13 @@
package com.senseidb.search.req;

public enum ErrorType {
JsonParsingError, BQLParsingError, BoboExecutionError, ExecutionTimeout
JsonParsingError(100), BQLParsingError(150), BoboExecutionError(200), ExecutionTimeout(250), BrokerGatherError(300), PartitionCallError(350), BrokerTimeout(400), InternalError(450), MergePartitionError(500);
private final int defaultErrorCode;

private ErrorType(int defaultErrorCode) {
this.defaultErrorCode = defaultErrorCode;
}
public int getDefaultErrorCode() {
return defaultErrorCode;
}
}
21 changes: 19 additions & 2 deletions sensei-core/src/main/java/com/senseidb/search/req/SenseiError.java
@@ -1,14 +1,22 @@
package com.senseidb.search.req;

public class SenseiError {
import java.io.Serializable;

public class SenseiError implements Serializable {
private final String message;
private final ErrorType errorType;
private final int errorCode;

public SenseiError(String message, ErrorType errorType) {
this.message = message;
this.errorType = errorType;
this.errorCode = errorType.getDefaultErrorCode();
}
public SenseiError(String message, ErrorType errorType, int errorCode) {
this.message = message;
this.errorType = errorType;
this.errorCode = errorCode;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down Expand Up @@ -36,6 +44,15 @@ public boolean equals(Object obj) {
return false;
return true;
}
public String getMessage() {
return message;
}
public ErrorType getErrorType() {
return errorType;
}
public int getErrorCode() {
return errorCode;
}



Expand Down
@@ -1,6 +1,7 @@
package com.senseidb.search.req;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -21,6 +22,8 @@ public class SenseiResult extends BrowseResult implements AbstractSenseiResult
private static final long serialVersionUID = 1L;

private String _parsedQuery = null;

private List<SenseiError> errors = new ArrayList<SenseiError>();

public SenseiHit[] getSenseiHits()
{
Expand Down Expand Up @@ -124,6 +127,14 @@ private boolean facetAccessibleAreEqual(FacetAccessible a, FacetAccessible b) {
return true;
}

public List<SenseiError> getErrors() {
return errors ;
}

public void addError(SenseiError error) {
this.errors.add(error);
}



}
Expand Up @@ -116,6 +116,8 @@ public String toString()
private Set<SenseiFacetInfo> _facetInfos;
private String _schema; /* JSONObject is not protobuf serializerable, we use string here. */
private List<SenseiNodeInfo> _clusterInfo;

private List<SenseiError> errors = new ArrayList<SenseiError>();

public SenseiSystemInfo(){
_numDocs = 0;
Expand Down Expand Up @@ -183,7 +185,13 @@ public List<SenseiNodeInfo> getClusterInfo() {
public void setClusterInfo(List<SenseiNodeInfo> clusterInfo) {
_clusterInfo = clusterInfo;
}

public List<SenseiError> getErrors() {
return errors ;
}

public void addError(SenseiError error) {
this.errors.add(error);
}
@Override
public String toString(){
StringBuffer buf = new StringBuffer();
Expand Down

0 comments on commit c6ed00c

Please sign in to comment.