diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionRequest.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionRequest.java index af3effb0a..906531a23 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionRequest.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionRequest.java @@ -54,7 +54,7 @@ public String getOpcode() { } public List getFilters() { - if(filters == null) { + if (filters == null) { return Lists.newArrayList(); } return filters; @@ -82,7 +82,7 @@ public String toString() { } public Object clone() throws CloneNotSupportedException { - ActionRequest actionRequestClone = (ActionRequest)super.clone(); + ActionRequest actionRequestClone = (ActionRequest) super.clone(); actionRequestClone.setFilters(new ArrayList<>(this.filters)); return actionRequestClone; } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionResponse.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionResponse.java index 04a1ba767..edcb0ace9 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionResponse.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/ActionResponse.java @@ -36,12 +36,12 @@ */ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "opcode") @JsonSubTypes({@JsonSubTypes.Type(value = CountResponse.class, name = Opcodes.COUNT), @JsonSubTypes.Type(value = DistinctResponse.class, - name = Opcodes.DISTINCT), - @JsonSubTypes.Type(value = GroupResponse.class, name = Opcodes.GROUP), @JsonSubTypes.Type(value = HistogramResponse.class, - name = Opcodes.HISTOGRAM), - @JsonSubTypes.Type(value = QueryResponse.class, name = Opcodes.QUERY), @JsonSubTypes.Type(value = MultiQueryResponse + name = Opcodes.DISTINCT), + @JsonSubTypes.Type(value = GroupResponse.class, name = Opcodes.GROUP), @JsonSubTypes.Type(value = HistogramResponse.class, + name = Opcodes.HISTOGRAM), + @JsonSubTypes.Type(value = QueryResponse.class, name = Opcodes.QUERY), @JsonSubTypes.Type(value = MultiQueryResponse .class, name = Opcodes.MULTI_QUERY), @JsonSubTypes.Type(value = StatsResponse.class, name = Opcodes.STATS), @JsonSubTypes.Type - (value = TrendResponse.class, name = Opcodes.TREND), @JsonSubTypes.Type(value = StatsTrendResponse.class, name = + (value = TrendResponse.class, name = Opcodes.TREND), @JsonSubTypes.Type(value = StatsTrendResponse.class, name = Opcodes.STATS_TREND), @JsonSubTypes.Type(value = MultiTimeQueryResponse.class, name = Opcodes.MULTI_TIME_QUERY)}) @Data public abstract class ActionResponse { diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/Opcodes.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/Opcodes.java index e01959e54..e9eec30c8 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/Opcodes.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/Opcodes.java @@ -15,6 +15,7 @@ public final class Opcodes { public static final String STATS = "stats"; public static final String TREND = "trend"; public static final String STATS_TREND = "statstrend"; + private Opcodes() { } } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/distinct/DistinctResponse.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/distinct/DistinctResponse.java index d7803be70..a49250677 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/distinct/DistinctResponse.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/distinct/DistinctResponse.java @@ -63,14 +63,14 @@ public void accept(ResponseVisitor visitor) { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(o == null || getClass() != o.getClass()) + if (o == null || getClass() != o.getClass()) return false; - DistinctResponse that = (DistinctResponse)o; + DistinctResponse that = (DistinctResponse) o; - if(headers != null ? !headers.equals(that.headers) : that.headers != null) + if (headers != null ? !headers.equals(that.headers) : that.headers != null) return false; return result != null ? result.equals(that.result) : that.result == null; } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/estimation/EstimationDataType.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/estimation/EstimationDataType.java index 9ae91f7b7..b72a3a7fe 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/estimation/EstimationDataType.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/estimation/EstimationDataType.java @@ -8,7 +8,8 @@ public enum EstimationDataType { * Fixed estimation data, for: * {@link com.flipkart.foxtrot.common.FieldType#BOOLEAN} */ - FIXED, /** + FIXED, + /** * Percentile based estimation for: * {@link com.flipkart.foxtrot.common.FieldType#LONG} * {@link com.flipkart.foxtrot.common.FieldType#DATE} @@ -16,7 +17,8 @@ public enum EstimationDataType { * {@link com.flipkart.foxtrot.common.FieldType#FLOAT} * {@link com.flipkart.foxtrot.common.FieldType#INTEGER} */ - PERCENTILE, /** + PERCENTILE, + /** * Fixed estimation data, for: * {@link com.flipkart.foxtrot.common.FieldType#STRING} */ diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/histogram/HistogramResponse.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/histogram/HistogramResponse.java index 5d7ebf0ef..e6969f520 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/histogram/HistogramResponse.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/histogram/HistogramResponse.java @@ -78,14 +78,14 @@ public void setCount(long count) { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(!(o instanceof Count)) + if (!(o instanceof Count)) return false; - Count count1 = (Count)o; + Count count1 = (Count) o; - if(count != count1.count) + if (count != count1.count) return false; return period.equals(count1.period); } @@ -93,7 +93,7 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = period.hashCode(); - result = 31 * result + (int)(count ^ (count >>> 32)); + result = 31 * result + (int) (count ^ (count >>> 32)); return result; } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/Filter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/Filter.java index 5ba698a30..e1685f1f7 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/Filter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/Filter.java @@ -39,20 +39,20 @@ */ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "operator") @JsonSubTypes({ - //Numeric - @JsonSubTypes.Type(value = GreaterEqualFilter.class, name = FilterOperator.greater_equal), @JsonSubTypes.Type(value = GreaterThanFilter.class, name = FilterOperator.greater_than), @JsonSubTypes.Type(value = LessEqualFilter.class, name = FilterOperator.less_equal), @JsonSubTypes.Type(value = LessThanFilter.class, name = FilterOperator.less_than), @JsonSubTypes.Type(value = BetweenFilter.class, name = FilterOperator.between), + //Numeric + @JsonSubTypes.Type(value = GreaterEqualFilter.class, name = FilterOperator.greater_equal), @JsonSubTypes.Type(value = GreaterThanFilter.class, name = FilterOperator.greater_than), @JsonSubTypes.Type(value = LessEqualFilter.class, name = FilterOperator.less_equal), @JsonSubTypes.Type(value = LessThanFilter.class, name = FilterOperator.less_than), @JsonSubTypes.Type(value = BetweenFilter.class, name = FilterOperator.between), - //General - @JsonSubTypes.Type(value = EqualsFilter.class, name = FilterOperator.equals), @JsonSubTypes.Type(value = InFilter + //General + @JsonSubTypes.Type(value = EqualsFilter.class, name = FilterOperator.equals), @JsonSubTypes.Type(value = InFilter .class, name = FilterOperator.in), @JsonSubTypes.Type(value = NotInFilter.class, name = FilterOperator.not_in), @JsonSubTypes .Type(value = NotEqualsFilter.class, name = FilterOperator.not_equals), @JsonSubTypes.Type(value = AnyFilter.class, name = FilterOperator.any), @JsonSubTypes.Type(value = ExistsFilter.class, name = FilterOperator.exists), @JsonSubTypes.Type(value = MissingFilter.class, name = FilterOperator.missing), - //String - @JsonSubTypes.Type(value = ContainsFilter.class, name = FilterOperator.contains), @JsonSubTypes.Type(value = WildCardFilter.class, name = FilterOperator.wildcard), + //String + @JsonSubTypes.Type(value = ContainsFilter.class, name = FilterOperator.contains), @JsonSubTypes.Type(value = WildCardFilter.class, name = FilterOperator.wildcard), - //String - @JsonSubTypes.Type(value = LastFilter.class, name = FilterOperator.last)}) + //String + @JsonSubTypes.Type(value = LastFilter.class, name = FilterOperator.last)}) public abstract class Filter implements Serializable { @@ -87,14 +87,14 @@ public String getOperator() { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(o == null || getClass() != o.getClass()) + if (o == null || getClass() != o.getClass()) return false; - Filter filter = (Filter)o; + Filter filter = (Filter) o; - if(!field.equals(filter.field)) + if (!field.equals(filter.field)) return false; return operator.equals(filter.operator); } @@ -121,7 +121,7 @@ public boolean isFilterTemporal() { public Set validate() { Set validationErrors = new HashSet<>(); - if(CollectionUtils.isNullOrEmpty(field)) { + if (CollectionUtils.isNullOrEmpty(field)) { validationErrors.add("filter field cannot be null or empty"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/MultiQueryRequest.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/MultiQueryRequest.java index cdce2e9d2..7c093266a 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/MultiQueryRequest.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/MultiQueryRequest.java @@ -18,7 +18,9 @@ import com.flipkart.foxtrot.common.ActionRequest; import com.flipkart.foxtrot.common.ActionRequestVisitor; import com.flipkart.foxtrot.common.Opcodes; + import java.util.Map; + import lombok.Data; /*** diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/ResultSort.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/ResultSort.java index 5877ed183..455833c8a 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/ResultSort.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/ResultSort.java @@ -58,14 +58,14 @@ public void setOrder(Order order) { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(o == null || getClass() != o.getClass()) + if (o == null || getClass() != o.getClass()) return false; - ResultSort that = (ResultSort)o; + ResultSort that = (ResultSort) o; - if(!field.equals(that.field)) + if (!field.equals(that.field)) return false; return order == that.order; } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/LastFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/LastFilter.java index 4569b505a..b266993c0 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/LastFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/LastFilter.java @@ -69,10 +69,9 @@ public int hashCode() { result = 31 * result + getField().hashCode(); if (!getField().equals("_timestamp")) { result = result * 21 + (getCurrentTime() == 0 - ? 43 - : Long.valueOf(getCurrentTime()).hashCode()); - } - else { + ? 43 + : Long.valueOf(getCurrentTime()).hashCode()); + } else { result = result * 21 + Long.valueOf(getCurrentTime() / (long) 30000).hashCode(); } result = result * 13 + getRoundingMode().name().hashCode(); @@ -84,8 +83,7 @@ public int hashCode() { public boolean equals(Object o) { if (this == o) { return true; - } - else if (!(o instanceof LastFilter)) { + } else if (!(o instanceof LastFilter)) { return false; } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/WindowUtil.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/WindowUtil.java index 2edc3ec4c..0626f5ecb 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/WindowUtil.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/datetime/WindowUtil.java @@ -11,10 +11,10 @@ private WindowUtil() { public static TimeWindow calculate(long endTime, Duration duration, RoundingMode roundingMode) { DateTime windowStartTime = new DateTime(endTime - duration.toMilliseconds()); - if(roundingMode == null || roundingMode == RoundingMode.NONE) { + if (roundingMode == null || roundingMode == RoundingMode.NONE) { return new TimeWindow(windowStartTime.getMillis(), endTime); } return new TimeWindow(roundingMode.translate(windowStartTime, duration) - .getMillis(), endTime); + .getMillis(), endTime); } } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/EqualsFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/EqualsFilter.java index 1403a0bb8..0e72c9858 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/EqualsFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/EqualsFilter.java @@ -57,7 +57,7 @@ public T accept(FilterVisitor visitor) { @Override public Set validate() { Set validationErrors = super.validate(); - if(value == null) { + if (value == null) { validationErrors.add("filter field value cannot be null or empty"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/InFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/InFilter.java index 82b7b3054..19c0be59a 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/InFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/InFilter.java @@ -50,7 +50,7 @@ public T accept(FilterVisitor visitor) { @Override public Set validate() { Set validationErrors = super.validate(); - if(CollectionUtils.isNullOrEmpty(values)) { + if (CollectionUtils.isNullOrEmpty(values)) { validationErrors.add("at least one value needs to be provided for field"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotEqualsFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotEqualsFilter.java index 17f77ae5e..97dc2fae2 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotEqualsFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotEqualsFilter.java @@ -57,7 +57,7 @@ public T accept(FilterVisitor visitor) { @Override public Set validate() { Set validationErrors = super.validate(); - if(value == null) { + if (value == null) { validationErrors.add("filter field value cannot be null or empty"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotInFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotInFilter.java index 951d4a58f..8b33eb5a5 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotInFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/general/NotInFilter.java @@ -50,7 +50,7 @@ public T accept(FilterVisitor visitor) { @Override public Set validate() { Set validationErrors = super.validate(); - if(CollectionUtils.isNullOrEmpty(values)) { + if (CollectionUtils.isNullOrEmpty(values)) { validationErrors.add("at least one value needs to be provided for field"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/BetweenFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/BetweenFilter.java index 085c8dcb0..e7c7df5ce 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/BetweenFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/BetweenFilter.java @@ -70,11 +70,11 @@ public boolean isFilterTemporal() { @Override public Set validate() { Set validationErrors = super.validate(); - if(from == null) { + if (from == null) { validationErrors.add("from field cannot be null"); } - if(to == null) { + if (to == null) { validationErrors.add("to field cannot be null"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/NumericBinaryFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/NumericBinaryFilter.java index 3251d7911..e692e958a 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/NumericBinaryFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/numeric/NumericBinaryFilter.java @@ -56,7 +56,7 @@ public boolean isFilterTemporal() { @Override public Set validate() { Set validationErrors = super.validate(); - if(value == null) { + if (value == null) { validationErrors.add("filter value cannot be null"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/ContainsFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/ContainsFilter.java index b6190c7fc..e99994580 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/ContainsFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/ContainsFilter.java @@ -55,7 +55,7 @@ public T accept(FilterVisitor visitor) { @Override public Set validate() { Set validationErrors = super.validate(); - if(value == null) { + if (value == null) { validationErrors.add("filter value cannot be null"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/WildCardFilter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/WildCardFilter.java index 667c47df8..eca723b43 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/WildCardFilter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/query/string/WildCardFilter.java @@ -37,7 +37,7 @@ public T accept(FilterVisitor visitor) { @Override public Set validate() { Set validationErrors = super.validate(); - if(value == null) { + if (value == null) { validationErrors.add("filter value cannot be null"); } return validationErrors; diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/AnalyticsRequestFlags.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/AnalyticsRequestFlags.java index 2ba1583a7..6d0c599ad 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/AnalyticsRequestFlags.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/AnalyticsRequestFlags.java @@ -6,8 +6,7 @@ * */ public enum AnalyticsRequestFlags { - STATS_SKIP_PERCENTILES - ; + STATS_SKIP_PERCENTILES; public static boolean hasFlag(Collection flags, AnalyticsRequestFlags requestFlag) { return null != flags && flags.contains(requestFlag); diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValue.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValue.java index bb20476c1..a66dc4ad9 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValue.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValue.java @@ -1,7 +1,9 @@ package com.flipkart.foxtrot.common.stats; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + import java.util.Map; + import lombok.NoArgsConstructor; /** diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValueDeserializer.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValueDeserializer.java index 9be9c3129..851c7035d 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValueDeserializer.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/stats/StatsValueDeserializer.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.KeyDeserializer; + import java.io.IOException; /*** diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/trend/TrendResponse.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/trend/TrendResponse.java index c638bfb8b..681fa8c03 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/trend/TrendResponse.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/trend/TrendResponse.java @@ -55,12 +55,12 @@ public void accept(ResponseVisitor visitor) { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(!(o instanceof TrendResponse)) + if (!(o instanceof TrendResponse)) return false; - TrendResponse that = (TrendResponse)o; + TrendResponse that = (TrendResponse) o; return trends.equals(that.trends); } @@ -100,14 +100,14 @@ public void setCount(long count) { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(!(o instanceof Count)) + if (!(o instanceof Count)) return false; - Count count1 = (Count)o; + Count count1 = (Count) o; - if(count != count1.count) + if (count != count1.count) return false; return period.equals(count1.period); } @@ -115,7 +115,7 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = period.hashCode(); - result = 31 * result + (int)(count ^ (count >>> 32)); + result = 31 * result + (int) (count ^ (count >>> 32)); return result; } } diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/CollectionUtils.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/CollectionUtils.java index b6aa04875..7a5075158 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/CollectionUtils.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/CollectionUtils.java @@ -7,7 +7,8 @@ */ public class CollectionUtils { - private CollectionUtils() {} + private CollectionUtils() { + } public static boolean isNullOrEmpty(String s) { return s == null || s.trim() diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/Utils.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/Utils.java index 919c5de44..6c0a17773 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/Utils.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/util/Utils.java @@ -23,7 +23,8 @@ ***/ public class Utils { - private Utils() {} + private Utils() { + } public static Date getDate(long timestamp) { DateTime dateTime = new DateTime(timestamp); diff --git a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/visitor/CountPrecisionThresholdVisitorAdapter.java b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/visitor/CountPrecisionThresholdVisitorAdapter.java index 0e9da9fbd..f5cb62c4c 100644 --- a/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/visitor/CountPrecisionThresholdVisitorAdapter.java +++ b/foxtrot-common/src/main/java/com/flipkart/foxtrot/common/visitor/CountPrecisionThresholdVisitorAdapter.java @@ -16,31 +16,31 @@ public CountPrecisionThresholdVisitorAdapter(Integer defaultValue) { @Override public Integer visit(CountRequest request) { return Objects.isNull(request.getPrecision()) - ? defaultValue - : request.getPrecision().getPrecisionThreshold(); + ? defaultValue + : request.getPrecision().getPrecisionThreshold(); } @Override public Integer visit(GroupRequest request) { return Objects.isNull(request.getPrecision()) - ? defaultValue - : request.getPrecision().getPrecisionThreshold(); + ? defaultValue + : request.getPrecision().getPrecisionThreshold(); } @Override public Integer visit(HistogramRequest request) { return Objects.isNull(request.getPrecision()) - ? defaultValue - : request.getPrecision().getPrecisionThreshold(); + ? defaultValue + : request.getPrecision().getPrecisionThreshold(); } @Override public Integer visit(TrendRequest request) { return Objects.isNull(request.getPrecision()) - ? defaultValue - : request.getPrecision().getPrecisionThreshold(); + ? defaultValue + : request.getPrecision().getPrecisionThreshold(); } } diff --git a/foxtrot-common/src/test/java/com/flipkart/foxtrot/common/query/datetime/LastFilterTest.java b/foxtrot-common/src/test/java/com/flipkart/foxtrot/common/query/datetime/LastFilterTest.java index f05e614eb..a76c5c2ea 100644 --- a/foxtrot-common/src/test/java/com/flipkart/foxtrot/common/query/datetime/LastFilterTest.java +++ b/foxtrot-common/src/test/java/com/flipkart/foxtrot/common/query/datetime/LastFilterTest.java @@ -29,16 +29,16 @@ public void testGetWindowStartFloor() throws Exception { final String json = String.format( "{ \"operator\": \"last\", \"currentTime\": %d, \"roundingMode\": \"FLOOR\", \"duration\": \"1h\" }", currentTime.getMillis() - ); + ); LastFilter lastFilter = objectMapper.readValue(json, LastFilter.class); TimeWindow timeWindow = lastFilter.getWindow(); Assert.assertEquals("_timestamp", lastFilter.getField()); Assert.assertEquals(currentTime.getMillis(), lastFilter.getCurrentTime()); Assert.assertEquals(currentTime.withMinuteOfHour(0) - .withSecondOfMinute(0) - .withMillisOfSecond(0) - .minusHours(1) - .getMillis(), timeWindow.getStartTime()); + .withSecondOfMinute(0) + .withMillisOfSecond(0) + .minusHours(1) + .getMillis(), timeWindow.getStartTime()); } @Test @@ -47,15 +47,15 @@ public void testGetWindowStartCeiling() throws Exception { final String json = String.format( "{ \"operator\": \"last\", \"currentTime\": %d, \"roundingMode\": \"CEILING\", \"duration\": \"1h\" }", currentTime.getMillis() - ); + ); LastFilter lastFilter = objectMapper.readValue(json, LastFilter.class); TimeWindow timeWindow = lastFilter.getWindow(); Assert.assertEquals("_timestamp", lastFilter.getField()); Assert.assertEquals(currentTime.getMillis(), lastFilter.getCurrentTime()); Assert.assertEquals(currentTime.withMinuteOfHour(0) - .withSecondOfMinute(0) - .withMillisOfSecond(0) - .getMillis(), timeWindow.getStartTime()); + .withSecondOfMinute(0) + .withMillisOfSecond(0) + .getMillis(), timeWindow.getStartTime()); } @Test @@ -67,7 +67,7 @@ public void testGetWindowStartNoRounding() throws Exception { Assert.assertEquals("_timestamp", lastFilter.getField()); Assert.assertEquals(currentTime.getMillis(), lastFilter.getCurrentTime()); Assert.assertEquals(currentTime.minusHours(1) - .getMillis(), timeWindow.getStartTime()); + .getMillis(), timeWindow.getStartTime()); } } \ No newline at end of file diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/alerts/EmailBuilder.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/alerts/EmailBuilder.java index caa3b6a30..c51fd69fe 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/alerts/EmailBuilder.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/alerts/EmailBuilder.java @@ -34,8 +34,8 @@ public Email visit(QueryProcessingError queryProcessingError) { switch (exception.getCode()) { case CARDINALITY_OVERFLOW: { return richEmailBuilder.build(templateIdFromEvent(queryProcessingError), - Collections.emptyList(), - exception.toMap()); + Collections.emptyList(), + exception.toMap()); } default: break; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/auth/FoxtrotRole.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/auth/FoxtrotRole.java index ed2723a95..65475f184 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/auth/FoxtrotRole.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/auth/FoxtrotRole.java @@ -13,6 +13,8 @@ public enum FoxtrotRole { SYSADMIN(Value.SYSADMIN), ; + private final String value; + FoxtrotRole(String value) { this.value = value; } @@ -23,6 +25,4 @@ public static class Value { public static final String QUERY = "QUERY"; public static final String SYSADMIN = "SYSADMIN"; } - - private final String value; } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/CacheManager.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/CacheManager.java index 8589a6a9a..a7a6fd103 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/CacheManager.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/CacheManager.java @@ -20,7 +20,7 @@ public CacheManager(CacheFactory cacheFactory) { } public void create(String name) { - if(!cacheMap.containsKey(name)) { + if (!cacheMap.containsKey(name)) { cacheMap.put(name, cacheFactory.create(name)); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCache.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCache.java index 8733cb81e..60834f54e 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCache.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCache.java @@ -51,15 +51,15 @@ public DistributedCache(HazelcastConnection hazelcastConnection, String name, Ob public ActionResponse put(String key, ActionResponse data) { try { final String serializedData = mapper.writeValueAsString(data); - if(serializedData != null) { + if (serializedData != null) { // Only cache if size is less that 256 KB - if(serializedData.length() <= Constants.CACHE_VALUE_SIZE_IN_KB) { + if (serializedData.length() <= Constants.CACHE_VALUE_SIZE_IN_KB) { distributedMap.put(key, mapper.writeValueAsString(data)); } else { String responsePart = serializedData.substring(0, 1024); logger.error("Size of response is too big for cache. Skipping it. Response Part : {}", - responsePart - ); + responsePart + ); } } } catch (JsonProcessingException e) { @@ -70,11 +70,11 @@ public ActionResponse put(String key, ActionResponse data) { @Override public ActionResponse get(String key) { - if(null == key) { + if (null == key) { return null; //Hazelcast map throws NPE if key is null } String data = distributedMap.get(key); - if(null != data) { + if (null != data) { try { return mapper.readValue(data, ActionResponse.class); } catch (IOException e) { diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCacheFactory.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCacheFactory.java index da6ee8ba5..357270c51 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCacheFactory.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cache/impl/DistributedCacheFactory.java @@ -62,13 +62,13 @@ private MapConfig getDefaultMapConfig(CacheConfig cacheConfig) { evictionConfig.setSize(cacheConfig.getSize() == 0 ? DEFAULT_SIZE : cacheConfig.getSize()); evictionConfig.setMaxSizePolicy(MaxSizePolicy.USED_HEAP_SIZE); - if(cacheConfig.getMaxIdleSeconds() == 0) { + if (cacheConfig.getMaxIdleSeconds() == 0) { mapConfig.setMaxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS); } else { mapConfig.setMaxIdleSeconds(cacheConfig.getMaxIdleSeconds()); } - if(cacheConfig.getTimeToLiveSeconds() == 0) { + if (cacheConfig.getTimeToLiveSeconds() == 0) { mapConfig.setTimeToLiveSeconds(DEFAULT_TIME_TO_LIVE_SECONDS); } else { mapConfig.setTimeToLiveSeconds(cacheConfig.getTimeToLiveSeconds()); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationManager.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationManager.java index 0dfbad796..0c8843d6f 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationManager.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationManager.java @@ -59,7 +59,7 @@ protected void runImpl(LockingTaskExecutor executor, Instant lockAtMostUntil) { executor.executeWithLock(() -> { try { int maxTimeToRunJob = MAX_TIME_TO_RUN_JOB; - if(cardinalityConfig.getMaxTimeToRunJobInMinutes() != 0) { + if (cardinalityConfig.getMaxTimeToRunJobInMinutes() != 0) { maxTimeToRunJob = cardinalityConfig.getMaxTimeToRunJobInMinutes(); } Instant start = Instant.now(); @@ -67,14 +67,14 @@ protected void runImpl(LockingTaskExecutor executor, Instant lockAtMostUntil) { .stream() .map(Table::getName) .collect(Collectors.toSet()); - for(String table : tables) { - if(!tableMetadataManager.cardinalityCacheContains(table)) { + for (String table : tables) { + if (!tableMetadataManager.cardinalityCacheContains(table)) { tableMetadataManager.getFieldMappings(table, true, true); LOGGER.info("Cardinality calculated for table: {}", table); } Instant now = Instant.now(); Duration timeElapsed = Duration.between(start, now); - if(timeElapsed.compareTo(Duration.ofMinutes(maxTimeToRunJob)) > 0) { + if (timeElapsed.compareTo(Duration.ofMinutes(maxTimeToRunJob)) > 0) { break; } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationRunnable.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationRunnable.java index 58b446f14..699614166 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationRunnable.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityCalculationRunnable.java @@ -45,8 +45,8 @@ public void run() { .stream() .map(Table::getName) .collect(Collectors.toSet()); - for(String table : tables) { - if(!tableMetadataManager.cardinalityCacheContains(table)) { + for (String table : tables) { + if (!tableMetadataManager.cardinalityCacheContains(table)) { tableMetadataManager.getFieldMappings(table, true, true); LOGGER.info("Cardinality calculated for table: {}", table); } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityConfig.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityConfig.java index c2786f78f..7860d0b56 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityConfig.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/cardinality/CardinalityConfig.java @@ -46,14 +46,14 @@ public CardinalityConfig(String enabled, String batchSize) { } public boolean isEnabled() { - if(StringUtils.isEmpty(enabled)) { + if (StringUtils.isEmpty(enabled)) { return false; } return Boolean.valueOf(enabled); } public int getSubListSize() { - if(StringUtils.isEmpty(batchSize)) { + if (StringUtils.isEmpty(batchSize)) { return ElasticsearchUtils.DEFAULT_SUB_LIST_SIZE; } return Integer.parseInt(batchSize); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/Action.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/Action.java index 30344c41d..c48838f2a 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/Action.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/Action.java @@ -83,14 +83,12 @@ private void preProcessRequest() { public ActionValidationResponse validate() { try { preProcessRequest(); - } - catch (MalformedQueryException e) { + } catch (MalformedQueryException e) { return ActionValidationResponse.builder() .processedRequest(parameter) .validationErrors(e.getReasons()) .build(); - } - catch (Exception e) { + } catch (Exception e) { return ActionValidationResponse.builder() .processedRequest(parameter) .validationErrors(Collections.singletonList(e.getMessage())) @@ -184,8 +182,7 @@ protected Filter getDefaultTimeSpan() { protected String requestString() { try { return objectMapper.writeValueAsString(parameter); - } - catch (JsonProcessingException e) { + } catch (JsonProcessingException e) { logger.error("Error serializing request: ", e); return ""; } @@ -201,8 +198,7 @@ private List checkAndAddTemporalBoundary(List filters) { } if (null == filters) { filters = Lists.newArrayList(); - } - else { + } else { filters = Lists.newArrayList(filters); } filters.add(getDefaultTimeSpan()); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/DataDeletionManager.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/DataDeletionManager.java index 9c5204ddb..7e14ed470 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/DataDeletionManager.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/DataDeletionManager.java @@ -44,7 +44,7 @@ public DataDeletionManager(DataDeletionManagerConfig deletionManagerConfig, Quer @Override public void start() throws Exception { logger.info("Starting Deletion Manager"); - if(config.isActive()) { + if (config.isActive()) { logger.info("Scheduling data deletion Job"); scheduledExecutorService.scheduleAtFixedRate(() -> { LockingTaskExecutor executor = new DefaultLockingTaskExecutor( diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/PeriodSelector.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/PeriodSelector.java index f6b7fb196..8dff73555 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/PeriodSelector.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/common/PeriodSelector.java @@ -43,13 +43,13 @@ public Interval analyze() { } public Interval analyze(long currentTime) { - for(Filter filter : filters) { - if(filter.isFilterTemporal()) { + for (Filter filter : filters) { + if (filter.isFilterTemporal()) { filter.accept(this); } } - for(Filter filter : filters) { - if(ElasticsearchUtils.TIME_FIELD.equals(filter.getField())) { + for (Filter filter : filters) { + if (ElasticsearchUtils.TIME_FIELD.equals(filter.getField())) { filter.accept(this); } } @@ -60,32 +60,32 @@ public Interval analyze(long currentTime) { @Override public Void visit(BetweenFilter betweenFilter) { - timeWindow.setStartTime(Math.min((Long)betweenFilter.getFrom(), timeWindow.getStartTime())); - timeWindow.setEndTime(Math.max((Long)betweenFilter.getTo(), timeWindow.getEndTime())); + timeWindow.setStartTime(Math.min((Long) betweenFilter.getFrom(), timeWindow.getStartTime())); + timeWindow.setEndTime(Math.max((Long) betweenFilter.getTo(), timeWindow.getEndTime())); return null; } @Override public Void visit(GreaterThanFilter greaterThanFilter) { - timeWindow.setStartTime(Math.min((Long)greaterThanFilter.getValue() + 1, timeWindow.getStartTime())); + timeWindow.setStartTime(Math.min((Long) greaterThanFilter.getValue() + 1, timeWindow.getStartTime())); return null; } @Override public Void visit(GreaterEqualFilter greaterEqualFilter) { - timeWindow.setStartTime(Math.min((Long)greaterEqualFilter.getValue(), timeWindow.getStartTime())); + timeWindow.setStartTime(Math.min((Long) greaterEqualFilter.getValue(), timeWindow.getStartTime())); return null; } @Override public Void visit(LessThanFilter lessThanFilter) { - timeWindow.setEndTime(Math.max((Long)lessThanFilter.getValue() - 1, timeWindow.getEndTime())); + timeWindow.setEndTime(Math.max((Long) lessThanFilter.getValue() - 1, timeWindow.getEndTime())); return null; } @Override public Void visit(LessEqualFilter lessEqualFilter) { - timeWindow.setEndTime(Math.max((Long)lessEqualFilter.getValue(), timeWindow.getEndTime())); + timeWindow.setEndTime(Math.max((Long) lessEqualFilter.getValue(), timeWindow.getEndTime())); return null; } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStore.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStore.java index 8720f61e8..a01d6d784 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStore.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStore.java @@ -72,15 +72,15 @@ public void initializeTable(Table table, boolean forceTableCreate) { // Check for existence of HBase table during init to make sure HBase is ready for taking writes try { boolean isTableAvailable = tableWrapper.isTableAvailable(table); - if(isTableAvailable) { + if (isTableAvailable) { return; } - if(forceTableCreate) { + if (forceTableCreate) { tableWrapper.createTable(table); } else { throw FoxtrotExceptions.createTableInitializationException(table, String.format("Create HBase Table - %s", - tableWrapper.getHBaseTableName(table) - )); + tableWrapper.getHBaseTableName(table) + )); } } catch (IOException e) { throw FoxtrotExceptions.createConnectionException(table, e); @@ -90,7 +90,7 @@ public void initializeTable(Table table, boolean forceTableCreate) { @Override @Timed public Document save(final Table table, Document document) { - if(document == null || document.getData() == null || document.getId() == null) { + if (document == null || document.getData() == null || document.getId() == null) { throw FoxtrotExceptions.createBadRequestException(table.getName(), "Invalid Input Document"); } Document translatedDocument = null; @@ -108,16 +108,16 @@ public Document save(final Table table, Document document) { @Override @Timed public List saveAll(final Table table, List documents) { - if(documents == null || documents.isEmpty()) { + if (documents == null || documents.isEmpty()) { throw FoxtrotExceptions.createBadRequestException(table.getName(), "null/empty document list not allowed"); } List puts = new ArrayList<>(); ImmutableList.Builder translatedDocuments = ImmutableList.builder(); List errorMessages = new ArrayList<>(); try { - for(int i = 0; i < documents.size(); i++) { + for (int i = 0; i < documents.size(); i++) { Document document = documents.get(i); - if(!isValidDocument(document, errorMessages, i)) { + if (!isValidDocument(document, errorMessages, i)) { continue; } Document translatedDocument = translator.translate(table, document); @@ -127,7 +127,7 @@ public List saveAll(final Table table, List documents) { } catch (JsonProcessingException e) { throw FoxtrotExceptions.createBadRequestException(table, e); } - if(!errorMessages.isEmpty()) { + if (!errorMessages.isEmpty()) { throw FoxtrotExceptions.createBadRequestException(table.getName(), errorMessages); } @@ -141,17 +141,17 @@ public List saveAll(final Table table, List documents) { } private boolean isValidDocument(Document document, List errorMessages, int index) { - if(document == null) { + if (document == null) { errorMessages.add("null document at index - " + index); return false; } - if(document.getId() == null || document.getId() + if (document.getId() == null || document.getId() .trim() .isEmpty()) { errorMessages.add("null/empty document id at index - " + index); return false; } - if(document.getData() == null) { + if (document.getData() == null) { errorMessages.add("null document data at index - " + index); return false; } @@ -167,7 +167,7 @@ public Document get(final Table table, String id) { .addColumn(COLUMN_FAMILY, TIMESTAMP_FIELD_NAME) .setMaxVersions(1); Result getResult = hTable.get(get); - if(!getResult.isEmpty()) { + if (!getResult.isEmpty()) { byte[] data = getResult.getValue(COLUMN_FAMILY, DOCUMENT_FIELD_NAME); byte[] metadata = getResult.getValue(COLUMN_FAMILY, DOCUMENT_META_FIELD_NAME); byte[] timestamp = getResult.getValue(COLUMN_FAMILY, TIMESTAMP_FIELD_NAME); @@ -186,15 +186,15 @@ public Document get(final Table table, String id) { @Override @Timed public List getAll(final Table table, List ids) { - if(CollectionUtils.isEmpty(ids)) { + if (CollectionUtils.isEmpty(ids)) { throw FoxtrotExceptions.createBadRequestException(table.getName(), "Empty ID List"); } try (org.apache.hadoop.hbase.client.Table hTable = tableWrapper.getTable(table)) { List gets = new ArrayList<>(ids.size()); - for(String id : ids) { + for (String id : ids) { Get get = new Get(Bytes.toBytes(translator.rawStorageIdFromDocumentId(table, id))).addColumn(COLUMN_FAMILY, - DOCUMENT_FIELD_NAME - ) + DOCUMENT_FIELD_NAME + ) .addColumn(COLUMN_FAMILY, DOCUMENT_META_FIELD_NAME) .addColumn(COLUMN_FAMILY, TIMESTAMP_FIELD_NAME) .setMaxVersions(1); @@ -203,9 +203,9 @@ public List getAll(final Table table, List ids) { Result[] getResults = hTable.get(gets); List missingIds = new ArrayList<>(); List results = new ArrayList<>(ids.size()); - for(int index = 0; index < getResults.length; index++) { + for (int index = 0; index < getResults.length; index++) { Result getResult = getResults[index]; - if(!getResult.isEmpty()) { + if (!getResult.isEmpty()) { byte[] data = getResult.getValue(COLUMN_FAMILY, DOCUMENT_FIELD_NAME); byte[] metadata = getResult.getValue(COLUMN_FAMILY, DOCUMENT_META_FIELD_NAME); byte[] timestamp = getResult.getValue(COLUMN_FAMILY, TIMESTAMP_FIELD_NAME); @@ -233,9 +233,9 @@ public List getAll(final Table table, List ids) { @VisibleForTesting public Put getPutForDocument(Document document) throws JsonProcessingException { return new Put(Bytes.toBytes(document.getMetadata() - .getRawStorageId())).addColumn(COLUMN_FAMILY, DOCUMENT_META_FIELD_NAME, - mapper.writeValueAsBytes(document.getMetadata()) - ) + .getRawStorageId())).addColumn(COLUMN_FAMILY, DOCUMENT_META_FIELD_NAME, + mapper.writeValueAsBytes(document.getMetadata()) + ) .addColumn(COLUMN_FAMILY, DOCUMENT_FIELD_NAME, mapper.writeValueAsBytes(document.getData())) .addColumn(COLUMN_FAMILY, TIMESTAMP_FIELD_NAME, Bytes.toBytes(document.getTimestamp())) .addColumn(COLUMN_FAMILY, DATE_FIELD_NAME, mapper.writeValueAsBytes(document.getDate())); @@ -250,8 +250,7 @@ public void updateTable(final Table table) { if (isTableAvailable) { tableWrapper.updateTable(table); } - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createConnectionException(table, e); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseUtil.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseUtil.java index 291076e8b..a1298034d 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseUtil.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseUtil.java @@ -46,7 +46,7 @@ public static void createTable(final HbaseConfig hbaseConfig, final String table columnDescriptor.setCompressionType(Compression.Algorithm.GZ); hTableDescriptor.addFamily(columnDescriptor); Configuration configuration = HBaseUtil.create(hbaseConfig); - try(Connection connection = ConnectionFactory.createConnection(configuration)) { + try (Connection connection = ConnectionFactory.createConnection(configuration)) { try (Admin admin = connection.getAdmin()) { if (admin.tableExists(TableName.valueOf(tableName))) { logger.info("Table {} already exists. Nothing to do.", tableName); @@ -54,8 +54,7 @@ public static void createTable(final HbaseConfig hbaseConfig, final String table } logger.info("Creating table: {}", tableName); admin.createTable(hTableDescriptor); - } - catch (Exception e) { + } catch (Exception e) { logger.error("Could not create table: " + tableName, e); } } @@ -64,27 +63,27 @@ public static void createTable(final HbaseConfig hbaseConfig, final String table public static Configuration create(final HbaseConfig hbaseConfig) throws IOException { Configuration configuration = HBaseConfiguration.create(); - if(isValidFile(hbaseConfig.getCoreSite())) { + if (isValidFile(hbaseConfig.getCoreSite())) { configuration.addResource(new File(hbaseConfig.getCoreSite()).toURI() - .toURL()); + .toURL()); } - if(isValidFile(hbaseConfig.getHdfsSite())) { + if (isValidFile(hbaseConfig.getHdfsSite())) { configuration.addResource(new File(hbaseConfig.getHdfsSite()).toURI() - .toURL()); + .toURL()); } - if(isValidFile(hbaseConfig.getHbasePolicy())) { + if (isValidFile(hbaseConfig.getHbasePolicy())) { configuration.addResource(new File(hbaseConfig.getHbasePolicy()).toURI() - .toURL()); + .toURL()); } - if(isValidFile(hbaseConfig.getHbaseSite())) { + if (isValidFile(hbaseConfig.getHbaseSite())) { configuration.addResource(new File(hbaseConfig.getHbaseSite()).toURI() - .toURL()); + .toURL()); } - if(hbaseConfig.isSecure() && isValidFile(hbaseConfig.getKeytabFileName())) { + if (hbaseConfig.isSecure() && isValidFile(hbaseConfig.getKeytabFileName())) { configuration.set("hbase.master.kerberos.principal", hbaseConfig.getAuthString()); configuration.set("hadoop.kerberos.kinit.command", hbaseConfig.getKinitPath()); UserGroupInformation.setConfiguration(configuration); @@ -93,15 +92,15 @@ public static Configuration create(final HbaseConfig hbaseConfig) throws IOExcep logger.info("Logged into Hbase with User: {}", UserGroupInformation.getLoginUser()); } - if(null != hbaseConfig.getHbaseZookeeperQuorum()) { + if (null != hbaseConfig.getHbaseZookeeperQuorum()) { configuration.set("hbase.zookeeper.quorum", hbaseConfig.getHbaseZookeeperQuorum()); } - if(!Strings.isNullOrEmpty(hbaseConfig.getHbaseZookeeperZnodeParent())) { + if (!Strings.isNullOrEmpty(hbaseConfig.getHbaseZookeeperZnodeParent())) { configuration.set("zookeeper.znode.parent", hbaseConfig.getHbaseZookeeperZnodeParent()); } - if(null != hbaseConfig.getHbaseZookeeperClientPort()) { + if (null != hbaseConfig.getHbaseZookeeperClientPort()) { configuration.setInt("hbase.zookeeper.property.clientPort", hbaseConfig.getHbaseZookeeperClientPort()); } return configuration; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseRegions.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseRegions.java index a73c86c35..85a63336c 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseRegions.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseRegions.java @@ -35,7 +35,7 @@ public HbaseRegions(HbaseConfig hbaseConfig) { } public List> getMergeableRegions(TableName tablename, double threshSizeInGB) { - long threshSize = (long)(threshSizeInGB * BYTES_IN_GB); + long threshSize = (long) (threshSizeInGB * BYTES_IN_GB); Map hash = getRegionsMap(tablename); if (hash.isEmpty()) { return Collections.emptyList(); @@ -65,7 +65,7 @@ public List> getMergeableRegions(TableName tablename, double t } public void mergeRegions(TableName tablename, double threshSizeInGB, int numberOfMerges) { - long threshSize = (long)(threshSizeInGB * BYTES_IN_GB); + long threshSize = (long) (threshSizeInGB * BYTES_IN_GB); if (numberOfMerges == -1) { numberOfMerges = Integer.MAX_VALUE; } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseTableConnection.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseTableConnection.java index 519c429af..a5b17c809 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseTableConnection.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HbaseTableConnection.java @@ -63,7 +63,7 @@ public HbaseTableConnection(Configuration configuration, HbaseConfig hbaseConfig public synchronized org.apache.hadoop.hbase.client.Table getTable(final Table table) { try { - if(hbaseConfig.isSecure() && UserGroupInformation.isSecurityEnabled()) { + if (hbaseConfig.isSecure() && UserGroupInformation.isSecurityEnabled()) { UserGroupInformation.getCurrentUser() .reloginFromKeytab(); } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/EmailClient.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/EmailClient.java index 80d4d1bf6..40d1b9b71 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/EmailClient.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/EmailClient.java @@ -39,7 +39,7 @@ public EmailClient(EmailConfig emailConfig) { } public boolean sendEmail(final Email email) { - if(Strings.isNullOrEmpty(emailConfig.getFrom())) { + if (Strings.isNullOrEmpty(emailConfig.getFrom())) { LOGGER.warn("Mail config not set properly. No mail will be sent."); return false; } @@ -47,18 +47,18 @@ public boolean sendEmail(final Email email) { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(emailConfig.getFrom())); final List recipients = recipients(email); - if(recipients.isEmpty()) { + if (recipients.isEmpty()) { return false; } message.setRecipients(Message.RecipientType.TO, - InternetAddress.parse(String.join(",",recipients))); + InternetAddress.parse(String.join(",", recipients))); message.setSubject(email.getSubject()); InternetHeaders headers = new InternetHeaders(); headers.addHeader("Content-type", "text/html; charset=UTF-8"); final String content = email.getContent(); - if(null != content) { + if (null != content) { BodyPart messageBodyPart = new MimeBodyPart(headers, content.getBytes(StandardCharsets.UTF_8)); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); @@ -77,10 +77,10 @@ private List recipients(Email email) { final List emailRecipients = email.getRecipients(); final List defaultRecipients = emailConfig.getEventNotificationEmails(); final ImmutableList.Builder recipientsBuilder = ImmutableList.builder(); - if(null != email.getRecipients()) { + if (null != email.getRecipients()) { recipientsBuilder.addAll(emailRecipients); } - if(null != defaultRecipients) { + if (null != defaultRecipients) { recipientsBuilder.addAll(defaultRecipients); } return recipientsBuilder.build(); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/RichEmailBuilder.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/RichEmailBuilder.java index efba95e6e..12c6dd3e2 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/RichEmailBuilder.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/RichEmailBuilder.java @@ -25,7 +25,7 @@ public RichEmailBuilder( } public final Email build(final String id, final List recipients, final Map context) { - if(null == subjectBuilder || null == bodyBuilder) { + if (null == subjectBuilder || null == bodyBuilder) { return Email.builder().build(); } return new Email(subjectBuilder.content(id, context), bodyBuilder.content(id, context), recipients); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailBodyBuilder.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailBodyBuilder.java index 820fd8af3..5e76d5d2c 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailBodyBuilder.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailBodyBuilder.java @@ -13,13 +13,14 @@ public class StrSubstitutorEmailBodyBuilder implements EmailBodyBuilder { private static final Map TEMPLATES = ImmutableMap.builder() .put("query_processing_error_cardinality_overflow", - "Blocked Query: ${requestStr}\n" + - "Suspect field: ${field}\n" + - "Probability of screwing up the cluster: ${probability}") + "Blocked Query: ${requestStr}\n" + + "Suspect field: ${field}\n" + + "Probability of screwing up the cluster: ${probability}") .build(); + @Override public String content(String identifier, Map context) { - if(!TEMPLATES.containsKey(identifier)) { + if (!TEMPLATES.containsKey(identifier)) { return ""; } return StrSubstitutor.replace(TEMPLATES.get(identifier), context); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailSubjectBuilder.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailSubjectBuilder.java index bad26d6d3..6c4ef8698 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailSubjectBuilder.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/email/messageformatting/impl/StrSubstitutorEmailSubjectBuilder.java @@ -13,11 +13,12 @@ public class StrSubstitutorEmailSubjectBuilder implements EmailSubjectBuilder { private static final Map TEMPLATES = ImmutableMap.builder() .put("query_processing_error_cardinality_overflow", - "Blocked query as it might have screwed up the cluster") + "Blocked query as it might have screwed up the cluster") .build(); + @Override public String content(String identifier, Map context) { - if(!TEMPLATES.containsKey(identifier)) { + if (!TEMPLATES.containsKey(identifier)) { return ""; } return StrSubstitutor.replace(TEMPLATES.get(identifier), context); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/CardinalityOverflowException.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/CardinalityOverflowException.java index b5f4af92f..e804e5e99 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/CardinalityOverflowException.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/CardinalityOverflowException.java @@ -30,10 +30,10 @@ public CardinalityOverflowException(ActionRequest actionRequest, String requestS @Override public Map toMap() { return ImmutableMap.builder() - .put("field", this.field) - .put("probability", this.probability) - .put("request", this.actionRequest) - .put("requestStr", this.requestStr) - .build(); + .put("field", this.field) + .put("probability", this.probability) + .put("request", this.actionRequest) + .put("requestStr", this.requestStr) + .build(); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/ConsoleQueryBlockedException.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/ConsoleQueryBlockedException.java index d65fcb752..422e14781 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/ConsoleQueryBlockedException.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/ConsoleQueryBlockedException.java @@ -16,7 +16,7 @@ public class ConsoleQueryBlockedException extends FoxtrotException { public ConsoleQueryBlockedException(ActionRequest actionRequest) { super(ErrorCode.CONSOLE_QUERY_BLOCKED, - "Console Query blocked due to high load. Kindly run after sometime"); + "Console Query blocked due to high load. Kindly run after sometime"); this.actionRequest = actionRequest; } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/FoxtrotExceptions.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/FoxtrotExceptions.java index 5169efd86..2bcc4a2f4 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/FoxtrotExceptions.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/exception/FoxtrotExceptions.java @@ -11,7 +11,8 @@ */ public class FoxtrotExceptions { - private FoxtrotExceptions() {} + private FoxtrotExceptions() { + } public static TableInitializationException createTableInitializationException(Table table, String message) { return new TableInitializationException(table.getName(), message); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/InternalEventBus.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/InternalEventBus.java index c06b53a9f..33d94bb2c 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/InternalEventBus.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/InternalEventBus.java @@ -5,5 +5,6 @@ */ public interface InternalEventBus { void publish(final InternalSystemEvent systemEvent); + void subscribe(final InternalEventBusConsumer consumer); } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/events/QueryProcessingError.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/events/QueryProcessingError.java index bc6a21743..0e70051f7 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/events/QueryProcessingError.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/events/QueryProcessingError.java @@ -10,7 +10,7 @@ * */ @Data -public class QueryProcessingError implements InternalSystemEvent { +public class QueryProcessingError implements InternalSystemEvent { private final ActionRequest request; private final FoxtrotException exception; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/impl/GuavaInternalEventBus.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/impl/GuavaInternalEventBus.java index 1fcb84dc5..357da2895 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/impl/GuavaInternalEventBus.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/internalevents/impl/GuavaInternalEventBus.java @@ -15,19 +15,6 @@ @Singleton public class GuavaInternalEventBus implements InternalEventBus { - private class Subscriber { - private final InternalEventBusConsumer consumer; - - private Subscriber(InternalEventBusConsumer consumer) { - this.consumer = consumer; - } - - @Subscribe - public void receive(InternalSystemEvent event) { - consumer.process(event); - } - } - private final AsyncEventBus eventBus; public GuavaInternalEventBus() { @@ -43,4 +30,17 @@ public void publish(InternalSystemEvent systemEvent) { public void subscribe(InternalEventBusConsumer consumer) { eventBus.register(new Subscriber(consumer)); } + + private class Subscriber { + private final InternalEventBusConsumer consumer; + + private Subscriber(InternalEventBusConsumer consumer) { + this.consumer = consumer; + } + + @Subscribe + public void receive(InternalSystemEvent event) { + consumer.process(event); + } + } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/BaseJobManager.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/BaseJobManager.java index 5cd332a32..fb4202ad4 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/BaseJobManager.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/BaseJobManager.java @@ -36,7 +36,7 @@ public BaseJobManager(BaseJobConfig baseJobConfig, ScheduledExecutorService sche @Override public void start() { LOGGER.info("Starting {} Manager", baseJobConfig.getJobName()); - if(!baseJobConfig.isActive()) { + if (!baseJobConfig.isActive()) { LOGGER.info("Config is not active. Hence, aborting the {} job", baseJobConfig.getJobName()); } LOGGER.info("Scheduling {} Job", baseJobConfig.getJobName()); @@ -44,7 +44,7 @@ public void start() { Calendar now = Calendar.getInstance(); String timeZone = now.getTimeZone() .getID(); - if(StringUtils.isEmpty(timeZone)) { + if (StringUtils.isEmpty(timeZone)) { timeZone = TIME_ZONE; } ZoneId currentZone = ZoneId.of(timeZone); @@ -52,7 +52,7 @@ public void start() { ZonedDateTime timeToRunJob = zonedNow.withHour(baseJobConfig.getInitialDelay()) .withMinute(0) .withSecond(0); - if(zonedNow.compareTo(timeToRunJob) > 0) + if (zonedNow.compareTo(timeToRunJob) > 0) timeToRunJob = timeToRunJob.plusDays(1); Duration duration = Duration.between(zonedNow, timeToRunJob); @@ -63,7 +63,7 @@ public void start() { LockingTaskExecutor executor = new DefaultLockingTaskExecutor( new HazelcastLockProvider(hazelcastConnection.getHazelcast())); int lockAtMost = LOCK_AT_MOST; - if(baseJobConfig.getLockAtMostInMinutes() != 0) { + if (baseJobConfig.getLockAtMostInMinutes() != 0) { lockAtMost = baseJobConfig.getLockAtMostInMinutes(); } Instant lockAtMostUntil = Instant.now() diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/optimization/EsIndexOptimizationManager.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/optimization/EsIndexOptimizationManager.java index f7c71a717..cdb347973 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/optimization/EsIndexOptimizationManager.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/jobs/optimization/EsIndexOptimizationManager.java @@ -63,34 +63,33 @@ protected void runImpl(LockingTaskExecutor executor, Instant lockAtMostUntil) { val candidateIndices = Arrays.stream(indexes) .filter(index -> { String table = ElasticsearchUtils.getTableNameFromIndex(index); - if(Strings.isNullOrEmpty(table)) { + if (Strings.isNullOrEmpty(table)) { return false; } String currentIndex = ElasticsearchUtils.getCurrentIndex(table, System.currentTimeMillis()); String nextDayIndex = ElasticsearchUtils.getCurrentIndex(table, System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1)); - if(index.equals(currentIndex) || index.equals(nextDayIndex)) { + if (index.equals(currentIndex) || index.equals(nextDayIndex)) { return false; } return true; }) .collect(Collectors.toSet()); List> batchOfIndicesToOptimize = CollectionUtils.partition(candidateIndices, BATCH_SIZE); - for(List indices : batchOfIndicesToOptimize) { + for (List indices : batchOfIndicesToOptimize) { Stopwatch stopwatch = Stopwatch.createStarted(); elasticsearchConnection.getClient() .indices() .forcemerge(new ForceMergeRequest(indices.toArray(new String[0])) - .maxNumSegments(SEGMENTS_TO_OPTIMIZE_TO) - .flush(true) - .onlyExpungeDeletes(false), RequestOptions.DEFAULT); + .maxNumSegments(SEGMENTS_TO_OPTIMIZE_TO) + .flush(true) + .onlyExpungeDeletes(false), RequestOptions.DEFAULT); LOGGER.info("No of indexes optimized : {}", indices.size()); MetricUtil.getInstance() .registerActionSuccess("indexesOptimized", CollectionUtils.mkString(indices, ","), - stopwatch.elapsed(TimeUnit.MILLISECONDS) - ); + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ); } - } - catch (IOException e) { + } catch (IOException e) { LOGGER.error("Error getting index list", e); } }, new LockConfiguration(esIndexOptimizationConfig.getJobName(), lockAtMostUntil)); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/parsers/ElasticsearchMappingParser.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/parsers/ElasticsearchMappingParser.java index a61068695..a5add00c4 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/parsers/ElasticsearchMappingParser.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/parsers/ElasticsearchMappingParser.java @@ -49,22 +49,22 @@ private Set generateFieldMappings(String parentField, JsonNode js Iterator> iterator = jsonNode.fields(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - if(entry.getKey() + if (entry.getKey() .startsWith(ElasticsearchUtils.DOCUMENT_META_FIELD_NAME)) { continue; } String currentField = (parentField == null) ? entry.getKey() : (String.format("%s.%s", parentField, entry.getKey())); - if(entry.getValue() + if (entry.getValue() .has(PROPERTIES)) { fieldTypeMappings.addAll(generateFieldMappings(currentField, entry.getValue() .get(PROPERTIES))); } else { FieldType fieldType = getFieldType(entry.getValue() - .get("type")); + .get("type")); fieldTypeMappings.add(FieldMetadata.builder() - .field(currentField) - .type(fieldType) - .build()); + .field(currentField) + .type(fieldType) + .build()); } } return fieldTypeMappings; @@ -73,7 +73,7 @@ private Set generateFieldMappings(String parentField, JsonNode js private FieldType getFieldType(JsonNode jsonNode) { String type = jsonNode.asText(); FieldType fieldType = FieldType.valueOf(type.toUpperCase()); - if(fieldType == FieldType.TEXT || fieldType == FieldType.KEYWORD) { + if (fieldType == FieldType.TEXT || fieldType == FieldType.KEYWORD) { return FieldType.STRING; } return fieldType; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/ActionExecutionObserver.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/ActionExecutionObserver.java index 7ab1c42d6..7a6d4ecfb 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/ActionExecutionObserver.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/ActionExecutionObserver.java @@ -6,6 +6,9 @@ * */ public interface ActionExecutionObserver { - default void preExecution(ActionRequest request) {} - default void postExecution(ActionEvaluationResponse response) {} + default void preExecution(ActionRequest request) { + } + + default void postExecution(ActionEvaluationResponse response) { + } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/EventPublisherActionExecutionObserver.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/EventPublisherActionExecutionObserver.java index ec39ec5b8..1700d4239 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/EventPublisherActionExecutionObserver.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/EventPublisherActionExecutionObserver.java @@ -17,16 +17,15 @@ public EventPublisherActionExecutionObserver(InternalEventBus eventBus) { @Override public void postExecution(ActionEvaluationResponse response) { - if (null == response){ + if (null == response) { return; } - if(null != response.getException()) { + if (null != response.getException()) { eventBus.publish(new QueryProcessingError(response.getRequest(), response.getException())); - } - else { + } else { eventBus.publish(new QueryProcessed(response.getRequest(), - response.getResponse(), - response.getElapsedTime())); + response.getResponse(), + response.getElapsedTime())); } } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/QueryExecutor.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/QueryExecutor.java index 989f29c20..0b77fec8d 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/QueryExecutor.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/QueryExecutor.java @@ -84,8 +84,7 @@ public ActionResponse execute(T request) { evaluationResponse = ActionEvaluationResponse.failure( action, request, e, stopwatch.elapsed(TimeUnit.MILLISECONDS)); throw e; - } - finally { + } finally { notifyObserverPostExec(evaluationResponse); } } @@ -95,7 +94,7 @@ public AsyncDataToken executeAsync(T request) { final String cacheKey = action.cacheKey(); final AsyncDataToken dataToken = new AsyncDataToken(request.getOpcode(), cacheKey); final ActionResponse response = readCachedData(analyticsLoader.getCacheManager(), request, action); - if(null != response) { + if (null != response) { // If data exists in the cache nothing to do.. just return return dataToken; } @@ -111,14 +110,14 @@ public AsyncDataToken executeAsync(T request) { public Action resolve(T request) { Action action; action = analyticsLoader.getAction(request); - if(null == action) { + if (null == action) { throw FoxtrotExceptions.createUnresolvableActionException(request); } return action; } private void notifyObserverPreExec(final ActionRequest request) { - if(null == executionObservers) { + if (null == executionObservers) { return; } executionObservers @@ -126,7 +125,7 @@ private void notifyObserverPreExec(final ActionRequest request) { } private void notifyObserverPostExec(final ActionEvaluationResponse evaluationResponse) { - if(null == executionObservers) { + if (null == executionObservers) { return; } executionObservers @@ -142,8 +141,7 @@ private ActionResponse readCachedData(final CacheManager cacheManager, if (cache.has(cacheKey)) { log.info("Cache hit for key: {}", cacheKey); return cache.get(cacheKey); - } - else { + } else { log.info("Cache miss for key: {}", cacheKey); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Constants.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Constants.java index 4f470bbbf..689a9f923 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Constants.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Constants.java @@ -25,12 +25,14 @@ public class Constants { public static final Map rawKeyVersionToSuffixMap = ImmutableMap.builder().put("2.0", - "__RAW_KEY_VERSION_2__" - ) + "__RAW_KEY_VERSION_2__" + ) .build(); public static final String FIELD_REPLACEMENT_REGEX = "[^a-zA-Z0-9\\-_]"; public static final String FIELD_REPLACEMENT_VALUE = "_"; public static final String SEPARATOR = "_--&--_"; public static final String CACHE_NAME_PREFIX = "cache-for-"; - private Constants() {} + + private Constants() { + } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/CountAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/CountAction.java index 614a9f92e..f51a16049 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/CountAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/CountAction.java @@ -73,12 +73,12 @@ public String getRequestCacheKey() { } filterHashKey += 31 * (request.isDistinct() - ? "TRUE".hashCode() - : "FALSE".hashCode()); + ? "TRUE".hashCode() + : "FALSE".hashCode()); filterHashKey += 31 * (request.getField() != null - ? request.getField() - .hashCode() - : "COLUMN".hashCode()); + ? request.getField() + .hashCode() + : "COLUMN".hashCode()); return String.format("count-%s-%d", request.getTable(), filterHashKey); } @@ -105,8 +105,7 @@ public ActionResponse execute(CountRequest parameter) { .getClient() .search(request, RequestOptions.DEFAULT); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } @@ -117,28 +116,25 @@ public SearchRequest getRequestBuilder(CountRequest parameter, List extr if (parameter.isDistinct()) { try { return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) - .indicesOptions(Utils.indicesOptions()) - .source(new SearchSourceBuilder() - .size(QUERY_SIZE) - .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) - .aggregation(Utils.buildCardinalityAggregation(parameter.getField(), - parameter.accept(new CountPrecisionThresholdVisitorAdapter( - elasticsearchTuningConfig.getPrecisionThreshold()))))) - ; - } - catch (Exception e) { + .indicesOptions(Utils.indicesOptions()) + .source(new SearchSourceBuilder() + .size(QUERY_SIZE) + .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) + .aggregation(Utils.buildCardinalityAggregation(parameter.getField(), + parameter.accept(new CountPrecisionThresholdVisitorAdapter( + elasticsearchTuningConfig.getPrecisionThreshold()))))) + ; + } catch (Exception e) { throw FoxtrotExceptions.queryCreationException(parameter, e); } - } - else { + } else { try { return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(QUERY_SIZE) - .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters))); - } - catch (Exception e) { + .size(QUERY_SIZE) + .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters))); + } catch (Exception e) { throw FoxtrotExceptions.queryCreationException(parameter, e); } } @@ -151,14 +147,12 @@ public ActionResponse getResponse(org.elasticsearch.action.ActionResponse respon Cardinality cardinality = aggregations.get(Utils.sanitizeFieldForAggregation(parameter.getField())); if (cardinality == null) { return new CountResponse(0); - } - else { + } else { return new CountResponse(cardinality.getValue()); } - } - else { + } else { return new CountResponse(((SearchResponse) response).getHits() - .getTotalHits()); + .getTotalHits()); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/DistinctAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/DistinctAction.java index d54885497..526815ab6 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/DistinctAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/DistinctAction.java @@ -85,8 +85,7 @@ public void validateImpl(DistinctRequest parameter) { if (CollectionUtils.isNullOrEmpty(parameter.getNesting())) { validationErrors.add("At least one nesting parameter is required"); - } - else { + } else { for (ResultSort resultSort : com.collections.CollectionUtils.nullSafeList(parameter.getNesting())) { if (CollectionUtils.isNullOrEmpty(resultSort.getField())) { @@ -108,8 +107,7 @@ public ActionResponse execute(DistinctRequest request) { SearchRequest query; try { query = getRequestBuilder(request, Collections.emptyList()); - } - catch (Exception e) { + } catch (Exception e) { throw FoxtrotExceptions.queryCreationException(request, e); } @@ -119,8 +117,7 @@ public ActionResponse execute(DistinctRequest request) { .search(query); return getResponse(response, getParameter()); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(request, e); } } @@ -131,14 +128,13 @@ public SearchRequest getRequestBuilder(DistinctRequest request, List ext return new SearchRequest(ElasticsearchUtils.getIndices(request.getTable(), request)) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .query(ElasticsearchQueryUtils.translateFilter(request, extraFilters)) - .size(QUERY_SIZE) - .aggregation(Utils.buildTermsAggregation( - request.getNesting(), Sets.newHashSet(), elasticsearchTuningConfig.getAggregationSize())) - .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS))); + .query(ElasticsearchQueryUtils.translateFilter(request, extraFilters)) + .size(QUERY_SIZE) + .aggregation(Utils.buildTermsAggregation( + request.getNesting(), Sets.newHashSet(), elasticsearchTuningConfig.getAggregationSize())) + .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS))); - } - catch (Exception e) { + } catch (Exception e) { throw FoxtrotExceptions.queryCreationException(request, e); } } @@ -175,14 +171,13 @@ private void flatten( Aggregations aggregations) { final String field = fields.get(0); final List remainingFields = (fields.size() > 1) - ? fields.subList(1, fields.size()) - : new ArrayList<>(); + ? fields.subList(1, fields.size()) + : new ArrayList<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); for (Terms.Bucket bucket : terms.getBuckets()) { if (fields.size() == 1) { responseList.add(getValueList(parentKey, String.valueOf(bucket.getKey()))); - } - else { + } else { flatten(getProperKey(parentKey, String.valueOf(bucket.getKey())), remainingFields, responseList, @@ -193,8 +188,8 @@ private void flatten( private String getProperKey(String parentKey, String currentKey) { return parentKey == null - ? currentKey - : parentKey + Constants.SEPARATOR + currentKey; + ? currentKey + : parentKey + Constants.SEPARATOR + currentKey; } private List getValueList(String parentKey, String currentKey) { diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/FilterAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/FilterAction.java index b23915cd6..17b83ec49 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/FilterAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/FilterAction.java @@ -66,7 +66,7 @@ public FilterAction(Query parameter, AnalyticsLoader analyticsLoader) { @Override public void preprocess() { getParameter().setTable(ElasticsearchUtils.getValidTableName(getParameter().getTable())); - if(null == getParameter().getSort()) { + if (null == getParameter().getSort()) { ResultSort resultSort = new ResultSort(); resultSort.setField("_timestamp"); resultSort.setOrder(ResultSort.Order.desc); @@ -83,8 +83,8 @@ public String getMetricKey() { public String getRequestCacheKey() { long filterHashKey = 0L; Query query = getParameter(); - if(null != query.getFilters()) { - for(Filter filter : query.getFilters()) { + if (null != query.getFilters()) { + for (Filter filter : query.getFilters()) { filterHashKey += 31 * filter.hashCode(); } } @@ -97,27 +97,27 @@ public String getRequestCacheKey() { @Override public void validateImpl(Query parameter) { List validationErrors = new ArrayList<>(); - if(CollectionUtils.isNullOrEmpty(parameter.getTable())) { + if (CollectionUtils.isNullOrEmpty(parameter.getTable())) { validationErrors.add("table name cannot be null or empty"); } - if(parameter.getSort() == null) { + if (parameter.getSort() == null) { validationErrors.add("sort order needs to be specified"); } - if(parameter.getFrom() < 0) { + if (parameter.getFrom() < 0) { validationErrors.add("from must be non-negative integer"); } - if(parameter.getLimit() <= 0) { + if (parameter.getLimit() <= 0) { validationErrors.add("limit must be positive integer"); } - if (parameter.getLimit() > elasticsearchTuningConfig.getDocumentsLimitAllowed()){ + if (parameter.getLimit() > elasticsearchTuningConfig.getDocumentsLimitAllowed()) { validationErrors.add(String.format("Limit more than %s is not supported", - elasticsearchTuningConfig.getDocumentsLimitAllowed())); + elasticsearchTuningConfig.getDocumentsLimitAllowed())); } - if(!CollectionUtils.isNullOrEmpty(validationErrors)) { + if (!CollectionUtils.isNullOrEmpty(validationErrors)) { throw FoxtrotExceptions.createMalformedQueryException(parameter, validationErrors); } } @@ -155,7 +155,7 @@ public ActionResponse getResponse(org.elasticsearch.action.ActionResponse respon return QueryResponse .builder() .documents(getQueryStore() - .getAll(parameter.getTable(), ids, true)) + .getAll(parameter.getTable(), ids, true)) .totalHits(searchHits.getTotalHits()) .build(); } @@ -173,12 +173,12 @@ private SearchRequest getSearchRequest(Query parameter, List extraFilter .types(ElasticsearchUtils.DOCUMENT_TYPE_NAME) .searchType(SearchType.QUERY_THEN_FETCH) .source(new SearchSourceBuilder() - .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) - .size(parameter.getLimit()) - .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) - .sort(Utils.storedFieldName(parameter.getSort().getField()), - ResultSort.Order.desc == parameter.getSort().getOrder() - ? SortOrder.DESC : SortOrder.ASC)); + .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) + .size(parameter.getLimit()) + .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) + .sort(Utils.storedFieldName(parameter.getSort().getField()), + ResultSort.Order.desc == parameter.getSort().getOrder() + ? SortOrder.DESC : SortOrder.ASC)); } private ActionResponse executeScrollRequest(Query parameter, List extraFilters) { @@ -191,14 +191,13 @@ private ActionResponse executeScrollRequest(Query parameter, List extraF SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); scrollRequest.scroll(TimeValue.timeValueSeconds(elasticsearchTuningConfig.getScrollTimeInSeconds())); SearchResponse searchScrollResponse = getConnection().getClient().scroll(scrollRequest, - RequestOptions.DEFAULT); + RequestOptions.DEFAULT); scrollId = searchScrollResponse.getScrollId(); SearchHits hits = searchScrollResponse.getHits(); for (SearchHit searchHit : hits) { ids.add(searchHit.getId()); } - } - else { + } else { SearchRequest searchRequest = getScrollRequestBuilder(parameter, extraFilters); SearchResponse searchResponse = getConnection() .getClient() @@ -211,8 +210,7 @@ private ActionResponse executeScrollRequest(Query parameter, List extraF scrollId = searchResponse.getScrollId(); } return getResponse(parameter, totalHits, ids, scrollId); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } @@ -229,7 +227,7 @@ private QueryResponse getResponse(Query parameter, long totalHits, List return QueryResponse .builder() .documents(getQueryStore() - .getAll(parameter.getTable(), ids, true)) + .getAll(parameter.getTable(), ids, true)) .totalHits(totalHits) .scrollId(scrollId) .moreDataAvailable(StringUtils.isNotEmpty(scrollId) ? true : false) @@ -244,8 +242,7 @@ private ActionResponse executeRequest(Query parameter) { .getClient() .search(search); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/GroupAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/GroupAction.java index c522f703d..d3620ad23 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/GroupAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/GroupAction.java @@ -122,8 +122,8 @@ public String getRequestCacheKey() { .hashCode(); } - if(null != query.getAggregationType()){ - filterHashKey += 31 * query.getAggregationType().hashCode(); + if (null != query.getAggregationType()) { + filterHashKey += 31 * query.getAggregationType().hashCode(); } for (int i = 0; i < query.getNesting() @@ -144,13 +144,12 @@ public void validateImpl(GroupRequest parameter) { if (CollectionUtils.isNullOrEmpty(parameter.getNesting())) { validationErrors.add("at least one grouping parameter is required"); - } - else { + } else { validationErrors.addAll(parameter.getNesting() - .stream() - .filter(CollectionUtils::isNullOrEmpty) - .map(field -> "grouping parameter cannot have null or empty name") - .collect(Collectors.toList())); + .stream() + .filter(CollectionUtils::isNullOrEmpty) + .map(field -> "grouping parameter cannot have null or empty name") + .collect(Collectors.toList())); } if (parameter.getUniqueCountOn() != null && parameter.getUniqueCountOn() @@ -180,8 +179,7 @@ public ActionResponse execute(GroupRequest parameter) { .getClient() .search(query, RequestOptions.DEFAULT); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } @@ -191,10 +189,10 @@ public SearchRequest getRequestBuilder(GroupRequest parameter, List extr return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(QUERY_SIZE) - .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) + .size(QUERY_SIZE) + .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) - .aggregation(buildAggregation(parameter))); + .aggregation(buildAggregation(parameter))); } @Override @@ -217,25 +215,25 @@ private double estimateProbability(TableFieldMapping tableFieldMapping, GroupReq long estimatedMaxDocCount = extractMaxDocCount(metaMap); log.debug("cacheKey:{} msg:DOC_COUNT_ESTIMATION_COMPLETED maxDocCount:{}", cacheKey, estimatedMaxDocCount); long estimatedDocCountBasedOnTime = estimateDocCountBasedOnTime(estimatedMaxDocCount, - parameter, - getTableMetadataManager(), - tableFieldMapping.getTable() - ); + parameter, + getTableMetadataManager(), + tableFieldMapping.getTable() + ); log.debug("cacheKey:{} msg:TIME_BASED_DOC_ESTIMATION_COMPLETED maxDocCount:{} docCountAfterTimeFilters:{}", - cacheKey, - estimatedMaxDocCount, - estimatedDocCountBasedOnTime - ); + cacheKey, + estimatedMaxDocCount, + estimatedDocCountBasedOnTime + ); long estimatedDocCountAfterFilters = estimateDocCountWithFilters(estimatedDocCountBasedOnTime, - metaMap, - parameter.getFilters()); + metaMap, + parameter.getFilters()); log.debug("cacheKey:{} msg:ALL_FILTER_ESTIMATION_COMPLETED maxDocCount:{} docCountAfterTimeFilters:{} " + "docCountAfterFilters:{}", - cacheKey, estimatedMaxDocCount, estimatedDocCountBasedOnTime, estimatedDocCountAfterFilters - ); + cacheKey, estimatedMaxDocCount, estimatedDocCountBasedOnTime, estimatedDocCountAfterFilters + ); if (estimatedDocCountAfterFilters < MIN_ESTIMATION_THRESHOLD) { log.debug("cacheKey:{} msg:NESTING_ESTIMATION_SKIPPED estimatedDocCount:{} threshold:{}", cacheKey, - estimatedDocCountAfterFilters, MIN_ESTIMATION_THRESHOLD - ); + estimatedDocCountAfterFilters, MIN_ESTIMATION_THRESHOLD + ); return 0.0; } @@ -248,9 +246,9 @@ private double estimateProbability(TableFieldMapping tableFieldMapping, GroupReq FieldMetadata metadata = metaMap.get(field); if (null == metadata || null == metadata.getEstimationData()) { log.warn("cacheKey:{} msg:NO_FIELD_ESTIMATION_DATA table:{} field:{}", - cacheKey, - parameter.getTable(), - field); + cacheKey, + parameter.getTable(), + field); continue; } long fieldCardinality = metadata.getEstimationData() @@ -280,17 +278,17 @@ public Long visit(TermHistogramEstimationData termEstimationData) { } }); log.debug("cacheKey:{} msg:NESTING_FIELD_ESTIMATED field:{} overallCardinality:{} fieldCardinality:{} " + "newCardinality:{}", - cacheKey, field, outputCardinality, fieldCardinality, outputCardinality * fieldCardinality - ); + cacheKey, field, outputCardinality, fieldCardinality, outputCardinality * fieldCardinality + ); fieldCardinality = (long) Utils.ensureOne(fieldCardinality); log.debug("cacheKey:{} msg:NESTING_FIELD_ESTIMATION_COMPLETED field:{} overallCardinality:{} " + - "fieldCardinality:{} newCardinality:{}", - cacheKey, - field, - outputCardinality, - fieldCardinality, - outputCardinality * fieldCardinality - ); + "fieldCardinality:{} newCardinality:{}", + cacheKey, + field, + outputCardinality, + fieldCardinality, + outputCardinality * fieldCardinality + ); outputCardinality *= fieldCardinality; } @@ -305,13 +303,13 @@ public Long visit(TermHistogramEstimationData termEstimationData) { } log.debug("cacheKey:{} msg:NESTING_FIELDS_ESTIMATION_COMPLETED maxDocCount:{} docCountAfterTimeFilters:{} " + - "docCountAfterFilters:{} outputCardinality:{}", - cacheKey, - estimatedMaxDocCount, - estimatedDocCountBasedOnTime, - estimatedDocCountAfterFilters, - outputCardinality - ); + "docCountAfterFilters:{} outputCardinality:{}", + cacheKey, + estimatedMaxDocCount, + estimatedDocCountBasedOnTime, + estimatedDocCountAfterFilters, + outputCardinality + ); long maxCardinality = MAX_CARDINALITY; if (getQueryStore() instanceof ElasticsearchQueryStore && ((ElasticsearchQueryStore) getQueryStore()).getCardinalityConfig() != null && @@ -322,17 +320,16 @@ public Long visit(TermHistogramEstimationData termEstimationData) { } if (outputCardinality > maxCardinality) { log.warn("Output cardinality : {}, estimatedMaxDocCount : {}, estimatedDocCountBasedOnTime : {}, " + - "estimatedDocCountAfterFilters : {}, TableFieldMapping : {}, Query: {}", - outputCardinality, - estimatedMaxDocCount, - estimatedDocCountBasedOnTime, - estimatedDocCountAfterFilters, - tableFieldMapping, - parameter.toString() - ); + "estimatedDocCountAfterFilters : {}, TableFieldMapping : {}, Query: {}", + outputCardinality, + estimatedMaxDocCount, + estimatedDocCountBasedOnTime, + estimatedDocCountAfterFilters, + tableFieldMapping, + parameter.toString() + ); return 1.0; - } - else { + } else { return 0; } } @@ -356,8 +353,7 @@ private long estimateDocCountBasedOnTime( //This is done because we only store docs for last maxDays. Sometimes, we get startTime starting from 1970 year if (days > maxDays) { return currentDocCount * maxDays; - } - else { + } else { return (long) (currentDocCount * days); } } @@ -367,9 +363,9 @@ private long extractMaxDocCount(Map metaMap) { return metaMap.values() .stream() .map(x -> x.getEstimationData() == null - ? 0 - : x.getEstimationData() - .getCount()) + ? 0 + : x.getEstimationData() + .getCount()) .max(Comparator.naturalOrder()) .orElse((long) 0); } @@ -393,19 +389,19 @@ private long estimateDocCountWithFilters( continue; } log.debug("cacheKey:{} msg:FILTER_ESTIMATION_STARTED filter:{} mapping:{}", - cacheKey, - filter, - fieldMetadata); + cacheKey, + filter, + fieldMetadata); double currentFilterMultiplier = fieldMetadata.getEstimationData() .accept(getDocCountWithFilterEstimationDataVisitor(filter, cacheKey)); log.debug("cacheKey:{} msg:FILTER_ESTIMATION_COMPLETED field:{} fieldMultiplier:{} overallOldMultiplier:{} " + - "overallNewMultiplier:{}", - cacheKey, - filterField, - currentFilterMultiplier, - overallFilterMultiplier, - overallFilterMultiplier * currentFilterMultiplier - ); + "overallNewMultiplier:{}", + cacheKey, + filterField, + currentFilterMultiplier, + overallFilterMultiplier, + overallFilterMultiplier * currentFilterMultiplier + ); overallFilterMultiplier *= currentFilterMultiplier; } return (long) (currentDocCount * overallFilterMultiplier); @@ -471,7 +467,7 @@ public Double visit(ContainsFilter stringContainsFilterElement) { public Double visit(InFilter inFilter) { // Assuming there are M matches, the probability is M/N return Utils.ensurePositive(inFilter.getValues() - .size()) / Utils.ensureOne(fixedEstimationData.getCount()); + .size()) / Utils.ensureOne(fixedEstimationData.getCount()); } @Override @@ -507,9 +503,9 @@ public Double visit(BetweenFilter betweenFilter) { int numBuckets = maxBound - minBound + 1; final double result = (double) numBuckets / 10.0; log.debug("cacheKey:{} Between filter: {} " + "percentiles[{}] = {} to percentiles[{}] = {} " + - "buckets {} multiplier {}", cacheKey, betweenFilter, minBound, percentiles[minBound], - maxBound, percentiles[maxBound], numBuckets, result - ); + "buckets {} multiplier {}", cacheKey, betweenFilter, minBound, percentiles[minBound], + maxBound, percentiles[maxBound], numBuckets, result + ); return result; } @@ -529,8 +525,8 @@ public Double visit(EqualsFilter equalsFilter) { int numBuckets = maxBound - minBound + 1; final double result = (double) numBuckets / 10.0; log.debug("cacheKey:{} EqualsFilter:{} numMatches:{} multiplier:{}", cacheKey, equalsFilter, numMatches, - result - ); + result + ); return result; } @@ -555,8 +551,8 @@ public Double visit(GreaterThanFilter greaterThanFilter) { //Everything below this percentile do not affect final double result = (double) (10 - minBound - 1) / 10.0; log.debug("cacheKey:{} GreaterThanFilter: {} percentiles[{}] = {} multiplier: {}", cacheKey, - greaterThanFilter, minBound, percentiles[minBound], result - ); + greaterThanFilter, minBound, percentiles[minBound], result + ); return result; } @@ -574,8 +570,8 @@ public Double visit(GreaterEqualFilter greaterEqualFilter) { //Everything below this do not affect final double result = (double) (10 - minBound - 1) / 10.0; log.debug("cacheKey:{} GreaterEqualsFilter:{} percentiles[{}] = {} multiplier: {}", cacheKey, - greaterEqualFilter, minBound, percentiles[minBound], result - ); + greaterEqualFilter, minBound, percentiles[minBound], result + ); return result; } @@ -593,8 +589,8 @@ public Double visit(LessThanFilter lessThanFilter) { //Everything above this do not affect final double result = ((double) minBound + 1.0) / 10.0; log.debug("cacheKey:{} LessThanFilter:{} percentiles[{}] = {} multiplier: {}", cacheKey, lessThanFilter, - minBound, percentiles[minBound], result - ); + minBound, percentiles[minBound], result + ); return result; } @@ -611,8 +607,8 @@ public Double visit(LessEqualFilter lessEqualFilter) { //Everything above this do not affect final double result = ((double) minBound + 1.0) / 10.0; log.debug("cacheKey:{} LessEqualsFilter: {} percentiles[{}] = {} multiplier: {}", cacheKey, - lessEqualFilter, minBound, percentiles[minBound], result - ); + lessEqualFilter, minBound, percentiles[minBound], result + ); return result; } }; @@ -647,7 +643,7 @@ public Double visit(ContainsFilter stringContainsFilterElement) { public Double visit(InFilter inFilter) { // Assuming there are M matches, the probability is M/N return Utils.ensurePositive(inFilter.getValues() - .size()) / Utils.ensureOne( + .size()) / Utils.ensureOne( cardinalityEstimationData.getCardinality()); } @@ -729,35 +725,35 @@ private boolean isObjectInstanceOfString(List objects) { private Long getValidCount(Long count) { return count == null - ? 0 - : count; + ? 0 + : count; } private AbstractAggregationBuilder buildAggregation(GroupRequest groupRequest) { return Utils.buildTermsAggregation(getParameter().getNesting() - .stream() - .map(x -> new ResultSort(x, ResultSort.Order.asc)) - .collect(Collectors.toList()),buildSubAggregation(getParameter()), - elasticsearchTuningConfig.getAggregationSize()); + .stream() + .map(x -> new ResultSort(x, ResultSort.Order.asc)) + .collect(Collectors.toList()), buildSubAggregation(getParameter()), + elasticsearchTuningConfig.getAggregationSize()); } private Set buildSubAggregation(GroupRequest groupRequest) { // Keep this for backward compatibility to support uniqueCountOn attribute coming from old requests - if(!Strings.isNullOrEmpty(groupRequest.getUniqueCountOn())){ + if (!Strings.isNullOrEmpty(groupRequest.getUniqueCountOn())) { return Sets.newHashSet(buildCardinalityAggregation(groupRequest.getUniqueCountOn(), groupRequest)); } - if(Strings.isNullOrEmpty(groupRequest.getAggregationField())){ + if (Strings.isNullOrEmpty(groupRequest.getAggregationField())) { return Sets.newHashSet(); } boolean isNumericField = Utils.isNumericField(getTableMetadataManager(), groupRequest.getTable(), - groupRequest.getAggregationField()); + groupRequest.getAggregationField()); final AbstractAggregationBuilder groupAggStats; if (isNumericField) { groupAggStats = Utils.buildStatsAggregation(groupRequest.getAggregationField(), - Collections.singleton(groupRequest.getAggregationType())); + Collections.singleton(groupRequest.getAggregationType())); } else { groupAggStats = buildCardinalityAggregation(groupRequest.getAggregationField(), groupRequest); } @@ -767,15 +763,15 @@ private Set buildSubAggregation(GroupRequest groupRequest) { private CardinalityAggregationBuilder buildCardinalityAggregation(String aggregationField, GroupRequest groupRequest) { return Utils.buildCardinalityAggregation(aggregationField, - groupRequest.accept(new CountPrecisionThresholdVisitorAdapter( - elasticsearchTuningConfig.getPrecisionThreshold()))); + groupRequest.accept(new CountPrecisionThresholdVisitorAdapter( + elasticsearchTuningConfig.getPrecisionThreshold()))); } private Map getMap(List fields, Aggregations aggregations) { final String field = fields.get(0); final List remainingFields = (fields.size() > 1) - ? fields.subList(1, fields.size()) - : new ArrayList<>(); + ? fields.subList(1, fields.size()) + : new ArrayList<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); Map levelCount = Maps.newHashMap(); for (Terms.Bucket bucket : terms.getBuckets()) { @@ -785,17 +781,14 @@ private Map getMap(List fields, Aggregations aggregation Cardinality cardinality = bucket.getAggregations() .get(key); levelCount.put(String.valueOf(bucket.getKey()), cardinality.getValue()); - } - else if (!Strings.isNullOrEmpty(getParameter().getAggregationField())) { + } else if (!Strings.isNullOrEmpty(getParameter().getAggregationField())) { String metricKey = Utils.getExtendedStatsAggregationKey(getParameter().getAggregationField()); levelCount.put(String.valueOf(bucket.getKey()), Utils.toStats( bucket.getAggregations().get(metricKey)).get(statsString(getParameter().getAggregationType()))); - } - else { + } else { levelCount.put(String.valueOf(bucket.getKey()), bucket.getDocCount()); } - } - else { + } else { levelCount.put(String.valueOf(bucket.getKey()), getMap(remainingFields, bucket.getAggregations())); } } @@ -811,8 +804,8 @@ private void validateCardinality(GroupRequest parameter) { double probability = 0; try { TableFieldMapping fieldMappings = getTableMetadataManager().getFieldMappings(parameter.getTable(), - true, - false); + true, + false); if (null == fieldMappings) { fieldMappings = TableFieldMapping.builder() .mappings(Collections.emptySet()) @@ -821,19 +814,17 @@ private void validateCardinality(GroupRequest parameter) { } probability = estimateProbability(fieldMappings, parameter); - } - catch (Exception e) { + } catch (Exception e) { log.error("Error running estimation", e); } if (probability > PROBABILITY_CUT_OFF) { final String content = requestString(); log.warn("Blocked query as it might have screwed up the cluster. Probability: {} Query: {}", - probability, content); + probability, content); throw FoxtrotExceptions.createCardinalityOverflow( parameter, content, parameter.getNesting().get(0), probability); - } - else { + } else { log.info("Allowing group by with probability {} for query: {}", probability, parameter); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/HistogramAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/HistogramAction.java index 51d464f11..9ae34c693 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/HistogramAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/HistogramAction.java @@ -127,8 +127,7 @@ public ActionResponse execute(HistogramRequest parameter) { .getClient() .search(query); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } @@ -138,10 +137,10 @@ public SearchRequest getRequestBuilder(HistogramRequest parameter, List return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(QUERY_SIZE) - .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) - .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) - .aggregation(buildAggregation(parameter))); + .size(QUERY_SIZE) + .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) + .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) + .aggregation(buildAggregation(parameter))); } @Override @@ -166,9 +165,8 @@ private HistogramResponse buildResponse(Aggregations aggregations) { Cardinality cardinality = bucket.getAggregations() .get(key); counts.add(new HistogramResponse.Count(((DateTime) bucket.getKey()).getMillis(), - cardinality.getValue())); - } - else { + cardinality.getValue())); + } else { counts.add(new HistogramResponse.Count(((DateTime) bucket.getKey()).getMillis(), bucket.getDocCount())); } } @@ -178,7 +176,7 @@ private HistogramResponse buildResponse(Aggregations aggregations) { private AbstractAggregationBuilder buildAggregation(HistogramRequest parameter) { DateHistogramInterval interval = Utils.getHistogramInterval(getParameter().getPeriod()); DateHistogramAggregationBuilder histogramBuilder = Utils.buildDateHistogramAggregation(getParameter().getField(), - interval); + interval); if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) { histogramBuilder.subAggregation(Utils.buildCardinalityAggregation( getParameter().getUniqueCountOn(), parameter.accept(new CountPrecisionThresholdVisitorAdapter( diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryAction.java index 9fe768e44..fc0ef967e 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryAction.java @@ -70,8 +70,8 @@ public String getRequestCacheKey() { } } return String.format("multquery-%d-%s", - filterHashKey, - processForSubQueries(parameter, (action, request) -> action.getRequestCacheKey())); + filterHashKey, + processForSubQueries(parameter, (action, request) -> action.getRequestCacheKey())); } @Override @@ -86,7 +86,7 @@ public void validateImpl(MultiQueryRequest parameter) { } return null; }); - if(CollectionUtils.isNotEmpty(multiException.getErrors())) { + if (CollectionUtils.isNotEmpty(multiException.getErrors())) { throw multiException; } @@ -94,12 +94,12 @@ public void validateImpl(MultiQueryRequest parameter) { @Override public ActionResponse execute(MultiQueryRequest parameter) { - if(Utils.hasTemporalFilters(parameter.getFilters())) { + if (Utils.hasTemporalFilters(parameter.getFilters())) { val offendingRequests = parameter.getRequests().entrySet().stream() .filter(entry -> Utils.hasTemporalFilters(entry.getValue().getFilters())) .map(Map.Entry::getKey) .collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(offendingRequests)) { + if (CollectionUtils.isNotEmpty(offendingRequests)) { throw FoxtrotExceptions.createMalformedQueryException( parameter, Collections.singletonList( @@ -123,23 +123,23 @@ public MultiSearchRequest getRequestBuilder(MultiQueryRequest parameter, Listbuilder(); - if(null != parameter.getFilters()) { + if (null != parameter.getFilters()) { filterBuilder.addAll(parameter.getFilters()); } - if(null != extraFilters) { + if (null != extraFilters) { filterBuilder.addAll(extraFilters); } - val filters = filterBuilder.build(); + val filters = filterBuilder.build(); - for(Map.Entry entry : parameter.getRequests() + for (Map.Entry entry : parameter.getRequests() .entrySet()) { ActionRequest request = entry.getValue(); Action action = analyticsLoader.getAction(request); - if(null == action) { + if (null == action) { throw FoxtrotExceptions.queryCreationException(request, null); } org.elasticsearch.action.ActionRequest requestBuilder = action.getRequestBuilder(request, filters); - if(requestBuilder instanceof SearchRequest) { + if (requestBuilder instanceof SearchRequest) { multiSearchRequest.add((SearchRequest) requestBuilder); } } @@ -153,12 +153,12 @@ public ActionResponse getResponse(org.elasticsearch.action.ActionResponse multiS int queryCounter = 0; List queryKeys = Lists.newArrayList(); List requests = Lists.newArrayList(); - for(Map.Entry entry : getParameter().getRequests() + for (Map.Entry entry : getParameter().getRequests() .entrySet()) { queryKeys.add(entry.getKey()); requests.add(entry.getValue()); } - for(MultiSearchResponse.Item item : ((MultiSearchResponse)multiSearchResponse).getResponses()) { + for (MultiSearchResponse.Item item : ((MultiSearchResponse) multiSearchResponse).getResponses()) { Action action = null; ActionRequest request = requests.get(queryCounter); try { @@ -166,7 +166,7 @@ public ActionResponse getResponse(org.elasticsearch.action.ActionResponse multiS } catch (Exception e) { log.error("Error occurred while executing multiQuery request : {}", e); } - if(null == action) { + if (null == action) { throw FoxtrotExceptions.queryCreationException(request, null); } String key = queryKeys.get(queryCounter++); @@ -177,12 +177,12 @@ public ActionResponse getResponse(org.elasticsearch.action.ActionResponse multiS } private void createActions(final MultiQueryRequest multiQueryRequest) { - if(Utils.hasTemporalFilters(multiQueryRequest.getFilters())) { + if (Utils.hasTemporalFilters(multiQueryRequest.getFilters())) { val offendingRequests = multiQueryRequest.getRequests().entrySet().stream() .filter(entry -> Utils.hasTemporalFilters(entry.getValue().getFilters())) .map(Map.Entry::getKey) .collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(offendingRequests)) { + if (CollectionUtils.isNotEmpty(offendingRequests)) { throw FoxtrotExceptions.createMalformedQueryException( multiQueryRequest, Collections.singletonList( @@ -190,13 +190,12 @@ private void createActions(final MultiQueryRequest multiQueryRequest) { } } - for(Map.Entry entry : multiQueryRequest.getRequests().entrySet()) { + for (Map.Entry entry : multiQueryRequest.getRequests().entrySet()) { ActionRequest request = entry.getValue(); Action action; if (requestActionMap.get(request) != null) { action = requestActionMap.get(request); - } - else { + } else { action = analyticsLoader.getAction(request); requestActionMap.put(request, action); } @@ -210,13 +209,13 @@ private void createActions(final MultiQueryRequest multiQueryRequest) { private String processForSubQueries(MultiQueryRequest multiQueryRequest, ActionInterface actionInterface) { List results = Lists.newArrayList(); - for(Map.Entry entry : multiQueryRequest.getRequests().entrySet()) { - if(null == entry.getValue()) { + for (Map.Entry entry : multiQueryRequest.getRequests().entrySet()) { + if (null == entry.getValue()) { log.warn("Empty response for query: {}", entry.getKey()); continue; } String result = actionInterface.invoke(requestActionMap.get(entry.getValue()), entry.getValue()); - if(!Strings.isNullOrEmpty(result)) { + if (!Strings.isNullOrEmpty(result)) { results.add(result); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryAction.java index 8f13de83c..2cec34b49 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryAction.java @@ -35,15 +35,15 @@ public void preprocess() { MultiTimeQueryRequest multiTimeQueryRequest = getParameter(); int sampleSize; - if(multiTimeQueryRequest.getActionRequest() == null && CollectionUtils.isEmpty(multiTimeQueryRequest.getFilters())) { + if (multiTimeQueryRequest.getActionRequest() == null && CollectionUtils.isEmpty(multiTimeQueryRequest.getFilters())) { throw FoxtrotExceptions.createBadRequestException("multi_time_query", - "No Between Filter found in actionRequest multiQueryRequest : " + - multiQueryRequest.toString() - ); + "No Between Filter found in actionRequest multiQueryRequest : " + + multiQueryRequest.toString() + ); } - if(CollectionUtils.isEmpty(multiTimeQueryRequest.getActionRequest() - .getFilters())) { + if (CollectionUtils.isEmpty(multiTimeQueryRequest.getActionRequest() + .getFilters())) { multiTimeQueryRequest.getActionRequest() .setFilters(Lists.newArrayList()); } @@ -56,27 +56,27 @@ public void preprocess() { .stream() .filter(filter -> filter instanceof BetweenFilter) .findFirst(); - if(!optionalBetweenFilter.isPresent()) { + if (!optionalBetweenFilter.isPresent()) { throw FoxtrotExceptions.createBadRequestException("multi_time_query", - "No Between Filter found in actionRequest multiQueryRequest : " + - multiQueryRequest.toString() - ); + "No Between Filter found in actionRequest multiQueryRequest : " + + multiQueryRequest.toString() + ); } - BetweenFilter betweenFilter = (BetweenFilter)optionalBetweenFilter.get(); + BetweenFilter betweenFilter = (BetweenFilter) optionalBetweenFilter.get(); - if(multiTimeQueryRequest.getSampleSize() != 0) { + if (multiTimeQueryRequest.getSampleSize() != 0) { sampleSize = multiTimeQueryRequest.getSampleSize(); - } else if(multiTimeQueryRequest.getSkipDuration() - .toHours() > TimeUnit.DAYS.toHours(1)) { - sampleSize = (int)(30 / (multiTimeQueryRequest.getSkipDuration() + } else if (multiTimeQueryRequest.getSkipDuration() + .toHours() > TimeUnit.DAYS.toHours(1)) { + sampleSize = (int) (30 / (multiTimeQueryRequest.getSkipDuration() .toDays())); } else { - sampleSize = (int)(24 / (multiTimeQueryRequest.getSkipDuration() + sampleSize = (int) (24 / (multiTimeQueryRequest.getSkipDuration() .toHours())); } multiQueryRequest = createMultiQueryRequests(sampleSize, betweenFilter); action = analyticsLoader.getAction(multiQueryRequest); - if(null == action) { + if (null == action) { throw FoxtrotExceptions.queryCreationException(multiTimeQueryRequest, null); } action.preprocess(); @@ -95,13 +95,13 @@ public String getRequestCacheKey() { @Override public void validateImpl(MultiTimeQueryRequest parameter) { List validationErrors = new ArrayList<>(); - if(parameter.getActionRequest() == null) { + if (parameter.getActionRequest() == null) { validationErrors.add("action request cannot be null or empty"); } - if(parameter.getSkipDuration() == null) { + if (parameter.getSkipDuration() == null) { validationErrors.add("skip duration cannot be null or empty"); } - if(com.collections.CollectionUtils.isNotEmpty(validationErrors)) { + if (com.collections.CollectionUtils.isNotEmpty(validationErrors)) { throw FoxtrotExceptions.createMalformedQueryException(parameter, validationErrors); } action.validateImpl(multiQueryRequest); @@ -110,7 +110,7 @@ public void validateImpl(MultiTimeQueryRequest parameter) { @Override public ActionResponse execute(MultiTimeQueryRequest parameter) { MultiTimeQueryResponse multiTimeQueryResponse = new MultiTimeQueryResponse(); - multiTimeQueryResponse.setResponses(((MultiQueryResponse)action.execute(multiQueryRequest)).getResponses()); + multiTimeQueryResponse.setResponses(((MultiQueryResponse) action.execute(multiQueryRequest)).getResponses()); return multiTimeQueryResponse; } @@ -121,7 +121,7 @@ public org.elasticsearch.action.ActionRequest getRequestBuilder(MultiTimeQueryRe @Override public ActionResponse getResponse(org.elasticsearch.action.ActionResponse multiSearchResponse, MultiTimeQueryRequest parameter) { - MultiQueryResponse multiQueryResponse = (MultiQueryResponse)action.getResponse(multiSearchResponse, multiQueryRequest); + MultiQueryResponse multiQueryResponse = (MultiQueryResponse) action.getResponse(multiSearchResponse, multiQueryRequest); return new MultiTimeQueryResponse(multiQueryResponse.getResponses()); } @@ -132,14 +132,14 @@ private MultiQueryRequest createMultiQueryRequests(int sampleSize, BetweenFilter .longValue(); long to = betweenFilter.getTo() .longValue(); - for(int itr = 0; itr < sampleSize; itr++) { + for (int itr = 0; itr < sampleSize; itr++) { List filters = multiTimeQueryRequest.getActionRequest() .getFilters(); - for(int i = 0; i < filters.size(); i++) { - if(filters.get(i) instanceof BetweenFilter) { - BetweenFilter tempBetweenFilter = (BetweenFilter)filters.get(i); + for (int i = 0; i < filters.size(); i++) { + if (filters.get(i) instanceof BetweenFilter) { + BetweenFilter tempBetweenFilter = (BetweenFilter) filters.get(i); BetweenFilter tempBetweenFilter1 = new BetweenFilter(tempBetweenFilter.getField(), from, to, - tempBetweenFilter.isFilterTemporal() + tempBetweenFilter.isFilterTemporal() ); filters.set(i, tempBetweenFilter1); break; @@ -148,7 +148,7 @@ private MultiQueryRequest createMultiQueryRequests(int sampleSize, BetweenFilter multiTimeQueryRequest.getActionRequest() .setFilters(filters); try { - requests.put(Long.toString(from), (ActionRequest)multiTimeQueryRequest.getActionRequest() + requests.put(Long.toString(from), (ActionRequest) multiTimeQueryRequest.getActionRequest() .clone()); } catch (Exception e) { throw FoxtrotExceptions.queryCreationException(multiTimeQueryRequest.getActionRequest(), e); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsAction.java index f9bfafd1c..79156eff1 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsAction.java @@ -53,7 +53,7 @@ private static StatsValue buildStatsValue(String field, Aggregations aggregation // Build top level stats StatsValue statsValue = new StatsValue(); statsValue.setStats(Utils.toStats(aggregations.getAsMap() - .get(metricKey))); + .get(metricKey))); Percentiles internalPercentile = (Percentiles) aggregations.getAsMap() .get(percentileMetricKey); if (null != internalPercentile) { @@ -113,8 +113,7 @@ public ActionResponse execute(StatsRequest parameter) { .getClient() .search(query, RequestOptions.DEFAULT); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } @@ -132,13 +131,12 @@ public SearchRequest getRequestBuilder(StatsRequest parameter, List extr final AbstractAggregationBuilder extendedStats; if (isNumericField) { if (!AnalyticsRequestFlags.hasFlag(parameter.getFlags(), - AnalyticsRequestFlags.STATS_SKIP_PERCENTILES)) { + AnalyticsRequestFlags.STATS_SKIP_PERCENTILES)) { percentiles = Utils.buildPercentileAggregation(field, getParameter().getPercentiles()); sourceBuilder.aggregation(percentiles); } extendedStats = Utils.buildStatsAggregation(field, getParameter().getStats()); - } - else { + } else { extendedStats = Utils.buildStatsAggregation(field, Collections.singleton(Stat.COUNT)); } sourceBuilder.aggregation(extendedStats); @@ -150,11 +148,11 @@ public SearchRequest getRequestBuilder(StatsRequest parameter, List extr } sourceBuilder.aggregation( Utils.buildTermsAggregation(getParameter().getNesting() - .stream() - .map(x -> new ResultSort(x, ResultSort.Order.asc)) - .collect(Collectors.toList()), - subAggregations, - elasticsearchTuningConfig.getAggregationSize())); + .stream() + .map(x -> new ResultSort(x, ResultSort.Order.asc)) + .collect(Collectors.toList()), + subAggregations, + elasticsearchTuningConfig.getAggregationSize())); } return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .indicesOptions(Utils.indicesOptions()) @@ -190,8 +188,8 @@ private StatsResponse buildResponse(StatsRequest request, Aggregations aggregati private List> buildNestedStats(List nesting, Aggregations aggregations) { final String field = nesting.get(0); final List remainingFields = (nesting.size() > 1) - ? nesting.subList(1, nesting.size()) - : new ArrayList<>(); + ? nesting.subList(1, nesting.size()) + : new ArrayList<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); List> bucketResponses = Lists.newArrayList(); for (Terms.Bucket bucket : terms.getBuckets()) { @@ -199,8 +197,7 @@ private List> buildNestedStats(List nesting, bucketResponse.setKey(String.valueOf(bucket.getKey())); if (nesting.size() == 1) { bucketResponse.setResult(buildStatsValue(getParameter().getField(), bucket.getAggregations())); - } - else { + } else { bucketResponse.setBuckets(buildNestedStats(remainingFields, bucket.getAggregations())); } bucketResponses.add(bucketResponse); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendAction.java index 9b3a85bf1..2331dd8ff 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendAction.java @@ -86,15 +86,15 @@ public String getRequestCacheKey() { hashKey += 31 * statsRequest.getTimestamp() .hashCode(); hashKey += 31 * (statsRequest.getField() != null - ? statsRequest.getField() - .hashCode() - : "FIELD".hashCode()); + ? statsRequest.getField() + .hashCode() + : "FIELD".hashCode()); return String.format("stats-trend-%s-%s-%s-%d", - statsRequest.getTable(), - statsRequest.getField(), - statsRequest.getPeriod(), - hashKey - ); + statsRequest.getTable(), + statsRequest.getField(), + statsRequest.getPeriod(), + hashKey + ); } @Override @@ -125,8 +125,7 @@ public ActionResponse execute(StatsTrendRequest parameter) { .getClient() .search(query); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } @@ -136,10 +135,10 @@ public SearchRequest getRequestBuilder(StatsTrendRequest parameter, List return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(0) - .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) - .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) - .aggregation(buildAggregation(parameter, parameter.getTable()))); + .size(0) + .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) + .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) + .aggregation(buildAggregation(parameter, parameter.getTable()))); } @@ -156,7 +155,7 @@ private AbstractAggregationBuilder buildAggregation(StatsTrendRequest request, S final String field = request.getField(); DateHistogramInterval interval = Utils.getHistogramInterval(request.getPeriod()); AbstractAggregationBuilder dateHistogramBuilder = Utils.buildDateHistogramAggregation(request.getTimestamp(), - interval); + interval); boolean isNumericField = Utils.isNumericField(getTableMetadataManager(), table, field); if (isNumericField) { dateHistogramBuilder @@ -165,8 +164,7 @@ private AbstractAggregationBuilder buildAggregation(StatsTrendRequest request, S dateHistogramBuilder.subAggregation(Utils.buildPercentileAggregation( field, request.getPercentiles(), request.getCompression())); } - } - else { + } else { dateHistogramBuilder .subAggregation(Utils.buildStatsAggregation(field, Collections.singleton(Stat.COUNT))); } @@ -175,10 +173,10 @@ private AbstractAggregationBuilder buildAggregation(StatsTrendRequest request, S return dateHistogramBuilder; } return Utils.buildTermsAggregation(getParameter().getNesting() - .stream() - .map(x -> new ResultSort(x, ResultSort.Order.asc)) - .collect(Collectors.toList()), Sets.newHashSet(dateHistogramBuilder), - elasticsearchTuningConfig.getAggregationSize()); + .stream() + .map(x -> new ResultSort(x, ResultSort.Order.asc)) + .collect(Collectors.toList()), Sets.newHashSet(dateHistogramBuilder), + elasticsearchTuningConfig.getAggregationSize()); } private StatsTrendResponse buildResponse(StatsTrendRequest request, Aggregations aggregations) { @@ -187,10 +185,9 @@ private StatsTrendResponse buildResponse(StatsTrendRequest request, Aggregations if (CollectionUtils.isNullOrEmpty(request.getNesting())) { List trends = buildStatsTrendValue(request.getField(), aggregations); response.setResult(trends); - } - else { + } else { List>> buckets = buildNestedTrendStats(request.getNesting(), - aggregations); + aggregations); response.setBuckets(buckets); } return response; @@ -201,8 +198,8 @@ private List>> buildNestedTrendStats( Aggregations aggregations) { final String field = nesting.get(0); final List remainingFields = (nesting.size() > 1) - ? nesting.subList(1, nesting.size()) - : new ArrayList<>(); + ? nesting.subList(1, nesting.size()) + : new ArrayList<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); List>> bucketResponses = Lists.newArrayList(); for (Terms.Bucket bucket : terms.getBuckets()) { @@ -210,8 +207,7 @@ private List>> buildNestedTrendStats( bucketResponse.setKey(String.valueOf(bucket.getKey())); if (nesting.size() == 1) { bucketResponse.setResult(buildStatsTrendValue(getParameter().getField(), bucket.getAggregations())); - } - else { + } else { bucketResponse.setBuckets(buildNestedTrendStats(remainingFields, bucket.getAggregations())); } bucketResponses.add(bucketResponse); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/TrendAction.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/TrendAction.java index dc620b464..46994b2db 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/TrendAction.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/TrendAction.java @@ -76,9 +76,9 @@ public TrendAction(TrendRequest parameter, AnalyticsLoader analyticsLoader) { @Override public void preprocess() { getParameter().setTable(ElasticsearchUtils.getValidTableName(getParameter().getTable())); - if(null != getParameter().getValues() && !getParameter().getValues() + if (null != getParameter().getValues() && !getParameter().getValues() .isEmpty()) { - List values = (List)getParameter().getValues(); + List values = (List) getParameter().getValues(); Filter filter = new InFilter(getParameter().getField(), values); getParameter().getFilters() .add(filter); @@ -94,18 +94,18 @@ public String getMetricKey() { public String getRequestCacheKey() { TrendRequest query = getParameter(); long filterHashKey = 0L; - if(query.getFilters() != null) { - for(Filter filter : query.getFilters()) { + if (query.getFilters() != null) { + for (Filter filter : query.getFilters()) { filterHashKey += 31 * filter.hashCode(); } } - if(query.getValues() != null) { - for(String value : query.getValues()) { + if (query.getValues() != null) { + for (String value : query.getValues()) { filterHashKey += 31 * value.hashCode(); } } - if(null != query.getUniqueCountOn()) { + if (null != query.getUniqueCountOn()) { filterHashKey += 31 * query.getUniqueCountOn() .hashCode(); } @@ -125,25 +125,25 @@ public String getRequestCacheKey() { @Override public void validateImpl(TrendRequest parameter) { List validationErrors = Lists.newArrayList(); - if(CollectionUtils.isNullOrEmpty(parameter.getTable())) { + if (CollectionUtils.isNullOrEmpty(parameter.getTable())) { validationErrors.add("table name cannot be null or empty"); } - if(CollectionUtils.isNullOrEmpty(parameter.getField())) { + if (CollectionUtils.isNullOrEmpty(parameter.getField())) { validationErrors.add("field name cannot be null or empty"); } - if(CollectionUtils.isNullOrEmpty(parameter.getTimestamp())) { + if (CollectionUtils.isNullOrEmpty(parameter.getTimestamp())) { validationErrors.add("timestamp field cannot be null or empty"); } - if(parameter.getPeriod() == null) { + if (parameter.getPeriod() == null) { validationErrors.add(String.format("specify time period (%s)", StringUtils.join(Period.values()))); } - if(parameter.getUniqueCountOn() != null && parameter.getUniqueCountOn() + if (parameter.getUniqueCountOn() != null && parameter.getUniqueCountOn() .isEmpty()) { validationErrors.add("unique field cannot be empty (can be null)"); } - if(!CollectionUtils.isNullOrEmpty(validationErrors)) { + if (!CollectionUtils.isNullOrEmpty(validationErrors)) { throw FoxtrotExceptions.createMalformedQueryException(parameter, validationErrors); } } @@ -156,8 +156,7 @@ public ActionResponse execute(TrendRequest parameter) { .getClient() .search(query); return getResponse(response, parameter); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } @@ -167,17 +166,17 @@ public SearchRequest getRequestBuilder(TrendRequest parameter, List extr return new SearchRequest(ElasticsearchUtils.getIndices(parameter.getTable(), parameter)) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(QUERY_SIZE) - .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) - .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) - .aggregation(buildAggregation(parameter))); + .size(QUERY_SIZE) + .timeout(new TimeValue(getGetQueryTimeout(), TimeUnit.MILLISECONDS)) + .query(ElasticsearchQueryUtils.translateFilter(parameter, extraFilters)) + .aggregation(buildAggregation(parameter))); } @Override public ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, TrendRequest parameter) { - Aggregations aggregations = ((SearchResponse)response).getAggregations(); - if(aggregations != null) { + Aggregations aggregations = ((SearchResponse) response).getAggregations(); + if (aggregations != null) { return buildResponse(parameter, aggregations); } else { return new TrendResponse(Collections.>emptyMap()); @@ -198,7 +197,7 @@ private AbstractAggregationBuilder buildAggregation(TrendRequest request) { String field = request.getField(); DateHistogramAggregationBuilder histogramBuilder = Utils.buildDateHistogramAggregation(request.getTimestamp(), interval); - if(!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) { + if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) { histogramBuilder.subAggregation(Utils.buildCardinalityAggregation( getParameter().getUniqueCountOn(), request.accept(new CountPrecisionThresholdVisitorAdapter( elasticsearchTuningConfig.getPrecisionThreshold())))); @@ -211,19 +210,19 @@ private TrendResponse buildResponse(TrendRequest request, Aggregations aggregati String field = request.getField(); Map> trendCounts = new TreeMap<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); - for(Terms.Bucket bucket : terms.getBuckets()) { + for (Terms.Bucket bucket : terms.getBuckets()) { final String key = String.valueOf(bucket.getKey()); List counts = Lists.newArrayList(); Aggregations subAggregations = bucket.getAggregations(); Histogram histogram = subAggregations.get(Utils.getDateHistogramKey(request.getTimestamp())); - for(Histogram.Bucket histogramBucket : histogram.getBuckets()) { - if(!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) { + for (Histogram.Bucket histogramBucket : histogram.getBuckets()) { + if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) { String uniqueCountKey = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn()); Cardinality cardinality = histogramBucket.getAggregations() .get(uniqueCountKey); - counts.add(new TrendResponse.Count(((DateTime)histogramBucket.getKey()).getMillis(), cardinality.getValue())); + counts.add(new TrendResponse.Count(((DateTime) histogramBucket.getKey()).getMillis(), cardinality.getValue())); } else { - counts.add(new TrendResponse.Count(((DateTime)histogramBucket.getKey()).getMillis(), histogramBucket.getDocCount())); + counts.add(new TrendResponse.Count(((DateTime) histogramBucket.getKey()).getMillis(), histogramBucket.getDocCount())); } } trendCounts.put(key, counts); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Utils.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Utils.java index b62b686d5..819126b9b 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Utils.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/Utils.java @@ -42,9 +42,6 @@ */ public class Utils { - private static final double[] DEFAULT_PERCENTILES = {1d, 5d, 25, 50d, 75d, 95d, 99d}; - private static final double DEFAULT_COMPRESSION = 100.0; - private static final int PRECISION_THRESHOLD = 500; public static final String COUNT = "count"; public static final String AVG = "avg"; public static final String SUM = "sum"; @@ -53,6 +50,9 @@ public class Utils { public static final String SUM_OF_SQUARES = "sum_of_squares"; public static final String VARIANCE = "variance"; public static final String STD_DEVIATION = "std_deviation"; + private static final double[] DEFAULT_PERCENTILES = {1d, 5d, 25, 50d, 75d, 95d, 99d}; + private static final double DEFAULT_COMPRESSION = 100.0; + private static final int PRECISION_THRESHOLD = 500; private static final EnumSet NUMERIC_FIELD_TYPES = EnumSet.of(FieldType.INTEGER, FieldType.LONG, FieldType.FLOAT, FieldType.DOUBLE); @@ -71,8 +71,7 @@ public static TermsAggregationBuilder buildTermsAggregation( termsBuilder = AggregationBuilders.terms(Utils.sanitizeFieldForAggregation(field)) .field(storedFieldName(field)) .order(bucketOrder); - } - else { + } else { TermsAggregationBuilder tempBuilder = AggregationBuilders.terms(Utils.sanitizeFieldForAggregation(field)) .field(storedFieldName(field)) .order(bucketOrder); @@ -171,10 +170,10 @@ public static AbstractAggregationBuilder buildPercentileAggregation( String field, Collection inputPercentiles, double compression) { double[] percentiles = inputPercentiles != null - ? inputPercentiles.stream() - .mapToDouble(x -> x) - .toArray() - : DEFAULT_PERCENTILES; + ? inputPercentiles.stream() + .mapToDouble(x -> x) + .toArray() + : DEFAULT_PERCENTILES; if (compression == 0.0) { compression = DEFAULT_COMPRESSION; } @@ -308,37 +307,31 @@ public static Map createPercentilesResponse(Percentiles internal public static double ensurePositive(long number) { return number <= 0.0 - ? 0.0 - : number; + ? 0.0 + : number; } public static double ensureOne(long number) { return number <= 0 - ? 1 - : number; + ? 1 + : number; } public static Map toStats(Aggregation statAggregation) { if (statAggregation instanceof ParsedExtendedStats) { return Utils.createStatsResponse((ParsedExtendedStats) statAggregation); - } - else if (statAggregation instanceof ParsedStats) { + } else if (statAggregation instanceof ParsedStats) { return Utils.createStatsResponse((ParsedStats) statAggregation); - } - else if (statAggregation instanceof ParsedMax) { + } else if (statAggregation instanceof ParsedMax) { return Utils.createStatResponse((ParsedMax) statAggregation); - } - else if (statAggregation instanceof ParsedMin) { + } else if (statAggregation instanceof ParsedMin) { return Utils.createStatResponse((ParsedMin) statAggregation); - } - else if (statAggregation instanceof ParsedAvg) { + } else if (statAggregation instanceof ParsedAvg) { return Utils.createStatResponse((ParsedAvg) statAggregation); - } - else if (statAggregation instanceof ParsedSum) { + } else if (statAggregation instanceof ParsedSum) { return Utils.createStatResponse((ParsedSum) statAggregation); - } - else if (statAggregation instanceof ParsedValueCount) { + } else if (statAggregation instanceof ParsedValueCount) { return Utils.createStatResponse((ParsedValueCount) statAggregation); } return new HashMap<>(); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/spi/AnalyticsLoader.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/spi/AnalyticsLoader.java index 6df495832..4756b6311 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/spi/AnalyticsLoader.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/actions/spi/AnalyticsLoader.java @@ -95,8 +95,7 @@ public Action getAction(R request) { Constructor constructor = metadata.getAction() .getConstructor(metadata.getRequest(), AnalyticsLoader.class); return constructor.newInstance(r, this); - } - catch (Exception e) { + } catch (Exception e) { throw FoxtrotExceptions.createActionResolutionException(request, e); } } @@ -130,7 +129,7 @@ public void start() throws Exception { throw new AnalyticsActionLoaderException("Invalid annotation on " + action.getCanonicalName()); } register(new ActionMetadata(analyticsProvider.request(), action, analyticsProvider.cacheable()), - analyticsProvider.opcode()); + analyticsProvider.opcode()); types.add(new NamedType(analyticsProvider.request(), opcode)); types.add(new NamedType(analyticsProvider.response(), opcode)); logger.info("Registered action: {}", action.getCanonicalName()); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/MetricRecorder.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/MetricRecorder.java index f5f87dadd..b92752345 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/MetricRecorder.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/MetricRecorder.java @@ -11,19 +11,18 @@ public class MetricRecorder implements ActionExecutionObserver { @Override public void postExecution(ActionEvaluationResponse response) { - if(null == response || null == response.getExecutedAction()) { + if (null == response || null == response.getExecutedAction()) { return; } final ActionRequest request = response.getRequest(); final String metricKey = response.getExecutedAction().getMetricKey(); - if(null == response.getException()) { + if (null == response.getException()) { MetricUtil.getInstance() .registerActionFailure(request.getOpcode(), metricKey, response.getElapsedTime()); } - if(response.isCached()) { + if (response.isCached()) { MetricUtil.getInstance().registerActionCacheHit(request.getOpcode(), metricKey); - } - else { + } else { MetricUtil.getInstance().registerActionCacheMiss(request.getOpcode(), metricKey); } MetricUtil.getInstance() diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/ResponseCacheUpdater.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/ResponseCacheUpdater.java index 47b6b5f3c..8aedac5a6 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/ResponseCacheUpdater.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/ResponseCacheUpdater.java @@ -18,11 +18,11 @@ public ResponseCacheUpdater(CacheManager cacheManager) { @Override public void postExecution(ActionEvaluationResponse response) { - if(null == response || null == response.getResponse() || null == response.getExecutedAction()) { + if (null == response || null == response.getResponse() || null == response.getExecutedAction()) { return; } final Cache cache = cacheManager.getCacheFor(response.getRequest().getOpcode()); - if(null == cache || response.getRequest().isBypassCache()) { + if (null == cache || response.getRequest().isBypassCache()) { return; } final String cacheKey = response.getExecutedAction().cacheKey(); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/SlowQueryReporter.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/SlowQueryReporter.java index 850b715fa..10d964eda 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/SlowQueryReporter.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/handlers/SlowQueryReporter.java @@ -12,15 +12,14 @@ public class SlowQueryReporter implements ActionExecutionObserver { @Override public void postExecution(ActionEvaluationResponse response) { - if(null == response || null != response.getException() || response.isCached()) { + if (null == response || null != response.getException() || response.isCached()) { return; } if (response.getElapsedTime() > 1000) { try { String query = response.getExecutedAction().getObjectMapper().writeValueAsString(response.getRequest()); log.warn("SLOW_QUERY: Time: {} ms Query: {}", response.getElapsedTime(), query); - } - catch (JsonProcessingException e) { + } catch (JsonProcessingException e) { log.error("Error serializing slow query", e); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConfig.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConfig.java index a408e0dd4..fdfb90e55 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConfig.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConfig.java @@ -30,25 +30,11 @@ */ @NoArgsConstructor public class ElasticsearchConfig { - public ConnectionType getConnectionType() { - return connectionType; - } - - public void setConnectionType(ConnectionType connectionType) { - this.connectionType = connectionType; - } - - public enum ConnectionType { - HTTP, - HTTPS - } - public static final long DEFAULT_TIMEOUT = 10000L; @Valid @NotNull @JsonProperty private List hosts; - @Valid @NotNull @JsonProperty @@ -60,6 +46,14 @@ public enum ConnectionType { @Builder.Default private ConnectionType connectionType = ConnectionType.HTTP; + public ConnectionType getConnectionType() { + return connectionType; + } + + public void setConnectionType(ConnectionType connectionType) { + this.connectionType = connectionType; + } + public List getHosts() { return hosts; } @@ -96,4 +90,9 @@ public long getGetQueryTimeout() { return getQueryTimeout > 0 ? getQueryTimeout : DEFAULT_TIMEOUT; } + public enum ConnectionType { + HTTP, + HTTPS + } + } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConnection.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConnection.java index 27d46c102..b0b46287a 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConnection.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchConnection.java @@ -55,22 +55,22 @@ public void start() throws Exception { final int defaultPort = config.getConnectionType() == ElasticsearchConfig.ConnectionType.HTTP ? 80 : 443; int port = config.getPort() == 0 ? defaultPort : config.getPort(); val hosts = config.getHosts() - .stream() - .map(host -> { - final String scheme = config.getConnectionType() == ElasticsearchConfig.ConnectionType.HTTP - ? "http" - : "https"; - return new HttpHost(host, port, scheme); - }) - .toArray(HttpHost[]::new); - client = new RestHighLevelClient(RestClient.builder(hosts)); + .stream() + .map(host -> { + final String scheme = config.getConnectionType() == ElasticsearchConfig.ConnectionType.HTTP + ? "http" + : "https"; + return new HttpHost(host, port, scheme); + }) + .toArray(HttpHost[]::new); + client = new RestHighLevelClient(RestClient.builder(hosts)); logger.info("Started ElasticSearch Client"); } @Override public void stop() throws Exception { logger.info("Stopping ElasticSearch client"); - if(client != null) { + if (client != null) { client.close(); } client = null; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStore.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStore.java index 447f99972..fe9c3192c 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStore.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStore.java @@ -116,12 +116,12 @@ public void save(String table, Document document) { Stopwatch stopwatch = Stopwatch.createStarted(); String action = StringUtils.EMPTY; try { - if(!tableMetadataManager.exists(table)) { + if (!tableMetadataManager.exists(table)) { throw FoxtrotExceptions.createBadRequestException(table, String.format(UNKNOWN_TABLE_ERROR_MESSAGE, table)); } - if(new DateTime().plusDays(1) - .minus(document.getTimestamp()) - .getMillis() < 0) { + if (new DateTime().plusDays(1) + .minus(document.getTimestamp()) + .getMillis() < 0) { return; } action = TABLE_META; @@ -174,10 +174,10 @@ public void save(String table, List documents) { Stopwatch stopwatch = Stopwatch.createStarted(); String action = StringUtils.EMPTY; try { - if(!tableMetadataManager.exists(table)) { + if (!tableMetadataManager.exists(table)) { throw FoxtrotExceptions.createBadRequestException(table, String.format(UNKNOWN_TABLE_ERROR_MESSAGE, table)); } - if(documents == null || documents.isEmpty()) { + if (documents == null || documents.isEmpty()) { throw FoxtrotExceptions.createBadRequestException(table, "Empty Document List Not Allowed"); } action = TABLE_META; @@ -201,10 +201,10 @@ public void save(String table, List documents) { action = QUERY_STORE; BulkRequest bulkRequest = new BulkRequest(); DateTime dateTime = new DateTime().plusDays(1); - for(Document document : translatedDocuments) { + for (Document document : translatedDocuments) { long timestamp = document.getTimestamp(); - if(dateTime.minus(timestamp) - .getMillis() < 0) { + if (dateTime.minus(timestamp) + .getMillis() < 0) { continue; } final String index = ElasticsearchUtils.getCurrentIndex(table, timestamp); @@ -214,7 +214,7 @@ public void save(String table, List documents) { .source(convert(table, document)); bulkRequest.add(indexRequest); } - if(bulkRequest.numberOfActions() > 0) { + if (bulkRequest.numberOfActions() > 0) { bulkRequest .timeout(new TimeValue(10, TimeUnit.SECONDS)); BulkResponse responses = getConnection() @@ -223,9 +223,9 @@ public void save(String table, List documents) { logger.info("QueryStoreTook:{}", stopwatch.elapsed(TimeUnit.MILLISECONDS)); MetricUtil.getInstance() .registerActionSuccess(action, table, stopwatch.elapsed(TimeUnit.MILLISECONDS)); - for(int i = 0; i < responses.getItems().length; i++) { + for (int i = 0; i < responses.getItems().length; i++) { BulkItemResponse itemResponse = responses.getItems()[i]; - if(itemResponse.isFailed()) { + if (itemResponse.isFailed()) { String failedDocument = mapper.writeValueAsString(documents.get(i)); logger.error("Table : {} Failure Message : {} Document : {}", table, itemResponse.getFailureMessage(), failedDocument @@ -251,7 +251,7 @@ public void save(String table, List documents) { public Document get(String table, String id) { table = ElasticsearchUtils.getValidTableName(table); Table fxTable; - if(!tableMetadataManager.exists(table)) { + if (!tableMetadataManager.exists(table)) { throw FoxtrotExceptions.createBadRequestException(table, String.format(UNKNOWN_TABLE_ERROR_MESSAGE, table)); } fxTable = tableMetadataManager.get(table); @@ -260,19 +260,18 @@ public Document get(String table, String id) { .types(ElasticsearchUtils.DOCUMENT_TYPE_NAME) .source(new SearchSourceBuilder() .query(boolQuery().filter(termQuery(ElasticsearchUtils.DOCUMENT_META_ID_FIELD_NAME, id))) - .fetchSource(false) - .size(1)); + .fetchSource(false) + .size(1)); SearchResponse searchResponse = null; try { searchResponse = getConnection() .getClient() .search(searchRequest); - } - catch (IOException e) { + } catch (IOException e) { throw FoxtrotExceptions.createExecutionException(table, e); } - if(searchResponse.getHits() - .getTotalHits() == 0) { + if (searchResponse.getHits() + .getTotalHits() == 0) { logger.warn("Going into compatibility mode, looks using passed in ID as the data store id: {}", id); lookupKey = id; } else { @@ -292,30 +291,29 @@ public List getAll(String table, List ids) { @Timed public List getAll(String table, List ids, boolean bypassMetalookup) { table = ElasticsearchUtils.getValidTableName(table); - if(!tableMetadataManager.exists(table)) { + if (!tableMetadataManager.exists(table)) { throw FoxtrotExceptions.createBadRequestException(table, String.format(UNKNOWN_TABLE_ERROR_MESSAGE, table)); } Map rowKeys = Maps.newLinkedHashMap(); - for(String id : ids) { + for (String id : ids) { rowKeys.put(id, id); } - if(!bypassMetalookup) { + if (!bypassMetalookup) { SearchResponse response = null; try { response = connection.getClient() .search(new SearchRequest(ElasticsearchUtils.getIndices(table)) - .types(ElasticsearchUtils.DOCUMENT_TYPE_NAME) - .source(new SearchSourceBuilder() - .query(boolQuery().filter(termsQuery( - ElasticsearchUtils.DOCUMENT_META_ID_FIELD_NAME, ids.toArray(new String[ids.size()])))) - .fetchSource(false) - .storedField(ElasticsearchUtils.DOCUMENT_META_ID_FIELD_NAME) // Used for compatibility - .size(ids.size())), RequestOptions.DEFAULT); - } - catch (IOException e) { + .types(ElasticsearchUtils.DOCUMENT_TYPE_NAME) + .source(new SearchSourceBuilder() + .query(boolQuery().filter(termsQuery( + ElasticsearchUtils.DOCUMENT_META_ID_FIELD_NAME, ids.toArray(new String[ids.size()])))) + .fetchSource(false) + .storedField(ElasticsearchUtils.DOCUMENT_META_ID_FIELD_NAME) // Used for compatibility + .size(ids.size())), RequestOptions.DEFAULT); + } catch (IOException e) { throw new RuntimeException(e); //TODO } - for(SearchHit hit : response.getHits()) { + for (SearchHit hit : response.getHits()) { final String id = hit.getFields() .get(ElasticsearchUtils.DOCUMENT_META_ID_FIELD_NAME) .getValue() @@ -371,12 +369,11 @@ public ClusterHealthResponse getClusterHealth() { public JsonNode getNodeStats() { try { return mapper.readTree(new InputStreamReader(connection.getClient() - .getLowLevelClient() - .performRequest(new Request("GET", "/_nodes/stats")) - .getEntity() - .getContent())); - } - catch (IOException e) { + .getLowLevelClient() + .performRequest(new Request("GET", "/_nodes/stats")) + .getEntity() + .getContent())); + } catch (IOException e) { logger.error("Error finding node stats", e); } return mapper.createObjectNode(); @@ -386,12 +383,11 @@ public JsonNode getNodeStats() { public JsonNode getIndicesStats() { try { return mapper.readTree(new InputStreamReader(connection.getClient() - .getLowLevelClient() - .performRequest(new Request("GET", "/_stats")) - .getEntity() - .getContent())); - } - catch (IOException e) { + .getLowLevelClient() + .performRequest(new Request("GET", "/_stats")) + .getEntity() + .getContent())); + } catch (IOException e) { logger.error("Error finding node stats", e); } return mapper.createObjectNode(); @@ -408,15 +404,15 @@ private Map convert(String table, Document document) { .deepCopy(); dataNode.set(ElasticsearchUtils.DOCUMENT_META_FIELD_NAME, metaNode); dataNode.set(ElasticsearchUtils.DOCUMENT_TIME_FIELD_NAME, mapper.valueToTree(document.getDate())); - mutators.forEach(mutator-> mutator.mutate(table, document.getId(), dataNode)); + mutators.forEach(mutator -> mutator.mutate(table, document.getId(), dataNode)); return ElasticsearchQueryUtils.toMap(mapper, dataNode); } private void deleteIndices(List indicesToDelete) { logger.warn("Deleting Indexes - Indexes - {}", indicesToDelete); - if(!indicesToDelete.isEmpty()) { + if (!indicesToDelete.isEmpty()) { List> subLists = Lists.partition(indicesToDelete, 5); - for(List subList : subLists) { + for (List subList : subLists) { try { connection.getClient() .indices() @@ -431,13 +427,13 @@ private void deleteIndices(List indicesToDelete) { private List getIndicesToDelete(Set tables, Collection currentIndices) { List indicesToDelete = new ArrayList<>(); - for(String currentIndex : currentIndices) { + for (String currentIndex : currentIndices) { String table = ElasticsearchUtils.getTableNameFromIndex(currentIndex); - if(table != null && tables.contains(table)) { + if (table != null && tables.contains(table)) { boolean indexEligibleForDeletion; try { indexEligibleForDeletion = ElasticsearchUtils.isIndexEligibleForDeletion(currentIndex, tableMetadataManager.get(table)); - if(indexEligibleForDeletion) { + if (indexEligibleForDeletion) { logger.warn("Index eligible for deletion : {}", currentIndex); indicesToDelete.add(currentIndex); } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtils.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtils.java index 9ccb76303..3936fee92 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtils.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtils.java @@ -75,8 +75,7 @@ public static String getTableNamePrefix() { public static void setTableNamePrefix(ElasticsearchConfig config) { if (StringUtils.isNotEmpty(config.getTableNamePrefix())) { tableNamePrefix = config.getTableNamePrefix(); - } - else { + } else { tableNamePrefix = "foxtrot"; } } @@ -122,10 +121,10 @@ public static String getCurrentIndex(final String table, long timestamp) { //TODO::THROW IF TIMESTAMP IS BEYOND TABLE META.TTL String datePostfix = FORMATTER.print(timestamp); return String.format("%s-%s-%s-%s", - getTableNamePrefix(), - table, - ElasticsearchUtils.TABLENAME_POSTFIX, - datePostfix); + getTableNamePrefix(), + table, + ElasticsearchUtils.TABLENAME_POSTFIX, + datePostfix); } public static PutIndexTemplateRequest getClusterTemplateMapping() { @@ -133,8 +132,7 @@ public static PutIndexTemplateRequest getClusterTemplateMapping() { return new PutIndexTemplateRequest().name("template_foxtrot_mappings") .patterns(Lists.newArrayList(String.format("%s-*", getTableNamePrefix()))) .mapping(DOCUMENT_TYPE_NAME, getDocumentMapping()); - } - catch (IOException ex) { + } catch (IOException ex) { logger.error("TEMPLATE_CREATION_FAILED", ex); return null; } @@ -255,8 +253,7 @@ public static void initializeMappings(RestHighLevelClient client) { try { client.indices() .putTemplate(templateRequest, RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { logger.error("Error occurred: ", e); } } @@ -299,8 +296,7 @@ public static String getTableNameFromIndex(String currentIndex) { String tempIndex = currentIndex.substring(currentIndex.indexOf(getTableNamePrefix()) + getTableNamePrefix().length() + 1); int position = tempIndex.lastIndexOf(String.format("-%s", TABLENAME_POSTFIX)); return tempIndex.substring(0, position); - } - else { + } else { return null; } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/HazelcastConnection.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/HazelcastConnection.java index 444424b62..2d2a58834 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/HazelcastConnection.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/impl/HazelcastConnection.java @@ -40,10 +40,8 @@ @Order(10) @Singleton public class HazelcastConnection implements Managed { - private static final Logger logger = LoggerFactory.getLogger(HazelcastConnection.class.getSimpleName()); - public static final String HEALTHCHECK_MAP = "healthCheck"; - + private static final Logger logger = LoggerFactory.getLogger(HazelcastConnection.class.getSimpleName()); private HazelcastInstance hazelcast; private Config hazelcastConfig; @@ -116,34 +114,34 @@ public HazelcastConnection(ClusterConfig clusterConfig) throws UnknownHostExcept .setEnabled(false); AwsConfig awsConfig = new AwsConfig(); - if(!Strings.isNullOrEmpty(ec2Config.getServiceName())) { + if (!Strings.isNullOrEmpty(ec2Config.getServiceName())) { awsConfig.setProperty("service-name", ec2Config.getServiceName()); } - if(!Strings.isNullOrEmpty(ec2Config.getAccessKey())) { + if (!Strings.isNullOrEmpty(ec2Config.getAccessKey())) { awsConfig.setProperty("access-key", ec2Config.getAccessKey()); } - if(!Strings.isNullOrEmpty(ec2Config.getSecretKey())) { + if (!Strings.isNullOrEmpty(ec2Config.getSecretKey())) { awsConfig.setProperty("secret-key", ec2Config.getSecretKey()); } - if(!Strings.isNullOrEmpty(ec2Config.getIamRole())) { + if (!Strings.isNullOrEmpty(ec2Config.getIamRole())) { awsConfig.setProperty("iam-role", ec2Config.getIamRole()); } - if(!Strings.isNullOrEmpty(ec2Config.getRegion())) { + if (!Strings.isNullOrEmpty(ec2Config.getRegion())) { awsConfig.setProperty("region", ec2Config.getRegion()); } - if(!Strings.isNullOrEmpty(ec2Config.getHostHeader())) { + if (!Strings.isNullOrEmpty(ec2Config.getHostHeader())) { awsConfig.setProperty("host-header", ec2Config.getHostHeader()); } - if(!Strings.isNullOrEmpty(ec2Config.getSecurityGroupName())) { + if (!Strings.isNullOrEmpty(ec2Config.getSecurityGroupName())) { awsConfig.setProperty("security-group-name", ec2Config.getSecurityGroupName()); } - if(ec2Config.getOpTimeoutSeconds() > 0) { + if (ec2Config.getOpTimeoutSeconds() > 0) { awsConfig.setProperty("connection-timeout-seconds", - Integer.toString(ec2Config.getOpTimeoutSeconds())); + Integer.toString(ec2Config.getOpTimeoutSeconds())); awsConfig.setProperty("read-timeout-seconds", - Integer.toString(ec2Config.getOpTimeoutSeconds())); + Integer.toString(ec2Config.getOpTimeoutSeconds())); } - if(ec2Config.isExternalClient()) { + if (ec2Config.isExternalClient()) { awsConfig.setProperty("use-public-ip", Boolean.TRUE.toString()); } hazelcastConfigNetworkConfigJoin.setAwsConfig(awsConfig); @@ -165,34 +163,34 @@ public HazelcastConnection(ClusterConfig clusterConfig) throws UnknownHostExcept AwsConfig awsConfig = new AwsConfig(); awsConfig.setEnabled(true); - if(!Strings.isNullOrEmpty(ecsConfig.getAccessKey())) { + if (!Strings.isNullOrEmpty(ecsConfig.getAccessKey())) { awsConfig.setProperty("access-key", ecsConfig.getAccessKey()); } - if(!Strings.isNullOrEmpty(ecsConfig.getSecretKey())) { + if (!Strings.isNullOrEmpty(ecsConfig.getSecretKey())) { awsConfig.setProperty("secret-key", ecsConfig.getSecretKey()); } - if(!Strings.isNullOrEmpty(ecsConfig.getRegion())) { + if (!Strings.isNullOrEmpty(ecsConfig.getRegion())) { awsConfig.setProperty("region", ecsConfig.getRegion()); } - if(!Strings.isNullOrEmpty(ecsConfig.getCluster())) { + if (!Strings.isNullOrEmpty(ecsConfig.getCluster())) { awsConfig.setProperty("cluster", ecsConfig.getCluster()); } - if(!Strings.isNullOrEmpty(ecsConfig.getFamily())) { + if (!Strings.isNullOrEmpty(ecsConfig.getFamily())) { awsConfig.setProperty("family", ecsConfig.getFamily()); } - if(!Strings.isNullOrEmpty(ecsConfig.getServiceName())) { + if (!Strings.isNullOrEmpty(ecsConfig.getServiceName())) { awsConfig.setProperty("service-name", ecsConfig.getServiceName()); } - if(!Strings.isNullOrEmpty(ecsConfig.getHostHeader())) { + if (!Strings.isNullOrEmpty(ecsConfig.getHostHeader())) { awsConfig.setProperty("host-header", ecsConfig.getHostHeader()); } - if(ecsConfig.getOpTimeoutSeconds() > 0) { + if (ecsConfig.getOpTimeoutSeconds() > 0) { awsConfig.setProperty("connection-timeout-seconds", - Integer.toString(ecsConfig.getOpTimeoutSeconds())); + Integer.toString(ecsConfig.getOpTimeoutSeconds())); awsConfig.setProperty("read-timeout-seconds", - Integer.toString(ecsConfig.getOpTimeoutSeconds())); + Integer.toString(ecsConfig.getOpTimeoutSeconds())); } - if(ecsConfig.isExternalClient()) { + if (ecsConfig.isExternalClient()) { awsConfig.setProperty("use-public-ip", Boolean.TRUE.toString()); } break; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/mutator/LargeTextNodeRemover.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/mutator/LargeTextNodeRemover.java index 3f455e6c5..0c8b687c9 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/mutator/LargeTextNodeRemover.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/mutator/LargeTextNodeRemover.java @@ -42,7 +42,7 @@ private void walkTree(String table, if (node.isObject()) { handleObjectNode(table, documentId, (ObjectNode) node); } else if (node.isArray()) { - handleArrayNode(table, documentId,null, (ArrayNode) node); + handleArrayNode(table, documentId, null, (ArrayNode) node); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/query/ElasticSearchQueryGenerator.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/query/ElasticSearchQueryGenerator.java index 6b22ad10b..3fa755355 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/query/ElasticSearchQueryGenerator.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/querystore/query/ElasticSearchQueryGenerator.java @@ -46,7 +46,7 @@ public ElasticSearchQueryGenerator() { @Override public Void visit(BetweenFilter filter) { addFilter(rangeQuery(Utils.storedFieldName(filter.getField())).from(filter.getFrom()) - .to(filter.getTo())); + .to(filter.getTo())); return null; } @@ -118,9 +118,9 @@ public Void visit(ExistsFilter filter) { @Override public Void visit(LastFilter filter) { addFilter(rangeQuery(Utils.storedFieldName(filter.getField())).from(filter.getWindow() - .getStartTime()) - .to(filter.getWindow() - .getEndTime())); + .getStartTime()) + .to(filter.getWindow() + .getEndTime())); return null; } @@ -142,7 +142,7 @@ private void addFilter(QueryBuilder queryBuilder) { } public QueryBuilder genFilter(List filters) { - for(Filter filter : filters) { + for (Filter filter : filters) { filter.accept(this); } return QueryBuilders.constantScoreQuery(boolFilterBuilder); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/DistributedTableMetadataManager.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/DistributedTableMetadataManager.java index 0bf22bca7..ef74ff9e2 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/DistributedTableMetadataManager.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/DistributedTableMetadataManager.java @@ -93,10 +93,10 @@ public class DistributedTableMetadataManager implements TableMetadataManager { private static final String CARDINALITY_FIELD_MAP = "cardinalitytablefieldmap"; private static final String CARDINALITY = "cardinality"; private static final int PRECISION_THRESHOLD = 100; - private static final int TIME_TO_LIVE_CACHE = (int)TimeUnit.MINUTES.toSeconds(15); - private static final int TIME_TO_LIVE_TABLE_CACHE = (int)TimeUnit.DAYS.toSeconds(30); - private static final int TIME_TO_LIVE_CARDINALITY_CACHE = (int)TimeUnit.DAYS.toSeconds(7); - private static final int TIME_TO_NEAR_CACHE = (int)TimeUnit.MINUTES.toSeconds(15); + private static final int TIME_TO_LIVE_CACHE = (int) TimeUnit.MINUTES.toSeconds(15); + private static final int TIME_TO_LIVE_TABLE_CACHE = (int) TimeUnit.DAYS.toSeconds(30); + private static final int TIME_TO_LIVE_CARDINALITY_CACHE = (int) TimeUnit.DAYS.toSeconds(7); + private static final int TIME_TO_NEAR_CACHE = (int) TimeUnit.MINUTES.toSeconds(15); private final HazelcastConnection hazelcastConnection; private final ElasticsearchConnection elasticsearchConnection; private final ObjectMapper mapper; @@ -127,14 +127,14 @@ public DistributedTableMetadataManager(HazelcastConnection hazelcastConnection, l.get(l.size() - 1) .put(e.getKey(), e.getValue()); }, (l1, l2) -> { - if(l1.isEmpty()) { + if (l1.isEmpty()) { return l2; } - if(l2.isEmpty()) { + if (l2.isEmpty()) { return l1; } - if(l1.get(l1.size() - 1) - .size() < limit) { + if (l1.get(l1.size() - 1) + .size() < limit) { Map map = l1.get(l1.size() - 1); ListIterator> mapsIte = l2.listIterator(l2.size()); processMap(limit, map, mapsIte); @@ -145,21 +145,21 @@ public DistributedTableMetadataManager(HazelcastConnection hazelcastConnection, } private static void processMap(int limit, Map map, ListIterator> mapsIte) { - while(mapsIte.hasPrevious() && map.size() < limit) { + while (mapsIte.hasPrevious() && map.size() < limit) { Iterator> ite = mapsIte.previous() .entrySet() .iterator(); processNestedMap(limit, map, ite); - if(!ite.hasNext()) { + if (!ite.hasNext()) { mapsIte.remove(); } } } private static void processNestedMap(int limit, Map map, Iterator> ite) { - while(ite.hasNext() && map.size() < limit) { + while (ite.hasNext() && map.size() < limit) { Entry entry = ite.next(); map.put(entry.getKey(), entry.getValue()); ite.remove(); @@ -167,8 +167,8 @@ private static void processNestedMap(int limit, Map map, Iterator void addEmptyMap(int limit, List> l) { - if(l.isEmpty() || l.get(l.size() - 1) - .size() == limit) { + if (l.isEmpty() || l.get(l.size() - 1) + .size() == limit) { l.add(new HashMap<>()); } } @@ -240,7 +240,7 @@ public void save(Table table) { @Override public Table get(String tableName) { logger.debug("Getting Table : {}", tableName); - if(tableDataStore.containsKey(tableName)) { + if (tableDataStore.containsKey(tableName)) { return tableDataStore.get(tableName); } return null; @@ -249,7 +249,7 @@ public Table get(String tableName) { @Override @SneakyThrows public List get() { - if(0 == tableDataStore.size()) { //HACK::Check https://github.com/hazelcast/hazelcast/issues/1404 + if (0 == tableDataStore.size()) { //HACK::Check https://github.com/hazelcast/hazelcast/issues/1404 return Collections.emptyList(); } ArrayList
tables = Lists.newArrayList(tableDataStore.values()); @@ -264,20 +264,20 @@ public TableFieldMapping getFieldMappings( String tableName, boolean withCardinality, boolean calculateCardinality, long timestamp) { final String table = ElasticsearchUtils.getValidTableName(tableName); - if(!tableDataStore.containsKey(table)) { + if (!tableDataStore.containsKey(table)) { throw FoxtrotExceptions.createBadRequestException(table, String.format("unknown_table table:%s", table)); } - if(fieldDataCardinalityCache.size() == 0) { + if (fieldDataCardinalityCache.size() == 0) { initializeCardinalityCache(); } TableFieldMapping tableFieldMapping; - if(fieldDataCache.containsKey(table) && !withCardinality) { + if (fieldDataCache.containsKey(table) && !withCardinality) { tableFieldMapping = fieldDataCache.get(table); - } else if(fieldDataCardinalityCache.containsKey(table) && withCardinality && !calculateCardinality) { + } else if (fieldDataCardinalityCache.containsKey(table) && withCardinality && !calculateCardinality) { tableFieldMapping = fieldDataCardinalityCache.get(table); } else { tableFieldMapping = getTableFieldMapping(table); - if(calculateCardinality) { + if (calculateCardinality) { estimateCardinality(table, tableFieldMapping.getMappings(), timestamp); fieldDataCardinalityCache.put(table, tableFieldMapping); saveCardinalityCache(table, tableFieldMapping); @@ -288,13 +288,13 @@ public TableFieldMapping getFieldMappings( return TableFieldMapping.builder() .table(table) .mappings(tableFieldMapping.getMappings() - .stream() - .map(x -> FieldMetadata.builder() - .field(x.getField()) - .type(x.getType()) - .estimationData(withCardinality ? x.getEstimationData() : null) - .build()) - .collect(Collectors.toSet())) + .stream() + .map(x -> FieldMetadata.builder() + .field(x.getField()) + .type(x.getType()) + .estimationData(withCardinality ? x.getEstimationData() : null) + .build()) + .collect(Collectors.toSet())) .build(); } @@ -307,13 +307,12 @@ private TableFieldMapping getTableFieldMapping(String table) { mappingsResponse = elasticsearchConnection.getClient() .indices() .getMapping(new GetMappingsRequest().indices(indices), RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } Set indicesName = Sets.newHashSet(); - for(ObjectCursor index : mappingsResponse.getMappings() + for (ObjectCursor index : mappingsResponse.getMappings() .keys()) { indicesName.add(index.value); } @@ -346,7 +345,7 @@ private TableFieldMapping getTableFieldMapping(String table) { @Override public void updateEstimationData(final String table, long timestamp) { - if(!tableDataStore.containsKey(table)) { + if (!tableDataStore.containsKey(table)) { throw FoxtrotExceptions.createBadRequestException(table, String.format("unknown_table table:%s", table)); } final TableFieldMapping tableFieldMapping = getFieldMappings(table, cardinalityConfig.isEnabled(), false, timestamp); @@ -354,7 +353,7 @@ public void updateEstimationData(final String table, long timestamp) { } private void estimateCardinality(final String table, final Collection fields, long time) { - if(CollectionUtils.isNullOrEmpty(fields)) { + if (CollectionUtils.isNullOrEmpty(fields)) { logger.warn("No fields.. Nothing to query"); return; } @@ -373,7 +372,7 @@ private Map estimateFirstPhaseData(String table, String Map fields) { Map estimationDataMap = Maps.newHashMap(); int subListSize; - if(cardinalityConfig == null || cardinalityConfig.getSubListSize() == 0) { + if (cardinalityConfig == null || cardinalityConfig.getSubListSize() == 0) { subListSize = ElasticsearchUtils.DEFAULT_SUB_LIST_SIZE; } else { subListSize = cardinalityConfig.getSubListSize(); @@ -383,7 +382,7 @@ private Map estimateFirstPhaseData(String table, String .stream() .collect(mapSize(subListSize)); - for(Map innerMap : listOfMaps) { + for (Map innerMap : listOfMaps) { MultiSearchRequest multiQuery = new MultiSearchRequest(); innerMap.values() .forEach(fieldMetadata -> { @@ -391,8 +390,8 @@ private Map estimateFirstPhaseData(String table, String SearchRequest query = new SearchRequest(index) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(0) - .query(QueryBuilders.existsQuery(field))); + .size(0) + .query(QueryBuilders.existsQuery(field))); switch (fieldMetadata.getType()) { case STRING: evaluateStringAggregation(table, field, fieldMetadata.getType(), query); @@ -416,15 +415,14 @@ private Map estimateFirstPhaseData(String table, String try { try { multiResponse = client.msearch(multiQuery, RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { logger.error("Error", e); } } finally { logger.info("Cardinality query on table {} for {} fields took {} ms", table, fields.size(), - stopwatch.elapsed(TimeUnit.MILLISECONDS)); + stopwatch.elapsed(TimeUnit.MILLISECONDS)); } - if(null != multiResponse) { + if (null != multiResponse) { handleFirstPhaseMultiSearchResponse(multiResponse, table, fields, estimationDataMap); } } @@ -433,9 +431,9 @@ private Map estimateFirstPhaseData(String table, String private void handleFirstPhaseMultiSearchResponse(MultiSearchResponse multiResponse, String table, Map fields, Map estimationDataMap) { - for(MultiSearchResponse.Item item : multiResponse.getResponses()) { + for (MultiSearchResponse.Item item : multiResponse.getResponses()) { SearchResponse response = validateAndGetSearchResponse(item, table); - if(null == response) { + if (null == response) { continue; } final long hits = response.getHits() @@ -443,10 +441,10 @@ private void handleFirstPhaseMultiSearchResponse(MultiSearchResponse multiRespon Map output = response.getAggregations().asMap(); output.forEach((key, value) -> { FieldMetadata fieldMetadata = fields.get(key); - if(fieldMetadata == null) { + if (fieldMetadata == null) { fieldMetadata = fields.get(key.replace("_", "")); } - if(fieldMetadata == null) { + if (fieldMetadata == null) { return; } switch (fieldMetadata.getType()) { @@ -461,7 +459,7 @@ private void handleFirstPhaseMultiSearchResponse(MultiSearchResponse multiRespon break; case BOOLEAN: evaluateBooleanEstimation(key, estimationDataMap); - break; + break; case DATE: case OBJECT: case TEXT: @@ -475,8 +473,8 @@ private void evaluateStringAggregation(String table, String field, FieldType typ logger.info("table:{} field:{} type:{} aggregationType:{}", table, field, type, CARDINALITY); query.source() .aggregation(AggregationBuilders.cardinality(field) - .field(field) - .precisionThreshold(PRECISION_THRESHOLD)); + .field(field) + .precisionThreshold(PRECISION_THRESHOLD)); } private void evaluateDoubleAggregation(String table, String field, FieldType type, SearchRequest query) { @@ -485,17 +483,17 @@ private void evaluateDoubleAggregation(String table, String field, FieldType typ ); query.source() .aggregation(AggregationBuilders.percentiles(field) - .field(field) - .percentiles(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)); + .field(field) + .percentiles(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)); query.source() .aggregation(AggregationBuilders.cardinality("_" + field) - .field(field) - .precisionThreshold(PRECISION_THRESHOLD)); + .field(field) + .precisionThreshold(PRECISION_THRESHOLD)); } private void evaluateStringEstimation(Aggregation value, String table, String key, FieldType type, Map estimationDataMap, long hits) { - Cardinality cardinality = (Cardinality)value; + Cardinality cardinality = (Cardinality) value; logger.info("table:{} field:{} type:{} aggregationType:{} value:{} ", table, key, type, CARDINALITY, cardinality.getValue() ); @@ -507,10 +505,10 @@ private void evaluateStringEstimation(Aggregation value, String table, String ke private void evaluateDoubleEstimation(Aggregation value, String table, String key, FieldType type, Map estimationDataMap, long hits) { - if(value instanceof Percentiles) { - Percentiles percentiles = (Percentiles)value; + if (value instanceof Percentiles) { + Percentiles percentiles = (Percentiles) value; double[] values = new double[10]; - for(int i = 10; i <= 100; i += 10) { + for (int i = 10; i <= 100; i += 10) { final Double percentile = percentiles.percentile(i); values[(i / 10) - 1] = percentile.isNaN() ? 0 : percentile; } @@ -521,7 +519,7 @@ private void evaluateDoubleEstimation(Aggregation value, String table, String ke .values(values) .count(hits) .build()); - } else if(value instanceof Cardinality) { + } else if (value instanceof Cardinality) { Cardinality cardinality = (Cardinality) value; logger.info("table:{} field:{} type:{} aggregationType:{} value:{}", table, key, type, CARDINALITY, cardinality.getValue() @@ -550,7 +548,7 @@ private Map estimateSecondPhaseData(String table, String .map(EstimationData::getCount) .max(Comparator.naturalOrder()) .orElse(0L); - if(maxDocuments == 0) { + if (maxDocuments == 0) { return estimationData; } @@ -568,23 +566,23 @@ public Void visit(PercentileEstimationData percentileEstimationData) { @Override public Void visit(CardinalityEstimationData cardinalityEstimationData) { - if(cardinalityEstimationData.getCount() > 0 && cardinalityEstimationData.getCardinality() > 0) { - int countToCardinalityRatio = (int)(cardinalityEstimationData.getCount() / cardinalityEstimationData.getCardinality()); - int documentToCountRatio = (int)(maxDocuments / cardinalityEstimationData.getCount()); - if(cardinalityEstimationData.getCardinality() <= 100 || (countToCardinalityRatio > 100 && documentToCountRatio < 100 && - cardinalityEstimationData.getCardinality() <= 5000)) { + if (cardinalityEstimationData.getCount() > 0 && cardinalityEstimationData.getCardinality() > 0) { + int countToCardinalityRatio = (int) (cardinalityEstimationData.getCount() / cardinalityEstimationData.getCardinality()); + int documentToCountRatio = (int) (maxDocuments / cardinalityEstimationData.getCount()); + if (cardinalityEstimationData.getCardinality() <= 100 || (countToCardinalityRatio > 100 && documentToCountRatio < 100 && + cardinalityEstimationData.getCardinality() <= 5000)) { logger.info("field:{} maxCount:{} countToCardinalityRatio:{} documentToCountRatio:{}", key, maxDocuments, - countToCardinalityRatio, documentToCountRatio - ); + countToCardinalityRatio, documentToCountRatio + ); multiQuery.add( new SearchRequest(index) - .indicesOptions(Utils.indicesOptions()) - .source(new SearchSourceBuilder() - .query(QueryBuilders.existsQuery(key)) - .aggregation(AggregationBuilders.terms(key) - .field(key) - .size(ElasticsearchQueryUtils.QUERY_SIZE)) - .size(0))); + .indicesOptions(Utils.indicesOptions()) + .source(new SearchSourceBuilder() + .query(QueryBuilders.existsQuery(key)) + .aggregation(AggregationBuilders.terms(key) + .field(key) + .size(ElasticsearchQueryUtils.QUERY_SIZE)) + .size(0))); } } return null; @@ -602,11 +600,10 @@ public Void visit(TermHistogramEstimationData termHistogramEstimationData) { try { multiResponse = client .msearch(multiQuery, RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { logger.error("Error occurred", e); } - if(null != multiResponse) { + if (null != multiResponse) { handleSecondPhaseMultiSearchResponse(multiResponse, table, estimationDataMap); } return estimationDataMap; @@ -614,16 +611,16 @@ public Void visit(TermHistogramEstimationData termHistogramEstimationData) { private void handleSecondPhaseMultiSearchResponse(MultiSearchResponse multiResponse, String table, Map estimationDataMap) { - for(MultiSearchResponse.Item item : multiResponse.getResponses()) { + for (MultiSearchResponse.Item item : multiResponse.getResponses()) { SearchResponse response = validateAndGetSearchResponse(item, table); - if(null == response) { + if (null == response) { continue; } final long hits = response.getHits() .getTotalHits(); Map output = response.getAggregations().asMap(); output.forEach((key, value) -> { - Terms terms = (Terms)output.get(key); + Terms terms = (Terms) output.get(key); estimationDataMap.put(key, TermHistogramEstimationData.builder() .count(hits) .termCounts(terms.getBuckets() @@ -635,12 +632,12 @@ private void handleSecondPhaseMultiSearchResponse(MultiSearchResponse multiRespo } private SearchResponse validateAndGetSearchResponse(MultiSearchResponse.Item item, String table) { - if(item.isFailure()) { + if (item.isFailure()) { logger.info("FailureInDeducingCardinality table:{} failureMessage:{}", table, item.getFailureMessage()); return null; } SearchResponse response = item.getResponse(); - if(null == response.getAggregations()) { + if (null == response.getAggregations()) { return null; } return response; @@ -654,7 +651,7 @@ public boolean exists(String tableName) { @Override public void delete(String tableName) { logger.info("Deleting Table : {}", tableName); - if(tableDataStore.containsKey(tableName)) { + if (tableDataStore.containsKey(tableName)) { tableDataStore.delete(tableName); } logger.info("Deleted Table : {}", tableName); @@ -680,7 +677,7 @@ private void saveCardinalityCache(String table, TableFieldMapping tableFieldMapp elasticsearchConnection.getClient() .index(new IndexRequest(CARDINALITY_CACHE_INDEX, ElasticsearchUtils.DOCUMENT_TYPE_NAME, table) .timeout(new TimeValue(2, TimeUnit.SECONDS)) - .source(mapper.writeValueAsBytes(tableFieldMapping), XContentType.JSON), RequestOptions.DEFAULT); + .source(mapper.writeValueAsBytes(tableFieldMapping), XContentType.JSON), RequestOptions.DEFAULT); } catch (Exception e) { logger.error("Error in saving cardinality cache: " + e.getMessage(), e); } @@ -695,9 +692,9 @@ private List getAllCardinalityCache() { .types(ElasticsearchUtils.DOCUMENT_TYPE_NAME) .indicesOptions(Utils.indicesOptions()) .source(new SearchSourceBuilder() - .size(maxSize)), RequestOptions.DEFAULT); - for(SearchHit hit : com.collections.CollectionUtils.nullAndEmptySafeValueList(response.getHits() - .getHits())) { + .size(maxSize)), RequestOptions.DEFAULT); + for (SearchHit hit : com.collections.CollectionUtils.nullAndEmptySafeValueList(response.getHits() + .getHits())) { tableFieldMappings.add(mapper.readValue(hit.getSourceAsString(), TableFieldMapping.class)); } return tableFieldMappings; @@ -710,7 +707,7 @@ private List getAllCardinalityCache() { @Override public void initializeCardinalityCache() { List tableFieldMappings = getAllCardinalityCache(); - for(TableFieldMapping tableFieldMapping : com.collections.CollectionUtils.nullSafeList(tableFieldMappings)) { + for (TableFieldMapping tableFieldMapping : com.collections.CollectionUtils.nullSafeList(tableFieldMappings)) { fieldDataCardinalityCache.put(tableFieldMapping.getTable(), tableFieldMapping); } } @@ -721,11 +718,11 @@ private static class FieldMetadataComparator implements Comparator getAll() { @Override public void update(Table table) { validateTableParams(table); - if(!metadataManager.exists(table.getName())) { + if (!metadataManager.exists(table.getName())) { throw FoxtrotExceptions.createTableMissingException(table.getName()); } metadataManager.save(table); @@ -80,7 +80,7 @@ public void delete(String tableName) { } private void validateTableParams(Table table) { - if(table == null || table.getName() == null || table.getName() + if (table == null || table.getName() == null || table.getName() .trim() .isEmpty() || table.getTtl() <= 0) { throw FoxtrotExceptions.createBadRequestException(table != null ? table.getName() : null, "Invalid Table Params"); diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/TableMapStore.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/TableMapStore.java index f9ab4ee40..b0194c712 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/TableMapStore.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/table/impl/TableMapStore.java @@ -69,7 +69,7 @@ public static Factory factory(ElasticsearchConnection elasticsearchConnection) { @Override public void store(String key, Table value) { - if(key == null || value == null || value.getName() == null) { + if (key == null || value == null || value.getName() == null) { throw new TableMapStoreException(String.format("Illegal Store Request - %s - %s", key, value)); } logger.info("Storing key: {}", key); @@ -89,25 +89,25 @@ public void store(String key, Table value) { @Override public void storeAll(Map map) { - if(map == null) { + if (map == null) { throw new TableMapStoreException("Illegal Store Request - Null Map"); } - if(map.containsKey(null)) { + if (map.containsKey(null)) { throw new TableMapStoreException("Illegal Store Request - Null Key is Present"); } logger.info("Store all called for multiple values"); BulkRequest bulkRequestBuilder = new BulkRequest() .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - for(Map.Entry mapEntry : map.entrySet()) { + for (Map.Entry mapEntry : map.entrySet()) { try { - if(mapEntry.getValue() == null) { + if (mapEntry.getValue() == null) { throw new TableMapStoreException( String.format("Illegal Store Request - Object is Null for Table - %s", mapEntry.getKey())); } Map sourceMap = ElasticsearchQueryUtils.toMap(objectMapper, mapEntry.getValue()); bulkRequestBuilder.add(new IndexRequest(TABLE_META_INDEX, TABLE_META_TYPE, mapEntry.getKey()) - .source(sourceMap)); + .source(sourceMap)); } catch (Exception e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } @@ -116,8 +116,7 @@ public void storeAll(Map map) { elasticsearchConnection .getClient() .bulk(bulkRequestBuilder, RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } } @@ -128,9 +127,8 @@ public void delete(String key) { try { elasticsearchConnection.getClient() .delete(new DeleteRequest(TABLE_META_INDEX, TABLE_META_TYPE, key) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); - } - catch (IOException e) { + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } logger.info("Deleted value: {}", key); @@ -141,15 +139,14 @@ public void deleteAll(Collection keys) { logger.info("Delete all called for multiple values: {}", keys); BulkRequest bulRequest = new BulkRequest() .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - for(String key : keys) { + for (String key : keys) { bulRequest.add(new DeleteRequest(TABLE_META_INDEX, TABLE_META_TYPE, key)); } try { elasticsearchConnection .getClient() .bulk(bulRequest, RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } logger.info("Deleted multiple values: {}", keys); @@ -161,7 +158,7 @@ public Table load(String key) { try { GetResponse response = elasticsearchConnection.getClient() .get(new GetRequest(TABLE_META_INDEX, TABLE_META_TYPE, key), RequestOptions.DEFAULT); - if(!response.isExists()) { + if (!response.isExists()) { return null; } return objectMapper.readValue(response.getSourceAsBytes(), Table.class); @@ -180,15 +177,14 @@ public Map loadAll(Collection keys) { try { response = elasticsearchConnection.getClient() .multiGet(multiGetRequest, RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } Map tables = Maps.newHashMap(); - for(MultiGetItemResponse multiGetItemResponse : response) { + for (MultiGetItemResponse multiGetItemResponse : response) { try { Table table = objectMapper.readValue(multiGetItemResponse.getResponse() - .getSourceAsString(), Table.class); + .getSourceAsString(), Table.class); tables.put(table.getName(), table); } catch (Exception e) { throw new TableMapStoreException("Error getting data for table: " + multiGetItemResponse.getId()); @@ -207,34 +203,32 @@ public Set loadAllKeys() { .search(new SearchRequest(TABLE_META_INDEX) .types(TABLE_META_TYPE) .source(new SearchSourceBuilder() - .query(QueryBuilders.matchAllQuery()) - .size(ElasticsearchQueryUtils.QUERY_SIZE) - .fetchSource(false)) + .query(QueryBuilders.matchAllQuery()) + .size(ElasticsearchQueryUtils.QUERY_SIZE) + .fetchSource(false)) .scroll(new TimeValue(30, TimeUnit.SECONDS)), RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } Set ids = Sets.newHashSet(); do { - for(SearchHit hit : response.getHits() + for (SearchHit hit : response.getHits() .getHits()) { ids.add(hit.getId()); } - if(0 == response.getHits() + if (0 == response.getHits() .getHits().length) { break; } try { response = elasticsearchConnection.getClient() .scroll(new SearchScrollRequest(response.getScrollId()) - .scroll(new TimeValue(60000)), RequestOptions.DEFAULT); - } - catch (IOException e) { + .scroll(new TimeValue(60000)), RequestOptions.DEFAULT); + } catch (IOException e) { throw new TableMapStoreException("Error bulk saving meta: ", e); } } while (response.getHits() - .getHits().length != 0) + .getHits().length != 0) ; logger.info("Loaded value count: {}", ids.size()); return ids; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/Constants.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/Constants.java index a00d4e1a0..25f1cf6b3 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/Constants.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/Constants.java @@ -6,7 +6,7 @@ /*** Created by nitish.goyal on 01/06/20 ***/ -@NoArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class Constants { public static final int CACHE_VALUE_SIZE_IN_KB = 256 * 1024; diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/ElasticsearchQueryUtils.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/ElasticsearchQueryUtils.java index fa90d7231..64968c860 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/ElasticsearchQueryUtils.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/ElasticsearchQueryUtils.java @@ -46,9 +46,9 @@ public static QueryBuilder translateFilter(ActionRequest request, List e val filters = (null == extraFilters || extraFilters.isEmpty()) ? request.getFilters() : ImmutableList.builder() - .addAll(request.getFilters()) - .addAll(extraFilters) - .build(); + .addAll(request.getFilters()) + .addAll(extraFilters) + .build(); return new ElasticSearchQueryGenerator().genFilter(filters); } } diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/MetricUtil.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/MetricUtil.java index 5ee01c191..cf3675526 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/MetricUtil.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/MetricUtil.java @@ -3,6 +3,7 @@ import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.jmx.JmxReporter; + import java.util.concurrent.TimeUnit; /** diff --git a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/TableUtil.java b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/TableUtil.java index 35b862db4..9b71d7358 100644 --- a/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/TableUtil.java +++ b/foxtrot-core/src/main/java/com/flipkart/foxtrot/core/util/TableUtil.java @@ -26,9 +26,9 @@ private TableUtil() { } public static String getTableName(final HbaseConfig hbaseConfig, final Table table) { - if(table.isSeggregatedBackend()) { + if (table.isSeggregatedBackend()) { final String tablePrefix = hbaseConfig.getSeggregatedTablePrefix(); - if(!Strings.isNullOrEmpty(tablePrefix)) { + if (!Strings.isNullOrEmpty(tablePrefix)) { return String.format("%s%s", tablePrefix, table.getName()); } else { return table.getName(); diff --git a/foxtrot-core/src/main/java/com/google/common/base/Stopwatch.java b/foxtrot-core/src/main/java/com/google/common/base/Stopwatch.java index 5d102f7cd..21e2b6640 100644 --- a/foxtrot-core/src/main/java/com/google/common/base/Stopwatch.java +++ b/foxtrot-core/src/main/java/com/google/common/base/Stopwatch.java @@ -110,35 +110,27 @@ public static Stopwatch createStarted(Ticker ticker) { return new Stopwatch(ticker).start(); } - public long elapsedTime(TimeUnit desiredUnit) { - return desiredUnit.convert(this.elapsedNanos(), TimeUnit.NANOSECONDS); - } - - public long elapsedMillis() { - return this.elapsedTime(TimeUnit.MILLISECONDS); - } - static String formatCompact4Digits(double value) { return String.format(Locale.ROOT, "%.4g", value); } private static TimeUnit chooseUnit(long nanos) { - if(DAYS.convert(nanos, NANOSECONDS) > 0) { + if (DAYS.convert(nanos, NANOSECONDS) > 0) { return DAYS; } - if(HOURS.convert(nanos, NANOSECONDS) > 0) { + if (HOURS.convert(nanos, NANOSECONDS) > 0) { return HOURS; } - if(MINUTES.convert(nanos, NANOSECONDS) > 0) { + if (MINUTES.convert(nanos, NANOSECONDS) > 0) { return MINUTES; } - if(SECONDS.convert(nanos, NANOSECONDS) > 0) { + if (SECONDS.convert(nanos, NANOSECONDS) > 0) { return SECONDS; } - if(MILLISECONDS.convert(nanos, NANOSECONDS) > 0) { + if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) { return MILLISECONDS; } - if(MICROSECONDS.convert(nanos, NANOSECONDS) > 0) { + if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) { return MICROSECONDS; } return NANOSECONDS; @@ -165,6 +157,14 @@ private static String abbreviate(TimeUnit unit) { } } + public long elapsedTime(TimeUnit desiredUnit) { + return desiredUnit.convert(this.elapsedNanos(), TimeUnit.NANOSECONDS); + } + + public long elapsedMillis() { + return this.elapsedTime(TimeUnit.MILLISECONDS); + } + /** * Returns {@code true} if {@link #start()} has been called on this stopwatch, and {@link #stop()} * has not been called since the last call to {@code @@ -238,7 +238,7 @@ public String toString() { long nanos = elapsedNanos(); TimeUnit unit = chooseUnit(nanos); - double value = (double)nanos / NANOSECONDS.convert(1, unit); + double value = (double) nanos / NANOSECONDS.convert(1, unit); // Too bad this functionality is not exposed as a regular method call return formatCompact4Digits(value) + " " + abbreviate(unit); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/MockHTable.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/MockHTable.java index e6ac8f3c4..ee9774fb3 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/MockHTable.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/MockHTable.java @@ -156,20 +156,20 @@ private static List toKeyValue(byte[] row, NavigableMap toKeyValue(byte[] row, NavigableMap>> rowdata, long timestampStart, long timestampEnd, int maxVersions) { List ret = new ArrayList(); - for(byte[] family : rowdata.keySet()) { - for(byte[] qualifier : rowdata.get(family) + for (byte[] family : rowdata.keySet()) { + for (byte[] qualifier : rowdata.get(family) .keySet()) { int versionsAdded = 0; - for(Entry tsToVal : rowdata.get(family) + for (Entry tsToVal : rowdata.get(family) .get(qualifier) .descendingMap() .entrySet()) { - if(versionsAdded++ == maxVersions) + if (versionsAdded++ == maxVersions) break; Long timestamp = tsToVal.getKey(); - if(timestamp < timestampStart) + if (timestamp < timestampStart) continue; - if(timestamp > timestampEnd) + if (timestamp > timestampEnd) continue; byte[] value = tsToVal.getValue(); ret.add(new KeyValue(row, family, qualifier, timestamp, value)); @@ -203,8 +203,8 @@ public static MockHTable create() { */ public static MockHTable with(Map> dump) { MockHTable ret = new MockHTable(); - for(String row : dump.keySet()) { - for(String column : dump.get(row) + for (String row : dump.keySet()) { + for (String column : dump.get(row) .keySet()) { String val = dump.get(row) .get(column); @@ -227,15 +227,15 @@ private static void put(MockHTable ret, String row, String column, String val) { byte[] family = Bytes.toBytesBinary(fq[0]); byte[] qualifier = Bytes.toBytesBinary(fq[1]); NavigableMap>> families = ret.forceFind(ret.data, Bytes.toBytesBinary(row), - new TreeMap>>( - Bytes.BYTES_COMPARATOR) - ); + new TreeMap>>( + Bytes.BYTES_COMPARATOR) + ); NavigableMap> qualifiers = ret.forceFind(families, family, - new TreeMap>( - Bytes.BYTES_COMPARATOR) - ); + new TreeMap>( + Bytes.BYTES_COMPARATOR) + ); NavigableMap values = ret.forceFind(qualifiers, qualifier, new TreeMap()); values.put(System.currentTimeMillis(), Bytes.toBytesBinary(val)); } @@ -257,7 +257,7 @@ private static void put(MockHTable ret, String row, String column, String val) { */ public static MockHTable with(String[][] dump) { MockHTable ret = new MockHTable(); - for(String[] row : dump) { + for (String[] row : dump) { put(ret, row[0], row[1], row[2]); } return ret; @@ -324,23 +324,23 @@ public HTableDescriptor getTableDescriptor() { @Override public boolean exists(Get get) throws IOException { - if(get.getFamilyMap() == null || get.getFamilyMap() - .size() == 0) { + if (get.getFamilyMap() == null || get.getFamilyMap() + .size() == 0) { return data.containsKey(get.getRow()); } else { byte[] row = get.getRow(); - if(!data.containsKey(row)) { + if (!data.containsKey(row)) { return false; } - for(byte[] family : get.getFamilyMap() + for (byte[] family : get.getFamilyMap() .keySet()) { - if(!data.get(row) + if (!data.get(row) .containsKey(family)) { return false; } else { - for(byte[] qualifier : get.getFamilyMap() + for (byte[] qualifier : get.getFamilyMap() .get(family)) { - if(!data.get(row) + if (!data.get(row) .get(family) .containsKey(qualifier)) { return false; @@ -359,34 +359,34 @@ public boolean[] existsAll(List gets) throws IOException { @Override public Result get(Get get) throws IOException { - if(!data.containsKey(get.getRow())) + if (!data.containsKey(get.getRow())) return new Result(); byte[] row = get.getRow(); List kvs = new ArrayList(); - if(!get.hasFamilies()) { + if (!get.hasFamilies()) { kvs = toKeyValue(row, data.get(row), get.getMaxVersions()); } else { - for(byte[] family : get.getFamilyMap() + for (byte[] family : get.getFamilyMap() .keySet()) { - if(data.get(row) - .get(family) == null) + if (data.get(row) + .get(family) == null) continue; NavigableSet qualifiers = get.getFamilyMap() .get(family); - if(qualifiers == null || qualifiers.isEmpty()) + if (qualifiers == null || qualifiers.isEmpty()) qualifiers = data.get(row) .get(family) .navigableKeySet(); - for(byte[] qualifier : qualifiers) { - if(qualifier == null) + for (byte[] qualifier : qualifiers) { + if (qualifier == null) qualifier = "".getBytes(); - if(!data.get(row) + if (!data.get(row) .containsKey(family) || !data.get(row) .get(family) .containsKey(qualifier) || data.get(row) - .get(family) - .get(qualifier) - .isEmpty()) + .get(family) + .get(qualifier) + .isEmpty()) continue; Entry timestampAndValue = data.get(row) .get(family) @@ -397,17 +397,17 @@ public Result get(Get get) throws IOException { } } Filter filter = get.getFilter(); - if(filter != null) { + if (filter != null) { filter.reset(); List nkvs = new ArrayList(kvs.size()); - for(KeyValue kv : kvs) { - if(filter.filterAllRemaining()) { + for (KeyValue kv : kvs) { + if (filter.filterAllRemaining()) { break; } - if(filter.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength())) { + if (filter.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength())) { continue; } - if(filter.filterKeyValue(kv) == ReturnCode.INCLUDE) { + if (filter.filterKeyValue(kv) == ReturnCode.INCLUDE) { nkvs.add(kv); } // ignoring next key hint which is a optimization to reduce file system IO @@ -428,50 +428,50 @@ public ResultScanner getScanner(Scan scan) throws IOException { byte[] sp = scan.getStopRow(); Filter filter = scan.getFilter(); - for(byte[] row : data.keySet()) { + for (byte[] row : data.keySet()) { // if row is equal to startRow emit it. When startRow (inclusive) and // stopRow (exclusive) is the same, it should not be excluded which would // happen w/o this control. - if(st != null && st.length > 0 && Bytes.BYTES_COMPARATOR.compare(st, row) != 0) { + if (st != null && st.length > 0 && Bytes.BYTES_COMPARATOR.compare(st, row) != 0) { // if row is before startRow do not emit, pass to next row - if(st != null && st.length > 0 && Bytes.BYTES_COMPARATOR.compare(st, row) > 0) + if (st != null && st.length > 0 && Bytes.BYTES_COMPARATOR.compare(st, row) > 0) continue; // if row is equal to stopRow or after it do not emit, stop iteration - if(sp != null && sp.length > 0 && Bytes.BYTES_COMPARATOR.compare(sp, row) <= 0) + if (sp != null && sp.length > 0 && Bytes.BYTES_COMPARATOR.compare(sp, row) <= 0) break; } List kvs = null; - if(!scan.hasFamilies()) { + if (!scan.hasFamilies()) { kvs = toKeyValue(row, data.get(row), scan.getTimeRange() .getMin(), scan.getTimeRange() - .getMax(), scan.getMaxVersions()); + .getMax(), scan.getMaxVersions()); } else { kvs = new ArrayList(); - for(byte[] family : scan.getFamilyMap() + for (byte[] family : scan.getFamilyMap() .keySet()) { - if(data.get(row) - .get(family) == null) + if (data.get(row) + .get(family) == null) continue; NavigableSet qualifiers = scan.getFamilyMap() .get(family); - if(qualifiers == null || qualifiers.isEmpty()) + if (qualifiers == null || qualifiers.isEmpty()) qualifiers = data.get(row) .get(family) .navigableKeySet(); - for(byte[] qualifier : qualifiers) { - if(data.get(row) - .get(family) - .get(qualifier) == null) + for (byte[] qualifier : qualifiers) { + if (data.get(row) + .get(family) + .get(qualifier) == null) continue; - for(Long timestamp : data.get(row) + for (Long timestamp : data.get(row) .get(family) .get(qualifier) .descendingKeySet()) { - if(timestamp < scan.getTimeRange() + if (timestamp < scan.getTimeRange() .getMin()) continue; - if(timestamp > scan.getTimeRange() + if (timestamp > scan.getTimeRange() .getMax()) continue; byte[] value = data.get(row) @@ -479,27 +479,27 @@ public ResultScanner getScanner(Scan scan) throws IOException { .get(qualifier) .get(timestamp); kvs.add(new KeyValue(row, family, qualifier, timestamp, value)); - if(kvs.size() == scan.getMaxVersions()) { + if (kvs.size() == scan.getMaxVersions()) { break; } } } } } - if(filter != null) { + if (filter != null) { filter.reset(); List nkvs = new ArrayList(kvs.size()); - for(KeyValue kv : kvs) { - if(filter.filterAllRemaining()) { + for (KeyValue kv : kvs) { + if (filter.filterAllRemaining()) { break; } - if(filter.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength())) { + if (filter.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength())) { continue; } ReturnCode filterResult = filter.filterKeyValue(kv); - if(filterResult == ReturnCode.INCLUDE) { + if (filterResult == ReturnCode.INCLUDE) { nkvs.add(kv); - } else if(filterResult == ReturnCode.NEXT_ROW) { + } else if (filterResult == ReturnCode.NEXT_ROW) { break; } // ignoring next key hint which is a optimization to reduce file system IO @@ -509,7 +509,7 @@ public ResultScanner getScanner(Scan scan) throws IOException { // } kvs = nkvs; } - if(!kvs.isEmpty()) { + if (!kvs.isEmpty()) { ret.add(new Result(kvs)); } } @@ -523,9 +523,9 @@ public Iterator iterator() { public Result[] next(int nbRows) throws IOException { ArrayList resultSets = new ArrayList(nbRows); - for(int i = 0; i < nbRows; i++) { + for (int i = 0; i < nbRows; i++) { Result next = next(); - if(next != null) { + if (next != null) { resultSets.add(next); } else { break; @@ -565,19 +565,19 @@ public ResultScanner getScanner(byte[] family, byte[] qualifier) throws IOExcept public void put(Put put) throws IOException { byte[] row = put.getRow(); NavigableMap>> rowData = forceFind(data, row, - new TreeMap>>( - Bytes.BYTES_COMPARATOR) - ); - for(byte[] family : put.getFamilyMap() + new TreeMap>>( + Bytes.BYTES_COMPARATOR) + ); + for (byte[] family : put.getFamilyMap() .keySet()) { NavigableMap> familyData = forceFind(rowData, family, - new TreeMap>( - Bytes.BYTES_COMPARATOR) - ); - for(KeyValue kv : put.getFamilyMap() + new TreeMap>( + Bytes.BYTES_COMPARATOR) + ); + for (KeyValue kv : put.getFamilyMap() .get(family)) { kv.updateLatestStamp(Bytes.toBytes(System.currentTimeMillis())); byte[] qualifier = kv.getQualifier(); @@ -598,7 +598,7 @@ public void put(Put put) throws IOException { */ private V forceFind(NavigableMap map, K key, V newObject) { V data = map.get(key); - if(data == null) { + if (data == null) { data = newObject; map.put(key, data); } @@ -607,7 +607,7 @@ private V forceFind(NavigableMap map, K key, V newObject) { @Override public void put(List puts) throws IOException { - for(Put put : puts) { + for (Put put : puts) { put(put); } } @@ -624,7 +624,7 @@ public void put(List puts) throws IOException { * not exists in db, false otherwise */ private boolean check(byte[] row, byte[] family, byte[] qualifier, byte[] value) { - if(value == null || value.length == 0) + if (value == null || value.length == 0) return !data.containsKey(row) || !data.get(row) .containsKey(family) || !data.get(row) .get(family) @@ -632,20 +632,20 @@ private boolean check(byte[] row, byte[] family, byte[] qualifier, byte[] value) else return data.containsKey(row) && data.get(row) .containsKey(family) && data.get(row) - .get(family) - .containsKey(qualifier) && !data.get(row) + .get(family) + .containsKey(qualifier) && !data.get(row) .get(family) .get(qualifier) .isEmpty() && Arrays.equals(data.get(row) - .get(family) - .get(qualifier) - .lastEntry() - .getValue(), value); + .get(family) + .get(qualifier) + .lastEntry() + .getValue(), value); } @Override public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) throws IOException { - if(check(row, family, qualifier, value)) { + if (check(row, family, qualifier, value)) { put(put); return true; } @@ -661,39 +661,39 @@ public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareF @Override public void delete(Delete delete) throws IOException { byte[] row = delete.getRow(); - if(data.get(row) == null) + if (data.get(row) == null) return; - if(delete.getFamilyMap() - .size() == 0) { + if (delete.getFamilyMap() + .size() == 0) { data.remove(row); return; } - for(byte[] family : delete.getFamilyMap() + for (byte[] family : delete.getFamilyMap() .keySet()) { - if(data.get(row) - .get(family) == null) + if (data.get(row) + .get(family) == null) continue; - if(delete.getFamilyMap() + if (delete.getFamilyMap() .get(family) .isEmpty()) { data.get(row) .remove(family); continue; } - for(KeyValue kv : delete.getFamilyMap() + for (KeyValue kv : delete.getFamilyMap() .get(family)) { data.get(row) .get(kv.getFamily()) .remove(kv.getQualifier()); } - if(data.get(row) + if (data.get(row) .get(family) .isEmpty()) { data.get(row) .remove(family); } } - if(data.get(row) + if (data.get(row) .isEmpty()) { data.remove(row); } @@ -701,14 +701,14 @@ public void delete(Delete delete) throws IOException { @Override public void delete(List deletes) throws IOException { - for(Delete delete : deletes) { + for (Delete delete : deletes) { delete(delete); } } @Override public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value, Delete delete) throws IOException { - if(check(row, family, qualifier, value)) { + if (check(row, family, qualifier, value)) { delete(delete); return true; } @@ -728,17 +728,17 @@ public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, lo @Override public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, Durability durability) throws IOException { - if(check(row, family, qualifier, null)) { + if (check(row, family, qualifier, null)) { Put put = new Put(row); put.add(family, qualifier, Bytes.toBytes(amount)); put(put); return amount; } long newValue = Bytes.toLong(data.get(row) - .get(family) - .get(qualifier) - .lastEntry() - .getValue()) + amount; + .get(family) + .get(qualifier) + .lastEntry() + .getValue()) + amount; data.get(row) .get(family) .get(qualifier) @@ -770,17 +770,17 @@ public void coprocessorService(Class service, byte[] s @Override public Object[] batch(List actions) throws IOException, InterruptedException { List results = new ArrayList(); - for(Row r : actions) { - if(r instanceof Delete) { - delete((Delete)r); + for (Row r : actions) { + if (r instanceof Delete) { + delete((Delete) r); continue; } - if(r instanceof Put) { - put((Put)r); + if (r instanceof Put) { + put((Put) r); continue; } - if(r instanceof Get) { - results.add(get((Get)r)); + if (r instanceof Get) { + results.add(get((Get) r)); } } return results.toArray(); @@ -805,7 +805,7 @@ public void batch(List actions, Object[] results) throws IOExcept @Override public Result[] get(List gets) throws IOException { List results = new ArrayList(); - for(Get g : gets) { + for (Get g : gets) { results.add(get(g)); } return results.toArray(new Result[results.size()]); @@ -815,10 +815,10 @@ public Result[] get(List gets) throws IOException { public Result increment(Increment increment) throws IOException { List kvs = new ArrayList(); NavigableMap> famToVal = increment.getFamilyMap(); - for(Entry> ef : famToVal.entrySet()) { + for (Entry> ef : famToVal.entrySet()) { byte[] family = ef.getKey(); List qToVal = ef.getValue(); - for(KeyValue eq : qToVal) { + for (KeyValue eq : qToVal) { incrementColumnValue(increment.getRow(), family, eq.getKey(), eq.getValueLength()); kvs.add(new KeyValue(increment.getRow(), family, eq.getKey(), eq.getValue())); } @@ -835,14 +835,14 @@ public Result increment(Increment increment) throws IOException { */ public byte[] read(String rowid, String column) { NavigableMap>> row = data.get(Bytes.toBytesBinary(rowid)); - if(row == null) + if (row == null) return null; String[] fq = split(column); byte[] family = Bytes.toBytesBinary(fq[0]); byte[] qualifier = Bytes.toBytesBinary(fq[1]); - if(!row.containsKey(family)) + if (!row.containsKey(family)) return null; - if(!row.get(family) + if (!row.get(family) .containsKey(qualifier)) return null; return row.get(family) diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/TestUtils.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/TestUtils.java index 3662a6510..00f05bbd1 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/TestUtils.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/TestUtils.java @@ -76,7 +76,7 @@ public static DataStore getDataStore() throws FoxtrotException { doReturn(new HbaseConfig()).when(tableConnection) .getHbaseConfig(); HBaseDataStore hBaseDataStore = new HBaseDataStore(tableConnection, new ObjectMapper(), - new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV2()) + new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV2()) ); hBaseDataStore = spy(hBaseDataStore); return hBaseDataStore; @@ -84,8 +84,8 @@ public static DataStore getDataStore() throws FoxtrotException { public static Document getDocument(String id, long timestamp, Object[] args, ObjectMapper mapper) { Map data = Maps.newHashMap(); - for(int i = 0; i < args.length; i += 2) { - data.put((String)args[i], args[i + 1]); + for (int i = 0; i < args.length; i += 2) { + data.put((String) args[i], args[i + 1]); } return new Document(id, timestamp, mapper.valueToTree(data)); } @@ -93,20 +93,20 @@ public static Document getDocument(String id, long timestamp, Object[] args, Obj public static void registerActions(AnalyticsLoader analyticsLoader, ObjectMapper mapper) throws Exception { Reflections reflections = new Reflections("com.flipkart.foxtrot", new SubTypesScanner()); Set> actions = reflections.getSubTypesOf(Action.class); - if(actions.isEmpty()) { + if (actions.isEmpty()) { throw new Exception("No analytics actions found!!"); } List types = new Vector<>(); - for(Class action : actions) { + for (Class action : actions) { AnalyticsProvider analyticsProvider = action.getAnnotation(AnalyticsProvider.class); final String opcode = analyticsProvider.opcode(); - if(Strings.isNullOrEmpty(opcode)) { + if (Strings.isNullOrEmpty(opcode)) { throw new Exception("Invalid annotation on " + action.getCanonicalName()); } analyticsLoader.register( new ActionMetadata(analyticsProvider.request(), action, analyticsProvider.cacheable()), analyticsProvider.opcode()); - if(analyticsProvider.cacheable()) { + if (analyticsProvider.cacheable()) { analyticsLoader.registerCache(opcode); } types.add(new NamedType(analyticsProvider.request(), opcode)); @@ -156,44 +156,44 @@ public static List getGroupDocuments(ObjectMapper mapper) { List documents = new Vector(); documents.add( TestUtils.getDocument("Z", 1397658117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 24}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1397658117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 48}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("X", 1397658117000L, new Object[]{"os", "android", "version", 3, "device", "galaxy", "battery", 74}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("W", 1397658117000L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 99}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 3, "device", "nexus", "battery", 87}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("B", 1397658118001L, new Object[]{"os", "android", "version", 2, "device", "galaxy", "battery", 76}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("C", 1397658118002L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 78}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("D", 1397658118003L, new Object[]{"os", "ios", "version", 1, "device", "iphone", "battery", 24}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 56}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("F", 1397658118005L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 35}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("G", 1397658118006L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 44}, - mapper - )); + mapper + )); return documents; } @@ -205,10 +205,10 @@ public static List getGroupDocumentsForEstimation(ObjectMapper mapper) .timestamp(i * 10_000 + 1397658117000L) .data(mapper.valueToTree(ImmutableMap.builder().put("deviceId", UUID.randomUUID() .toString()) - .put("os", new String[]{"ios", "android", "android", "android"}[random.nextInt(4)]) - .put("registered", new boolean[]{true, false, false}[random.nextInt(3)]) - .put("value", random.nextInt(101)) - .build())) + .put("os", new String[]{"ios", "android", "android", "android"}[random.nextInt(4)]) + .put("registered", new boolean[]{true, false, false}[random.nextInt(3)]) + .put("value", random.nextInt(101)) + .build())) .build()) .collect(Collectors.toList()); } @@ -217,44 +217,44 @@ public static List getHistogramDocuments(ObjectMapper mapper) { List documents = new Vector(); documents.add( TestUtils.getDocument("Z", 1397658117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 24}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1397651117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 48}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("X", 1397651117000L, new Object[]{"os", "android", "version", 3, "device", "galaxy", "battery", 74}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("W", 1397658117000L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 99}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 3, "device", "nexus", "battery", 87}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("B", 1397658218001L, new Object[]{"os", "android", "version", 2, "device", "galaxy", "battery", 76}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("C", 1398658218002L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 78}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("D", 1397758218003L, new Object[]{"os", "ios", "version", 1, "device", "iphone", "battery", 24}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("E", 1397958118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 56}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("F", 1398653118005L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 35}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("G", 1398653118006L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 44}, - mapper - )); + mapper + )); return documents; } @@ -262,44 +262,44 @@ public static List getTrendDocuments(ObjectMapper mapper) { List documents = new Vector(); documents.add( TestUtils.getDocument("Z", 1397658117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 24}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1397651117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 48}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("X", 1397651117000L, new Object[]{"os", "android", "version", 3, "device", "galaxy", "battery", 74}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("W", 1397658117000L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 99}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 3, "device", "nexus", "battery", 87}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("B", 1397658218001L, new Object[]{"os", "android", "version", 2, "device", "galaxy", "battery", 76}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("C", 1398658218002L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 78}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("D", 1397758218003L, new Object[]{"os", "ios", "version", 1, "device", "iphone", "battery", 24}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("E", 1397958118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 56}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("F", 1398653118005L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 35}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("G", 1398653118006L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 44}, - mapper - )); + mapper + )); return documents; } @@ -307,21 +307,21 @@ public static List getStatsDocuments(ObjectMapper mapper) { List documents = Lists.newArrayList(); documents.add( TestUtils.getDocument("Z", 1467282856000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 10}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1467331200000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 20}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("X", 1467417600000L, new Object[]{"os", "ios", "version", 3, "device", "galaxy", "battery", 30}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("W", 1467504000000L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 40}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("A", 1467590400000L, new Object[]{"os", "wp", "version", 3, "device", "nexus", "battery", 50}, - mapper - )); + mapper + )); return documents; } @@ -329,21 +329,21 @@ public static List getStatsTrendDocuments(ObjectMapper mapper) { List documents = Lists.newArrayList(); documents.add( TestUtils.getDocument("Z", 1467282856000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 24}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1467331200000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 48}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("X", 1467417600000L, new Object[]{"os", "ios", "version", 3, "device", "galaxy", "battery", 74}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("W", 1467504000000L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 99}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("A", 1467590400000L, new Object[]{"os", "wp", "version", 3, "device", "nexus", "battery", 87}, - mapper - )); + mapper + )); return documents; } @@ -351,44 +351,44 @@ public static List getCountDocuments(ObjectMapper mapper) { List documents = new Vector(); documents.add( TestUtils.getDocument("Z", 1397658117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 24}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1397651117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 48}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("X", 1397651117000L, new Object[]{"os", "android", "version", 3, "device", "galaxy", "battery", 74}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("W", 1397658117000L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 99}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 3, "device", "nexus", "battery", 87}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("B", 1397658218001L, new Object[]{"os", "android", "version", 2, "device", "galaxy", "battery", 76}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("C", 1398658218002L, new Object[]{"os", "android", "version", 2, "device", "nexus", "battery", 78}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("D", 1397758218003L, new Object[]{"os", "ios", "version", 1, "device", "iphone", "battery", 24}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("E", 1397958118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 56}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("F", 1398653118005L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 35}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("G", 1398653118006L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 44}, - mapper - )); + mapper + )); return documents; } @@ -396,28 +396,28 @@ public static List getDistinctDocuments(ObjectMapper mapper) { List documents = new Vector(); documents.add( TestUtils.getDocument("Z", 1397658117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 24}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("Y", 1397651117000L, new Object[]{"os", "android", "version", 1, "device", "nexus", "battery", 48}, - mapper - )); + mapper + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 3, "device", "nexus", "battery", 87}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("D", 1397758218003L, new Object[]{"os", "ios", "version", 1, "device", "iphone", "battery", 24}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("E", 1397958118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 56}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("F", 1398653118005L, new Object[]{"os", "ios", "version", 2, "device", "nexus", "battery", 35}, - mapper - )); + mapper + )); documents.add(TestUtils.getDocument("G", 1398653118006L, new Object[]{"os", "ios", "version", 2, "device", "ipad", "battery", 44}, - mapper - )); + mapper + )); return documents; } @@ -495,7 +495,7 @@ public static void ensureIndex(ElasticsearchConnection connection, final String .indices() .exists(new GetIndexRequest(table), RequestOptions.DEFAULT); - if(!exists) { + if (!exists) { Settings indexSettings = Settings.builder() .put("number_of_replicas", 0) .build(); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/alerts/EmailBuilderTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/alerts/EmailBuilderTest.java index fc9029742..f4e8b66d3 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/alerts/EmailBuilderTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/alerts/EmailBuilderTest.java @@ -22,7 +22,7 @@ public class EmailBuilderTest { @Test public void testCardinalityEmailBuild() throws JsonProcessingException { EmailBuilder emailBuilder = new EmailBuilder(new RichEmailBuilder(new StrSubstitutorEmailSubjectBuilder(), - new StrSubstitutorEmailBodyBuilder())); + new StrSubstitutorEmailBodyBuilder())); GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Lists.newArrayList("os", "deviceId")); @@ -30,11 +30,11 @@ public void testCardinalityEmailBuild() throws JsonProcessingException { QueryProcessingError error = new QueryProcessingError( groupRequest, new CardinalityOverflowException(groupRequest, - Jackson.newObjectMapper() - .writeValueAsString( - groupRequest), - "deviceId", - 0.75)); + Jackson.newObjectMapper() + .writeValueAsString( + groupRequest), + "deviceId", + 0.75)); final Email email = emailBuilder.visit(error); Assert.assertEquals("Blocked query as it might have screwed up the cluster", email.getSubject()); Assert.assertEquals( diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/cacheable/DummyCacheableAction.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/cacheable/DummyCacheableAction.java index 14eed3666..9e5212581 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/cacheable/DummyCacheableAction.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/cacheable/DummyCacheableAction.java @@ -32,7 +32,7 @@ */ @VisibleForTesting @AnalyticsProvider(opcode = "cache-hit-test", request = DummyCacheableActionRequest.class, response = DummyCacheableActionResponse.class, - cacheable = false) + cacheable = false) public class DummyCacheableAction extends Action { public DummyCacheableAction(DummyCacheableActionRequest parameter, AnalyticsLoader analyticsLoader) { diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/noncacheable/NonCacheableAction.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/noncacheable/NonCacheableAction.java index 690f3feea..6066b5f24 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/noncacheable/NonCacheableAction.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/common/noncacheable/NonCacheableAction.java @@ -32,7 +32,7 @@ */ @VisibleForTesting @AnalyticsProvider(opcode = "no-cache-test", request = NonCacheableActionRequest.class, response = NonCacheableActionResponse.class, - cacheable = false) + cacheable = false) public class NonCacheableAction extends Action { public NonCacheableAction(NonCacheableActionRequest parameter, AnalyticsLoader analyticsLoader) { diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStoreTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStoreTest.java index 74f2006ff..62dc6d64a 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStoreTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/HBaseDataStoreTest.java @@ -69,7 +69,7 @@ public void setUp() throws Exception { when(hbaseTableConnection.getTable(Matchers.
any())).thenReturn(tableInterface); when(hbaseTableConnection.getHbaseConfig()).thenReturn(new HbaseConfig()); hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, - new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()) + new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()) ); } @@ -80,7 +80,7 @@ public void testSaveSingle() throws Exception { hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, documentTranslator); Document expectedDocument = new Document(); expectedDocument.setId(UUID.randomUUID() - .toString()); + .toString()); expectedDocument.setTimestamp(System.currentTimeMillis()); JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); expectedDocument.setData(data); @@ -92,14 +92,14 @@ public void testSaveSingle() throws Exception { hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, documentTranslator); expectedDocument = new Document(); expectedDocument.setId(UUID.randomUUID() - .toString()); + .toString()); expectedDocument.setTimestamp(System.currentTimeMillis()); data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); expectedDocument.setData(data); hbaseDataStore.save(TEST_APP, expectedDocument); validateSave(documentTranslator.translate(TEST_APP, expectedDocument) - .getMetadata() - .getRawStorageId(), expectedDocument); + .getMetadata() + .getRawStorageId(), expectedDocument); } @Test @@ -128,7 +128,7 @@ public void testSaveSingleNullId() throws Exception { @Test public void testSaveSingleNullData() throws Exception { Document document = new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), null); + .toString(), System.currentTimeMillis(), null); try { hbaseDataStore.save(TEST_APP, document); fail(); @@ -140,8 +140,8 @@ public void testSaveSingleNullData() throws Exception { @Test public void testSaveSingleHBaseWriteException() throws Exception { Document document = new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), - mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) + .toString(), System.currentTimeMillis(), + mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) ); doThrow(new IOException()).when(tableInterface) .put(Matchers.any()); @@ -156,8 +156,8 @@ public void testSaveSingleHBaseWriteException() throws Exception { @Test(expected = StoreConnectionException.class) public void testSaveSingleHBaseCloseException() throws Exception { Document document = new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), - mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) + .toString(), System.currentTimeMillis(), + mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) ); doThrow(new IOException()).when(tableInterface) .close(); @@ -170,11 +170,11 @@ public void testSaveBulk() throws Exception { hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, documentTranslator); List documents = Lists.newArrayList(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { documents.add(createDummyDocument()); } hbaseDataStore.saveAll(TEST_APP, documents); - for(Document document : documents) { + for (Document document : documents) { validateSave(v1FormatKey(document.getId()), document); } } @@ -182,7 +182,7 @@ public void testSaveBulk() throws Exception { @Test public void testSaveBulkNullDocuments() throws Exception { List documents = new Vector(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { documents.add(null); } try { @@ -207,9 +207,9 @@ public void testSaveBulkNullIdList() throws Exception { @Test public void testSaveBulkNullId() throws Exception { List documents = new Vector(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { documents.add(new Document(null, System.currentTimeMillis(), - mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) + mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) )); } try { @@ -223,9 +223,9 @@ public void testSaveBulkNullId() throws Exception { @Test public void testSaveBulkNullData() throws Exception { List documents = new Vector(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { documents.add(new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), null)); + .toString(), System.currentTimeMillis(), null)); } try { hbaseDataStore.saveAll(TEST_APP, documents); @@ -239,8 +239,8 @@ public void testSaveBulkNullData() throws Exception { public void testSaveBulkHBaseWriteException() throws Exception { List documents = new Vector(); documents.add(new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), - mapper.valueToTree(Collections.singletonMap("TEST", "TEST")) + .toString(), System.currentTimeMillis(), + mapper.valueToTree(Collections.singletonMap("TEST", "TEST")) )); doThrow(new IOException()).when(tableInterface) .put(Matchers.anyListOf(Put.class)); @@ -256,8 +256,8 @@ public void testSaveBulkHBaseWriteException() throws Exception { public void testSaveBulkHBaseCloseException() throws Exception { List documents = new Vector(); documents.add(new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), - mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_SAVE_TEST")) + .toString(), System.currentTimeMillis(), + mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_SAVE_TEST")) )); doThrow(new IOException()).when(tableInterface) .close(); @@ -269,7 +269,7 @@ public void validateSave(String id, Document expectedDocument) throws Exception Result result = tableInterface.get(get); assertNotNull("Get for Id should not be null", result); Document actualDocument = new Document(expectedDocument.getId(), expectedDocument.getTimestamp(), - mapper.readTree(result.getValue(COLUMN_FAMILY, DATA_FIELD_NAME)) + mapper.readTree(result.getValue(COLUMN_FAMILY, DATA_FIELD_NAME)) ); compare(expectedDocument, actualDocument); } @@ -283,7 +283,7 @@ public void testGetSingle() throws Exception { // rawKeyVersion 1.0 with no metadata stored in the system (This will happen for documents which were indexed // before rawKey versioning came into place) hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, - new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()) + new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()) ); String id = UUID.randomUUID() @@ -292,15 +292,15 @@ public void testGetSingle() throws Exception { JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); Document expectedDocument = new Document(id, timestamp, data); tableInterface.put(new Put(Bytes.toBytes(v1FormatKey(id))).addColumn(COLUMN_FAMILY, Bytes.toBytes("data"), - mapper.writeValueAsBytes(data) - ) - .addColumn(COLUMN_FAMILY, Bytes.toBytes("timestamp"), Bytes.toBytes(timestamp))); + mapper.writeValueAsBytes(data) + ) + .addColumn(COLUMN_FAMILY, Bytes.toBytes("timestamp"), Bytes.toBytes(timestamp))); Document actualDocument = hbaseDataStore.get(TEST_APP, id); compare(expectedDocument, actualDocument); // rawKeyVersion 1.0 hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, - new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()) + new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()) ); id = UUID.randomUUID() @@ -348,7 +348,7 @@ public void testGetSingleHBaseReadException() throws Exception { JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); Document expectedDocument = new Document(id, System.currentTimeMillis(), - new DocumentMetadata(id, v1FormatKey(id), System.currentTimeMillis()), data + new DocumentMetadata(id, v1FormatKey(id), System.currentTimeMillis()), data ); tableInterface.put(hbaseDataStore.getPutForDocument(expectedDocument)); doThrow(new IOException()).when(tableInterface) @@ -367,7 +367,7 @@ public void testGetSingleHBaseCloseException() throws Exception { originalDocument.setMetadata( new DocumentMetadata(originalDocument.getId(), v1FormatKey(originalDocument.getId()), System.currentTimeMillis())); Document expectedDocument = new Document(v1FormatKey(originalDocument.getId()), originalDocument.getTimestamp(), - originalDocument.getMetadata(), originalDocument.getData() + originalDocument.getMetadata(), originalDocument.getData() ); tableInterface.put(hbaseDataStore.getPutForDocument(expectedDocument)); @@ -382,7 +382,7 @@ public void testV1GetBulk() throws Exception { List ids = new Vector<>(); List putList = new Vector<>(); HashMap actualIdValues = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { String id = UUID.randomUUID() .toString(); long timestamp = System.currentTimeMillis(); @@ -395,11 +395,11 @@ public void testV1GetBulk() throws Exception { } tableInterface.put(putList); List actualDocuments = hbaseDataStore.getAll(TEST_APP, ids); - for(Document doc : actualDocuments) { + for (Document doc : actualDocuments) { actualIdValues.put(doc.getId(), doc); } assertNotNull("List of returned Documents should not be null", actualDocuments); - for(String id : ids) { + for (String id : ids) { assertTrue("Requested Id should be present in response", actualIdValues.containsKey(id)); compare(idValues.get(id), actualIdValues.get(id)); } @@ -415,7 +415,7 @@ public void testV2GetBulk() throws Exception { List putList = Lists.newArrayList(); HashMap actualIdValues = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Document document = createDummyDocument(); ids.add(document.getId()); idValues.put(document.getId(), document); @@ -427,11 +427,11 @@ public void testV2GetBulk() throws Exception { } tableInterface.put(putList); List actualDocuments = hbaseDataStore.getAll(TEST_APP, rawIds); - for(Document doc : actualDocuments) { + for (Document doc : actualDocuments) { actualIdValues.put(doc.getId(), doc); } assertNotNull("List of returned Documents should not be null", actualDocuments); - for(String id : ids) { + for (String id : ids) { assertTrue("Requested Id should be present in response", actualIdValues.containsKey(id)); compare(idValues.get(id), actualIdValues.get(id)); } @@ -450,9 +450,9 @@ public void testGetBulkNullIdList() throws Exception { @Test public void testGetBulkMissingDocument() throws Exception { List ids = new Vector(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { ids.add(UUID.randomUUID() - .toString()); + .toString()); } try { hbaseDataStore.getAll(TEST_APP, ids); @@ -466,12 +466,12 @@ public void testGetBulkMissingDocument() throws Exception { public void testGetBulkHBaseReadException() throws Exception { List ids = new ArrayList<>(); List putList = new ArrayList<>(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { String id = UUID.randomUUID() .toString(); Document document = new Document(id, System.currentTimeMillis(), - new DocumentMetadata(id, String.format("row:%d", i), System.currentTimeMillis()), - mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_GET_TEST")) + new DocumentMetadata(id, String.format("row:%d", i), System.currentTimeMillis()), + mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_GET_TEST")) ); putList.add(hbaseDataStore.getPutForDocument(document)); } @@ -490,12 +490,12 @@ public void testGetBulkHBaseReadException() throws Exception { public void testGetBulkHBaseCloseException() throws Exception { List ids = new Vector<>(); List putList = new Vector<>(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { String id = UUID.randomUUID() .toString(); Document document = new Document(id, System.currentTimeMillis(), - new DocumentMetadata(id, String.format("row:%d", i), System.currentTimeMillis()), - mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_GET_TEST")) + new DocumentMetadata(id, String.format("row:%d", i), System.currentTimeMillis()), + mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_GET_TEST")) ); putList.add(hbaseDataStore.getPutForDocument(document)); } @@ -521,7 +521,7 @@ public void compare(Document expected, Document actual) throws Exception { private Document createDummyDocument() { Document document = new Document(); document.setId(UUID.randomUUID() - .toString()); + .toString()); document.setTimestamp(System.currentTimeMillis()); JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); document.setData(data); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/SeggregatedHBaseDataStoreTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/SeggregatedHBaseDataStoreTest.java index 3f6239238..e8da01b93 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/SeggregatedHBaseDataStoreTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/datastore/impl/hbase/SeggregatedHBaseDataStoreTest.java @@ -46,7 +46,7 @@ public void testPrefixedSegTable() { hbaseConfig.setSeggregatedTablePrefix("foxtrot-"); Table table = new Table(TestUtils.TEST_TABLE_NAME, 7, true, 1); Assert.assertEquals(String.format("%s%s", hbaseConfig.getSeggregatedTablePrefix(), TestUtils.TEST_TABLE_NAME), - TableUtil.getTableName(hbaseConfig, table) - ); + TableUtil.getTableName(hbaseConfig, table) + ); } } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/internalevents/GuavaInternalEventBusTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/internalevents/GuavaInternalEventBusTest.java index 30e8b23a9..7c7965860 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/internalevents/GuavaInternalEventBusTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/internalevents/GuavaInternalEventBusTest.java @@ -12,6 +12,15 @@ @Slf4j public class GuavaInternalEventBusTest { + @Test + public void testPubSub() { + InternalEventBus eventBus = new GuavaInternalEventBus(); + eventBus.subscribe(event -> { + Assert.assertEquals("Hello", ((GenericEvent) event).getData()); + }); + eventBus.publish(new GenericEvent("Hello")); + } + @Data private class GenericEvent implements InternalSystemEvent { private final String data; @@ -22,13 +31,4 @@ public T accept(InternalSystemEventVisitor visitor) { } } - @Test - public void testPubSub() { - InternalEventBus eventBus = new GuavaInternalEventBus(); - eventBus.subscribe(event -> { - Assert.assertEquals("Hello", ((GenericEvent)event).getData()); - }); - eventBus.publish(new GenericEvent("Hello")); - } - } \ No newline at end of file diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/ActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/ActionTest.java index 2b072a85b..f534dc986 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/ActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/ActionTest.java @@ -45,11 +45,6 @@ */ public abstract class ActionTest { - static { - Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - root.setLevel(Level.WARN); - } - private static HazelcastInstance hazelcastInstance; @Getter private static ElasticsearchConnection elasticsearchConnection; @@ -64,6 +59,11 @@ public abstract class ActionTest { @Getter private static CacheManager cacheManager; + static { + Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + root.setLevel(Level.WARN); + } + @BeforeClass public static void setupClass() throws Exception { hazelcastInstance = new TestHazelcastInstanceFactory(1).newHazelcastInstance(new Config()); @@ -84,11 +84,11 @@ public static void setupClass() throws Exception { tableMetadataManager.start(); tableMetadataManager.save(Table.builder() - .name(TestUtils.TEST_TABLE_NAME) - .ttl(30) - .build()); + .name(TestUtils.TEST_TABLE_NAME) + .ttl(30) + .build()); List mutators = Lists.newArrayList(new LargeTextNodeRemover(mapper, - TextNodeRemoverConfiguration.builder().build())); + TextNodeRemoverConfiguration.builder().build())); DataStore dataStore = TestUtils.getDataStore(); queryStore = new ElasticsearchQueryStore(tableMetadataManager, elasticsearchConnection, dataStore, mutators, mapper, cardinalityConfig); cacheManager = new CacheManager(new DistributedCacheFactory(hazelcastConnection, mapper, new CacheConfig())); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/FilterActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/FilterActionTest.java index 507b68537..8dcd61dfc 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/FilterActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/FilterActionTest.java @@ -92,22 +92,22 @@ public void testQueryNoFilterAscending() throws FoxtrotException { documents.add( TestUtils.getDocument("Z", 1397658117004L, new Object[]{"os", "android", "device", "nexus", "battery", 24}, getMapper())); documents.add(TestUtils.getDocument("A", 1397658118000L, - new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper() + )); documents.add(TestUtils.getDocument("B", 1397658118001L, - new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() + )); documents.add(TestUtils.getDocument("C", 1397658118002L, - new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() + )); documents.add( TestUtils.getDocument("D", 1397658118003L, new Object[]{"os", "ios", "version", 1, "device", "iphone"}, - getMapper() - )); + getMapper() + )); documents.add( TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad"}, - getMapper() - )); + getMapper() + )); QueryResponse actualResponse = QueryResponse.class.cast(getQueryExecutor().execute(query)); compare(documents, actualResponse.getDocuments()); @@ -125,18 +125,18 @@ public void testQueryNoFilterDescending() throws FoxtrotException, JsonProcessin List documents = new ArrayList(); documents.add( TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad"}, - getMapper() - )); + getMapper() + )); documents.add( TestUtils.getDocument("D", 1397658118003L, new Object[]{"os", "ios", "version", 1, "device", "iphone"}, - getMapper() - )); + getMapper() + )); documents.add(TestUtils.getDocument("C", 1397658118002L, - new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() + )); documents.add(TestUtils.getDocument("B", 1397658118001L, - new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper())); documents.add( @@ -205,18 +205,18 @@ public void testQueryAnyFilter() throws FoxtrotException, JsonProcessingExceptio List documents = new ArrayList(); documents.add( TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad"}, - getMapper() - )); + getMapper() + )); documents.add( TestUtils.getDocument("D", 1397658118003L, new Object[]{"os", "ios", "version", 1, "device", "iphone"}, - getMapper() - )); + getMapper() + )); documents.add(TestUtils.getDocument("C", 1397658118002L, - new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() + )); documents.add(TestUtils.getDocument("B", 1397658118001L, - new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper())); documents.add( @@ -663,27 +663,27 @@ public void testQueryNullFilters() throws FoxtrotException, JsonProcessingExcept List documents = new ArrayList(); documents.add( TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad"}, - getMapper() - )); + getMapper() + )); documents.add( TestUtils.getDocument("D", 1397658118003L, new Object[]{"os", "ios", "version", 1, "device", "iphone"}, - getMapper() - )); + getMapper() + )); documents.add(TestUtils.getDocument("C", 1397658118002L, - new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() + )); documents.add(TestUtils.getDocument("B", 1397658118001L, - new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() + )); documents.add(TestUtils.getDocument("A", 1397658118000L, - new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper() + )); documents.add(TestUtils.getDocument("Z", 1397658117004L, - new Object[]{"os", "android", "device", "nexus", "battery", 24}, getMapper() - )); + new Object[]{"os", "android", "device", "nexus", "battery", 24}, getMapper() + )); documents.add(TestUtils.getDocument("Y", 1397658117003L, - new Object[]{"os", "android", "device", "nexus", "battery", 48}, getMapper() - )); + new Object[]{"os", "android", "device", "nexus", "battery", 48}, getMapper() + )); documents.add( TestUtils.getDocument("X", 1397658117002L, new Object[]{"os", "android", "device", "nexus", "battery", 74}, getMapper())); documents.add( @@ -706,18 +706,18 @@ public void testQueryNullCombiner() throws FoxtrotException, JsonProcessingExcep List documents = new ArrayList(); documents.add( TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad"}, - getMapper() - )); + getMapper() + )); documents.add( TestUtils.getDocument("D", 1397658118003L, new Object[]{"os", "ios", "version", 1, "device", "iphone"}, - getMapper() - )); + getMapper() + )); documents.add(TestUtils.getDocument("C", 1397658118002L, - new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() - )); + new Object[]{"os", "android", "version", 2, "device", "nexus"}, getMapper() + )); documents.add(TestUtils.getDocument("B", 1397658118001L, - new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() - )); + new Object[]{"os", "android", "version", 1, "device", "galaxy"}, getMapper() + )); documents.add( TestUtils.getDocument("A", 1397658118000L, new Object[]{"os", "android", "version", 1, "device", "nexus"}, getMapper())); documents.add( @@ -766,11 +766,11 @@ public void testMissingIndicesQuery() throws FoxtrotException { List documents = TestUtils.getQueryDocumentsDifferentDate(mapper, new Date(2014 - 1900, 4, 1).getTime()); documents.addAll(TestUtils.getQueryDocumentsDifferentDate(mapper, new Date(2014 - 1900, 4, 5).getTime())); getQueryStore().save(TestUtils.TEST_TABLE_NAME, documents); - for(Document document : documents) { + for (Document document : documents) { getElasticsearchConnection().getClient() .indices() .refresh(new RefreshRequest(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, document.getTimestamp())), - RequestOptions.DEFAULT); + RequestOptions.DEFAULT); } GetIndexResponse response = getElasticsearchConnection().getClient() .indices() @@ -786,9 +786,9 @@ public void testMissingIndicesQuery() throws FoxtrotException { BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setField("_timestamp"); betweenFilter.setFrom(documents.get(0) - .getTimestamp()); + .getTimestamp()); betweenFilter.setTo(documents.get(documents.size() - 1) - .getTimestamp()); + .getTimestamp()); betweenFilter.setTemporal(true); query.setFilters(Lists.newArrayList(betweenFilter)); QueryResponse actualResponse = QueryResponse.class.cast(getQueryExecutor().execute(query)); @@ -823,7 +823,7 @@ public void testScrollResponse() throws FoxtrotException, JsonProcessingExceptio public void compare(List expectedDocuments, List actualDocuments) { assertEquals(expectedDocuments.size(), actualDocuments.size()); - for(int i = 0; i < expectedDocuments.size(); i++) { + for (int i = 0; i < expectedDocuments.size(); i++) { Document expected = expectedDocuments.get(i); Document actual = actualDocuments.get(i); assertNotNull(expected); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionCardinalityTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionCardinalityTest.java index bd0b6b1f1..901dc3cad 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionCardinalityTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionCardinalityTest.java @@ -33,7 +33,7 @@ public static void setUp() throws Exception { .refresh(new RefreshRequest("*"), RequestOptions.DEFAULT); getTableMetadataManager().getFieldMappings( TestUtils.TEST_TABLE_NAME, true, true, 1397658117000L); - ((ElasticsearchQueryStore)getQueryStore()).getCardinalityConfig() + ((ElasticsearchQueryStore) getQueryStore()).getCardinalityConfig() .setMaxCardinality(15000); getTableMetadataManager().updateEstimationData(TestUtils.TEST_TABLE_NAME, 1397658117000L); } @@ -46,9 +46,9 @@ public void testEstimationWithMultipleNestingHighCardinality() throws Exception GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); Assert.assertTrue(response.getResult() - .containsKey("android")); + .containsKey("android")); Assert.assertTrue(response.getResult() - .containsKey("ios")); + .containsKey("ios")); } @Test @@ -60,9 +60,9 @@ public void testEstimationWithMultipleNesting() throws Exception { GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); Assert.assertTrue(response.getResult() - .containsKey("android")); + .containsKey("android")); Assert.assertTrue(response.getResult() - .containsKey("ios")); + .containsKey("ios")); } @Test @@ -73,7 +73,7 @@ public void testEstimationBooleanCardinality() throws Exception { GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); Assert.assertTrue(response.getResult() - .containsKey("0")); + .containsKey("0")); } @@ -85,7 +85,7 @@ public void testEstimationPercentileCardinality() throws Exception { GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); Assert.assertTrue(response.getResult() - .containsKey("0")); + .containsKey("0")); } } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionEstimationTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionEstimationTest.java index 02db48a33..5a187bc26 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionEstimationTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionEstimationTest.java @@ -35,7 +35,7 @@ public static void setUp() throws Exception { .indices() .refresh(new RefreshRequest("*"), RequestOptions.DEFAULT); getTableMetadataManager().getFieldMappings(TestUtils.TEST_TABLE_NAME, true, true, 1397658117000L); - ((ElasticsearchQueryStore)getQueryStore()).getCardinalityConfig() + ((ElasticsearchQueryStore) getQueryStore()).getCardinalityConfig() .setMaxCardinality(15000); getTableMetadataManager().updateEstimationData(TestUtils.TEST_TABLE_NAME, 1397658117000L); } @@ -49,9 +49,9 @@ public void testEstimationNoFilter() throws Exception { GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); Assert.assertTrue(response.getResult() - .containsKey("android")); + .containsKey("android")); Assert.assertTrue(response.getResult() - .containsKey("ios")); + .containsKey("ios")); } @@ -72,17 +72,17 @@ public void testEstimationTemporalFilterHighCardinality() throws Exception { groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("deviceId")); groupRequest.setFilters(ImmutableList.of(BetweenFilter.builder() - .field("_timestamp") - .temporal(true) - .from(1397658117000L) - .to(1397658117000L + 2 * 60000) - .build())); + .field("_timestamp") + .temporal(true) + .from(1397658117000L) + .to(1397658117000L + 2 * 60000) + .build())); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(groupRequest)); + .writeValueAsString(groupRequest)); GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(response)); + .writeValueAsString(response)); Assert.assertFalse(response.getResult().isEmpty()); } @@ -94,17 +94,17 @@ public void testEstimationCardinalFilterHighCardinality() throws Exception { groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("deviceId")); groupRequest.setFilters(ImmutableList.of(EqualsFilter.builder() - .field("os") - .value("ios") - .build())); + .field("os") + .value("ios") + .build())); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(groupRequest)); + .writeValueAsString(groupRequest)); GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(response)); + .writeValueAsString(response)); Assert.assertFalse(response.getResult() - .isEmpty()); + .isEmpty()); } @Test(expected = CardinalityOverflowException.class) @@ -113,9 +113,9 @@ public void testEstimationGTFilterHighCardinality() throws Exception { groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("deviceId")); groupRequest.setFilters(ImmutableList.of(GreaterThanFilter.builder() - .field("value") - .value(10) - .build())); + .field("value") + .value(10) + .build())); getQueryExecutor().execute(groupRequest); } @@ -126,16 +126,16 @@ public void testEstimationLTFilterHighCardinality() throws Exception { groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("deviceId")); groupRequest.setFilters(ImmutableList.of(LessThanFilter.builder() - .field("value") - .value(30) - .build())); + .field("value") + .value(30) + .build())); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(groupRequest)); + .writeValueAsString(groupRequest)); GroupResponse response = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(response)); + .writeValueAsString(response)); Assert.assertFalse(response.getResult() - .isEmpty()); + .isEmpty()); } // High cardinality field queries with filters including small subset are allowed @@ -144,11 +144,11 @@ public void testEstimationLTFilterHighCardinalityBlocked() throws Exception { groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("deviceId")); groupRequest.setFilters(ImmutableList.of(LessThanFilter.builder() - .field("value") - .value(80) - .build())); + .field("value") + .value(80) + .build())); log.debug(getMapper().writerWithDefaultPrettyPrinter() - .writeValueAsString(groupRequest)); + .writeValueAsString(groupRequest)); getQueryExecutor().execute(groupRequest); } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionTest.java index b095179c9..d36ef61b2 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/GroupActionTest.java @@ -34,11 +34,13 @@ import com.google.common.collect.Maps; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.client.RequestOptions; + import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; + import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -331,11 +333,11 @@ public void testGroupActionMaxAggregation() { response.put("android", new HashMap() {{ put("1", 48.0); put("2", 99.0); - put("3",87.0); + put("3", 87.0); }}); response.put("ios", new HashMap() {{ put("1", 24.0); - put("2",56.0); + put("2", 56.0); }}); GroupResponse actualResult = (GroupResponse) getQueryExecutor().execute(groupRequest); @@ -399,9 +401,9 @@ public void testGroupActionSumAggregation() { Map response = Maps.newHashMap(); response.put("android", new HashMap() {{ - put("1",72.0); + put("1", 72.0); put("2", 253.0); - put("3",161.0); + put("3", 161.0); }}); response.put("ios", new HashMap() {{ put("1", 24.0); @@ -433,7 +435,7 @@ public void testGroupActionCountAggregation() { Map response = Maps.newHashMap(); response.put("android", new HashMap() {{ put("1", 2L); - put("2",3L); + put("2", 3L); put("3", 2L); }}); response.put("ios", new HashMap() {{ diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/HistogramActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/HistogramActionTest.java index 212a057dc..1883e4bc2 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/HistogramActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/HistogramActionTest.java @@ -79,7 +79,7 @@ public void testHistogramActionIntervalMinuteNoFilter() throws FoxtrotException, counts.add(new HistogramResponse.Count(1398653100000L, 2)); counts.add(new HistogramResponse.Count(1398658200000L, 1)); assertTrue(response.getCounts() - .equals(counts)); + .equals(counts)); } @Test @@ -155,7 +155,7 @@ public void testHistogramActionIntervalHourWithFilter() throws FoxtrotException, counts.add(new HistogramResponse.Count(1397957400000L, 1)); counts.add(new HistogramResponse.Count(1398655800000L, 1)); assertTrue(response.getCounts() - .equals(counts)); + .equals(counts)); } @Test @@ -201,6 +201,6 @@ public void testHistogramActionIntervalDayWithFilter() throws FoxtrotException, counts.add(new HistogramResponse.Count(1397932200000L, 1)); counts.add(new HistogramResponse.Count(1398623400000L, 1)); assertTrue(response.getCounts() - .equals(counts)); + .equals(counts)); } } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryActionTest.java index 988e3ebb2..b2d855e43 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiQueryActionTest.java @@ -72,14 +72,14 @@ public void testMultiQuery() throws FoxtrotException, JsonProcessingException { MultiQueryRequest multiQueryRequest = new MultiQueryRequest(requests); ActionResponse actionResponse = getQueryExecutor().execute(multiQueryRequest); MultiQueryResponse multiQueryResponse = null; - if(actionResponse instanceof MultiQueryResponse) { - multiQueryResponse = (MultiQueryResponse)actionResponse; + if (actionResponse instanceof MultiQueryResponse) { + multiQueryResponse = (MultiQueryResponse) actionResponse; } assertNotNull(multiQueryResponse); - QueryResponse queryResponse = (QueryResponse)multiQueryResponse.getResponses() + QueryResponse queryResponse = (QueryResponse) multiQueryResponse.getResponses() .get("1"); - CountResponse countResponse = (CountResponse)multiQueryResponse.getResponses() + CountResponse countResponse = (CountResponse) multiQueryResponse.getResponses() .get("2"); System.out.println(getMapper().writerWithDefaultPrettyPrinter().writeValueAsString(queryResponse)); @@ -109,14 +109,14 @@ public void testMultiQueryCommonFilters() throws FoxtrotException, JsonProcessin ActionResponse actionResponse = getQueryExecutor().execute(multiQueryRequest); MultiQueryResponse multiQueryResponse = null; - if(actionResponse instanceof MultiQueryResponse) { - multiQueryResponse = (MultiQueryResponse)actionResponse; + if (actionResponse instanceof MultiQueryResponse) { + multiQueryResponse = (MultiQueryResponse) actionResponse; } assertNotNull(multiQueryResponse); - QueryResponse queryResponse = (QueryResponse)multiQueryResponse.getResponses() + QueryResponse queryResponse = (QueryResponse) multiQueryResponse.getResponses() .get("1"); - CountResponse countResponse = (CountResponse)multiQueryResponse.getResponses() + CountResponse countResponse = (CountResponse) multiQueryResponse.getResponses() .get("2"); assertEquals(2, queryResponse.getDocuments().size()); @@ -157,15 +157,15 @@ public void testQueryNoFilterAscending() throws FoxtrotException, JsonProcessing documents.add(TestUtils.getDocument("E", 1397658118004L, new Object[]{"os", "ios", "version", 2, "device", "ipad"}, getMapper())); MultiQueryResponse multiQueryResponse = MultiQueryResponse.class.cast(getQueryExecutor().execute(multiQueryRequest)); - for(Map.Entry response : multiQueryResponse.getResponses() + for (Map.Entry response : multiQueryResponse.getResponses() .entrySet()) { - compare(documents, ((QueryResponse)response.getValue()).getDocuments()); + compare(documents, ((QueryResponse) response.getValue()).getDocuments()); } } public void compare(List expectedDocuments, List actualDocuments) { assertEquals(expectedDocuments.size(), actualDocuments.size()); - for(int i = 0; i < expectedDocuments.size(); i++) { + for (int i = 0; i < expectedDocuments.size(); i++) { Document expected = expectedDocuments.get(i); Document actual = actualDocuments.get(i); assertNotNull(expected); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryActionTest.java index 594263971..2289b34e4 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/MultiTimeQueryActionTest.java @@ -47,12 +47,12 @@ public void testMultiTimeQuery() throws FoxtrotException, JsonProcessingExceptio MultiTimeQueryRequest multiTimeQueryRequest = new MultiTimeQueryRequest(1, duration, query); ActionResponse actionResponse = getQueryExecutor().execute(multiTimeQueryRequest); MultiTimeQueryResponse multiTimeQueryResponse = null; - if(actionResponse instanceof MultiTimeQueryResponse) { - multiTimeQueryResponse = (MultiTimeQueryResponse)actionResponse; + if (actionResponse instanceof MultiTimeQueryResponse) { + multiTimeQueryResponse = (MultiTimeQueryResponse) actionResponse; } assertNotNull(multiTimeQueryResponse); - QueryResponse queryResponse = (QueryResponse)multiTimeQueryResponse.getResponses() + QueryResponse queryResponse = (QueryResponse) multiTimeQueryResponse.getResponses() .get("1397658117000"); assertEquals(9, queryResponse.getTotalHits()); @@ -93,16 +93,16 @@ public void testQueryNoFilterAscending() throws FoxtrotException, JsonProcessing MultiTimeQueryResponse multiTimeQueryResponse = MultiTimeQueryResponse.class.cast( getQueryExecutor().execute(multiTimeQueryRequest)); - for(String key : multiTimeQueryResponse.getResponses() + for (String key : multiTimeQueryResponse.getResponses() .keySet()) { - compare(documents, ((QueryResponse)multiTimeQueryResponse.getResponses() + compare(documents, ((QueryResponse) multiTimeQueryResponse.getResponses() .get(key)).getDocuments()); } } public void compare(List expectedDocuments, List actualDocuments) { assertEquals(expectedDocuments.size(), actualDocuments.size()); - for(int i = 0; i < expectedDocuments.size(); i++) { + for (int i = 0; i < expectedDocuments.size(); i++) { Document expected = expectedDocuments.get(i); Document actual = actualDocuments.get(i); assertNotNull(expected); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsActionTest.java index 3ace7662b..cc437ccd2 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsActionTest.java @@ -116,9 +116,9 @@ public void testStatsActionNoExtendedStat() throws FoxtrotException { request.setTable(TestUtils.TEST_TABLE_NAME); request.setField("battery"); request.setStats(EnumSet.allOf(Stat.class) - .stream() - .filter(x -> !x.isExtended()) - .collect(Collectors.toSet())); + .stream() + .filter(x -> !x.isExtended()) + .collect(Collectors.toSet())); StatsResponse statsResponse = StatsResponse.class.cast(getQueryExecutor().execute(request)); assertNotNull(statsResponse); @@ -142,8 +142,8 @@ public void testStatsActionOnlyCountStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsResponse.getResult() - .getStats() - .containsKey("count")); + .getStats() + .containsKey("count")); } @Test @@ -160,8 +160,8 @@ public void testStatsActionOnlyMaxStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsResponse.getResult() - .getStats() - .containsKey("max")); + .getStats() + .containsKey("max")); } @Test @@ -178,8 +178,8 @@ public void testStatsActionOnlyMinStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsResponse.getResult() - .getStats() - .containsKey("min")); + .getStats() + .containsKey("min")); } @@ -197,8 +197,8 @@ public void testStatsActionOnlyAvgStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsResponse.getResult() - .getStats() - .containsKey("avg")); + .getStats() + .containsKey("avg")); } @Test @@ -212,8 +212,8 @@ public void testStatsActionOnlySumStat() throws FoxtrotException { assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertTrue(statsResponse.getResult() - .getStats() - .containsKey("sum")); + .getStats() + .containsKey("sum")); } @Test @@ -230,15 +230,15 @@ public void testStatsActionOnlyOnePercentile() throws FoxtrotException { betweenFilter.setField("_timestamp"); request.setFilters(Collections.singletonList(betweenFilter)); - StatsResponse statsResponse = (StatsResponse)getQueryExecutor().execute(request); + StatsResponse statsResponse = (StatsResponse) getQueryExecutor().execute(request); assertNotNull(statsResponse); assertNotNull(statsResponse.getResult()); assertEquals(1, statsResponse.getResult() .getPercentiles() .size()); assertTrue(statsResponse.getResult() - .getPercentiles() - .containsKey(5d)); + .getPercentiles() + .containsKey(5d)); } @Test @@ -253,7 +253,7 @@ public void testStatsActionWithNesting() throws FoxtrotException { assertNotNull(statsResponse.getResult()); assertEquals(3, statsResponse.getBuckets() .size()); - for(BucketResponse bucketResponse : statsResponse.getBuckets()) { + for (BucketResponse bucketResponse : statsResponse.getBuckets()) { assertNotNull(bucketResponse.getResult()); } } @@ -270,7 +270,7 @@ public void testStatsActionWithMultiLevelNesting() throws FoxtrotException { assertNotNull(statsResponse.getResult()); assertEquals(3, statsResponse.getBuckets() .size()); - for(BucketResponse bucketResponse : statsResponse.getBuckets()) { + for (BucketResponse bucketResponse : statsResponse.getBuckets()) { assertNull(bucketResponse.getResult()); assertNotNull(bucketResponse.getBuckets()); } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendActionTest.java index 7e9c3e03e..39c925a90 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/StatsTrendActionTest.java @@ -125,9 +125,9 @@ public void testStatsTrendActionNoExtendedStat() throws FoxtrotException, JsonPr request.setTimestamp("_timestamp"); request.setField("battery"); request.setStats(EnumSet.allOf(Stat.class) - .stream() - .filter(x -> !x.isExtended()) - .collect(Collectors.toSet())); + .stream() + .filter(x -> !x.isExtended()) + .collect(Collectors.toSet())); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); @@ -177,9 +177,9 @@ public void testStatsTrendActionOnlyCountStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getStats() - .containsKey("count")); + .get(0) + .getStats() + .containsKey("count")); assertNull(statsTrendResponse.getBuckets()); } @@ -207,9 +207,9 @@ public void testStatsTrendActionOnlyMaxStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getStats() - .containsKey("max")); + .get(0) + .getStats() + .containsKey("max")); assertNull(statsTrendResponse.getBuckets()); } @@ -237,9 +237,9 @@ public void testStatsTrendActionOnlyMinStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getStats() - .containsKey("min")); + .get(0) + .getStats() + .containsKey("min")); assertNull(statsTrendResponse.getBuckets()); } @@ -267,9 +267,9 @@ public void testStatsTrendActionOnlyAvgStat() throws FoxtrotException { .getStats() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getStats() - .containsKey("avg")); + .get(0) + .getStats() + .containsKey("avg")); assertNull(statsTrendResponse.getBuckets()); } @@ -297,9 +297,9 @@ public void testStatsTrendActionOnlySumStat() throws FoxtrotException, JsonProce .getStats() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getStats() - .containsKey("sum")); + .get(0) + .getStats() + .containsKey("sum")); assertNull(statsTrendResponse.getBuckets()); } @@ -327,9 +327,9 @@ public void testStatsTrendActionOnlyOnePercentile() throws FoxtrotException { .getPercentiles() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getPercentiles() - .containsKey(5d)); + .get(0) + .getPercentiles() + .containsKey(5d)); } @Test @@ -390,12 +390,12 @@ public void testStatsTrendActionWithMultiLevelNesting() throws FoxtrotException, .getBuckets() .size()); assertNotNull(statsTrendResponse.getBuckets() - .get(0) - .getBuckets() - .get(0) - .getResult() - .get(0) - .getPercentiles()); + .get(0) + .getBuckets() + .get(0) + .getResult() + .get(0) + .getPercentiles()); } @Test @@ -417,10 +417,9 @@ public void testStatsTrendActionWithMultiLevelNestingSkipPercentile() throws Fox StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); try { System.out.println(Jackson.newObjectMapper() - .writerWithDefaultPrettyPrinter() - .writeValueAsString(statsTrendResponse)); - } - catch (JsonProcessingException e) { + .writerWithDefaultPrettyPrinter() + .writeValueAsString(statsTrendResponse)); + } catch (JsonProcessingException e) { e.printStackTrace(); } assertNotNull(statsTrendResponse); @@ -442,12 +441,12 @@ public void testStatsTrendActionWithMultiLevelNestingSkipPercentile() throws Fox .getBuckets() .size()); assertNull(statsTrendResponse.getBuckets() - .get(0) - .getBuckets() - .get(0) - .getResult() - .get(0) - .getPercentiles()); + .get(0) + .getBuckets() + .get(0) + .getResult() + .get(0) + .getPercentiles()); } @@ -507,9 +506,9 @@ public void testStatsTrendActionTextField() throws FoxtrotException { .getStats() .size()); assertTrue(statsTrendResponse.getResult() - .get(0) - .getStats() - .containsKey("count")); + .get(0) + .getStats() + .containsKey("count")); assertNull(statsTrendResponse.getBuckets()); } @@ -532,10 +531,9 @@ public void testStatsTrendActionTextFieldNested() throws FoxtrotException { StatsTrendResponse statsTrendResponse = StatsTrendResponse.class.cast(getQueryExecutor().execute(request)); try { System.out.println(Jackson.newObjectMapper() - .writerWithDefaultPrettyPrinter() - .writeValueAsString(statsTrendResponse)); - } - catch (JsonProcessingException e) { + .writerWithDefaultPrettyPrinter() + .writeValueAsString(statsTrendResponse)); + } catch (JsonProcessingException e) { e.printStackTrace(); } assertNull(statsTrendResponse.getResult()); @@ -545,24 +543,24 @@ public void testStatsTrendActionTextFieldNested() throws FoxtrotException { Assert.assertNotNull(statsTrendResponse.getBuckets().get(0).getBuckets()); //android-nexus Assert.assertEquals(1L, - statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(0).getStats().get("count")); + statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(0).getStats().get("count")); //Make sure few of them are actually zero Assert.assertEquals(0L, - statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(2).getStats().get("count")); + statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(2).getStats().get("count")); Assert.assertEquals(0L, - statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(12).getStats().get("count")); + statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(12).getStats().get("count")); //Now data should come Assert.assertEquals(1L, - statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(13).getStats().get("count")); + statsTrendResponse.getBuckets().get(0).getBuckets().get(0).getResult().get(13).getStats().get("count")); //ios-galaxy Assert.assertEquals(1L, - statsTrendResponse.getBuckets().get(1).getBuckets().get(0).getResult().get(0).getStats().get("count")); + statsTrendResponse.getBuckets().get(1).getBuckets().get(0).getResult().get(0).getStats().get("count")); //ios-nexus Assert.assertEquals(1L, - statsTrendResponse.getBuckets().get(1).getBuckets().get(1).getResult().get(0).getStats().get("count")); + statsTrendResponse.getBuckets().get(1).getBuckets().get(1).getResult().get(0).getStats().get("count")); //wp Assert.assertEquals(1L, - statsTrendResponse.getBuckets().get(2).getBuckets().get(0).getResult().get(0).getStats().get("count")); + statsTrendResponse.getBuckets().get(2).getBuckets().get(0).getResult().get(0).getStats().get("count")); } private void filterNonZeroCounts(StatsTrendResponse statsTrendResponse) { diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/TrendActionTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/TrendActionTest.java index 3597fc654..727dfab6a 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/TrendActionTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/actions/TrendActionTest.java @@ -188,9 +188,9 @@ public void testTrendActionWithField() throws FoxtrotException, JsonProcessingEx TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() - .get("android")); + .get("android")); filterNonZeroCounts(actualResponse.getTrends() - .get("ios")); + .get("ios")); assertEquals(expectedResponse, actualResponse); } @@ -224,9 +224,9 @@ public void testTrendActionWithFieldZeroTo() throws FoxtrotException, JsonProces TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() - .get("android")); + .get("android")); filterNonZeroCounts(actualResponse.getTrends() - .get("ios")); + .get("ios")); assertEquals(expectedResponse, actualResponse); } @@ -260,9 +260,9 @@ public void testTrendActionWithFieldZeroFrom() throws FoxtrotException, JsonProc TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() - .get("android")); + .get("android")); filterNonZeroCounts(actualResponse.getTrends() - .get("ios")); + .get("ios")); assertEquals(expectedResponse, actualResponse); } @@ -291,7 +291,7 @@ public void testTrendActionWithFieldWithValues() throws FoxtrotException, JsonPr TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() - .get("android")); + .get("android")); assertEquals(expectedResponse, actualResponse); } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/DistributedCacheTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/DistributedCacheTest.java index 876309fbf..8ebc4cdc2 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/DistributedCacheTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/DistributedCacheTest.java @@ -82,7 +82,7 @@ public void testPut() throws Exception { GroupResponse actualResponse = GroupResponse.class.cast(distributedCache.get("DUMMY_KEY_PUT")); assertEquals(GroupResponse.class.cast(expectedResponse) - .getResult(), actualResponse.getResult()); + .getResult(), actualResponse.getResult()); } @Test @@ -93,7 +93,7 @@ public void testPutCacheException() throws Exception { verify(mapper, times(1)).writeValueAsString(any()); assertNull(returnResponse); assertNull(hazelcastInstance.getMap("TEST") - .get("DUMMY_KEY_PUT")); + .get("DUMMY_KEY_PUT")); } @Test diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStoreTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStoreTest.java index 583956701..cd21b8e61 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStoreTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchQueryStoreTest.java @@ -135,9 +135,9 @@ public void testSaveSingleRawKeyVersion1() throws Exception { GetResponse getResponse = elasticsearchConnection.getClient() .get(new GetRequest(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, originalDocument.getTimestamp()), - ElasticsearchUtils.DOCUMENT_TYPE_NAME, originalDocument.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), - RequestOptions.DEFAULT); + ElasticsearchUtils.DOCUMENT_TYPE_NAME, originalDocument.getId()) + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), + RequestOptions.DEFAULT); assertTrue("Id should exist in ES", getResponse.isExists()); assertEquals("Id should match requestId", originalDocument.getId(), getResponse.getId()); } @@ -154,9 +154,9 @@ public void testSaveSingleRawKeyVersion2() throws Exception { GetResponse getResponse = elasticsearchConnection.getClient() .get(new GetRequest(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, originalDocument.getTimestamp()), - ElasticsearchUtils.DOCUMENT_TYPE_NAME, translatedDocument.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), - RequestOptions.DEFAULT); + ElasticsearchUtils.DOCUMENT_TYPE_NAME, translatedDocument.getId()) + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), + RequestOptions.DEFAULT); assertTrue("Id should exist in ES", getResponse.isExists()); assertEquals("Id should match requestId", translatedDocument.getId(), getResponse.getId()); } @@ -174,8 +174,8 @@ public void testSaveBulkLargeTextNodeWithBlockingEnabled() throws Exception { val currentIndex = ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, originalDocument.getTimestamp()); val response = elasticsearchConnection.getClient() .get(new GetRequest(currentIndex, ElasticsearchUtils.DOCUMENT_TYPE_NAME, originalDocument.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME, "testField", "testLargeField"), - RequestOptions.DEFAULT); + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME, "testField", "testLargeField"), + RequestOptions.DEFAULT); assertTrue(response.isExists()); val request = new GetFieldMappingsRequest(); request.indices(currentIndex); @@ -210,8 +210,8 @@ public void testSaveBulkLargeTextNodeWithBlockingDisabled() throws Exception { val currentIndex = ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, originalDocument.getTimestamp()); val response = elasticsearchConnection.getClient() .get(new GetRequest(currentIndex, ElasticsearchUtils.DOCUMENT_TYPE_NAME, originalDocument.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME, "testField", "testLargeField"), - RequestOptions.DEFAULT); + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME, "testField", "testLargeField"), + RequestOptions.DEFAULT); assertTrue(response.isExists()); val request = new GetFieldMappingsRequest(); request.indices(currentIndex); @@ -255,8 +255,8 @@ public void testSaveBulkNestedLargeTextNode() throws Exception { "testField", String.format("testLargeField%s", StringUtils.repeat(".testField", 5))}; val response = elasticsearchConnection.getClient() .get(new GetRequest(currentIndex, ElasticsearchUtils.DOCUMENT_TYPE_NAME, originalDocument.getId()) - .storedFields(fields), - RequestOptions.DEFAULT); + .storedFields(fields), + RequestOptions.DEFAULT); assertTrue(response.isExists()); val request = new GetFieldMappingsRequest(); @@ -353,9 +353,9 @@ public void testSaveBulkRawKeyVersion1() throws Exception { for (Document document : documents) { GetResponse getResponse = elasticsearchConnection.getClient() .get(new GetRequest(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, document.getTimestamp()), - ElasticsearchUtils.DOCUMENT_TYPE_NAME, document.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), - RequestOptions.DEFAULT); + ElasticsearchUtils.DOCUMENT_TYPE_NAME, document.getId()) + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), + RequestOptions.DEFAULT); assertTrue("Id should exist in ES", getResponse.isExists()); assertEquals("Id should match requestId", document.getId(), getResponse.getId()); } @@ -382,18 +382,18 @@ public void testSaveBulkRawKeyVersion2() throws Exception { for (Document document : documents) { GetResponse getResponse = elasticsearchConnection.getClient() .get(new GetRequest(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, document.getTimestamp()), - ElasticsearchUtils.DOCUMENT_TYPE_NAME, document.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), - RequestOptions.DEFAULT); + ElasticsearchUtils.DOCUMENT_TYPE_NAME, document.getId()) + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), + RequestOptions.DEFAULT); assertFalse("Id should not exist in ES", getResponse.isExists()); } for (Document document : translatedDocuments) { GetResponse getResponse = elasticsearchConnection.getClient() .get(new GetRequest(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, document.getTimestamp()), - ElasticsearchUtils.DOCUMENT_TYPE_NAME, document.getId()) - .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), - RequestOptions.DEFAULT); + ElasticsearchUtils.DOCUMENT_TYPE_NAME, document.getId()) + .storedFields(ElasticsearchUtils.DOCUMENT_META_TIMESTAMP_FIELD_NAME), + RequestOptions.DEFAULT); assertTrue("Id should exist in ES", getResponse.isExists()); assertEquals("Id should match requestId", document.getId(), getResponse.getId()); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtilsTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtilsTest.java index 56b47fd64..ff8cd423f 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtilsTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/ElasticsearchUtilsTest.java @@ -48,8 +48,8 @@ public void testGetIndicesLastSameSDay() throws Exception { filter.setCurrentTime(TEST_CURRENT_TIME); request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals(new String[]{"foxtrot-test-table-04-4-2015"}, indexes); } @@ -62,8 +62,8 @@ public void testGetIndicesLastLastDays() throws Exception { filter.setCurrentTime(TEST_CURRENT_TIME); request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals( new String[]{"foxtrot-test-table-02-4-2015", "foxtrot-test-table-03-4-2015", "foxtrot-test-table-04-4-2015"}, indexes); @@ -78,8 +78,8 @@ public void testGetIndicesBetween() throws Exception { filter.setTo(1428001200000L); //4/3/2015, 12:30:00 AM IST request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals(new String[]{"foxtrot-test-table-02-4-2015", "foxtrot-test-table-03-4-2015"}, indexes); } @@ -92,8 +92,8 @@ public void testGetIndicesGreaterThan() throws Exception { filter.setValue(1427997600000L); //4/2/2015, 11:30:00 PM IST request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals( new String[]{"foxtrot-test-table-02-4-2015", "foxtrot-test-table-03-4-2015", "foxtrot-test-table-04-4-2015"}, indexes); @@ -107,8 +107,8 @@ public void testGetIndicesGreaterEquals() throws Exception { filter.setValue(1427997600000L); //4/2/2015, 11:30:00 PM IST request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals( new String[]{"foxtrot-test-table-02-4-2015", "foxtrot-test-table-03-4-2015", "foxtrot-test-table-04-4-2015"}, indexes); @@ -122,8 +122,8 @@ public void testGetIndicesLessThan() throws Exception { filter.setValue(1427997600000L); //4/2/2015, 11:30:00 PM IST request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals(new String[]{"foxtrot-test-table-*"}, indexes); } @@ -136,8 +136,8 @@ public void testGetIndicesLessThanEquals() throws Exception { filter.setValue(1427997600000L); //4/2/2015, 11:30:00 PM IST request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); System.out.println(Arrays.toString(indexes)); Assert.assertArrayEquals(new String[]{"foxtrot-test-table-*"}, indexes); } @@ -153,8 +153,8 @@ public void testWithDifferentTableprefixShouldCreateIndexWithTableName() throws filter.setCurrentTime(TEST_CURRENT_TIME); request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); Assert.assertArrayEquals(new String[]{"azkaban-test-table-04-4-2015"}, indexes); config.setTableNamePrefix("foxtrot"); ElasticsearchUtils.setTableNamePrefix(config); @@ -171,8 +171,8 @@ public void testGetIndicesGreaterEqualsWithDifferentTablePrefixName() throws Exc filter.setValue(1427997600000L); //4/2/2015, 11:30:00 PM IST request.setFilters(Collections.singletonList(filter)); String indexes[] = ElasticsearchUtils.getIndices("test", request, - new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) - ); + new PeriodSelector(request.getFilters()).analyze(TEST_CURRENT_TIME) + ); Assert.assertArrayEquals( new String[]{"azkaban-test-table-02-4-2015", "azkaban-test-table-03-4-2015", "azkaban-test-table-04-4-2015"}, indexes); config.setTableNamePrefix("foxtrot"); diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/TableMapStoreTest.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/TableMapStoreTest.java index c6c273437..883290a2b 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/TableMapStoreTest.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/querystore/impl/TableMapStoreTest.java @@ -44,8 +44,8 @@ public class TableMapStoreTest { public static final String TEST_TABLE = "test-table"; public static final String TABLE_META_INDEX = "table-meta"; public static final String TABLE_META_TYPE = "table-meta"; - private ObjectMapper mapper = new ObjectMapper(); private static ElasticsearchConnection elasticsearchConnection; + private ObjectMapper mapper = new ObjectMapper(); private TableMapStore tableMapStore; @@ -112,10 +112,10 @@ public void testStoreNullTableName() throws Exception { @Test public void testStoreAll() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); } @@ -125,12 +125,12 @@ public void testStoreAll() throws Exception { .forEach(key -> multiGetRequest.add(TABLE_META_INDEX, TABLE_META_TYPE, key)); MultiGetResponse response = elasticsearchConnection.getClient().mget(multiGetRequest, RequestOptions.DEFAULT); Map responseTables = Maps.newHashMap(); - for(MultiGetItemResponse multiGetItemResponse : response) { + for (MultiGetItemResponse multiGetItemResponse : response) { Table table = mapper.readValue(multiGetItemResponse.getResponse() - .getSourceAsString(), Table.class); + .getSourceAsString(), Table.class); responseTables.put(table.getName(), table); } - for(Map.Entry entry : tables.entrySet()) { + for (Map.Entry entry : tables.entrySet()) { compareTables(entry.getValue(), responseTables.get(entry.getKey())); } } @@ -143,10 +143,10 @@ public void testStoreAllNull() throws Exception { @Test(expected = RuntimeException.class) public void testStoreAllNullTableKey() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(null, table); } @@ -156,9 +156,9 @@ public void testStoreAllNullTableKey() throws Exception { @Test(expected = RuntimeException.class) public void testStoreAllNullTableValue() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { tables.put(UUID.randomUUID() - .toString(), null); + .toString(), null); } tableMapStore.storeAll(tables); } @@ -166,7 +166,7 @@ public void testStoreAllNullTableValue() throws Exception { @Test(expected = RuntimeException.class) public void testStoreAllNullTableKeyValue() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { tables.put(null, null); } tableMapStore.storeAll(tables); @@ -175,17 +175,17 @@ public void testStoreAllNullTableKeyValue() throws Exception { @Test(expected = RuntimeException.class) public void testStoreAllSomeNullKeys() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); } Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(null, table); tableMapStore.storeAll(tables); @@ -194,17 +194,17 @@ public void testStoreAllSomeNullKeys() throws Exception { @Test(expected = RuntimeException.class) public void testStoreAllSomeNullValues() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); } Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), null); tableMapStore.storeAll(tables); @@ -213,10 +213,10 @@ public void testStoreAllSomeNullValues() throws Exception { @Test(expected = RuntimeException.class) public void testStoreAllSomeNullKeyValues() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); } @@ -256,24 +256,26 @@ public void testDeleteMissingKey() throws Exception { @Test public void testDeleteAll() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); } tableMapStore.storeAll(tables); - for(String name : tables.keySet()) { + for (String name : tables.keySet()) { GetResponse response = elasticsearchConnection.getClient() - .get(new GetRequest(TABLE_META_INDEX, TABLE_META_TYPE, name), RequestOptions.DEFAULT);; + .get(new GetRequest(TABLE_META_INDEX, TABLE_META_TYPE, name), RequestOptions.DEFAULT); + ; assertTrue(response.isExists()); } tableMapStore.deleteAll(tables.keySet()); - for(String name : tables.keySet()) { + for (String name : tables.keySet()) { GetResponse response = elasticsearchConnection.getClient() - .get(new GetRequest(TABLE_META_INDEX, TABLE_META_TYPE, name), RequestOptions.DEFAULT);; + .get(new GetRequest(TABLE_META_INDEX, TABLE_META_TYPE, name), RequestOptions.DEFAULT); + ; assertFalse(response.isExists()); } @@ -313,14 +315,14 @@ public void testLoad() throws Exception { @Test public void testLoadMissingKey() throws Exception { assertNull(tableMapStore.load(UUID.randomUUID() - .toString())); + .toString())); } //TODO Why not an error ? @Test public void testLoadNullKey() throws Exception { assertNull(tableMapStore.load(UUID.randomUUID() - .toString())); + .toString())); } // Exception Caught because of Runtime. Not an IOException @@ -328,10 +330,10 @@ public void testLoadNullKey() throws Exception { public void testLoadKeyWithWrongJson() throws Exception { elasticsearchConnection.getClient() .index(new IndexRequest(TABLE_META_INDEX) - .type(TABLE_META_TYPE) - .source("{ \"test\" : \"test\"}") - .id(TEST_TABLE) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + .type(TABLE_META_TYPE) + .source("{ \"test\" : \"test\"}") + .id(TEST_TABLE) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); tableMapStore.load(TEST_TABLE); } @@ -339,26 +341,26 @@ public void testLoadKeyWithWrongJson() throws Exception { @Test public void testLoadAll() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); Map sourceMap = ElasticsearchQueryUtils.toMap(mapper, table); elasticsearchConnection.getClient() .index(new IndexRequest(TABLE_META_INDEX) - .type(TABLE_META_TYPE) - .source(sourceMap) - .id(table.getName()) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + .type(TABLE_META_TYPE) + .source(sourceMap) + .id(table.getName()) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); } Set names = ImmutableSet.copyOf(Iterables.limit(tables.keySet(), 5)); Map responseTables = tableMapStore.loadAll(names); assertEquals(names.size(), responseTables.size()); - for(String name : names) { + for (String name : names) { compareTables(tables.get(name), responseTables.get(name)); } } @@ -372,10 +374,10 @@ public void testLoadAllNull() throws Exception { public void testLoadAllKeyWithWrongJson() throws Exception { elasticsearchConnection.getClient() .index(new IndexRequest(TABLE_META_INDEX) - .type(TABLE_META_TYPE) - .source("{ \"test\" : \"test\"}") - .id(TEST_TABLE) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)); + .type(TABLE_META_TYPE) + .source("{ \"test\" : \"test\"}") + .id(TEST_TABLE) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)); tableMapStore.loadAll(Arrays.asList(TEST_TABLE)); } @@ -383,24 +385,24 @@ public void testLoadAllKeyWithWrongJson() throws Exception { @Test public void testLoadAllKeys() throws Exception { Map tables = Maps.newHashMap(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); table.setTtl(20); tables.put(table.getName(), table); Map sourceMap = ElasticsearchQueryUtils.toMap(mapper, table); elasticsearchConnection.getClient() .index(new IndexRequest(TABLE_META_INDEX) - .type(TABLE_META_TYPE) - .source(sourceMap) - .id(table.getName()) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + .type(TABLE_META_TYPE) + .source(sourceMap) + .id(table.getName()) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); } Set responseKeys = tableMapStore.loadAllKeys(); - for(String name : tables.keySet()) { + for (String name : tables.keySet()) { assertTrue(responseKeys.contains(name)); } } diff --git a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/table/impl/ElasticsearchTestUtils.java b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/table/impl/ElasticsearchTestUtils.java index 822daea21..ec0e191f7 100644 --- a/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/table/impl/ElasticsearchTestUtils.java +++ b/foxtrot-core/src/test/java/com/flipkart/foxtrot/core/table/impl/ElasticsearchTestUtils.java @@ -39,6 +39,28 @@ @Slf4j public class ElasticsearchTestUtils { + public static synchronized ElasticsearchConnection getConnection() throws Exception { + // To make sure we load class which will start the server. + ElasticsearchContainerHolder.containerLoaded = true; + ElasticsearchConnection elasticsearchConnection = new ElasticsearchConnection( + ElasticsearchContainerHolder.getElasticsearchConfig()); + elasticsearchConnection.start(); + + return elasticsearchConnection; + } + + public static void cleanupIndices(final ElasticsearchConnection elasticsearchConnection) { + try { + DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("_all"); + final AcknowledgedResponse deleteIndexResponse = elasticsearchConnection.getClient() + .indices() + .delete(deleteIndexRequest, RequestOptions.DEFAULT); + log.info("Delete index response: {}", deleteIndexResponse); + } catch (Exception e) { + log.error("Index Cleanup failed", e); + } + } + /** * Class to make sure we run the server only once. */ @@ -79,26 +101,4 @@ private static class ElasticsearchContainerHolder { } } } - - public static synchronized ElasticsearchConnection getConnection() throws Exception { - // To make sure we load class which will start the server. - ElasticsearchContainerHolder.containerLoaded = true; - ElasticsearchConnection elasticsearchConnection = new ElasticsearchConnection( - ElasticsearchContainerHolder.getElasticsearchConfig()); - elasticsearchConnection.start(); - - return elasticsearchConnection; - } - - public static void cleanupIndices(final ElasticsearchConnection elasticsearchConnection) { - try { - DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("_all"); - final AcknowledgedResponse deleteIndexResponse = elasticsearchConnection.getClient() - .indices() - .delete(deleteIndexRequest, RequestOptions.DEFAULT); - log.info("Delete index response: {}", deleteIndexResponse); - } catch (Exception e) { - log.error("Index Cleanup failed", e); - } - } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/FoxtrotServer.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/FoxtrotServer.java index c530fbd19..18e0e30da 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/FoxtrotServer.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/FoxtrotServer.java @@ -69,23 +69,23 @@ protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(FoxtrotServer swaggerBundleConfiguration.setResourcePackage("com.flipkart.foxtrot.server.resources"); swaggerBundleConfiguration.setUriPrefix("/foxtrot"); swaggerBundleConfiguration.setDescription("A store abstraction and analytics system for real-time event data."); - if(!Strings.isNullOrEmpty(configuration.getSwaggerHost())) { + if (!Strings.isNullOrEmpty(configuration.getSwaggerHost())) { swaggerBundleConfiguration.setHost(configuration.getSwaggerHost()); } - if(!Strings.isNullOrEmpty(configuration.getSwaggerScheme())) { - swaggerBundleConfiguration.setSchemes(new String[] {configuration.getSwaggerScheme()}); + if (!Strings.isNullOrEmpty(configuration.getSwaggerScheme())) { + swaggerBundleConfiguration.setSchemes(new String[]{configuration.getSwaggerScheme()}); } return swaggerBundleConfiguration; } }); bootstrap.addBundle(GuiceBundle.builder() - .enableAutoConfig("com.flipkart.foxtrot") - .modules( - new FoxtrotModule()) - .useWebInstallers() - .printDiagnosticInfo() - .build(Stage.PRODUCTION)); + .enableAutoConfig("com.flipkart.foxtrot") + .modules( + new FoxtrotModule()) + .useWebInstallers() + .printDiagnosticInfo() + .build(Stage.PRODUCTION)); bootstrap.addCommand(new InitializerCommand()); configureObjectMapper(bootstrap.getObjectMapper()); } @@ -109,7 +109,7 @@ public void run(FoxtrotServerConfiguration configuration, Environment environmen // Add URL mapping cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); - ((AbstractServerFactory)configuration.getServerFactory()).setJerseyRootPath("/foxtrot"); + ((AbstractServerFactory) configuration.getServerFactory()).setJerseyRootPath("/foxtrot"); MetricUtil.setup(environment.metrics()); ElasticsearchUtils.setTableNamePrefix(configuration.getElasticsearch()); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/InitializerCommand.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/InitializerCommand.java index 684aae73b..05411be25 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/InitializerCommand.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/InitializerCommand.java @@ -62,8 +62,8 @@ protected void run(Bootstrap bootstrap, .health(new ClusterHealthRequest(), RequestOptions.DEFAULT); int numDataNodes = clusterHealth.getNumberOfDataNodes(); int numReplicas = (numDataNodes < 2) - ? 0 - : 1; + ? 0 + : 1; logger.info("# data nodes: {}, Setting replica count to: {}", numDataNodes, numReplicas); @@ -81,8 +81,7 @@ protected void run(Bootstrap bootstrap, .indices() .putTemplate(putIndexTemplateRequest, RequestOptions.DEFAULT); logger.info("Created mapping: {}", response.isAcknowledged()); - } - finally { + } finally { connection.stop(); } logger.info("Creating hbase table"); @@ -92,7 +91,7 @@ protected void run(Bootstrap bootstrap, private void createMetaIndex(final ElasticsearchConnection connection, final String indexName, int replicaCount) { try { - if(connection.getClient() + if (connection.getClient() .indices() .exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT)) { logger.info("Index {} already exists. Nothing to do.", indexName); @@ -110,11 +109,11 @@ private void createMetaIndex(final ElasticsearchConnection connection, final Str .indices() .create(createIndexRequest, RequestOptions.DEFAULT); logger.info("'{}' creation acknowledged: {}", indexName, response.isAcknowledged()); - if(!response.isAcknowledged()) { + if (!response.isAcknowledged()) { logger.error("Index {} could not be created.", indexName); } } catch (Exception e) { - if(null != e.getCause()) { + if (null != e.getCause()) { logger.error("Index {} could not be created: {}", indexName, e.getCause() .getLocalizedMessage()); } else { diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthConfig.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthConfig.java index 6d238c2b5..e0afe1ca3 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthConfig.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthConfig.java @@ -12,13 +12,11 @@ */ @Data public class AuthConfig { - private boolean enabled; - @NotNull @Valid - private JwtConfig jwt = new JwtConfig(); - + public AuthProviderConfig provider = new NoneAuthProviderConfig(); + private boolean enabled; @NotNull @Valid - public AuthProviderConfig provider = new NoneAuthProviderConfig(); + private JwtConfig jwt = new JwtConfig(); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthInfoVisitor.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthInfoVisitor.java index 91dc17a6a..1dde01e41 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthInfoVisitor.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/AuthInfoVisitor.java @@ -3,6 +3,6 @@ /** * */ -public interface AuthInfoVisitor { +public interface AuthInfoVisitor { T visit(TokenAuthInfo tokenAuthInfo); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/ESAuthStore.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/ESAuthStore.java index 2c965df08..321dc2794 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/ESAuthStore.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/ESAuthStore.java @@ -75,7 +75,7 @@ public Optional getUser(String userId) { public boolean deleteUser(String userId) { boolean status = connection.getClient() .delete(new DeleteRequest(USERS_INDEX, USER_TYPE, userId) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT) .status() == RestStatus.OK; if (status) { val count = deleteTokensForUser(userId); @@ -113,20 +113,19 @@ public Optional provisionToken(String userId, String tokenId, TokenType t try { val saveStatus = connection.getClient() .index(new IndexRequest(TOKENS_INDEX) - .source(mapper.writeValueAsString(new Token(tokenId, IdType.SESSION_ID, tokenType, userId, expiry)), - XContentType.JSON) - .id(tokenId) - .type(TOKEN_TYPE) - .create(true) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT) + .source(mapper.writeValueAsString(new Token(tokenId, IdType.SESSION_ID, tokenType, userId, expiry)), + XContentType.JSON) + .id(tokenId) + .type(TOKEN_TYPE) + .create(true) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT) .status(); if (saveStatus != RestStatus.CREATED) { log.error("ES save status for token for user {} is: {}", userId, saveStatus); return Optional.empty(); } log.info("Token created for user: {}", userId); - } - catch (ElasticsearchException v) { + } catch (ElasticsearchException v) { log.warn("A valid token exists exists already.. for id: {}", userId); return getTokenForUser(userId); } @@ -166,7 +165,7 @@ public Optional getTokenForUser(String userId) { public boolean deleteToken(String tokenId) { return connection.getClient() .delete(new DeleteRequest(TOKENS_INDEX, TOKEN_TYPE, tokenId) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT) .status() == RestStatus.OK; } @@ -177,11 +176,11 @@ public boolean deleteExpiredTokens(Date date, Duration sessionDuration) { Date oldestValidDate = new Date(date.getTime() - sessionDuration.toMilliseconds()); val deletedCount = connection.getClient() .deleteByQuery(new DeleteByQueryRequest(TOKENS_INDEX) - .setDocTypes(TOKEN_TYPE) - .setIndicesOptions(Utils.indicesOptions()) - .setQuery(QueryBuilders.rangeQuery("expiry") - .lt(oldestValidDate.getTime())) - .setRefresh(true), RequestOptions.DEFAULT) + .setDocTypes(TOKEN_TYPE) + .setIndicesOptions(Utils.indicesOptions()) + .setQuery(QueryBuilders.rangeQuery("expiry") + .lt(oldestValidDate.getTime())) + .setRefresh(true), RequestOptions.DEFAULT) .getDeleted(); log.info("Deleted {} expired tokens", deletedCount); return true; @@ -192,12 +191,12 @@ public boolean deleteExpiredTokens(Date date, Duration sessionDuration) { private RestStatus saveUser(User user, DocWriteRequest.OpType opType) { return connection.getClient() .index(new IndexRequest(USERS_INDEX) - .source(mapper.writeValueAsString(user), XContentType.JSON) - .id(user.getId()) - .type(USER_TYPE) - .opType(opType) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), - RequestOptions.DEFAULT) + .source(mapper.writeValueAsString(user), XContentType.JSON) + .id(user.getId()) + .type(USER_TYPE) + .opType(opType) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT) .status(); } @@ -205,10 +204,10 @@ private RestStatus saveUser(User user, DocWriteRequest.OpType opType) { public long deleteTokensForUser(String userId) { return connection.getClient() .deleteByQuery(new DeleteByQueryRequest(TOKENS_INDEX) - .setDocTypes(TOKEN_TYPE) - .setQuery(QueryBuilders.termQuery("userId", userId)) - .setRefresh(true), - RequestOptions.DEFAULT) + .setDocTypes(TOKEN_TYPE) + .setQuery(QueryBuilders.termQuery("userId", userId)) + .setRefresh(true), + RequestOptions.DEFAULT) .getDeleted(); } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/RoleAuthorizer.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/RoleAuthorizer.java index 82a2c5fb7..dcb4c1ab2 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/RoleAuthorizer.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/RoleAuthorizer.java @@ -18,14 +18,14 @@ public boolean authorize(UserPrincipal userPrincipal, String role) { val user = userPrincipal.getUser(); val foxtrotRole = FoxtrotRole.valueOf(role); - if(!user.getRoles().contains(foxtrotRole)) { + if (!user.getRoles().contains(foxtrotRole)) { log.warn("User {} is trying to access unauthorized role: {}", user.getId(), role); return false; } val token = userPrincipal.getToken(); - if(!token.getTokenType().getAllowedRoles().contains(foxtrotRole)) { + if (!token.getTokenType().getAllowedRoles().contains(foxtrotRole)) { log.warn("User {} trying to access resource with role: {} with token of tye: {}", - user.getId(), role, token.getTokenType().name()); + user.getId(), role, token.getTokenType().name()); return false; } return true; diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/Token.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/Token.java index e7cab5300..a8af0a954 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/Token.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/Token.java @@ -11,10 +11,10 @@ @Value public class Token { public static final Token DEFAULT = new Token("__DEFAULT_TOKEN__", - IdType.SESSION_ID, - TokenType.SYSTEM, - "__DEFAULT__", - null); + IdType.SESSION_ID, + TokenType.SYSTEM, + "__DEFAULT__", + null); String id; IdType idType; diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/TokenAuthenticator.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/TokenAuthenticator.java index 2416ef119..9b7911e05 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/TokenAuthenticator.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/TokenAuthenticator.java @@ -30,12 +30,12 @@ public TokenAuthenticator(AuthConfig config, Provider provider) { @Override public Optional authenticate(String token) throws AuthenticationException { - if(!config.isEnabled()) { + if (!config.isEnabled()) { log.debug("Authentication is disabled"); return Optional.of(UserPrincipal.DEFAULT); } log.debug("Auth called"); - if(Strings.isNullOrEmpty(token)) { + if (Strings.isNullOrEmpty(token)) { log.warn("authentication_failed::empty token"); return Optional.empty(); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/GoogleAuthProvider.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/GoogleAuthProvider.java index ae2c91184..17b9b7efb 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/GoogleAuthProvider.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/GoogleAuthProvider.java @@ -70,13 +70,13 @@ public GoogleAuthProvider( case HTTP: { Preconditions.checkArgument(!Strings.isNullOrEmpty(googleAuthConfig.getProxyHost())); proxy = new Proxy(Proxy.Type.HTTP, - proxyAddress(googleAuthConfig)); + proxyAddress(googleAuthConfig)); break; } case SOCKS: Preconditions.checkArgument(!Strings.isNullOrEmpty(googleAuthConfig.getProxyHost())); proxy = new Proxy(Proxy.Type.SOCKS, - proxyAddress(googleAuthConfig)); + proxyAddress(googleAuthConfig)); break; } } @@ -90,8 +90,8 @@ public GoogleAuthProvider( ImmutableSet.of("https://www.googleapis.com/auth/userinfo.email")) .build(); this.redirectionUrl = (googleAuthConfig.isSecureEndpoint() - ? "https" - : "http") + ? "https" + : "http") + "://" + googleAuthConfig.getServer() + CALLBACK_PATH; @@ -114,8 +114,8 @@ public String redirectionURL(String sessionId) { // .setRedirectUri("http://localhost:8080/auth/google") .build(); return !Strings.isNullOrEmpty(googleAuthConfig.getLoginDomain()) - ? (url + "&hd=" + googleAuthConfig.getLoginDomain()) - : url; + ? (url + "&hd=" + googleAuthConfig.getLoginDomain()) + : url; } @Override @@ -139,8 +139,7 @@ public Optional login(String authToken, String sessionId) { final String jsonIdentity = request.execute().parseAsString(); log.debug("Identity: {}", jsonIdentity); email = mapper.readTree(jsonIdentity).get("email").asText(); - } - catch (IOException e) { + } catch (IOException e) { log.error("Error logging in using google:", e); return Optional.empty(); } @@ -152,9 +151,9 @@ public Optional login(String authToken, String sessionId) { } final Duration sessionDuration = AuthUtils.sessionDuration(authConfig); return credentialsStorage.provisionToken(user.getId(), - sessionId, - TokenType.DYNAMIC, - new Date(new Date().getTime() + sessionDuration.toMilliseconds())); + sessionId, + TokenType.DYNAMIC, + new Date(new Date().getTime() + sessionDuration.toMilliseconds())); } @Override @@ -163,8 +162,7 @@ public Optional authenticate(AuthInfo authInfo) { final JwtContext jwtContext; try { jwtContext = consumer.process(jwt); - } - catch (InvalidJwtException e) { + } catch (InvalidJwtException e) { log.error("Jwt validation failure: ", e); return Optional.empty(); } @@ -174,8 +172,7 @@ public Optional authenticate(AuthInfo authInfo) { val claims = jwtContext.getJwtClaims(); userId = claims.getSubject(); tokenId = claims.getJwtId(); - } - catch (MalformedClaimException e) { + } catch (MalformedClaimException e) { log.error(String.format("exception in claim extraction %s", e.getMessage()), e); return Optional.empty(); } @@ -221,15 +218,15 @@ private JwtConsumer buildJwtConsumer() { AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.HMAC_SHA512)) .setExpectedAudience(Arrays.stream(TokenType.values()) - .map(TokenType::name) - .toArray(String[]::new)) + .map(TokenType::name) + .toArray(String[]::new)) .build(); } private InetSocketAddress proxyAddress(GoogleAuthProviderConfig googleAuthConfig) { return new InetSocketAddress(googleAuthConfig.getProxyHost(), - googleAuthConfig.getProxyPort()); + googleAuthConfig.getProxyPort()); } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProvider.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProvider.java index 1e231c8ff..9828d06d8 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProvider.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProvider.java @@ -67,15 +67,15 @@ public String redirectionURL(String sessionId) { @SneakyThrows public Optional login(String authCode, String sessionId) { return remoteTokenCall("authorization_code", authCode, - ti -> { - val idmanUser = ti.getUser(); - val expiry = Date.from(Instant.now().plusSeconds(ti.getExpiry())); - return Optional.of(new Token(ti.getAccessToken(), - IdType.ACCESS_TOKEN, - TokenType.DYNAMIC, - idmanUser.getUser().getId(), - expiry)); - }); + ti -> { + val idmanUser = ti.getUser(); + val expiry = Date.from(Instant.now().plusSeconds(ti.getExpiry())); + return Optional.of(new Token(ti.getAccessToken(), + IdType.ACCESS_TOKEN, + TokenType.DYNAMIC, + idmanUser.getUser().getId(), + expiry)); + }); } @Override @@ -89,16 +89,16 @@ public Optional authenticate(AuthInfo authInfo) { val expiry = Date.from(Instant.now().plusSeconds(ti.getExpiry())); val remoteUser = idmanUser.getUser(); val t = new Token(ti.getAccessToken(), - IdType.ACCESS_TOKEN, - TokenType.DYNAMIC, - remoteUser.getId(), - expiry); + IdType.ACCESS_TOKEN, + TokenType.DYNAMIC, + remoteUser.getId(), + expiry); val user = new User(remoteUser.getId(), - mapToFoxtrotRoles(idmanUser), - Collections.emptySet(), - isSystemUser(idmanUser), - new Date(), - new Date()); + mapToFoxtrotRoles(idmanUser), + Collections.emptySet(), + isSystemUser(idmanUser), + new Date(), + new Date()); return Optional.of(new AuthenticatedInfo(t, user)); }); } @@ -106,7 +106,7 @@ public Optional authenticate(AuthInfo authInfo) { @Override @SneakyThrows public boolean logout(String sessionId) { - if(Strings.isNullOrEmpty(sessionId)) { + if (Strings.isNullOrEmpty(sessionId)) { log.warn("Empty token send for logout"); return true; } @@ -138,12 +138,12 @@ private Optional remoteTokenCall( String param, Function> handler) throws URISyntaxException, IOException { val post = new HttpPost(new URIBuilder(config.getIdmanEndpoint()) - .setPath("/apis/oauth2/token") - .build()); + .setPath("/apis/oauth2/token") + .build()); val form = ImmutableList.builder() .add(new BasicNameValuePair(callMode.equals("authorization_code") - ? "code" - : "refresh_token", param)) + ? "code" + : "refresh_token", param)) .add(new BasicNameValuePair("client_id", config.getClientId())) .add(new BasicNameValuePair("client_secret", config.getClientSecret())) .add(new BasicNameValuePair("grant_type", callMode)) @@ -161,8 +161,8 @@ private Optional remoteTokenCall( private boolean remoteTokenRevokeCall(String token) throws URISyntaxException, IOException { val post = new HttpPost(new URIBuilder(config.getIdmanEndpoint()) - .setPath("/apis/oauth2/revoke") - .build()); + .setPath("/apis/oauth2/revoke") + .build()); val params = ImmutableList.builder() .add(new BasicNameValuePair("client_id", config.getClientId())) .add(new BasicNameValuePair("client_secret", config.getClientSecret())) diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProviderConfig.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProviderConfig.java index f16c9ea93..1a41a415d 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProviderConfig.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/authprovider/impl/IdmanAuthProviderConfig.java @@ -16,14 +16,14 @@ @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public class IdmanAuthProviderConfig extends AuthProviderConfig { + @NotEmpty + public String serverEndpoint; @NotEmpty private String idmanEndpoint; @NotEmpty private String clientId; @NotEmpty private String clientSecret; - @NotEmpty - public String serverEndpoint; public IdmanAuthProviderConfig() { super(AuthType.OAUTH_IDMAN); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/JwtAuthDynamicFeature.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/JwtAuthDynamicFeature.java index 2c95782b9..49371b67f 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/JwtAuthDynamicFeature.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/JwtAuthDynamicFeature.java @@ -22,7 +22,7 @@ public JwtAuthDynamicFeature( Authorizer authorizer, Environment environment) { super(new UserAuthorizationFilter(authConfig, authorizer)); - if(null != environment) { + if (null != environment) { environment.jersey().register(new AuthValueFactoryProvider.Binder<>(UserPrincipal.class)); environment.jersey().register(RolesAllowedDynamicFeature.class); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/SessionUser.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/SessionUser.java index d501c4bd5..fae5ac6ea 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/SessionUser.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/SessionUser.java @@ -14,10 +14,8 @@ @Builder public class SessionUser implements Serializable { private static final long serialVersionUID = -7917711435258380077L; - - private final UserPrincipal user; - private static ThreadLocal currentUser = new ThreadLocal<>(); + private final UserPrincipal user; public static void put(UserPrincipal user) { currentUser.set(user); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthenticationFilter.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthenticationFilter.java index e54addf58..c5ab54146 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthenticationFilter.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthenticationFilter.java @@ -34,7 +34,7 @@ public class UserAuthenticationFilter implements Filter { private static final Set WHITELISTED_PATTERNS = ImmutableSet.builder() .add("/foxtrot/oauth") .add("^/foxtrot/auth.*") - .build(); + .build(); private final AuthConfig authConfig; private final Provider> authenticator; @@ -53,7 +53,7 @@ public void init(FilterConfig filterConfig) throws ServletException { @Override public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if(!authConfig.isEnabled()) { + if (!authConfig.isEnabled()) { log.trace("Auth disabled"); chain.doFilter(request, response); return; @@ -61,29 +61,26 @@ public void doFilter( HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; final String requestURI = httpRequest.getRequestURI(); - if(WHITELISTED_PATTERNS.stream().anyMatch(requestURI::startsWith)) { + if (WHITELISTED_PATTERNS.stream().anyMatch(requestURI::startsWith)) { chain.doFilter(request, response); return; } val jwt = getTokenFromCookieOrHeader(httpRequest).orElse(null); - if(!Strings.isNullOrEmpty(jwt)) { + if (!Strings.isNullOrEmpty(jwt)) { try { val principal = authenticator.get() .authenticate(jwt).orElse(null); - if(null != principal) { + if (null != principal) { SessionUser.put(principal); chain.doFilter(request, response); return; - } - else { + } else { log.info("No principal "); } - } - catch (AuthenticationException e) { + } catch (AuthenticationException e) { log.error("Jwt validation failure: ", e); } - } - else { + } else { log.debug("No token in request"); } val referrer = httpRequest.getHeader(org.apache.http.HttpHeaders.REFERER); @@ -119,9 +116,9 @@ private Optional getTokenFromHeader(HttpServletRequest servletRequest) { private Optional getTokenFromCookie(HttpServletRequest request) { val cookies = request.getCookies(); - if(null != cookies && cookies.length != 0) { + if (null != cookies && cookies.length != 0) { val token = Arrays.stream(cookies).filter(cookie -> cookie.getName().equals("token")).findAny().orElse(null); - if(null != token) { + if (null != token) { return Optional.of(token.getValue()); } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthorizationFilter.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthorizationFilter.java index 9c16c7991..1ef4a722d 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthorizationFilter.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/filter/UserAuthorizationFilter.java @@ -31,12 +31,12 @@ public UserAuthorizationFilter( @Override public void filter(final ContainerRequestContext requestContext) throws IOException { - if(!authConfig.isEnabled()) { + if (!authConfig.isEnabled()) { updateContext(requestContext, UserPrincipal.DEFAULT); return; } UserPrincipal principal = SessionUser.take(); - if(null != principal) { + if (null != principal) { updateContext(requestContext, principal); return; } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/DistributedSessionDataStore.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/DistributedSessionDataStore.java index 57855a133..398a1f1ec 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/DistributedSessionDataStore.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/DistributedSessionDataStore.java @@ -35,7 +35,7 @@ public DistributedSessionDataStore(HazelcastConnection hazelcastConnection) { @Override public void put(String sessionId, Object data) { - if(Strings.isNullOrEmpty(sessionId) || null == data) { + if (Strings.isNullOrEmpty(sessionId) || null == data) { return; } store.put(sessionId, data); @@ -43,7 +43,7 @@ public void put(String sessionId, Object data) { @Override public Optional get(String sessionId) { - if(Strings.isNullOrEmpty(sessionId)) { + if (Strings.isNullOrEmpty(sessionId)) { return Optional.empty(); } return Optional.ofNullable(store.get(sessionId)); @@ -51,7 +51,7 @@ public Optional get(String sessionId) { @Override public void delete(String sessionId) { - if(Strings.isNullOrEmpty(sessionId)) { + if (Strings.isNullOrEmpty(sessionId)) { return; } store.delete(sessionId); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/SessionDataStore.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/SessionDataStore.java index 1c8e58c4f..a7df272d6 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/SessionDataStore.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/auth/sessionstore/SessionDataStore.java @@ -7,6 +7,8 @@ */ public interface SessionDataStore { void put(String sessionId, Object data); + Optional get(String sessionId); + void delete(String sessionId); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/cluster/ClusterManager.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/cluster/ClusterManager.java index 0d66fc318..cd81e3154 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/cluster/ClusterManager.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/cluster/ClusterManager.java @@ -54,12 +54,12 @@ public ClusterManager(HazelcastConnection connection, List healthCh String hostname = Inet4Address.getLocalHost() .getCanonicalHostName(); //Auto detect marathon environment and query for host environment variable - if(!Strings.isNullOrEmpty(System.getenv("HOST"))) + if (!Strings.isNullOrEmpty(System.getenv("HOST"))) hostname = System.getenv("HOST"); Preconditions.checkNotNull(hostname, "Could not retrieve hostname, cannot proceed"); int port = ServerUtils.port(serverFactory); //Auto detect marathon environment and query for host environment variable - if(!Strings.isNullOrEmpty(System.getenv("PORT_" + port))) + if (!Strings.isNullOrEmpty(System.getenv("PORT_" + port))) port = Integer.parseInt(System.getenv("PORT_" + port)); executor = Executors.newScheduledThreadPool(1); clusterMember = new ClusterMember(hostname, port); @@ -98,17 +98,17 @@ private NodeDataUpdater(List healthChecks, IMap get() { try { response = connection.getClient() .search(new SearchRequest(INDEX) - .types(TYPE) - .source(new SearchSourceBuilder() - .query(boolQuery().must(matchAllQuery())) - .sort(fieldSort("name").order(SortOrder.DESC))) - .scroll(new TimeValue(60000)), + .types(TYPE) + .source(new SearchSourceBuilder() + .query(boolQuery().must(matchAllQuery())) + .sort(fieldSort("name").order(SortOrder.DESC))) + .scroll(new TimeValue(60000)), RequestOptions.DEFAULT); - } - catch (IOException e) { + } catch (IOException e) { throw new ConsolePersistenceException("", "console listing failed", e); } try { @@ -120,8 +119,8 @@ public List get() { while (true) { response = connection.getClient() .scroll(new SearchScrollRequest(response.getScrollId()) - .scroll(new TimeValue(60000)), - RequestOptions.DEFAULT); + .scroll(new TimeValue(60000)), + RequestOptions.DEFAULT); SearchHits hits = response.getHits(); for (SearchHit hit : hits) { results.add(mapper.readValue(hit.getSourceAsString(), Console.class)); @@ -142,10 +141,10 @@ public void delete(final String id) { try { connection.getClient() .delete(new DeleteRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .index(INDEX) - .type(TYPE) - .id(id), RequestOptions.DEFAULT); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .index(INDEX) + .type(TYPE) + .id(id), RequestOptions.DEFAULT); logger.info("Deleted Console : {}", id); } catch (Exception e) { throw new ConsolePersistenceException(id, "console deletion_failed", e); @@ -158,11 +157,11 @@ public void saveV2(ConsoleV2 console, boolean newConsole) { try { connection.getClient() .index(new IndexRequest(INDEX_V2) - .type(TYPE) - .id(console.getId()) - .source(mapper.writeValueAsBytes(console), XContentType.JSON) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), - RequestOptions.DEFAULT); + .type(TYPE) + .id(console.getId()) + .source(mapper.writeValueAsBytes(console), XContentType.JSON) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT); logger.info("Saved Console : {}", console); } catch (Exception e) { throw new ConsolePersistenceException(console.getId(), "console save failed", e); @@ -187,13 +186,13 @@ public ConsoleV2 getV2(String id) { public List getV2() { try { SearchResponse response = connection.getClient() - .search(new SearchRequest(INDEX_V2) - .types(TYPE) - .source(new SearchSourceBuilder() + .search(new SearchRequest(INDEX_V2) + .types(TYPE) + .source(new SearchSourceBuilder() .query(boolQuery().must(matchAllQuery())) .size(SCROLL_SIZE) .sort(fieldSort("name.keyword").order(SortOrder.DESC).unmappedType("keyword"))) - .scroll(new TimeValue(SCROLL_TIMEOUT)), RequestOptions.DEFAULT); + .scroll(new TimeValue(SCROLL_TIMEOUT)), RequestOptions.DEFAULT); List results = new ArrayList<>(); while (true) { SearchHits hits = response.getHits(); @@ -207,7 +206,7 @@ public List getV2() { response = connection.getClient() .scroll(new SearchScrollRequest(response.getScrollId()) - .scroll(new TimeValue(SCROLL_TIMEOUT)), RequestOptions.DEFAULT); + .scroll(new TimeValue(SCROLL_TIMEOUT)), RequestOptions.DEFAULT); } return results; } catch (Exception e) { @@ -220,13 +219,13 @@ public List getAllOldVersions(final String name, final String sortBy) try { SearchHits searchHits = connection.getClient() .search(new SearchRequest(INDEX_HISTORY) - .searchType(SearchType.QUERY_THEN_FETCH) - .source(new SearchSourceBuilder() - .query(QueryBuilders.termQuery("name.keyword", name)) - .sort(SortBuilders.fieldSort(sortBy).order(SortOrder.DESC)) - .from(0) - .size(10)), - RequestOptions.DEFAULT) + .searchType(SearchType.QUERY_THEN_FETCH) + .source(new SearchSourceBuilder() + .query(QueryBuilders.termQuery("name.keyword", name)) + .sort(SortBuilders.fieldSort(sortBy).order(SortOrder.DESC)) + .from(0) + .size(10)), + RequestOptions.DEFAULT) .getHits(); List results = new ArrayList<>(); for (SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) { @@ -309,7 +308,7 @@ public void deleteV2(String id) { try { connection.getClient() .delete(new DeleteRequest(INDEX_V2, TYPE, id) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); logger.info("Deleted Console : {}", id); } catch (Exception e) { throw new ConsolePersistenceException(id, "console deletion_failed", e); @@ -321,7 +320,7 @@ public void deleteOldVersion(String id) { try { connection.getClient() .delete(new DeleteRequest(INDEX_HISTORY, TYPE, id) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); logger.info("Deleted Old Console : {}", id); } catch (Exception e) { throw new ConsolePersistenceException(id, "old console deletion_failed", e); @@ -335,9 +334,9 @@ private void saveOldConsole(ConsoleV2 console) { try { connection.getClient() .index(new IndexRequest(INDEX_HISTORY, TYPE, id) - .source(mapper.writeValueAsBytes(console), XContentType.JSON) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), - RequestOptions.DEFAULT); + .source(mapper.writeValueAsBytes(console), XContentType.JSON) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT); logger.info("Saved Old Console : {}", console); } catch (Exception e) { throw new ConsolePersistenceException(console.getId(), "old console save failed", e); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/FoxtrotModule.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/FoxtrotModule.java index cd36f6c7f..40d3482ec 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/FoxtrotModule.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/FoxtrotModule.java @@ -145,16 +145,16 @@ public ClusterConfig clusterConfig(FoxtrotServerConfiguration configuration) { @Singleton public CardinalityConfig cardinalityConfig(FoxtrotServerConfiguration configuration) { return null == configuration.getCardinality() - ? new CardinalityConfig("false", String.valueOf(ElasticsearchUtils.DEFAULT_SUB_LIST_SIZE)) - : configuration.getCardinality(); + ? new CardinalityConfig("false", String.valueOf(ElasticsearchUtils.DEFAULT_SUB_LIST_SIZE)) + : configuration.getCardinality(); } @Provides @Singleton public EsIndexOptimizationConfig esIndexOptimizationConfig(FoxtrotServerConfiguration configuration) { return null == configuration.getEsIndexOptimizationConfig() - ? new EsIndexOptimizationConfig() - : configuration.getEsIndexOptimizationConfig(); + ? new EsIndexOptimizationConfig() + : configuration.getEsIndexOptimizationConfig(); } @Provides @@ -167,16 +167,16 @@ public DataDeletionManagerConfig dataDeletionManagerConfig(FoxtrotServerConfigur @Singleton public ConsoleHistoryConfig consoleHistoryConfig(FoxtrotServerConfiguration configuration) { return null == configuration.getConsoleHistoryConfig() - ? new ConsoleHistoryConfig() - : configuration.getConsoleHistoryConfig(); + ? new ConsoleHistoryConfig() + : configuration.getConsoleHistoryConfig(); } @Provides @Singleton public SessionCleanupConfig sessionCleanupConfig(FoxtrotServerConfiguration configuration) { return null == configuration.getSessionCleanupConfig() - ? new SessionCleanupConfig() - : configuration.getSessionCleanupConfig(); + ? new SessionCleanupConfig() + : configuration.getSessionCleanupConfig(); } @Provides @@ -309,8 +309,8 @@ public JwtConsumer provideJwtConsumer(AuthConfig config) { AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.HMAC_SHA512)) .setExpectedAudience(Arrays.stream(TokenType.values()) - .map(TokenType::name) - .toArray(String[]::new)) + .map(TokenType::name) + .toArray(String[]::new)) .build(); } @@ -333,16 +333,16 @@ public Authorizer authorizer( final RoleAuthorizer authorizer, final AuthConfig authConfig) { return new CachingAuthorizer<>(environment.metrics(), - authorizer, - CacheBuilderSpec.parse(authConfig.getJwt().getAuthCachePolicy())); + authorizer, + CacheBuilderSpec.parse(authConfig.getJwt().getAuthCachePolicy())); } @Provides @Singleton public ElasticsearchTuningConfig provideElasticsearchTuningConfig(FoxtrotServerConfiguration configuration) { return Objects.nonNull(configuration.getElasticsearchTuningConfig()) - ? configuration.getElasticsearchTuningConfig() - : new ElasticsearchTuningConfig(); + ? configuration.getElasticsearchTuningConfig() + : new ElasticsearchTuningConfig(); } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/HealthcheckListProvider.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/HealthcheckListProvider.java index 186fefd5d..2ac6ba8be 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/HealthcheckListProvider.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/di/HealthcheckListProvider.java @@ -5,6 +5,7 @@ import com.flipkart.foxtrot.server.healthcheck.HazelcastHealthCheck; import com.google.inject.Inject; import com.google.inject.Provider; + import java.util.Arrays; import java.util.List; diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/ElasticSearchHealthCheck.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/ElasticSearchHealthCheck.java index dde8f6cba..bd893e604 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/ElasticSearchHealthCheck.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/ElasticSearchHealthCheck.java @@ -38,9 +38,9 @@ protected HealthCheck.Result check() throws Exception { .cluster() .health(new ClusterHealthRequest(), RequestOptions.DEFAULT); return (response.getStatus() - .name() - .equalsIgnoreCase("GREEN") || response.getStatus() - .name() - .equalsIgnoreCase("YELLOW")) ? HealthCheck.Result.healthy() : HealthCheck.Result.unhealthy("Cluster unhealthy"); + .name() + .equalsIgnoreCase("GREEN") || response.getStatus() + .name() + .equalsIgnoreCase("YELLOW")) ? HealthCheck.Result.healthy() : HealthCheck.Result.unhealthy("Cluster unhealthy"); } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HBaseHealthCheck.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HBaseHealthCheck.java index 634b76c66..4eca228c2 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HBaseHealthCheck.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HBaseHealthCheck.java @@ -10,8 +10,9 @@ @Singleton public class HBaseHealthCheck extends NamedHealthCheck { - private Configuration configuration; private static final String HBASE_HEALTHCHECK = "hbaseHealthcheck"; + private Configuration configuration; + @Inject public HBaseHealthCheck(Configuration configuration) { this.configuration = configuration; diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HazelcastHealthCheck.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HazelcastHealthCheck.java index c73a97b70..a12558096 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HazelcastHealthCheck.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/healthcheck/HazelcastHealthCheck.java @@ -5,29 +5,30 @@ import com.flipkart.foxtrot.core.querystore.impl.HazelcastConnection; import com.google.inject.Inject; import com.google.inject.Singleton; + import java.util.UUID; + import ru.vyarus.dropwizard.guice.module.installer.feature.health.NamedHealthCheck; @Singleton public class HazelcastHealthCheck extends NamedHealthCheck { - private HazelcastConnection hazelcastConnection; private static final String HAZELCAST_HEALTHCHECK = "hazelcastHealthcheck"; - - @Inject - public HazelcastHealthCheck(final HazelcastConnection hazelcastConnection){ - this.hazelcastConnection = hazelcastConnection; - } + private HazelcastConnection hazelcastConnection; /** * A random UUID to healthcheck */ private String uuid = UUID.randomUUID().toString(); - /** * A counter for healthcheck */ private int counter = 0; + @Inject + public HazelcastHealthCheck(final HazelcastConnection hazelcastConnection) { + this.hazelcastConnection = hazelcastConnection; + } + @Override protected Result check() throws Exception { // Update the counter and store in the map @@ -37,7 +38,7 @@ protected Result check() throws Exception { int toCheck = (int) hazelcastConnection.getHazelcast().getMap(HEALTHCHECK_MAP).get(uuid); return toCheck == counter ? Result.healthy("UUID:" + uuid + ", counter: " + counter + " - OK") : Result.unhealthy("UUID:" + uuid + ", counter: " + counter - + " Something is wrong: healthCheck count is not updating"); + + " Something is wrong: healthCheck count is not updating"); } catch (Exception e) { return Result.healthy("UUID:" + uuid + " - Error: " + e.getMessage()); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/jobs/consolehistory/ConsoleHistoryManager.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/jobs/consolehistory/ConsoleHistoryManager.java index 04eb42937..a350f87b8 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/jobs/consolehistory/ConsoleHistoryManager.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/jobs/consolehistory/ConsoleHistoryManager.java @@ -63,16 +63,16 @@ protected void runImpl(LockingTaskExecutor executor, Instant lockAtMostUntil) { try { SearchResponse searchResponse = connection.getClient() .search(new SearchRequest(INDEX_V2) - .types(TYPE) - .searchType(SearchType.QUERY_THEN_FETCH) - .source(new SearchSourceBuilder() - .aggregation(AggregationBuilders.terms("names") + .types(TYPE) + .searchType(SearchType.QUERY_THEN_FETCH) + .source(new SearchSourceBuilder() + .aggregation(AggregationBuilders.terms("names") .field("name.keyword") .size(1000))), RequestOptions.DEFAULT); Terms agg = searchResponse.getAggregations() .get("names"); - for(Terms.Bucket entry : agg.getBuckets()) { + for (Terms.Bucket entry : agg.getBuckets()) { deleteOldData(entry.getKeyAsString()); } } catch (Exception e) { @@ -87,16 +87,16 @@ private void deleteOldData(final String name) { try { SearchHits searchHits = connection.getClient() .search(new SearchRequest(INDEX_HISTORY) - .types(TYPE) - .searchType(SearchType.QUERY_THEN_FETCH) - .source(new SearchSourceBuilder() - .query(QueryBuilders.termQuery("name.keyword", name)) - .sort(SortBuilders.fieldSort(updatedAt).order(SortOrder.DESC)) - .from(10) - .size(9000)), - RequestOptions.DEFAULT) + .types(TYPE) + .searchType(SearchType.QUERY_THEN_FETCH) + .source(new SearchSourceBuilder() + .query(QueryBuilders.termQuery("name.keyword", name)) + .sort(SortBuilders.fieldSort(updatedAt).order(SortOrder.DESC)) + .from(10) + .size(9000)), + RequestOptions.DEFAULT) .getHits(); - for(SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) { + for (SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) { ConsoleV2 consoleV2 = mapper.readValue(searchHit.getSourceAsString(), ConsoleV2.class); elasticsearchConsolePersistence.deleteOldVersion(consoleV2.getId()); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseCsvProvider.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseCsvProvider.java index a6b43e004..3e65cfdca 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseCsvProvider.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseCsvProvider.java @@ -31,14 +31,14 @@ public long getSize(FlatRepresentation response, Class type, Type genericType @Override public void writeTo(FlatRepresentation response, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException { - if(null == response) { + if (null == response) { entityStream.write("No records found matching the specified criterion".getBytes()); return; } StringWriter dataBuffer = new StringWriter(); FlatToCsvConverter.convert(response, dataBuffer); entityStream.write(dataBuffer.toString() - .getBytes()); + .getBytes()); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseErrorTextProvider.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseErrorTextProvider.java index e86840177..cfebe503e 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseErrorTextProvider.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseErrorTextProvider.java @@ -18,8 +18,8 @@ public class FlatResponseErrorTextProvider implements MessageBodyWriter { @Override public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Map.class.isAssignableFrom(type) && (mediaType.toString() - .equals(MediaType.TEXT_PLAIN) || mediaType.toString() - .equals(FoxtrotExtraMediaType.TEXT_CSV)); + .equals(MediaType.TEXT_PLAIN) || mediaType.toString() + .equals(FoxtrotExtraMediaType.TEXT_CSV)); } @Override @@ -31,19 +31,19 @@ public long getSize(Map map, Class type, Type genericType, Annotation[] annot public void writeTo(Map map, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException { StringBuilder data = new StringBuilder(); - for(Object key : map.keySet()) { + for (Object key : map.keySet()) { data.append(key.toString()); data.append(":"); - if(null == map.get(key.toString())) { + if (null == map.get(key.toString())) { data.append("Check logs for more details"); } else { data.append(map.get(key.toString()) - .toString()); + .toString()); } data.append("\n"); } entityStream.write(data.toString() - .getBytes()); + .getBytes()); } public void hrLine(int length, StringBuilder stringBuilder) { diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseTextProvider.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseTextProvider.java index c7c9c8158..79387a587 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseTextProvider.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatResponseTextProvider.java @@ -33,7 +33,7 @@ public long getSize(FlatRepresentation response, Class type, Type genericType @Override public void writeTo(FlatRepresentation response, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException { - if(null == response) { + if (null == response) { entityStream.write("No records found matching the specified criterion".getBytes()); return; } @@ -41,9 +41,9 @@ public void writeTo(FlatRepresentation response, Class type, Type genericType StringBuilder data = new StringBuilder(); StringBuilder headerLineBuilder = new StringBuilder(); headerLineBuilder.append("|"); - for(FieldHeader fieldHeader : headers) { + for (FieldHeader fieldHeader : headers) { final String name = fieldHeader.getName(); - if(name.length() > fieldHeader.getMaxLength()) { + if (name.length() > fieldHeader.getMaxLength()) { fieldHeader.setMaxLength(name.length()); } headerLineBuilder.append(" "); @@ -56,22 +56,22 @@ public void writeTo(FlatRepresentation response, Class type, Type genericType data.append(headerLine); hrLine(headerLine.length(), data); List> rows = response.getRows(); - for(Map row : rows) { + for (Map row : rows) { StringBuilder rowBuilder = new StringBuilder(); rowBuilder.append("|"); - for(FieldHeader fieldHeader : headers) { + for (FieldHeader fieldHeader : headers) { rowBuilder.append(" "); rowBuilder.append(String.format("%" + fieldHeader.getMaxLength() + "s", row.get(fieldHeader.getName()))); rowBuilder.append(" |"); } rowBuilder.append("\n"); data.append(rowBuilder.toString() - .replaceAll("\"", " ") - .replaceAll("null", " ")); + .replaceAll("\"", " ") + .replaceAll("null", " ")); } hrLine(headerLine.length(), data); entityStream.write(data.toString() - .getBytes()); + .getBytes()); } public void hrLine(int length, StringBuilder stringBuilder) { diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatToCsvConverter.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatToCsvConverter.java index 2eb0bb4b2..aff4ba30a 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatToCsvConverter.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FlatToCsvConverter.java @@ -11,24 +11,25 @@ public class FlatToCsvConverter { - private FlatToCsvConverter() {} + private FlatToCsvConverter() { + } public static void convert(final FlatRepresentation representation, Writer writer) throws IOException { CSVWriter data = new CSVWriter(writer); List headers = representation.getHeaders(); String[] headerNames = new String[headers.size()]; int i = 0; - for(FieldHeader fieldHeader : headers) { + for (FieldHeader fieldHeader : headers) { headerNames[i++] = fieldHeader.getName(); } data.writeNext(headerNames); List> rows = representation.getRows(); - for(Map row : rows) { + for (Map row : rows) { String[] rowData = new String[headers.size()]; i = 0; - for(FieldHeader fieldHeader : headers) { - if(row.containsKey(fieldHeader.getName())) { + for (FieldHeader fieldHeader : headers) { + if (row.containsKey(fieldHeader.getName())) { rowData[i++] = row.get(fieldHeader.getName()) .toString() .replaceAll("\"", "") diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FoxtrotExtraMediaType.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FoxtrotExtraMediaType.java index 9aca0db95..6883dd3e2 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FoxtrotExtraMediaType.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/providers/FoxtrotExtraMediaType.java @@ -4,5 +4,6 @@ public class FoxtrotExtraMediaType { public static final String TEXT_CSV = "text/csv"; - private FoxtrotExtraMediaType() {} + private FoxtrotExtraMediaType() { + } } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/Auth.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/Auth.java index e9ece20e9..57f0dd218 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/Auth.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/Auth.java @@ -44,7 +44,7 @@ public Auth(Provider authProvider, AuthConfig authConfig) { @Consumes(MediaType.APPLICATION_JSON) public Response provisionUser(@NotNull @Valid final CreateUserRequest createUserRequest) { val user = new User(createUserRequest.getId(), createUserRequest.getRoles(), createUserRequest.getTables(), - createUserRequest.isSystem(), new Date(), new Date()); + createUserRequest.isSystem(), new Date(), new Date()); return Response.ok(authProvider.get().provisionUser(user)).build(); } @@ -57,7 +57,7 @@ public Response getUser(@NotNull @NotEmpty @PathParam("userId") final String use @PUT @Path("/users/{userId}/roles/grant/{role}") public Response grantRole(@NotNull @NotEmpty @PathParam("userId") final String userId, - @NotNull @PathParam("role") final FoxtrotRole role) { + @NotNull @PathParam("role") final FoxtrotRole role) { val status = authProvider.get() .grantRole(userId, role); return updateUserResponse(userId, status); @@ -66,7 +66,7 @@ public Response grantRole(@NotNull @NotEmpty @PathParam("userId") final String u @PUT @Path("/users/{userId}/roles/revoke/{role}") public Response revokeRole(@NotNull @NotEmpty @PathParam("userId") final String userId, - @NotNull @PathParam("role") final FoxtrotRole role) { + @NotNull @PathParam("role") final FoxtrotRole role) { val status = authProvider.get() .revokeRole(userId, role); return updateUserResponse(userId, status); @@ -75,7 +75,7 @@ public Response revokeRole(@NotNull @NotEmpty @PathParam("userId") final String @PUT @Path("/users/{userId}/tables/access/grant/{table}") public Response grantTableAccess(@NotNull @NotEmpty @PathParam("userId") final String userId, - @NotNull @NotEmpty @PathParam("table") final String table) { + @NotNull @NotEmpty @PathParam("table") final String table) { val status = authProvider.get() .grantTableAccess(userId, table); return updateUserResponse(userId, status); @@ -94,7 +94,7 @@ public Response revokeTableAccess(@NotNull @NotEmpty @PathParam("userId") final @Path("/users/{userId}") public Response deleteUser(@NotNull @NotEmpty @PathParam("userId") final String userId) { final boolean status = authProvider.get().deleteUser(userId); - if(!status) { + if (!status) { return Response.notModified().build(); } return Response.ok().build(); @@ -104,7 +104,7 @@ public Response deleteUser(@NotNull @NotEmpty @PathParam("userId") final String @Path("/tokens/{userId}") public Response provisionToken(@NotNull @NotEmpty @PathParam("userId") final String userId) { val token = authProvider.get().provisionToken(userId, TokenType.STATIC, null).orElse(null); - if(null == token) { + if (null == token) { return Response.notModified().build(); } return Response @@ -122,9 +122,9 @@ public Response getToken(@NotNull @NotEmpty @PathParam("tokenId") final String t @DELETE @Path("/tokens/{userId}") public Response deleteToken(@NotNull @NotEmpty @PathParam("userId") final String userId, - @NotNull @NotEmpty @PathParam("tokenId") final String tokenId) { + @NotNull @NotEmpty @PathParam("tokenId") final String tokenId) { val status = authProvider.get().deleteToken(tokenId); - if(!status) { + if (!status) { return Response.notModified().build(); } return Response.ok().build(); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/DocumentResource.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/DocumentResource.java index db94011e0..bc879c6f3 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/DocumentResource.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/DocumentResource.java @@ -65,10 +65,10 @@ public DocumentResource(QueryStore queryStore, TableTranslator tableTranslator) @ApiOperation("Save Document") public Response saveDocument(@PathParam("table") String table, @Valid final Document document) { String tableName = tableTranslator.getTable(table, document); - if(tableName != null) { + if (tableName != null) { queryStore.save(tableName, document); } - if(tableName != null && !table.equals(tableName)) { + if (tableName != null && !table.equals(tableName)) { queryStore.save(table, document); } return Response.created(URI.create("/" + document.getId())) @@ -83,7 +83,7 @@ public Response saveDocument(@PathParam("table") String table, @Valid final Docu @ApiOperation("Save list of documents") public Response saveDocuments(@PathParam("table") String table, @Valid final List documents) { Map> tableVsDocuments = getTableVsDocuments(table, documents); - for(Map.Entry> entry : CollectionUtils.nullSafeSet(tableVsDocuments.entrySet())) { + for (Map.Entry> entry : CollectionUtils.nullSafeSet(tableVsDocuments.entrySet())) { queryStore.save(entry.getKey(), entry.getValue()); } return Response.created(URI.create("/" + table)) @@ -112,11 +112,11 @@ public Response getDocuments(@PathParam("table") final String table, @QueryParam private Map> getTableVsDocuments(String table, List documents) { Map> tableVsDocuments = new HashMap<>(); - if(tableTranslator.isTransformableTable(table)) { - for(Document document : CollectionUtils.nullSafeList(documents)) { + if (tableTranslator.isTransformableTable(table)) { + for (Document document : CollectionUtils.nullSafeList(documents)) { String tableName = tableTranslator.getTable(table, document); - if(tableVsDocuments.containsKey(tableName)) { + if (tableVsDocuments.containsKey(tableName)) { tableVsDocuments.get(tableName) .add(document); } else { diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/ESClusterResource.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/ESClusterResource.java index 2a0423af7..1f0a4822e 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/ESClusterResource.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/ESClusterResource.java @@ -2,12 +2,14 @@ import com.flipkart.foxtrot.core.reroute.ClusterRerouteManager; import io.swagger.annotations.Api; + import javax.inject.Inject; import javax.inject.Singleton; import javax.ws.rs.Consumes; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; + import ru.vyarus.dropwizard.guice.module.installer.order.Order; /*** diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/HbaseRegionsMergeResource.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/HbaseRegionsMergeResource.java index a98a1a4df..9530d0dad 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/HbaseRegionsMergeResource.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/HbaseRegionsMergeResource.java @@ -41,7 +41,7 @@ public HbaseRegionsMergeResource(HbaseConfig hbaseConfig) { @Timed @ApiOperation("Get all Hbase regions which can be merged") public Map>> listMergableRegions(@PathParam("table") final String tableName, - @PathParam("threshSizeInGB") @Min(0) final double threshSizeInGB) { + @PathParam("threshSizeInGB") @Min(0) final double threshSizeInGB) { return Collections.singletonMap("regions", hbaseRegions.getMergeableRegions(TableName.valueOf(tableName), threshSizeInGB)); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/OAuth.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/OAuth.java index b4c17048e..6ca221627 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/OAuth.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/OAuth.java @@ -53,11 +53,11 @@ public Response login( .redirectionURL(sessionId); log.info("Redirection uri: {}", redirectionURL); final String cookieReferrerUrl = null == cookieReferrer - ? null - : cookieReferrer.getValue(); + ? null + : cookieReferrer.getValue(); val source = Strings.isNullOrEmpty(cookieReferrerUrl) - ? referrer - : cookieReferrerUrl; + ? referrer + : cookieReferrerUrl; log.debug("Call source: {} Referrer: {} Redirection: {}", source, referrer, cookieReferrerUrl); if (!Strings.isNullOrEmpty(source)) { sessionDataStore.get() @@ -109,13 +109,13 @@ public Response handleGoogleCallback( } log.debug("Got: {} against session: {}", existingReferrer, authCode); final String finalRedirect = Strings.isNullOrEmpty((String) existingReferrer) - ? "/" - : (String) existingReferrer; + ? "/" + : (String) existingReferrer; log.debug("Will be redirecting to: {}. Existing: {}", finalRedirect, existingReferrer); return Response.seeOther(URI.create(finalRedirect)) .cookie(createTokenCookie(token.getIdType().equals(IdType.SESSION_ID) - ? AuthUtils.createJWT(token, authConfig.getJwt()) - : token.getId()), + ? AuthUtils.createJWT(token, authConfig.getJwt()) + : token.getId()), expireCookie(cookieState)) .build(); } @@ -132,8 +132,7 @@ public Response logout(@CookieParam("token") final Cookie token) { return Response.seeOther(URI.create("/")) .cookie(createTokenCookie("")) .build(); - } - else { + } else { log.info("Empty token in logout call"); } return Response.seeOther(URI.create("/")).build(); @@ -141,15 +140,15 @@ public Response logout(@CookieParam("token") final Cookie token) { private NewCookie createTokenCookie(String value) { return new NewCookie("token", - value, - "/", - null, - Cookie.DEFAULT_VERSION, - null, - NewCookie.DEFAULT_MAX_AGE, - null, - false, - true); + value, + "/", + null, + Cookie.DEFAULT_VERSION, + null, + NewCookie.DEFAULT_MAX_AGE, + null, + false, + true); } private NewCookie expireCookie(@CookieParam("gauth-state") Cookie cookieState) { diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResource.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResource.java index 6b4b7b6aa..9bff8ad25 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResource.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResource.java @@ -71,12 +71,12 @@ public Response getAllFields(@QueryParam("withCardinality") @DefaultValue("false @QueryParam("calculateCardinality") @DefaultValue("false") boolean calculateCardinality) { return Response.ok() .entity(tableManager.getAll() - .stream() - .collect(Collectors.toMap(Table::getName, - table -> tableMetadataManager.getFieldMappings(table.getName(), withCardinality, - calculateCardinality - ) - ))) + .stream() + .collect(Collectors.toMap(Table::getName, + table -> tableMetadataManager.getFieldMappings(table.getName(), withCardinality, + calculateCardinality + ) + ))) .build(); } diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/UtilResource.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/UtilResource.java index 9adc7ef55..e356a0b02 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/UtilResource.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/resources/UtilResource.java @@ -34,7 +34,8 @@ @Produces(MediaType.APPLICATION_JSON) @Api(value = "/v1/util") @Singleton -@PermitAll public class UtilResource { +@PermitAll +public class UtilResource { private final ElasticsearchConfig elasticsearch; private final ObjectMapper mapper; diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/AuthUtils.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/AuthUtils.java index eba410bda..c0c8c393c 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/AuthUtils.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/AuthUtils.java @@ -30,7 +30,7 @@ public static String createJWT(final Token token, final JwtConfig jwtConfig) { claims.setNotBeforeMinutesInThePast(2); claims.setSubject(token.getUserId()); claims.setAudience(token.getTokenType().name()); - if(null != token.getExpiry()) { + if (null != token.getExpiry()) { claims.setExpirationTime(NumericDate.fromMilliseconds(token.getExpiry().getTime())); } JsonWebSignature jws = new JsonWebSignature(); diff --git a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/ServerUtils.java b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/ServerUtils.java index 0bb94b0a8..f39916846 100644 --- a/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/ServerUtils.java +++ b/foxtrot-server/src/main/java/com/flipkart/foxtrot/server/utils/ServerUtils.java @@ -12,18 +12,19 @@ */ public class ServerUtils { - private ServerUtils() {} + private ServerUtils() { + } public static int port(ServerFactory serverFactory) { - if(serverFactory instanceof SimpleServerFactory) { - SimpleServerFactory simpleServerFactory = (SimpleServerFactory)serverFactory; + if (serverFactory instanceof SimpleServerFactory) { + SimpleServerFactory simpleServerFactory = (SimpleServerFactory) serverFactory; return getPortFromConnector(simpleServerFactory.getConnector()); } - if(serverFactory instanceof DefaultServerFactory) { - DefaultServerFactory defaultServerFactory = (DefaultServerFactory)serverFactory; - for(ConnectorFactory connectorFactory : defaultServerFactory.getApplicationConnectors()) { - if(connectorFactory instanceof HttpConnectorFactory) { + if (serverFactory instanceof DefaultServerFactory) { + DefaultServerFactory defaultServerFactory = (DefaultServerFactory) serverFactory; + for (ConnectorFactory connectorFactory : defaultServerFactory.getApplicationConnectors()) { + if (connectorFactory instanceof HttpConnectorFactory) { return getPortFromConnector(connectorFactory); } } @@ -33,8 +34,8 @@ public static int port(ServerFactory serverFactory) { } private static int getPortFromConnector(ConnectorFactory connectorFactory) { - if(connectorFactory instanceof HttpConnectorFactory) { - return ((HttpConnectorFactory)connectorFactory).getPort(); + if (connectorFactory instanceof HttpConnectorFactory) { + return ((HttpConnectorFactory) connectorFactory).getPort(); } throw new PortExtractionException("Cannot extract port from connector"); } diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/auth/ESAuthStoreTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/auth/ESAuthStoreTest.java index 096f8d9ef..bb00e57c1 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/auth/ESAuthStoreTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/auth/ESAuthStoreTest.java @@ -46,23 +46,23 @@ public void teardown() { public void usersTest() { final String TEST_USER = "test_user"; final User originalUser = new User(TEST_USER, - EnumSet.of(FoxtrotRole.INGEST), - Collections.emptySet(), - true, - null, - null); + EnumSet.of(FoxtrotRole.INGEST), + Collections.emptySet(), + true, + null, + null); Assert.assertEquals(originalUser, authStore.provisionUser(originalUser).orElse(null)); Assert.assertEquals(originalUser, authStore.getUser(TEST_USER).orElse(null)); Assert.assertTrue(authStore.grantRole(TEST_USER, FoxtrotRole.QUERY)); Assert.assertTrue(Sets.difference(EnumSet.of(FoxtrotRole.INGEST, FoxtrotRole.QUERY), - Objects.requireNonNull(authStore.getUser(TEST_USER) - .map(User::getRoles) - .orElse(null))).isEmpty()); + Objects.requireNonNull(authStore.getUser(TEST_USER) + .map(User::getRoles) + .orElse(null))).isEmpty()); Assert.assertTrue(authStore.revokeRole(TEST_USER, FoxtrotRole.INGEST)); Assert.assertTrue(Sets.difference(EnumSet.of(FoxtrotRole.QUERY), - Objects.requireNonNull(authStore.getUser(TEST_USER) - .map(User::getRoles) - .orElse(null))).isEmpty()); + Objects.requireNonNull(authStore.getUser(TEST_USER) + .map(User::getRoles) + .orElse(null))).isEmpty()); Assert.assertTrue(authStore.deleteUser(TEST_USER)); } @@ -70,19 +70,19 @@ public void usersTest() { public void tokensTest() { final String TEST_USER = "test_user"; Assert.assertNull(authStore.provisionToken(TEST_USER, TokenType.STATIC, null) - .orElse(null)); + .orElse(null)); final User originalUser = new User(TEST_USER, - EnumSet.of(FoxtrotRole.INGEST), - Collections.emptySet(), true,null, null); + EnumSet.of(FoxtrotRole.INGEST), + Collections.emptySet(), true, null, null); Assert.assertEquals(originalUser, authStore.provisionUser(originalUser).orElse(null)); val token = authStore.provisionToken(TEST_USER, TokenType.STATIC, null) .orElse(null); Assert.assertNotNull(token); Assert.assertEquals(TEST_USER, token.getUserId()); Assert.assertEquals(token.getId(), - Objects.requireNonNull(authStore.getToken(token.getId()).orElse(null)).getId()); + Objects.requireNonNull(authStore.getToken(token.getId()).orElse(null)).getId()); Assert.assertEquals(token.getId(), - Objects.requireNonNull(authStore.getTokenForUser(TEST_USER).orElse(null)).getId()); + Objects.requireNonNull(authStore.getTokenForUser(TEST_USER).orElse(null)).getId()); Assert.assertTrue(authStore.deleteToken(token.getId())); Assert.assertNull(authStore.getToken(token.getId()).orElse(null)); Assert.assertTrue(authStore.deleteUser(TEST_USER)); @@ -92,19 +92,19 @@ public void tokensTest() { public void tokensDeleteTest() { final String TEST_USER = "test_user"; Assert.assertNull(authStore.provisionToken(TEST_USER, TokenType.STATIC, null) - .orElse(null)); + .orElse(null)); final User originalUser = new User(TEST_USER, - EnumSet.of(FoxtrotRole.INGEST), - Collections.emptySet(), true,null, null); + EnumSet.of(FoxtrotRole.INGEST), + Collections.emptySet(), true, null, null); Assert.assertEquals(originalUser, authStore.provisionUser(originalUser).orElse(null)); val token = authStore.provisionToken(TEST_USER, TokenType.STATIC, null) .orElse(null); Assert.assertNotNull(token); Assert.assertEquals(TEST_USER, token.getUserId()); Assert.assertEquals(token.getId(), - Objects.requireNonNull(authStore.getToken(token.getId()).orElse(null)).getId()); + Objects.requireNonNull(authStore.getToken(token.getId()).orElse(null)).getId()); Assert.assertEquals(token.getId(), - Objects.requireNonNull(authStore.getTokenForUser(TEST_USER).orElse(null)).getId()); + Objects.requireNonNull(authStore.getTokenForUser(TEST_USER).orElse(null)).getId()); //Delete user and make sure tokens are deleted along with that Assert.assertTrue(authStore.deleteUser(TEST_USER)); @@ -116,8 +116,8 @@ public void expiredTokensDeleteTest() { val sessionDuration = Duration.seconds(2); final String TEST_USER = "test_user"; final User originalUser = new User(TEST_USER, - EnumSet.of(FoxtrotRole.INGEST), - Collections.emptySet(), true,null, null); + EnumSet.of(FoxtrotRole.INGEST), + Collections.emptySet(), true, null, null); Assert.assertEquals(originalUser, authStore.provisionUser(originalUser).orElse(null)); final Date testDate = new Date(); val tokenDate = new Date(testDate.getTime() - sessionDuration.toMilliseconds() - 5_000); diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/AnalyticsResourceTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/AnalyticsResourceTest.java index 84d99e2a9..da7520564 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/AnalyticsResourceTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/AnalyticsResourceTest.java @@ -164,7 +164,7 @@ public void testRunSyncAsync() throws Exception { .post(serviceUserEntity, AsyncDataToken.class); await().pollDelay(2000, TimeUnit.MILLISECONDS).until(() -> true); GroupResponse actualResponse = GroupResponse.class.cast(getCacheManager().getCacheFor(response.getAction()) - .get(response.getKey())); + .get(response.getKey())); assertEquals(expectedResponse, actualResponse.getResult()); } @@ -182,7 +182,7 @@ public void testRunSyncAsyncInvalidTable() throws Exception { .post(serviceUserEntity, AsyncDataToken.class); await().pollDelay(2000, TimeUnit.MILLISECONDS).until(() -> true); GroupResponse actualResponse = GroupResponse.class.cast(getCacheManager().getCacheFor(asyncDataToken.getAction()) - .get(asyncDataToken.getKey())); + .get(asyncDataToken.getKey())); assertEquals(expectedResponse.getResult(), actualResponse.getResult()); } } diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/ClusterHealthResourceTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/ClusterHealthResourceTest.java index 50f2a87ee..417403df7 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/ClusterHealthResourceTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/ClusterHealthResourceTest.java @@ -38,10 +38,9 @@ */ public class ClusterHealthResourceTest extends FoxtrotResourceTest { - private FoxtrotTableManager tableManager; - @Rule public ResourceTestRule resources; + private FoxtrotTableManager tableManager; public ClusterHealthResourceTest() throws Exception { super(); diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/DocumentResourceTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/DocumentResourceTest.java index 7eaa17b33..34560f6b9 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/DocumentResourceTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/DocumentResourceTest.java @@ -100,7 +100,7 @@ public void testSaveDocumentNullId() throws Exception { @Test public void testSaveDocumentNullData() throws Exception { Document document = new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), null); + .toString(), System.currentTimeMillis(), null); Entity documentEntity = Entity.json(document); Response response = resources .target("/v1/document/" + TestUtils.TEST_TABLE_NAME) @@ -192,9 +192,9 @@ public void testSaveDocumentsNullDocument() throws Exception { List documents = new Vector(); documents.add(null); documents.add(new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), getMapper().getNodeFactory() - .objectNode() - .put("d", "d"))); + .toString(), System.currentTimeMillis(), getMapper().getNodeFactory() + .objectNode() + .put("d", "d"))); Entity> listEntity = Entity.json(documents); Response response = resources .target(String.format("/v1/document/%s/bulk", TestUtils.TEST_TABLE_NAME)) @@ -221,7 +221,7 @@ public void testSaveDocumentsNullId() throws Exception { public void testSaveDocumentsNullData() throws Exception { List documents = new Vector(); documents.add(new Document(UUID.randomUUID() - .toString(), System.currentTimeMillis(), null)); + .toString(), System.currentTimeMillis(), null)); Entity> listEntity = Entity.json(documents); Response response = resources .target(String.format("/v1/document/%s/bulk", TestUtils.TEST_TABLE_NAME)) diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/FoxtrotResourceTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/FoxtrotResourceTest.java index 50f979b0a..0fa598001 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/FoxtrotResourceTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/FoxtrotResourceTest.java @@ -58,31 +58,30 @@ @Slf4j public abstract class FoxtrotResourceTest { - protected final HealthCheckRegistry healthChecks = mock(HealthCheckRegistry.class); - protected final JerseyEnvironment jerseyEnvironment = mock(JerseyEnvironment.class); - protected final LifecycleEnvironment lifecycleEnvironment = new LifecycleEnvironment(); - protected final Environment environment = mock(Environment.class); - protected final Bootstrap bootstrap = mock(Bootstrap.class); - protected final ObjectMapper objectMapper = Jackson.newObjectMapper(); - private final ObjectMapper mapper; - private final HazelcastInstance hazelcastInstance; - private final ElasticsearchConnection elasticsearchConnection; - private final TableMetadataManager tableMetadataManager; - private final CardinalityConfig cardinalityConfig; - private final List mutators; - private final CacheManager cacheManager; + static { + Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + root.setLevel(Level.WARN); + } + + protected final HealthCheckRegistry healthChecks = mock(HealthCheckRegistry.class); + protected final JerseyEnvironment jerseyEnvironment = mock(JerseyEnvironment.class); + protected final LifecycleEnvironment lifecycleEnvironment = new LifecycleEnvironment(); + protected final Environment environment = mock(Environment.class); + protected final Bootstrap bootstrap = mock(Bootstrap.class); + protected final ObjectMapper objectMapper = Jackson.newObjectMapper(); + private final ObjectMapper mapper; + private final HazelcastInstance hazelcastInstance; + private final ElasticsearchConnection elasticsearchConnection; + private final TableMetadataManager tableMetadataManager; + private final CardinalityConfig cardinalityConfig; + private final List mutators; + private final CacheManager cacheManager; private AnalyticsLoader analyticsLoader; private QueryExecutor queryExecutor; private QueryStore queryStore; private DataStore dataStore; - - static { - Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - root.setLevel(Level.WARN); - } - - protected FoxtrotResourceTest() { + protected FoxtrotResourceTest() { when(jerseyEnvironment.getResourceConfig()).thenReturn(new DropwizardResourceConfig()); when(environment.jersey()).thenReturn(jerseyEnvironment); when(environment.lifecycle()).thenReturn(lifecycleEnvironment); @@ -112,8 +111,7 @@ protected FoxtrotResourceTest() { hazelcastInstance = new TestHazelcastInstanceFactory(1).newHazelcastInstance(new Config()); try { elasticsearchConnection = ElasticsearchTestUtils.getConnection(); - } - catch (Exception e) { + } catch (Exception e) { throw new IllegalStateException(e); } ElasticsearchUtils.initializeMappings(elasticsearchConnection.getClient()); @@ -132,14 +130,13 @@ protected FoxtrotResourceTest() { tableMetadataManager = new DistributedTableMetadataManager(hazelcastConnection, elasticsearchConnection, mapper, cardinalityConfig); try { tableMetadataManager.start(); - } - catch (Exception e) { + } catch (Exception e) { throw new IllegalStateException(e); } tableMetadataManager.save(Table.builder() - .name(TestUtils.TEST_TABLE_NAME) - .ttl(7) - .build()); + .name(TestUtils.TEST_TABLE_NAME) + .ttl(7) + .build()); mutators = Lists.newArrayList( new LargeTextNodeRemover(mapper, TextNodeRemoverConfiguration.builder().build())); @@ -159,15 +156,15 @@ protected FoxtrotResourceTest() { queryExecutor = new QueryExecutor(analyticsLoader, executorService, Collections.emptyList()); } - protected TableMetadataManager getTableMetadataManager() { + protected TableMetadataManager getTableMetadataManager() { return tableMetadataManager; } - protected ElasticsearchConnection getElasticsearchConnection() { + protected ElasticsearchConnection getElasticsearchConnection() { return elasticsearchConnection; } - protected HazelcastInstance getHazelcastInstance() { + protected HazelcastInstance getHazelcastInstance() { return hazelcastInstance; } @@ -175,11 +172,11 @@ protected QueryExecutor getQueryExecutor() { return queryExecutor; } - protected ObjectMapper getMapper() { + protected ObjectMapper getMapper() { return mapper; } - protected CacheManager getCacheManager() { + protected CacheManager getCacheManager() { return cacheManager; } diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResourceTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResourceTest.java index 2759a7aad..02cca9f7b 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResourceTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableFieldMappingResourceTest.java @@ -68,53 +68,53 @@ public void testGet() throws Exception { Set mappings = new HashSet<>(); mappings.add(FieldMetadata.builder() - .field("time") - .type(FieldType.LONG) - .build()); + .field("time") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("word") - .type(FieldType.STRING) - .build()); + .field("word") + .type(FieldType.STRING) + .build()); mappings.add(FieldMetadata.builder() - .field("data.data") - .type(FieldType.STRING) - .build()); + .field("data.data") + .type(FieldType.STRING) + .build()); mappings.add(FieldMetadata.builder() - .field("header.hello") - .type(FieldType.STRING) - .build()); + .field("header.hello") + .type(FieldType.STRING) + .build()); mappings.add(FieldMetadata.builder() - .field("head.hello") - .type(FieldType.LONG) - .build()); + .field("head.hello") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.dayOfWeek") - .type(FieldType.LONG) - .build()); + .field("date.dayOfWeek") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.minuteOfDay") - .type(FieldType.LONG) - .build()); + .field("date.minuteOfDay") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.monthOfYear") - .type(FieldType.LONG) - .build()); + .field("date.monthOfYear") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.hourOfDay") - .type(FieldType.LONG) - .build()); + .field("date.hourOfDay") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.year") - .type(FieldType.LONG) - .build()); + .field("date.year") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.dayOfMonth") - .type(FieldType.LONG) - .build()); + .field("date.dayOfMonth") + .type(FieldType.LONG) + .build()); mappings.add(FieldMetadata.builder() - .field("date.minuteOfHour") - .type(FieldType.LONG) - .build()); + .field("date.minuteOfHour") + .type(FieldType.LONG) + .build()); TableFieldMapping tableFieldMapping = new TableFieldMapping(TestUtils.TEST_TABLE_NAME, mappings); String response = resources @@ -125,7 +125,7 @@ public void testGet() throws Exception { TableFieldMapping mapping = getMapper().readValue(response, TableFieldMapping.class); assertEquals(tableFieldMapping.getTable(), mapping.getTable()); assertTrue(tableFieldMapping.getMappings() - .equals(mapping.getMappings())); + .equals(mapping.getMappings())); } @Test diff --git a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableManagerResourceTest.java b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableManagerResourceTest.java index d37eccce3..3cc5124bc 100644 --- a/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableManagerResourceTest.java +++ b/foxtrot-server/src/test/java/com/flipkart/foxtrot/server/resources/TableManagerResourceTest.java @@ -110,7 +110,7 @@ public void testSaveNullTableName() throws Exception { public void testSaveBackendError() throws Exception { Table table = Table.builder() .name(UUID.randomUUID() - .toString()) + .toString()) .ttl(30) .build(); Entity
tableEntity = Entity.json(table); diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/FqlEngine.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/FqlEngine.java index 626bc7b9e..f8a7379ae 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/FqlEngine.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/FqlEngine.java @@ -73,8 +73,8 @@ public FlatRepresentation process(FqlQuery query) { public void visit(FqlDescribeTable fqlDescribeTable) { TableFieldMapping fieldMetaData = queryStore.getFieldMappings(fqlDescribeTable.getTableName()); result = FlatteningUtils.genericMultiRowParse(mapper.valueToTree(fieldMetaData.getMappings()), - Lists.newArrayList("field", "type"), "field" - ); + Lists.newArrayList("field", "type"), "field" + ); } @Override diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/MetaStatementMatcher.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/MetaStatementMatcher.java index 092a01142..59b0b00a7 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/MetaStatementMatcher.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/MetaStatementMatcher.java @@ -15,14 +15,14 @@ public class MetaStatementMatcher { private static final String SHOWTABLES_STATEMENT_MATCH = "^\\s*[sS][hH][oO][wW]\\s+[tT][aA][bB][lL][eE][sS]$"; public ExtendedSqlStatement parse(final String fql) { - if(fql.matches(DESC_STATEMENT_MATCH)) { + if (fql.matches(DESC_STATEMENT_MATCH)) { final String[] parts = fql.split(DESC_STATEMENT_DELIMITER); - if(parts.length != 2) { + if (parts.length != 2) { throw new FqlParsingException("Could not decode table name from desc statement. Table name format is: [a-zA-Z-_]+"); } return new Describe(new Table(parts[1].toLowerCase())); } - if(fql.matches(SHOWTABLES_STATEMENT_MATCH)) { + if (fql.matches(SHOWTABLES_STATEMENT_MATCH)) { return new ShowTables(); } return null; diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/QueryTranslator.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/QueryTranslator.java index e79b44cdb..679fe84eb 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/QueryTranslator.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/QueryTranslator.java @@ -70,12 +70,12 @@ public class QueryTranslator extends SqlElementVisitor { @Override public void visit(PlainSelect plainSelect) { List selectItems = plainSelect.getSelectItems(); - for(Object selectItem : selectItems) { - SelectItem selectExpressionItem = (SelectItem)selectItem; + for (Object selectItem : selectItems) { + SelectItem selectExpressionItem = (SelectItem) selectItem; FunctionReader functionReader = new FunctionReader(); selectExpressionItem.accept(functionReader); final String columnName = functionReader.columnName; - if(!Strings.isNullOrEmpty(columnName)) { + if (!Strings.isNullOrEmpty(columnName)) { selectedColumns.add(columnName); continue; } @@ -86,7 +86,7 @@ public void visit(PlainSelect plainSelect) { plainSelect.getFromItem() .accept(this); //Populate table name List groupByItems = plainSelect.getGroupByColumnReferences(); - if(null != groupByItems) { + if (null != groupByItems) { for (Expression groupByItem : CollectionUtils.nullSafeList(groupByItems)) { queryType = FqlQueryType.GROUP; if (groupByItem instanceof Column) { @@ -95,10 +95,10 @@ public void visit(PlainSelect plainSelect) { } } } - if(FqlQueryType.SELECT == queryType) { + if (FqlQueryType.SELECT == queryType) { List orderByElements = plainSelect.getOrderByElements(); resultSort = generateResultSort(orderByElements); - if(null != plainSelect.getLimit()) { + if (null != plainSelect.getLimit()) { hasLimit = true; limitFrom = plainSelect.getLimit() .getOffset(); @@ -107,7 +107,7 @@ public void visit(PlainSelect plainSelect) { } } - if(null != plainSelect.getWhere()) { + if (null != plainSelect.getWhere()) { FilterParser filterParser = new FilterParser(); plainSelect.getWhere() .accept(filterParser); @@ -119,17 +119,17 @@ public void visit(PlainSelect plainSelect) { private void handleDistinct(PlainSelect plainSelect) { List tempColumnsWithSort = generateColumnSort(plainSelect.getOrderByElements()); - if(null != plainSelect.getDistinct()) { - for(String selectedColumn : selectedColumns) { + if (null != plainSelect.getDistinct()) { + for (String selectedColumn : selectedColumns) { boolean alreadyAdded = false; - for(ResultSort columnWithSort : tempColumnsWithSort) { - if(selectedColumn.equalsIgnoreCase(columnWithSort.getField())) { + for (ResultSort columnWithSort : tempColumnsWithSort) { + if (selectedColumn.equalsIgnoreCase(columnWithSort.getField())) { columnsWithSort.add(columnWithSort); alreadyAdded = true; break; } } - if(!alreadyAdded) { + if (!alreadyAdded) { ResultSort columnWithoutSort = new ResultSort(); columnWithoutSort.setField(selectedColumn); columnWithoutSort.setOrder(ResultSort.Order.desc); @@ -157,13 +157,13 @@ public void visit(Function function) { List params = function.getParameters() .getExpressions(); - ((Expression)params.toArray()[0]).accept(this); + ((Expression) params.toArray()[0]).accept(this); } @Override public void visit(ExpressionList expressionList) { - ExpressionList expressions = (ExpressionList)expressionList.getExpressions(); - for(Object expression : expressions.getExpressions()) { + ExpressionList expressions = (ExpressionList) expressionList.getExpressions(); + for (Object expression : expressions.getExpressions()) { logger.info("Expression: {}", expression.getClass()); } } @@ -176,7 +176,7 @@ public void visit(SelectExpressionItem selectExpressionItem) { public FqlQuery translate(String sql) { ExtendedSqlStatement extendedSqlStatement = metastatementMatcher.parse(sql); - if(null != extendedSqlStatement) { + if (null != extendedSqlStatement) { ExtendedSqlParser parser = new ExtendedSqlParser(); extendedSqlStatement.receive(parser); return parser.getQuery(); @@ -189,7 +189,7 @@ public FqlQuery translate(String sql) { } catch (JSQLParserException e) { throw new FqlParsingException(e.getMessage(), e); } - Select select = (Select)statement; + Select select = (Select) statement; select.accept(this); ActionRequest request = null; switch (queryType) { @@ -232,7 +232,7 @@ public FqlQuery translate(String sql) { default: break; } - if(null == request) { + if (null == request) { throw new FqlParsingException("Could not parse provided FQL."); } return new FqlActionQuery(request, selectedColumns); @@ -242,9 +242,9 @@ private ActionRequest createSelectActionRequest() { Query query = new Query(); query.setTable(tableName); query.setSort(resultSort); - if(hasLimit) { - query.setFrom((int)limitFrom); - query.setLimit((int)limitCount); + if (hasLimit) { + query.setFrom((int) limitFrom); + query.setLimit((int) limitCount); } query.setFilters(filters); return query; @@ -260,35 +260,35 @@ private ActionRequest createGroupActionRequest() { } private ActionRequest createTrendActionRequest() { - TrendRequest trend = (TrendRequest)calledAction; + TrendRequest trend = (TrendRequest) calledAction; trend.setTable(tableName); trend.setFilters(filters); return trend; } private ActionRequest createStatsTrendActionRequest() { - StatsTrendRequest statsTrend = (StatsTrendRequest)calledAction; + StatsTrendRequest statsTrend = (StatsTrendRequest) calledAction; statsTrend.setTable(tableName); statsTrend.setFilters(filters); return statsTrend; } private ActionRequest createStatsActionRequest() { - StatsRequest stats = (StatsRequest)calledAction; + StatsRequest stats = (StatsRequest) calledAction; stats.setTable(tableName); stats.setFilters(filters); return stats; } private ActionRequest createHistogramActionRequest() { - HistogramRequest histogram = (HistogramRequest)calledAction; + HistogramRequest histogram = (HistogramRequest) calledAction; histogram.setTable(tableName); histogram.setFilters(filters); return histogram; } private ActionRequest createCountActionRequest() { - CountRequest countRequest = (CountRequest)calledAction; + CountRequest countRequest = (CountRequest) calledAction; countRequest.setTable(tableName); countRequest.setFilters(filters); return countRequest; @@ -304,11 +304,11 @@ private ActionRequest createDistinctActionRequest() { private ResultSort generateResultSort(List orderByElements) { - if(CollectionUtils.isEmpty(orderByElements)) { + if (CollectionUtils.isEmpty(orderByElements)) { return null; } OrderByElement orderByElement = orderByElements.get(0); - Column sortColumn = (Column)orderByElement.getExpression(); + Column sortColumn = (Column) orderByElement.getExpression(); ResultSort resultSortColumn = new ResultSort(); resultSortColumn.setField(sortColumn.getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT)); resultSortColumn.setOrder(orderByElement.isAsc() ? ResultSort.Order.asc : ResultSort.Order.desc); @@ -317,15 +317,15 @@ private ResultSort generateResultSort(List orderByElements) { } private void setGroupAggregation(GroupRequest group) { - if(calledAction instanceof CountRequest) { - CountRequest countRequest = (CountRequest)this.calledAction; - if(countRequest.isDistinct()) { + if (calledAction instanceof CountRequest) { + CountRequest countRequest = (CountRequest) this.calledAction; + if (countRequest.isDistinct()) { group.setUniqueCountOn(countRequest.getField()); } else { group.setAggregationType(Stat.COUNT); group.setAggregationField(countRequest.getField()); } - } else if (calledAction instanceof StatsRequest){ + } else if (calledAction instanceof StatsRequest) { StatsRequest statsRequest = (StatsRequest) this.calledAction; group.setAggregationType(statsRequest.getStats().stream().findFirst().orElse(Stat.COUNT)); group.setAggregationField(statsRequest.getField()); @@ -334,11 +334,11 @@ private void setGroupAggregation(GroupRequest group) { private List generateColumnSort(List orderItems) { List resultSortList = Lists.newArrayList(); - if(orderItems == null || orderItems.isEmpty()) { + if (orderItems == null || orderItems.isEmpty()) { return resultSortList; } - for(OrderByElement orderByElement : orderItems) { - Column sortColumn = (Column)orderByElement.getExpression(); + for (OrderByElement orderByElement : orderItems) { + Column sortColumn = (Column) orderByElement.getExpression(); ResultSort resultSortColumn = new ResultSort(); resultSortColumn.setField(sortColumn.getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT)); resultSortColumn.setOrder(orderByElement.isAsc() ? ResultSort.Order.asc : ResultSort.Order.desc); @@ -355,46 +355,46 @@ private static final class FunctionReader extends SqlElementVisitor { @Override public void visit(SelectExpressionItem selectExpressionItem) { Expression expression = selectExpressionItem.getExpression(); - if(expression instanceof Function) { - Function function = (Function)expression; + if (expression instanceof Function) { + Function function = (Function) expression; String functionName = function.getName().replaceAll(REGEX, REPLACEMENT); queryType = getType(functionName); switch (queryType) { case TREND: actionRequest = parseTrendFunction(function.getParameters() - .getExpressions()); + .getExpressions()); break; case STATSTREND: actionRequest = parseStatsTrendFunction(function.getParameters() - .getExpressions()); + .getExpressions()); break; case STATS: actionRequest = parseStatsFunction(function.getParameters() - .getExpressions()); + .getExpressions()); break; case HISTOGRAM: actionRequest = parseHistogramRequest(function.getParameters()); break; case COUNT: actionRequest = parseCountRequest(function.getParameters(), - function.isAllColumns(), - function.isDistinct()); + function.isAllColumns(), + function.isDistinct()); break; case SUM: actionRequest = parseStatsFunction(function.getParameters() - .getExpressions(), Sets.newHashSet(Stat.SUM)); + .getExpressions(), Sets.newHashSet(Stat.SUM)); break; case MIN: actionRequest = parseStatsFunction(function.getParameters() - .getExpressions(), Sets.newHashSet(Stat.MIN)); + .getExpressions(), Sets.newHashSet(Stat.MIN)); break; case MAX: actionRequest = parseStatsFunction(function.getParameters() - .getExpressions(), Sets.newHashSet(Stat.MAX)); + .getExpressions(), Sets.newHashSet(Stat.MAX)); break; case AVG: actionRequest = parseStatsFunction(function.getParameters() - .getExpressions(), Sets.newHashSet(Stat.AVG)); + .getExpressions(), Sets.newHashSet(Stat.AVG)); break; case DESC: case SELECT: @@ -405,70 +405,70 @@ public void visit(SelectExpressionItem selectExpressionItem) { } } else { - if(expression instanceof Parenthesis) { - columnName = ((Column)((Parenthesis)expression).getExpression()).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); - } else if(expression instanceof Column) { - columnName = ((Column)expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); + if (expression instanceof Parenthesis) { + columnName = ((Column) ((Parenthesis) expression).getExpression()).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); + } else if (expression instanceof Column) { + columnName = ((Column) expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); } } } private FqlQueryType getType(String function) { - if(function.equalsIgnoreCase(FqlFunctionType.TREND)){ + if (function.equalsIgnoreCase(FqlFunctionType.TREND)) { return FqlQueryType.TREND; } - if(function.equalsIgnoreCase(FqlFunctionType.STATSTREND)) { + if (function.equalsIgnoreCase(FqlFunctionType.STATSTREND)) { return FqlQueryType.STATSTREND; } - if(function.equalsIgnoreCase(FqlFunctionType.STATS)) { + if (function.equalsIgnoreCase(FqlFunctionType.STATS)) { return FqlQueryType.STATS; } - if(function.equalsIgnoreCase(FqlFunctionType.HISTOGRAM)) { + if (function.equalsIgnoreCase(FqlFunctionType.HISTOGRAM)) { return FqlQueryType.HISTOGRAM; } - if(function.equalsIgnoreCase(FqlFunctionType.COUNT)) { + if (function.equalsIgnoreCase(FqlFunctionType.COUNT)) { return FqlQueryType.COUNT; } - if(function.equalsIgnoreCase(FqlFunctionType.SUM)) { + if (function.equalsIgnoreCase(FqlFunctionType.SUM)) { return FqlQueryType.SUM; } - if(function.equalsIgnoreCase(FqlFunctionType.AVG)) { + if (function.equalsIgnoreCase(FqlFunctionType.AVG)) { return FqlQueryType.AVG; } - if(function.equalsIgnoreCase(FqlFunctionType.MIN)) { + if (function.equalsIgnoreCase(FqlFunctionType.MIN)) { return FqlQueryType.MIN; } - if(function.equalsIgnoreCase(FqlFunctionType.MAX)) { + if (function.equalsIgnoreCase(FqlFunctionType.MAX)) { return FqlQueryType.MAX; } return FqlQueryType.SELECT; } private TrendRequest parseTrendFunction(List expressions) { - if(expressions == null || expressions.isEmpty() || expressions.size() > 3) { + if (expressions == null || expressions.isEmpty() || expressions.size() > 3) { throw new FqlParsingException("trend function has following format: trend(fieldname, [period, [timestamp field]])"); } TrendRequest trendRequest = new TrendRequest(); - trendRequest.setField(QueryUtils.expressionToString((Expression)expressions.get(0))); - if(expressions.size() > 1) { - trendRequest.setPeriod(Period.valueOf(QueryUtils.expressionToString((Expression)expressions.get(1)) - .toLowerCase())); + trendRequest.setField(QueryUtils.expressionToString((Expression) expressions.get(0))); + if (expressions.size() > 1) { + trendRequest.setPeriod(Period.valueOf(QueryUtils.expressionToString((Expression) expressions.get(1)) + .toLowerCase())); } - if(expressions.size() > 2) { - trendRequest.setTimestamp(QueryUtils.expressionToString((Expression)expressions.get(2))); + if (expressions.size() > 2) { + trendRequest.setTimestamp(QueryUtils.expressionToString((Expression) expressions.get(2))); } return trendRequest; } private StatsTrendRequest parseStatsTrendFunction(List expressions) { - if(expressions == null || expressions.isEmpty() || expressions.size() > 2) { + if (expressions == null || expressions.isEmpty() || expressions.size() > 2) { throw new FqlParsingException("statstrend function has following format: statstrend(fieldname, [period])"); } StatsTrendRequest statsTrendRequest = new StatsTrendRequest(); - statsTrendRequest.setField(QueryUtils.expressionToString((Expression)expressions.get(0))); - if(expressions.size() > 1) { - statsTrendRequest.setPeriod(Period.valueOf(QueryUtils.expressionToString((Expression)expressions.get(1)) - .toLowerCase())); + statsTrendRequest.setField(QueryUtils.expressionToString((Expression) expressions.get(0))); + if (expressions.size() > 1) { + statsTrendRequest.setPeriod(Period.valueOf(QueryUtils.expressionToString((Expression) expressions.get(1)) + .toLowerCase())); } return statsTrendRequest; } @@ -484,26 +484,26 @@ private StatsRequest parseStatsFunction(List expressions, Set stats) { } private StatsRequest parseStatsFunction(List expressions) { - if(expressions == null || expressions.isEmpty() || expressions.size() > 1) { + if (expressions == null || expressions.isEmpty() || expressions.size() > 1) { throw new FqlParsingException("stats function has following format: stats(fieldname)"); } StatsRequest statsRequest = new StatsRequest(); - statsRequest.setField(QueryUtils.expressionToString((Expression)expressions.get(0))); + statsRequest.setField(QueryUtils.expressionToString((Expression) expressions.get(0))); return statsRequest; } private HistogramRequest parseHistogramRequest(ExpressionList expressionList) { - if(expressionList != null && (expressionList.getExpressions() != null && expressionList.getExpressions() - .size() > 2)) { + if (expressionList != null && (expressionList.getExpressions() != null && expressionList.getExpressions() + .size() > 2)) { throw new FqlParsingException("histogram function has the following format: histogram([period, [timestamp field]])"); } HistogramRequest histogramRequest = new HistogramRequest(); - if(null != expressionList) { + if (null != expressionList) { List expressions = expressionList.getExpressions(); - histogramRequest.setPeriod(Period.valueOf(QueryUtils.expressionToString((Expression)expressions.get(0)) - .toLowerCase())); - if(expressions.size() > 1) { - histogramRequest.setField(QueryUtils.expressionToString((Expression)expressions.get(1))); + histogramRequest.setPeriod(Period.valueOf(QueryUtils.expressionToString((Expression) expressions.get(0)) + .toLowerCase())); + if (expressions.size() > 1) { + histogramRequest.setField(QueryUtils.expressionToString((Expression) expressions.get(1))); } } return histogramRequest; @@ -512,13 +512,13 @@ private HistogramRequest parseHistogramRequest(ExpressionList expressionList) { private ActionRequest parseCountRequest(ExpressionList expressionList, boolean allColumns, boolean isDistinct) { CountRequest countRequest = new CountRequest(); - if(allColumns) { + if (allColumns) { countRequest.setField(null); return countRequest; } - if(expressionList != null && (expressionList.getExpressions() != null && expressionList.getExpressions() - .size() == 1)) { + if (expressionList != null && (expressionList.getExpressions() != null && expressionList.getExpressions() + .size() == 1)) { List expressions = expressionList.getExpressions(); countRequest.setField(expressionToString(expressions.get(0))); countRequest.setDistinct(isDistinct); @@ -528,11 +528,11 @@ private ActionRequest parseCountRequest(ExpressionList expressionList, boolean a } private String expressionToString(Expression expression) { - if(expression instanceof Column) { - return ((Column)expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); + if (expression instanceof Column) { + return ((Column) expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); } - if(expression instanceof StringValue) { - return ((StringValue)expression).getValue().replaceAll(REGEX, REPLACEMENT); + if (expression instanceof StringValue) { + return ((StringValue) expression).getValue().replaceAll(REGEX, REPLACEMENT); } return null; } @@ -546,7 +546,7 @@ private static final class FilterParser extends SqlElementVisitor { @Override public void visit(EqualsTo equalsTo) { EqualsFilter equalsFilter = new EqualsFilter(); - String field = ((Column)equalsTo.getLeftExpression()).getFullyQualifiedName(); + String field = ((Column) equalsTo.getLeftExpression()).getFullyQualifiedName(); equalsFilter.setField(field.replaceAll(SQL_FIELD_REGEX, "")); equalsFilter.setValue(getValueFromExpression(equalsTo.getRightExpression())); filters.add(equalsFilter); @@ -555,7 +555,7 @@ public void visit(EqualsTo equalsTo) { @Override public void visit(NotEqualsTo notEqualsTo) { NotEqualsFilter notEqualsFilter = new NotEqualsFilter(); - String field = ((Column)notEqualsTo.getLeftExpression()).getFullyQualifiedName(); + String field = ((Column) notEqualsTo.getLeftExpression()).getFullyQualifiedName(); notEqualsFilter.setField(field.replaceAll(SQL_FIELD_REGEX, "")); notEqualsFilter.setValue(getValueFromExpression(notEqualsTo.getRightExpression())); filters.add(notEqualsFilter); @@ -574,7 +574,7 @@ public void visit(Between between) { BetweenFilter betweenFilter = new BetweenFilter(); ColumnData columnData = setupColumn(between.getLeftExpression()); betweenFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); betweenFilter.setTemporal(columnData.isTemporal()); Number from = getNumbericValue(between.getBetweenExpressionStart()); Number to = getNumbericValue(between.getBetweenExpressionEnd()); @@ -588,7 +588,7 @@ public void visit(GreaterThan greaterThan) { GreaterThanFilter greaterThanFilter = new GreaterThanFilter(); ColumnData columnData = setupColumn(greaterThan.getLeftExpression()); greaterThanFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); greaterThanFilter.setTemporal(columnData.isTemporal()); greaterThanFilter.setValue(getNumbericValue(greaterThan.getRightExpression())); filters.add(greaterThanFilter); @@ -599,7 +599,7 @@ public void visit(GreaterThanEquals greaterThanEquals) { GreaterEqualFilter greaterEqualFilter = new GreaterEqualFilter(); ColumnData columnData = setupColumn(greaterThanEquals.getLeftExpression()); greaterEqualFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); greaterEqualFilter.setTemporal(columnData.isTemporal()); greaterEqualFilter.setValue(getNumbericValue(greaterThanEquals.getRightExpression())); filters.add(greaterEqualFilter); @@ -608,28 +608,28 @@ public void visit(GreaterThanEquals greaterThanEquals) { @Override public void visit(InExpression inExpression) { InFilter inFilter = new InFilter(); - inFilter.setField(((Column)inExpression.getLeftExpression()).getFullyQualifiedName() - .replaceAll(SQL_FIELD_REGEX, "")); + inFilter.setField(((Column) inExpression.getLeftExpression()).getFullyQualifiedName() + .replaceAll(SQL_FIELD_REGEX, "")); ItemsList itemsList = inExpression.getRightItemsList(); - if(!(itemsList instanceof ExpressionList)) { + if (!(itemsList instanceof ExpressionList)) { throw new FqlParsingException("Sub selects not supported"); } - ExpressionList expressionList = (ExpressionList)itemsList; + ExpressionList expressionList = (ExpressionList) itemsList; List filterValues = Lists.newArrayList(); - for(Expression expression : expressionList.getExpressions()) { + for (Expression expression : expressionList.getExpressions()) { filterValues.add(getValueFromExpression(expression)); } - if(inExpression.isNot()) { + if (inExpression.isNot()) { NotInFilter filter = new NotInFilter(); - filter.setField(((Column)inExpression.getLeftExpression()).getFullyQualifiedName() - .replaceAll(SQL_FIELD_REGEX, "")); + filter.setField(((Column) inExpression.getLeftExpression()).getFullyQualifiedName() + .replaceAll(SQL_FIELD_REGEX, "")); filter.setValues(filterValues); filters.add(filter); } else { InFilter filter = new InFilter(); - filter.setField(((Column)inExpression.getLeftExpression()).getFullyQualifiedName() - .replaceAll(SQL_FIELD_REGEX, "")); + filter.setField(((Column) inExpression.getLeftExpression()).getFullyQualifiedName() + .replaceAll(SQL_FIELD_REGEX, "")); filter.setValues(filterValues); filters.add(filter); } @@ -639,16 +639,16 @@ public void visit(InExpression inExpression) { public void visit(IsNullExpression isNullExpression) { super.visit(isNullExpression); ColumnData columnData = setupColumn(isNullExpression.getLeftExpression()); - if(isNullExpression.isNot()) { + if (isNullExpression.isNot()) { ExistsFilter existsFilter = new ExistsFilter(); existsFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); filters.add(existsFilter); } else { MissingFilter missingFilter = new MissingFilter(); missingFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); filters.add(missingFilter); } } @@ -658,8 +658,8 @@ public void visit(LikeExpression likeExpression) { super.visit(likeExpression); ContainsFilter containsFilter = new ContainsFilter(); containsFilter.setValue(getStringValue(likeExpression.getRightExpression())); - containsFilter.setField(((Column)likeExpression.getLeftExpression()).getFullyQualifiedName() - .replaceAll(SQL_FIELD_REGEX, "")); + containsFilter.setField(((Column) likeExpression.getLeftExpression()).getFullyQualifiedName() + .replaceAll(SQL_FIELD_REGEX, "")); filters.add(containsFilter); } @@ -668,7 +668,7 @@ public void visit(MinorThan minorThan) { LessThanFilter lessThanFilter = new LessThanFilter(); ColumnData columnData = setupColumn(minorThan.getLeftExpression()); lessThanFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); lessThanFilter.setTemporal(columnData.isTemporal()); lessThanFilter.setValue(getNumbericValue(minorThan.getRightExpression())); filters.add(lessThanFilter); @@ -680,7 +680,7 @@ public void visit(MinorThanEquals minorThanEquals) { LessEqualFilter lessEqualFilter = new LessEqualFilter(); ColumnData columnData = setupColumn(minorThanEquals.getLeftExpression()); lessEqualFilter.setField(columnData.getColumnName() - .replaceAll(SQL_FIELD_REGEX, "")); + .replaceAll(SQL_FIELD_REGEX, "")); lessEqualFilter.setTemporal(columnData.isTemporal()); lessEqualFilter.setValue(getNumbericValue(minorThanEquals.getRightExpression())); filters.add(lessEqualFilter); @@ -688,10 +688,10 @@ public void visit(MinorThanEquals minorThanEquals) { @Override public void visit(Function function) { - if(function.getName() + if (function.getName() .equalsIgnoreCase("last")) { LastFilter lastFilter = parseWindowFunction(function.getParameters() - .getExpressions()); + .getExpressions()); filters.add(lastFilter); return; } @@ -700,49 +700,49 @@ public void visit(Function function) { private LastFilter parseWindowFunction(List expressions) { - if(expressions == null || expressions.isEmpty() || expressions.size() > 3) { + if (expressions == null || expressions.isEmpty() || expressions.size() > 3) { throw new FqlParsingException("last function has following format: last(duration, [start-time, [timestamp field]])"); } LastFilter lastFilter = new LastFilter(); - lastFilter.setDuration(Duration.parse(QueryUtils.expressionToString((Expression)expressions.get(0)))); - if(expressions.size() > 1) { - lastFilter.setCurrentTime(QueryUtils.expressionToNumber((Expression)expressions.get(1)) - .longValue()); + lastFilter.setDuration(Duration.parse(QueryUtils.expressionToString((Expression) expressions.get(0)))); + if (expressions.size() > 1) { + lastFilter.setCurrentTime(QueryUtils.expressionToNumber((Expression) expressions.get(1)) + .longValue()); } - if(expressions.size() > 2) { - lastFilter.setField(QueryUtils.expressionToString((Expression)expressions.get(2)) - .replaceAll(SQL_FIELD_REGEX, "")); + if (expressions.size() > 2) { + lastFilter.setField(QueryUtils.expressionToString((Expression) expressions.get(2)) + .replaceAll(SQL_FIELD_REGEX, "")); } return lastFilter; } private Object getValueFromExpression(Expression expression) { - if(expression instanceof StringValue) { - return ((StringValue)expression).getValue(); + if (expression instanceof StringValue) { + return ((StringValue) expression).getValue(); } return getNumbericValue(expression); } private String getStringValue(Expression expression) { - if(expression instanceof StringValue) { - return ((StringValue)expression).getValue(); + if (expression instanceof StringValue) { + return ((StringValue) expression).getValue(); } throw new FqlParsingException("Unsupported value type."); } private Number getNumbericValue(Expression expression) { - if(expression instanceof DoubleValue) { - return ((DoubleValue)expression).getValue(); + if (expression instanceof DoubleValue) { + return ((DoubleValue) expression).getValue(); } - if(expression instanceof LongValue) { - return ((LongValue)expression).getValue(); + if (expression instanceof LongValue) { + return ((LongValue) expression).getValue(); } - if(expression instanceof DateValue) { - return ((DateValue)expression).getValue() + if (expression instanceof DateValue) { + return ((DateValue) expression).getValue() .getTime(); } - if(expression instanceof TimeValue) { - return ((TimeValue)expression).getValue() + if (expression instanceof TimeValue) { + return ((TimeValue) expression).getValue() .getTime(); } throw new FqlParsingException("Unsupported value type."); @@ -750,21 +750,21 @@ private Number getNumbericValue(Expression expression) { } private ColumnData setupColumn(Expression expression) { - if(expression instanceof Function) { - Function function = (Function)expression; - if(function.getName() + if (expression instanceof Function) { + Function function = (Function) expression; + if (function.getName() .equalsIgnoreCase("temporal")) { List parameters = function.getParameters() .getExpressions(); - if(parameters.size() != 1 || !(parameters.get(0) instanceof Column)) { + if (parameters.size() != 1 || !(parameters.get(0) instanceof Column)) { throw new FqlParsingException("temporal function must have a fieldname as parameter"); } - return ColumnData.temporal(((Column)parameters.get(0)).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT)); + return ColumnData.temporal(((Column) parameters.get(0)).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT)); } throw new FqlParsingException("Only the function 'temporal' is supported in where clause"); } - if(expression instanceof Column) { - return new ColumnData(((Column)expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT)); + if (expression instanceof Column) { + return new ColumnData(((Column) expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT)); } throw new FqlParsingException("Only the function 'temporal([fieldname)' and fieldname is supported in where clause"); } @@ -804,7 +804,7 @@ private static final class ExtendedSqlParser extends SqlElementVisitor { @Override public void visit(Describe describe) { query = new FqlDescribeTable(describe.getTable() - .getName()); + .getName()); } @Override diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/SqlElementVisitor.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/SqlElementVisitor.java index 3a5f7338a..29b84a840 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/SqlElementVisitor.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/SqlElementVisitor.java @@ -27,7 +27,7 @@ public class SqlElementVisitor implements StatementVisitor, SelectVisitor, FromItemVisitor, ItemsListVisitor, ExpressionVisitor, SelectItemVisitor, - ExtendedSqlStatementVisitor { + ExtendedSqlStatementVisitor { private void invalid(Object object) { throw new FqlParsingException("Unsupported construct: " + object.getClass() .getSimpleName()); diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/fqlstore/FqlStoreServiceImpl.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/fqlstore/FqlStoreServiceImpl.java index d3b7201d2..bf4e0697a 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/fqlstore/FqlStoreServiceImpl.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/fqlstore/FqlStoreServiceImpl.java @@ -46,11 +46,11 @@ public FqlStoreServiceImpl(ElasticsearchConnection elasticsearchConnection, Obje @Override public void save(FqlStore fqlStore) { fqlStore.setId(UUID.randomUUID() - .toString()); + .toString()); try { elasticsearchConnection.getClient() .index(new IndexRequest(FQL_STORE_INDEX, DOCUMENT_TYPE_NAME, fqlStore.getId()) - .source(objectMapper.writeValueAsBytes(fqlStore), XContentType.JSON), RequestOptions.DEFAULT); + .source(objectMapper.writeValueAsBytes(fqlStore), XContentType.JSON), RequestOptions.DEFAULT); logger.info("Saved FQL Query : {}", fqlStore.getQuery()); } catch (Exception e) { throw new FqlPersistenceException("Couldn't save FQL query: " + fqlStore.getQuery() + " Error Message: " + e.getMessage(), e); @@ -64,15 +64,15 @@ public List get(FqlGetRequest fqlGetRequest) { try { searchHits = elasticsearchConnection.getClient() .search(new SearchRequest(FQL_STORE_INDEX) - .types(DOCUMENT_TYPE_NAME) - .searchType(SearchType.QUERY_THEN_FETCH) - .source(new SearchSourceBuilder() - .query(QueryBuilders.prefixQuery(TITLE_FIELD, fqlGetRequest.getTitle().toLowerCase())) - .from(fqlGetRequest.getFrom()) - .size(fqlGetRequest.getSize())), + .types(DOCUMENT_TYPE_NAME) + .searchType(SearchType.QUERY_THEN_FETCH) + .source(new SearchSourceBuilder() + .query(QueryBuilders.prefixQuery(TITLE_FIELD, fqlGetRequest.getTitle().toLowerCase())) + .from(fqlGetRequest.getFrom()) + .size(fqlGetRequest.getSize())), RequestOptions.DEFAULT) .getHits(); - for(SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) { + for (SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) { fqlStoreList.add(objectMapper.readValue(searchHit.getSourceAsString(), FqlStore.class)); } } catch (Exception e) { diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/Flattener.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/Flattener.java index 2c0d384e4..c33e1d848 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/Flattener.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/Flattener.java @@ -50,21 +50,21 @@ public void visit(GroupResponse groupResponse) { final String separator = "__SEPARATOR__"; Map fieldNames = Maps.newTreeMap(); Map dataFields = generateFieldMappings(null, objectMapper.valueToTree(groupResponse.getResult()), separator); - GroupRequest groupRequest = (GroupRequest)request; + GroupRequest groupRequest = (GroupRequest) request; String statsHeader = getStatsHeader(groupRequest); List> rows = Lists.newArrayList(); - for(Map.Entry groupData : dataFields.entrySet()) { + for (Map.Entry groupData : dataFields.entrySet()) { String[] values = groupData.getKey() .split(separator); Map row = Maps.newHashMap(); - for(int i = 0; i < groupRequest.getNesting() + for (int i = 0; i < groupRequest.getNesting() .size(); i++) { final String fieldName = groupRequest.getNesting() .get(i); row.put(fieldName, values[i]); - if(!fieldNames.containsKey(fieldName)) { + if (!fieldNames.containsKey(fieldName)) { fieldNames.put(fieldName, 0); } fieldNames.put(fieldName, lengthMax(fieldNames.get(fieldName), values[i])); @@ -75,7 +75,7 @@ public void visit(GroupResponse groupResponse) { } fieldNames.put(statsHeader, 10); List headers = Lists.newArrayList(); - for(String fieldName : groupRequest.getNesting()) { + for (String fieldName : groupRequest.getNesting()) { headers.add(new FieldHeader(fieldName, fieldNames.get(fieldName))); } headers.add(new FieldHeader(statsHeader, 10)); @@ -84,7 +84,7 @@ public void visit(GroupResponse groupResponse) { private String getStatsHeader(GroupRequest groupRequest) { String statsHeader = Utils.COUNT; - if(Objects.nonNull(groupRequest.getAggregationType())){ + if (Objects.nonNull(groupRequest.getAggregationType())) { statsHeader = statsString(groupRequest.getAggregationType()); } return statsHeader; @@ -95,14 +95,14 @@ private String getStatsHeader(GroupRequest groupRequest) { public void visit(HistogramResponse histogramResponse) { List> rows = Lists.newArrayList(); rows.addAll(histogramResponse.getCounts() - .stream() - .map(count -> { - Map map = new HashMap<>(); - map.put("timestamp", count.getPeriod()); - map.put(COUNT, count.getCount()); - return map; - }) - .collect(Collectors.toList())); + .stream() + .map(count -> { + Map map = new HashMap<>(); + map.put("timestamp", count.getPeriod()); + map.put(COUNT, count.getCount()); + return map; + }) + .collect(Collectors.toList())); List headers = Lists.newArrayList(); headers.add(new FieldHeader("timestamp", 15)); @@ -116,18 +116,18 @@ public void visit(QueryResponse queryResponse) { List> rows = Lists.newArrayList(); Set fieldToLookup = (null == fieldsToReturn) ? Collections.emptySet() : new HashSet<>(fieldsToReturn); boolean isAllFields = fieldToLookup.isEmpty(); - for(Document document : queryResponse.getDocuments()) { + for (Document document : queryResponse.getDocuments()) { Map docFields = generateFieldMappings(null, objectMapper.valueToTree(document)); Map row = Maps.newTreeMap(); - for(Map.Entry docField : docFields.entrySet()) { + for (Map.Entry docField : docFields.entrySet()) { String fieldName = docField.getKey(); String prettyFieldName = fieldName.replaceFirst("data.", ""); - if(!isAllFields && !fieldToLookup.contains(prettyFieldName)) { + if (!isAllFields && !fieldToLookup.contains(prettyFieldName)) { continue; } row.put(prettyFieldName, docField.getValue() .getData()); - if(!fieldNames.containsKey(prettyFieldName)) { + if (!fieldNames.containsKey(prettyFieldName)) { fieldNames.put(prettyFieldName, 0); } fieldNames.put(prettyFieldName, Math.max(fieldNames.get(prettyFieldName), docField.getValue() @@ -135,7 +135,7 @@ public void visit(QueryResponse queryResponse) { } rows.add(row); } - if(!rows.isEmpty()) { + if (!rows.isEmpty()) { flatRepresentation = new FlatRepresentation("query", getFieldsFromList(fieldNames), rows); } } @@ -191,7 +191,7 @@ public void visit(StatsTrendResponse statsTrendResponse) { public void visit(TrendResponse trendResponse) { List headers = Lists.newArrayListWithCapacity(3); JsonNode root = objectMapper.valueToTree(trendResponse.getTrends()); - if(null == root || !root.isObject()) { + if (null == root || !root.isObject()) { return; } List types = Lists.newArrayList(); @@ -203,10 +203,10 @@ public void visit(TrendResponse trendResponse) { String typeName = typeNameIt.next(); types.add(typeName); typeNameMaxLength = Math.max(typeNameMaxLength, typeName.length()); - for(JsonNode dataNode : root.get(typeName)) { + for (JsonNode dataNode : root.get(typeName)) { final String time = Long.toString(dataNode.get("period") - .asLong()); - if(!representation.containsKey(time)) { + .asLong()); + if (!representation.containsKey(time)) { representation.put(time, Maps.newHashMap()); } representation.get(time) @@ -216,13 +216,13 @@ public void visit(TrendResponse trendResponse) { } headers.add(new FieldHeader("time", 20)); - for(String type : types) { + for (String type : types) { headers.add(new FieldHeader(type, 20)); } - for(Map.Entry> element : representation.entrySet()) { + for (Map.Entry> element : representation.entrySet()) { Map row = Maps.newTreeMap(); row.put("time", element.getKey()); - for(Map.Entry data : element.getValue() + for (Map.Entry data : element.getValue() .entrySet()) { row.put(data.getKey(), data.getValue()); } @@ -242,16 +242,16 @@ public void visit(CountResponse countResponse) { @Override public void visit(DistinctResponse distinctResponse) { List fieldHeaders = Lists.newArrayList(); - for(String header : distinctResponse.getHeaders()) { + for (String header : distinctResponse.getHeaders()) { fieldHeaders.add(new FieldHeader(header, 10)); } List> distinctResponseRows = distinctResponse.getResult(); List> rows = Lists.newArrayList(); - for(List responseRow : distinctResponseRows) { + for (List responseRow : distinctResponseRows) { Map row = Maps.newHashMap(); - for(int i = 0; i < fieldHeaders.size(); i++) { + for (int i = 0; i < fieldHeaders.size(); i++) { row.put(fieldHeaders.get(i) - .getName(), responseRow.get(i)); + .getName(), responseRow.get(i)); } rows.add(row); } @@ -278,12 +278,12 @@ private int lengthMax(int currMax, final String rhs) { private List getFieldsFromList(Map fieldNames) { List headers = Lists.newArrayList(); - if(null == fieldsToReturn || fieldsToReturn.isEmpty()) { - for(Map.Entry entry : fieldNames.entrySet()) { + if (null == fieldsToReturn || fieldsToReturn.isEmpty()) { + for (Map.Entry entry : fieldNames.entrySet()) { headers.add(new FieldHeader(entry.getKey(), entry.getValue())); } } else { - for(String fieldName : fieldsToReturn) { + for (String fieldName : fieldsToReturn) { headers.add(new FieldHeader(fieldName, fieldNames.get(fieldName))); } } diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/FlatteningUtils.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/FlatteningUtils.java index 59d591594..fb465883e 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/FlatteningUtils.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/FlatteningUtils.java @@ -16,13 +16,14 @@ public class FlatteningUtils { private static final String DEFAULT_SEPARATOR = "."; - private FlatteningUtils() {} + private FlatteningUtils() { + } public static FlatRepresentation genericParse(JsonNode response) { List headers = Lists.newArrayList(); Map docFields = generateFieldMappings(null, response); Map row = Maps.newTreeMap(); - for(Map.Entry docField : docFields.entrySet()) { + for (Map.Entry docField : docFields.entrySet()) { row.put(docField.getKey(), docField.getValue() .getData()); headers.add(new FieldHeader(docField.getKey(), 20)); @@ -35,22 +36,22 @@ public static FlatRepresentation genericMultiRowParse(JsonNode response, final L List> rows = Lists.newArrayList(); Map headerData = Maps.newTreeMap(); - for(JsonNode arrayElement : response) { + for (JsonNode arrayElement : response) { Map element = generateFieldMappings(null, arrayElement); Map row = Maps.newHashMap(); - for(Map.Entry elementData : element.entrySet()) { - if(!headerData.containsKey(elementData.getKey())) { + for (Map.Entry elementData : element.entrySet()) { + if (!headerData.containsKey(elementData.getKey())) { headerData.put(elementData.getKey(), elementData.getValue() .getLength()); } headerData.put(elementData.getKey(), Math.max(elementData.getValue() - .getLength(), headerData.get(elementData.getKey()))); + .getLength(), headerData.get(elementData.getKey()))); row.put(elementData.getKey(), elementData.getValue() .getData()); } rows.add(row); } - if(!Strings.isNullOrEmpty(sortField)) { + if (!Strings.isNullOrEmpty(sortField)) { rows.sort(Comparator.comparing((Map row) -> row.get(sortField) .toString())); } @@ -60,14 +61,14 @@ public static FlatRepresentation genericMultiRowParse(JsonNode response, final L } private static void populateHeaders(List predefinedHeaders, Map headerData, List headers) { - if(!CollectionUtils.isNullOrEmpty(predefinedHeaders)) { - for(String predefinedHeader : predefinedHeaders) { - if(headerData.containsKey(predefinedHeader)) { + if (!CollectionUtils.isNullOrEmpty(predefinedHeaders)) { + for (String predefinedHeader : predefinedHeaders) { + if (headerData.containsKey(predefinedHeader)) { headers.add(new FieldHeader(predefinedHeader, headerData.get(predefinedHeader))); } } } else { - for(Map.Entry entry : headerData.entrySet()) { + for (Map.Entry entry : headerData.entrySet()) { headers.add(new FieldHeader(entry.getKey(), entry.getValue())); } } @@ -79,14 +80,14 @@ public static Map generateFieldMappings(String parentField, Js public static Map generateFieldMappings(String parentField, JsonNode jsonNode, final String separator) { Map fields = Maps.newTreeMap(); - if(null == jsonNode) { + if (null == jsonNode) { log.info("NULL for {}", parentField); return Collections.emptyMap(); } - if(jsonNode.isArray()) { + if (jsonNode.isArray()) { int index = 0; - for(JsonNode arrayElement : jsonNode) { - if(!isArrayOrObject(arrayElement)) { + for (JsonNode arrayElement : jsonNode) { + if (!isArrayOrObject(arrayElement)) { fields.put(parentField + separator + Integer.toString(index), new MetaData(arrayElement, arrayElement.toString() .length())); } else { @@ -100,9 +101,9 @@ public static Map generateFieldMappings(String parentField, Js while (iterator.hasNext()) { Map.Entry entry = iterator.next(); String currentField = (parentField == null) ? entry.getKey() : (String.format("%s%s%s", parentField, separator, - entry.getKey() - )); - if(isArrayOrObject(entry.getValue())) { + entry.getKey() + )); + if (isArrayOrObject(entry.getValue())) { fields.putAll(generateFieldMappings(currentField, entry.getValue(), separator)); } else { fields.put(currentField, new MetaData(entry.getValue(), entry.getValue() diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/model/FieldHeader.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/model/FieldHeader.java index a1ca9f070..06fde5991 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/model/FieldHeader.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/responseprocessors/model/FieldHeader.java @@ -30,12 +30,12 @@ public void setMaxLength(int maxLength) { @Override public boolean equals(Object o) { - if(this == o) + if (this == o) return true; - if(o == null || getClass() != o.getClass()) + if (o == null || getClass() != o.getClass()) return false; - FieldHeader that = (FieldHeader)o; + FieldHeader that = (FieldHeader) o; return name.equals(that.name); } diff --git a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/util/QueryUtils.java b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/util/QueryUtils.java index c7eba47f2..1e45083c4 100644 --- a/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/util/QueryUtils.java +++ b/foxtrot-sql/src/main/java/com/flipkart/foxtrot/sql/util/QueryUtils.java @@ -11,27 +11,28 @@ public class QueryUtils { - private QueryUtils() {} + private QueryUtils() { + } public static String expressionToString(Expression expression) { - if(expression instanceof Column) { - return ((Column)expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); + if (expression instanceof Column) { + return ((Column) expression).getFullyQualifiedName().replaceAll(REGEX, REPLACEMENT); } - if(expression instanceof StringValue) { - return ((StringValue)expression).getValue().replaceAll(REGEX, REPLACEMENT); + if (expression instanceof StringValue) { + return ((StringValue) expression).getValue().replaceAll(REGEX, REPLACEMENT); } return null; } public static Number expressionToNumber(Expression expression) { - if(expression instanceof StringValue) { - return Long.valueOf(((StringValue)expression).getValue()); + if (expression instanceof StringValue) { + return Long.valueOf(((StringValue) expression).getValue()); } - if(expression instanceof LongValue) { - return ((LongValue)expression).getValue(); + if (expression instanceof LongValue) { + return ((LongValue) expression).getValue(); } - if(expression instanceof DoubleValue) { - return ((DoubleValue)expression).getValue(); + if (expression instanceof DoubleValue) { + return ((DoubleValue) expression).getValue(); } return null; } diff --git a/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/MetaStatementMatcherTest.java b/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/MetaStatementMatcherTest.java index 8107a8ca7..aa3855094 100644 --- a/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/MetaStatementMatcherTest.java +++ b/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/MetaStatementMatcherTest.java @@ -14,7 +14,7 @@ public void testParseDescPositive1() throws Exception { ExtendedSqlStatement extendedSqlStatement = metaStatementMatcher.parse("desc europa"); Assert.assertNotNull(extendedSqlStatement); Assert.assertTrue(extendedSqlStatement instanceof Describe); - Describe describe = (Describe)extendedSqlStatement; + Describe describe = (Describe) extendedSqlStatement; Assert.assertEquals("europa", describe.getTable() .getName()); } @@ -24,7 +24,7 @@ public void testParseDescPositive2() throws Exception { ExtendedSqlStatement extendedSqlStatement = metaStatementMatcher.parse(" \tDeSc \tEuRopa"); Assert.assertNotNull(extendedSqlStatement); Assert.assertTrue(extendedSqlStatement instanceof Describe); - Describe describe = (Describe)extendedSqlStatement; + Describe describe = (Describe) extendedSqlStatement; Assert.assertEquals("europa", describe.getTable() .getName()); } diff --git a/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/ParseTest.java b/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/ParseTest.java index 31bafbc5d..45c01a1ab 100644 --- a/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/ParseTest.java +++ b/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/ParseTest.java @@ -117,7 +117,7 @@ public void testGroupAggregationSumQueryParsing() { .contains("date.hourOfDay")); Assert.assertEquals("eventData.amount", groupRequest.getAggregationField()); Assert.assertNotNull(groupRequest.getAggregationType()); - Assert.assertEquals(groupRequest.getAggregationType(),Stat.SUM); + Assert.assertEquals(groupRequest.getAggregationType(), Stat.SUM); } @@ -157,7 +157,7 @@ public void testGroupAggregationCountQueryParsing() { Assert.assertEquals("eventData.amount", groupRequest.getAggregationField()); Assert.assertNotNull(groupRequest.getAggregationType()); Assert.assertNull(groupRequest.getUniqueCountOn()); - Assert.assertEquals(groupRequest.getAggregationType(),Stat.COUNT); + Assert.assertEquals(groupRequest.getAggregationType(), Stat.COUNT); } @Test @@ -178,7 +178,7 @@ public void testGroupAggregationAvgQueryParsing() { Assert.assertNotNull(groupRequest.getAggregationType()); Assert.assertNull(groupRequest.getUniqueCountOn()); - Assert.assertEquals(groupRequest.getAggregationType(),Stat.AVG); + Assert.assertEquals(groupRequest.getAggregationType(), Stat.AVG); } @@ -199,7 +199,7 @@ public void testGroupAggregationMinQueryParsing() { Assert.assertEquals("eventData.amount", groupRequest.getAggregationField()); Assert.assertNotNull(groupRequest.getAggregationType()); Assert.assertNull(groupRequest.getUniqueCountOn()); - Assert.assertEquals(groupRequest.getAggregationType(),Stat.MIN); + Assert.assertEquals(groupRequest.getAggregationType(), Stat.MIN); } @@ -219,7 +219,7 @@ public void testGroupAggregationMaxQueryParsing() { .contains("date.hourOfDay")); Assert.assertNull(groupRequest.getUniqueCountOn()); Assert.assertEquals("eventData.amount", groupRequest.getAggregationField()); - Assert.assertEquals(groupRequest.getAggregationType(),Stat.MAX); + Assert.assertEquals(groupRequest.getAggregationType(), Stat.MAX); } } diff --git a/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/TestsMultiRowParse.java b/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/TestsMultiRowParse.java index f530b7060..dad159afe 100644 --- a/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/TestsMultiRowParse.java +++ b/foxtrot-sql/src/test/java/com/flipkart/foxtrot/sql/TestsMultiRowParse.java @@ -17,31 +17,31 @@ public void parseMeta() throws Exception { objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter(); final String json = "{\"table\":\"europa\",\"mappings\":[{\"field\":\"data.checkoutId\",\"type\":\"STRING\"}," + - "{\"field\":\"data.errorMessage\",\"type\":\"STRING\"},{\"field\":\"data.startTime\"," + - "\"type\":\"LONG\"},{\"field\":\"data.error\",\"type\":\"STRING\"},{\"field\":\"data" + ".pin\"," + - "\"type\":\"LONG\"},{\"field\":\"data.version\",\"type\":\"STRING\"},{\"field\":\"data" + - ".invalidationReason\",\"type\":\"STRING\"},{\"field\":\"data.request.clientHostName\"," + - "\"type\":\"STRING\"},{\"field\":\"data.storedValueType\",\"type\":\"STRING\"}," + "{\"field\":\"data" + - ".request.clientTraceId\",\"type\":\"STRING\"},{\"field\":\"data.accountId\"," + "\"type\":\"STRING\"}," + - "{\"field\":\"header.profile\",\"type\":\"STRING\"},{\"field\":\"data.endTime\"," + "\"type\":\"LONG\"}," + - "{\"field\":\"data.channel.sessionId\",\"type\":\"STRING\"},{\"field\":\"data.channel" + ".salesChannel\"," + - "\"type\":\"STRING\"},{\"field\":\"data.storedValue\",\"type\":\"STRING\"}," + "{\"field\":\"data" + - ".duration\",\"type\":\"LONG\"},{\"field\":\"data.invalidationMessage\"," + "\"type\":\"STRING\"}," + - "{\"field\":\"data.channel.terminalId\",\"type\":\"STRING\"},{\"field\":\"data.email\"," + - "\"type\":\"STRING\"},{\"field\":\"data.channel.channelSource\",\"type\":\"STRING\"}," + - "{\"field\":\"header.configName\",\"type\":\"STRING\"},{\"field\":\"header.appName\"," + - "\"type\":\"STRING\"},{\"field\":\"data.errorCode\",\"type\":\"STRING\"}," + "{\"field\":\"header" + - ".timestamp\",\"type\":\"DATE\"},{\"field\":\"data.request.requestId\"," + "\"type\":\"STRING\"}," + - "{\"field\":\"data.request.clientAppIPAddress\",\"type\":\"STRING\"},{\"field\":\"data" + ".key\"," + - "\"type\":\"STRING\"},{\"field\":\"data.channel.createdBy\",\"type\":\"STRING\"}," + "{\"field\":\"data" + - ".callistoAPI\",\"type\":\"STRING\"},{\"field\":\"data.addressId\",\"type\":\"STRING\"}," + - "{\"field\":\"data.channel.userAgent\",\"type\":\"STRING\"},{\"field\":\"data" + ".salesChannel\"," + - "\"type\":\"STRING\"},{\"field\":\"header.instanceId\",\"type\":\"STRING\"}," + "{\"field\":\"data.request" + - ".checkoutId\",\"type\":\"STRING\"},{\"field\":\"data.checkoutType\"," + "\"type\":\"STRING\"}," + - "{\"field\":\"data.channel.userIp\",\"type\":\"STRING\"},{\"field\":\"data.tenant\"," + - "\"type\":\"STRING\"},{\"field\":\"header.eventId\",\"type\":\"STRING\"}," + "{\"field\":\"data.actionType" + - ".type\",\"type\":\"STRING\"},{\"field\":\"data.request.client\",\"type\":\"STRING\"}," + - "{\"field\":\"data.dataFlow\",\"type\":\"STRING\"}]}"; + "{\"field\":\"data.errorMessage\",\"type\":\"STRING\"},{\"field\":\"data.startTime\"," + + "\"type\":\"LONG\"},{\"field\":\"data.error\",\"type\":\"STRING\"},{\"field\":\"data" + ".pin\"," + + "\"type\":\"LONG\"},{\"field\":\"data.version\",\"type\":\"STRING\"},{\"field\":\"data" + + ".invalidationReason\",\"type\":\"STRING\"},{\"field\":\"data.request.clientHostName\"," + + "\"type\":\"STRING\"},{\"field\":\"data.storedValueType\",\"type\":\"STRING\"}," + "{\"field\":\"data" + + ".request.clientTraceId\",\"type\":\"STRING\"},{\"field\":\"data.accountId\"," + "\"type\":\"STRING\"}," + + "{\"field\":\"header.profile\",\"type\":\"STRING\"},{\"field\":\"data.endTime\"," + "\"type\":\"LONG\"}," + + "{\"field\":\"data.channel.sessionId\",\"type\":\"STRING\"},{\"field\":\"data.channel" + ".salesChannel\"," + + "\"type\":\"STRING\"},{\"field\":\"data.storedValue\",\"type\":\"STRING\"}," + "{\"field\":\"data" + + ".duration\",\"type\":\"LONG\"},{\"field\":\"data.invalidationMessage\"," + "\"type\":\"STRING\"}," + + "{\"field\":\"data.channel.terminalId\",\"type\":\"STRING\"},{\"field\":\"data.email\"," + + "\"type\":\"STRING\"},{\"field\":\"data.channel.channelSource\",\"type\":\"STRING\"}," + + "{\"field\":\"header.configName\",\"type\":\"STRING\"},{\"field\":\"header.appName\"," + + "\"type\":\"STRING\"},{\"field\":\"data.errorCode\",\"type\":\"STRING\"}," + "{\"field\":\"header" + + ".timestamp\",\"type\":\"DATE\"},{\"field\":\"data.request.requestId\"," + "\"type\":\"STRING\"}," + + "{\"field\":\"data.request.clientAppIPAddress\",\"type\":\"STRING\"},{\"field\":\"data" + ".key\"," + + "\"type\":\"STRING\"},{\"field\":\"data.channel.createdBy\",\"type\":\"STRING\"}," + "{\"field\":\"data" + + ".callistoAPI\",\"type\":\"STRING\"},{\"field\":\"data.addressId\",\"type\":\"STRING\"}," + + "{\"field\":\"data.channel.userAgent\",\"type\":\"STRING\"},{\"field\":\"data" + ".salesChannel\"," + + "\"type\":\"STRING\"},{\"field\":\"header.instanceId\",\"type\":\"STRING\"}," + "{\"field\":\"data.request" + + ".checkoutId\",\"type\":\"STRING\"},{\"field\":\"data.checkoutType\"," + "\"type\":\"STRING\"}," + + "{\"field\":\"data.channel.userIp\",\"type\":\"STRING\"},{\"field\":\"data.tenant\"," + + "\"type\":\"STRING\"},{\"field\":\"header.eventId\",\"type\":\"STRING\"}," + "{\"field\":\"data.actionType" + + ".type\",\"type\":\"STRING\"},{\"field\":\"data.request.client\",\"type\":\"STRING\"}," + + "{\"field\":\"data.dataFlow\",\"type\":\"STRING\"}]}"; JsonNode root = objectMapper.readTree(json); FlatRepresentation representation = FlatteningUtils.genericMultiRowParse(root.get("mappings"), null, "field"); System.out.println(writer.writeValueAsString(representation)); @@ -56,14 +56,14 @@ public void parseTable() throws Exception { objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter(); final String json = "[{\"name\":\"60\",\"ttl\":15},{\"name\":\"aprameya\",\"ttl\":15},{\"name\":\"athena\",\"ttl\":7}," + - "{\"name\":\"callisto\",\"ttl\":30},{\"name\":\"cart-service\",\"ttl\":15}," + - "{\"name\":\"digital-ingestion-pipeline\",\"ttl\":60},{\"name\":\"ebooks-delivery\",\"ttl\":15}," + - "{\"name\":\"europa\",\"ttl\":60},{\"name\":\"fk-w3-co-prezente\",\"ttl\":15}," + - "{\"name\":\"fk-w3-uss\",\"ttl\":60},{\"name\":\"flip-sync\",\"ttl\":30},{\"name\":\"flipcast\"," + - "\"ttl\":30},{\"name\":\"flipkart-ebook\",\"ttl\":60},{\"name\":\"flipkart-ebooks\",\"ttl\":30}," + - "{\"name\":\"flipkartretailapp\",\"ttl\":30},{\"name\":\"flipkartretailapp-referral\",\"ttl\":60}," + - "{\"name\":\"ganymede\",\"ttl\":30},{\"name\":\"santa\",\"ttl\":30},{\"name\":\"selfserve\"," + - "\"ttl\":15},{\"name\":\"warehouse\",\"ttl\":30},{\"name\":\"webreader\",\"ttl\":60}]"; + "{\"name\":\"callisto\",\"ttl\":30},{\"name\":\"cart-service\",\"ttl\":15}," + + "{\"name\":\"digital-ingestion-pipeline\",\"ttl\":60},{\"name\":\"ebooks-delivery\",\"ttl\":15}," + + "{\"name\":\"europa\",\"ttl\":60},{\"name\":\"fk-w3-co-prezente\",\"ttl\":15}," + + "{\"name\":\"fk-w3-uss\",\"ttl\":60},{\"name\":\"flip-sync\",\"ttl\":30},{\"name\":\"flipcast\"," + + "\"ttl\":30},{\"name\":\"flipkart-ebook\",\"ttl\":60},{\"name\":\"flipkart-ebooks\",\"ttl\":30}," + + "{\"name\":\"flipkartretailapp\",\"ttl\":30},{\"name\":\"flipkartretailapp-referral\",\"ttl\":60}," + + "{\"name\":\"ganymede\",\"ttl\":30},{\"name\":\"santa\",\"ttl\":30},{\"name\":\"selfserve\"," + + "\"ttl\":15},{\"name\":\"warehouse\",\"ttl\":30},{\"name\":\"webreader\",\"ttl\":60}]"; JsonNode root = objectMapper.readTree(json); FlatRepresentation representation = FlatteningUtils.genericMultiRowParse(root, null, "name"); System.out.println(writer.writeValueAsString(representation)); diff --git a/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/DocumentTranslator.java b/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/DocumentTranslator.java index 2363418db..88f59343e 100644 --- a/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/DocumentTranslator.java +++ b/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/DocumentTranslator.java @@ -95,8 +95,8 @@ private String rawStorageIdFromDocument(final Table table, final Document docume return document.getId() + ":" + table.getName(); case "2.0": return String.format("%s:%020d:%s:%s", table.getName(), document.getTimestamp(), document.getId(), - Constants.RAW_KEY_VERSION_TO_SUFFIX_MAP.get(rawKeyVersion) - ); + Constants.RAW_KEY_VERSION_TO_SUFFIX_MAP.get(rawKeyVersion) + ); default: throw new IllegalArgumentException(String.format(EXCEPTION_MESSAGE, rawKeyVersion)); } @@ -108,7 +108,7 @@ public String generateScalableKey(String id) { } public String rawStorageIdFromDocumentId(Table table, String id) { - if(id.endsWith(Constants.RAW_KEY_VERSION_TO_SUFFIX_MAP.get("2.0"))) { + if (id.endsWith(Constants.RAW_KEY_VERSION_TO_SUFFIX_MAP.get("2.0"))) { return id; } diff --git a/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/TableTranslator.java b/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/TableTranslator.java index ad28c8070..72b135cc1 100644 --- a/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/TableTranslator.java +++ b/foxtrot-translator/src/main/java/com/foxtrot/flipkart/translator/TableTranslator.java @@ -22,11 +22,11 @@ public class TableTranslator { @Inject public TableTranslator(SegregationConfiguration segregationConfiguration) { - if(segregationConfiguration != null) { + if (segregationConfiguration != null) { segregationConfiguration.getTableSegregationConfigs().forEach(tableSegregationConfig -> { tableVsSegregationConfig.putIfAbsent(tableSegregationConfig.getOldTable(), tableSegregationConfig); tableSegregationConfig.getNewTableVsEventTypes().forEach((newTable, eventTypes) -> { - if(CollectionUtils.isNotEmpty(eventTypes)) { + if (CollectionUtils.isNotEmpty(eventTypes)) { eventTypes.forEach(s -> eventTypeVsNewTable.putIfAbsent(s, newTable)); } }); @@ -36,10 +36,10 @@ public TableTranslator(SegregationConfiguration segregationConfiguration) { } public String getTable(String table, Document document) { - if(!isTransformableTable(table)) { + if (!isTransformableTable(table)) { return table; } - if(document.getData() + if (document.getData() .has(EVENT_TYPE)) { String eventType = document.getData() .get(EVENT_TYPE) diff --git a/foxtrot-translator/src/test/java/com/foxtrot/flipkart/translator/DocumentTranslatorTest.java b/foxtrot-translator/src/test/java/com/foxtrot/flipkart/translator/DocumentTranslatorTest.java index adf39efec..e7454ee8a 100644 --- a/foxtrot-translator/src/test/java/com/foxtrot/flipkart/translator/DocumentTranslatorTest.java +++ b/foxtrot-translator/src/test/java/com/foxtrot/flipkart/translator/DocumentTranslatorTest.java @@ -20,7 +20,7 @@ public class DocumentTranslatorTest { public void testInvalidRawKeyVersion() { TranslatorConfig translatorConfig = new TranslatorConfig(); translatorConfig.setRawKeyVersion(UUID.randomUUID() - .toString()); + .toString()); new DocumentTranslator(translatorConfig); } @@ -31,23 +31,23 @@ public void testTranslationWithNullRawKeyVersion() { DocumentTranslator translator = new DocumentTranslator(hbaseConfig); Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); Document document = new Document(); document.setId(UUID.randomUUID() .toString()); document.setTimestamp(System.currentTimeMillis()); document.setData(mapper.createObjectNode() - .put("name", NAME)); + .put("name", NAME)); Document translatedDocument = translator.translate(table, document); assertEquals(translatedDocument.getId(), document.getId()); assertNotNull(translatedDocument.getMetadata()); assertEquals(translatedDocument.getMetadata() - .getId(), document.getId()); + .getId(), document.getId()); assertEquals(translatedDocument.getMetadata() - .getRawStorageId(), document.getId() + ":" + table.getName()); + .getRawStorageId(), document.getId() + ":" + table.getName()); } @Test @@ -55,23 +55,23 @@ public void testTranslationWithRawKeyVersion1() { DocumentTranslator translator = new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()); Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); Document document = new Document(); document.setId(UUID.randomUUID() - .toString()); + .toString()); document.setTimestamp(System.currentTimeMillis()); document.setData(mapper.createObjectNode() - .put("name", NAME)); + .put("name", NAME)); Document translatedDocument = translator.translate(table, document); assertEquals(translatedDocument.getId(), document.getId()); assertNotNull(translatedDocument.getMetadata()); assertEquals(translatedDocument.getMetadata() - .getId(), document.getId()); + .getId(), document.getId()); assertEquals(translatedDocument.getMetadata() - .getRawStorageId(), document.getId() + ":" + table.getName()); + .getRawStorageId(), document.getId() + ":" + table.getName()); } @Test @@ -79,14 +79,14 @@ public void testTranslationWithRawKeyVersion2() { DocumentTranslator translator = new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV2()); Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); Document document = new Document(); document.setId(UUID.randomUUID() - .toString()); + .toString()); document.setTimestamp(System.currentTimeMillis()); document.setData(mapper.createObjectNode() - .put("name", NAME)); + .put("name", NAME)); Document translatedDocument = translator.translate(table, document); @@ -94,10 +94,10 @@ public void testTranslationWithRawKeyVersion2() { assertEquals(translatedDocument.getId(), translatedDocument.getMetadata() .getRawStorageId()); assertEquals(translatedDocument.getMetadata() - .getId(), document.getId()); + .getId(), document.getId()); assertTrue(translatedDocument.getMetadata() - .getRawStorageId() - .endsWith(Constants.RAW_KEY_VERSION_TO_SUFFIX_MAP.get("2.0"))); + .getRawStorageId() + .endsWith(Constants.RAW_KEY_VERSION_TO_SUFFIX_MAP.get("2.0"))); } @Test @@ -105,14 +105,14 @@ public void testTranslationBackWithRawKeyVersion1() { DocumentTranslator translator = new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()); Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); Document document = new Document(); document.setId(UUID.randomUUID() - .toString()); + .toString()); document.setTimestamp(System.currentTimeMillis()); document.setData(mapper.createObjectNode() - .put("name", NAME)); + .put("name", NAME)); Document translatedDocument = translator.translate(table, document); @@ -130,14 +130,14 @@ public void testTranslationBackWithRawKeyVersion2() { DocumentTranslator translator = new DocumentTranslator(translatorConfig); Table table = new Table(); table.setName(UUID.randomUUID() - .toString()); + .toString()); Document document = new Document(); document.setId(UUID.randomUUID() - .toString()); + .toString()); document.setTimestamp(System.currentTimeMillis()); document.setData(mapper.createObjectNode() - .put("name", NAME)); + .put("name", NAME)); Document translatedDocument = translator.translate(table, document);